blob: 0fc7b6d1d67164c576cc5423d4be272f684f05c7 [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"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000035#include "parser.h"
36#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000038#include "scopes.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000039#include "virtual-frame-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
kasperl@chromium.org71affb52009-05-26 05:44:31 +000041namespace v8 {
42namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
ager@chromium.org65dad4b2009-04-23 08:48:43 +000044#define __ ACCESS_MASM(masm_)
45
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000046static void EmitIdenticalObjectComparison(MacroAssembler* masm,
47 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000048 Condition cc,
49 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000050static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000051 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000052 Label* slow,
53 bool strict);
54static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
55static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000056static void MultiplyByKnownInt(MacroAssembler* masm,
57 Register source,
58 Register destination,
59 int known_int);
60static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000061
62
63
ager@chromium.orge2902be2009-06-08 12:21:35 +000064// -------------------------------------------------------------------------
65// Platform-specific DeferredCode functions.
66
67void DeferredCode::SaveRegisters() {
68 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
69 int action = registers_[i];
70 if (action == kPush) {
71 __ push(RegisterAllocator::ToRegister(i));
72 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
73 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
74 }
75 }
76}
77
78
79void DeferredCode::RestoreRegisters() {
80 // Restore registers in reverse order due to the stack.
81 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
82 int action = registers_[i];
83 if (action == kPush) {
84 __ pop(RegisterAllocator::ToRegister(i));
85 } else if (action != kIgnore) {
86 action &= ~kSyncedFlag;
87 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
88 }
89 }
90}
91
ager@chromium.org3bf7b912008-11-17 09:09:45 +000092
93// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000094// CodeGenState implementation.
95
ager@chromium.org7c537e22008-10-16 08:43:32 +000096CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000098 true_target_(NULL),
99 false_target_(NULL),
100 previous_(NULL) {
101 owner_->set_state(this);
102}
103
104
ager@chromium.org7c537e22008-10-16 08:43:32 +0000105CodeGenState::CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000106 JumpTarget* true_target,
107 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000109 true_target_(true_target),
110 false_target_(false_target),
111 previous_(owner->state()) {
112 owner_->set_state(this);
113}
114
115
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000116CodeGenState::~CodeGenState() {
117 ASSERT(owner_->state() == this);
118 owner_->set_state(previous_);
119}
120
121
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000122// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000123// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
ager@chromium.org5c838252010-02-19 08:53:10 +0000125CodeGenerator::CodeGenerator(MacroAssembler* masm)
126 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000127 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000128 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000129 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000130 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 cc_reg_(al),
132 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000133 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134}
135
136
137// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000138// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141// cp: callee's context
142
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000143void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000144 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000145 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000146 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147
148 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000149 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000150 ASSERT(allocator_ == NULL);
151 RegisterAllocator register_allocator(this);
152 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000153 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000154 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000155 cc_reg_ = al;
156 {
157 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000159 // Entry:
160 // Stack: receiver, arguments
161 // lr: return address
162 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000167
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168#ifdef DEBUG
169 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000170 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000172 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 }
174#endif
175
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000176 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000177 frame_->Enter();
178 // tos: code slot
179
180 // Allocate space for locals and initialize them. This also checks
181 // for stack overflow.
182 frame_->AllocateStackSlots();
183
ager@chromium.org357bf652010-04-12 11:30:10 +0000184 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +0000185 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000186 if (heap_slots > 0) {
187 // Allocate local context.
188 // Get outer context and create a new context based on it.
189 __ ldr(r0, frame_->Function());
190 frame_->EmitPush(r0);
191 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
192 FastNewContextStub stub(heap_slots);
193 frame_->CallStub(&stub, 1);
194 } else {
195 frame_->CallRuntime(Runtime::kNewContext, 1);
196 }
197
198#ifdef DEBUG
199 JumpTarget verified_true;
200 __ cmp(r0, Operand(cp));
201 verified_true.Branch(eq);
202 __ stop("NewContext: r0 is expected to be the same as cp");
203 verified_true.Bind();
204#endif
205 // Update context local.
206 __ str(cp, frame_->Context());
207 }
208
209 // TODO(1241774): Improve this code:
210 // 1) only needed if we have a context
211 // 2) no need to recompute context ptr every single time
212 // 3) don't copy parameter operand code from SlotOperand!
213 {
214 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000215 // Note that iteration order is relevant here! If we have the same
216 // parameter twice (e.g., function (x, y, x)), and that parameter
217 // needs to be copied into the context, it must be the last argument
218 // passed to the parameter that needs to be copied. This is a rare
219 // case so we don't check for it, instead we rely on the copying
220 // order: such a parameter is copied repeatedly into the same
221 // context location and thus the last value is what is seen inside
222 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000223 for (int i = 0; i < scope()->num_parameters(); i++) {
224 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225 Slot* slot = par->slot();
226 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000227 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000228 __ ldr(r1, frame_->ParameterAt(i));
229 // Loads r2 with context; used below in RecordWrite.
230 __ str(r1, SlotOperand(slot, r2));
231 // Load the offset into r3.
232 int slot_offset =
233 FixedArray::kHeaderSize + slot->index() * kPointerSize;
234 __ mov(r3, Operand(slot_offset));
235 __ RecordWrite(r2, r3, r1);
236 }
237 }
238 }
239
240 // Store the arguments object. This must happen after context
241 // initialization because the arguments object may be stored in the
242 // context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000243 if (scope()->arguments() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000244 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org5c838252010-02-19 08:53:10 +0000245 ASSERT(scope()->arguments_shadow() != NULL);
246 Variable* arguments = scope()->arguments()->var();
247 Variable* shadow = scope()->arguments_shadow()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000248 ASSERT(arguments != NULL && arguments->slot() != NULL);
249 ASSERT(shadow != NULL && shadow->slot() != NULL);
250 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
251 __ ldr(r2, frame_->Function());
252 // The receiver is below the arguments, the return address, and the
253 // frame pointer on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000254 const int kReceiverDisplacement = 2 + scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000255 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +0000256 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000257 frame_->Adjust(3);
258 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
259 frame_->CallStub(&stub, 3);
260 frame_->EmitPush(r0);
261 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
262 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
263 frame_->Drop(); // Value is no longer needed.
264 }
265
266 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000267 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000268 __ mov(ip, Operand(Factory::the_hole_value()));
269 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000270 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000271 }
272 } else {
273 // When used as the secondary compiler for splitting, r1, cp,
274 // fp, and lr have been pushed on the stack. Adjust the virtual
275 // frame to match this state.
276 frame_->Adjust(4);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000277
278 // Bind all the bailout labels to the beginning of the function.
279 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
280 for (int i = 0; i < bailouts->length(); i++) {
281 __ bind(bailouts->at(i)->label());
282 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000283 }
284
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000285 // Initialize the function return target after the locals are set
286 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000287 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000290 // Generate code to 'execute' declarations and initialize functions
291 // (source elements). In case of an illegal redeclaration we need to
292 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000293 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 } else {
297 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000298 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000299 // Bail out if a stack-overflow exception occurred when processing
300 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000301 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 }
303
mads.s.ager31e71382008-08-13 09:32:07 +0000304 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000305 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000306 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308
309 // Compile the body of the function in a vanilla state. Don't
310 // bother compiling all the code if the scope has an illegal
311 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000312 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 Comment cmnt(masm_, "[ function body");
314#ifdef DEBUG
315 bool is_builtin = Bootstrapper::IsActive();
316 bool should_trace =
317 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000318 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000320 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000323 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 }
326
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000327 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000328 if (has_valid_frame() || function_return_.is_linked()) {
329 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000330 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000331 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000332 // exit
333 // r0: result
334 // sp: stack pointer
335 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000337 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000338
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000339 function_return_.Bind();
340 if (FLAG_trace) {
341 // Push the return value on the stack as the parameter.
342 // Runtime::TraceExit returns the parameter as it is.
343 frame_->EmitPush(r0);
344 frame_->CallRuntime(Runtime::kTraceExit, 1);
345 }
346
ager@chromium.org4af710e2009-09-15 12:20:11 +0000347 // Add a label for checking the size of the code used for returning.
348 Label check_exit_codesize;
349 masm_->bind(&check_exit_codesize);
350
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000351 // Calculate the exact length of the return sequence and make sure that
352 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000353 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000354 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000355 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
356 // Additional mov instruction generated.
357 return_sequence_length++;
358 }
359 masm_->BlockConstPoolFor(return_sequence_length);
360
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000361 // Tear down the frame which will restore the caller's frame pointer and
362 // the link register.
363 frame_->Exit();
364
ager@chromium.org4af710e2009-09-15 12:20:11 +0000365 // Here we use masm_-> instead of the __ macro to avoid the code coverage
366 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000367 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000368 masm_->Jump(lr);
369
370 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000371 // expected by the debugger. The add instruction above is an addressing
372 // mode 1 instruction where there are restrictions on which immediate values
373 // can be encoded in the instruction and which immediate values requires
374 // use of an additional instruction for moving the immediate to a temporary
375 // register.
376 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000377 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000378 }
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 ASSERT(!has_cc());
382 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000383 ASSERT(!function_return_is_shadowed_);
384 function_return_.Unuse();
385 DeleteFrame();
386
387 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000388 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000389 ProcessDeferred();
390 }
391
392 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393}
394
395
ager@chromium.org7c537e22008-10-16 08:43:32 +0000396MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
397 // Currently, this assertion will fail if we try to assign to
398 // a constant variable that is constant because it is read-only
399 // (such as the variable referring to a named function expression).
400 // We need to implement assignments to read-only variables.
401 // Ideally, we should do this during AST generation (by converting
402 // such assignments into expression statements); however, in general
403 // we may not be able to make the decision until past AST generation,
404 // that is when the entire program is known.
405 ASSERT(slot != NULL);
406 int index = slot->index();
407 switch (slot->type()) {
408 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000409 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000411 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000412 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413
414 case Slot::CONTEXT: {
415 // Follow the context chain if necessary.
416 ASSERT(!tmp.is(cp)); // do not overwrite context register
417 Register context = cp;
418 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000419 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000420 // Load the closure.
421 // (All contexts, even 'with' contexts, have a closure,
422 // and it is the same for all contexts inside a function.
423 // There is no need to go to the function context first.)
424 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
425 // Load the function context (which is the incoming, outer context).
426 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
427 context = tmp;
428 }
429 // We may have a 'with' context now. Get the function context.
430 // (In fact this mov may never be the needed, since the scope analysis
431 // may not permit a direct context access in this case and thus we are
432 // always at a function context. However it is safe to dereference be-
433 // cause the function context of a function context is itself. Before
434 // deleting this mov we should try to create a counter-example first,
435 // though...)
436 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
437 return ContextOperand(tmp, index);
438 }
439
440 default:
441 UNREACHABLE();
442 return MemOperand(r0, 0);
443 }
444}
445
446
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000447MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
448 Slot* slot,
449 Register tmp,
450 Register tmp2,
451 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000452 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000453 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454
ager@chromium.org381abbb2009-02-25 13:23:22 +0000455 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
456 if (s->num_heap_slots() > 0) {
457 if (s->calls_eval()) {
458 // Check that extension is NULL.
459 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
460 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000462 }
463 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
464 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
465 context = tmp;
466 }
467 }
468 // Check that last extension is NULL.
469 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
470 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000471 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000472 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000474}
475
476
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000477// Loads a value on TOS. If it is a boolean value, the result may have been
478// (partially) translated into branches, or it may have set the condition
479// code register. If force_cc is set, the value is forced to set the
480// condition code register and no value is pushed. If the condition code
481// register was set, has_cc() is true and cc_reg_ contains the condition to
482// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000483void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000484 JumpTarget* true_target,
485 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000486 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000487 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000488 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000490 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000491 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000492
493 // If we hit a stack overflow, we may not have actually visited
494 // the expression. In that case, we ensure that we have a
495 // valid-looking frame state because we will continue to generate
496 // code as we unwind the C++ stack.
497 //
498 // It's possible to have both a stack overflow and a valid frame
499 // state (eg, a subexpression overflowed, visiting it returned
500 // with a dummied frame state, and visiting this expression
501 // returned with a normal-looking state).
502 if (HasStackOverflow() &&
503 has_valid_frame() &&
504 !has_cc() &&
505 frame_->height() == original_height) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000506 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507 true_target->Jump();
508 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000509 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000511 // Convert the TOS value to a boolean in the condition code register.
512 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 ASSERT(!force_cc || !has_valid_frame() || has_cc());
515 ASSERT(!has_valid_frame() ||
516 (has_cc() && frame_->height() == original_height) ||
517 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518}
519
520
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000521void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000522#ifdef DEBUG
523 int original_height = frame_->height();
524#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000525 JumpTarget true_target;
526 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000527 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528
529 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 // Convert cc_reg_ into a boolean value.
ager@chromium.org357bf652010-04-12 11:30:10 +0000531 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000532 JumpTarget loaded;
533 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000535 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000536 frame_->EmitPush(r0);
537 loaded.Jump();
538 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000539 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000540 frame_->EmitPush(r0);
541 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 cc_reg_ = al;
543 }
544
545 if (true_target.is_linked() || false_target.is_linked()) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000546 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 // We have at least one condition value that has been "translated"
548 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000549 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 if (frame_ != NULL) {
551 loaded.Jump(); // Don't lose the current TOS.
552 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000557 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000560 // If both "true" and "false" need to be loaded jump across the code for
561 // "false".
562 if (both) {
563 loaded.Jump();
564 }
565 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000568 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 // A value is loaded on all paths reaching this point.
572 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000574 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000576 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577}
578
579
ager@chromium.org7c537e22008-10-16 08:43:32 +0000580void CodeGenerator::LoadGlobal() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000581 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000582 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000583 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584}
585
586
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000587void CodeGenerator::LoadGlobalReceiver(Register scratch) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000588 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000589 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
590 __ ldr(scratch,
591 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000592 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000593}
594
595
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000596void CodeGenerator::LoadTypeofExpression(Expression* expr) {
597 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.org357bf652010-04-12 11:30:10 +0000598 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000599 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000601 // For a global variable we build the property reference
602 // <global>.<variable> and perform a (regular non-contextual) property
603 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
605 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000606 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000607 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000608 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000609 } else if (variable != NULL && variable->slot() != NULL) {
610 // For a variable that rewrites to a slot, we signal it is the immediate
611 // subexpression of a typeof.
612 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
613 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000615 // Anything else can be handled normally.
616 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 }
618}
619
620
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621Reference::Reference(CodeGenerator* cgen,
622 Expression* expression,
623 bool persist_after_get)
624 : cgen_(cgen),
625 expression_(expression),
626 type_(ILLEGAL),
627 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 cgen->LoadReference(this);
629}
630
631
632Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000633 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634}
635
636
ager@chromium.org7c537e22008-10-16 08:43:32 +0000637void CodeGenerator::LoadReference(Reference* ref) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000638 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000639 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 Expression* e = ref->expression();
641 Property* property = e->AsProperty();
642 Variable* var = e->AsVariableProxy()->AsVariable();
643
644 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000645 // The expression is either a property or a variable proxy that rewrites
646 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000648 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 ref->set_type(Reference::NAMED);
650 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000651 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 ref->set_type(Reference::KEYED);
653 }
654 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000655 // The expression is a variable proxy that does not rewrite to a
656 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 LoadGlobal();
659 ref->set_type(Reference::NAMED);
660 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000661 ASSERT(var->slot() != NULL);
662 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 }
664 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000665 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000666 LoadAndSpill(e);
667 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 }
669}
670
671
ager@chromium.org7c537e22008-10-16 08:43:32 +0000672void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000674 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000675 if (size == 0) return;
676
677 // Pop a reference from the stack while preserving TOS.
678 VirtualFrame::RegisterAllocationScope scope(this);
679 Comment cmnt(masm_, "[ UnloadReference");
680 if (size > 0) {
681 Register tos = frame_->PopToRegister();
682 frame_->Drop(size);
683 frame_->EmitPush(tos);
684 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685}
686
687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
689// register to a boolean in the condition code register. The code
690// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000691void CodeGenerator::ToBoolean(JumpTarget* true_target,
692 JumpTarget* false_target) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000693 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000694 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000696 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
698 // Fast case checks
699
mads.s.ager31e71382008-08-13 09:32:07 +0000700 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000701 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
702 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000703 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704
mads.s.ager31e71382008-08-13 09:32:07 +0000705 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000706 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
707 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000708 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
mads.s.ager31e71382008-08-13 09:32:07 +0000710 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000711 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
712 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000713 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714
mads.s.ager31e71382008-08-13 09:32:07 +0000715 // Check if the value is a smi.
716 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000717 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000718 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
721 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000722 frame_->EmitPush(r0);
723 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000724 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000725 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
726 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727
728 cc_reg_ = ne;
729}
730
731
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000732void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000733 OverwriteMode overwrite_mode,
734 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000735 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000736 // sp[0] : y
737 // sp[1] : x
738 // result : r0
739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 // Stub is entered with a call: 'return address' is in lr.
741 switch (op) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000742 case Token::ADD:
743 case Token::SUB:
744 case Token::MUL:
745 case Token::DIV:
746 case Token::MOD:
747 case Token::BIT_OR:
748 case Token::BIT_AND:
749 case Token::BIT_XOR:
750 case Token::SHL:
751 case Token::SHR:
752 case Token::SAR: {
753 frame_->EmitPop(r0); // r0 : y
754 frame_->EmitPop(r1); // r1 : x
755 GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs);
756 frame_->CallStub(&stub, 0);
757 break;
758 }
759
760 case Token::COMMA:
761 frame_->EmitPop(r0);
762 // Simply discard left value.
763 frame_->Drop();
764 break;
765
766 default:
767 // Other cases should have been handled before this point.
768 UNREACHABLE();
769 break;
770 }
771}
772
773
774void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op,
775 OverwriteMode overwrite_mode,
776 int constant_rhs) {
777 // top of virtual frame: y
778 // 2nd elt. on virtual frame : x
779 // result : top of virtual frame
780
781 // Stub is entered with a call: 'return address' is in lr.
782 switch (op) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783 case Token::ADD: // fall through.
784 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000785 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000786 case Token::DIV:
787 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000788 case Token::BIT_OR:
789 case Token::BIT_AND:
790 case Token::BIT_XOR:
791 case Token::SHL:
792 case Token::SHR:
793 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000794 Register rhs = frame_->PopToRegister();
795 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
796 {
797 VirtualFrame::SpilledScope spilled_scope(frame_);
798 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
799 frame_->CallStub(&stub, 0);
800 }
801 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802 break;
803 }
804
ager@chromium.org357bf652010-04-12 11:30:10 +0000805 case Token::COMMA: {
806 Register scratch = frame_->PopToRegister();
807 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000808 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000809 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000811 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812
813 default:
814 // Other cases should have been handled before this point.
815 UNREACHABLE();
816 break;
817 }
818}
819
820
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000821class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000822 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000823 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000824 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000825 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000826 OverwriteMode overwrite_mode,
827 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000828 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000829 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000830 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000831 overwrite_mode_(overwrite_mode),
832 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000833 set_comment("[ DeferredInlinedSmiOperation");
834 }
835
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000836 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000837
838 private:
839 Token::Value op_;
840 int value_;
841 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000842 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000843 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000844};
845
846
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000847void DeferredInlineSmiOperation::Generate() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000848 Register lhs = r1;
849 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850 switch (op_) {
851 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000852 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000853 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000854 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000855 __ mov(r1, Operand(Smi::FromInt(value_)));
856 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000857 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000858 __ mov(r0, Operand(Smi::FromInt(value_)));
859 }
860 break;
861 }
862
863 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000864 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000865 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000866 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000867 __ mov(r1, Operand(Smi::FromInt(value_)));
868 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000869 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000870 __ mov(r0, Operand(Smi::FromInt(value_)));
871 }
872 break;
873 }
874
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000875 // For these operations there is no optimistic operation that needs to be
876 // reverted.
877 case Token::MUL:
878 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000879 case Token::BIT_OR:
880 case Token::BIT_XOR:
881 case Token::BIT_AND: {
882 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000883 if (tos_register_.is(r0)) {
884 __ mov(r1, Operand(Smi::FromInt(value_)));
885 } else {
886 ASSERT(tos_register_.is(r1));
887 __ mov(r0, Operand(Smi::FromInt(value_)));
888 lhs = r0;
889 rhs = r1;
890 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000891 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000892 if (tos_register_.is(r1)) {
893 __ mov(r0, Operand(Smi::FromInt(value_)));
894 } else {
895 ASSERT(tos_register_.is(r0));
896 __ mov(r1, Operand(Smi::FromInt(value_)));
897 lhs = r0;
898 rhs = r1;
899 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000900 }
901 break;
902 }
903
904 case Token::SHL:
905 case Token::SHR:
906 case Token::SAR: {
907 if (!reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000908 if (tos_register_.is(r1)) {
909 __ mov(r0, Operand(Smi::FromInt(value_)));
910 } else {
911 ASSERT(tos_register_.is(r0));
912 __ mov(r1, Operand(Smi::FromInt(value_)));
913 lhs = r0;
914 rhs = r1;
915 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000916 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000917 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000918 }
919 break;
920 }
921
922 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000923 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000924 UNREACHABLE();
925 break;
926 }
927
ager@chromium.org357bf652010-04-12 11:30:10 +0000928 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000929 __ CallStub(&stub);
ager@chromium.org357bf652010-04-12 11:30:10 +0000930 // The generic stub returns its value in r0, but that's not
931 // necessarily what we want. We want whatever the inlined code
932 // expected, which is that the answer is in the same register as
933 // the operand was.
934 __ Move(tos_register_, r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000935}
936
937
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000938static bool PopCountLessThanEqual2(unsigned int x) {
939 x &= x - 1;
940 return (x & (x - 1)) == 0;
941}
942
943
944// Returns the index of the lowest bit set.
945static int BitPosition(unsigned x) {
946 int bit_posn = 0;
947 while ((x & 0xf) == 0) {
948 bit_posn += 4;
949 x >>= 4;
950 }
951 while ((x & 1) == 0) {
952 bit_posn++;
953 x >>= 1;
954 }
955 return bit_posn;
956}
957
958
ager@chromium.org357bf652010-04-12 11:30:10 +0000959void CodeGenerator::VirtualFrameSmiOperation(Token::Value op,
960 Handle<Object> value,
961 bool reversed,
962 OverwriteMode mode) {
963 int int_value = Smi::cast(*value)->value();
964
965 bool something_to_inline;
966 switch (op) {
967 case Token::ADD:
968 case Token::SUB:
969 case Token::BIT_AND:
970 case Token::BIT_OR:
971 case Token::BIT_XOR: {
972 something_to_inline = true;
973 break;
974 }
975 case Token::SHL:
976 case Token::SHR:
977 case Token::SAR: {
978 if (reversed) {
979 something_to_inline = false;
980 } else {
981 something_to_inline = true;
982 }
983 break;
984 }
985 case Token::MOD: {
986 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
987 something_to_inline = false;
988 } else {
989 something_to_inline = true;
990 }
991 break;
992 }
993 case Token::MUL: {
994 if (!IsEasyToMultiplyBy(int_value)) {
995 something_to_inline = false;
996 } else {
997 something_to_inline = true;
998 }
999 break;
1000 }
1001 default: {
1002 something_to_inline = false;
1003 break;
1004 }
1005 }
1006
1007 if (!something_to_inline) {
1008 if (!reversed) {
1009 // Push the rhs onto the virtual frame by putting it in a TOS register.
1010 Register rhs = frame_->GetTOSRegister();
1011 __ mov(rhs, Operand(value));
1012 frame_->EmitPush(rhs);
1013 VirtualFrameBinaryOperation(op, mode, int_value);
1014 } else {
1015 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1016 // at most one pop, the rest takes place in TOS registers.
1017 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1018 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1019 __ mov(lhs, Operand(value));
1020 frame_->EmitPush(lhs);
1021 frame_->EmitPush(rhs);
1022 VirtualFrameBinaryOperation(op, mode, kUnknownIntValue);
1023 }
1024 return;
1025 }
1026
1027 // We move the top of stack to a register (normally no move is invoved).
1028 Register tos = frame_->PopToRegister();
1029 // All other registers are spilled. The deferred code expects one argument
1030 // in a register and all other values are flushed to the stack. The
1031 // answer is returned in the same register that the top of stack argument was
1032 // in.
1033 frame_->SpillAll();
1034
1035 switch (op) {
1036 case Token::ADD: {
1037 DeferredCode* deferred =
1038 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1039
1040 __ add(tos, tos, Operand(value), SetCC);
1041 deferred->Branch(vs);
1042 __ tst(tos, Operand(kSmiTagMask));
1043 deferred->Branch(ne);
1044 deferred->BindExit();
1045 frame_->EmitPush(tos);
1046 break;
1047 }
1048
1049 case Token::SUB: {
1050 DeferredCode* deferred =
1051 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1052
1053 if (reversed) {
1054 __ rsb(tos, tos, Operand(value), SetCC);
1055 } else {
1056 __ sub(tos, tos, Operand(value), SetCC);
1057 }
1058 deferred->Branch(vs);
1059 __ tst(tos, Operand(kSmiTagMask));
1060 deferred->Branch(ne);
1061 deferred->BindExit();
1062 frame_->EmitPush(tos);
1063 break;
1064 }
1065
1066
1067 case Token::BIT_OR:
1068 case Token::BIT_XOR:
1069 case Token::BIT_AND: {
1070 DeferredCode* deferred =
1071 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1072 __ tst(tos, Operand(kSmiTagMask));
1073 deferred->Branch(ne);
1074 switch (op) {
1075 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1076 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
1077 case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break;
1078 default: UNREACHABLE();
1079 }
1080 deferred->BindExit();
1081 frame_->EmitPush(tos);
1082 break;
1083 }
1084
1085 case Token::SHL:
1086 case Token::SHR:
1087 case Token::SAR: {
1088 ASSERT(!reversed);
1089 Register scratch = VirtualFrame::scratch0();
1090 Register scratch2 = VirtualFrame::scratch1();
1091 int shift_value = int_value & 0x1f; // least significant 5 bits
1092 DeferredCode* deferred =
1093 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
1094 __ tst(tos, Operand(kSmiTagMask));
1095 deferred->Branch(ne);
1096 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags
1097 switch (op) {
1098 case Token::SHL: {
1099 if (shift_value != 0) {
1100 __ mov(scratch, Operand(scratch, LSL, shift_value));
1101 }
1102 // check that the *signed* result fits in a smi
1103 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1104 deferred->Branch(mi);
1105 break;
1106 }
1107 case Token::SHR: {
1108 // LSR by immediate 0 means shifting 32 bits.
1109 if (shift_value != 0) {
1110 __ mov(scratch, Operand(scratch, LSR, shift_value));
1111 }
1112 // check that the *unsigned* result fits in a smi
1113 // neither of the two high-order bits can be set:
1114 // - 0x80000000: high bit would be lost when smi tagging
1115 // - 0x40000000: this number would convert to negative when
1116 // smi tagging these two cases can only happen with shifts
1117 // by 0 or 1 when handed a valid smi
1118 __ tst(scratch, Operand(0xc0000000));
1119 deferred->Branch(ne);
1120 break;
1121 }
1122 case Token::SAR: {
1123 if (shift_value != 0) {
1124 // ASR by immediate 0 means shifting 32 bits.
1125 __ mov(scratch, Operand(scratch, ASR, shift_value));
1126 }
1127 break;
1128 }
1129 default: UNREACHABLE();
1130 }
1131 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1132 deferred->BindExit();
1133 frame_->EmitPush(tos);
1134 break;
1135 }
1136
1137 case Token::MOD: {
1138 ASSERT(!reversed);
1139 ASSERT(int_value >= 2);
1140 ASSERT(IsPowerOf2(int_value));
1141 DeferredCode* deferred =
1142 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1143 unsigned mask = (0x80000000u | kSmiTagMask);
1144 __ tst(tos, Operand(mask));
1145 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1146 mask = (int_value << kSmiTagSize) - 1;
1147 __ and_(tos, tos, Operand(mask));
1148 deferred->BindExit();
1149 frame_->EmitPush(tos);
1150 break;
1151 }
1152
1153 case Token::MUL: {
1154 ASSERT(IsEasyToMultiplyBy(int_value));
1155 DeferredCode* deferred =
1156 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1157 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1158 max_smi_that_wont_overflow <<= kSmiTagSize;
1159 unsigned mask = 0x80000000u;
1160 while ((mask & max_smi_that_wont_overflow) == 0) {
1161 mask |= mask >> 1;
1162 }
1163 mask |= kSmiTagMask;
1164 // This does a single mask that checks for a too high value in a
1165 // conservative way and for a non-Smi. It also filters out negative
1166 // numbers, unfortunately, but since this code is inline we prefer
1167 // brevity to comprehensiveness.
1168 __ tst(tos, Operand(mask));
1169 deferred->Branch(ne);
1170 MultiplyByKnownInt(masm_, tos, tos, int_value);
1171 deferred->BindExit();
1172 frame_->EmitPush(tos);
1173 break;
1174 }
1175
1176 default:
1177 UNREACHABLE();
1178 break;
1179 }
1180}
1181
1182
ager@chromium.org7c537e22008-10-16 08:43:32 +00001183void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001184 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001185 bool reversed,
1186 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001187 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 // NOTE: This is an attempt to inline (a bit) more of the code for
1189 // some possible smi operations (like + and -) when (at least) one
1190 // of the operands is a literal smi. With this optimization, the
1191 // performance of the system is increased by ~15%, and the generated
1192 // code size is increased by ~1% (measured on a combination of
1193 // different benchmarks).
1194
mads.s.ager31e71382008-08-13 09:32:07 +00001195 // sp[0] : operand
1196
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001197 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001199 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001202 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 switch (op) {
1204 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001205 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001206 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001208 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001209 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001211 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001212 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 break;
1214 }
1215
1216 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001217 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001218 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219
ager@chromium.orge2902be2009-06-08 12:21:35 +00001220 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001221 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001222 } else {
1223 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 }
ager@chromium.orge2902be2009-06-08 12:21:35 +00001225 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001226 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001227 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001228 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001229 break;
1230 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001232
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001233 case Token::BIT_OR:
1234 case Token::BIT_XOR:
1235 case Token::BIT_AND: {
1236 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001237 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001238 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001239 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001240 switch (op) {
1241 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
1242 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
1243 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
1244 default: UNREACHABLE();
1245 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001246 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001247 break;
1248 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001250 case Token::SHL:
1251 case Token::SHR:
1252 case Token::SAR: {
1253 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001254 something_to_inline = false;
1255 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001256 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001257 int shift_value = int_value & 0x1f; // least significant 5 bits
1258 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001259 new DeferredInlineSmiOperation(op, shift_value, false, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001260 __ tst(r0, Operand(kSmiTagMask));
1261 deferred->Branch(ne);
1262 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
1263 switch (op) {
1264 case Token::SHL: {
1265 if (shift_value != 0) {
1266 __ mov(r2, Operand(r2, LSL, shift_value));
1267 }
1268 // check that the *unsigned* result fits in a smi
1269 __ add(r3, r2, Operand(0x40000000), SetCC);
1270 deferred->Branch(mi);
1271 break;
1272 }
1273 case Token::SHR: {
1274 // LSR by immediate 0 means shifting 32 bits.
1275 if (shift_value != 0) {
1276 __ mov(r2, Operand(r2, LSR, shift_value));
1277 }
1278 // check that the *unsigned* result fits in a smi
1279 // neither of the two high-order bits can be set:
1280 // - 0x80000000: high bit would be lost when smi tagging
1281 // - 0x40000000: this number would convert to negative when
1282 // smi tagging these two cases can only happen with shifts
1283 // by 0 or 1 when handed a valid smi
1284 __ and_(r3, r2, Operand(0xc0000000), SetCC);
1285 deferred->Branch(ne);
1286 break;
1287 }
1288 case Token::SAR: {
1289 if (shift_value != 0) {
1290 // ASR by immediate 0 means shifting 32 bits.
1291 __ mov(r2, Operand(r2, ASR, shift_value));
1292 }
1293 break;
1294 }
1295 default: UNREACHABLE();
1296 }
1297 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1298 deferred->BindExit();
1299 break;
1300 }
1301
1302 case Token::MOD: {
1303 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1304 something_to_inline = false;
1305 break;
1306 }
1307 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001308 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001309 unsigned mask = (0x80000000u | kSmiTagMask);
1310 __ tst(r0, Operand(mask));
1311 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1312 mask = (int_value << kSmiTagSize) - 1;
1313 __ and_(r0, r0, Operand(mask));
1314 deferred->BindExit();
1315 break;
1316 }
1317
1318 case Token::MUL: {
1319 if (!IsEasyToMultiplyBy(int_value)) {
1320 something_to_inline = false;
1321 break;
1322 }
1323 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001324 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001325 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1326 max_smi_that_wont_overflow <<= kSmiTagSize;
1327 unsigned mask = 0x80000000u;
1328 while ((mask & max_smi_that_wont_overflow) == 0) {
1329 mask |= mask >> 1;
1330 }
1331 mask |= kSmiTagMask;
1332 // This does a single mask that checks for a too high value in a
1333 // conservative way and for a non-Smi. It also filters out negative
1334 // numbers, unfortunately, but since this code is inline we prefer
1335 // brevity to comprehensiveness.
1336 __ tst(r0, Operand(mask));
1337 deferred->Branch(ne);
1338 MultiplyByKnownInt(masm_, r0, r0, int_value);
1339 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 break;
1341 }
1342
1343 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001344 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 break;
1346 }
1347
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001348 if (!something_to_inline) {
1349 if (!reversed) {
1350 frame_->EmitPush(r0);
1351 __ mov(r0, Operand(value));
1352 frame_->EmitPush(r0);
1353 GenericBinaryOperation(op, mode, int_value);
1354 } else {
1355 __ mov(ip, Operand(value));
1356 frame_->EmitPush(ip);
1357 frame_->EmitPush(r0);
1358 GenericBinaryOperation(op, mode, kUnknownIntValue);
1359 }
1360 }
1361
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363}
1364
1365
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001366void CodeGenerator::Comparison(Condition cc,
1367 Expression* left,
1368 Expression* right,
1369 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001370 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001371
ager@chromium.org357bf652010-04-12 11:30:10 +00001372 if (left != NULL) Load(left);
1373 if (right != NULL) Load(right);
1374
mads.s.ager31e71382008-08-13 09:32:07 +00001375 // sp[0] : y
1376 // sp[1] : x
1377 // result : cc register
1378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 // Strict only makes sense for equality comparisons.
1380 ASSERT(!strict || cc == eq);
1381
ager@chromium.org357bf652010-04-12 11:30:10 +00001382 Register lhs;
1383 Register rhs;
1384
1385 // We load the top two stack positions into registers chosen by the virtual
1386 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001387 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1388 if (cc == gt || cc == le) {
1389 cc = ReverseCondition(cc);
ager@chromium.org357bf652010-04-12 11:30:10 +00001390 lhs = frame_->PopToRegister();
1391 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001392 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001393 rhs = frame_->PopToRegister();
1394 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001395 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001396
1397 ASSERT(rhs.is(r0) || rhs.is(r1));
1398 ASSERT(lhs.is(r0) || lhs.is(r1));
1399
1400 // Now we have the two sides in r0 and r1. We flush any other registers
1401 // because the stub doesn't know about register allocation.
1402 frame_->SpillAll();
1403 Register scratch = VirtualFrame::scratch0();
1404 __ orr(scratch, lhs, Operand(rhs));
1405 __ tst(scratch, Operand(kSmiTagMask));
1406 JumpTarget smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001409 // Perform non-smi comparison by stub.
1410 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1411 // We call with 0 args because there are 0 on the stack.
ager@chromium.org357bf652010-04-12 11:30:10 +00001412 if (!rhs.is(r0)) {
1413 __ Swap(rhs, lhs, ip);
1414 }
1415
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001416 CompareStub stub(cc, strict);
1417 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001418 __ cmp(r0, Operand(0));
ager@chromium.org357bf652010-04-12 11:30:10 +00001419 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001420 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001422 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001423 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001424 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001426 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 cc_reg_ = cc;
1428}
1429
1430
mads.s.ager31e71382008-08-13 09:32:07 +00001431// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001432void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001433 CallFunctionFlags flags,
1434 int position) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001435 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001437 int arg_count = args->length();
1438 for (int i = 0; i < arg_count; i++) {
1439 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001440 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
kasper.lund7276f142008-07-30 08:49:36 +00001442 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001443 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444
kasper.lund7276f142008-07-30 08:49:36 +00001445 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001446 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001447 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001448 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449
1450 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001451 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453}
1454
1455
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001456void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001457 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 ASSERT(has_cc());
1459 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 cc_reg_ = al;
1462}
1463
1464
ager@chromium.org7c537e22008-10-16 08:43:32 +00001465void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001466 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001467 Comment cmnt(masm_, "[ check stack");
1468 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1469 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1470 // the implicit 8 byte offset that always applies to operations with pc and
1471 // gives a return address 12 bytes down.
1472 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1473 masm_->cmp(sp, Operand(ip));
1474 StackCheckStub stub;
1475 // Call the stub if lower.
1476 masm_->mov(pc,
1477 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1478 RelocInfo::CODE_TARGET),
1479 LeaveCC,
1480 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481}
1482
1483
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001484void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1485#ifdef DEBUG
1486 int original_height = frame_->height();
1487#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001488 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001489 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1490 VisitAndSpill(statements->at(i));
1491 }
1492 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1493}
1494
1495
ager@chromium.org7c537e22008-10-16 08:43:32 +00001496void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497#ifdef DEBUG
1498 int original_height = frame_->height();
1499#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001500 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001502 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001503 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001504 VisitStatementsAndSpill(node->statements());
1505 if (node->break_target()->is_linked()) {
1506 node->break_target()->Bind();
1507 }
1508 node->break_target()->Unuse();
1509 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510}
1511
1512
ager@chromium.org7c537e22008-10-16 08:43:32 +00001513void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001514 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001515 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001516 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001517 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001518 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001519 frame_->EmitPush(r0);
1520 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001521 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522}
1523
1524
ager@chromium.org7c537e22008-10-16 08:43:32 +00001525void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001526#ifdef DEBUG
1527 int original_height = frame_->height();
1528#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001529 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 Comment cmnt(masm_, "[ Declaration");
1531 Variable* var = node->proxy()->var();
1532 ASSERT(var != NULL); // must have been resolved
1533 Slot* slot = var->slot();
1534
1535 // If it was not possible to allocate the variable at compile time,
1536 // we need to "declare" it at runtime to make sure it actually
1537 // exists in the local context.
1538 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1539 // Variables with a "LOOKUP" slot were introduced as non-locals
1540 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001541 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001544 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001545 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 // Declaration nodes are always declared in only two modes.
1547 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1548 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001549 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001550 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 // Push initial value, if any.
1552 // Note: For variables we must not push an initial value (such as
1553 // 'undefined') because we may have a (legal) redeclaration and we
1554 // must not destroy the current value.
1555 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001556 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001557 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001559 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001561 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001562 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001564 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001565 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 return;
1568 }
1569
1570 ASSERT(!var->is_global());
1571
1572 // If we have a function or a constant, we need to initialize the variable.
1573 Expression* val = NULL;
1574 if (node->mode() == Variable::CONST) {
1575 val = new Literal(Factory::the_hole_value());
1576 } else {
1577 val = node->fun(); // NULL if we don't have a function
1578 }
1579
1580 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001581 {
1582 // Set initial value.
1583 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001584 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001585 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001586 }
1587 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001588 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001590 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591}
1592
1593
ager@chromium.org7c537e22008-10-16 08:43:32 +00001594void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595#ifdef DEBUG
1596 int original_height = frame_->height();
1597#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001598 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 Expression* expression = node->expression();
1602 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001603 LoadAndSpill(expression);
1604 frame_->Drop();
1605 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606}
1607
1608
ager@chromium.org7c537e22008-10-16 08:43:32 +00001609void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610#ifdef DEBUG
1611 int original_height = frame_->height();
1612#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001613 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001615 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001617 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618}
1619
1620
ager@chromium.org7c537e22008-10-16 08:43:32 +00001621void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001622#ifdef DEBUG
1623 int original_height = frame_->height();
1624#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001625 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001627 // Generate different code depending on which parts of the if statement
1628 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 bool has_then_stm = node->HasThenStatement();
1630 bool has_else_stm = node->HasElseStatement();
1631
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001632 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001634 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001636 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001637 JumpTarget then;
1638 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001640 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001641 if (frame_ != NULL) {
1642 Branch(false, &else_);
1643 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001645 if (frame_ != NULL || then.is_linked()) {
1646 then.Bind();
1647 VisitAndSpill(node->then_statement());
1648 }
1649 if (frame_ != NULL) {
1650 exit.Jump();
1651 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001653 if (else_.is_linked()) {
1654 else_.Bind();
1655 VisitAndSpill(node->else_statement());
1656 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
1658 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001659 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001661 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001663 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001664 if (frame_ != NULL) {
1665 Branch(false, &exit);
1666 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001668 if (frame_ != NULL || then.is_linked()) {
1669 then.Bind();
1670 VisitAndSpill(node->then_statement());
1671 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672
1673 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001674 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001676 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001678 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679 if (frame_ != NULL) {
1680 Branch(true, &exit);
1681 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001683 if (frame_ != NULL || else_.is_linked()) {
1684 else_.Bind();
1685 VisitAndSpill(node->else_statement());
1686 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687
1688 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001689 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 ASSERT(!has_then_stm && !has_else_stm);
1691 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001692 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001693 if (frame_ != NULL) {
1694 if (has_cc()) {
1695 cc_reg_ = al;
1696 } else {
1697 frame_->Drop();
1698 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 }
1700 }
1701
1702 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001703 if (exit.is_linked()) {
1704 exit.Bind();
1705 }
1706 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707}
1708
1709
ager@chromium.org7c537e22008-10-16 08:43:32 +00001710void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001711 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001713 CodeForStatementPosition(node);
1714 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715}
1716
1717
ager@chromium.org7c537e22008-10-16 08:43:32 +00001718void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001719 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001721 CodeForStatementPosition(node);
1722 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723}
1724
1725
ager@chromium.org7c537e22008-10-16 08:43:32 +00001726void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001727 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001729
ager@chromium.org4af710e2009-09-15 12:20:11 +00001730 CodeForStatementPosition(node);
1731 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001732 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001733 frame_->EmitPop(r0);
1734 function_return_.Jump();
1735 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001736 // Pop the result from the frame and prepare the frame for
1737 // returning thus making it easier to merge.
1738 frame_->EmitPop(r0);
1739 frame_->PrepareForReturn();
1740
1741 function_return_.Jump();
1742 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743}
1744
1745
ager@chromium.org7c537e22008-10-16 08:43:32 +00001746void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001747#ifdef DEBUG
1748 int original_height = frame_->height();
1749#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001750 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 CodeForStatementPosition(node);
1753 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001754 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001755 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001756 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001757 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001758 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001759#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001760 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001761 __ cmp(r0, Operand(cp));
1762 verified_true.Branch(eq);
1763 __ stop("PushContext: r0 is expected to be the same as cp");
1764 verified_true.Bind();
1765#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001767 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769}
1770
1771
ager@chromium.org7c537e22008-10-16 08:43:32 +00001772void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773#ifdef DEBUG
1774 int original_height = frame_->height();
1775#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001776 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 // Pop context.
1780 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1781 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001782 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784}
1785
1786
ager@chromium.org7c537e22008-10-16 08:43:32 +00001787void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788#ifdef DEBUG
1789 int original_height = frame_->height();
1790#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001791 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001794 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001796 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001797
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001798 JumpTarget next_test;
1799 JumpTarget fall_through;
1800 JumpTarget default_entry;
1801 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 ZoneList<CaseClause*>* cases = node->cases();
1803 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001804 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805
1806 for (int i = 0; i < length; i++) {
1807 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 // Remember the default clause and compile it at the end.
1810 default_clause = clause;
1811 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 }
1813
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814 Comment cmnt(masm_, "[ Case clause");
1815 // Compile the test.
1816 next_test.Bind();
1817 next_test.Unuse();
1818 // Duplicate TOS.
1819 __ ldr(r0, frame_->Top());
1820 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001821 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001823
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 // Before entering the body from the test, remove the switch value from
1825 // the stack.
1826 frame_->Drop();
1827
1828 // Label the body so that fall through is enabled.
1829 if (i > 0 && cases->at(i - 1)->is_default()) {
1830 default_exit.Bind();
1831 } else {
1832 fall_through.Bind();
1833 fall_through.Unuse();
1834 }
1835 VisitStatementsAndSpill(clause->statements());
1836
1837 // If control flow can fall through from the body, jump to the next body
1838 // or the end of the statement.
1839 if (frame_ != NULL) {
1840 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1841 default_entry.Jump();
1842 } else {
1843 fall_through.Jump();
1844 }
1845 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 }
1847
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001848 // The final "test" removes the switch value.
1849 next_test.Bind();
1850 frame_->Drop();
1851
1852 // If there is a default clause, compile it.
1853 if (default_clause != NULL) {
1854 Comment cmnt(masm_, "[ Default clause");
1855 default_entry.Bind();
1856 VisitStatementsAndSpill(default_clause->statements());
1857 // If control flow can fall out of the default and there is a case after
1858 // it, jup to that case's body.
1859 if (frame_ != NULL && default_exit.is_bound()) {
1860 default_exit.Jump();
1861 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001862 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001864 if (fall_through.is_linked()) {
1865 fall_through.Bind();
1866 }
1867
1868 if (node->break_target()->is_linked()) {
1869 node->break_target()->Bind();
1870 }
1871 node->break_target()->Unuse();
1872 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873}
1874
1875
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001876void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877#ifdef DEBUG
1878 int original_height = frame_->height();
1879#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001880 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001881 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001883 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001884 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001886 // Label the top of the loop for the backward CFG edge. If the test
1887 // is always true we can use the continue target, and if the test is
1888 // always false there is no need.
1889 ConditionAnalysis info = AnalyzeCondition(node->cond());
1890 switch (info) {
1891 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001892 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001894 break;
1895 case ALWAYS_FALSE:
1896 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1897 break;
1898 case DONT_KNOW:
1899 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1900 body.Bind();
1901 break;
1902 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001903
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001904 CheckStack(); // TODO(1222600): ignore if body contains calls.
1905 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001907 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001908 switch (info) {
1909 case ALWAYS_TRUE:
1910 // If control can fall off the end of the body, jump back to the
1911 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001913 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001916 case ALWAYS_FALSE:
1917 // If we have a continue in the body, we only have to bind its
1918 // jump target.
1919 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001921 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001922 break;
1923 case DONT_KNOW:
1924 // We have to compile the test expression if it can be reached by
1925 // control flow falling out of the body or via continue.
1926 if (node->continue_target()->is_linked()) {
1927 node->continue_target()->Bind();
1928 }
1929 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001930 Comment cmnt(masm_, "[ DoWhileCondition");
1931 CodeForDoWhileConditionPosition(node);
1932 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001933 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001934 // A invalid frame here indicates that control did not
1935 // fall out of the test expression.
1936 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 }
1938 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 break;
1940 }
1941
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001942 if (node->break_target()->is_linked()) {
1943 node->break_target()->Bind();
1944 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001945 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1946}
1947
1948
1949void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1950#ifdef DEBUG
1951 int original_height = frame_->height();
1952#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001953 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001954 Comment cmnt(masm_, "[ WhileStatement");
1955 CodeForStatementPosition(node);
1956
1957 // If the test is never true and has no side effects there is no need
1958 // to compile the test or body.
1959 ConditionAnalysis info = AnalyzeCondition(node->cond());
1960 if (info == ALWAYS_FALSE) return;
1961
1962 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1963
1964 // Label the top of the loop with the continue target for the backward
1965 // CFG edge.
1966 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1967 node->continue_target()->Bind();
1968
1969 if (info == DONT_KNOW) {
1970 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001971 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001972 if (has_valid_frame()) {
1973 // A NULL frame indicates that control did not fall out of the
1974 // test expression.
1975 Branch(false, node->break_target());
1976 }
1977 if (has_valid_frame() || body.is_linked()) {
1978 body.Bind();
1979 }
1980 }
1981
1982 if (has_valid_frame()) {
1983 CheckStack(); // TODO(1222600): ignore if body contains calls.
1984 VisitAndSpill(node->body());
1985
1986 // If control flow can fall out of the body, jump back to the top.
1987 if (has_valid_frame()) {
1988 node->continue_target()->Jump();
1989 }
1990 }
1991 if (node->break_target()->is_linked()) {
1992 node->break_target()->Bind();
1993 }
1994 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1995}
1996
1997
1998void CodeGenerator::VisitForStatement(ForStatement* node) {
1999#ifdef DEBUG
2000 int original_height = frame_->height();
2001#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002002 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002003 Comment cmnt(masm_, "[ ForStatement");
2004 CodeForStatementPosition(node);
2005 if (node->init() != NULL) {
2006 VisitAndSpill(node->init());
2007 }
2008
2009 // If the test is never true there is no need to compile the test or
2010 // body.
2011 ConditionAnalysis info = AnalyzeCondition(node->cond());
2012 if (info == ALWAYS_FALSE) return;
2013
2014 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2015
2016 // If there is no update statement, label the top of the loop with the
2017 // continue target, otherwise with the loop target.
2018 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2019 if (node->next() == NULL) {
2020 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2021 node->continue_target()->Bind();
2022 } else {
2023 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2024 loop.Bind();
2025 }
2026
2027 // If the test is always true, there is no need to compile it.
2028 if (info == DONT_KNOW) {
2029 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002030 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002031 if (has_valid_frame()) {
2032 Branch(false, node->break_target());
2033 }
2034 if (has_valid_frame() || body.is_linked()) {
2035 body.Bind();
2036 }
2037 }
2038
2039 if (has_valid_frame()) {
2040 CheckStack(); // TODO(1222600): ignore if body contains calls.
2041 VisitAndSpill(node->body());
2042
2043 if (node->next() == NULL) {
2044 // If there is no update statement and control flow can fall out
2045 // of the loop, jump directly to the continue label.
2046 if (has_valid_frame()) {
2047 node->continue_target()->Jump();
2048 }
2049 } else {
2050 // If there is an update statement and control flow can reach it
2051 // via falling out of the body of the loop or continuing, we
2052 // compile the update statement.
2053 if (node->continue_target()->is_linked()) {
2054 node->continue_target()->Bind();
2055 }
2056 if (has_valid_frame()) {
2057 // Record source position of the statement as this code which is
2058 // after the code for the body actually belongs to the loop
2059 // statement and not the body.
2060 CodeForStatementPosition(node);
2061 VisitAndSpill(node->next());
2062 loop.Jump();
2063 }
2064 }
2065 }
2066 if (node->break_target()->is_linked()) {
2067 node->break_target()->Bind();
2068 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070}
2071
2072
ager@chromium.org7c537e22008-10-16 08:43:32 +00002073void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002074#ifdef DEBUG
2075 int original_height = frame_->height();
2076#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002077 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002081 JumpTarget primitive;
2082 JumpTarget jsobject;
2083 JumpTarget fixed_array;
2084 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2085 JumpTarget end_del_check;
2086 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087
2088 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002089 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090
2091 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2092 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002093 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002094 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2095 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002097 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2098 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100
2101 // Stack layout in body:
2102 // [iteration counter (Smi)]
2103 // [length of array]
2104 // [FixedArray]
2105 // [Map or 0]
2106 // [Object]
2107
2108 // Check if enumerable is already a JSObject
2109 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002111 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002114 primitive.Bind();
2115 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002116 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002120 // r0: value to be iterated over
2121 frame_->EmitPush(r0); // Push the object being iterated over.
2122
2123 // Check cache validity in generated code. This is a fast case for
2124 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2125 // guarantee cache validity, call the runtime system to check cache
2126 // validity or get the property names in a fixed array.
2127 JumpTarget call_runtime;
2128 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2129 JumpTarget check_prototype;
2130 JumpTarget use_cache;
2131 __ mov(r1, Operand(r0));
2132 loop.Bind();
2133 // Check that there are no elements.
2134 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2135 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2136 __ cmp(r2, r4);
2137 call_runtime.Branch(ne);
2138 // Check that instance descriptors are not empty so that we can
2139 // check for an enum cache. Leave the map in r3 for the subsequent
2140 // prototype load.
2141 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2142 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2143 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2144 __ cmp(r2, ip);
2145 call_runtime.Branch(eq);
2146 // Check that there in an enum cache in the non-empty instance
2147 // descriptors. This is the case if the next enumeration index
2148 // field does not contain a smi.
2149 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2150 __ tst(r2, Operand(kSmiTagMask));
2151 call_runtime.Branch(eq);
2152 // For all objects but the receiver, check that the cache is empty.
2153 // r4: empty fixed array root.
2154 __ cmp(r1, r0);
2155 check_prototype.Branch(eq);
2156 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2157 __ cmp(r2, r4);
2158 call_runtime.Branch(ne);
2159 check_prototype.Bind();
2160 // Load the prototype from the map and loop if non-null.
2161 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2162 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2163 __ cmp(r1, ip);
2164 loop.Branch(ne);
2165 // The enum cache is valid. Load the map of the object being
2166 // iterated over and use the cache for the iteration.
2167 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2168 use_cache.Jump();
2169
2170 call_runtime.Bind();
2171 // Call the runtime to get the property names for the object.
2172 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002175 // If we got a map from the runtime call, we can do a fast
2176 // modification check. Otherwise, we got a fixed array, and we have
2177 // to do a slow check.
2178 // r0: map or fixed array (result from call to
2179 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 __ mov(r2, Operand(r0));
2181 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002182 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2183 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002184 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002186 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002188 // r0: map (either the result from a call to
2189 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2190 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 __ mov(r1, Operand(r0));
2192 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2193 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2194 __ ldr(r2,
2195 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2196
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 frame_->EmitPush(r0); // map
2198 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002199 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002201 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002202 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002203 frame_->EmitPush(r0);
2204 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002208 frame_->EmitPush(r1); // insert 0 in place of Map
2209 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210
2211 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002212 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002214 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002215 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217
2218 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002219 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002220 // sp[0] : index
2221 // sp[1] : array/enum cache length
2222 // sp[2] : array or enum cache
2223 // sp[3] : 0 or map
2224 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 // Grab the current frame's height for the break and continue
2226 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002227 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2228 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002230 __ ldr(r0, frame_->ElementAt(0)); // load the current count
2231 __ ldr(r1, frame_->ElementAt(1)); // load the length
2232 __ cmp(r0, Operand(r1)); // compare to the array length
2233 node->break_target()->Branch(hs);
2234
2235 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002238 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2240 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2241
2242 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 // Check if this (still) matches the map of the enumerable.
2245 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002246 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2248 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250
2251 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002252 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2253 frame_->EmitPush(r0);
2254 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002255 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002256 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
2258 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002259 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2260 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002263 end_del_check.Bind();
2264 // Store the entry in the 'each' expression and take another spin in the
2265 // loop. r3: i'th entry of the enum cache (or string there of)
2266 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 { Reference each(this, node->each());
2268 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002269 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 __ ldr(r0, frame_->ElementAt(each.size()));
2271 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002272 each.SetValue(NOT_CONST_INIT);
2273 frame_->Drop(2);
2274 } else {
2275 // If the reference was to a slot we rely on the convenient property
2276 // that it doesn't matter whether a value (eg, r3 pushed above) is
2277 // right on top of or right underneath a zero-sized reference.
2278 each.SetValue(NOT_CONST_INIT);
2279 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002280 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 }
2282 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002283 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002285 VisitAndSpill(node->body());
2286
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002287 // Next. Reestablish a spilled frame in case we are coming here via
2288 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002290 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002291 frame_->EmitPop(r0);
2292 __ add(r0, r0, Operand(Smi::FromInt(1)));
2293 frame_->EmitPush(r0);
2294 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002296 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2297 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002298 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002299 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300
2301 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 exit.Bind();
2303 node->continue_target()->Unuse();
2304 node->break_target()->Unuse();
2305 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306}
2307
2308
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002309void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310#ifdef DEBUG
2311 int original_height = frame_->height();
2312#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002313 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002314 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002317 JumpTarget try_block;
2318 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002322 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323
2324 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002325 Variable* catch_var = node->catch_var()->var();
2326 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2327 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328
2329 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002330 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002332 VisitStatementsAndSpill(node->catch_block()->statements());
2333 if (frame_ != NULL) {
2334 exit.Jump();
2335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336
2337
2338 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2342 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002344 // Shadow the labels for all escapes from the try block, including
2345 // returns. During shadowing, the original label is hidden as the
2346 // LabelShadow and operations on the original actually affect the
2347 // shadowing label.
2348 //
2349 // We should probably try to unify the escaping labels and the return
2350 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 int nof_escapes = node->escaping_targets()->length();
2352 List<ShadowTarget*> shadows(1 + nof_escapes);
2353
2354 // Add the shadow target for the function return.
2355 static const int kReturnShadowIndex = 0;
2356 shadows.Add(new ShadowTarget(&function_return_));
2357 bool function_return_was_shadowed = function_return_is_shadowed_;
2358 function_return_is_shadowed_ = true;
2359 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2360
2361 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 }
2365
2366 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002367 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368
2369 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002370 // After shadowing stops, the original labels are unshadowed and the
2371 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002372 bool has_unlinks = false;
2373 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 has_unlinks = has_unlinks || shadows[i]->is_linked();
2376 }
2377 function_return_is_shadowed_ = function_return_was_shadowed;
2378
2379 // Get an external reference to the handler address.
2380 ExternalReference handler_address(Top::k_handler_address);
2381
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002382 // If we can fall off the end of the try block, unlink from try chain.
2383 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002384 // The next handler address is on top of the frame. Unlink from
2385 // the handler list and drop the rest of this handler from the
2386 // frame.
2387 ASSERT(StackHandlerConstants::kNextOffset == 0);
2388 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002389 __ mov(r3, Operand(handler_address));
2390 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002391 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002392 if (has_unlinks) {
2393 exit.Jump();
2394 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 }
2396
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002397 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002398 // jumped to. Deallocate each shadow target.
2399 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002401 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002402 shadows[i]->Bind();
2403 // Because we can be jumping here (to spilled code) from unspilled
2404 // code, we need to reestablish a spilled frame at this block.
2405 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407 // Reload sp from the top handler, because some statements that we
2408 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002409 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002411 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002413 ASSERT(StackHandlerConstants::kNextOffset == 0);
2414 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002416 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002418 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2419 frame_->PrepareForReturn();
2420 }
2421 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 }
2423 }
2424
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 exit.Bind();
2426 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427}
2428
2429
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002430void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002431#ifdef DEBUG
2432 int original_height = frame_->height();
2433#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002434 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002435 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002436 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
2438 // State: Used to keep track of reason for entering the finally
2439 // block. Should probably be extended to hold information for
2440 // break/continue from within the try block.
2441 enum { FALLING, THROWING, JUMPING };
2442
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002443 JumpTarget try_block;
2444 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002446 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002448 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 // In case of thrown exceptions, this is where we continue.
2450 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002451 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452
2453 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002454 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2457 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002459 // Shadow the labels for all escapes from the try block, including
2460 // returns. Shadowing hides the original label as the LabelShadow and
2461 // operations on the original actually affect the shadowing label.
2462 //
2463 // We should probably try to unify the escaping labels and the return
2464 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 int nof_escapes = node->escaping_targets()->length();
2466 List<ShadowTarget*> shadows(1 + nof_escapes);
2467
2468 // Add the shadow target for the function return.
2469 static const int kReturnShadowIndex = 0;
2470 shadows.Add(new ShadowTarget(&function_return_));
2471 bool function_return_was_shadowed = function_return_is_shadowed_;
2472 function_return_is_shadowed_ = true;
2473 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2474
2475 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 }
2479
2480 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002481 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002483 // Stop the introduced shadowing and count the number of required unlinks.
2484 // After shadowing stops, the original labels are unshadowed and the
2485 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 shadows[i]->StopShadowing();
2489 if (shadows[i]->is_linked()) nof_unlinks++;
2490 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002491 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493 // Get an external reference to the handler address.
2494 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002496 // If we can fall off the end of the try block, unlink from the try
2497 // chain and set the state on the frame to FALLING.
2498 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002499 // The next handler address is on top of the frame.
2500 ASSERT(StackHandlerConstants::kNextOffset == 0);
2501 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002502 __ mov(r3, Operand(handler_address));
2503 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002504 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002505
2506 // Fake a top of stack value (unneeded when FALLING) and set the
2507 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002508 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 frame_->EmitPush(r0);
2510 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2511 if (nof_unlinks > 0) {
2512 finally_block.Jump();
2513 }
2514 }
2515
2516 // Generate code to unlink and set the state for the (formerly)
2517 // shadowing targets that have been jumped to.
2518 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002520 // If we have come from the shadowed return, the return value is
2521 // in (a non-refcounted reference to) r0. We must preserve it
2522 // until it is pushed.
2523 //
2524 // Because we can be jumping here (to spilled code) from
2525 // unspilled code, we need to reestablish a spilled frame at
2526 // this block.
2527 shadows[i]->Bind();
2528 frame_->SpillAll();
2529
2530 // Reload sp from the top handler, because some statements that
2531 // we break from (eg, for...in) may have left stuff on the
2532 // stack.
2533 __ mov(r3, Operand(handler_address));
2534 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002535 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002536
2537 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002538 // handler address is currently on top of the frame.
2539 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002540 frame_->EmitPop(r1);
2541 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002542 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543
2544 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002545 // If this label shadowed the function return, materialize the
2546 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002547 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002548 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002550 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 }
2553 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 if (--nof_unlinks > 0) {
2555 // If this is not the last unlink block, jump around the next.
2556 finally_block.Jump();
2557 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 }
2559 }
2560
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002564 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002566
2567 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568 // and the state - while evaluating the finally block.
2569 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 if (has_valid_frame()) {
2574 // Restore state and return value or faked TOS.
2575 frame_->EmitPop(r2);
2576 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 }
2578
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002579 // Generate code to jump to the right destination for all used
2580 // formerly shadowing targets. Deallocate each shadow target.
2581 for (int i = 0; i < shadows.length(); i++) {
2582 if (has_valid_frame() && shadows[i]->is_bound()) {
2583 JumpTarget* original = shadows[i]->other_target();
2584 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2585 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002586 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002587 skip.Branch(ne);
2588 frame_->PrepareForReturn();
2589 original->Jump();
2590 skip.Bind();
2591 } else {
2592 original->Branch(eq);
2593 }
2594 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002597 if (has_valid_frame()) {
2598 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002599 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002600 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2601 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 // Rethrow exception.
2604 frame_->EmitPush(r0);
2605 frame_->CallRuntime(Runtime::kReThrow, 1);
2606
2607 // Done.
2608 exit.Bind();
2609 }
2610 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611}
2612
2613
ager@chromium.org7c537e22008-10-16 08:43:32 +00002614void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002615#ifdef DEBUG
2616 int original_height = frame_->height();
2617#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002618 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002621#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002622 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002623#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002624 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002625 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626}
2627
2628
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002629void CodeGenerator::InstantiateFunction(
2630 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002631 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002632 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002633 // Use the fast case closure allocation code that allocates in new
2634 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002635 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002636 FastNewClosureStub stub;
2637 frame_->EmitPush(r0);
2638 frame_->CallStub(&stub, 1);
2639 frame_->EmitPush(r0);
2640 } else {
2641 // Create a new closure.
2642 frame_->EmitPush(cp);
2643 frame_->EmitPush(r0);
2644 frame_->CallRuntime(Runtime::kNewClosure, 2);
2645 frame_->EmitPush(r0);
2646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647}
2648
2649
ager@chromium.org7c537e22008-10-16 08:43:32 +00002650void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651#ifdef DEBUG
2652 int original_height = frame_->height();
2653#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002654 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 Comment cmnt(masm_, "[ FunctionLiteral");
2656
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002657 // Build the function info and instantiate it.
2658 Handle<SharedFunctionInfo> function_info =
2659 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002660 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 if (HasStackOverflow()) {
2662 ASSERT(frame_->height() == original_height);
2663 return;
2664 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002665 InstantiateFunction(function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002666 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667}
2668
2669
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002670void CodeGenerator::VisitSharedFunctionInfoLiteral(
2671 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002672#ifdef DEBUG
2673 int original_height = frame_->height();
2674#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002675 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002676 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2677 InstantiateFunction(node->shared_function_info());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679}
2680
2681
ager@chromium.org7c537e22008-10-16 08:43:32 +00002682void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683#ifdef DEBUG
2684 int original_height = frame_->height();
2685#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002686 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002688 JumpTarget then;
2689 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002690 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002691 if (has_valid_frame()) {
2692 Branch(false, &else_);
2693 }
2694 if (has_valid_frame() || then.is_linked()) {
2695 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002696 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002697 }
2698 if (else_.is_linked()) {
2699 JumpTarget exit;
2700 if (has_valid_frame()) exit.Jump();
2701 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002702 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002703 if (exit.is_linked()) exit.Bind();
2704 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002705 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706}
2707
2708
ager@chromium.org7c537e22008-10-16 08:43:32 +00002709void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2710 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002711 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002712 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002714 JumpTarget slow;
2715 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002716
2717 // Generate fast-case code for variables that might be shadowed by
2718 // eval-introduced variables. Eval is used a lot without
2719 // introducing variables. In those cases, we do not want to
2720 // perform a runtime call for all variables in the scope
2721 // containing the eval.
2722 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2723 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 // If there was no control flow to slow, we can exit early.
2725 if (!slow.is_linked()) {
2726 frame_->EmitPush(r0);
2727 return;
2728 }
2729
2730 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002731
2732 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2733 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2734 // Only generate the fast case for locals that rewrite to slots.
2735 // This rules out argument loads.
2736 if (potential_slot != NULL) {
2737 __ ldr(r0,
2738 ContextSlotOperandCheckExtensions(potential_slot,
2739 r1,
2740 r2,
2741 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002742 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002743 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2744 __ cmp(r0, ip);
2745 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002746 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002747 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002748 // ContextSlotOperandCheckExtensions so we have to jump around
2749 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002750 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002751 }
2752 }
2753
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 slow.Bind();
2755 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002756 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758
ager@chromium.org7c537e22008-10-16 08:43:32 +00002759 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002761 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002762 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002764
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002765 done.Bind();
2766 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767
2768 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002769 Register scratch = VirtualFrame::scratch0();
2770 frame_->EmitPush(SlotOperand(slot, scratch));
ager@chromium.org7c537e22008-10-16 08:43:32 +00002771 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002772 // Const slots may contain 'the hole' value (the constant hasn't been
2773 // initialized yet) which needs to be converted into the 'undefined'
2774 // value.
2775 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002776 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002777 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002778 __ cmp(scratch, ip);
2779 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
2780 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781 }
2782 }
2783}
2784
2785
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002786void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2787 ASSERT(slot != NULL);
2788 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002789 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002790 ASSERT(slot->var()->is_dynamic());
2791
2792 // For now, just do a runtime call.
2793 frame_->EmitPush(cp);
2794 __ mov(r0, Operand(slot->var()->name()));
2795 frame_->EmitPush(r0);
2796
2797 if (init_state == CONST_INIT) {
2798 // Same as the case for a normal store, but ignores attribute
2799 // (e.g. READ_ONLY) of context slot so that we can initialize
2800 // const properties (introduced via eval("const foo = (some
2801 // expr);")). Also, uses the current function context instead of
2802 // the top context.
2803 //
2804 // Note that we must declare the foo upon entry of eval(), via a
2805 // context slot declaration, but we cannot initialize it at the
2806 // same time, because the const declaration may be at the end of
2807 // the eval code (sigh...) and the const variable may have been
2808 // used before (where its value is 'undefined'). Thus, we can only
2809 // do the initialization when we actually encounter the expression
2810 // and when the expression operands are defined and valid, and
2811 // thus we need the split into 2 operations: declaration of the
2812 // context slot followed by initialization.
2813 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2814 } else {
2815 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2816 }
2817 // Storing a variable must keep the (new) value on the expression
2818 // stack. This is necessary for compiling assignment expressions.
2819 frame_->EmitPush(r0);
2820
2821 } else {
2822 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00002823 Register scratch = VirtualFrame::scratch0();
2824 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002825
ager@chromium.org357bf652010-04-12 11:30:10 +00002826 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002827 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00002828
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002829 if (init_state == CONST_INIT) {
2830 ASSERT(slot->var()->mode() == Variable::CONST);
2831 // Only the first const initialization must be executed (the slot
2832 // still contains 'the hole' value). When the assignment is
2833 // executed, the code is identical to a normal store (see below).
2834 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002835 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002836 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002837 __ cmp(scratch, ip);
2838 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002839 exit.Branch(ne);
2840 }
2841
2842 // We must execute the store. Storing a variable must keep the
2843 // (new) value on the stack. This is necessary for compiling
2844 // assignment expressions.
2845 //
2846 // Note: We will reach here even with slot->var()->mode() ==
2847 // Variable::CONST because of const declarations which will
2848 // initialize consts to 'the hole' value and by doing so, end up
2849 // calling this code. r2 may be loaded with context; used below in
2850 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00002851 Register tos = frame_->Peek();
2852 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002853 if (slot->type() == Slot::CONTEXT) {
2854 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00002855 __ tst(tos, Operand(kSmiTagMask));
2856 // We don't use tos any more after here.
2857 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002858 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00002859 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002860 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2861 __ mov(r3, Operand(offset));
ager@chromium.org357bf652010-04-12 11:30:10 +00002862 // r1 could be identical with tos, but that doesn't matter.
2863 __ RecordWrite(scratch, r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002864 }
2865 // If we definitely did not jump over the assignment, we do not need
2866 // to bind the exit label. Doing so can defeat peephole
2867 // optimization.
2868 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002869 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002870 exit.Bind();
2871 }
2872 }
2873}
2874
2875
ager@chromium.org381abbb2009-02-25 13:23:22 +00002876void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2877 TypeofState typeof_state,
2878 Register tmp,
2879 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002881 // Check that no extension objects have been created by calls to
2882 // eval from the current scope to the global scope.
2883 Register context = cp;
2884 Scope* s = scope();
2885 while (s != NULL) {
2886 if (s->num_heap_slots() > 0) {
2887 if (s->calls_eval()) {
2888 // Check that extension is NULL.
2889 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2890 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002891 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002892 }
2893 // Load next context in chain.
2894 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2895 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2896 context = tmp;
2897 }
2898 // If no outer scope calls eval, we do not need to check more
2899 // context extensions.
2900 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2901 s = s->outer_scope();
2902 }
2903
2904 if (s->is_eval_scope()) {
2905 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00002906 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002907 __ bind(&next);
2908 // Terminate at global context.
2909 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002910 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2911 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002912 __ b(eq, &fast);
2913 // Check that extension is NULL.
2914 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2915 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002916 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002917 // Load next context in chain.
2918 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2919 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2920 __ b(&next);
2921 __ bind(&fast);
2922 }
2923
2924 // All extension objects were empty and it is safe to use a global
2925 // load IC call.
2926 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2927 // Load the global object.
2928 LoadGlobal();
2929 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002930 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002931 // Call IC stub.
2932 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002933 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002934 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002935 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002936 }
2937
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002938 // Drop the global object. The result is in r0.
2939 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002940}
2941
2942
ager@chromium.org7c537e22008-10-16 08:43:32 +00002943void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002944#ifdef DEBUG
2945 int original_height = frame_->height();
2946#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00002947 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002948 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002949 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002950}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951
ager@chromium.org7c537e22008-10-16 08:43:32 +00002952
2953void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954#ifdef DEBUG
2955 int original_height = frame_->height();
2956#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00002957 Comment cmnt(masm_, "[ VariableProxy");
2958
2959 Variable* var = node->var();
2960 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002961 if (expr != NULL) {
2962 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002964 ASSERT(var->is_global());
2965 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00002966 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002968 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969}
2970
2971
ager@chromium.org7c537e22008-10-16 08:43:32 +00002972void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002973#ifdef DEBUG
2974 int original_height = frame_->height();
2975#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00002977 Register reg = frame_->GetTOSRegister();
2978 __ mov(reg, Operand(node->handle()));
2979 frame_->EmitPush(reg);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002980 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981}
2982
2983
ager@chromium.org7c537e22008-10-16 08:43:32 +00002984void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002985#ifdef DEBUG
2986 int original_height = frame_->height();
2987#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002988 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989 Comment cmnt(masm_, "[ RexExp Literal");
2990
2991 // Retrieve the literal array and check the allocated entry.
2992
2993 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002994 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995
2996 // Load the literals array of the function.
2997 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2998
2999 // Load the literal at the ast saved index.
3000 int literal_offset =
3001 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3002 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3003
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003004 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003005 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3006 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003007 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008
3009 // If the entry is undefined we call the runtime system to computed
3010 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003011 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003012 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003014 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003015 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003016 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003017 frame_->EmitPush(r0);
3018 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003019 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003021 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003023 frame_->EmitPush(r2);
3024 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025}
3026
3027
ager@chromium.org7c537e22008-10-16 08:43:32 +00003028void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003029#ifdef DEBUG
3030 int original_height = frame_->height();
3031#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003032 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033 Comment cmnt(masm_, "[ ObjectLiteral");
3034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003035 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003036 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003037 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003038 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003039 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003040 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003041 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003042 __ mov(r1, Operand(node->constant_properties()));
3043 // Should the object literal have fast elements?
3044 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3045 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003046 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003047 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003048 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003049 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003050 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003053 // At the start of each iteration, the top of stack contains
3054 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003055 ObjectLiteral::Property* property = node->properties()->at(i);
3056 Literal* key = property->key();
3057 Expression* value = property->value();
3058 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003059 case ObjectLiteral::Property::CONSTANT:
3060 break;
3061 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3062 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3063 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003064 case ObjectLiteral::Property::COMPUTED:
3065 if (key->handle()->IsSymbol()) {
3066 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3067 LoadAndSpill(value);
3068 frame_->EmitPop(r0);
3069 __ mov(r2, Operand(key->handle()));
3070 __ ldr(r1, frame_->Top()); // Load the receiver.
3071 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3072 break;
3073 }
3074 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003076 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003077 frame_->EmitPush(r0); // dup the result
3078 LoadAndSpill(key);
3079 LoadAndSpill(value);
3080 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081 break;
3082 }
3083 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003084 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003085 frame_->EmitPush(r0);
3086 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003087 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 frame_->EmitPush(r0);
3089 LoadAndSpill(value);
3090 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003091 break;
3092 }
3093 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003094 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003095 frame_->EmitPush(r0);
3096 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003097 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003098 frame_->EmitPush(r0);
3099 LoadAndSpill(value);
3100 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 break;
3102 }
3103 }
3104 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003105 ASSERT(frame_->height() == original_height + 1);
3106}
3107
3108
ager@chromium.org7c537e22008-10-16 08:43:32 +00003109void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003110#ifdef DEBUG
3111 int original_height = frame_->height();
3112#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003113 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003115
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003116 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003117 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003118 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003119 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003120 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003121 __ mov(r0, Operand(node->constant_elements()));
3122 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003123 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003124 if (node->depth() > 1) {
3125 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003126 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003127 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003128 } else {
3129 FastCloneShallowArrayStub stub(length);
3130 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003131 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003132 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003133 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003134
3135 // Generate code to set the elements in the array that are not
3136 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137 for (int i = 0; i < node->values()->length(); i++) {
3138 Expression* value = node->values()->at(i);
3139
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003140 // If value is a literal the property value is already set in the
3141 // boilerplate object.
3142 if (value->AsLiteral() != NULL) continue;
3143 // If value is a materialized literal the property value is already set
3144 // in the boilerplate object if it is simple.
3145 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003147 // The property must be set by generated code.
3148 LoadAndSpill(value);
3149 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003151 // Fetch the object literal.
3152 __ ldr(r1, frame_->Top());
3153 // Get the elements array.
3154 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003156 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003157 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003158 __ str(r0, FieldMemOperand(r1, offset));
3159
3160 // Update the write barrier for the array address.
3161 __ mov(r3, Operand(offset));
3162 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003164 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165}
3166
3167
ager@chromium.org32912102009-01-16 10:38:43 +00003168void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003169#ifdef DEBUG
3170 int original_height = frame_->height();
3171#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003172 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org32912102009-01-16 10:38:43 +00003173 // Call runtime routine to allocate the catch extension object and
3174 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003175 Comment cmnt(masm_, "[ CatchExtensionObject");
3176 LoadAndSpill(node->key());
3177 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003178 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3179 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003180 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00003181}
3182
3183
ager@chromium.org7c537e22008-10-16 08:43:32 +00003184void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003185 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003186#ifdef DEBUG
3187 int original_height = frame_->height();
3188#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003190
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003191 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003192 if (target.is_illegal()) {
3193 // Fool the virtual frame into thinking that we left the assignment's
3194 // value on the frame.
ager@chromium.org357bf652010-04-12 11:30:10 +00003195 Register tos = frame_->GetTOSRegister();
3196 __ mov(tos, Operand(Smi::FromInt(0)));
3197 frame_->EmitPush(tos);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003198 ASSERT(frame_->height() == original_height + 1);
3199 return;
3200 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003201
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003202 if (node->op() == Token::ASSIGN ||
3203 node->op() == Token::INIT_VAR ||
3204 node->op() == Token::INIT_CONST) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003205 Load(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00003206
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003207 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003208 // Get the old value of the lhs.
ager@chromium.org357bf652010-04-12 11:30:10 +00003209 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003210 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003211 bool overwrite =
3212 (node->value()->AsBinaryOperation() != NULL &&
3213 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003214 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003215 VirtualFrameSmiOperation(node->binary_op(),
3216 literal->handle(),
3217 false,
3218 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003220 Load(node->value());
3221 VirtualFrameBinaryOperation(node->binary_op(),
3222 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003224 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003225 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3226 if (var != NULL &&
3227 (var->mode() == Variable::CONST) &&
3228 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3229 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003230 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003231 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003232 CodeForSourcePosition(node->position());
3233 if (node->op() == Token::INIT_CONST) {
3234 // Dynamic constant initializations must use the function context
3235 // and initialize the actual constant declared. Dynamic variable
3236 // initializations are simply assignments and use SetValue.
3237 target.SetValue(CONST_INIT);
3238 } else {
3239 target.SetValue(NOT_CONST_INIT);
3240 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 }
3242 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003243 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244}
3245
3246
ager@chromium.org7c537e22008-10-16 08:43:32 +00003247void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248#ifdef DEBUG
3249 int original_height = frame_->height();
3250#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003251 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252 Comment cmnt(masm_, "[ Throw");
3253
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003254 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003255 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003256 frame_->CallRuntime(Runtime::kThrow, 1);
3257 frame_->EmitPush(r0);
3258 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259}
3260
3261
ager@chromium.org7c537e22008-10-16 08:43:32 +00003262void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003263#ifdef DEBUG
3264 int original_height = frame_->height();
3265#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003266 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003268
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003269 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003270 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003271 }
3272 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273}
3274
3275
ager@chromium.org7c537e22008-10-16 08:43:32 +00003276void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003277#ifdef DEBUG
3278 int original_height = frame_->height();
3279#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003280 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 Comment cmnt(masm_, "[ Call");
3282
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003283 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284 ZoneList<Expression*>* args = node->arguments();
3285
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 Variable* var = function->AsVariableProxy()->AsVariable();
3289 Property* property = function->AsProperty();
3290
3291 // ------------------------------------------------------------------------
3292 // Fast-case: Use inline caching.
3293 // ---
3294 // According to ECMA-262, section 11.2.3, page 44, the function to call
3295 // must be resolved after the arguments have been evaluated. The IC code
3296 // automatically handles this by loading the arguments before the function
3297 // is resolved in cache misses (this also holds for megamorphic calls).
3298 // ------------------------------------------------------------------------
3299
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003300 if (var != NULL && var->is_possibly_eval()) {
3301 // ----------------------------------
3302 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3303 // ----------------------------------
3304
3305 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3306 // resolve the function we need to call and the receiver of the
3307 // call. Then we call the resolved function using the given
3308 // arguments.
3309 // Prepare stack for call to resolved function.
3310 LoadAndSpill(function);
3311 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3312 frame_->EmitPush(r2); // Slot for receiver
3313 int arg_count = args->length();
3314 for (int i = 0; i < arg_count; i++) {
3315 LoadAndSpill(args->at(i));
3316 }
3317
3318 // Prepare stack for call to ResolvePossiblyDirectEval.
3319 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3320 frame_->EmitPush(r1);
3321 if (arg_count > 0) {
3322 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3323 frame_->EmitPush(r1);
3324 } else {
3325 frame_->EmitPush(r2);
3326 }
3327
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003328 // Push the receiver.
3329 __ ldr(r1, frame_->Receiver());
3330 frame_->EmitPush(r1);
3331
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003332 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003333 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003334
3335 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003336 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003337 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3338
3339 // Call the function.
3340 CodeForSourcePosition(node->position());
3341
3342 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003343 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003344 frame_->CallStub(&call_function, arg_count + 1);
3345
3346 __ ldr(cp, frame_->Context());
3347 // Remove the function from the stack.
3348 frame_->Drop();
3349 frame_->EmitPush(r0);
3350
3351 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352 // ----------------------------------
3353 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3354 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003355 // Pass the global object as the receiver and let the IC stub
3356 // patch the stack to use the global proxy as 'this' in the
3357 // invoked function.
3358 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359
3360 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003361 int arg_count = args->length();
3362 for (int i = 0; i < arg_count; i++) {
3363 LoadAndSpill(args->at(i));
3364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365
ager@chromium.org5c838252010-02-19 08:53:10 +00003366 // Setup the name register and call the IC initialization code.
3367 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003368 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3369 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003370 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003371 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3372 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003373 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003374 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003375
3376 } else if (var != NULL && var->slot() != NULL &&
3377 var->slot()->type() == Slot::LOOKUP) {
3378 // ----------------------------------
3379 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3380 // ----------------------------------
3381
3382 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003383 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003384 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003385 frame_->EmitPush(r0);
3386 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387 // r0: slot value; r1: receiver
3388
3389 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003390 frame_->EmitPush(r0); // function
3391 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003392
3393 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003394 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003395 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396
3397 } else if (property != NULL) {
3398 // Check if the key is a literal string.
3399 Literal* literal = property->key()->AsLiteral();
3400
3401 if (literal != NULL && literal->handle()->IsSymbol()) {
3402 // ------------------------------------------------------------------
3403 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3404 // ------------------------------------------------------------------
3405
ager@chromium.org5c838252010-02-19 08:53:10 +00003406 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003407 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003408 int arg_count = args->length();
3409 for (int i = 0; i < arg_count; i++) {
3410 LoadAndSpill(args->at(i));
3411 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003412
ager@chromium.org5c838252010-02-19 08:53:10 +00003413 // Set the name register and call the IC initialization code.
3414 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003415 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3416 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003417 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003418 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003419 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003420 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003421
3422 } else {
3423 // -------------------------------------------
3424 // JavaScript example: 'array[index](1, 2, 3)'
3425 // -------------------------------------------
3426
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003427 LoadAndSpill(property->obj());
3428 LoadAndSpill(property->key());
3429 EmitKeyedLoad(false);
3430 frame_->Drop(); // key
3431 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003432 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003433 // Use the global receiver.
3434 frame_->Drop();
3435 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003436 LoadGlobalReceiver(r0);
3437 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003438 frame_->EmitPop(r1); // receiver
3439 frame_->EmitPush(r0); // function
3440 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003441 }
3442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003444 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003445 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 }
3447
3448 } else {
3449 // ----------------------------------
3450 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3451 // ----------------------------------
3452
3453 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003454 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003455
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003456 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003457 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003460 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003461 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003462 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003463 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003464}
3465
3466
ager@chromium.org7c537e22008-10-16 08:43:32 +00003467void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003468#ifdef DEBUG
3469 int original_height = frame_->height();
3470#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003471 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003472 Comment cmnt(masm_, "[ CallNew");
3473
3474 // According to ECMA-262, section 11.2.2, page 44, the function
3475 // expression in new calls must be evaluated before the
3476 // arguments. This is different from ordinary calls, where the
3477 // actual function to call is resolved after the arguments have been
3478 // evaluated.
3479
3480 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003481 // receiver. There is no need to use the global proxy here because
3482 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003484 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485
3486 // Push the arguments ("left-to-right") on the stack.
3487 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003488 int arg_count = args->length();
3489 for (int i = 0; i < arg_count; i++) {
3490 LoadAndSpill(args->at(i));
3491 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492
mads.s.ager31e71382008-08-13 09:32:07 +00003493 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003494 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003495 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003496 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 // Call the construct call builtin that handles allocation and
3499 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003500 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003501 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003502 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003503
3504 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003505 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003506 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507}
3508
3509
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003510void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003511 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003512 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003513 JumpTarget leave, null, function, non_function_constructor;
3514
3515 // Load the object into r0.
3516 LoadAndSpill(args->at(0));
3517 frame_->EmitPop(r0);
3518
3519 // If the object is a smi, we return null.
3520 __ tst(r0, Operand(kSmiTagMask));
3521 null.Branch(eq);
3522
3523 // Check that the object is a JS object but take special care of JS
3524 // functions to make sure they have 'Function' as their class.
3525 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3526 null.Branch(lt);
3527
3528 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3529 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3530 // LAST_JS_OBJECT_TYPE.
3531 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3532 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3533 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3534 function.Branch(eq);
3535
3536 // Check if the constructor in the map is a function.
3537 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3538 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3539 non_function_constructor.Branch(ne);
3540
3541 // The r0 register now contains the constructor function. Grab the
3542 // instance class name from there.
3543 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3544 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003545 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003546 leave.Jump();
3547
3548 // Functions have class 'Function'.
3549 function.Bind();
3550 __ mov(r0, Operand(Factory::function_class_symbol()));
3551 frame_->EmitPush(r0);
3552 leave.Jump();
3553
3554 // Objects with a non-function constructor have class 'Object'.
3555 non_function_constructor.Bind();
3556 __ mov(r0, Operand(Factory::Object_symbol()));
3557 frame_->EmitPush(r0);
3558 leave.Jump();
3559
3560 // Non-JS objects have class null.
3561 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003562 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003563 frame_->EmitPush(r0);
3564
3565 // All done.
3566 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003567}
3568
3569
ager@chromium.org7c537e22008-10-16 08:43:32 +00003570void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003571 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003573 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003574 LoadAndSpill(args->at(0));
3575 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003576 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003578 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003579 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3580 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003581 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582 // Load the value.
3583 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 leave.Bind();
3585 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586}
3587
3588
ager@chromium.org7c537e22008-10-16 08:43:32 +00003589void CodeGenerator::GenerateSetValueOf(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() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003592 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 LoadAndSpill(args->at(0)); // Load the object.
3594 LoadAndSpill(args->at(1)); // Load the value.
3595 frame_->EmitPop(r0); // r0 contains value
3596 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003597 // if (object->IsSmi()) return object.
3598 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003599 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003600 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3601 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003602 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 // Store the value.
3604 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3605 // Update the write barrier.
3606 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3607 __ RecordWrite(r1, r2, r3);
3608 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003609 leave.Bind();
3610 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611}
3612
3613
ager@chromium.org7c537e22008-10-16 08:43:32 +00003614void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003615 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003617 LoadAndSpill(args->at(0));
3618 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003619 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620 cc_reg_ = eq;
3621}
3622
3623
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003624void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003625 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003626 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3627 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003628#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003629 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003630 LoadAndSpill(args->at(1));
3631 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003632 __ CallRuntime(Runtime::kLog, 2);
3633 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003634#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003635 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003636 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003637}
3638
3639
ager@chromium.org7c537e22008-10-16 08:43:32 +00003640void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003641 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003642 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003643 LoadAndSpill(args->at(0));
3644 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003645 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003646 cc_reg_ = eq;
3647}
3648
3649
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003650// Generates the Math.pow method - currently just calls runtime.
3651void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3652 ASSERT(args->length() == 2);
3653 Load(args->at(0));
3654 Load(args->at(1));
3655 frame_->CallRuntime(Runtime::kMath_pow, 2);
3656 frame_->EmitPush(r0);
3657}
3658
3659
3660// Generates the Math.sqrt method - currently just calls runtime.
3661void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
3662 ASSERT(args->length() == 1);
3663 Load(args->at(0));
3664 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
3665 frame_->EmitPush(r0);
3666}
3667
3668
kasper.lund7276f142008-07-30 08:49:36 +00003669// This should generate code that performs a charCodeAt() call or returns
3670// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3671// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003672void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003673 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00003674 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003675 Comment(masm_, "[ GenerateFastCharCodeAt");
3676
3677 LoadAndSpill(args->at(0));
3678 LoadAndSpill(args->at(1));
3679 frame_->EmitPop(r0); // Index.
3680 frame_->EmitPop(r1); // String.
3681
3682 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3683
3684 __ tst(r1, Operand(kSmiTagMask));
3685 __ b(eq, &slow); // The 'string' was a Smi.
3686
3687 ASSERT(kSmiTag == 0);
3688 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3689 __ b(ne, &slow); // The index was negative or not a Smi.
3690
3691 __ bind(&try_again_with_new_string);
3692 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3693 __ b(ge, &slow);
3694
3695 // Now r2 has the string type.
3696 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003697 // Now r3 has the length of the string. Compare with the index.
3698 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3699 __ b(le, &slow);
3700
3701 // Here we know the index is in range. Check that string is sequential.
3702 ASSERT_EQ(0, kSeqStringTag);
3703 __ tst(r2, Operand(kStringRepresentationMask));
3704 __ b(ne, &not_a_flat_string);
3705
3706 // Check whether it is an ASCII string.
3707 ASSERT_EQ(0, kTwoByteStringTag);
3708 __ tst(r2, Operand(kStringEncodingMask));
3709 __ b(ne, &ascii_string);
3710
3711 // 2-byte string. We can add without shifting since the Smi tag size is the
3712 // log2 of the number of bytes in a two-byte character.
3713 ASSERT_EQ(1, kSmiTagSize);
3714 ASSERT_EQ(0, kSmiShiftSize);
3715 __ add(r1, r1, Operand(r0));
3716 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3717 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3718 __ jmp(&end);
3719
3720 __ bind(&ascii_string);
3721 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3722 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3723 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3724 __ jmp(&end);
3725
3726 __ bind(&not_a_flat_string);
3727 __ and_(r2, r2, Operand(kStringRepresentationMask));
3728 __ cmp(r2, Operand(kConsStringTag));
3729 __ b(ne, &slow);
3730
3731 // ConsString.
3732 // Check that the right hand side is the empty string (ie if this is really a
3733 // flat string in a cons string). If that is not the case we would rather go
3734 // to the runtime system now, to flatten the string.
3735 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3736 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3737 __ cmp(r2, Operand(r3));
3738 __ b(ne, &slow);
3739
3740 // Get the first of the two strings.
3741 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3742 __ jmp(&try_again_with_new_string);
3743
3744 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003745 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003746
3747 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003748 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003749}
3750
3751
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003752void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
3753 Comment(masm_, "[ GenerateCharFromCode");
3754 ASSERT(args->length() == 1);
3755
3756 LoadAndSpill(args->at(0));
3757 frame_->EmitPop(r0);
3758
3759 JumpTarget slow_case;
3760 JumpTarget exit;
3761
3762 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3763 ASSERT(kSmiTag == 0);
3764 ASSERT(kSmiShiftSize == 0);
3765 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3766 __ tst(r0, Operand(kSmiTagMask |
3767 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3768 slow_case.Branch(nz);
3769
3770 ASSERT(kSmiTag == 0);
3771 __ mov(r1, Operand(Factory::single_character_string_cache()));
3772 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
3773 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
3774 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3775 __ cmp(r1, ip);
3776 slow_case.Branch(eq);
3777
3778 frame_->EmitPush(r1);
3779 exit.Jump();
3780
3781 slow_case.Bind();
3782 frame_->EmitPush(r0);
3783 frame_->CallRuntime(Runtime::kCharFromCode, 1);
3784 frame_->EmitPush(r0);
3785
3786 exit.Bind();
3787}
3788
3789
ager@chromium.org7c537e22008-10-16 08:43:32 +00003790void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003791 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003792 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003793 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003794 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003795 // We need the CC bits to come out as not_equal in the case where the
3796 // object is a smi. This can't be done with the usual test opcode so
3797 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003798 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003799 __ and_(r1, r0, Operand(kSmiTagMask));
3800 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003801 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003802 // It is a heap object - get the map. Check if the object is a JS array.
3803 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003804 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805 cc_reg_ = eq;
3806}
3807
3808
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003809void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003810 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003811 ASSERT(args->length() == 1);
3812 LoadAndSpill(args->at(0));
3813 JumpTarget answer;
3814 // 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.
3817 frame_->EmitPop(r0);
3818 __ and_(r1, r0, Operand(kSmiTagMask));
3819 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3820 answer.Branch(ne);
3821 // It is a heap object - get the map. Check if the object is a regexp.
3822 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3823 answer.Bind();
3824 cc_reg_ = eq;
3825}
3826
3827
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003828void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3829 // This generates a fast version of:
3830 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00003831 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003832 ASSERT(args->length() == 1);
3833 LoadAndSpill(args->at(0));
3834 frame_->EmitPop(r1);
3835 __ tst(r1, Operand(kSmiTagMask));
3836 false_target()->Branch(eq);
3837
3838 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3839 __ cmp(r1, ip);
3840 true_target()->Branch(eq);
3841
3842 Register map_reg = r2;
3843 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3844 // Undetectable objects behave like undefined when tested with typeof.
3845 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3846 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3847 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3848 false_target()->Branch(eq);
3849
3850 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3851 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3852 false_target()->Branch(lt);
3853 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3854 cc_reg_ = le;
3855}
3856
3857
3858void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3859 // This generates a fast version of:
3860 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00003861 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003862 ASSERT(args->length() == 1);
3863 LoadAndSpill(args->at(0));
3864 frame_->EmitPop(r0);
3865 __ tst(r0, Operand(kSmiTagMask));
3866 false_target()->Branch(eq);
3867 Register map_reg = r2;
3868 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3869 cc_reg_ = eq;
3870}
3871
3872
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003873void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003874 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003875 ASSERT(args->length() == 1);
3876 LoadAndSpill(args->at(0));
3877 frame_->EmitPop(r0);
3878 __ tst(r0, Operand(kSmiTagMask));
3879 false_target()->Branch(eq);
3880 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3881 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3882 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3883 cc_reg_ = ne;
3884}
3885
3886
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003887void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003888 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003889 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003890
3891 // Get the frame pointer for the calling frame.
3892 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3893
3894 // Skip the arguments adaptor frame if it exists.
3895 Label check_frame_marker;
3896 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003897 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003898 __ b(ne, &check_frame_marker);
3899 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3900
3901 // Check the marker in the calling frame.
3902 __ bind(&check_frame_marker);
3903 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3904 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3905 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003906}
3907
3908
ager@chromium.org7c537e22008-10-16 08:43:32 +00003909void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003910 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 ASSERT(args->length() == 0);
3912
lrn@chromium.org25156de2010-04-06 13:10:27 +00003913 Label exit;
3914
3915 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00003916 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917
lrn@chromium.org25156de2010-04-06 13:10:27 +00003918 // Check if the calling frame is an arguments adaptor frame.
3919 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3920 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3921 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3922 __ b(ne, &exit);
3923
3924 // Arguments adaptor case: Read the arguments length from the
3925 // adaptor frame.
3926 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3927
3928 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003929 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930}
3931
3932
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003933void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003934 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 ASSERT(args->length() == 1);
3936
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003937 // Satisfy contract with ArgumentsAccessStub:
3938 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003939 LoadAndSpill(args->at(0));
3940 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003941 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942
3943 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003944 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 frame_->CallStub(&stub, 0);
3946 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947}
3948
3949
ager@chromium.org357bf652010-04-12 11:30:10 +00003950void CodeGenerator::GenerateRandomHeapNumber(
3951 ZoneList<Expression*>* args) {
3952 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003953 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00003954
3955 Label slow_allocate_heapnumber;
3956 Label heapnumber_allocated;
3957
3958 __ AllocateHeapNumber(r0, r1, r2, &slow_allocate_heapnumber);
3959 __ jmp(&heapnumber_allocated);
3960
3961 __ bind(&slow_allocate_heapnumber);
3962 __ mov(r0, Operand(Smi::FromInt(0)));
3963 __ push(r0);
3964 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
3965
3966 __ bind(&heapnumber_allocated);
3967 __ PrepareCallCFunction(1, r1);
3968 __ CallCFunction(
3969 ExternalReference::fill_heap_number_with_random_function(), 1);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003970 frame_->EmitPush(r0);
3971}
3972
3973
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003974void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3975 ASSERT_EQ(2, args->length());
3976
3977 Load(args->at(0));
3978 Load(args->at(1));
3979
ager@chromium.org5c838252010-02-19 08:53:10 +00003980 StringAddStub stub(NO_STRING_ADD_FLAGS);
3981 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003982 frame_->EmitPush(r0);
3983}
3984
3985
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003986void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3987 ASSERT_EQ(3, args->length());
3988
3989 Load(args->at(0));
3990 Load(args->at(1));
3991 Load(args->at(2));
3992
ager@chromium.org5c838252010-02-19 08:53:10 +00003993 SubStringStub stub;
3994 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003995 frame_->EmitPush(r0);
3996}
3997
3998
3999void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4000 ASSERT_EQ(2, args->length());
4001
4002 Load(args->at(0));
4003 Load(args->at(1));
4004
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004005 StringCompareStub stub;
4006 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004007 frame_->EmitPush(r0);
4008}
4009
4010
4011void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4012 ASSERT_EQ(4, args->length());
4013
4014 Load(args->at(0));
4015 Load(args->at(1));
4016 Load(args->at(2));
4017 Load(args->at(3));
4018
4019 frame_->CallRuntime(Runtime::kRegExpExec, 4);
4020 frame_->EmitPush(r0);
4021}
4022
4023
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004024void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4025 // No stub. This code only occurs a few times in regexp.js.
4026 const int kMaxInlineLength = 100;
4027 ASSERT_EQ(3, args->length());
4028 Load(args->at(0)); // Size of array, smi.
4029 Load(args->at(1)); // "index" property value.
4030 Load(args->at(2)); // "input" property value.
4031 {
4032 VirtualFrame::SpilledScope spilled_scope(frame_);
4033 Label slowcase;
4034 Label done;
4035 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4036 STATIC_ASSERT(kSmiTag == 0);
4037 STATIC_ASSERT(kSmiTagSize == 1);
4038 __ tst(r1, Operand(kSmiTagMask));
4039 __ b(ne, &slowcase);
4040 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4041 __ b(hi, &slowcase);
4042 // Smi-tagging is equivalent to multiplying by 2.
4043 // Allocate RegExpResult followed by FixedArray with size in ebx.
4044 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4045 // Elements: [Map][Length][..elements..]
4046 // Size of JSArray with two in-object properties and the header of a
4047 // FixedArray.
4048 int objects_size =
4049 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4050 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4051 __ add(r2, r5, Operand(objects_size));
4052 __ AllocateInNewSpace(r2, // In: Size, in words.
4053 r0, // Out: Start of allocation (tagged).
4054 r3, // Scratch register.
4055 r4, // Scratch register.
4056 &slowcase,
4057 TAG_OBJECT);
4058 // r0: Start of allocated area, object-tagged.
4059 // r1: Number of elements in array, as smi.
4060 // r5: Number of elements, untagged.
4061
4062 // Set JSArray map to global.regexp_result_map().
4063 // Set empty properties FixedArray.
4064 // Set elements to point to FixedArray allocated right after the JSArray.
4065 // Interleave operations for better latency.
4066 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4067 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4068 __ mov(r4, Operand(Factory::empty_fixed_array()));
4069 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4070 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4071 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4072 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4073 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4074
4075 // Set input, index and length fields from arguments.
4076 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4077 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4078 __ add(sp, sp, Operand(kPointerSize));
4079 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4080 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4081
4082 // Fill out the elements FixedArray.
4083 // r0: JSArray, tagged.
4084 // r3: FixedArray, tagged.
4085 // r5: Number of elements in array, untagged.
4086
4087 // Set map.
4088 __ mov(r2, Operand(Factory::fixed_array_map()));
4089 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4090 // Set FixedArray length.
4091 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4092 // Fill contents of fixed-array with the-hole.
4093 __ mov(r2, Operand(Factory::the_hole_value()));
4094 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4095 // Fill fixed array elements with hole.
4096 // r0: JSArray, tagged.
4097 // r2: the hole.
4098 // r3: Start of elements in FixedArray.
4099 // r5: Number of elements to fill.
4100 Label loop;
4101 __ tst(r5, Operand(r5));
4102 __ bind(&loop);
4103 __ b(le, &done); // Jump if r1 is negative or zero.
4104 __ sub(r5, r5, Operand(1), SetCC);
4105 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4106 __ jmp(&loop);
4107
4108 __ bind(&slowcase);
4109 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4110
4111 __ bind(&done);
4112 }
4113 frame_->Forget(3);
4114 frame_->EmitPush(r0);
4115}
4116
4117
ager@chromium.org5c838252010-02-19 08:53:10 +00004118void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4119 ASSERT_EQ(args->length(), 1);
4120
4121 // Load the argument on the stack and jump to the runtime.
4122 Load(args->at(0));
4123
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004124 NumberToStringStub stub;
4125 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004126 frame_->EmitPush(r0);
4127}
4128
4129
ager@chromium.org357bf652010-04-12 11:30:10 +00004130void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4131 Comment cmnt(masm_, "[ GenerateCallFunction");
4132
4133 ASSERT(args->length() >= 2);
4134
4135 int n_args = args->length() - 2; // for receiver and function.
4136 Load(args->at(0)); // receiver
4137 for (int i = 0; i < n_args; i++) {
4138 Load(args->at(i + 1));
4139 }
4140 Load(args->at(n_args + 1)); // function
4141 frame_->CallJSFunction(n_args);
4142 frame_->EmitPush(r0);
4143}
4144
4145
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004146void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4147 ASSERT_EQ(args->length(), 1);
4148 // Load the argument on the stack and jump to the runtime.
4149 Load(args->at(0));
4150 frame_->CallRuntime(Runtime::kMath_sin, 1);
4151 frame_->EmitPush(r0);
4152}
4153
4154
4155void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4156 ASSERT_EQ(args->length(), 1);
4157 // Load the argument on the stack and jump to the runtime.
4158 Load(args->at(0));
4159 frame_->CallRuntime(Runtime::kMath_cos, 1);
4160 frame_->EmitPush(r0);
4161}
4162
4163
ager@chromium.org7c537e22008-10-16 08:43:32 +00004164void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004165 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004166 ASSERT(args->length() == 2);
4167
4168 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004169 LoadAndSpill(args->at(0));
4170 LoadAndSpill(args->at(1));
4171 frame_->EmitPop(r0);
4172 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004173 __ cmp(r0, Operand(r1));
4174 cc_reg_ = eq;
4175}
4176
4177
ager@chromium.org7c537e22008-10-16 08:43:32 +00004178void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004179#ifdef DEBUG
4180 int original_height = frame_->height();
4181#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004182 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004183 if (CheckForInlineRuntimeCall(node)) {
4184 ASSERT((has_cc() && frame_->height() == original_height) ||
4185 (!has_cc() && frame_->height() == original_height + 1));
4186 return;
4187 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188
4189 ZoneList<Expression*>* args = node->arguments();
4190 Comment cmnt(masm_, "[ CallRuntime");
4191 Runtime::Function* function = node->function();
4192
ager@chromium.org41826e72009-03-30 13:30:57 +00004193 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004194 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004195 // Push the builtins object found in the current global object.
4196 __ ldr(r1, GlobalObject());
4197 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004198 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004199 }
mads.s.ager31e71382008-08-13 09:32:07 +00004200
ager@chromium.org41826e72009-03-30 13:30:57 +00004201 // Push the arguments ("left-to-right").
4202 int arg_count = args->length();
4203 for (int i = 0; i < arg_count; i++) {
4204 LoadAndSpill(args->at(i));
4205 }
mads.s.ager31e71382008-08-13 09:32:07 +00004206
ager@chromium.org41826e72009-03-30 13:30:57 +00004207 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004208 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004209 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004210 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4211 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004212 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004213 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004214 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004215 } else {
4216 // Call the C runtime function.
4217 frame_->CallRuntime(function, arg_count);
4218 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004219 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004220 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004221}
4222
4223
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004225#ifdef DEBUG
4226 int original_height = frame_->height();
4227#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004228 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004229 Comment cmnt(masm_, "[ UnaryOperation");
4230
4231 Token::Value op = node->op();
4232
4233 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004235 false_target(),
4236 true_target(),
4237 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004238 // LoadCondition may (and usually does) leave a test and branch to
4239 // be emitted by the caller. In that case, negate the condition.
4240 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004241
4242 } else if (op == Token::DELETE) {
4243 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004244 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004245 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004246 LoadAndSpill(property->obj());
4247 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004248 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249
mads.s.ager31e71382008-08-13 09:32:07 +00004250 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 Slot* slot = variable->slot();
4252 if (variable->is_global()) {
4253 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004254 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004255 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004256 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004257
4258 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4259 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004260 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004261 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004262 frame_->EmitPush(r0);
4263 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004264 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004265 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004266 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004267 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004268 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004269
mads.s.ager31e71382008-08-13 09:32:07 +00004270 } else {
4271 // Default: Result of deleting non-global, not dynamically
4272 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004273 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004274 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004275
4276 } else {
4277 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004278 LoadAndSpill(node->expression()); // may have side-effects
4279 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004280 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004282 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004283
4284 } else if (op == Token::TYPEOF) {
4285 // Special case for loading the typeof expression; see comment on
4286 // LoadTypeofExpression().
4287 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004288 frame_->CallRuntime(Runtime::kTypeof, 1);
4289 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290
4291 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004292 bool overwrite =
4293 (node->expression()->AsBinaryOperation() != NULL &&
4294 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295 LoadAndSpill(node->expression());
4296 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004297 switch (op) {
4298 case Token::NOT:
4299 case Token::DELETE:
4300 case Token::TYPEOF:
4301 UNREACHABLE(); // handled above
4302 break;
4303
4304 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004305 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004306 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004307 break;
4308 }
4309
4310 case Token::BIT_NOT: {
4311 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004312 JumpTarget smi_label;
4313 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004314 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004315 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004317 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4318 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004319 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004320
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004321 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004322 __ mvn(r0, Operand(r0));
4323 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004324 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004325 break;
4326 }
4327
4328 case Token::VOID:
4329 // since the stack top is cached in r0, popping and then
4330 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004331 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004332 break;
4333
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004334 case Token::ADD: {
4335 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004336 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004337 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004338 continue_label.Branch(eq);
4339 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004340 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004341 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004342 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344 default:
4345 UNREACHABLE();
4346 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004347 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004348 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004349 ASSERT(!has_valid_frame() ||
4350 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004351 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004352}
4353
4354
ager@chromium.org7c537e22008-10-16 08:43:32 +00004355void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004356#ifdef DEBUG
4357 int original_height = frame_->height();
4358#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004359 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004360 Comment cmnt(masm_, "[ CountOperation");
4361
4362 bool is_postfix = node->is_postfix();
4363 bool is_increment = node->op() == Token::INC;
4364
4365 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4366 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4367
4368 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004369 if (is_postfix) {
4370 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004371 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004372 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004373
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004374 // A constant reference is not saved to, so a constant reference is not a
4375 // compound assignment reference.
4376 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004377 if (target.is_illegal()) {
4378 // Spoof the virtual frame to have the expected height (one higher
4379 // than on entry).
4380 if (!is_postfix) {
4381 __ mov(r0, Operand(Smi::FromInt(0)));
4382 frame_->EmitPush(r0);
4383 }
4384 ASSERT(frame_->height() == original_height + 1);
4385 return;
4386 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004387 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004388 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004389
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004390 JumpTarget slow;
4391 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004392
4393 // Load the value (1) into register r1.
4394 __ mov(r1, Operand(Smi::FromInt(1)));
4395
4396 // Check for smi operand.
4397 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004398 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004399
4400 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004401 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004402 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004403 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004404
4405 // Perform optimistic increment/decrement.
4406 if (is_increment) {
4407 __ add(r0, r0, Operand(r1), SetCC);
4408 } else {
4409 __ sub(r0, r0, Operand(r1), SetCC);
4410 }
4411
4412 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004413 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004414
4415 // Revert optimistic increment/decrement.
4416 if (is_increment) {
4417 __ sub(r0, r0, Operand(r1));
4418 } else {
4419 __ add(r0, r0, Operand(r1));
4420 }
4421
4422 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004423 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004424 {
4425 // Convert the operand to a number.
4426 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004427 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004428 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004429 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004430 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004431 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004432 }
4433
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004434 // Compute the new value.
4435 __ mov(r1, Operand(Smi::FromInt(1)));
4436 frame_->EmitPush(r0);
4437 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004438 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004439 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004440 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004441 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004442 }
4443
4444 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004445 exit.Bind();
4446 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004447 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004448 }
4449
4450 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004451 if (is_postfix) frame_->EmitPop(r0);
4452 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004453}
4454
4455
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004456void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004457 // According to ECMA-262 section 11.11, page 58, the binary logical
4458 // operators must yield the result of one of the two expressions
4459 // before any ToBoolean() conversions. This means that the value
4460 // produced by a && or || operator is not necessarily a boolean.
4461
4462 // NOTE: If the left hand side produces a materialized value (not in
4463 // the CC register), we force the right hand side to do the
4464 // same. This is necessary because we may have to branch to the exit
4465 // after evaluating the left hand side (due to the shortcut
4466 // semantics), but the compiler must (statically) know if the result
4467 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004468 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004469 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004470 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004471 &is_true,
4472 false_target(),
4473 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004474 if (has_valid_frame() && !has_cc()) {
4475 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004476 JumpTarget pop_and_continue;
4477 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004478
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004479 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004480 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004481 // Avoid popping the result if it converts to 'false' using the
4482 // standard ToBoolean() conversion as described in ECMA-262,
4483 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004484 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004485 Branch(false, &exit);
4486
4487 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004488 pop_and_continue.Bind();
4489 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004490
4491 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004492 is_true.Bind();
4493 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004494
4495 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004496 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004497 } else if (has_cc() || is_true.is_linked()) {
4498 // The left-hand side is either (a) partially compiled to
4499 // control flow with a final branch left to emit or (b) fully
4500 // compiled to control flow and possibly true.
4501 if (has_cc()) {
4502 Branch(false, false_target());
4503 }
4504 is_true.Bind();
4505 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004506 true_target(),
4507 false_target(),
4508 false);
4509 } else {
4510 // Nothing to do.
4511 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004512 }
4513
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004514 } else {
4515 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004516 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004517 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004518 true_target(),
4519 &is_false,
4520 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004521 if (has_valid_frame() && !has_cc()) {
4522 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004523 JumpTarget pop_and_continue;
4524 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004525
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004526 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004527 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004528 // Avoid popping the result if it converts to 'true' using the
4529 // standard ToBoolean() conversion as described in ECMA-262,
4530 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004531 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004532 Branch(true, &exit);
4533
4534 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004535 pop_and_continue.Bind();
4536 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004537
4538 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004539 is_false.Bind();
4540 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004541
4542 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004543 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004544 } else if (has_cc() || is_false.is_linked()) {
4545 // The left-hand side is either (a) partially compiled to
4546 // control flow with a final branch left to emit or (b) fully
4547 // compiled to control flow and possibly false.
4548 if (has_cc()) {
4549 Branch(true, true_target());
4550 }
4551 is_false.Bind();
4552 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004553 true_target(),
4554 false_target(),
4555 false);
4556 } else {
4557 // Nothing to do.
4558 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004559 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004560 }
4561}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004562
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004563
4564void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4565#ifdef DEBUG
4566 int original_height = frame_->height();
4567#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004568 Comment cmnt(masm_, "[ BinaryOperation");
4569
4570 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004571 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004572 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004573 } else {
4574 // Optimize for the case where (at least) one of the expressions
4575 // is a literal small integer.
4576 Literal* lliteral = node->left()->AsLiteral();
4577 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004578 // NOTE: The code below assumes that the slow cases (calls to runtime)
4579 // never return a constant/immutable object.
4580 bool overwrite_left =
4581 (node->left()->AsBinaryOperation() != NULL &&
4582 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4583 bool overwrite_right =
4584 (node->right()->AsBinaryOperation() != NULL &&
4585 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004586
4587 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004588 VirtualFrame::RegisterAllocationScope scope(this);
4589 Load(node->left());
4590 VirtualFrameSmiOperation(
4591 node->op(),
4592 rliteral->handle(),
4593 false,
4594 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004595 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004596 VirtualFrame::RegisterAllocationScope scope(this);
4597 Load(node->right());
4598 VirtualFrameSmiOperation(node->op(),
4599 lliteral->handle(),
4600 true,
4601 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004602 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00004603 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004604 OverwriteMode overwrite_mode = NO_OVERWRITE;
4605 if (overwrite_left) {
4606 overwrite_mode = OVERWRITE_LEFT;
4607 } else if (overwrite_right) {
4608 overwrite_mode = OVERWRITE_RIGHT;
4609 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004610 Load(node->left());
4611 Load(node->right());
4612 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004613 }
4614 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004615 ASSERT(!has_valid_frame() ||
4616 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004617 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004618}
4619
4620
ager@chromium.org7c537e22008-10-16 08:43:32 +00004621void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004622#ifdef DEBUG
4623 int original_height = frame_->height();
4624#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004625 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004626 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004627 frame_->EmitPush(r0);
4628 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004629}
4630
4631
ager@chromium.org7c537e22008-10-16 08:43:32 +00004632void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004633#ifdef DEBUG
4634 int original_height = frame_->height();
4635#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004636 Comment cmnt(masm_, "[ CompareOperation");
4637
ager@chromium.org357bf652010-04-12 11:30:10 +00004638 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
4639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004640 // Get the expressions from the node.
4641 Expression* left = node->left();
4642 Expression* right = node->right();
4643 Token::Value op = node->op();
4644
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004645 // To make null checks efficient, we check if either left or right is the
4646 // literal 'null'. If so, we optimize the code by inlining a null check
4647 // instead of calling the (very) general runtime routine for checking
4648 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004649 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004650 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004651 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004652 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004653 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4654 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004655 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004656 Load(left_is_null ? right : left);
4657 Register tos = frame_->PopToRegister();
4658 // JumpTargets can't cope with register allocation yet.
4659 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004660 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004661 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004662
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004663 // The 'null' value is only equal to 'undefined' if using non-strict
4664 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004665 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004666 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004667
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004668 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004669 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004670 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004671
ager@chromium.org357bf652010-04-12 11:30:10 +00004672 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004673 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004674
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004675 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004676 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4677 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
4678 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4679 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680 }
4681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004682 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004683 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004684 return;
4685 }
4686 }
4687
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004688 // To make typeof testing for natives implemented in JavaScript really
4689 // efficient, we generate special code for expressions of the form:
4690 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004691 UnaryOperation* operation = left->AsUnaryOperation();
4692 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4693 (operation != NULL && operation->op() == Token::TYPEOF) &&
4694 (right->AsLiteral() != NULL &&
4695 right->AsLiteral()->handle()->IsString())) {
4696 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4697
ager@chromium.org357bf652010-04-12 11:30:10 +00004698 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004699 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00004700 Register tos = frame_->PopToRegister();
4701
4702 // JumpTargets can't cope with register allocation yet.
4703 frame_->SpillAll();
4704
4705 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004706
4707 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004708 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004709 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004710 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004711 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004712 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004713 cc_reg_ = eq;
4714
4715 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004716 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004717 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004718
ager@chromium.org357bf652010-04-12 11:30:10 +00004719 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004720
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004721 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004722 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4723 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4724 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004725 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004726
ager@chromium.org357bf652010-04-12 11:30:10 +00004727 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
4728 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004729 cc_reg_ = lt;
4730
4731 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004732 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004733 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004734 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004735 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004736 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004737 cc_reg_ = eq;
4738
4739 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004740 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004741 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004742 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004743
ager@chromium.org357bf652010-04-12 11:30:10 +00004744 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004745 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004746
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004747 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004748 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4749 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4750 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4751 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004752
4753 cc_reg_ = eq;
4754
4755 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004756 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004757 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004758 Register map_reg = scratch;
4759 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004760 true_target()->Branch(eq);
4761 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00004762 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004763 cc_reg_ = eq;
4764
4765 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004766 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004767 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004768
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004769 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004770 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004771 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004772
ager@chromium.org357bf652010-04-12 11:30:10 +00004773 Register map_reg = scratch;
4774 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004775 false_target()->Branch(eq);
4776
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004777 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004778 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4779 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4780 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004781 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004782
ager@chromium.org357bf652010-04-12 11:30:10 +00004783 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4784 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004785 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00004786 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004787 cc_reg_ = le;
4788
4789 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004790 // Uncommon case: typeof testing against a string literal that is
4791 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004792 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004793 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004794 ASSERT(!has_valid_frame() ||
4795 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004796 return;
4797 }
4798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004799 switch (op) {
4800 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004801 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004802 break;
4803
4804 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004805 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004806 break;
4807
4808 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004809 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004810 break;
4811
4812 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004813 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004814 break;
4815
4816 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004817 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004818 break;
4819
4820 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004821 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004822 break;
4823
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004824 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004825 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004826 LoadAndSpill(left);
4827 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004828 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004829 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004830 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004831 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004832
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004833 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004834 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004835 LoadAndSpill(left);
4836 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004837 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004838 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004839 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004840 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004841 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004842 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004843 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004844
4845 default:
4846 UNREACHABLE();
4847 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004848 ASSERT((has_cc() && frame_->height() == original_height) ||
4849 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004850}
4851
4852
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004853void CodeGenerator::EmitKeyedLoad(bool is_global) {
4854 Comment cmnt(masm_, "[ Load from keyed Property");
4855 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4856 RelocInfo::Mode rmode = is_global
4857 ? RelocInfo::CODE_TARGET_CONTEXT
4858 : RelocInfo::CODE_TARGET;
4859 frame_->CallCodeObject(ic, rmode, 0);
4860}
4861
4862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004863#ifdef DEBUG
4864bool CodeGenerator::HasValidEntryRegisters() { return true; }
4865#endif
4866
4867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004868#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004869#define __ ACCESS_MASM(masm)
4870
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004871
ager@chromium.org7c537e22008-10-16 08:43:32 +00004872Handle<String> Reference::GetName() {
4873 ASSERT(type_ == NAMED);
4874 Property* property = expression_->AsProperty();
4875 if (property == NULL) {
4876 // Global variable reference treated as a named property reference.
4877 VariableProxy* proxy = expression_->AsVariableProxy();
4878 ASSERT(proxy->AsVariable() != NULL);
4879 ASSERT(proxy->AsVariable()->is_global());
4880 return proxy->name();
4881 } else {
4882 Literal* raw_name = property->key()->AsLiteral();
4883 ASSERT(raw_name != NULL);
4884 return Handle<String>(String::cast(*raw_name->handle()));
4885 }
4886}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004887
ager@chromium.org7c537e22008-10-16 08:43:32 +00004888
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004889void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004890 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004891 ASSERT(!is_illegal());
4892 ASSERT(!cgen_->has_cc());
4893 MacroAssembler* masm = cgen_->masm();
4894 Property* property = expression_->AsProperty();
4895 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004896 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004897 }
4898
4899 switch (type_) {
4900 case SLOT: {
4901 Comment cmnt(masm, "[ Load from Slot");
4902 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4903 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004904 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004905 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004906 }
4907
ager@chromium.org7c537e22008-10-16 08:43:32 +00004908 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004909 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004910 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004911 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004912 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004913 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4914 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004915 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004916 ASSERT(var == NULL || var->is_global());
4917 RelocInfo::Mode rmode = (var == NULL)
4918 ? RelocInfo::CODE_TARGET
4919 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004920 frame->CallCodeObject(ic, rmode, 0);
4921 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004922 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004923 }
4924
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004925 case KEYED: {
4926 // TODO(181): Implement inlined version of array indexing once
4927 // loop nesting is properly tracked on ARM.
4928 ASSERT(property != NULL);
4929 Variable* var = expression_->AsVariableProxy()->AsVariable();
4930 ASSERT(var == NULL || var->is_global());
4931 cgen_->EmitKeyedLoad(var != NULL);
4932 cgen_->frame()->EmitPush(r0);
4933 break;
4934 }
4935
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004936 default:
4937 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004938 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004939
4940 if (!persist_after_get_) {
4941 cgen_->UnloadReference(this);
4942 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004943}
4944
4945
ager@chromium.org7c537e22008-10-16 08:43:32 +00004946void Reference::SetValue(InitState init_state) {
4947 ASSERT(!is_illegal());
4948 ASSERT(!cgen_->has_cc());
4949 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004950 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004951 Property* property = expression_->AsProperty();
4952 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004953 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004954 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004955
ager@chromium.org7c537e22008-10-16 08:43:32 +00004956 switch (type_) {
4957 case SLOT: {
4958 Comment cmnt(masm, "[ Store to Slot");
4959 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004960 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004961 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004962 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004963 }
4964
ager@chromium.org7c537e22008-10-16 08:43:32 +00004965 case NAMED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004966 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004967 Comment cmnt(masm, "[ Store to named Property");
4968 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004969 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004970 Handle<String> name(GetName());
4971
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004972 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004973 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004974 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004975 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004976 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004977 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004978 break;
4979 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004980
ager@chromium.org7c537e22008-10-16 08:43:32 +00004981 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004982 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004983 Comment cmnt(masm, "[ Store to keyed Property");
4984 Property* property = expression_->AsProperty();
4985 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004986 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004987
4988 // Call IC code.
4989 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004990 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004991 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004992 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004993 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004994 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004995 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004996
4997 default:
4998 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004999 }
5000}
5001
5002
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005003void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005004 // Create a new closure from the given function info in new
5005 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005006 Label gc;
5007
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005008 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005009 __ pop(r3);
5010
5011 // Attempt to allocate new JSFunction in new space.
5012 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
5013 r0,
5014 r1,
5015 r2,
5016 &gc,
5017 TAG_OBJECT);
5018
5019 // Compute the function map in the current global context and set that
5020 // as the map of the allocated object.
5021 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5022 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5023 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5024 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5025
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005026 // Initialize the rest of the function. We don't have to update the
5027 // write barrier because the allocated object is in new space.
5028 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5029 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5030 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5031 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5032 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5033 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5034 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5035 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005036
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005037 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005038 __ Ret();
5039
5040 // Create a new closure through the slower runtime call.
5041 __ bind(&gc);
5042 __ push(cp);
5043 __ push(r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005044 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005045}
5046
5047
5048void FastNewContextStub::Generate(MacroAssembler* masm) {
5049 // Try to allocate the context in new space.
5050 Label gc;
5051 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5052
5053 // Attempt to allocate the context in new space.
5054 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
5055 r0,
5056 r1,
5057 r2,
5058 &gc,
5059 TAG_OBJECT);
5060
5061 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005062 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005063
5064 // Setup the object header.
5065 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5066 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5067 __ mov(r2, Operand(length));
5068 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5069
5070 // Setup the fixed slots.
5071 __ mov(r1, Operand(Smi::FromInt(0)));
5072 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5073 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5074 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5075 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5076
5077 // Copy the global object from the surrounding context.
5078 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5079 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5080
5081 // Initialize the rest of the slots to undefined.
5082 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5083 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5084 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5085 }
5086
5087 // Remove the on-stack argument and return.
5088 __ mov(cp, r0);
5089 __ pop();
5090 __ Ret();
5091
5092 // Need to collect. Call into runtime system.
5093 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005094 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005095}
5096
5097
ager@chromium.org5c838252010-02-19 08:53:10 +00005098void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5099 // Stack layout on entry:
5100 //
5101 // [sp]: constant elements.
5102 // [sp + kPointerSize]: literal index.
5103 // [sp + (2 * kPointerSize)]: literals array.
5104
5105 // All sizes here are multiples of kPointerSize.
5106 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5107 int size = JSArray::kSize + elements_size;
5108
5109 // Load boilerplate object into r3 and check if we need to create a
5110 // boilerplate.
5111 Label slow_case;
5112 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5113 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5114 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5115 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5116 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5117 __ cmp(r3, ip);
5118 __ b(eq, &slow_case);
5119
5120 // Allocate both the JS array and the elements array in one big
5121 // allocation. This avoids multiple limit checks.
5122 __ AllocateInNewSpace(size / kPointerSize,
5123 r0,
5124 r1,
5125 r2,
5126 &slow_case,
5127 TAG_OBJECT);
5128
5129 // Copy the JS array part.
5130 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5131 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5132 __ ldr(r1, FieldMemOperand(r3, i));
5133 __ str(r1, FieldMemOperand(r0, i));
5134 }
5135 }
5136
5137 if (length_ > 0) {
5138 // Get hold of the elements array of the boilerplate and setup the
5139 // elements pointer in the resulting object.
5140 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5141 __ add(r2, r0, Operand(JSArray::kSize));
5142 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
5143
5144 // Copy the elements array.
5145 for (int i = 0; i < elements_size; i += kPointerSize) {
5146 __ ldr(r1, FieldMemOperand(r3, i));
5147 __ str(r1, FieldMemOperand(r2, i));
5148 }
5149 }
5150
5151 // Return and remove the on-stack parameters.
5152 __ add(sp, sp, Operand(3 * kPointerSize));
5153 __ Ret();
5154
5155 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005156 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005157}
5158
5159
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005160// Takes a Smi and converts to an IEEE 64 bit floating point value in two
5161// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
5162// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
5163// scratch register. Destroys the source register. No GC occurs during this
5164// stub so you don't have to set up the frame.
5165class ConvertToDoubleStub : public CodeStub {
5166 public:
5167 ConvertToDoubleStub(Register result_reg_1,
5168 Register result_reg_2,
5169 Register source_reg,
5170 Register scratch_reg)
5171 : result1_(result_reg_1),
5172 result2_(result_reg_2),
5173 source_(source_reg),
5174 zeros_(scratch_reg) { }
5175
5176 private:
5177 Register result1_;
5178 Register result2_;
5179 Register source_;
5180 Register zeros_;
5181
5182 // Minor key encoding in 16 bits.
5183 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
5184 class OpBits: public BitField<Token::Value, 2, 14> {};
5185
5186 Major MajorKey() { return ConvertToDouble; }
5187 int MinorKey() {
5188 // Encode the parameters in a unique 16 bit value.
5189 return result1_.code() +
5190 (result2_.code() << 4) +
5191 (source_.code() << 8) +
5192 (zeros_.code() << 12);
5193 }
5194
5195 void Generate(MacroAssembler* masm);
5196
5197 const char* GetName() { return "ConvertToDoubleStub"; }
5198
5199#ifdef DEBUG
5200 void Print() { PrintF("ConvertToDoubleStub\n"); }
5201#endif
5202};
5203
5204
5205void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
5206#ifndef BIG_ENDIAN_FLOATING_POINT
5207 Register exponent = result1_;
5208 Register mantissa = result2_;
5209#else
5210 Register exponent = result2_;
5211 Register mantissa = result1_;
5212#endif
5213 Label not_special;
5214 // Convert from Smi to integer.
5215 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
5216 // Move sign bit from source to destination. This works because the sign bit
5217 // in the exponent word of the double has the same position and polarity as
5218 // the 2's complement sign bit in a Smi.
5219 ASSERT(HeapNumber::kSignMask == 0x80000000u);
5220 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
5221 // Subtract from 0 if source was negative.
5222 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005223
5224 // We have -1, 0 or 1, which we treat specially. Register source_ contains
5225 // absolute value: it is either equal to 1 (special case of -1 and 1),
5226 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005227 __ cmp(source_, Operand(1));
5228 __ b(gt, &not_special);
5229
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005230 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
5231 static const uint32_t exponent_word_for_1 =
5232 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005233 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005234 // 1, 0 and -1 all have 0 for the second word.
5235 __ mov(mantissa, Operand(0));
5236 __ Ret();
5237
5238 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005239 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005240 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005241 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005242 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005243 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005244 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
5245 __ orr(exponent,
5246 exponent,
5247 Operand(mantissa, LSL, HeapNumber::kExponentShift));
5248 // Shift up the source chopping the top bit off.
5249 __ add(zeros_, zeros_, Operand(1));
5250 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
5251 __ mov(source_, Operand(source_, LSL, zeros_));
5252 // Compute lower part of fraction (last 12 bits).
5253 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
5254 // And the top (top 20 bits).
5255 __ orr(exponent,
5256 exponent,
5257 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
5258 __ Ret();
5259}
5260
5261
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005262// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005263void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005264 Label max_negative_int;
5265 // the_int_ has the answer which is a signed int32 but not a Smi.
5266 // We test for the special value that has a different exponent. This test
5267 // has the neat side effect of setting the flags according to the sign.
5268 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005269 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005270 __ b(eq, &max_negative_int);
5271 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
5272 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
5273 uint32_t non_smi_exponent =
5274 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5275 __ mov(scratch_, Operand(non_smi_exponent));
5276 // Set the sign bit in scratch_ if the value was negative.
5277 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
5278 // Subtract from 0 if the value was negative.
5279 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
5280 // We should be masking the implict first digit of the mantissa away here,
5281 // but it just ends up combining harmlessly with the last digit of the
5282 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
5283 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
5284 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
5285 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5286 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
5287 __ str(scratch_, FieldMemOperand(the_heap_number_,
5288 HeapNumber::kExponentOffset));
5289 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
5290 __ str(scratch_, FieldMemOperand(the_heap_number_,
5291 HeapNumber::kMantissaOffset));
5292 __ Ret();
5293
5294 __ bind(&max_negative_int);
5295 // The max negative int32 is stored as a positive number in the mantissa of
5296 // a double because it uses a sign bit instead of using two's complement.
5297 // The actual mantissa bits stored are all 0 because the implicit most
5298 // significant 1 bit is not stored.
5299 non_smi_exponent += 1 << HeapNumber::kExponentShift;
5300 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
5301 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
5302 __ mov(ip, Operand(0));
5303 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
5304 __ Ret();
5305}
5306
5307
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005308// Handle the case where the lhs and rhs are the same object.
5309// Equality is almost reflexive (everything but NaN), so this is a test
5310// for "identity and not NaN".
5311static void EmitIdenticalObjectComparison(MacroAssembler* masm,
5312 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005313 Condition cc,
5314 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005315 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005316 Label heap_number, return_equal;
5317 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005318 __ cmp(r0, Operand(r1));
5319 __ b(ne, &not_identical);
5320
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005321 // The two objects are identical. If we know that one of them isn't NaN then
5322 // we now know they test equal.
5323 if (cc != eq || !never_nan_nan) {
5324 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005325
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005326 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
5327 // so we do the second best thing - test it ourselves.
5328 // They are both equal and they are not both Smis so both of them are not
5329 // Smis. If it's not a heap number, then return equal.
5330 if (cc == lt || cc == gt) {
5331 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005332 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005333 } else {
5334 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5335 __ b(eq, &heap_number);
5336 // Comparing JS objects with <=, >= is complicated.
5337 if (cc != eq) {
5338 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
5339 __ b(ge, slow);
5340 // Normally here we fall through to return_equal, but undefined is
5341 // special: (undefined == undefined) == true, but
5342 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
5343 if (cc == le || cc == ge) {
5344 __ cmp(r4, Operand(ODDBALL_TYPE));
5345 __ b(ne, &return_equal);
5346 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
5347 __ cmp(r0, Operand(r2));
5348 __ b(ne, &return_equal);
5349 if (cc == le) {
5350 // undefined <= undefined should fail.
5351 __ mov(r0, Operand(GREATER));
5352 } else {
5353 // undefined >= undefined should fail.
5354 __ mov(r0, Operand(LESS));
5355 }
5356 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005357 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005358 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005359 }
5360 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005361
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005362 __ bind(&return_equal);
5363 if (cc == lt) {
5364 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
5365 } else if (cc == gt) {
5366 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
5367 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005368 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005369 }
5370 __ mov(pc, Operand(lr)); // Return.
5371
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005372 if (cc != eq || !never_nan_nan) {
5373 // For less and greater we don't have to check for NaN since the result of
5374 // x < x is false regardless. For the others here is some code to check
5375 // for NaN.
5376 if (cc != lt && cc != gt) {
5377 __ bind(&heap_number);
5378 // It is a heap number, so return non-equal if it's NaN and equal if it's
5379 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005380
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005381 // The representation of NaN values has all exponent bits (52..62) set,
5382 // and not all mantissa bits (0..51) clear.
5383 // Read top bits of double representation (second word of value).
5384 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5385 // Test that exponent bits are all set.
5386 __ and_(r3, r2, Operand(exp_mask_reg));
5387 __ cmp(r3, Operand(exp_mask_reg));
5388 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005389
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005390 // Shift out flag and all exponent bits, retaining only mantissa.
5391 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
5392 // Or with all low-bits of mantissa.
5393 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5394 __ orr(r0, r3, Operand(r2), SetCC);
5395 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005396 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
5397 // not (it's a NaN). For <= and >= we need to load r0 with the failing
5398 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005399 if (cc != eq) {
5400 // All-zero means Infinity means equal.
5401 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
5402 if (cc == le) {
5403 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
5404 } else {
5405 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
5406 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005407 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005408 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005409 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005410 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005411 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005412
5413 __ bind(&not_identical);
5414}
5415
5416
5417// See comment at call site.
5418static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005419 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005420 Label* slow,
5421 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005422 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005423 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005424 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005425
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005426 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005427 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5428 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005429 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005430 // succeed. Return non-equal (r0 is already not zero)
5431 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5432 } else {
5433 // Smi compared non-strictly with a non-Smi non-heap-number. Call
5434 // the runtime.
5435 __ b(ne, slow);
5436 }
5437
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005438 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005439 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005440 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005441 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005442 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5443 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005444 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005445 // Load the double from rhs, tagged HeapNumber r0, to d6.
5446 __ sub(r7, r0, Operand(kHeapObjectTag));
5447 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005448 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005449 __ push(lr);
5450 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005451 __ mov(r7, Operand(r1));
5452 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5453 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005454 // Load rhs to a double in r0, r1.
5455 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5456 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5457 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005458 }
5459
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005460 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005461 // since it's a smi.
5462 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005464 __ bind(&rhs_is_smi);
5465 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005466 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5467 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005468 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005469 // succeed. Return non-equal.
5470 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
5471 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5472 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005473 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005474 // the runtime.
5475 __ b(ne, slow);
5476 }
5477
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005478 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005479 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005480 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005481 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005482 // Load the double from lhs, tagged HeapNumber r1, to d7.
5483 __ sub(r7, r1, Operand(kHeapObjectTag));
5484 __ vldr(d7, r7, HeapNumber::kValueOffset);
5485 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5486 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005487 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005488 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005489 __ push(lr);
5490 // Load lhs to a double in r2, r3.
5491 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5492 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5493 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005494 __ mov(r7, Operand(r0));
5495 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5496 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005497 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005498 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005499 // Fall through to both_loaded_as_doubles.
5500}
5501
5502
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005503void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005504 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005505 Register rhs_exponent = exp_first ? r0 : r1;
5506 Register lhs_exponent = exp_first ? r2 : r3;
5507 Register rhs_mantissa = exp_first ? r1 : r0;
5508 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005509 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005510 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005511
5512 Register exp_mask_reg = r5;
5513
5514 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005515 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5516 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005517 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005518 __ mov(r4,
5519 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5520 SetCC);
5521 __ b(ne, &one_is_nan);
5522 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005523 __ b(ne, &one_is_nan);
5524
5525 __ bind(lhs_not_nan);
5526 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5527 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5528 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5529 __ cmp(r4, Operand(exp_mask_reg));
5530 __ b(ne, &neither_is_nan);
5531 __ mov(r4,
5532 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5533 SetCC);
5534 __ b(ne, &one_is_nan);
5535 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005536 __ b(eq, &neither_is_nan);
5537
5538 __ bind(&one_is_nan);
5539 // NaN comparisons always fail.
5540 // Load whatever we need in r0 to make the comparison fail.
5541 if (cc == lt || cc == le) {
5542 __ mov(r0, Operand(GREATER));
5543 } else {
5544 __ mov(r0, Operand(LESS));
5545 }
5546 __ mov(pc, Operand(lr)); // Return.
5547
5548 __ bind(&neither_is_nan);
5549}
5550
5551
5552// See comment at call site.
5553static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5554 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005555 Register rhs_exponent = exp_first ? r0 : r1;
5556 Register lhs_exponent = exp_first ? r2 : r3;
5557 Register rhs_mantissa = exp_first ? r1 : r0;
5558 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005559
5560 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5561 if (cc == eq) {
5562 // Doubles are not equal unless they have the same bit pattern.
5563 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005564 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5565 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005566 // Return non-zero if the numbers are unequal.
5567 __ mov(pc, Operand(lr), LeaveCC, ne);
5568
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005569 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005570 // If exponents are equal then return 0.
5571 __ mov(pc, Operand(lr), LeaveCC, eq);
5572
5573 // Exponents are unequal. The only way we can return that the numbers
5574 // are equal is if one is -0 and the other is 0. We already dealt
5575 // with the case where both are -0 or both are 0.
5576 // We start by seeing if the mantissas (that are equal) or the bottom
5577 // 31 bits of the rhs exponent are non-zero. If so we return not
5578 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005579 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005580 __ mov(r0, Operand(r4), LeaveCC, ne);
5581 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5582 // Now they are equal if and only if the lhs exponent is zero in its
5583 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005584 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005585 __ mov(pc, Operand(lr));
5586 } else {
5587 // Call a native function to do a comparison between two non-NaNs.
5588 // Call C routine that may not cause GC or other trouble.
5589 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5590 __ Jump(r5); // Tail call.
5591 }
5592}
5593
5594
5595// See comment at call site.
5596static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5597 // If either operand is a JSObject or an oddball value, then they are
5598 // not equal since their pointers are different.
5599 // There is no test for undetectability in strict equality.
5600 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5601 Label first_non_object;
5602 // Get the type of the first operand into r2 and compare it with
5603 // FIRST_JS_OBJECT_TYPE.
5604 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5605 __ b(lt, &first_non_object);
5606
5607 // Return non-zero (r0 is not zero)
5608 Label return_not_equal;
5609 __ bind(&return_not_equal);
5610 __ mov(pc, Operand(lr)); // Return.
5611
5612 __ bind(&first_non_object);
5613 // Check for oddballs: true, false, null, undefined.
5614 __ cmp(r2, Operand(ODDBALL_TYPE));
5615 __ b(eq, &return_not_equal);
5616
5617 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5618 __ b(ge, &return_not_equal);
5619
5620 // Check for oddballs: true, false, null, undefined.
5621 __ cmp(r3, Operand(ODDBALL_TYPE));
5622 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005623
5624 // Now that we have the types we might as well check for symbol-symbol.
5625 // Ensure that no non-strings have the symbol bit set.
5626 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5627 ASSERT(kSymbolTag != 0);
5628 __ and_(r2, r2, Operand(r3));
5629 __ tst(r2, Operand(kIsSymbolMask));
5630 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005631}
5632
5633
5634// See comment at call site.
5635static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5636 Label* both_loaded_as_doubles,
5637 Label* not_heap_numbers,
5638 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005639 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005640 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005641 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5642 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005643 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5644
5645 // Both are heap numbers. Load them up then jump to the code we have
5646 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005647 if (CpuFeatures::IsSupported(VFP3)) {
5648 CpuFeatures::Scope scope(VFP3);
5649 __ sub(r7, r0, Operand(kHeapObjectTag));
5650 __ vldr(d6, r7, HeapNumber::kValueOffset);
5651 __ sub(r7, r1, Operand(kHeapObjectTag));
5652 __ vldr(d7, r7, HeapNumber::kValueOffset);
5653 } else {
5654 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5655 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5656 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5657 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5658 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005659 __ jmp(both_loaded_as_doubles);
5660}
5661
5662
5663// Fast negative check for symbol-to-symbol equality.
5664static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5665 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005666 // Ensure that no non-strings have the symbol bit set.
5667 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5668 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005669 __ tst(r2, Operand(kIsSymbolMask));
5670 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005671 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5672 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005673 __ tst(r3, Operand(kIsSymbolMask));
5674 __ b(eq, slow);
5675
5676 // Both are symbols. We already checked they weren't the same pointer
5677 // so they are not equal.
5678 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5679 __ mov(pc, Operand(lr)); // Return.
5680}
5681
5682
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005683void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
5684 Register object,
5685 Register result,
5686 Register scratch1,
5687 Register scratch2,
5688 bool object_is_smi,
5689 Label* not_found) {
5690 // Currently only lookup for smis. Check for smi if object is not known to be
5691 // a smi.
5692 if (!object_is_smi) {
5693 ASSERT(kSmiTag == 0);
5694 __ tst(object, Operand(kSmiTagMask));
5695 __ b(ne, not_found);
5696 }
5697
5698 // Use of registers. Register result is used as a temporary.
5699 Register number_string_cache = result;
5700 Register mask = scratch1;
5701 Register scratch = scratch2;
5702
5703 // Load the number string cache.
5704 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
5705
5706 // Make the hash mask from the length of the number string cache. It
5707 // contains two elements (number and string) for each cache entry.
5708 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
5709 // Divide length by two (length is not a smi).
5710 __ mov(mask, Operand(mask, ASR, 1));
5711 __ sub(mask, mask, Operand(1)); // Make mask.
5712
5713 // Calculate the entry in the number string cache. The hash value in the
5714 // number string cache for smis is just the smi value.
5715 __ and_(scratch, mask, Operand(object, ASR, 1));
5716
5717 // Calculate address of entry in string cache: each entry consists
5718 // of two pointer sized fields.
5719 __ add(scratch,
5720 number_string_cache,
5721 Operand(scratch, LSL, kPointerSizeLog2 + 1));
5722
5723 // Check if the entry is the smi we are looking for.
5724 Register object1 = scratch1;
5725 __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize));
5726 __ cmp(object, object1);
5727 __ b(ne, not_found);
5728
5729 // Get the result from the cache.
5730 __ ldr(result,
5731 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
5732
5733 __ IncrementCounter(&Counters::number_to_string_native,
5734 1,
5735 scratch1,
5736 scratch2);
5737}
5738
5739
5740void NumberToStringStub::Generate(MacroAssembler* masm) {
5741 Label runtime;
5742
5743 __ ldr(r1, MemOperand(sp, 0));
5744
5745 // Generate code to lookup number in the number string cache.
5746 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime);
5747 __ add(sp, sp, Operand(1 * kPointerSize));
5748 __ Ret();
5749
5750 __ bind(&runtime);
5751 // Handle number to string in the runtime system if not found in the cache.
5752 __ TailCallRuntime(Runtime::kNumberToString, 1, 1);
5753}
5754
5755
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005756// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5757// On exit r0 is 0, positive or negative to indicate the result of
5758// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005759void CompareStub::Generate(MacroAssembler* masm) {
5760 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005761 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005762
5763 // NOTICE! This code is only reached after a smi-fast-case check, so
5764 // it is certain that at least one operand isn't a smi.
5765
5766 // Handle the case where the objects are identical. Either returns the answer
5767 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005768 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005769
5770 // If either is a Smi (we know that not both are), then they can only
5771 // be strictly equal if the other is a HeapNumber.
5772 ASSERT_EQ(0, kSmiTag);
5773 ASSERT_EQ(0, Smi::FromInt(0));
5774 __ and_(r2, r0, Operand(r1));
5775 __ tst(r2, Operand(kSmiTagMask));
5776 __ b(ne, &not_smis);
5777 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5778 // 1) Return the answer.
5779 // 2) Go to slow.
5780 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005781 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005782 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005783 // comparison. If VFP3 is supported the double values of the numbers have
5784 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5785 // into r0, r1, r2, and r3.
5786 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005787
5788 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005789 // The arguments have been converted to doubles and stored in d6 and d7, if
5790 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005791 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005792 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005793 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005794 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005795 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005796 __ vcmp(d7, d6);
5797 __ vmrs(pc); // Move vector status bits to normal status bits.
5798 Label nan;
5799 __ b(vs, &nan);
5800 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5801 __ mov(r0, Operand(LESS), LeaveCC, lt);
5802 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5803 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005804
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005805 __ bind(&nan);
5806 // If one of the sides was a NaN then the v flag is set. Load r0 with
5807 // whatever it takes to make the comparison fail, since comparisons with NaN
5808 // always fail.
5809 if (cc_ == lt || cc_ == le) {
5810 __ mov(r0, Operand(GREATER));
5811 } else {
5812 __ mov(r0, Operand(LESS));
5813 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005814 __ mov(pc, Operand(lr));
5815 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005816 // Checks for NaN in the doubles we have loaded. Can return the answer or
5817 // fall through if neither is a NaN. Also binds lhs_not_nan.
5818 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005819 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5820 // answer. Never falls through.
5821 EmitTwoNonNanDoubleComparison(masm, cc_);
5822 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005823
5824 __ bind(&not_smis);
5825 // At this point we know we are dealing with two different objects,
5826 // and neither of them is a Smi. The objects are in r0 and r1.
5827 if (strict_) {
5828 // This returns non-equal for some object types, or falls through if it
5829 // was not lucky.
5830 EmitStrictTwoHeapObjectCompare(masm);
5831 }
5832
5833 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005834 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005835 // Check for heap-number-heap-number comparison. Can jump to slow case,
5836 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5837 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005838 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005839 EmitCheckForTwoHeapNumbers(masm,
5840 &both_loaded_as_doubles,
5841 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005842 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005843
5844 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005845 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5846 // symbols.
5847 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005848 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5849 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005850 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005851 }
5852
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005853 // Check for both being sequential ASCII strings, and inline if that is the
5854 // case.
5855 __ bind(&flat_string_check);
5856
5857 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5858
5859 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5860 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5861 r1,
5862 r0,
5863 r2,
5864 r3,
5865 r4,
5866 r5);
5867 // Never falls through to here.
5868
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005869 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005870
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005871 __ push(r1);
5872 __ push(r0);
5873 // Figure out which native to call and setup the arguments.
5874 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005875 if (cc_ == eq) {
5876 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5877 } else {
5878 native = Builtins::COMPARE;
5879 int ncr; // NaN compare result
5880 if (cc_ == lt || cc_ == le) {
5881 ncr = GREATER;
5882 } else {
5883 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5884 ncr = LESS;
5885 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005886 __ mov(r0, Operand(Smi::FromInt(ncr)));
5887 __ push(r0);
5888 }
5889
5890 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5891 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005892 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005893}
5894
5895
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005896// We fall into this code if the operands were Smis, but the result was
5897// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005898// the operands were not both Smi. The operands are in r0 and r1. In order
5899// to call the C-implemented binary fp operation routines we need to end up
5900// with the double precision floating point operands in r0 and r1 (for the
5901// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00005902void GenericBinaryOpStub::HandleBinaryOpSlowCases(
5903 MacroAssembler* masm,
5904 Label* not_smi,
5905 Register lhs,
5906 Register rhs,
5907 const Builtins::JavaScript& builtin) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005908 Label slow, slow_pop_2_first, do_the_call;
5909 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
ager@chromium.org357bf652010-04-12 11:30:10 +00005910 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005911
ager@chromium.org357bf652010-04-12 11:30:10 +00005912 ASSERT((lhs.is(r0) && rhs.is(r1)) || lhs.is(r1) && rhs.is(r0));
5913
5914 if (ShouldGenerateSmiCode()) {
5915 // Smi-smi case (overflow).
5916 // Since both are Smis there is no heap number to overwrite, so allocate.
5917 // The new heap number is in r5. r6 and r7 are scratch.
5918 __ AllocateHeapNumber(r5, r6, r7, &slow);
5919
5920 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5921 // using registers d7 and d6 for the double values.
5922 if (use_fp_registers) {
5923 CpuFeatures::Scope scope(VFP3);
5924 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
5925 __ vmov(s15, r7);
5926 __ vcvt_f64_s32(d7, s15);
5927 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
5928 __ vmov(s13, r7);
5929 __ vcvt_f64_s32(d6, s13);
5930 } else {
5931 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
5932 __ mov(r7, Operand(rhs));
5933 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5934 __ push(lr);
5935 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5936 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
5937 __ mov(r7, Operand(lhs));
5938 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5939 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5940 __ pop(lr);
5941 }
5942 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005943 }
5944
ager@chromium.org357bf652010-04-12 11:30:10 +00005945 // We branch here if at least one of r0 and r1 is not a Smi.
5946 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005947
ager@chromium.org357bf652010-04-12 11:30:10 +00005948 if (lhs.is(r0)) {
5949 __ Swap(r0, r1, ip);
5950 }
5951
5952 if (ShouldGenerateFPCode()) {
5953 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
5954 switch (op_) {
5955 case Token::ADD:
5956 case Token::SUB:
5957 case Token::MUL:
5958 case Token::DIV:
5959 GenerateTypeTransition(masm);
5960 break;
5961
5962 default:
5963 break;
5964 }
5965 }
5966
5967 if (mode_ == NO_OVERWRITE) {
5968 // In the case where there is no chance of an overwritable float we may as
5969 // well do the allocation immediately while r0 and r1 are untouched.
5970 __ AllocateHeapNumber(r5, r6, r7, &slow);
5971 }
5972
5973 // Move r0 to a double in r2-r3.
5974 __ tst(r0, Operand(kSmiTagMask));
5975 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5976 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5977 __ b(ne, &slow);
5978 if (mode_ == OVERWRITE_RIGHT) {
5979 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5980 }
5981 if (use_fp_registers) {
5982 CpuFeatures::Scope scope(VFP3);
5983 // Load the double from tagged HeapNumber r0 to d7.
5984 __ sub(r7, r0, Operand(kHeapObjectTag));
5985 __ vldr(d7, r7, HeapNumber::kValueOffset);
5986 } else {
5987 // Calling convention says that second double is in r2 and r3.
5988 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5989 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5990 }
5991 __ jmp(&finished_loading_r0);
5992 __ bind(&r0_is_smi);
5993 if (mode_ == OVERWRITE_RIGHT) {
5994 // We can't overwrite a Smi so get address of new heap number into r5.
5995 __ AllocateHeapNumber(r5, r6, r7, &slow);
5996 }
5997
5998 if (use_fp_registers) {
5999 CpuFeatures::Scope scope(VFP3);
6000 // Convert smi in r0 to double in d7.
6001 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6002 __ vmov(s15, r7);
6003 __ vcvt_f64_s32(d7, s15);
6004 } else {
6005 // Write Smi from r0 to r3 and r2 in double format.
6006 __ mov(r7, Operand(r0));
6007 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6008 __ push(lr);
6009 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6010 __ pop(lr);
6011 }
6012
6013 __ bind(&finished_loading_r0);
6014
6015 // Move r1 to a double in r0-r1.
6016 __ tst(r1, Operand(kSmiTagMask));
6017 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
6018 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6019 __ b(ne, &slow);
6020 if (mode_ == OVERWRITE_LEFT) {
6021 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6022 }
6023 if (use_fp_registers) {
6024 CpuFeatures::Scope scope(VFP3);
6025 // Load the double from tagged HeapNumber r1 to d6.
6026 __ sub(r7, r1, Operand(kHeapObjectTag));
6027 __ vldr(d6, r7, HeapNumber::kValueOffset);
6028 } else {
6029 // Calling convention says that first double is in r0 and r1.
6030 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
6031 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
6032 }
6033 __ jmp(&finished_loading_r1);
6034 __ bind(&r1_is_smi);
6035 if (mode_ == OVERWRITE_LEFT) {
6036 // We can't overwrite a Smi so get address of new heap number into r5.
6037 __ AllocateHeapNumber(r5, r6, r7, &slow);
6038 }
6039
6040 if (use_fp_registers) {
6041 CpuFeatures::Scope scope(VFP3);
6042 // Convert smi in r1 to double in d6.
6043 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6044 __ vmov(s13, r7);
6045 __ vcvt_f64_s32(d6, s13);
6046 } else {
6047 // Write Smi from r1 to r1 and r0 in double format.
6048 __ mov(r7, Operand(r1));
6049 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6050 __ push(lr);
6051 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6052 __ pop(lr);
6053 }
6054
6055 __ bind(&finished_loading_r1);
6056
6057 __ bind(&do_the_call);
6058 // If we are inlining the operation using VFP3 instructions for
6059 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6060 if (use_fp_registers) {
6061 CpuFeatures::Scope scope(VFP3);
6062 // ARMv7 VFP3 instructions to implement
6063 // double precision, add, subtract, multiply, divide.
6064
6065 if (Token::MUL == op_) {
6066 __ vmul(d5, d6, d7);
6067 } else if (Token::DIV == op_) {
6068 __ vdiv(d5, d6, d7);
6069 } else if (Token::ADD == op_) {
6070 __ vadd(d5, d6, d7);
6071 } else if (Token::SUB == op_) {
6072 __ vsub(d5, d6, d7);
6073 } else {
6074 UNREACHABLE();
6075 }
6076 __ sub(r0, r5, Operand(kHeapObjectTag));
6077 __ vstr(d5, r0, HeapNumber::kValueOffset);
6078 __ add(r0, r0, Operand(kHeapObjectTag));
6079 __ mov(pc, lr);
6080 } else {
6081 // If we did not inline the operation, then the arguments are in:
6082 // r0: Left value (least significant part of mantissa).
6083 // r1: Left value (sign, exponent, top of mantissa).
6084 // r2: Right value (least significant part of mantissa).
6085 // r3: Right value (sign, exponent, top of mantissa).
6086 // r5: Address of heap number for result.
6087
6088 __ push(lr); // For later.
6089 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
6090 // Call C routine that may not cause GC or other trouble. r5 is callee
6091 // save.
6092 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
6093 // Store answer in the overwritable heap number.
6094 #if !defined(USE_ARM_EABI)
6095 // Double returned in fp coprocessor register 0 and 1, encoded as register
6096 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
6097 // substract the tag from r5.
6098 __ sub(r4, r5, Operand(kHeapObjectTag));
6099 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
6100 #else
6101 // Double returned in registers 0 and 1.
6102 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
6103 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
6104 #endif
6105 __ mov(r0, Operand(r5));
6106 // And we are done.
6107 __ pop(pc);
6108 }
6109 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006110 // We jump to here if something goes wrong (one param is not a number of any
6111 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006112 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006113
6114 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006115 __ push(r1);
6116 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006117
ager@chromium.org357bf652010-04-12 11:30:10 +00006118 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006119 // Test for string arguments before calling runtime.
6120 // r1 : first argument
6121 // r0 : second argument
6122 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00006123 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006124
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006125 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006126 __ tst(r1, Operand(kSmiTagMask));
6127 __ b(eq, &not_string1);
6128 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
6129 __ b(ge, &not_string1);
6130
6131 // First argument is a a string, test second.
6132 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006133 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006134 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6135 __ b(ge, &string1);
6136
6137 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006138 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
6139 __ TailCallStub(&string_add_stub);
6140
6141 __ bind(&string1_smi2);
6142 // First argument is a string, second is a smi. Try to lookup the number
6143 // string for the smi in the number string cache.
6144 NumberToStringStub::GenerateLookupNumberStringCache(
6145 masm, r0, r2, r4, r5, true, &string1);
6146
6147 // Replace second argument on stack and tailcall string add stub to make
6148 // the result.
6149 __ str(r2, MemOperand(sp, 0));
6150 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006151
6152 // Only first argument is a string.
6153 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006154 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
6155
6156 // First argument was not a string, test second.
6157 __ bind(&not_string1);
6158 __ tst(r0, Operand(kSmiTagMask));
6159 __ b(eq, &not_strings);
6160 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6161 __ b(ge, &not_strings);
6162
6163 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006164 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
6165
6166 __ bind(&not_strings);
6167 }
6168
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006169 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170}
6171
6172
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006173// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006174// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006175// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
6176// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006177// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
6178// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006179static void GetInt32(MacroAssembler* masm,
6180 Register source,
6181 Register dest,
6182 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006183 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006184 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006185 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006186 // Get exponent word.
6187 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
6188 // Get exponent alone in scratch2.
6189 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006190 // Load dest with zero. We use this either for the final shift or
6191 // for the answer.
6192 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006193 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006194 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
6195 // the exponent that we are fastest at and also the highest exponent we can
6196 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006197 const uint32_t non_smi_exponent =
6198 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6199 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006200 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
6201 __ b(eq, &right_exponent);
6202 // If the exponent is higher than that then go to slow case. This catches
6203 // numbers that don't fit in a signed int32, infinities and NaNs.
6204 __ b(gt, slow);
6205
6206 // We know the exponent is smaller than 30 (biased). If it is less than
6207 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
6208 // it rounds to zero.
6209 const uint32_t zero_exponent =
6210 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
6211 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
6212 // Dest already has a Smi zero.
6213 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006214 if (!CpuFeatures::IsSupported(VFP3)) {
6215 // We have a shifted exponent between 0 and 30 in scratch2.
6216 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
6217 // We now have the exponent in dest. Subtract from 30 to get
6218 // how much to shift down.
6219 __ rsb(dest, dest, Operand(30));
6220 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006221 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006222 if (CpuFeatures::IsSupported(VFP3)) {
6223 CpuFeatures::Scope scope(VFP3);
6224 // ARMv7 VFP3 instructions implementing double precision to integer
6225 // conversion using round to zero.
6226 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006227 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006228 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006229 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006230 } else {
6231 // Get the top bits of the mantissa.
6232 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
6233 // Put back the implicit 1.
6234 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
6235 // Shift up the mantissa bits to take up the space the exponent used to
6236 // take. We just orred in the implicit bit so that took care of one and
6237 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
6238 // distance.
6239 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6240 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
6241 // Put sign in zero flag.
6242 __ tst(scratch, Operand(HeapNumber::kSignMask));
6243 // Get the second half of the double. For some exponents we don't
6244 // actually need this because the bits get shifted out again, but
6245 // it's probably slower to test than just to do it.
6246 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
6247 // Shift down 22 bits to get the last 10 bits.
6248 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
6249 // Move down according to the exponent.
6250 __ mov(dest, Operand(scratch, LSR, dest));
6251 // Fix sign if sign bit was set.
6252 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
6253 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006254 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006255}
6256
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006257// For bitwise ops where the inputs are not both Smis we here try to determine
6258// whether both inputs are either Smis or at least heap numbers that can be
6259// represented by a 32 bit signed value. We truncate towards zero as required
6260// by the ES spec. If this is the case we do the bitwise op and see if the
6261// result is a Smi. If so, great, otherwise we try to find a heap number to
6262// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00006263// On entry the operands are in lhs and rhs. On exit the answer is in r0.
6264void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
6265 Register lhs,
6266 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006267 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00006268 Label rhs_is_smi, lhs_is_smi;
6269 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006270
ager@chromium.org357bf652010-04-12 11:30:10 +00006271 __ tst(lhs, Operand(kSmiTagMask));
6272 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
6273 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006274 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006275 GetInt32(masm, lhs, r3, r5, r4, &slow);
6276 __ jmp(&done_checking_lhs);
6277 __ bind(&lhs_is_smi);
6278 __ mov(r3, Operand(lhs, ASR, 1));
6279 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006280
ager@chromium.org357bf652010-04-12 11:30:10 +00006281 __ tst(rhs, Operand(kSmiTagMask));
6282 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
6283 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006284 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006285 GetInt32(masm, rhs, r2, r5, r4, &slow);
6286 __ jmp(&done_checking_rhs);
6287 __ bind(&rhs_is_smi);
6288 __ mov(r2, Operand(rhs, ASR, 1));
6289 __ bind(&done_checking_rhs);
6290
6291 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006292
6293 // r0 and r1: Original operands (Smi or heap numbers).
6294 // r2 and r3: Signed int32 operands.
6295 switch (op_) {
6296 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
6297 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
6298 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
6299 case Token::SAR:
6300 // Use only the 5 least significant bits of the shift count.
6301 __ and_(r2, r2, Operand(0x1f));
6302 __ mov(r2, Operand(r3, ASR, r2));
6303 break;
6304 case Token::SHR:
6305 // Use only the 5 least significant bits of the shift count.
6306 __ and_(r2, r2, Operand(0x1f));
6307 __ mov(r2, Operand(r3, LSR, r2), SetCC);
6308 // SHR is special because it is required to produce a positive answer.
6309 // The code below for writing into heap numbers isn't capable of writing
6310 // the register as an unsigned int so we go to slow case if we hit this
6311 // case.
6312 __ b(mi, &slow);
6313 break;
6314 case Token::SHL:
6315 // Use only the 5 least significant bits of the shift count.
6316 __ and_(r2, r2, Operand(0x1f));
6317 __ mov(r2, Operand(r3, LSL, r2));
6318 break;
6319 default: UNREACHABLE();
6320 }
6321 // check that the *signed* result fits in a smi
6322 __ add(r3, r2, Operand(0x40000000), SetCC);
6323 __ b(mi, &result_not_a_smi);
6324 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6325 __ Ret();
6326
6327 Label have_to_allocate, got_a_heap_number;
6328 __ bind(&result_not_a_smi);
6329 switch (mode_) {
6330 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006331 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006332 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006333 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006334 break;
6335 }
6336 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006337 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006338 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006339 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006340 break;
6341 }
6342 case NO_OVERWRITE: {
6343 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006344 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006345 }
6346 default: break;
6347 }
6348 __ bind(&got_a_heap_number);
6349 // r2: Answer as signed int32.
6350 // r5: Heap number to write answer into.
6351
6352 // Nothing can go wrong now, so move the heap number to r0, which is the
6353 // result.
6354 __ mov(r0, Operand(r5));
6355
6356 // Tail call that writes the int32 in r2 to the heap number in r0, using
6357 // r3 as scratch. r0 is preserved and returned.
6358 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
6359 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6360
6361 if (mode_ != NO_OVERWRITE) {
6362 __ bind(&have_to_allocate);
6363 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006364 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006365 __ jmp(&got_a_heap_number);
6366 }
6367
6368 // If all else failed then we go to the runtime system.
6369 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006370 __ push(lhs); // restore stack
6371 __ push(rhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006372 switch (op_) {
6373 case Token::BIT_OR:
6374 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
6375 break;
6376 case Token::BIT_AND:
6377 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
6378 break;
6379 case Token::BIT_XOR:
6380 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
6381 break;
6382 case Token::SAR:
6383 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
6384 break;
6385 case Token::SHR:
6386 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
6387 break;
6388 case Token::SHL:
6389 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
6390 break;
6391 default:
6392 UNREACHABLE();
6393 }
6394}
6395
6396
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006397// Can we multiply by x with max two shifts and an add.
6398// This answers yes to all integers from 2 to 10.
6399static bool IsEasyToMultiplyBy(int x) {
6400 if (x < 2) return false; // Avoid special cases.
6401 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
6402 if (IsPowerOf2(x)) return true; // Simple shift.
6403 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
6404 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
6405 return false;
6406}
6407
6408
6409// Can multiply by anything that IsEasyToMultiplyBy returns true for.
6410// Source and destination may be the same register. This routine does
6411// not set carry and overflow the way a mul instruction would.
6412static void MultiplyByKnownInt(MacroAssembler* masm,
6413 Register source,
6414 Register destination,
6415 int known_int) {
6416 if (IsPowerOf2(known_int)) {
6417 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
6418 } else if (PopCountLessThanEqual2(known_int)) {
6419 int first_bit = BitPosition(known_int);
6420 int second_bit = BitPosition(known_int ^ (1 << first_bit));
6421 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
6422 if (first_bit != 0) {
6423 __ mov(destination, Operand(destination, LSL, first_bit));
6424 }
6425 } else {
6426 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
6427 int the_bit = BitPosition(known_int + 1);
6428 __ rsb(destination, source, Operand(source, LSL, the_bit));
6429 }
6430}
6431
6432
6433// This function (as opposed to MultiplyByKnownInt) takes the known int in a
6434// a register for the cases where it doesn't know a good trick, and may deliver
6435// a result that needs shifting.
6436static void MultiplyByKnownInt2(
6437 MacroAssembler* masm,
6438 Register result,
6439 Register source,
6440 Register known_int_register, // Smi tagged.
6441 int known_int,
6442 int* required_shift) { // Including Smi tag shift
6443 switch (known_int) {
6444 case 3:
6445 __ add(result, source, Operand(source, LSL, 1));
6446 *required_shift = 1;
6447 break;
6448 case 5:
6449 __ add(result, source, Operand(source, LSL, 2));
6450 *required_shift = 1;
6451 break;
6452 case 6:
6453 __ add(result, source, Operand(source, LSL, 1));
6454 *required_shift = 2;
6455 break;
6456 case 7:
6457 __ rsb(result, source, Operand(source, LSL, 3));
6458 *required_shift = 1;
6459 break;
6460 case 9:
6461 __ add(result, source, Operand(source, LSL, 3));
6462 *required_shift = 1;
6463 break;
6464 case 10:
6465 __ add(result, source, Operand(source, LSL, 2));
6466 *required_shift = 2;
6467 break;
6468 default:
6469 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
6470 __ mul(result, source, known_int_register);
6471 *required_shift = 0;
6472 }
6473}
6474
6475
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006476const char* GenericBinaryOpStub::GetName() {
6477 if (name_ != NULL) return name_;
6478 const int len = 100;
6479 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
6480 if (name_ == NULL) return "OOM";
6481 const char* op_name = Token::Name(op_);
6482 const char* overwrite_name;
6483 switch (mode_) {
6484 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
6485 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
6486 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
6487 default: overwrite_name = "UnknownOverwrite"; break;
6488 }
6489
6490 OS::SNPrintF(Vector<char>(name_, len),
6491 "GenericBinaryOpStub_%s_%s%s",
6492 op_name,
6493 overwrite_name,
6494 specialized_on_rhs_ ? "_ConstantRhs" : 0);
6495 return name_;
6496}
6497
6498
ager@chromium.org5c838252010-02-19 08:53:10 +00006499
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006500void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006501 // lhs_ : x
6502 // rhs_ : y
6503 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006504
ager@chromium.org357bf652010-04-12 11:30:10 +00006505 Register result = r0;
6506 Register lhs = lhs_;
6507 Register rhs = rhs_;
6508
6509 // This code can't cope with other register allocations yet.
6510 ASSERT(result.is(r0) &&
6511 ((lhs.is(r0) && rhs.is(r1)) ||
6512 (lhs.is(r1) && rhs.is(r0))));
6513
6514 Register smi_test_reg = VirtualFrame::scratch0();
6515 Register scratch = VirtualFrame::scratch1();
6516
6517 // All ops need to know whether we are dealing with two Smis. Set up
6518 // smi_test_reg to tell us that.
6519 if (ShouldGenerateSmiCode()) {
6520 __ orr(smi_test_reg, lhs, Operand(rhs));
6521 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006522
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006523 switch (op_) {
6524 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006525 Label not_smi;
6526 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00006527 if (ShouldGenerateSmiCode()) {
6528 ASSERT(kSmiTag == 0); // Adjust code below.
6529 __ tst(smi_test_reg, Operand(kSmiTagMask));
6530 __ b(ne, &not_smi);
6531 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
6532 // Return if no overflow.
6533 __ Ret(vc);
6534 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
6535 }
6536 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006537 break;
6538 }
6539
6540 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006541 Label not_smi;
6542 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00006543 if (ShouldGenerateSmiCode()) {
6544 ASSERT(kSmiTag == 0); // Adjust code below.
6545 __ tst(smi_test_reg, Operand(kSmiTagMask));
6546 __ b(ne, &not_smi);
6547 if (lhs.is(r1)) {
6548 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
6549 // Return if no overflow.
6550 __ Ret(vc);
6551 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
6552 } else {
6553 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
6554 // Return if no overflow.
6555 __ Ret(vc);
6556 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
6557 }
6558 }
6559 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006560 break;
6561 }
6562
6563 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006564 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00006565 if (ShouldGenerateSmiCode()) {
6566 ASSERT(kSmiTag == 0); // adjust code below
6567 __ tst(smi_test_reg, Operand(kSmiTagMask));
6568 Register scratch2 = smi_test_reg;
6569 smi_test_reg = no_reg;
6570 __ b(ne, &not_smi);
6571 // Remove tag from one operand (but keep sign), so that result is Smi.
6572 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
6573 // Do multiplication
6574 // scratch = lower 32 bits of ip * lhs.
6575 __ smull(scratch, scratch2, lhs, ip);
6576 // Go slow on overflows (overflow bit is not set).
6577 __ mov(ip, Operand(scratch, ASR, 31));
6578 // No overflow if higher 33 bits are identical.
6579 __ cmp(ip, Operand(scratch2));
6580 __ b(ne, &slow);
6581 // Go slow on zero result to handle -0.
6582 __ tst(scratch, Operand(scratch));
6583 __ mov(result, Operand(scratch), LeaveCC, ne);
6584 __ Ret(ne);
6585 // We need -0 if we were multiplying a negative number with 0 to get 0.
6586 // We know one of them was zero.
6587 __ add(scratch2, rhs, Operand(lhs), SetCC);
6588 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
6589 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
6590 // Slow case. We fall through here if we multiplied a negative number
6591 // with 0, because that would mean we should produce -0.
6592 __ bind(&slow);
6593 }
6594 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006595 break;
6596 }
6597
6598 case Token::DIV:
6599 case Token::MOD: {
6600 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00006601 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006602 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00006603 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006604 if (IsPowerOf2(constant_rhs_)) {
6605 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006606 __ and_(rhs,
6607 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006608 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6609 SetCC);
6610 // We now have the answer, but if the input was negative we also
6611 // have the sign bit. Our work is done if the result is
6612 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00006613 if (!rhs.is(r0)) {
6614 __ mov(r0, rhs, LeaveCC, pl);
6615 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006616 __ Ret(pl);
6617 // A mod of a negative left hand side must return a negative number.
6618 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00006619 // already optimistically trashed rhs so we may need to restore it.
6620 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006621 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006622 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006623 __ b(eq, &smi_is_unsuitable);
6624 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00006625 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006626 } else {
6627 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00006628 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006629 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6630 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6631 int shift = 0;
6632 int d = constant_rhs_;
6633 while ((d & 1) == 0) {
6634 d >>= 1;
6635 shift++;
6636 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006637 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006638 __ bic(r0, r0, Operand(kSmiTagMask));
6639 }
6640 } else {
6641 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00006642 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006643 __ b(ne, &smi_is_unsuitable);
6644 // Find a fixed point reciprocal of the divisor so we can divide by
6645 // multiplying.
6646 double divisor = 1.0 / constant_rhs_;
6647 int shift = 32;
6648 double scale = 4294967296.0; // 1 << 32.
6649 uint32_t mul;
6650 // Maximise the precision of the fixed point reciprocal.
6651 while (true) {
6652 mul = static_cast<uint32_t>(scale * divisor);
6653 if (mul >= 0x7fffffff) break;
6654 scale *= 2.0;
6655 shift++;
6656 }
6657 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00006658 Register scratch2 = smi_test_reg;
6659 smi_test_reg = no_reg;
6660 __ mov(scratch2, Operand(mul));
6661 __ umull(scratch, scratch2, scratch2, lhs);
6662 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
6663 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
6664 // rhs is still the known rhs. rhs is Smi tagged.
6665 // lhs is still the unkown lhs. lhs is Smi tagged.
6666 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
6667 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006668 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00006669 scratch,
6670 scratch2,
6671 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006672 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00006673 &required_scratch_shift);
6674 // scratch << required_scratch_shift is now the Smi tagged rhs *
6675 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006676 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006677 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006678 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00006679 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006680 } else {
6681 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00006682 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006683 }
6684 }
6685 __ Ret();
6686 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006687 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006688 HandleBinaryOpSlowCases(
6689 masm,
6690 &not_smi,
6691 lhs,
6692 rhs,
6693 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006694 break;
6695 }
6696
6697 case Token::BIT_OR:
6698 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006699 case Token::BIT_XOR:
6700 case Token::SAR:
6701 case Token::SHR:
6702 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006703 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006704 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00006705 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006706 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006707 Register scratch2 = smi_test_reg;
6708 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006709 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006710 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
6711 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
6712 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006713 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006714 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00006715 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6716 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006717 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00006718 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006719 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006720 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006721 // Remove tags from operands. We can't do this on a 31 bit number
6722 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00006723 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
6724 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6725 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006726 // Unsigned shift is not allowed to produce a negative number, so
6727 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00006728 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006729 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006730 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00006731 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006732 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006733 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006734 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00006735 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
6736 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6737 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006738 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00006739 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006740 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006741 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006742 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006743 default: UNREACHABLE();
6744 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006745 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006746 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006747 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006748 break;
6749 }
6750
6751 default: UNREACHABLE();
6752 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006753 // This code should be unreachable.
6754 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00006755
6756 // Generate an unreachable reference to the DEFAULT stub so that it can be
6757 // found at the end of this stub when clearing ICs at GC.
6758 // TODO(kaznacheev): Check performance impact and get rid of this.
6759 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
6760 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
6761 __ CallStub(&uninit);
6762 }
6763}
6764
6765
6766void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
6767 Label get_result;
6768
6769 __ push(r1);
6770 __ push(r0);
6771
6772 // Internal frame is necessary to handle exceptions properly.
6773 __ EnterInternalFrame();
6774 // Call the stub proper to get the result in r0.
6775 __ Call(&get_result);
6776 __ LeaveInternalFrame();
6777
6778 __ push(r0);
6779
6780 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
6781 __ push(r0);
6782 __ mov(r0, Operand(Smi::FromInt(op_)));
6783 __ push(r0);
6784 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
6785 __ push(r0);
6786
6787 __ TailCallExternalReference(
6788 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
6789 6,
6790 1);
6791
6792 // The entry point for the result calculation is assumed to be immediately
6793 // after this sequence.
6794 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006795}
6796
6797
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006798Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006799 GenericBinaryOpStub stub(key, type_info);
6800 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006801}
6802
6803
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006804void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006805 // Do tail-call to runtime routine. Runtime routines expect at least one
6806 // argument, so give it a Smi.
6807 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006808 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006809 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006810
6811 __ StubReturn(1);
6812}
6813
6814
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006815void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006816 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006817
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006818 if (op_ == Token::SUB) {
6819 // Check whether the value is a smi.
6820 Label try_float;
6821 __ tst(r0, Operand(kSmiTagMask));
6822 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006823
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006824 // Go slow case if the value of the expression is zero
6825 // to make sure that we switch between 0 and -0.
6826 __ cmp(r0, Operand(0));
6827 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006828
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006829 // The value of the expression is a smi that is not zero. Try
6830 // optimistic subtraction '0 - value'.
6831 __ rsb(r1, r0, Operand(0), SetCC);
6832 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006833
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006834 __ mov(r0, Operand(r1)); // Set r0 to result.
6835 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006836
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006837 __ bind(&try_float);
6838 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6839 __ b(ne, &slow);
6840 // r0 is a heap number. Get a new heap number in r1.
6841 if (overwrite_) {
6842 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6843 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6844 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6845 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006846 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006847 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6848 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6849 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6850 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6851 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6852 __ mov(r0, Operand(r1));
6853 }
6854 } else if (op_ == Token::BIT_NOT) {
6855 // Check if the operand is a heap number.
6856 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6857 __ b(ne, &slow);
6858
6859 // Convert the heap number is r0 to an untagged integer in r1.
6860 GetInt32(masm, r0, r1, r2, r3, &slow);
6861
6862 // Do the bitwise operation (move negated) and check if the result
6863 // fits in a smi.
6864 Label try_float;
6865 __ mvn(r1, Operand(r1));
6866 __ add(r2, r1, Operand(0x40000000), SetCC);
6867 __ b(mi, &try_float);
6868 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6869 __ b(&done);
6870
6871 __ bind(&try_float);
6872 if (!overwrite_) {
6873 // Allocate a fresh heap number, but don't overwrite r0 until
6874 // we're sure we can do it without going through the slow case
6875 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006876 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006877 __ mov(r0, Operand(r2));
6878 }
6879
6880 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6881 // have to set up a frame.
6882 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6883 __ push(lr);
6884 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6885 __ pop(lr);
6886 } else {
6887 UNIMPLEMENTED();
6888 }
6889
6890 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006891 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006892
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006893 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006894 __ bind(&slow);
6895 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006896 switch (op_) {
6897 case Token::SUB:
6898 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6899 break;
6900 case Token::BIT_NOT:
6901 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6902 break;
6903 default:
6904 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006905 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006906}
6907
6908
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006909void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006910 // r0 holds the exception.
6911
6912 // Adjust this code if not the case.
6913 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6914
6915 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006916 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6917 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006918
6919 // Restore the next handler and frame pointer, discard handler state.
6920 ASSERT(StackHandlerConstants::kNextOffset == 0);
6921 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006922 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006923 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6924 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6925
6926 // Before returning we restore the context from the frame pointer if
6927 // not NULL. The frame pointer is NULL in the exception handler of a
6928 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006929 __ cmp(fp, Operand(0));
6930 // Set cp to NULL if fp is NULL.
6931 __ mov(cp, Operand(0), LeaveCC, eq);
6932 // Restore cp otherwise.
6933 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006934#ifdef DEBUG
6935 if (FLAG_debug_code) {
6936 __ mov(lr, Operand(pc));
6937 }
6938#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006939 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006940 __ pop(pc);
6941}
6942
6943
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006944void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6945 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006946 // Adjust this code if not the case.
6947 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6948
6949 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006950 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006951 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006952
6953 // Unwind the handlers until the ENTRY handler is found.
6954 Label loop, done;
6955 __ bind(&loop);
6956 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006957 const int kStateOffset = StackHandlerConstants::kStateOffset;
6958 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006959 __ cmp(r2, Operand(StackHandler::ENTRY));
6960 __ b(eq, &done);
6961 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006962 const int kNextOffset = StackHandlerConstants::kNextOffset;
6963 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006964 __ jmp(&loop);
6965 __ bind(&done);
6966
6967 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006968 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006969 __ pop(r2);
6970 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006971
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006972 if (type == OUT_OF_MEMORY) {
6973 // Set external caught exception to false.
6974 ExternalReference external_caught(Top::k_external_caught_exception_address);
6975 __ mov(r0, Operand(false));
6976 __ mov(r2, Operand(external_caught));
6977 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006978
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006979 // Set pending exception and r0 to out of memory exception.
6980 Failure* out_of_memory = Failure::OutOfMemoryException();
6981 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6982 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6983 __ str(r0, MemOperand(r2));
6984 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006985
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006986 // Stack layout at this point. See also StackHandlerConstants.
6987 // sp -> state (ENTRY)
6988 // fp
6989 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006990
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006991 // Discard handler state (r2 is not used) and restore frame pointer.
6992 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6993 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6994 // Before returning we restore the context from the frame pointer if
6995 // not NULL. The frame pointer is NULL in the exception handler of a
6996 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006997 __ cmp(fp, Operand(0));
6998 // Set cp to NULL if fp is NULL.
6999 __ mov(cp, Operand(0), LeaveCC, eq);
7000 // Restore cp otherwise.
7001 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007002#ifdef DEBUG
7003 if (FLAG_debug_code) {
7004 __ mov(lr, Operand(pc));
7005 }
7006#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007007 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007008 __ pop(pc);
7009}
7010
7011
7012void CEntryStub::GenerateCore(MacroAssembler* masm,
7013 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007014 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007015 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007016 bool do_gc,
7017 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007018 // r0: result parameter for PerformGC, if any
7019 // r4: number of arguments including receiver (C callee-saved)
7020 // r5: pointer to builtin function (C callee-saved)
7021 // r6: pointer to the first argument (C callee-saved)
7022
7023 if (do_gc) {
7024 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007025 ExternalReference gc_reference = ExternalReference::perform_gc_function();
7026 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007027 }
7028
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007029 ExternalReference scope_depth =
7030 ExternalReference::heap_always_allocate_scope_depth();
7031 if (always_allocate) {
7032 __ mov(r0, Operand(scope_depth));
7033 __ ldr(r1, MemOperand(r0));
7034 __ add(r1, r1, Operand(1));
7035 __ str(r1, MemOperand(r0));
7036 }
7037
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007038 // Call C built-in.
7039 // r0 = argc, r1 = argv
7040 __ mov(r0, Operand(r4));
7041 __ mov(r1, Operand(r6));
7042
7043 // TODO(1242173): To let the GC traverse the return address of the exit
7044 // frames, we need to know where the return address is. Right now,
7045 // we push it on the stack to be able to find it again, but we never
7046 // restore from it in case of changes, which makes it impossible to
7047 // support moving the C entry code stub. This should be fixed, but currently
7048 // this is OK because the CEntryStub gets generated so early in the V8 boot
7049 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007050 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
7051 masm->push(lr);
7052 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007053
7054 if (always_allocate) {
7055 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
7056 // though (contain the result).
7057 __ mov(r2, Operand(scope_depth));
7058 __ ldr(r3, MemOperand(r2));
7059 __ sub(r3, r3, Operand(1));
7060 __ str(r3, MemOperand(r2));
7061 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007062
7063 // check for failure result
7064 Label failure_returned;
7065 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
7066 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
7067 __ add(r2, r0, Operand(1));
7068 __ tst(r2, Operand(kFailureTagMask));
7069 __ b(eq, &failure_returned);
7070
7071 // Exit C frame and return.
7072 // r0:r1: result
7073 // sp: stack pointer
7074 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007075 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007076
7077 // check if we should retry or throw exception
7078 Label retry;
7079 __ bind(&failure_returned);
7080 ASSERT(Failure::RETRY_AFTER_GC == 0);
7081 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
7082 __ b(eq, &retry);
7083
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007084 // Special handling of out of memory exceptions.
7085 Failure* out_of_memory = Failure::OutOfMemoryException();
7086 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7087 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007088
7089 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00007090 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007091 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007092 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007093 __ ldr(r0, MemOperand(ip));
7094 __ str(r3, MemOperand(ip));
7095
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007096 // Special handling of termination exceptions which are uncatchable
7097 // by javascript code.
7098 __ cmp(r0, Operand(Factory::termination_exception()));
7099 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007100
7101 // Handle normal exception.
7102 __ jmp(throw_normal_exception);
7103
7104 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
7105}
7106
7107
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007108void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007109 // Called from JavaScript; parameters are on stack as if calling JS function
7110 // r0: number of arguments including receiver
7111 // r1: pointer to builtin function
7112 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007113 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007114 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007115
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007116 // Result returned in r0 or r0+r1 by default.
7117
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007118 // NOTE: Invocations of builtins may return failure objects
7119 // instead of a proper result. The builtin entry handles
7120 // this by performing a garbage collection and retrying the
7121 // builtin once.
7122
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007123 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007124 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007125
7126 // r4: number of arguments (C callee-saved)
7127 // r5: pointer to builtin function (C callee-saved)
7128 // r6: pointer to first argument (C callee-saved)
7129
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007130 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007131 Label throw_termination_exception;
7132 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007133
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007134 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007135 GenerateCore(masm,
7136 &throw_normal_exception,
7137 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007138 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007139 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007140 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007141
7142 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007143 GenerateCore(masm,
7144 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007145 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007146 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007147 true,
7148 false);
7149
7150 // Do full GC and retry runtime call one final time.
7151 Failure* failure = Failure::InternalError();
7152 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
7153 GenerateCore(masm,
7154 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007155 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007156 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007157 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007158 true);
7159
7160 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007161 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
7162
7163 __ bind(&throw_termination_exception);
7164 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007165
7166 __ bind(&throw_normal_exception);
7167 GenerateThrowTOS(masm);
7168}
7169
7170
7171void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
7172 // r0: code entry
7173 // r1: function
7174 // r2: receiver
7175 // r3: argc
7176 // [sp+0]: argv
7177
7178 Label invoke, exit;
7179
7180 // Called from C, so do not pop argc and args on exit (preserve sp)
7181 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007182 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007183 __ stm(db_w, sp, kCalleeSaved | lr.bit());
7184
7185 // Get address of argv, see stm above.
7186 // r0: code entry
7187 // r1: function
7188 // r2: receiver
7189 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00007190 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007191
7192 // Push a frame with special values setup to mark it as an entry frame.
7193 // r0: code entry
7194 // r1: function
7195 // r2: receiver
7196 // r3: argc
7197 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007198 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007199 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
7200 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007201 __ mov(r6, Operand(Smi::FromInt(marker)));
7202 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7203 __ ldr(r5, MemOperand(r5));
7204 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
7205
7206 // Setup frame pointer for the frame to be pushed.
7207 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7208
7209 // Call a faked try-block that does the invoke.
7210 __ bl(&invoke);
7211
7212 // Caught exception: Store result (exception) in the pending
7213 // exception field in the JSEnv and return a failure sentinel.
7214 // Coming in here the fp will be invalid because the PushTryHandler below
7215 // sets it to 0 to signal the existence of the JSEntry frame.
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 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007218 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007219 __ b(&exit);
7220
7221 // Invoke: Link this frame into the handler chain.
7222 __ bind(&invoke);
7223 // Must preserve r0-r4, r5-r7 are available.
7224 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007225 // If an exception not caught by another handler occurs, this handler
7226 // returns control to the code after the bl(&invoke) above, which
7227 // restores all kCalleeSaved registers (including cp and fp) to their
7228 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007229
7230 // Clear any pending exceptions.
7231 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
7232 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007233 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007234 __ str(r5, MemOperand(ip));
7235
7236 // Invoke the function by calling through JS entry trampoline builtin.
7237 // Notice that we cannot store a reference to the trampoline code directly in
7238 // this stub, because runtime stubs are not traversed when doing GC.
7239
7240 // Expected registers by Builtins::JSEntryTrampoline
7241 // r0: code entry
7242 // r1: function
7243 // r2: receiver
7244 // r3: argc
7245 // r4: argv
7246 if (is_construct) {
7247 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
7248 __ mov(ip, Operand(construct_entry));
7249 } else {
7250 ExternalReference entry(Builtins::JSEntryTrampoline);
7251 __ mov(ip, Operand(entry));
7252 }
7253 __ ldr(ip, MemOperand(ip)); // deref address
7254
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007255 // Branch and link to JSEntryTrampoline. We don't use the double underscore
7256 // macro for the add instruction because we don't want the coverage tool
7257 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007258 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007259 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007260
7261 // Unlink this frame from the handler chain. When reading the
7262 // address of the next handler, there is no need to use the address
7263 // displacement since the current stack pointer (sp) points directly
7264 // to the stack handler.
7265 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
7266 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
7267 __ str(r3, MemOperand(ip));
7268 // No need to restore registers
7269 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
7270
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007271
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007272 __ bind(&exit); // r0 holds result
7273 // Restore the top frame descriptors from the stack.
7274 __ pop(r3);
7275 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7276 __ str(r3, MemOperand(ip));
7277
7278 // Reset the stack to the callee saved registers.
7279 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7280
7281 // Restore callee-saved registers and return.
7282#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007283 if (FLAG_debug_code) {
7284 __ mov(lr, Operand(pc));
7285 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007286#endif
7287 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
7288}
7289
7290
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007291// This stub performs an instanceof, calling the builtin function if
7292// necessary. Uses r1 for the object, r0 for the function that it may
7293// be an instance of (these are fetched from the stack).
7294void InstanceofStub::Generate(MacroAssembler* masm) {
7295 // Get the object - slow case for smis (we may need to throw an exception
7296 // depending on the rhs).
7297 Label slow, loop, is_instance, is_not_instance;
7298 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
7299 __ BranchOnSmi(r0, &slow);
7300
7301 // Check that the left hand is a JS object and put map in r3.
7302 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
7303 __ b(lt, &slow);
7304 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
7305 __ b(gt, &slow);
7306
7307 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00007308 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007309 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
7310
7311 // Check that the function prototype is a JS object.
7312 __ BranchOnSmi(r4, &slow);
7313 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
7314 __ b(lt, &slow);
7315 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
7316 __ b(gt, &slow);
7317
7318 // Register mapping: r3 is object map and r4 is function prototype.
7319 // Get prototype of object into r2.
7320 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
7321
7322 // Loop through the prototype chain looking for the function prototype.
7323 __ bind(&loop);
7324 __ cmp(r2, Operand(r4));
7325 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00007326 __ LoadRoot(ip, Heap::kNullValueRootIndex);
7327 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007328 __ b(eq, &is_not_instance);
7329 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
7330 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
7331 __ jmp(&loop);
7332
7333 __ bind(&is_instance);
7334 __ mov(r0, Operand(Smi::FromInt(0)));
7335 __ pop();
7336 __ pop();
7337 __ mov(pc, Operand(lr)); // Return.
7338
7339 __ bind(&is_not_instance);
7340 __ mov(r0, Operand(Smi::FromInt(1)));
7341 __ pop();
7342 __ pop();
7343 __ mov(pc, Operand(lr)); // Return.
7344
7345 // Slow-case. Tail call builtin.
7346 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007347 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
7348}
7349
7350
ager@chromium.org7c537e22008-10-16 08:43:32 +00007351void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
7352 // The displacement is the offset of the last parameter (if any)
7353 // relative to the frame pointer.
7354 static const int kDisplacement =
7355 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007356
ager@chromium.org7c537e22008-10-16 08:43:32 +00007357 // Check that the key is a smi.
7358 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007359 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007360
ager@chromium.org7c537e22008-10-16 08:43:32 +00007361 // Check if the calling frame is an arguments adaptor frame.
7362 Label adaptor;
7363 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
7364 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007365 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00007366 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007367
ager@chromium.org7c537e22008-10-16 08:43:32 +00007368 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007369 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00007370 // check for free.
7371 __ cmp(r1, r0);
7372 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007373
ager@chromium.org7c537e22008-10-16 08:43:32 +00007374 // Read the argument from the stack and return it.
7375 __ sub(r3, r0, r1);
7376 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7377 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00007378 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007379
7380 // Arguments adaptor case: Check index against actual arguments
7381 // limit found in the arguments adaptor frame. Use unsigned
7382 // comparison to get negative check for free.
7383 __ bind(&adaptor);
7384 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
7385 __ cmp(r1, r0);
7386 __ b(cs, &slow);
7387
7388 // Read the argument from the adaptor frame and return it.
7389 __ sub(r3, r0, r1);
7390 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7391 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00007392 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007393
7394 // Slow-case: Handle non-smi or out-of-bounds access to arguments
7395 // by calling the runtime system.
7396 __ bind(&slow);
7397 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007398 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007399}
7400
7401
7402void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00007403 // sp[0] : number of parameters
7404 // sp[4] : receiver displacement
7405 // sp[8] : function
7406
ager@chromium.org7c537e22008-10-16 08:43:32 +00007407 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00007408 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00007409 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
7410 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007411 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00007412 __ b(eq, &adaptor_frame);
7413
7414 // Get the length from the frame.
7415 __ ldr(r1, MemOperand(sp, 0));
7416 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007417
7418 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00007419 __ bind(&adaptor_frame);
7420 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
7421 __ str(r1, MemOperand(sp, 0));
7422 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00007423 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
7424 __ str(r3, MemOperand(sp, 1 * kPointerSize));
7425
ager@chromium.org5c838252010-02-19 08:53:10 +00007426 // Try the new space allocation. Start out with computing the size
7427 // of the arguments object and the elements array (in words, not
7428 // bytes because AllocateInNewSpace expects words).
7429 Label add_arguments_object;
7430 __ bind(&try_allocate);
7431 __ cmp(r1, Operand(0));
7432 __ b(eq, &add_arguments_object);
7433 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
7434 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
7435 __ bind(&add_arguments_object);
7436 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
7437
7438 // Do the allocation of both objects in one go.
7439 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
7440
7441 // Get the arguments boilerplate from the current (global) context.
7442 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
7443 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7444 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
7445 __ ldr(r4, MemOperand(r4, offset));
7446
7447 // Copy the JS object part.
7448 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
7449 __ ldr(r3, FieldMemOperand(r4, i));
7450 __ str(r3, FieldMemOperand(r0, i));
7451 }
7452
7453 // Setup the callee in-object property.
7454 ASSERT(Heap::arguments_callee_index == 0);
7455 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
7456 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
7457
7458 // Get the length (smi tagged) and set that as an in-object property too.
7459 ASSERT(Heap::arguments_length_index == 1);
7460 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
7461 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
7462
7463 // If there are no actual arguments, we're done.
7464 Label done;
7465 __ cmp(r1, Operand(0));
7466 __ b(eq, &done);
7467
7468 // Get the parameters pointer from the stack and untag the length.
7469 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
7470 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
7471
7472 // Setup the elements pointer in the allocated arguments object and
7473 // initialize the header in the elements fixed array.
7474 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
7475 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
7476 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
7477 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
7478 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
7479
7480 // Copy the fixed array slots.
7481 Label loop;
7482 // Setup r4 to point to the first array slot.
7483 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7484 __ bind(&loop);
7485 // Pre-decrement r2 with kPointerSize on each iteration.
7486 // Pre-decrement in order to skip receiver.
7487 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
7488 // Post-increment r4 with kPointerSize on each iteration.
7489 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
7490 __ sub(r1, r1, Operand(1));
7491 __ cmp(r1, Operand(0));
7492 __ b(ne, &loop);
7493
7494 // Return and remove the on-stack parameters.
7495 __ bind(&done);
7496 __ add(sp, sp, Operand(3 * kPointerSize));
7497 __ Ret();
7498
ager@chromium.org7c537e22008-10-16 08:43:32 +00007499 // Do the runtime call to allocate the arguments object.
7500 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007501 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007502}
7503
7504
7505void CallFunctionStub::Generate(MacroAssembler* masm) {
7506 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007507
7508 // If the receiver might be a value (string, number or boolean) check for this
7509 // and box it if it is.
7510 if (ReceiverMightBeValue()) {
7511 // Get the receiver from the stack.
7512 // function, receiver [, arguments]
7513 Label receiver_is_value, receiver_is_js_object;
7514 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
7515
7516 // Check if receiver is a smi (which is a number value).
7517 __ BranchOnSmi(r1, &receiver_is_value);
7518
7519 // Check if the receiver is a valid JS object.
7520 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
7521 __ b(ge, &receiver_is_js_object);
7522
7523 // Call the runtime to box the value.
7524 __ bind(&receiver_is_value);
7525 __ EnterInternalFrame();
7526 __ push(r1);
7527 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
7528 __ LeaveInternalFrame();
7529 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
7530
7531 __ bind(&receiver_is_js_object);
7532 }
7533
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007534 // Get the function to call from the stack.
7535 // function, receiver [, arguments]
7536 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
7537
7538 // Check that the function is really a JavaScript function.
7539 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007540 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007541 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007542 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007543 __ b(ne, &slow);
7544
7545 // Fast-case: Invoke the function now.
7546 // r1: pushed function
7547 ParameterCount actual(argc_);
7548 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
7549
7550 // Slow-case: Non-function called.
7551 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00007552 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
7553 // of the original receiver from the call site).
7554 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007555 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007556 __ mov(r2, Operand(0));
7557 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
7558 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
7559 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007560}
7561
7562
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007563// Unfortunately you have to run without snapshots to see most of these
7564// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007565const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007566 if (name_ != NULL) return name_;
7567 const int kMaxNameLength = 100;
7568 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
7569 if (name_ == NULL) return "OOM";
7570
7571 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007572 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007573 case lt: cc_name = "LT"; break;
7574 case gt: cc_name = "GT"; break;
7575 case le: cc_name = "LE"; break;
7576 case ge: cc_name = "GE"; break;
7577 case eq: cc_name = "EQ"; break;
7578 case ne: cc_name = "NE"; break;
7579 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007580 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007581
7582 const char* strict_name = "";
7583 if (strict_ && (cc_ == eq || cc_ == ne)) {
7584 strict_name = "_STRICT";
7585 }
7586
7587 const char* never_nan_nan_name = "";
7588 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
7589 never_nan_nan_name = "_NO_NAN";
7590 }
7591
7592 const char* include_number_compare_name = "";
7593 if (!include_number_compare_) {
7594 include_number_compare_name = "_NO_NUMBER";
7595 }
7596
7597 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
7598 "CompareStub_%s%s%s%s",
7599 cc_name,
7600 strict_name,
7601 never_nan_nan_name,
7602 include_number_compare_name);
7603 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007604}
7605
7606
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007607int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007608 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
7609 // stubs the never NaN NaN condition is only taken into account if the
7610 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007611 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007612 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
7613 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007614 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
7615 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007616}
7617
7618
ager@chromium.org5c838252010-02-19 08:53:10 +00007619void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
7620 Register dest,
7621 Register src,
7622 Register count,
7623 Register scratch,
7624 bool ascii) {
7625 Label loop;
7626 Label done;
7627 // This loop just copies one character at a time, as it is only used for very
7628 // short strings.
7629 if (!ascii) {
7630 __ add(count, count, Operand(count), SetCC);
7631 } else {
7632 __ cmp(count, Operand(0));
7633 }
7634 __ b(eq, &done);
7635
7636 __ bind(&loop);
7637 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
7638 // Perform sub between load and dependent store to get the load time to
7639 // complete.
7640 __ sub(count, count, Operand(1), SetCC);
7641 __ strb(scratch, MemOperand(dest, 1, PostIndex));
7642 // last iteration.
7643 __ b(gt, &loop);
7644
7645 __ bind(&done);
7646}
7647
7648
7649enum CopyCharactersFlags {
7650 COPY_ASCII = 1,
7651 DEST_ALWAYS_ALIGNED = 2
7652};
7653
7654
7655void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
7656 Register dest,
7657 Register src,
7658 Register count,
7659 Register scratch1,
7660 Register scratch2,
7661 Register scratch3,
7662 Register scratch4,
7663 Register scratch5,
7664 int flags) {
7665 bool ascii = (flags & COPY_ASCII) != 0;
7666 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
7667
7668 if (dest_always_aligned && FLAG_debug_code) {
7669 // Check that destination is actually word aligned if the flag says
7670 // that it is.
7671 __ tst(dest, Operand(kPointerAlignmentMask));
7672 __ Check(eq, "Destination of copy not aligned.");
7673 }
7674
7675 const int kReadAlignment = 4;
7676 const int kReadAlignmentMask = kReadAlignment - 1;
7677 // Ensure that reading an entire aligned word containing the last character
7678 // of a string will not read outside the allocated area (because we pad up
7679 // to kObjectAlignment).
7680 ASSERT(kObjectAlignment >= kReadAlignment);
7681 // Assumes word reads and writes are little endian.
7682 // Nothing to do for zero characters.
7683 Label done;
7684 if (!ascii) {
7685 __ add(count, count, Operand(count), SetCC);
7686 } else {
7687 __ cmp(count, Operand(0));
7688 }
7689 __ b(eq, &done);
7690
7691 // Assume that you cannot read (or write) unaligned.
7692 Label byte_loop;
7693 // Must copy at least eight bytes, otherwise just do it one byte at a time.
7694 __ cmp(count, Operand(8));
7695 __ add(count, dest, Operand(count));
7696 Register limit = count; // Read until src equals this.
7697 __ b(lt, &byte_loop);
7698
7699 if (!dest_always_aligned) {
7700 // Align dest by byte copying. Copies between zero and three bytes.
7701 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
7702 Label dest_aligned;
7703 __ b(eq, &dest_aligned);
7704 __ cmp(scratch4, Operand(2));
7705 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
7706 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
7707 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
7708 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7709 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
7710 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
7711 __ bind(&dest_aligned);
7712 }
7713
7714 Label simple_loop;
7715
7716 __ sub(scratch4, dest, Operand(src));
7717 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
7718 __ b(eq, &simple_loop);
7719 // Shift register is number of bits in a source word that
7720 // must be combined with bits in the next source word in order
7721 // to create a destination word.
7722
7723 // Complex loop for src/dst that are not aligned the same way.
7724 {
7725 Label loop;
7726 __ mov(scratch4, Operand(scratch4, LSL, 3));
7727 Register left_shift = scratch4;
7728 __ and_(src, src, Operand(~3)); // Round down to load previous word.
7729 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7730 // Store the "shift" most significant bits of scratch in the least
7731 // signficant bits (i.e., shift down by (32-shift)).
7732 __ rsb(scratch2, left_shift, Operand(32));
7733 Register right_shift = scratch2;
7734 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
7735
7736 __ bind(&loop);
7737 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
7738 __ sub(scratch5, limit, Operand(dest));
7739 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
7740 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7741 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
7742 // Loop if four or more bytes left to copy.
7743 // Compare to eight, because we did the subtract before increasing dst.
7744 __ sub(scratch5, scratch5, Operand(8), SetCC);
7745 __ b(ge, &loop);
7746 }
7747 // There is now between zero and three bytes left to copy (negative that
7748 // number is in scratch5), and between one and three bytes already read into
7749 // scratch1 (eight times that number in scratch4). We may have read past
7750 // the end of the string, but because objects are aligned, we have not read
7751 // past the end of the object.
7752 // Find the minimum of remaining characters to move and preloaded characters
7753 // and write those as bytes.
7754 __ add(scratch5, scratch5, Operand(4), SetCC);
7755 __ b(eq, &done);
7756 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
7757 // Move minimum of bytes read and bytes left to copy to scratch4.
7758 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
7759 // Between one and three (value in scratch5) characters already read into
7760 // scratch ready to write.
7761 __ cmp(scratch5, Operand(2));
7762 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7763 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
7764 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
7765 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
7766 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
7767 // Copy any remaining bytes.
7768 __ b(&byte_loop);
7769
7770 // Simple loop.
7771 // Copy words from src to dst, until less than four bytes left.
7772 // Both src and dest are word aligned.
7773 __ bind(&simple_loop);
7774 {
7775 Label loop;
7776 __ bind(&loop);
7777 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7778 __ sub(scratch3, limit, Operand(dest));
7779 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7780 // Compare to 8, not 4, because we do the substraction before increasing
7781 // dest.
7782 __ cmp(scratch3, Operand(8));
7783 __ b(ge, &loop);
7784 }
7785
7786 // Copy bytes from src to dst until dst hits limit.
7787 __ bind(&byte_loop);
7788 __ cmp(dest, Operand(limit));
7789 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
7790 __ b(ge, &done);
7791 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7792 __ b(&byte_loop);
7793
7794 __ bind(&done);
7795}
7796
7797
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007798void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
7799 Register c1,
7800 Register c2,
7801 Register scratch1,
7802 Register scratch2,
7803 Register scratch3,
7804 Register scratch4,
7805 Register scratch5,
7806 Label* not_found) {
7807 // Register scratch3 is the general scratch register in this function.
7808 Register scratch = scratch3;
7809
7810 // Make sure that both characters are not digits as such strings has a
7811 // different hash algorithm. Don't try to look for these in the symbol table.
7812 Label not_array_index;
7813 __ sub(scratch, c1, Operand(static_cast<int>('0')));
7814 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
7815 __ b(hi, &not_array_index);
7816 __ sub(scratch, c2, Operand(static_cast<int>('0')));
7817 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
7818
7819 // If check failed combine both characters into single halfword.
7820 // This is required by the contract of the method: code at the
7821 // not_found branch expects this combination in c1 register
7822 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
7823 __ b(ls, not_found);
7824
7825 __ bind(&not_array_index);
7826 // Calculate the two character string hash.
7827 Register hash = scratch1;
7828 GenerateHashInit(masm, hash, c1);
7829 GenerateHashAddCharacter(masm, hash, c2);
7830 GenerateHashGetHash(masm, hash);
7831
7832 // Collect the two characters in a register.
7833 Register chars = c1;
7834 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
7835
7836 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
7837 // hash: hash of two character string.
7838
7839 // Load symbol table
7840 // Load address of first element of the symbol table.
7841 Register symbol_table = c2;
7842 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
7843
7844 // Load undefined value
7845 Register undefined = scratch4;
7846 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
7847
7848 // Calculate capacity mask from the symbol table capacity.
7849 Register mask = scratch2;
7850 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
7851 __ mov(mask, Operand(mask, ASR, 1));
7852 __ sub(mask, mask, Operand(1));
7853
7854 // Calculate untagged address of the first element of the symbol table.
7855 Register first_symbol_table_element = symbol_table;
7856 __ add(first_symbol_table_element, symbol_table,
7857 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
7858
7859 // Registers
7860 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
7861 // hash: hash of two character string
7862 // mask: capacity mask
7863 // first_symbol_table_element: address of the first element of
7864 // the symbol table
7865 // scratch: -
7866
7867 // Perform a number of probes in the symbol table.
7868 static const int kProbes = 4;
7869 Label found_in_symbol_table;
7870 Label next_probe[kProbes];
7871 for (int i = 0; i < kProbes; i++) {
7872 Register candidate = scratch5; // Scratch register contains candidate.
7873
7874 // Calculate entry in symbol table.
7875 if (i > 0) {
7876 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
7877 } else {
7878 __ mov(candidate, hash);
7879 }
7880
7881 __ and_(candidate, candidate, Operand(mask));
7882
7883 // Load the entry from the symble table.
7884 ASSERT_EQ(1, SymbolTable::kEntrySize);
7885 __ ldr(candidate,
7886 MemOperand(first_symbol_table_element,
7887 candidate,
7888 LSL,
7889 kPointerSizeLog2));
7890
7891 // If entry is undefined no string with this hash can be found.
7892 __ cmp(candidate, undefined);
7893 __ b(eq, not_found);
7894
7895 // If length is not 2 the string is not a candidate.
7896 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
7897 __ cmp(scratch, Operand(2));
7898 __ b(ne, &next_probe[i]);
7899
7900 // Check that the candidate is a non-external ascii string.
7901 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
7902 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
7903 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
7904 &next_probe[i]);
7905
7906 // Check if the two characters match.
7907 // Assumes that word load is little endian.
7908 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
7909 __ cmp(chars, scratch);
7910 __ b(eq, &found_in_symbol_table);
7911 __ bind(&next_probe[i]);
7912 }
7913
7914 // No matching 2 character string found by probing.
7915 __ jmp(not_found);
7916
7917 // Scratch register contains result when we fall through to here.
7918 Register result = scratch;
7919 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00007920 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007921}
7922
7923
7924void StringStubBase::GenerateHashInit(MacroAssembler* masm,
7925 Register hash,
7926 Register character) {
7927 // hash = character + (character << 10);
7928 __ add(hash, character, Operand(character, LSL, 10));
7929 // hash ^= hash >> 6;
7930 __ eor(hash, hash, Operand(hash, ASR, 6));
7931}
7932
7933
7934void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm,
7935 Register hash,
7936 Register character) {
7937 // hash += character;
7938 __ add(hash, hash, Operand(character));
7939 // hash += hash << 10;
7940 __ add(hash, hash, Operand(hash, LSL, 10));
7941 // hash ^= hash >> 6;
7942 __ eor(hash, hash, Operand(hash, ASR, 6));
7943}
7944
7945
7946void StringStubBase::GenerateHashGetHash(MacroAssembler* masm,
7947 Register hash) {
7948 // hash += hash << 3;
7949 __ add(hash, hash, Operand(hash, LSL, 3));
7950 // hash ^= hash >> 11;
7951 __ eor(hash, hash, Operand(hash, ASR, 11));
7952 // hash += hash << 15;
7953 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
7954
7955 // if (hash == 0) hash = 27;
7956 __ mov(hash, Operand(27), LeaveCC, nz);
7957}
7958
7959
ager@chromium.org5c838252010-02-19 08:53:10 +00007960void SubStringStub::Generate(MacroAssembler* masm) {
7961 Label runtime;
7962
7963 // Stack frame on entry.
7964 // lr: return address
7965 // sp[0]: to
7966 // sp[4]: from
7967 // sp[8]: string
7968
7969 // This stub is called from the native-call %_SubString(...), so
7970 // nothing can be assumed about the arguments. It is tested that:
7971 // "string" is a sequential string,
7972 // both "from" and "to" are smis, and
7973 // 0 <= from <= to <= string.length.
7974 // If any of these assumptions fail, we call the runtime system.
7975
7976 static const int kToOffset = 0 * kPointerSize;
7977 static const int kFromOffset = 1 * kPointerSize;
7978 static const int kStringOffset = 2 * kPointerSize;
7979
7980
7981 // Check bounds and smi-ness.
7982 __ ldr(r7, MemOperand(sp, kToOffset));
7983 __ ldr(r6, MemOperand(sp, kFromOffset));
7984 ASSERT_EQ(0, kSmiTag);
7985 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7986 // I.e., arithmetic shift right by one un-smi-tags.
7987 __ mov(r2, Operand(r7, ASR, 1), SetCC);
7988 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
7989 // If either r2 or r6 had the smi tag bit set, then carry is set now.
7990 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
7991 __ b(mi, &runtime); // From is negative.
7992
7993 __ sub(r2, r2, Operand(r3), SetCC);
7994 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007995 // Special handling of sub-strings of length 1 and 2. One character strings
7996 // are handled in the runtime system (looked up in the single character
7997 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00007998 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007999 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00008000
8001 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008002 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008003 // r6: from (smi)
8004 // r7: to (smi)
8005
8006 // Make sure first argument is a sequential (or flat) string.
8007 __ ldr(r5, MemOperand(sp, kStringOffset));
8008 ASSERT_EQ(0, kSmiTag);
8009 __ tst(r5, Operand(kSmiTagMask));
8010 __ b(eq, &runtime);
8011 Condition is_string = masm->IsObjectStringType(r5, r1);
8012 __ b(NegateCondition(is_string), &runtime);
8013
8014 // r1: instance type
8015 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008016 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008017 // r5: string
8018 // r6: from (smi)
8019 // r7: to (smi)
8020 Label seq_string;
8021 __ and_(r4, r1, Operand(kStringRepresentationMask));
8022 ASSERT(kSeqStringTag < kConsStringTag);
8023 ASSERT(kExternalStringTag > kConsStringTag);
8024 __ cmp(r4, Operand(kConsStringTag));
8025 __ b(gt, &runtime); // External strings go to runtime.
8026 __ b(lt, &seq_string); // Sequential strings are handled directly.
8027
8028 // Cons string. Try to recurse (once) on the first substring.
8029 // (This adds a little more generality than necessary to handle flattened
8030 // cons strings, but not much).
8031 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
8032 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
8033 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8034 __ tst(r1, Operand(kStringRepresentationMask));
8035 ASSERT_EQ(0, kSeqStringTag);
8036 __ b(ne, &runtime); // Cons and External strings go to runtime.
8037
8038 // Definitly a sequential string.
8039 __ bind(&seq_string);
8040
8041 // r1: instance type.
8042 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008043 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008044 // r5: string
8045 // r6: from (smi)
8046 // r7: to (smi)
8047 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
8048 __ cmp(r4, Operand(r7, ASR, 1));
8049 __ b(lt, &runtime); // Fail if to > length.
8050
8051 // r1: instance type.
8052 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008053 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008054 // r5: string.
8055 // r6: from offset (smi)
8056 // Check for flat ascii string.
8057 Label non_ascii_flat;
8058 __ tst(r1, Operand(kStringEncodingMask));
8059 ASSERT_EQ(0, kTwoByteStringTag);
8060 __ b(eq, &non_ascii_flat);
8061
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008062 Label result_longer_than_two;
8063 __ cmp(r2, Operand(2));
8064 __ b(gt, &result_longer_than_two);
8065
8066 // Sub string of length 2 requested.
8067 // Get the two characters forming the sub string.
8068 __ add(r5, r5, Operand(r3));
8069 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
8070 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
8071
8072 // Try to lookup two character string in symbol table.
8073 Label make_two_character_string;
8074 GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9,
8075 &make_two_character_string);
8076 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8077 __ add(sp, sp, Operand(3 * kPointerSize));
8078 __ Ret();
8079
8080 // r2: result string length.
8081 // r3: two characters combined into halfword in little endian byte order.
8082 __ bind(&make_two_character_string);
8083 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
8084 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8085 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8086 __ add(sp, sp, Operand(3 * kPointerSize));
8087 __ Ret();
8088
8089 __ bind(&result_longer_than_two);
8090
ager@chromium.org5c838252010-02-19 08:53:10 +00008091 // Allocate the result.
8092 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
8093
8094 // r0: result string.
8095 // r2: result string length.
8096 // r5: string.
8097 // r6: from offset (smi)
8098 // Locate first character of result.
8099 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8100 // Locate 'from' character of string.
8101 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8102 __ add(r5, r5, Operand(r6, ASR, 1));
8103
8104 // r0: result string.
8105 // r1: first character of result string.
8106 // r2: result string length.
8107 // r5: first character of sub string to copy.
8108 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
8109 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
8110 COPY_ASCII | DEST_ALWAYS_ALIGNED);
8111 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8112 __ add(sp, sp, Operand(3 * kPointerSize));
8113 __ Ret();
8114
8115 __ bind(&non_ascii_flat);
8116 // r2: result string length.
8117 // r5: string.
8118 // r6: from offset (smi)
8119 // Check for flat two byte string.
8120
8121 // Allocate the result.
8122 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
8123
8124 // r0: result string.
8125 // r2: result string length.
8126 // r5: string.
8127 // Locate first character of result.
8128 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8129 // Locate 'from' character of string.
8130 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8131 // As "from" is a smi it is 2 times the value which matches the size of a two
8132 // byte character.
8133 __ add(r5, r5, Operand(r6));
8134
8135 // r0: result string.
8136 // r1: first character of result.
8137 // r2: result length.
8138 // r5: first character of string to copy.
8139 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
8140 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
8141 DEST_ALWAYS_ALIGNED);
8142 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8143 __ add(sp, sp, Operand(3 * kPointerSize));
8144 __ Ret();
8145
8146 // Just jump to runtime to create the sub string.
8147 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008148 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00008149}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008150
8151
8152void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
8153 Register left,
8154 Register right,
8155 Register scratch1,
8156 Register scratch2,
8157 Register scratch3,
8158 Register scratch4) {
8159 Label compare_lengths;
8160 // Find minimum length and length difference.
8161 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
8162 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
8163 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
8164 Register length_delta = scratch3;
8165 __ mov(scratch1, scratch2, LeaveCC, gt);
8166 Register min_length = scratch1;
8167 __ tst(min_length, Operand(min_length));
8168 __ b(eq, &compare_lengths);
8169
8170 // Setup registers so that we only need to increment one register
8171 // in the loop.
8172 __ add(scratch2, min_length,
8173 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8174 __ add(left, left, Operand(scratch2));
8175 __ add(right, right, Operand(scratch2));
8176 // Registers left and right points to the min_length character of strings.
8177 __ rsb(min_length, min_length, Operand(-1));
8178 Register index = min_length;
8179 // Index starts at -min_length.
8180
8181 {
8182 // Compare loop.
8183 Label loop;
8184 __ bind(&loop);
8185 // Compare characters.
8186 __ add(index, index, Operand(1), SetCC);
8187 __ ldrb(scratch2, MemOperand(left, index), ne);
8188 __ ldrb(scratch4, MemOperand(right, index), ne);
8189 // Skip to compare lengths with eq condition true.
8190 __ b(eq, &compare_lengths);
8191 __ cmp(scratch2, scratch4);
8192 __ b(eq, &loop);
8193 // Fallthrough with eq condition false.
8194 }
8195 // Compare lengths - strings up to min-length are equal.
8196 __ bind(&compare_lengths);
8197 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
8198 // Use zero length_delta as result.
8199 __ mov(r0, Operand(length_delta), SetCC, eq);
8200 // Fall through to here if characters compare not-equal.
8201 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
8202 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
8203 __ Ret();
8204}
8205
8206
8207void StringCompareStub::Generate(MacroAssembler* masm) {
8208 Label runtime;
8209
8210 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00008211 // sp[0]: right string
8212 // sp[4]: left string
8213 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
8214 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008215
8216 Label not_same;
8217 __ cmp(r0, r1);
8218 __ b(ne, &not_same);
8219 ASSERT_EQ(0, EQUAL);
8220 ASSERT_EQ(0, kSmiTag);
8221 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
8222 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
8223 __ add(sp, sp, Operand(2 * kPointerSize));
8224 __ Ret();
8225
8226 __ bind(&not_same);
8227
8228 // Check that both objects are sequential ascii strings.
8229 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
8230
8231 // Compare flat ascii strings natively. Remove arguments from stack first.
8232 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
8233 __ add(sp, sp, Operand(2 * kPointerSize));
8234 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
8235
8236 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
8237 // tagged as a small integer.
8238 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008239 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008240}
8241
8242
ager@chromium.org5c838252010-02-19 08:53:10 +00008243void StringAddStub::Generate(MacroAssembler* masm) {
8244 Label string_add_runtime;
8245 // Stack on entry:
8246 // sp[0]: second argument.
8247 // sp[4]: first argument.
8248
8249 // Load the two arguments.
8250 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
8251 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
8252
8253 // Make sure that both arguments are strings if not known in advance.
8254 if (string_check_) {
8255 ASSERT_EQ(0, kSmiTag);
8256 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
8257 // Load instance types.
8258 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8259 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8260 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8261 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8262 ASSERT_EQ(0, kStringTag);
8263 // If either is not a string, go to runtime.
8264 __ tst(r4, Operand(kIsNotStringMask));
8265 __ tst(r5, Operand(kIsNotStringMask), eq);
8266 __ b(ne, &string_add_runtime);
8267 }
8268
8269 // Both arguments are strings.
8270 // r0: first string
8271 // r1: second string
8272 // r4: first string instance type (if string_check_)
8273 // r5: second string instance type (if string_check_)
8274 {
8275 Label strings_not_empty;
8276 // Check if either of the strings are empty. In that case return the other.
8277 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
8278 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
8279 __ cmp(r2, Operand(0)); // Test if first string is empty.
8280 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
8281 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
8282 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
8283
8284 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8285 __ add(sp, sp, Operand(2 * kPointerSize));
8286 __ Ret();
8287
8288 __ bind(&strings_not_empty);
8289 }
8290
8291 // Both strings are non-empty.
8292 // r0: first string
8293 // r1: second string
8294 // r2: length of first string
8295 // r3: length of second string
8296 // r4: first string instance type (if string_check_)
8297 // r5: second string instance type (if string_check_)
8298 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008299 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00008300 // Adding two lengths can't overflow.
8301 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
8302 __ add(r6, r2, Operand(r3));
8303 // Use the runtime system when adding two one character strings, as it
8304 // contains optimizations for this specific case using the symbol table.
8305 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008306 __ b(ne, &longer_than_two);
8307
8308 // Check that both strings are non-external ascii strings.
8309 if (!string_check_) {
8310 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8311 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8312 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8313 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8314 }
8315 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
8316 &string_add_runtime);
8317
8318 // Get the two characters forming the sub string.
8319 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8320 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
8321
8322 // Try to lookup two character string in symbol table. If it is not found
8323 // just allocate a new one.
8324 Label make_two_character_string;
8325 GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9,
8326 &make_two_character_string);
8327 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8328 __ add(sp, sp, Operand(2 * kPointerSize));
8329 __ Ret();
8330
8331 __ bind(&make_two_character_string);
8332 // Resulting string has length 2 and first chars of two strings
8333 // are combined into single halfword in r2 register.
8334 // So we can fill resulting string without two loops by a single
8335 // halfword store instruction (which assumes that processor is
8336 // in a little endian mode)
8337 __ mov(r6, Operand(2));
8338 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
8339 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8340 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8341 __ add(sp, sp, Operand(2 * kPointerSize));
8342 __ Ret();
8343
8344 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00008345 // Check if resulting string will be flat.
8346 __ cmp(r6, Operand(String::kMinNonFlatLength));
8347 __ b(lt, &string_add_flat_result);
8348 // Handle exceptionally long strings in the runtime system.
8349 ASSERT((String::kMaxLength & 0x80000000) == 0);
8350 ASSERT(IsPowerOf2(String::kMaxLength + 1));
8351 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
8352 __ cmp(r6, Operand(String::kMaxLength + 1));
8353 __ b(hs, &string_add_runtime);
8354
8355 // If result is not supposed to be flat, allocate a cons string object.
8356 // If both strings are ascii the result is an ascii cons string.
8357 if (!string_check_) {
8358 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8359 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8360 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8361 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8362 }
8363 Label non_ascii, allocated;
8364 ASSERT_EQ(0, kTwoByteStringTag);
8365 __ tst(r4, Operand(kStringEncodingMask));
8366 __ tst(r5, Operand(kStringEncodingMask), ne);
8367 __ b(eq, &non_ascii);
8368
8369 // Allocate an ASCII cons string.
8370 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
8371 __ bind(&allocated);
8372 // Fill the fields of the cons string.
8373 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
8374 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
8375 __ mov(r0, Operand(r7));
8376 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8377 __ add(sp, sp, Operand(2 * kPointerSize));
8378 __ Ret();
8379
8380 __ bind(&non_ascii);
8381 // Allocate a two byte cons string.
8382 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
8383 __ jmp(&allocated);
8384
8385 // Handle creating a flat result. First check that both strings are
8386 // sequential and that they have the same encoding.
8387 // r0: first string
8388 // r1: second string
8389 // r2: length of first string
8390 // r3: length of second string
8391 // r4: first string instance type (if string_check_)
8392 // r5: second string instance type (if string_check_)
8393 // r6: sum of lengths.
8394 __ bind(&string_add_flat_result);
8395 if (!string_check_) {
8396 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8397 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8398 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8399 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8400 }
8401 // Check that both strings are sequential.
8402 ASSERT_EQ(0, kSeqStringTag);
8403 __ tst(r4, Operand(kStringRepresentationMask));
8404 __ tst(r5, Operand(kStringRepresentationMask), eq);
8405 __ b(ne, &string_add_runtime);
8406 // Now check if both strings have the same encoding (ASCII/Two-byte).
8407 // r0: first string.
8408 // r1: second string.
8409 // r2: length of first string.
8410 // r3: length of second string.
8411 // r6: sum of lengths..
8412 Label non_ascii_string_add_flat_result;
8413 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
8414 __ eor(r7, r4, Operand(r5));
8415 __ tst(r7, Operand(kStringEncodingMask));
8416 __ b(ne, &string_add_runtime);
8417 // And see if it's ASCII or two-byte.
8418 __ tst(r4, Operand(kStringEncodingMask));
8419 __ b(eq, &non_ascii_string_add_flat_result);
8420
8421 // Both strings are sequential ASCII strings. We also know that they are
8422 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008423 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00008424 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
8425 // Locate first character of result.
8426 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8427 // Locate first character of first argument.
8428 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8429 // r0: first character of first string.
8430 // r1: second string.
8431 // r2: length of first string.
8432 // r3: length of second string.
8433 // r6: first character of result.
8434 // r7: result string.
8435 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
8436
8437 // Load second argument and locate first character.
8438 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8439 // r1: first character of second string.
8440 // r3: length of second string.
8441 // r6: next character of result.
8442 // r7: result string.
8443 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
8444 __ mov(r0, Operand(r7));
8445 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8446 __ add(sp, sp, Operand(2 * kPointerSize));
8447 __ Ret();
8448
8449 __ bind(&non_ascii_string_add_flat_result);
8450 // Both strings are sequential two byte strings.
8451 // r0: first string.
8452 // r1: second string.
8453 // r2: length of first string.
8454 // r3: length of second string.
8455 // r6: sum of length of strings.
8456 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
8457 // r0: first string.
8458 // r1: second string.
8459 // r2: length of first string.
8460 // r3: length of second string.
8461 // r7: result string.
8462
8463 // Locate first character of result.
8464 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8465 // Locate first character of first argument.
8466 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8467
8468 // r0: first character of first string.
8469 // r1: second string.
8470 // r2: length of first string.
8471 // r3: length of second string.
8472 // r6: first character of result.
8473 // r7: result string.
8474 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
8475
8476 // Locate first character of second argument.
8477 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8478
8479 // r1: first character of second string.
8480 // r3: length of second string.
8481 // r6: next character of result (after copy of first string).
8482 // r7: result string.
8483 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
8484
8485 __ mov(r0, Operand(r7));
8486 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8487 __ add(sp, sp, Operand(2 * kPointerSize));
8488 __ Ret();
8489
8490 // Just jump to runtime to add the two strings.
8491 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008492 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00008493}
8494
8495
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008496#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00008497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00008498} } // namespace v8::internal