blob: 0ca4d3560e02a91742da0777addc032b201a33e9 [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
184 VirtualFrame::SpilledScope spilled_scope;
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);
277 allocator_->Unuse(r1);
278 allocator_->Unuse(lr);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000279
280 // Bind all the bailout labels to the beginning of the function.
281 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
282 for (int i = 0; i < bailouts->length(); i++) {
283 __ bind(bailouts->at(i)->label());
284 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000285 }
286
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287 // Initialize the function return target after the locals are set
288 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000289 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000290 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000292 // Generate code to 'execute' declarations and initialize functions
293 // (source elements). In case of an illegal redeclaration we need to
294 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000297 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 } else {
299 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000300 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000301 // Bail out if a stack-overflow exception occurred when processing
302 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000303 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 }
305
mads.s.ager31e71382008-08-13 09:32:07 +0000306 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000308 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310
311 // Compile the body of the function in a vanilla state. Don't
312 // bother compiling all the code if the scope has an illegal
313 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000314 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 Comment cmnt(masm_, "[ function body");
316#ifdef DEBUG
317 bool is_builtin = Bootstrapper::IsActive();
318 bool should_trace =
319 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000320 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000322 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000325 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 }
328
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000330 if (has_valid_frame() || function_return_.is_linked()) {
331 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000332 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000334 // exit
335 // r0: result
336 // sp: stack pointer
337 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000338 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000339 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000340
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 function_return_.Bind();
342 if (FLAG_trace) {
343 // Push the return value on the stack as the parameter.
344 // Runtime::TraceExit returns the parameter as it is.
345 frame_->EmitPush(r0);
346 frame_->CallRuntime(Runtime::kTraceExit, 1);
347 }
348
ager@chromium.org4af710e2009-09-15 12:20:11 +0000349 // Add a label for checking the size of the code used for returning.
350 Label check_exit_codesize;
351 masm_->bind(&check_exit_codesize);
352
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000353 // Calculate the exact length of the return sequence and make sure that
354 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000355 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000356 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000357 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
358 // Additional mov instruction generated.
359 return_sequence_length++;
360 }
361 masm_->BlockConstPoolFor(return_sequence_length);
362
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000363 // Tear down the frame which will restore the caller's frame pointer and
364 // the link register.
365 frame_->Exit();
366
ager@chromium.org4af710e2009-09-15 12:20:11 +0000367 // Here we use masm_-> instead of the __ macro to avoid the code coverage
368 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000369 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000370 masm_->Jump(lr);
371
372 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000373 // expected by the debugger. The add instruction above is an addressing
374 // mode 1 instruction where there are restrictions on which immediate values
375 // can be encoded in the instruction and which immediate values requires
376 // use of an additional instruction for moving the immediate to a temporary
377 // register.
378 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000379 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000380 }
381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 ASSERT(!has_cc());
384 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000385 ASSERT(!function_return_is_shadowed_);
386 function_return_.Unuse();
387 DeleteFrame();
388
389 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000390 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000391 ProcessDeferred();
392 }
393
394 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395}
396
397
ager@chromium.org7c537e22008-10-16 08:43:32 +0000398MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
399 // Currently, this assertion will fail if we try to assign to
400 // a constant variable that is constant because it is read-only
401 // (such as the variable referring to a named function expression).
402 // We need to implement assignments to read-only variables.
403 // Ideally, we should do this during AST generation (by converting
404 // such assignments into expression statements); however, in general
405 // we may not be able to make the decision until past AST generation,
406 // that is when the entire program is known.
407 ASSERT(slot != NULL);
408 int index = slot->index();
409 switch (slot->type()) {
410 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000411 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000412
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000413 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000414 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000415
416 case Slot::CONTEXT: {
417 // Follow the context chain if necessary.
418 ASSERT(!tmp.is(cp)); // do not overwrite context register
419 Register context = cp;
420 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000421 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000422 // Load the closure.
423 // (All contexts, even 'with' contexts, have a closure,
424 // and it is the same for all contexts inside a function.
425 // There is no need to go to the function context first.)
426 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
427 // Load the function context (which is the incoming, outer context).
428 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
429 context = tmp;
430 }
431 // We may have a 'with' context now. Get the function context.
432 // (In fact this mov may never be the needed, since the scope analysis
433 // may not permit a direct context access in this case and thus we are
434 // always at a function context. However it is safe to dereference be-
435 // cause the function context of a function context is itself. Before
436 // deleting this mov we should try to create a counter-example first,
437 // though...)
438 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
439 return ContextOperand(tmp, index);
440 }
441
442 default:
443 UNREACHABLE();
444 return MemOperand(r0, 0);
445 }
446}
447
448
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000449MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
450 Slot* slot,
451 Register tmp,
452 Register tmp2,
453 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000454 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000455 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000456
ager@chromium.org381abbb2009-02-25 13:23:22 +0000457 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
458 if (s->num_heap_slots() > 0) {
459 if (s->calls_eval()) {
460 // Check that extension is NULL.
461 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
462 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000463 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000464 }
465 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
466 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
467 context = tmp;
468 }
469 }
470 // Check that last extension is NULL.
471 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
472 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000474 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000476}
477
478
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000479// Loads a value on TOS. If it is a boolean value, the result may have been
480// (partially) translated into branches, or it may have set the condition
481// code register. If force_cc is set, the value is forced to set the
482// condition code register and no value is pushed. If the condition code
483// register was set, has_cc() is true and cc_reg_ contains the condition to
484// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000485void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000486 JumpTarget* true_target,
487 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000488 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000489 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000492 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000493 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494
495 // If we hit a stack overflow, we may not have actually visited
496 // the expression. In that case, we ensure that we have a
497 // valid-looking frame state because we will continue to generate
498 // code as we unwind the C++ stack.
499 //
500 // It's possible to have both a stack overflow and a valid frame
501 // state (eg, a subexpression overflowed, visiting it returned
502 // with a dummied frame state, and visiting this expression
503 // returned with a normal-looking state).
504 if (HasStackOverflow() &&
505 has_valid_frame() &&
506 !has_cc() &&
507 frame_->height() == original_height) {
508 true_target->Jump();
509 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000510 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000511 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000512 // Convert the TOS value to a boolean in the condition code register.
513 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 ASSERT(!force_cc || !has_valid_frame() || has_cc());
516 ASSERT(!has_valid_frame() ||
517 (has_cc() && frame_->height() == original_height) ||
518 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519}
520
521
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000522void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523#ifdef DEBUG
524 int original_height = frame_->height();
525#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000526 JumpTarget true_target;
527 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000528 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529
530 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000531 // Convert cc_reg_ into a boolean value.
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()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 // We have at least one condition value that has been "translated"
547 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000548 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000549 if (frame_ != NULL) {
550 loaded.Jump(); // Don't lose the current TOS.
551 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000555 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000556 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000557 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000559 // If both "true" and "false" need to be loaded jump across the code for
560 // "false".
561 if (both) {
562 loaded.Jump();
563 }
564 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000566 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000567 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 // A value is loaded on all paths reaching this point.
571 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000573 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000575 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.org7c537e22008-10-16 08:43:32 +0000579void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000580 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000581 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000582 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583}
584
585
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000586void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000587 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000588 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
589 __ ldr(scratch,
590 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000591 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000592}
593
594
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000595void CodeGenerator::LoadTypeofExpression(Expression* expr) {
596 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000597 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000598 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000600 // For a global variable we build the property reference
601 // <global>.<variable> and perform a (regular non-contextual) property
602 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
604 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000605 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000606 Reference ref(this, &property);
607 ref.GetValueAndSpill();
608 } else if (variable != NULL && variable->slot() != NULL) {
609 // For a variable that rewrites to a slot, we signal it is the immediate
610 // subexpression of a typeof.
611 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
612 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000614 // Anything else can be handled normally.
615 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 }
617}
618
619
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000620Reference::Reference(CodeGenerator* cgen,
621 Expression* expression,
622 bool persist_after_get)
623 : cgen_(cgen),
624 expression_(expression),
625 type_(ILLEGAL),
626 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 cgen->LoadReference(this);
628}
629
630
631Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000632 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633}
634
635
ager@chromium.org7c537e22008-10-16 08:43:32 +0000636void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000637 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000638 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 Expression* e = ref->expression();
640 Property* property = e->AsProperty();
641 Variable* var = e->AsVariableProxy()->AsVariable();
642
643 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000644 // The expression is either a property or a variable proxy that rewrites
645 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000646 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000647 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 ref->set_type(Reference::NAMED);
649 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000650 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 ref->set_type(Reference::KEYED);
652 }
653 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000654 // The expression is a variable proxy that does not rewrite to a
655 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 LoadGlobal();
658 ref->set_type(Reference::NAMED);
659 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000660 ASSERT(var->slot() != NULL);
661 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 }
663 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000664 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000665 LoadAndSpill(e);
666 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 }
668}
669
670
ager@chromium.org7c537e22008-10-16 08:43:32 +0000671void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000672 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000673 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000674 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000676 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000677 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000678 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000679 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000681 ref->set_unloaded();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682}
683
684
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
686// register to a boolean in the condition code register. The code
687// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000688void CodeGenerator::ToBoolean(JumpTarget* true_target,
689 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000690 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000691 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000693 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
695 // Fast case checks
696
mads.s.ager31e71382008-08-13 09:32:07 +0000697 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000698 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
699 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000700 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
mads.s.ager31e71382008-08-13 09:32:07 +0000702 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000703 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
704 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000705 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
mads.s.ager31e71382008-08-13 09:32:07 +0000707 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000708 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
709 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000710 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
mads.s.ager31e71382008-08-13 09:32:07 +0000712 // Check if the value is a smi.
713 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000714 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000715 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000716 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717
718 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 frame_->EmitPush(r0);
720 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000721 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000722 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
723 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724
725 cc_reg_ = ne;
726}
727
728
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000729void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000730 OverwriteMode overwrite_mode,
731 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000732 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000733 // sp[0] : y
734 // sp[1] : x
735 // result : r0
736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 // Stub is entered with a call: 'return address' is in lr.
738 switch (op) {
739 case Token::ADD: // fall through.
740 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000741 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000742 case Token::DIV:
743 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000744 case Token::BIT_OR:
745 case Token::BIT_AND:
746 case Token::BIT_XOR:
747 case Token::SHL:
748 case Token::SHR:
749 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000750 frame_->EmitPop(r0); // r0 : y
751 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000752 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 break;
755 }
756
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000758 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 break;
762
763 default:
764 // Other cases should have been handled before this point.
765 UNREACHABLE();
766 break;
767 }
768}
769
770
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000771class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000772 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000773 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000774 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000775 bool reversed,
776 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000777 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000778 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000779 reversed_(reversed),
780 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000781 set_comment("[ DeferredInlinedSmiOperation");
782 }
783
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000784 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000785
786 private:
787 Token::Value op_;
788 int value_;
789 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000790 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000791};
792
793
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000794void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000795 switch (op_) {
796 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000797 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000798 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000799 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
800 __ mov(r1, Operand(Smi::FromInt(value_)));
801 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000802 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
803 __ mov(r0, Operand(Smi::FromInt(value_)));
804 }
805 break;
806 }
807
808 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000809 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000810 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000811 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
812 __ mov(r1, Operand(Smi::FromInt(value_)));
813 } else {
814 __ add(r1, r0, Operand(Smi::FromInt(value_)));
815 __ mov(r0, Operand(Smi::FromInt(value_)));
816 }
817 break;
818 }
819
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000820 // For these operations there is no optimistic operation that needs to be
821 // reverted.
822 case Token::MUL:
823 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000824 case Token::BIT_OR:
825 case Token::BIT_XOR:
826 case Token::BIT_AND: {
827 if (reversed_) {
828 __ mov(r1, Operand(Smi::FromInt(value_)));
829 } else {
830 __ mov(r1, Operand(r0));
831 __ mov(r0, Operand(Smi::FromInt(value_)));
832 }
833 break;
834 }
835
836 case Token::SHL:
837 case Token::SHR:
838 case Token::SAR: {
839 if (!reversed_) {
840 __ mov(r1, Operand(r0));
841 __ mov(r0, Operand(Smi::FromInt(value_)));
842 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000843 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000844 }
845 break;
846 }
847
848 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000849 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850 UNREACHABLE();
851 break;
852 }
853
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000854 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000855 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000856}
857
858
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000859static bool PopCountLessThanEqual2(unsigned int x) {
860 x &= x - 1;
861 return (x & (x - 1)) == 0;
862}
863
864
865// Returns the index of the lowest bit set.
866static int BitPosition(unsigned x) {
867 int bit_posn = 0;
868 while ((x & 0xf) == 0) {
869 bit_posn += 4;
870 x >>= 4;
871 }
872 while ((x & 1) == 0) {
873 bit_posn++;
874 x >>= 1;
875 }
876 return bit_posn;
877}
878
879
ager@chromium.org7c537e22008-10-16 08:43:32 +0000880void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000881 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000882 bool reversed,
883 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000884 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 // NOTE: This is an attempt to inline (a bit) more of the code for
886 // some possible smi operations (like + and -) when (at least) one
887 // of the operands is a literal smi. With this optimization, the
888 // performance of the system is increased by ~15%, and the generated
889 // code size is increased by ~1% (measured on a combination of
890 // different benchmarks).
891
mads.s.ager31e71382008-08-13 09:32:07 +0000892 // sp[0] : operand
893
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000894 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000896 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000897 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000899 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 switch (op) {
901 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000902 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000903 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000906 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000908 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000909 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 break;
911 }
912
913 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000914 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000915 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916
ager@chromium.orge2902be2009-06-08 12:21:35 +0000917 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000918 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000919 } else {
920 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000922 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000923 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000924 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000925 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000926 break;
927 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000929
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 case Token::BIT_OR:
931 case Token::BIT_XOR:
932 case Token::BIT_AND: {
933 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000934 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000936 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000937 switch (op) {
938 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
939 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
940 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
941 default: UNREACHABLE();
942 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000943 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000944 break;
945 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000947 case Token::SHL:
948 case Token::SHR:
949 case Token::SAR: {
950 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000951 something_to_inline = false;
952 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000953 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000954 int shift_value = int_value & 0x1f; // least significant 5 bits
955 DeferredCode* deferred =
956 new DeferredInlineSmiOperation(op, shift_value, false, mode);
957 __ tst(r0, Operand(kSmiTagMask));
958 deferred->Branch(ne);
959 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
960 switch (op) {
961 case Token::SHL: {
962 if (shift_value != 0) {
963 __ mov(r2, Operand(r2, LSL, shift_value));
964 }
965 // check that the *unsigned* result fits in a smi
966 __ add(r3, r2, Operand(0x40000000), SetCC);
967 deferred->Branch(mi);
968 break;
969 }
970 case Token::SHR: {
971 // LSR by immediate 0 means shifting 32 bits.
972 if (shift_value != 0) {
973 __ mov(r2, Operand(r2, LSR, shift_value));
974 }
975 // check that the *unsigned* result fits in a smi
976 // neither of the two high-order bits can be set:
977 // - 0x80000000: high bit would be lost when smi tagging
978 // - 0x40000000: this number would convert to negative when
979 // smi tagging these two cases can only happen with shifts
980 // by 0 or 1 when handed a valid smi
981 __ and_(r3, r2, Operand(0xc0000000), SetCC);
982 deferred->Branch(ne);
983 break;
984 }
985 case Token::SAR: {
986 if (shift_value != 0) {
987 // ASR by immediate 0 means shifting 32 bits.
988 __ mov(r2, Operand(r2, ASR, shift_value));
989 }
990 break;
991 }
992 default: UNREACHABLE();
993 }
994 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
995 deferred->BindExit();
996 break;
997 }
998
999 case Token::MOD: {
1000 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1001 something_to_inline = false;
1002 break;
1003 }
1004 DeferredCode* deferred =
1005 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1006 unsigned mask = (0x80000000u | kSmiTagMask);
1007 __ tst(r0, Operand(mask));
1008 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1009 mask = (int_value << kSmiTagSize) - 1;
1010 __ and_(r0, r0, Operand(mask));
1011 deferred->BindExit();
1012 break;
1013 }
1014
1015 case Token::MUL: {
1016 if (!IsEasyToMultiplyBy(int_value)) {
1017 something_to_inline = false;
1018 break;
1019 }
1020 DeferredCode* deferred =
1021 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1022 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1023 max_smi_that_wont_overflow <<= kSmiTagSize;
1024 unsigned mask = 0x80000000u;
1025 while ((mask & max_smi_that_wont_overflow) == 0) {
1026 mask |= mask >> 1;
1027 }
1028 mask |= kSmiTagMask;
1029 // This does a single mask that checks for a too high value in a
1030 // conservative way and for a non-Smi. It also filters out negative
1031 // numbers, unfortunately, but since this code is inline we prefer
1032 // brevity to comprehensiveness.
1033 __ tst(r0, Operand(mask));
1034 deferred->Branch(ne);
1035 MultiplyByKnownInt(masm_, r0, r0, int_value);
1036 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 break;
1038 }
1039
1040 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001041 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 break;
1043 }
1044
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001045 if (!something_to_inline) {
1046 if (!reversed) {
1047 frame_->EmitPush(r0);
1048 __ mov(r0, Operand(value));
1049 frame_->EmitPush(r0);
1050 GenericBinaryOperation(op, mode, int_value);
1051 } else {
1052 __ mov(ip, Operand(value));
1053 frame_->EmitPush(ip);
1054 frame_->EmitPush(r0);
1055 GenericBinaryOperation(op, mode, kUnknownIntValue);
1056 }
1057 }
1058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001059 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060}
1061
1062
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001063void CodeGenerator::Comparison(Condition cc,
1064 Expression* left,
1065 Expression* right,
1066 bool strict) {
1067 if (left != NULL) LoadAndSpill(left);
1068 if (right != NULL) LoadAndSpill(right);
1069
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001070 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001071 // sp[0] : y
1072 // sp[1] : x
1073 // result : cc register
1074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 // Strict only makes sense for equality comparisons.
1076 ASSERT(!strict || cc == eq);
1077
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001078 JumpTarget exit;
1079 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001080 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1081 if (cc == gt || cc == le) {
1082 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001083 frame_->EmitPop(r1);
1084 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001085 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001086 frame_->EmitPop(r0);
1087 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001088 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 __ orr(r2, r0, Operand(r1));
1090 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001091 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001093 // Perform non-smi comparison by stub.
1094 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1095 // We call with 0 args because there are 0 on the stack.
1096 CompareStub stub(cc, strict);
1097 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001098 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001099 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001101 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001102 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 __ cmp(r1, Operand(r0));
1104
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001105 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 cc_reg_ = cc;
1107}
1108
1109
mads.s.ager31e71382008-08-13 09:32:07 +00001110// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001111void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001112 CallFunctionFlags flags,
1113 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001114 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001116 int arg_count = args->length();
1117 for (int i = 0; i < arg_count; i++) {
1118 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001119 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
kasper.lund7276f142008-07-30 08:49:36 +00001121 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001122 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
kasper.lund7276f142008-07-30 08:49:36 +00001124 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001125 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001126 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001127 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128
1129 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001130 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001131 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132}
1133
1134
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001135void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001136 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 ASSERT(has_cc());
1138 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001139 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 cc_reg_ = al;
1141}
1142
1143
ager@chromium.org7c537e22008-10-16 08:43:32 +00001144void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001145 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001146 Comment cmnt(masm_, "[ check stack");
1147 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1148 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1149 // the implicit 8 byte offset that always applies to operations with pc and
1150 // gives a return address 12 bytes down.
1151 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1152 masm_->cmp(sp, Operand(ip));
1153 StackCheckStub stub;
1154 // Call the stub if lower.
1155 masm_->mov(pc,
1156 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1157 RelocInfo::CODE_TARGET),
1158 LeaveCC,
1159 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160}
1161
1162
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001163void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1164#ifdef DEBUG
1165 int original_height = frame_->height();
1166#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001167 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001168 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1169 VisitAndSpill(statements->at(i));
1170 }
1171 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1172}
1173
1174
ager@chromium.org7c537e22008-10-16 08:43:32 +00001175void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001176#ifdef DEBUG
1177 int original_height = frame_->height();
1178#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001179 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001181 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001182 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001183 VisitStatementsAndSpill(node->statements());
1184 if (node->break_target()->is_linked()) {
1185 node->break_target()->Bind();
1186 }
1187 node->break_target()->Unuse();
1188 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189}
1190
1191
ager@chromium.org7c537e22008-10-16 08:43:32 +00001192void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001193 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001194 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001195 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001196 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001197 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001198 frame_->EmitPush(r0);
1199 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001200 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201}
1202
1203
ager@chromium.org7c537e22008-10-16 08:43:32 +00001204void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001205#ifdef DEBUG
1206 int original_height = frame_->height();
1207#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001208 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209 Comment cmnt(masm_, "[ Declaration");
1210 Variable* var = node->proxy()->var();
1211 ASSERT(var != NULL); // must have been resolved
1212 Slot* slot = var->slot();
1213
1214 // If it was not possible to allocate the variable at compile time,
1215 // we need to "declare" it at runtime to make sure it actually
1216 // exists in the local context.
1217 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1218 // Variables with a "LOOKUP" slot were introduced as non-locals
1219 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001220 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001223 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 // Declaration nodes are always declared in only two modes.
1226 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1227 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001228 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001229 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 // Push initial value, if any.
1231 // Note: For variables we must not push an initial value (such as
1232 // 'undefined') because we may have a (legal) redeclaration and we
1233 // must not destroy the current value.
1234 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001235 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001236 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001238 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001240 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001243 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001244 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001245 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 return;
1247 }
1248
1249 ASSERT(!var->is_global());
1250
1251 // If we have a function or a constant, we need to initialize the variable.
1252 Expression* val = NULL;
1253 if (node->mode() == Variable::CONST) {
1254 val = new Literal(Factory::the_hole_value());
1255 } else {
1256 val = node->fun(); // NULL if we don't have a function
1257 }
1258
1259 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001260 {
1261 // Set initial value.
1262 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001263 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001264 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001265 }
1266 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270}
1271
1272
ager@chromium.org7c537e22008-10-16 08:43:32 +00001273void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274#ifdef DEBUG
1275 int original_height = frame_->height();
1276#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001277 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 Expression* expression = node->expression();
1281 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001282 LoadAndSpill(expression);
1283 frame_->Drop();
1284 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285}
1286
1287
ager@chromium.org7c537e22008-10-16 08:43:32 +00001288void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289#ifdef DEBUG
1290 int original_height = frame_->height();
1291#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001292 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001294 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297}
1298
1299
ager@chromium.org7c537e22008-10-16 08:43:32 +00001300void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001301#ifdef DEBUG
1302 int original_height = frame_->height();
1303#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001304 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 // Generate different code depending on which parts of the if statement
1307 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 bool has_then_stm = node->HasThenStatement();
1309 bool has_else_stm = node->HasElseStatement();
1310
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001311 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001313 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001315 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001316 JumpTarget then;
1317 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001319 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001320 if (frame_ != NULL) {
1321 Branch(false, &else_);
1322 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001324 if (frame_ != NULL || then.is_linked()) {
1325 then.Bind();
1326 VisitAndSpill(node->then_statement());
1327 }
1328 if (frame_ != NULL) {
1329 exit.Jump();
1330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001332 if (else_.is_linked()) {
1333 else_.Bind();
1334 VisitAndSpill(node->else_statement());
1335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336
1337 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001338 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001340 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001342 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 if (frame_ != NULL) {
1344 Branch(false, &exit);
1345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001347 if (frame_ != NULL || then.is_linked()) {
1348 then.Bind();
1349 VisitAndSpill(node->then_statement());
1350 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351
1352 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001353 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001355 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001357 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001358 if (frame_ != NULL) {
1359 Branch(true, &exit);
1360 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 if (frame_ != NULL || else_.is_linked()) {
1363 else_.Bind();
1364 VisitAndSpill(node->else_statement());
1365 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366
1367 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001368 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 ASSERT(!has_then_stm && !has_else_stm);
1370 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001371 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001372 if (frame_ != NULL) {
1373 if (has_cc()) {
1374 cc_reg_ = al;
1375 } else {
1376 frame_->Drop();
1377 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 }
1379 }
1380
1381 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001382 if (exit.is_linked()) {
1383 exit.Bind();
1384 }
1385 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386}
1387
1388
ager@chromium.org7c537e22008-10-16 08:43:32 +00001389void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001390 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001392 CodeForStatementPosition(node);
1393 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394}
1395
1396
ager@chromium.org7c537e22008-10-16 08:43:32 +00001397void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001398 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001400 CodeForStatementPosition(node);
1401 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402}
1403
1404
ager@chromium.org7c537e22008-10-16 08:43:32 +00001405void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001406 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001408
ager@chromium.org4af710e2009-09-15 12:20:11 +00001409 CodeForStatementPosition(node);
1410 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001411 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001412 frame_->EmitPop(r0);
1413 function_return_.Jump();
1414 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001415 // Pop the result from the frame and prepare the frame for
1416 // returning thus making it easier to merge.
1417 frame_->EmitPop(r0);
1418 frame_->PrepareForReturn();
1419
1420 function_return_.Jump();
1421 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422}
1423
1424
ager@chromium.org7c537e22008-10-16 08:43:32 +00001425void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001426#ifdef DEBUG
1427 int original_height = frame_->height();
1428#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001429 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001431 CodeForStatementPosition(node);
1432 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001433 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001435 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001436 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001437 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001438#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001439 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001440 __ cmp(r0, Operand(cp));
1441 verified_true.Branch(eq);
1442 __ stop("PushContext: r0 is expected to be the same as cp");
1443 verified_true.Bind();
1444#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001446 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001447 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448}
1449
1450
ager@chromium.org7c537e22008-10-16 08:43:32 +00001451void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452#ifdef DEBUG
1453 int original_height = frame_->height();
1454#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001455 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001457 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 // Pop context.
1459 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1460 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001461 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001462 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463}
1464
1465
ager@chromium.org7c537e22008-10-16 08:43:32 +00001466void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001467#ifdef DEBUG
1468 int original_height = frame_->height();
1469#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001470 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001472 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001473 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001476
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001477 JumpTarget next_test;
1478 JumpTarget fall_through;
1479 JumpTarget default_entry;
1480 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 ZoneList<CaseClause*>* cases = node->cases();
1482 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001483 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484
1485 for (int i = 0; i < length; i++) {
1486 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001488 // Remember the default clause and compile it at the end.
1489 default_clause = clause;
1490 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 }
1492
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001493 Comment cmnt(masm_, "[ Case clause");
1494 // Compile the test.
1495 next_test.Bind();
1496 next_test.Unuse();
1497 // Duplicate TOS.
1498 __ ldr(r0, frame_->Top());
1499 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001500 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001501 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001502
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001503 // Before entering the body from the test, remove the switch value from
1504 // the stack.
1505 frame_->Drop();
1506
1507 // Label the body so that fall through is enabled.
1508 if (i > 0 && cases->at(i - 1)->is_default()) {
1509 default_exit.Bind();
1510 } else {
1511 fall_through.Bind();
1512 fall_through.Unuse();
1513 }
1514 VisitStatementsAndSpill(clause->statements());
1515
1516 // If control flow can fall through from the body, jump to the next body
1517 // or the end of the statement.
1518 if (frame_ != NULL) {
1519 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1520 default_entry.Jump();
1521 } else {
1522 fall_through.Jump();
1523 }
1524 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 }
1526
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001527 // The final "test" removes the switch value.
1528 next_test.Bind();
1529 frame_->Drop();
1530
1531 // If there is a default clause, compile it.
1532 if (default_clause != NULL) {
1533 Comment cmnt(masm_, "[ Default clause");
1534 default_entry.Bind();
1535 VisitStatementsAndSpill(default_clause->statements());
1536 // If control flow can fall out of the default and there is a case after
1537 // it, jup to that case's body.
1538 if (frame_ != NULL && default_exit.is_bound()) {
1539 default_exit.Jump();
1540 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001541 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543 if (fall_through.is_linked()) {
1544 fall_through.Bind();
1545 }
1546
1547 if (node->break_target()->is_linked()) {
1548 node->break_target()->Bind();
1549 }
1550 node->break_target()->Unuse();
1551 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552}
1553
1554
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001555void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556#ifdef DEBUG
1557 int original_height = frame_->height();
1558#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001559 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001560 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001561 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001562 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001563 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001565 // Label the top of the loop for the backward CFG edge. If the test
1566 // is always true we can use the continue target, and if the test is
1567 // always false there is no need.
1568 ConditionAnalysis info = AnalyzeCondition(node->cond());
1569 switch (info) {
1570 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001571 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001572 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001573 break;
1574 case ALWAYS_FALSE:
1575 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1576 break;
1577 case DONT_KNOW:
1578 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1579 body.Bind();
1580 break;
1581 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001582
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001583 CheckStack(); // TODO(1222600): ignore if body contains calls.
1584 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001585
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001586 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001587 switch (info) {
1588 case ALWAYS_TRUE:
1589 // If control can fall off the end of the body, jump back to the
1590 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001591 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001592 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001593 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001595 case ALWAYS_FALSE:
1596 // If we have a continue in the body, we only have to bind its
1597 // jump target.
1598 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001599 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001601 break;
1602 case DONT_KNOW:
1603 // We have to compile the test expression if it can be reached by
1604 // control flow falling out of the body or via continue.
1605 if (node->continue_target()->is_linked()) {
1606 node->continue_target()->Bind();
1607 }
1608 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001609 Comment cmnt(masm_, "[ DoWhileCondition");
1610 CodeForDoWhileConditionPosition(node);
1611 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001612 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001613 // A invalid frame here indicates that control did not
1614 // fall out of the test expression.
1615 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 }
1617 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 break;
1619 }
1620
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001621 if (node->break_target()->is_linked()) {
1622 node->break_target()->Bind();
1623 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001624 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1625}
1626
1627
1628void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1629#ifdef DEBUG
1630 int original_height = frame_->height();
1631#endif
1632 VirtualFrame::SpilledScope spilled_scope;
1633 Comment cmnt(masm_, "[ WhileStatement");
1634 CodeForStatementPosition(node);
1635
1636 // If the test is never true and has no side effects there is no need
1637 // to compile the test or body.
1638 ConditionAnalysis info = AnalyzeCondition(node->cond());
1639 if (info == ALWAYS_FALSE) return;
1640
1641 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1642
1643 // Label the top of the loop with the continue target for the backward
1644 // CFG edge.
1645 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1646 node->continue_target()->Bind();
1647
1648 if (info == DONT_KNOW) {
1649 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001650 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001651 if (has_valid_frame()) {
1652 // A NULL frame indicates that control did not fall out of the
1653 // test expression.
1654 Branch(false, node->break_target());
1655 }
1656 if (has_valid_frame() || body.is_linked()) {
1657 body.Bind();
1658 }
1659 }
1660
1661 if (has_valid_frame()) {
1662 CheckStack(); // TODO(1222600): ignore if body contains calls.
1663 VisitAndSpill(node->body());
1664
1665 // If control flow can fall out of the body, jump back to the top.
1666 if (has_valid_frame()) {
1667 node->continue_target()->Jump();
1668 }
1669 }
1670 if (node->break_target()->is_linked()) {
1671 node->break_target()->Bind();
1672 }
1673 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1674}
1675
1676
1677void CodeGenerator::VisitForStatement(ForStatement* node) {
1678#ifdef DEBUG
1679 int original_height = frame_->height();
1680#endif
1681 VirtualFrame::SpilledScope spilled_scope;
1682 Comment cmnt(masm_, "[ ForStatement");
1683 CodeForStatementPosition(node);
1684 if (node->init() != NULL) {
1685 VisitAndSpill(node->init());
1686 }
1687
1688 // If the test is never true there is no need to compile the test or
1689 // body.
1690 ConditionAnalysis info = AnalyzeCondition(node->cond());
1691 if (info == ALWAYS_FALSE) return;
1692
1693 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1694
1695 // If there is no update statement, label the top of the loop with the
1696 // continue target, otherwise with the loop target.
1697 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1698 if (node->next() == NULL) {
1699 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1700 node->continue_target()->Bind();
1701 } else {
1702 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1703 loop.Bind();
1704 }
1705
1706 // If the test is always true, there is no need to compile it.
1707 if (info == DONT_KNOW) {
1708 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001709 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001710 if (has_valid_frame()) {
1711 Branch(false, node->break_target());
1712 }
1713 if (has_valid_frame() || body.is_linked()) {
1714 body.Bind();
1715 }
1716 }
1717
1718 if (has_valid_frame()) {
1719 CheckStack(); // TODO(1222600): ignore if body contains calls.
1720 VisitAndSpill(node->body());
1721
1722 if (node->next() == NULL) {
1723 // If there is no update statement and control flow can fall out
1724 // of the loop, jump directly to the continue label.
1725 if (has_valid_frame()) {
1726 node->continue_target()->Jump();
1727 }
1728 } else {
1729 // If there is an update statement and control flow can reach it
1730 // via falling out of the body of the loop or continuing, we
1731 // compile the update statement.
1732 if (node->continue_target()->is_linked()) {
1733 node->continue_target()->Bind();
1734 }
1735 if (has_valid_frame()) {
1736 // Record source position of the statement as this code which is
1737 // after the code for the body actually belongs to the loop
1738 // statement and not the body.
1739 CodeForStatementPosition(node);
1740 VisitAndSpill(node->next());
1741 loop.Jump();
1742 }
1743 }
1744 }
1745 if (node->break_target()->is_linked()) {
1746 node->break_target()->Bind();
1747 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749}
1750
1751
ager@chromium.org7c537e22008-10-16 08:43:32 +00001752void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001753#ifdef DEBUG
1754 int original_height = frame_->height();
1755#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001756 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001760 JumpTarget primitive;
1761 JumpTarget jsobject;
1762 JumpTarget fixed_array;
1763 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1764 JumpTarget end_del_check;
1765 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766
1767 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769
1770 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1771 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001772 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001773 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1774 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001775 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001776 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1777 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779
1780 // Stack layout in body:
1781 // [iteration counter (Smi)]
1782 // [length of array]
1783 // [FixedArray]
1784 // [Map or 0]
1785 // [Object]
1786
1787 // Check if enumerable is already a JSObject
1788 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001790 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 primitive.Bind();
1794 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001795 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001799 // r0: value to be iterated over
1800 frame_->EmitPush(r0); // Push the object being iterated over.
1801
1802 // Check cache validity in generated code. This is a fast case for
1803 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1804 // guarantee cache validity, call the runtime system to check cache
1805 // validity or get the property names in a fixed array.
1806 JumpTarget call_runtime;
1807 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1808 JumpTarget check_prototype;
1809 JumpTarget use_cache;
1810 __ mov(r1, Operand(r0));
1811 loop.Bind();
1812 // Check that there are no elements.
1813 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1814 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1815 __ cmp(r2, r4);
1816 call_runtime.Branch(ne);
1817 // Check that instance descriptors are not empty so that we can
1818 // check for an enum cache. Leave the map in r3 for the subsequent
1819 // prototype load.
1820 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1821 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1822 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1823 __ cmp(r2, ip);
1824 call_runtime.Branch(eq);
1825 // Check that there in an enum cache in the non-empty instance
1826 // descriptors. This is the case if the next enumeration index
1827 // field does not contain a smi.
1828 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1829 __ tst(r2, Operand(kSmiTagMask));
1830 call_runtime.Branch(eq);
1831 // For all objects but the receiver, check that the cache is empty.
1832 // r4: empty fixed array root.
1833 __ cmp(r1, r0);
1834 check_prototype.Branch(eq);
1835 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1836 __ cmp(r2, r4);
1837 call_runtime.Branch(ne);
1838 check_prototype.Bind();
1839 // Load the prototype from the map and loop if non-null.
1840 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1841 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1842 __ cmp(r1, ip);
1843 loop.Branch(ne);
1844 // The enum cache is valid. Load the map of the object being
1845 // iterated over and use the cache for the iteration.
1846 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1847 use_cache.Jump();
1848
1849 call_runtime.Bind();
1850 // Call the runtime to get the property names for the object.
1851 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001854 // If we got a map from the runtime call, we can do a fast
1855 // modification check. Otherwise, we got a fixed array, and we have
1856 // to do a slow check.
1857 // r0: map or fixed array (result from call to
1858 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 __ mov(r2, Operand(r0));
1860 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001861 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1862 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001865 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001867 // r0: map (either the result from a call to
1868 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1869 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 __ mov(r1, Operand(r0));
1871 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1872 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1873 __ ldr(r2,
1874 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 frame_->EmitPush(r0); // map
1877 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001878 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001881 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 frame_->EmitPush(r0);
1883 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001885 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001887 frame_->EmitPush(r1); // insert 0 in place of Map
1888 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889
1890 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001891 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001894 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001895 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896
1897 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001899 // sp[0] : index
1900 // sp[1] : array/enum cache length
1901 // sp[2] : array or enum cache
1902 // sp[3] : 0 or map
1903 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 // Grab the current frame's height for the break and continue
1905 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001906 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1907 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1910 __ ldr(r1, frame_->ElementAt(1)); // load the length
1911 __ cmp(r0, Operand(r1)); // compare to the array length
1912 node->break_target()->Branch(hs);
1913
1914 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1919 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1920
1921 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001922 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 // Check if this (still) matches the map of the enumerable.
1924 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1927 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929
1930 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001931 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1932 frame_->EmitPush(r0);
1933 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001934 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001935 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
1937 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001938 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1939 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001940 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001942 end_del_check.Bind();
1943 // Store the entry in the 'each' expression and take another spin in the
1944 // loop. r3: i'th entry of the enum cache (or string there of)
1945 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 { Reference each(this, node->each());
1947 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001948 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 __ ldr(r0, frame_->ElementAt(each.size()));
1950 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001951 each.SetValue(NOT_CONST_INIT);
1952 frame_->Drop(2);
1953 } else {
1954 // If the reference was to a slot we rely on the convenient property
1955 // that it doesn't matter whether a value (eg, r3 pushed above) is
1956 // right on top of or right underneath a zero-sized reference.
1957 each.SetValue(NOT_CONST_INIT);
1958 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00001959 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 }
1961 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001962 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 VisitAndSpill(node->body());
1965
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001966 // Next. Reestablish a spilled frame in case we are coming here via
1967 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001969 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001970 frame_->EmitPop(r0);
1971 __ add(r0, r0, Operand(Smi::FromInt(1)));
1972 frame_->EmitPush(r0);
1973 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001975 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1976 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001978 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979
1980 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 exit.Bind();
1982 node->continue_target()->Unuse();
1983 node->break_target()->Unuse();
1984 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985}
1986
1987
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001988void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989#ifdef DEBUG
1990 int original_height = frame_->height();
1991#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001992 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001993 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001996 JumpTarget try_block;
1997 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002001 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
2003 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002004 Variable* catch_var = node->catch_var()->var();
2005 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2006 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007
2008 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002011 VisitStatementsAndSpill(node->catch_block()->statements());
2012 if (frame_ != NULL) {
2013 exit.Jump();
2014 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015
2016
2017 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002020 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2021 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002023 // Shadow the labels for all escapes from the try block, including
2024 // returns. During shadowing, the original label is hidden as the
2025 // LabelShadow and operations on the original actually affect the
2026 // shadowing label.
2027 //
2028 // We should probably try to unify the escaping labels and the return
2029 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002030 int nof_escapes = node->escaping_targets()->length();
2031 List<ShadowTarget*> shadows(1 + nof_escapes);
2032
2033 // Add the shadow target for the function return.
2034 static const int kReturnShadowIndex = 0;
2035 shadows.Add(new ShadowTarget(&function_return_));
2036 bool function_return_was_shadowed = function_return_is_shadowed_;
2037 function_return_is_shadowed_ = true;
2038 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2039
2040 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 }
2044
2045 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
2048 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002049 // After shadowing stops, the original labels are unshadowed and the
2050 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002051 bool has_unlinks = false;
2052 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054 has_unlinks = has_unlinks || shadows[i]->is_linked();
2055 }
2056 function_return_is_shadowed_ = function_return_was_shadowed;
2057
2058 // Get an external reference to the handler address.
2059 ExternalReference handler_address(Top::k_handler_address);
2060
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 // If we can fall off the end of the try block, unlink from try chain.
2062 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002063 // The next handler address is on top of the frame. Unlink from
2064 // the handler list and drop the rest of this handler from the
2065 // frame.
2066 ASSERT(StackHandlerConstants::kNextOffset == 0);
2067 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002068 __ mov(r3, Operand(handler_address));
2069 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002070 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 if (has_unlinks) {
2072 exit.Jump();
2073 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 }
2075
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002076 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 // jumped to. Deallocate each shadow target.
2078 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002080 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081 shadows[i]->Bind();
2082 // Because we can be jumping here (to spilled code) from unspilled
2083 // code, we need to reestablish a spilled frame at this block.
2084 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 // Reload sp from the top handler, because some statements that we
2087 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002090 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002092 ASSERT(StackHandlerConstants::kNextOffset == 0);
2093 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002095 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002097 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2098 frame_->PrepareForReturn();
2099 }
2100 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 }
2102 }
2103
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002104 exit.Bind();
2105 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106}
2107
2108
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002109void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110#ifdef DEBUG
2111 int original_height = frame_->height();
2112#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002113 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002114 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002115 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116
2117 // State: Used to keep track of reason for entering the finally
2118 // block. Should probably be extended to hold information for
2119 // break/continue from within the try block.
2120 enum { FALLING, THROWING, JUMPING };
2121
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002122 JumpTarget try_block;
2123 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128 // In case of thrown exceptions, this is where we continue.
2129 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131
2132 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2136 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002138 // Shadow the labels for all escapes from the try block, including
2139 // returns. Shadowing hides the original label as the LabelShadow and
2140 // operations on the original actually affect the shadowing label.
2141 //
2142 // We should probably try to unify the escaping labels and the return
2143 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002144 int nof_escapes = node->escaping_targets()->length();
2145 List<ShadowTarget*> shadows(1 + nof_escapes);
2146
2147 // Add the shadow target for the function return.
2148 static const int kReturnShadowIndex = 0;
2149 shadows.Add(new ShadowTarget(&function_return_));
2150 bool function_return_was_shadowed = function_return_is_shadowed_;
2151 function_return_is_shadowed_ = true;
2152 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2153
2154 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 }
2158
2159 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002162 // Stop the introduced shadowing and count the number of required unlinks.
2163 // After shadowing stops, the original labels are unshadowed and the
2164 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 shadows[i]->StopShadowing();
2168 if (shadows[i]->is_linked()) nof_unlinks++;
2169 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 // Get an external reference to the handler address.
2173 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175 // If we can fall off the end of the try block, unlink from the try
2176 // chain and set the state on the frame to FALLING.
2177 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002178 // The next handler address is on top of the frame.
2179 ASSERT(StackHandlerConstants::kNextOffset == 0);
2180 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002181 __ mov(r3, Operand(handler_address));
2182 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002183 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002184
2185 // Fake a top of stack value (unneeded when FALLING) and set the
2186 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002187 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188 frame_->EmitPush(r0);
2189 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2190 if (nof_unlinks > 0) {
2191 finally_block.Jump();
2192 }
2193 }
2194
2195 // Generate code to unlink and set the state for the (formerly)
2196 // shadowing targets that have been jumped to.
2197 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002199 // If we have come from the shadowed return, the return value is
2200 // in (a non-refcounted reference to) r0. We must preserve it
2201 // until it is pushed.
2202 //
2203 // Because we can be jumping here (to spilled code) from
2204 // unspilled code, we need to reestablish a spilled frame at
2205 // this block.
2206 shadows[i]->Bind();
2207 frame_->SpillAll();
2208
2209 // Reload sp from the top handler, because some statements that
2210 // we break from (eg, for...in) may have left stuff on the
2211 // stack.
2212 __ mov(r3, Operand(handler_address));
2213 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002214 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002215
2216 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002217 // handler address is currently on top of the frame.
2218 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002219 frame_->EmitPop(r1);
2220 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002221 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002222
2223 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002224 // If this label shadowed the function return, materialize the
2225 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002227 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002228 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002229 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002230 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 }
2232 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002233 if (--nof_unlinks > 0) {
2234 // If this is not the last unlink block, jump around the next.
2235 finally_block.Jump();
2236 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 }
2238 }
2239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002241 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002243 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002244 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002245
2246 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002247 // and the state - while evaluating the finally block.
2248 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002252 if (has_valid_frame()) {
2253 // Restore state and return value or faked TOS.
2254 frame_->EmitPop(r2);
2255 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 }
2257
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002258 // Generate code to jump to the right destination for all used
2259 // formerly shadowing targets. Deallocate each shadow target.
2260 for (int i = 0; i < shadows.length(); i++) {
2261 if (has_valid_frame() && shadows[i]->is_bound()) {
2262 JumpTarget* original = shadows[i]->other_target();
2263 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2264 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002265 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266 skip.Branch(ne);
2267 frame_->PrepareForReturn();
2268 original->Jump();
2269 skip.Bind();
2270 } else {
2271 original->Branch(eq);
2272 }
2273 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002274 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002276 if (has_valid_frame()) {
2277 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002278 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002279 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2280 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002282 // Rethrow exception.
2283 frame_->EmitPush(r0);
2284 frame_->CallRuntime(Runtime::kReThrow, 1);
2285
2286 // Done.
2287 exit.Bind();
2288 }
2289 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290}
2291
2292
ager@chromium.org7c537e22008-10-16 08:43:32 +00002293void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002294#ifdef DEBUG
2295 int original_height = frame_->height();
2296#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002297 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002299 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002300#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002301 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002302#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002303 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305}
2306
2307
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002308void CodeGenerator::InstantiateFunction(
2309 Handle<SharedFunctionInfo> function_info) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002310 VirtualFrame::SpilledScope spilled_scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002311 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002312 // Use the fast case closure allocation code that allocates in new
2313 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002314 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002315 FastNewClosureStub stub;
2316 frame_->EmitPush(r0);
2317 frame_->CallStub(&stub, 1);
2318 frame_->EmitPush(r0);
2319 } else {
2320 // Create a new closure.
2321 frame_->EmitPush(cp);
2322 frame_->EmitPush(r0);
2323 frame_->CallRuntime(Runtime::kNewClosure, 2);
2324 frame_->EmitPush(r0);
2325 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326}
2327
2328
ager@chromium.org7c537e22008-10-16 08:43:32 +00002329void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002330#ifdef DEBUG
2331 int original_height = frame_->height();
2332#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002333 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 Comment cmnt(masm_, "[ FunctionLiteral");
2335
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002336 // Build the function info and instantiate it.
2337 Handle<SharedFunctionInfo> function_info =
2338 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002339 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 if (HasStackOverflow()) {
2341 ASSERT(frame_->height() == original_height);
2342 return;
2343 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002344 InstantiateFunction(function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002345 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346}
2347
2348
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002349void CodeGenerator::VisitSharedFunctionInfoLiteral(
2350 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351#ifdef DEBUG
2352 int original_height = frame_->height();
2353#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002354 VirtualFrame::SpilledScope spilled_scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002355 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2356 InstantiateFunction(node->shared_function_info());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002357 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358}
2359
2360
ager@chromium.org7c537e22008-10-16 08:43:32 +00002361void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362#ifdef DEBUG
2363 int original_height = frame_->height();
2364#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002365 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002367 JumpTarget then;
2368 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002369 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002370 if (has_valid_frame()) {
2371 Branch(false, &else_);
2372 }
2373 if (has_valid_frame() || then.is_linked()) {
2374 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002375 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002376 }
2377 if (else_.is_linked()) {
2378 JumpTarget exit;
2379 if (has_valid_frame()) exit.Jump();
2380 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002381 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002382 if (exit.is_linked()) exit.Bind();
2383 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002384 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385}
2386
2387
ager@chromium.org7c537e22008-10-16 08:43:32 +00002388void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002389 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002390 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002391 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002393 JumpTarget slow;
2394 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002395
2396 // Generate fast-case code for variables that might be shadowed by
2397 // eval-introduced variables. Eval is used a lot without
2398 // introducing variables. In those cases, we do not want to
2399 // perform a runtime call for all variables in the scope
2400 // containing the eval.
2401 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2402 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002403 // If there was no control flow to slow, we can exit early.
2404 if (!slow.is_linked()) {
2405 frame_->EmitPush(r0);
2406 return;
2407 }
2408
2409 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002410
2411 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2412 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2413 // Only generate the fast case for locals that rewrite to slots.
2414 // This rules out argument loads.
2415 if (potential_slot != NULL) {
2416 __ ldr(r0,
2417 ContextSlotOperandCheckExtensions(potential_slot,
2418 r1,
2419 r2,
2420 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002421 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002422 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2423 __ cmp(r0, ip);
2424 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002425 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002426 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002427 // ContextSlotOperandCheckExtensions so we have to jump around
2428 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002429 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002430 }
2431 }
2432
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002433 slow.Bind();
2434 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002435 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002436 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
ager@chromium.org7c537e22008-10-16 08:43:32 +00002438 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002439 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002440 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002441 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002443
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 done.Bind();
2445 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446
2447 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002448 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002449 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002451 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002452 // Const slots may contain 'the hole' value (the constant hasn't been
2453 // initialized yet) which needs to be converted into the 'undefined'
2454 // value.
2455 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002457 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2458 __ cmp(r0, ip);
2459 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 }
2462 }
2463}
2464
2465
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002466void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2467 ASSERT(slot != NULL);
2468 if (slot->type() == Slot::LOOKUP) {
2469 ASSERT(slot->var()->is_dynamic());
2470
2471 // For now, just do a runtime call.
2472 frame_->EmitPush(cp);
2473 __ mov(r0, Operand(slot->var()->name()));
2474 frame_->EmitPush(r0);
2475
2476 if (init_state == CONST_INIT) {
2477 // Same as the case for a normal store, but ignores attribute
2478 // (e.g. READ_ONLY) of context slot so that we can initialize
2479 // const properties (introduced via eval("const foo = (some
2480 // expr);")). Also, uses the current function context instead of
2481 // the top context.
2482 //
2483 // Note that we must declare the foo upon entry of eval(), via a
2484 // context slot declaration, but we cannot initialize it at the
2485 // same time, because the const declaration may be at the end of
2486 // the eval code (sigh...) and the const variable may have been
2487 // used before (where its value is 'undefined'). Thus, we can only
2488 // do the initialization when we actually encounter the expression
2489 // and when the expression operands are defined and valid, and
2490 // thus we need the split into 2 operations: declaration of the
2491 // context slot followed by initialization.
2492 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2493 } else {
2494 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2495 }
2496 // Storing a variable must keep the (new) value on the expression
2497 // stack. This is necessary for compiling assignment expressions.
2498 frame_->EmitPush(r0);
2499
2500 } else {
2501 ASSERT(!slot->var()->is_dynamic());
2502
2503 JumpTarget exit;
2504 if (init_state == CONST_INIT) {
2505 ASSERT(slot->var()->mode() == Variable::CONST);
2506 // Only the first const initialization must be executed (the slot
2507 // still contains 'the hole' value). When the assignment is
2508 // executed, the code is identical to a normal store (see below).
2509 Comment cmnt(masm_, "[ Init const");
2510 __ ldr(r2, SlotOperand(slot, r2));
2511 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2512 __ cmp(r2, ip);
2513 exit.Branch(ne);
2514 }
2515
2516 // We must execute the store. Storing a variable must keep the
2517 // (new) value on the stack. This is necessary for compiling
2518 // assignment expressions.
2519 //
2520 // Note: We will reach here even with slot->var()->mode() ==
2521 // Variable::CONST because of const declarations which will
2522 // initialize consts to 'the hole' value and by doing so, end up
2523 // calling this code. r2 may be loaded with context; used below in
2524 // RecordWrite.
2525 frame_->EmitPop(r0);
2526 __ str(r0, SlotOperand(slot, r2));
2527 frame_->EmitPush(r0);
2528 if (slot->type() == Slot::CONTEXT) {
2529 // Skip write barrier if the written value is a smi.
2530 __ tst(r0, Operand(kSmiTagMask));
2531 exit.Branch(eq);
2532 // r2 is loaded with context when calling SlotOperand above.
2533 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2534 __ mov(r3, Operand(offset));
2535 __ RecordWrite(r2, r3, r1);
2536 }
2537 // If we definitely did not jump over the assignment, we do not need
2538 // to bind the exit label. Doing so can defeat peephole
2539 // optimization.
2540 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2541 exit.Bind();
2542 }
2543 }
2544}
2545
2546
ager@chromium.org381abbb2009-02-25 13:23:22 +00002547void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2548 TypeofState typeof_state,
2549 Register tmp,
2550 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002552 // Check that no extension objects have been created by calls to
2553 // eval from the current scope to the global scope.
2554 Register context = cp;
2555 Scope* s = scope();
2556 while (s != NULL) {
2557 if (s->num_heap_slots() > 0) {
2558 if (s->calls_eval()) {
2559 // Check that extension is NULL.
2560 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2561 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002563 }
2564 // Load next context in chain.
2565 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2566 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2567 context = tmp;
2568 }
2569 // If no outer scope calls eval, we do not need to check more
2570 // context extensions.
2571 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2572 s = s->outer_scope();
2573 }
2574
2575 if (s->is_eval_scope()) {
2576 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002577 if (!context.is(tmp)) {
2578 __ mov(tmp, Operand(context));
2579 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002580 __ bind(&next);
2581 // Terminate at global context.
2582 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002583 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2584 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002585 __ b(eq, &fast);
2586 // Check that extension is NULL.
2587 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2588 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002589 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002590 // Load next context in chain.
2591 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2592 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2593 __ b(&next);
2594 __ bind(&fast);
2595 }
2596
2597 // All extension objects were empty and it is safe to use a global
2598 // load IC call.
2599 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2600 // Load the global object.
2601 LoadGlobal();
2602 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002603 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002604 // Call IC stub.
2605 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002606 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002607 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002608 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002609 }
2610
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002611 // Drop the global object. The result is in r0.
2612 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002613}
2614
2615
ager@chromium.org7c537e22008-10-16 08:43:32 +00002616void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617#ifdef DEBUG
2618 int original_height = frame_->height();
2619#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002620 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002621 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002622 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002624}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625
ager@chromium.org7c537e22008-10-16 08:43:32 +00002626
2627void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002628#ifdef DEBUG
2629 int original_height = frame_->height();
2630#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002631 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002632 Comment cmnt(masm_, "[ VariableProxy");
2633
2634 Variable* var = node->var();
2635 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002636 if (expr != NULL) {
2637 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002639 ASSERT(var->is_global());
2640 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002641 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002643 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644}
2645
2646
ager@chromium.org7c537e22008-10-16 08:43:32 +00002647void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002648#ifdef DEBUG
2649 int original_height = frame_->height();
2650#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002651 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002653 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 frame_->EmitPush(r0);
2655 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656}
2657
2658
ager@chromium.org7c537e22008-10-16 08:43:32 +00002659void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660#ifdef DEBUG
2661 int original_height = frame_->height();
2662#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002663 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 Comment cmnt(masm_, "[ RexExp Literal");
2665
2666 // Retrieve the literal array and check the allocated entry.
2667
2668 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002669 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670
2671 // Load the literals array of the function.
2672 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2673
2674 // Load the literal at the ast saved index.
2675 int literal_offset =
2676 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2677 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2678
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002679 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002680 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2681 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683
2684 // If the entry is undefined we call the runtime system to computed
2685 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002686 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002687 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002689 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002690 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002691 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 frame_->EmitPush(r0);
2693 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002694 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002696 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 frame_->EmitPush(r2);
2699 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700}
2701
2702
ager@chromium.org7c537e22008-10-16 08:43:32 +00002703void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002704#ifdef DEBUG
2705 int original_height = frame_->height();
2706#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002707 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 Comment cmnt(masm_, "[ ObjectLiteral");
2709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002711 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002712 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002713 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002714 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002715 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002716 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002717 __ mov(r1, Operand(node->constant_properties()));
2718 // Should the object literal have fast elements?
2719 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
2720 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002721 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002722 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002723 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002724 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002726 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002728 // At the start of each iteration, the top of stack contains
2729 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 ObjectLiteral::Property* property = node->properties()->at(i);
2731 Literal* key = property->key();
2732 Expression* value = property->value();
2733 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002734 case ObjectLiteral::Property::CONSTANT:
2735 break;
2736 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2737 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2738 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00002739 case ObjectLiteral::Property::COMPUTED:
2740 if (key->handle()->IsSymbol()) {
2741 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2742 LoadAndSpill(value);
2743 frame_->EmitPop(r0);
2744 __ mov(r2, Operand(key->handle()));
2745 __ ldr(r1, frame_->Top()); // Load the receiver.
2746 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
2747 break;
2748 }
2749 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002751 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002752 frame_->EmitPush(r0); // dup the result
2753 LoadAndSpill(key);
2754 LoadAndSpill(value);
2755 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 break;
2757 }
2758 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002759 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 frame_->EmitPush(r0);
2761 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002762 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002763 frame_->EmitPush(r0);
2764 LoadAndSpill(value);
2765 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 break;
2767 }
2768 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002769 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002770 frame_->EmitPush(r0);
2771 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002772 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 frame_->EmitPush(r0);
2774 LoadAndSpill(value);
2775 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776 break;
2777 }
2778 }
2779 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002780 ASSERT(frame_->height() == original_height + 1);
2781}
2782
2783
ager@chromium.org7c537e22008-10-16 08:43:32 +00002784void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002785#ifdef DEBUG
2786 int original_height = frame_->height();
2787#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002788 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002790
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002791 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002792 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00002793 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002794 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002795 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002796 __ mov(r0, Operand(node->constant_elements()));
2797 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00002798 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002799 if (node->depth() > 1) {
2800 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002801 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002802 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002803 } else {
2804 FastCloneShallowArrayStub stub(length);
2805 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002806 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002807 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002808 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002809
2810 // Generate code to set the elements in the array that are not
2811 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 for (int i = 0; i < node->values()->length(); i++) {
2813 Expression* value = node->values()->at(i);
2814
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002815 // If value is a literal the property value is already set in the
2816 // boilerplate object.
2817 if (value->AsLiteral() != NULL) continue;
2818 // If value is a materialized literal the property value is already set
2819 // in the boilerplate object if it is simple.
2820 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002822 // The property must be set by generated code.
2823 LoadAndSpill(value);
2824 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002826 // Fetch the object literal.
2827 __ ldr(r1, frame_->Top());
2828 // Get the elements array.
2829 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002831 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002832 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002833 __ str(r0, FieldMemOperand(r1, offset));
2834
2835 // Update the write barrier for the array address.
2836 __ mov(r3, Operand(offset));
2837 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840}
2841
2842
ager@chromium.org32912102009-01-16 10:38:43 +00002843void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844#ifdef DEBUG
2845 int original_height = frame_->height();
2846#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002847 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002848 // Call runtime routine to allocate the catch extension object and
2849 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002850 Comment cmnt(masm_, "[ CatchExtensionObject");
2851 LoadAndSpill(node->key());
2852 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002853 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2854 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002855 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002856}
2857
2858
ager@chromium.org7c537e22008-10-16 08:43:32 +00002859void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002860#ifdef DEBUG
2861 int original_height = frame_->height();
2862#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002863 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002865
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002866 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002867 if (target.is_illegal()) {
2868 // Fool the virtual frame into thinking that we left the assignment's
2869 // value on the frame.
2870 __ mov(r0, Operand(Smi::FromInt(0)));
2871 frame_->EmitPush(r0);
2872 ASSERT(frame_->height() == original_height + 1);
2873 return;
2874 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002876 if (node->op() == Token::ASSIGN ||
2877 node->op() == Token::INIT_VAR ||
2878 node->op() == Token::INIT_CONST) {
2879 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002880
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002881 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002882 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002883 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002885 bool overwrite =
2886 (node->value()->AsBinaryOperation() != NULL &&
2887 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002888 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002889 SmiOperation(node->binary_op(),
2890 literal->handle(),
2891 false,
2892 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 frame_->EmitPush(r0);
2894
2895 } else {
2896 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002897 GenericBinaryOperation(node->binary_op(),
2898 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899 frame_->EmitPush(r0);
2900 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2903 if (var != NULL &&
2904 (var->mode() == Variable::CONST) &&
2905 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2906 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002909 CodeForSourcePosition(node->position());
2910 if (node->op() == Token::INIT_CONST) {
2911 // Dynamic constant initializations must use the function context
2912 // and initialize the actual constant declared. Dynamic variable
2913 // initializations are simply assignments and use SetValue.
2914 target.SetValue(CONST_INIT);
2915 } else {
2916 target.SetValue(NOT_CONST_INIT);
2917 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 }
2919 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002920 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921}
2922
2923
ager@chromium.org7c537e22008-10-16 08:43:32 +00002924void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925#ifdef DEBUG
2926 int original_height = frame_->height();
2927#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002928 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929 Comment cmnt(masm_, "[ Throw");
2930
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002931 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002932 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002933 frame_->CallRuntime(Runtime::kThrow, 1);
2934 frame_->EmitPush(r0);
2935 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936}
2937
2938
ager@chromium.org7c537e22008-10-16 08:43:32 +00002939void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002940#ifdef DEBUG
2941 int original_height = frame_->height();
2942#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002943 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002945
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002947 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948 }
2949 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950}
2951
2952
ager@chromium.org7c537e22008-10-16 08:43:32 +00002953void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954#ifdef DEBUG
2955 int original_height = frame_->height();
2956#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002957 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 Comment cmnt(masm_, "[ Call");
2959
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002960 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 ZoneList<Expression*>* args = node->arguments();
2962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 Variable* var = function->AsVariableProxy()->AsVariable();
2966 Property* property = function->AsProperty();
2967
2968 // ------------------------------------------------------------------------
2969 // Fast-case: Use inline caching.
2970 // ---
2971 // According to ECMA-262, section 11.2.3, page 44, the function to call
2972 // must be resolved after the arguments have been evaluated. The IC code
2973 // automatically handles this by loading the arguments before the function
2974 // is resolved in cache misses (this also holds for megamorphic calls).
2975 // ------------------------------------------------------------------------
2976
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002977 if (var != NULL && var->is_possibly_eval()) {
2978 // ----------------------------------
2979 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2980 // ----------------------------------
2981
2982 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2983 // resolve the function we need to call and the receiver of the
2984 // call. Then we call the resolved function using the given
2985 // arguments.
2986 // Prepare stack for call to resolved function.
2987 LoadAndSpill(function);
2988 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2989 frame_->EmitPush(r2); // Slot for receiver
2990 int arg_count = args->length();
2991 for (int i = 0; i < arg_count; i++) {
2992 LoadAndSpill(args->at(i));
2993 }
2994
2995 // Prepare stack for call to ResolvePossiblyDirectEval.
2996 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2997 frame_->EmitPush(r1);
2998 if (arg_count > 0) {
2999 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3000 frame_->EmitPush(r1);
3001 } else {
3002 frame_->EmitPush(r2);
3003 }
3004
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003005 // Push the receiver.
3006 __ ldr(r1, frame_->Receiver());
3007 frame_->EmitPush(r1);
3008
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003009 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003010 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003011
3012 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003013 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003014 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3015
3016 // Call the function.
3017 CodeForSourcePosition(node->position());
3018
3019 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003020 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003021 frame_->CallStub(&call_function, arg_count + 1);
3022
3023 __ ldr(cp, frame_->Context());
3024 // Remove the function from the stack.
3025 frame_->Drop();
3026 frame_->EmitPush(r0);
3027
3028 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 // ----------------------------------
3030 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3031 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003032 // Pass the global object as the receiver and let the IC stub
3033 // patch the stack to use the global proxy as 'this' in the
3034 // invoked function.
3035 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
3037 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003038 int arg_count = args->length();
3039 for (int i = 0; i < arg_count; i++) {
3040 LoadAndSpill(args->at(i));
3041 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042
ager@chromium.org5c838252010-02-19 08:53:10 +00003043 // Setup the name register and call the IC initialization code.
3044 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003045 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3046 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003047 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3049 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003050 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052
3053 } else if (var != NULL && var->slot() != NULL &&
3054 var->slot()->type() == Slot::LOOKUP) {
3055 // ----------------------------------
3056 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3057 // ----------------------------------
3058
3059 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003060 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003061 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003062 frame_->EmitPush(r0);
3063 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 // r0: slot value; r1: receiver
3065
3066 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 frame_->EmitPush(r0); // function
3068 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069
3070 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003071 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073
3074 } else if (property != NULL) {
3075 // Check if the key is a literal string.
3076 Literal* literal = property->key()->AsLiteral();
3077
3078 if (literal != NULL && literal->handle()->IsSymbol()) {
3079 // ------------------------------------------------------------------
3080 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3081 // ------------------------------------------------------------------
3082
ager@chromium.org5c838252010-02-19 08:53:10 +00003083 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003085 int arg_count = args->length();
3086 for (int i = 0; i < arg_count; i++) {
3087 LoadAndSpill(args->at(i));
3088 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089
ager@chromium.org5c838252010-02-19 08:53:10 +00003090 // Set the name register and call the IC initialization code.
3091 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003092 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3093 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003094 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003095 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003096 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003097 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098
3099 } else {
3100 // -------------------------------------------
3101 // JavaScript example: 'array[index](1, 2, 3)'
3102 // -------------------------------------------
3103
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003104 LoadAndSpill(property->obj());
3105 LoadAndSpill(property->key());
3106 EmitKeyedLoad(false);
3107 frame_->Drop(); // key
3108 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003110 // Use the global receiver.
3111 frame_->Drop();
3112 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 LoadGlobalReceiver(r0);
3114 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003115 frame_->EmitPop(r1); // receiver
3116 frame_->EmitPush(r0); // function
3117 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003118 }
3119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003121 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 }
3124
3125 } else {
3126 // ----------------------------------
3127 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3128 // ----------------------------------
3129
3130 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003131 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003132
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003133 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003134 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003137 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003138 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003139 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141}
3142
3143
ager@chromium.org7c537e22008-10-16 08:43:32 +00003144void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003145#ifdef DEBUG
3146 int original_height = frame_->height();
3147#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003148 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149 Comment cmnt(masm_, "[ CallNew");
3150
3151 // According to ECMA-262, section 11.2.2, page 44, the function
3152 // expression in new calls must be evaluated before the
3153 // arguments. This is different from ordinary calls, where the
3154 // actual function to call is resolved after the arguments have been
3155 // evaluated.
3156
3157 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003158 // receiver. There is no need to use the global proxy here because
3159 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003160 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003161 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162
3163 // Push the arguments ("left-to-right") on the stack.
3164 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003165 int arg_count = args->length();
3166 for (int i = 0; i < arg_count; i++) {
3167 LoadAndSpill(args->at(i));
3168 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169
mads.s.ager31e71382008-08-13 09:32:07 +00003170 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003171 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003172 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003173 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175 // Call the construct call builtin that handles allocation and
3176 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003177 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003178 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003179 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003180
3181 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003182 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003183 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184}
3185
3186
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003187void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3188 VirtualFrame::SpilledScope spilled_scope;
3189 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003190 JumpTarget leave, null, function, non_function_constructor;
3191
3192 // Load the object into r0.
3193 LoadAndSpill(args->at(0));
3194 frame_->EmitPop(r0);
3195
3196 // If the object is a smi, we return null.
3197 __ tst(r0, Operand(kSmiTagMask));
3198 null.Branch(eq);
3199
3200 // Check that the object is a JS object but take special care of JS
3201 // functions to make sure they have 'Function' as their class.
3202 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3203 null.Branch(lt);
3204
3205 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3206 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3207 // LAST_JS_OBJECT_TYPE.
3208 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3209 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3210 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3211 function.Branch(eq);
3212
3213 // Check if the constructor in the map is a function.
3214 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3215 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3216 non_function_constructor.Branch(ne);
3217
3218 // The r0 register now contains the constructor function. Grab the
3219 // instance class name from there.
3220 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3221 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003222 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003223 leave.Jump();
3224
3225 // Functions have class 'Function'.
3226 function.Bind();
3227 __ mov(r0, Operand(Factory::function_class_symbol()));
3228 frame_->EmitPush(r0);
3229 leave.Jump();
3230
3231 // Objects with a non-function constructor have class 'Object'.
3232 non_function_constructor.Bind();
3233 __ mov(r0, Operand(Factory::Object_symbol()));
3234 frame_->EmitPush(r0);
3235 leave.Jump();
3236
3237 // Non-JS objects have class null.
3238 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003239 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003240 frame_->EmitPush(r0);
3241
3242 // All done.
3243 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003244}
3245
3246
ager@chromium.org7c537e22008-10-16 08:43:32 +00003247void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003248 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003250 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003251 LoadAndSpill(args->at(0));
3252 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003253 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003256 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3257 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 // Load the value.
3260 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261 leave.Bind();
3262 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263}
3264
3265
ager@chromium.org7c537e22008-10-16 08:43:32 +00003266void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003267 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003269 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 LoadAndSpill(args->at(0)); // Load the object.
3271 LoadAndSpill(args->at(1)); // Load the value.
3272 frame_->EmitPop(r0); // r0 contains value
3273 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 // if (object->IsSmi()) return object.
3275 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003277 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3278 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 // Store the value.
3281 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3282 // Update the write barrier.
3283 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3284 __ RecordWrite(r1, r2, r3);
3285 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 leave.Bind();
3287 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288}
3289
3290
ager@chromium.org7c537e22008-10-16 08:43:32 +00003291void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003292 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003294 LoadAndSpill(args->at(0));
3295 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003296 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 cc_reg_ = eq;
3298}
3299
3300
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003301void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003302 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003303 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3304 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003305#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003306 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 LoadAndSpill(args->at(1));
3308 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003309 __ CallRuntime(Runtime::kLog, 2);
3310 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003311#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003312 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003314}
3315
3316
ager@chromium.org7c537e22008-10-16 08:43:32 +00003317void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003318 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003319 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003320 LoadAndSpill(args->at(0));
3321 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003322 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003323 cc_reg_ = eq;
3324}
3325
3326
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003327// Generates the Math.pow method - currently just calls runtime.
3328void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3329 ASSERT(args->length() == 2);
3330 Load(args->at(0));
3331 Load(args->at(1));
3332 frame_->CallRuntime(Runtime::kMath_pow, 2);
3333 frame_->EmitPush(r0);
3334}
3335
3336
3337// Generates the Math.sqrt method - currently just calls runtime.
3338void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
3339 ASSERT(args->length() == 1);
3340 Load(args->at(0));
3341 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
3342 frame_->EmitPush(r0);
3343}
3344
3345
kasper.lund7276f142008-07-30 08:49:36 +00003346// This should generate code that performs a charCodeAt() call or returns
3347// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3348// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003349void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003350 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003351 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003352 Comment(masm_, "[ GenerateFastCharCodeAt");
3353
3354 LoadAndSpill(args->at(0));
3355 LoadAndSpill(args->at(1));
3356 frame_->EmitPop(r0); // Index.
3357 frame_->EmitPop(r1); // String.
3358
3359 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3360
3361 __ tst(r1, Operand(kSmiTagMask));
3362 __ b(eq, &slow); // The 'string' was a Smi.
3363
3364 ASSERT(kSmiTag == 0);
3365 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3366 __ b(ne, &slow); // The index was negative or not a Smi.
3367
3368 __ bind(&try_again_with_new_string);
3369 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3370 __ b(ge, &slow);
3371
3372 // Now r2 has the string type.
3373 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003374 // Now r3 has the length of the string. Compare with the index.
3375 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3376 __ b(le, &slow);
3377
3378 // Here we know the index is in range. Check that string is sequential.
3379 ASSERT_EQ(0, kSeqStringTag);
3380 __ tst(r2, Operand(kStringRepresentationMask));
3381 __ b(ne, &not_a_flat_string);
3382
3383 // Check whether it is an ASCII string.
3384 ASSERT_EQ(0, kTwoByteStringTag);
3385 __ tst(r2, Operand(kStringEncodingMask));
3386 __ b(ne, &ascii_string);
3387
3388 // 2-byte string. We can add without shifting since the Smi tag size is the
3389 // log2 of the number of bytes in a two-byte character.
3390 ASSERT_EQ(1, kSmiTagSize);
3391 ASSERT_EQ(0, kSmiShiftSize);
3392 __ add(r1, r1, Operand(r0));
3393 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3394 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3395 __ jmp(&end);
3396
3397 __ bind(&ascii_string);
3398 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3399 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3400 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3401 __ jmp(&end);
3402
3403 __ bind(&not_a_flat_string);
3404 __ and_(r2, r2, Operand(kStringRepresentationMask));
3405 __ cmp(r2, Operand(kConsStringTag));
3406 __ b(ne, &slow);
3407
3408 // ConsString.
3409 // Check that the right hand side is the empty string (ie if this is really a
3410 // flat string in a cons string). If that is not the case we would rather go
3411 // to the runtime system now, to flatten the string.
3412 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3413 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3414 __ cmp(r2, Operand(r3));
3415 __ b(ne, &slow);
3416
3417 // Get the first of the two strings.
3418 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3419 __ jmp(&try_again_with_new_string);
3420
3421 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003422 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003423
3424 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003425 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003426}
3427
3428
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003429void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
3430 Comment(masm_, "[ GenerateCharFromCode");
3431 ASSERT(args->length() == 1);
3432
3433 LoadAndSpill(args->at(0));
3434 frame_->EmitPop(r0);
3435
3436 JumpTarget slow_case;
3437 JumpTarget exit;
3438
3439 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3440 ASSERT(kSmiTag == 0);
3441 ASSERT(kSmiShiftSize == 0);
3442 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3443 __ tst(r0, Operand(kSmiTagMask |
3444 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3445 slow_case.Branch(nz);
3446
3447 ASSERT(kSmiTag == 0);
3448 __ mov(r1, Operand(Factory::single_character_string_cache()));
3449 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
3450 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
3451 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3452 __ cmp(r1, ip);
3453 slow_case.Branch(eq);
3454
3455 frame_->EmitPush(r1);
3456 exit.Jump();
3457
3458 slow_case.Bind();
3459 frame_->EmitPush(r0);
3460 frame_->CallRuntime(Runtime::kCharFromCode, 1);
3461 frame_->EmitPush(r0);
3462
3463 exit.Bind();
3464}
3465
3466
ager@chromium.org7c537e22008-10-16 08:43:32 +00003467void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003468 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003469 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003470 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003471 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003472 // We need the CC bits to come out as not_equal in the case where the
3473 // object is a smi. This can't be done with the usual test opcode so
3474 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003475 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003476 __ and_(r1, r0, Operand(kSmiTagMask));
3477 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003479 // It is a heap object - get the map. Check if the object is a JS array.
3480 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003481 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482 cc_reg_ = eq;
3483}
3484
3485
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003486void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
3487 VirtualFrame::SpilledScope spilled_scope;
3488 ASSERT(args->length() == 1);
3489 LoadAndSpill(args->at(0));
3490 JumpTarget answer;
3491 // We need the CC bits to come out as not_equal in the case where the
3492 // object is a smi. This can't be done with the usual test opcode so
3493 // we use XOR to get the right CC bits.
3494 frame_->EmitPop(r0);
3495 __ and_(r1, r0, Operand(kSmiTagMask));
3496 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3497 answer.Branch(ne);
3498 // It is a heap object - get the map. Check if the object is a regexp.
3499 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3500 answer.Bind();
3501 cc_reg_ = eq;
3502}
3503
3504
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003505void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3506 // This generates a fast version of:
3507 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3508 VirtualFrame::SpilledScope spilled_scope;
3509 ASSERT(args->length() == 1);
3510 LoadAndSpill(args->at(0));
3511 frame_->EmitPop(r1);
3512 __ tst(r1, Operand(kSmiTagMask));
3513 false_target()->Branch(eq);
3514
3515 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3516 __ cmp(r1, ip);
3517 true_target()->Branch(eq);
3518
3519 Register map_reg = r2;
3520 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3521 // Undetectable objects behave like undefined when tested with typeof.
3522 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3523 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3524 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3525 false_target()->Branch(eq);
3526
3527 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3528 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3529 false_target()->Branch(lt);
3530 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3531 cc_reg_ = le;
3532}
3533
3534
3535void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3536 // This generates a fast version of:
3537 // (%_ClassOf(arg) === 'Function')
3538 VirtualFrame::SpilledScope spilled_scope;
3539 ASSERT(args->length() == 1);
3540 LoadAndSpill(args->at(0));
3541 frame_->EmitPop(r0);
3542 __ tst(r0, Operand(kSmiTagMask));
3543 false_target()->Branch(eq);
3544 Register map_reg = r2;
3545 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3546 cc_reg_ = eq;
3547}
3548
3549
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003550void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
3551 VirtualFrame::SpilledScope spilled_scope;
3552 ASSERT(args->length() == 1);
3553 LoadAndSpill(args->at(0));
3554 frame_->EmitPop(r0);
3555 __ tst(r0, Operand(kSmiTagMask));
3556 false_target()->Branch(eq);
3557 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3558 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3559 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3560 cc_reg_ = ne;
3561}
3562
3563
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003564void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3565 VirtualFrame::SpilledScope spilled_scope;
3566 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003567
3568 // Get the frame pointer for the calling frame.
3569 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3570
3571 // Skip the arguments adaptor frame if it exists.
3572 Label check_frame_marker;
3573 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003574 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003575 __ b(ne, &check_frame_marker);
3576 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3577
3578 // Check the marker in the calling frame.
3579 __ bind(&check_frame_marker);
3580 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3581 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3582 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003583}
3584
3585
ager@chromium.org7c537e22008-10-16 08:43:32 +00003586void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003587 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 ASSERT(args->length() == 0);
3589
mads.s.ager31e71382008-08-13 09:32:07 +00003590 // Seed the result with the formal parameters count, which will be used
3591 // in case no arguments adaptor frame is found below the current frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00003592 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593
3594 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003595 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003596 frame_->CallStub(&stub, 0);
3597 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003598}
3599
3600
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003601void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003602 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 ASSERT(args->length() == 1);
3604
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003605 // Satisfy contract with ArgumentsAccessStub:
3606 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003607 LoadAndSpill(args->at(0));
3608 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003609 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610
3611 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003612 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003613 frame_->CallStub(&stub, 0);
3614 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615}
3616
3617
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003618void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3619 VirtualFrame::SpilledScope spilled_scope;
3620 ASSERT(args->length() == 0);
3621 __ Call(ExternalReference::random_positive_smi_function().address(),
3622 RelocInfo::RUNTIME_ENTRY);
3623 frame_->EmitPush(r0);
3624}
3625
3626
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003627void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3628 ASSERT_EQ(2, args->length());
3629
3630 Load(args->at(0));
3631 Load(args->at(1));
3632
ager@chromium.org5c838252010-02-19 08:53:10 +00003633 StringAddStub stub(NO_STRING_ADD_FLAGS);
3634 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003635 frame_->EmitPush(r0);
3636}
3637
3638
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003639void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3640 ASSERT_EQ(3, args->length());
3641
3642 Load(args->at(0));
3643 Load(args->at(1));
3644 Load(args->at(2));
3645
ager@chromium.org5c838252010-02-19 08:53:10 +00003646 SubStringStub stub;
3647 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003648 frame_->EmitPush(r0);
3649}
3650
3651
3652void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3653 ASSERT_EQ(2, args->length());
3654
3655 Load(args->at(0));
3656 Load(args->at(1));
3657
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003658 StringCompareStub stub;
3659 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003660 frame_->EmitPush(r0);
3661}
3662
3663
3664void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3665 ASSERT_EQ(4, args->length());
3666
3667 Load(args->at(0));
3668 Load(args->at(1));
3669 Load(args->at(2));
3670 Load(args->at(3));
3671
3672 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3673 frame_->EmitPush(r0);
3674}
3675
3676
ager@chromium.org5c838252010-02-19 08:53:10 +00003677void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
3678 ASSERT_EQ(args->length(), 1);
3679
3680 // Load the argument on the stack and jump to the runtime.
3681 Load(args->at(0));
3682
fschneider@chromium.org086aac62010-03-17 13:18:24 +00003683 NumberToStringStub stub;
3684 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003685 frame_->EmitPush(r0);
3686}
3687
3688
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003689void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
3690 ASSERT_EQ(args->length(), 1);
3691 // Load the argument on the stack and jump to the runtime.
3692 Load(args->at(0));
3693 frame_->CallRuntime(Runtime::kMath_sin, 1);
3694 frame_->EmitPush(r0);
3695}
3696
3697
3698void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
3699 ASSERT_EQ(args->length(), 1);
3700 // Load the argument on the stack and jump to the runtime.
3701 Load(args->at(0));
3702 frame_->CallRuntime(Runtime::kMath_cos, 1);
3703 frame_->EmitPush(r0);
3704}
3705
3706
ager@chromium.org7c537e22008-10-16 08:43:32 +00003707void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003708 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003709 ASSERT(args->length() == 2);
3710
3711 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003712 LoadAndSpill(args->at(0));
3713 LoadAndSpill(args->at(1));
3714 frame_->EmitPop(r0);
3715 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003716 __ cmp(r0, Operand(r1));
3717 cc_reg_ = eq;
3718}
3719
3720
ager@chromium.org7c537e22008-10-16 08:43:32 +00003721void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003722#ifdef DEBUG
3723 int original_height = frame_->height();
3724#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003725 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 if (CheckForInlineRuntimeCall(node)) {
3727 ASSERT((has_cc() && frame_->height() == original_height) ||
3728 (!has_cc() && frame_->height() == original_height + 1));
3729 return;
3730 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003731
3732 ZoneList<Expression*>* args = node->arguments();
3733 Comment cmnt(masm_, "[ CallRuntime");
3734 Runtime::Function* function = node->function();
3735
ager@chromium.org41826e72009-03-30 13:30:57 +00003736 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003737 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00003738 // Push the builtins object found in the current global object.
3739 __ ldr(r1, GlobalObject());
3740 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003741 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003742 }
mads.s.ager31e71382008-08-13 09:32:07 +00003743
ager@chromium.org41826e72009-03-30 13:30:57 +00003744 // Push the arguments ("left-to-right").
3745 int arg_count = args->length();
3746 for (int i = 0; i < arg_count; i++) {
3747 LoadAndSpill(args->at(i));
3748 }
mads.s.ager31e71382008-08-13 09:32:07 +00003749
ager@chromium.org41826e72009-03-30 13:30:57 +00003750 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00003752 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003753 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3754 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003755 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003756 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003757 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003758 } else {
3759 // Call the C runtime function.
3760 frame_->CallRuntime(function, arg_count);
3761 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003763 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764}
3765
3766
ager@chromium.org7c537e22008-10-16 08:43:32 +00003767void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768#ifdef DEBUG
3769 int original_height = frame_->height();
3770#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003771 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 Comment cmnt(masm_, "[ UnaryOperation");
3773
3774 Token::Value op = node->op();
3775
3776 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003777 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003778 false_target(),
3779 true_target(),
3780 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003781 // LoadCondition may (and usually does) leave a test and branch to
3782 // be emitted by the caller. In that case, negate the condition.
3783 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784
3785 } else if (op == Token::DELETE) {
3786 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003787 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 LoadAndSpill(property->obj());
3790 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003791 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792
mads.s.ager31e71382008-08-13 09:32:07 +00003793 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003794 Slot* slot = variable->slot();
3795 if (variable->is_global()) {
3796 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003797 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003798 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003799 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800
3801 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3802 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003803 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003804 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003805 frame_->EmitPush(r0);
3806 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003808 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003809 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003811 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003812
mads.s.ager31e71382008-08-13 09:32:07 +00003813 } else {
3814 // Default: Result of deleting non-global, not dynamically
3815 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003816 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003817 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818
3819 } else {
3820 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003821 LoadAndSpill(node->expression()); // may have side-effects
3822 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003823 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003825 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003826
3827 } else if (op == Token::TYPEOF) {
3828 // Special case for loading the typeof expression; see comment on
3829 // LoadTypeofExpression().
3830 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 frame_->CallRuntime(Runtime::kTypeof, 1);
3832 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833
3834 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003835 bool overwrite =
3836 (node->expression()->AsBinaryOperation() != NULL &&
3837 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003838 LoadAndSpill(node->expression());
3839 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 switch (op) {
3841 case Token::NOT:
3842 case Token::DELETE:
3843 case Token::TYPEOF:
3844 UNREACHABLE(); // handled above
3845 break;
3846
3847 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003848 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 break;
3851 }
3852
3853 case Token::BIT_NOT: {
3854 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003855 JumpTarget smi_label;
3856 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003858 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003860 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
3861 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003862 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003863
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865 __ mvn(r0, Operand(r0));
3866 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003867 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868 break;
3869 }
3870
3871 case Token::VOID:
3872 // since the stack top is cached in r0, popping and then
3873 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003874 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 break;
3876
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003877 case Token::ADD: {
3878 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003879 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003880 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881 continue_label.Branch(eq);
3882 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003883 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003884 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003885 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003886 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 default:
3888 UNREACHABLE();
3889 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003890 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003892 ASSERT(!has_valid_frame() ||
3893 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895}
3896
3897
ager@chromium.org7c537e22008-10-16 08:43:32 +00003898void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003899#ifdef DEBUG
3900 int original_height = frame_->height();
3901#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003902 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 Comment cmnt(masm_, "[ CountOperation");
3904
3905 bool is_postfix = node->is_postfix();
3906 bool is_increment = node->op() == Token::INC;
3907
3908 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3909 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3910
3911 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003912 if (is_postfix) {
3913 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003914 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003915 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003917 // A constant reference is not saved to, so a constant reference is not a
3918 // compound assignment reference.
3919 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 if (target.is_illegal()) {
3921 // Spoof the virtual frame to have the expected height (one higher
3922 // than on entry).
3923 if (!is_postfix) {
3924 __ mov(r0, Operand(Smi::FromInt(0)));
3925 frame_->EmitPush(r0);
3926 }
3927 ASSERT(frame_->height() == original_height + 1);
3928 return;
3929 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003930 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003931 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003933 JumpTarget slow;
3934 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935
3936 // Load the value (1) into register r1.
3937 __ mov(r1, Operand(Smi::FromInt(1)));
3938
3939 // Check for smi operand.
3940 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003941 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942
3943 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003944 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003946 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947
3948 // Perform optimistic increment/decrement.
3949 if (is_increment) {
3950 __ add(r0, r0, Operand(r1), SetCC);
3951 } else {
3952 __ sub(r0, r0, Operand(r1), SetCC);
3953 }
3954
3955 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003956 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957
3958 // Revert optimistic increment/decrement.
3959 if (is_increment) {
3960 __ sub(r0, r0, Operand(r1));
3961 } else {
3962 __ add(r0, r0, Operand(r1));
3963 }
3964
3965 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003966 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003967 {
3968 // Convert the operand to a number.
3969 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003970 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003971 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003973 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003974 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975 }
3976
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003977 // Compute the new value.
3978 __ mov(r1, Operand(Smi::FromInt(1)));
3979 frame_->EmitPush(r0);
3980 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003981 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003982 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003984 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003985 }
3986
3987 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003988 exit.Bind();
3989 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003990 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 }
3992
3993 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003994 if (is_postfix) frame_->EmitPop(r0);
3995 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996}
3997
3998
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003999void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004000 // According to ECMA-262 section 11.11, page 58, the binary logical
4001 // operators must yield the result of one of the two expressions
4002 // before any ToBoolean() conversions. This means that the value
4003 // produced by a && or || operator is not necessarily a boolean.
4004
4005 // NOTE: If the left hand side produces a materialized value (not in
4006 // the CC register), we force the right hand side to do the
4007 // same. This is necessary because we may have to branch to the exit
4008 // after evaluating the left hand side (due to the shortcut
4009 // semantics), but the compiler must (statically) know if the result
4010 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004011 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004012 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004013 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004014 &is_true,
4015 false_target(),
4016 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004017 if (has_valid_frame() && !has_cc()) {
4018 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004019 JumpTarget pop_and_continue;
4020 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004022 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004023 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 // Avoid popping the result if it converts to 'false' using the
4025 // standard ToBoolean() conversion as described in ECMA-262,
4026 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004027 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004028 Branch(false, &exit);
4029
4030 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 pop_and_continue.Bind();
4032 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033
4034 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004035 is_true.Bind();
4036 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037
4038 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004039 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004040 } else if (has_cc() || is_true.is_linked()) {
4041 // The left-hand side is either (a) partially compiled to
4042 // control flow with a final branch left to emit or (b) fully
4043 // compiled to control flow and possibly true.
4044 if (has_cc()) {
4045 Branch(false, false_target());
4046 }
4047 is_true.Bind();
4048 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004049 true_target(),
4050 false_target(),
4051 false);
4052 } else {
4053 // Nothing to do.
4054 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004055 }
4056
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004057 } else {
4058 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004059 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004060 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004061 true_target(),
4062 &is_false,
4063 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004064 if (has_valid_frame() && !has_cc()) {
4065 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004066 JumpTarget pop_and_continue;
4067 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004068
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004069 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 // Avoid popping the result if it converts to 'true' using the
4072 // standard ToBoolean() conversion as described in ECMA-262,
4073 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004074 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075 Branch(true, &exit);
4076
4077 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078 pop_and_continue.Bind();
4079 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080
4081 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 is_false.Bind();
4083 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084
4085 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004086 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004087 } else if (has_cc() || is_false.is_linked()) {
4088 // The left-hand side is either (a) partially compiled to
4089 // control flow with a final branch left to emit or (b) fully
4090 // compiled to control flow and possibly false.
4091 if (has_cc()) {
4092 Branch(true, true_target());
4093 }
4094 is_false.Bind();
4095 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004096 true_target(),
4097 false_target(),
4098 false);
4099 } else {
4100 // Nothing to do.
4101 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004102 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004103 }
4104}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004106
4107void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4108#ifdef DEBUG
4109 int original_height = frame_->height();
4110#endif
4111 VirtualFrame::SpilledScope spilled_scope;
4112 Comment cmnt(masm_, "[ BinaryOperation");
4113
4114 if (node->op() == Token::AND || node->op() == Token::OR) {
4115 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116 } else {
4117 // Optimize for the case where (at least) one of the expressions
4118 // is a literal small integer.
4119 Literal* lliteral = node->left()->AsLiteral();
4120 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004121 // NOTE: The code below assumes that the slow cases (calls to runtime)
4122 // never return a constant/immutable object.
4123 bool overwrite_left =
4124 (node->left()->AsBinaryOperation() != NULL &&
4125 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4126 bool overwrite_right =
4127 (node->right()->AsBinaryOperation() != NULL &&
4128 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004129
4130 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004131 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004132 SmiOperation(node->op(),
4133 rliteral->handle(),
4134 false,
4135 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004136
4137 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004139 SmiOperation(node->op(),
4140 lliteral->handle(),
4141 true,
4142 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143
4144 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004145 OverwriteMode overwrite_mode = NO_OVERWRITE;
4146 if (overwrite_left) {
4147 overwrite_mode = OVERWRITE_LEFT;
4148 } else if (overwrite_right) {
4149 overwrite_mode = OVERWRITE_RIGHT;
4150 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004151 LoadAndSpill(node->left());
4152 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004153 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004155 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004157 ASSERT(!has_valid_frame() ||
4158 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004160}
4161
4162
ager@chromium.org7c537e22008-10-16 08:43:32 +00004163void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004164#ifdef DEBUG
4165 int original_height = frame_->height();
4166#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004167 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004168 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004169 frame_->EmitPush(r0);
4170 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004171}
4172
4173
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004175#ifdef DEBUG
4176 int original_height = frame_->height();
4177#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004178 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004179 Comment cmnt(masm_, "[ CompareOperation");
4180
4181 // Get the expressions from the node.
4182 Expression* left = node->left();
4183 Expression* right = node->right();
4184 Token::Value op = node->op();
4185
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004186 // To make null checks efficient, we check if either left or right is the
4187 // literal 'null'. If so, we optimize the code by inlining a null check
4188 // instead of calling the (very) general runtime routine for checking
4189 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004190 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004191 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004192 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004193 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004194 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4195 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004197 LoadAndSpill(left_is_null ? right : left);
4198 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004199 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4200 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004202 // The 'null' value is only equal to 'undefined' if using non-strict
4203 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004204 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004205 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004206
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004207 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4208 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004209 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004211 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004212 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004214 // It can be an undetectable object.
4215 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4216 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4217 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4218 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004219 }
4220
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004221 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004222 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004223 return;
4224 }
4225 }
4226
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004227 // To make typeof testing for natives implemented in JavaScript really
4228 // efficient, we generate special code for expressions of the form:
4229 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004230 UnaryOperation* operation = left->AsUnaryOperation();
4231 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4232 (operation != NULL && operation->op() == Token::TYPEOF) &&
4233 (right->AsLiteral() != NULL &&
4234 right->AsLiteral()->handle()->IsString())) {
4235 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4236
mads.s.ager31e71382008-08-13 09:32:07 +00004237 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004239 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004240
4241 if (check->Equals(Heap::number_symbol())) {
4242 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004244 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004245 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4246 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 cc_reg_ = eq;
4248
4249 } else if (check->Equals(Heap::string_symbol())) {
4250 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004251 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252
4253 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4254
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004255 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004256 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4257 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4258 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004259 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260
4261 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4262 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4263 cc_reg_ = lt;
4264
4265 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004266 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4267 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004268 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004269 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4270 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004271 cc_reg_ = eq;
4272
4273 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004274 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4275 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004276 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004277
4278 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004279 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004280
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004281 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004282 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4283 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4284 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4285 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4286
4287 cc_reg_ = eq;
4288
4289 } else if (check->Equals(Heap::function_symbol())) {
4290 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004291 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004292 Register map_reg = r2;
4293 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4294 true_target()->Branch(eq);
4295 // Regular expressions are callable so typeof == 'function'.
4296 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004297 cc_reg_ = eq;
4298
4299 } else if (check->Equals(Heap::object_symbol())) {
4300 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004302
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004303 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4304 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004305 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004307 Register map_reg = r2;
4308 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4309 false_target()->Branch(eq);
4310
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004311 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004312 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4314 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004315 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004317 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4318 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004319 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004320 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004321 cc_reg_ = le;
4322
4323 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004324 // Uncommon case: typeof testing against a string literal that is
4325 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004326 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004327 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004328 ASSERT(!has_valid_frame() ||
4329 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330 return;
4331 }
4332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004333 switch (op) {
4334 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004335 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004336 break;
4337
4338 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004339 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004340 break;
4341
4342 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004343 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004344 break;
4345
4346 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004347 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004348 break;
4349
4350 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004351 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004352 break;
4353
4354 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004355 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004356 break;
4357
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004358 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004359 LoadAndSpill(left);
4360 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004361 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004362 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004363 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004365
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004366 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004367 LoadAndSpill(left);
4368 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004369 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004370 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004371 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004372 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004373 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004374 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004376
4377 default:
4378 UNREACHABLE();
4379 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004380 ASSERT((has_cc() && frame_->height() == original_height) ||
4381 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004382}
4383
4384
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004385void CodeGenerator::EmitKeyedLoad(bool is_global) {
4386 Comment cmnt(masm_, "[ Load from keyed Property");
4387 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4388 RelocInfo::Mode rmode = is_global
4389 ? RelocInfo::CODE_TARGET_CONTEXT
4390 : RelocInfo::CODE_TARGET;
4391 frame_->CallCodeObject(ic, rmode, 0);
4392}
4393
4394
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004395#ifdef DEBUG
4396bool CodeGenerator::HasValidEntryRegisters() { return true; }
4397#endif
4398
4399
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004400#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004401#define __ ACCESS_MASM(masm)
4402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004403
ager@chromium.org7c537e22008-10-16 08:43:32 +00004404Handle<String> Reference::GetName() {
4405 ASSERT(type_ == NAMED);
4406 Property* property = expression_->AsProperty();
4407 if (property == NULL) {
4408 // Global variable reference treated as a named property reference.
4409 VariableProxy* proxy = expression_->AsVariableProxy();
4410 ASSERT(proxy->AsVariable() != NULL);
4411 ASSERT(proxy->AsVariable()->is_global());
4412 return proxy->name();
4413 } else {
4414 Literal* raw_name = property->key()->AsLiteral();
4415 ASSERT(raw_name != NULL);
4416 return Handle<String>(String::cast(*raw_name->handle()));
4417 }
4418}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004419
ager@chromium.org7c537e22008-10-16 08:43:32 +00004420
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004421void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004422 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004423 ASSERT(!is_illegal());
4424 ASSERT(!cgen_->has_cc());
4425 MacroAssembler* masm = cgen_->masm();
4426 Property* property = expression_->AsProperty();
4427 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004428 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004429 }
4430
4431 switch (type_) {
4432 case SLOT: {
4433 Comment cmnt(masm, "[ Load from Slot");
4434 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4435 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004436 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004437 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004438 }
4439
ager@chromium.org7c537e22008-10-16 08:43:32 +00004440 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004441 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004442 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004443 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004444 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004445 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4446 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004447 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004448 ASSERT(var == NULL || var->is_global());
4449 RelocInfo::Mode rmode = (var == NULL)
4450 ? RelocInfo::CODE_TARGET
4451 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004452 frame->CallCodeObject(ic, rmode, 0);
4453 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004454 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004455 }
4456
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004457 case KEYED: {
4458 // TODO(181): Implement inlined version of array indexing once
4459 // loop nesting is properly tracked on ARM.
4460 ASSERT(property != NULL);
4461 Variable* var = expression_->AsVariableProxy()->AsVariable();
4462 ASSERT(var == NULL || var->is_global());
4463 cgen_->EmitKeyedLoad(var != NULL);
4464 cgen_->frame()->EmitPush(r0);
4465 break;
4466 }
4467
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004468 default:
4469 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004470 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004471
4472 if (!persist_after_get_) {
4473 cgen_->UnloadReference(this);
4474 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004475}
4476
4477
ager@chromium.org7c537e22008-10-16 08:43:32 +00004478void Reference::SetValue(InitState init_state) {
4479 ASSERT(!is_illegal());
4480 ASSERT(!cgen_->has_cc());
4481 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004482 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004483 Property* property = expression_->AsProperty();
4484 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004485 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004486 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004487
ager@chromium.org7c537e22008-10-16 08:43:32 +00004488 switch (type_) {
4489 case SLOT: {
4490 Comment cmnt(masm, "[ Store to Slot");
4491 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004492 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004493 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004494 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004495 }
4496
ager@chromium.org7c537e22008-10-16 08:43:32 +00004497 case NAMED: {
4498 Comment cmnt(masm, "[ Store to named Property");
4499 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004500 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004501 Handle<String> name(GetName());
4502
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004503 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004504 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004505 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004506 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004507 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004508 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004509 break;
4510 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004511
ager@chromium.org7c537e22008-10-16 08:43:32 +00004512 case KEYED: {
4513 Comment cmnt(masm, "[ Store to keyed Property");
4514 Property* property = expression_->AsProperty();
4515 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004516 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004517
4518 // Call IC code.
4519 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004520 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004521 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004522 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004523 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004524 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004525 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004526
4527 default:
4528 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004529 }
4530}
4531
4532
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004533void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004534 // Create a new closure from the given function info in new
4535 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004536 Label gc;
4537
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004538 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004539 __ pop(r3);
4540
4541 // Attempt to allocate new JSFunction in new space.
4542 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4543 r0,
4544 r1,
4545 r2,
4546 &gc,
4547 TAG_OBJECT);
4548
4549 // Compute the function map in the current global context and set that
4550 // as the map of the allocated object.
4551 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4552 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4553 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4554 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4555
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004556 // Initialize the rest of the function. We don't have to update the
4557 // write barrier because the allocated object is in new space.
4558 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
4559 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
4560 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4561 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
4562 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
4563 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
4564 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
4565 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004566
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004567 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004568 __ Ret();
4569
4570 // Create a new closure through the slower runtime call.
4571 __ bind(&gc);
4572 __ push(cp);
4573 __ push(r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004574 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004575}
4576
4577
4578void FastNewContextStub::Generate(MacroAssembler* masm) {
4579 // Try to allocate the context in new space.
4580 Label gc;
4581 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4582
4583 // Attempt to allocate the context in new space.
4584 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4585 r0,
4586 r1,
4587 r2,
4588 &gc,
4589 TAG_OBJECT);
4590
4591 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00004592 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004593
4594 // Setup the object header.
4595 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4596 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4597 __ mov(r2, Operand(length));
4598 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4599
4600 // Setup the fixed slots.
4601 __ mov(r1, Operand(Smi::FromInt(0)));
4602 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4603 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4604 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4605 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4606
4607 // Copy the global object from the surrounding context.
4608 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4609 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4610
4611 // Initialize the rest of the slots to undefined.
4612 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4613 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4614 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4615 }
4616
4617 // Remove the on-stack argument and return.
4618 __ mov(cp, r0);
4619 __ pop();
4620 __ Ret();
4621
4622 // Need to collect. Call into runtime system.
4623 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004624 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004625}
4626
4627
ager@chromium.org5c838252010-02-19 08:53:10 +00004628void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
4629 // Stack layout on entry:
4630 //
4631 // [sp]: constant elements.
4632 // [sp + kPointerSize]: literal index.
4633 // [sp + (2 * kPointerSize)]: literals array.
4634
4635 // All sizes here are multiples of kPointerSize.
4636 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
4637 int size = JSArray::kSize + elements_size;
4638
4639 // Load boilerplate object into r3 and check if we need to create a
4640 // boilerplate.
4641 Label slow_case;
4642 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4643 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
4644 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4645 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4646 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4647 __ cmp(r3, ip);
4648 __ b(eq, &slow_case);
4649
4650 // Allocate both the JS array and the elements array in one big
4651 // allocation. This avoids multiple limit checks.
4652 __ AllocateInNewSpace(size / kPointerSize,
4653 r0,
4654 r1,
4655 r2,
4656 &slow_case,
4657 TAG_OBJECT);
4658
4659 // Copy the JS array part.
4660 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
4661 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
4662 __ ldr(r1, FieldMemOperand(r3, i));
4663 __ str(r1, FieldMemOperand(r0, i));
4664 }
4665 }
4666
4667 if (length_ > 0) {
4668 // Get hold of the elements array of the boilerplate and setup the
4669 // elements pointer in the resulting object.
4670 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
4671 __ add(r2, r0, Operand(JSArray::kSize));
4672 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
4673
4674 // Copy the elements array.
4675 for (int i = 0; i < elements_size; i += kPointerSize) {
4676 __ ldr(r1, FieldMemOperand(r3, i));
4677 __ str(r1, FieldMemOperand(r2, i));
4678 }
4679 }
4680
4681 // Return and remove the on-stack parameters.
4682 __ add(sp, sp, Operand(3 * kPointerSize));
4683 __ Ret();
4684
4685 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004686 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004687}
4688
4689
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004690// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4691// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4692// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4693// scratch register. Destroys the source register. No GC occurs during this
4694// stub so you don't have to set up the frame.
4695class ConvertToDoubleStub : public CodeStub {
4696 public:
4697 ConvertToDoubleStub(Register result_reg_1,
4698 Register result_reg_2,
4699 Register source_reg,
4700 Register scratch_reg)
4701 : result1_(result_reg_1),
4702 result2_(result_reg_2),
4703 source_(source_reg),
4704 zeros_(scratch_reg) { }
4705
4706 private:
4707 Register result1_;
4708 Register result2_;
4709 Register source_;
4710 Register zeros_;
4711
4712 // Minor key encoding in 16 bits.
4713 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4714 class OpBits: public BitField<Token::Value, 2, 14> {};
4715
4716 Major MajorKey() { return ConvertToDouble; }
4717 int MinorKey() {
4718 // Encode the parameters in a unique 16 bit value.
4719 return result1_.code() +
4720 (result2_.code() << 4) +
4721 (source_.code() << 8) +
4722 (zeros_.code() << 12);
4723 }
4724
4725 void Generate(MacroAssembler* masm);
4726
4727 const char* GetName() { return "ConvertToDoubleStub"; }
4728
4729#ifdef DEBUG
4730 void Print() { PrintF("ConvertToDoubleStub\n"); }
4731#endif
4732};
4733
4734
4735void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4736#ifndef BIG_ENDIAN_FLOATING_POINT
4737 Register exponent = result1_;
4738 Register mantissa = result2_;
4739#else
4740 Register exponent = result2_;
4741 Register mantissa = result1_;
4742#endif
4743 Label not_special;
4744 // Convert from Smi to integer.
4745 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4746 // Move sign bit from source to destination. This works because the sign bit
4747 // in the exponent word of the double has the same position and polarity as
4748 // the 2's complement sign bit in a Smi.
4749 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4750 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4751 // Subtract from 0 if source was negative.
4752 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004753
4754 // We have -1, 0 or 1, which we treat specially. Register source_ contains
4755 // absolute value: it is either equal to 1 (special case of -1 and 1),
4756 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004757 __ cmp(source_, Operand(1));
4758 __ b(gt, &not_special);
4759
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004760 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4761 static const uint32_t exponent_word_for_1 =
4762 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004763 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004764 // 1, 0 and -1 all have 0 for the second word.
4765 __ mov(mantissa, Operand(0));
4766 __ Ret();
4767
4768 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004769 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004770 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004771 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004772 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004773 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004774 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4775 __ orr(exponent,
4776 exponent,
4777 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4778 // Shift up the source chopping the top bit off.
4779 __ add(zeros_, zeros_, Operand(1));
4780 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4781 __ mov(source_, Operand(source_, LSL, zeros_));
4782 // Compute lower part of fraction (last 12 bits).
4783 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4784 // And the top (top 20 bits).
4785 __ orr(exponent,
4786 exponent,
4787 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4788 __ Ret();
4789}
4790
4791
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004792// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004793void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004794 Label max_negative_int;
4795 // the_int_ has the answer which is a signed int32 but not a Smi.
4796 // We test for the special value that has a different exponent. This test
4797 // has the neat side effect of setting the flags according to the sign.
4798 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004799 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004800 __ b(eq, &max_negative_int);
4801 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4802 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4803 uint32_t non_smi_exponent =
4804 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4805 __ mov(scratch_, Operand(non_smi_exponent));
4806 // Set the sign bit in scratch_ if the value was negative.
4807 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4808 // Subtract from 0 if the value was negative.
4809 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4810 // We should be masking the implict first digit of the mantissa away here,
4811 // but it just ends up combining harmlessly with the last digit of the
4812 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4813 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4814 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4815 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4816 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4817 __ str(scratch_, FieldMemOperand(the_heap_number_,
4818 HeapNumber::kExponentOffset));
4819 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4820 __ str(scratch_, FieldMemOperand(the_heap_number_,
4821 HeapNumber::kMantissaOffset));
4822 __ Ret();
4823
4824 __ bind(&max_negative_int);
4825 // The max negative int32 is stored as a positive number in the mantissa of
4826 // a double because it uses a sign bit instead of using two's complement.
4827 // The actual mantissa bits stored are all 0 because the implicit most
4828 // significant 1 bit is not stored.
4829 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4830 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4831 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4832 __ mov(ip, Operand(0));
4833 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4834 __ Ret();
4835}
4836
4837
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004838// Handle the case where the lhs and rhs are the same object.
4839// Equality is almost reflexive (everything but NaN), so this is a test
4840// for "identity and not NaN".
4841static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4842 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004843 Condition cc,
4844 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004845 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004846 Label heap_number, return_equal;
4847 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004848 __ cmp(r0, Operand(r1));
4849 __ b(ne, &not_identical);
4850
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004851 // The two objects are identical. If we know that one of them isn't NaN then
4852 // we now know they test equal.
4853 if (cc != eq || !never_nan_nan) {
4854 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004855
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004856 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4857 // so we do the second best thing - test it ourselves.
4858 // They are both equal and they are not both Smis so both of them are not
4859 // Smis. If it's not a heap number, then return equal.
4860 if (cc == lt || cc == gt) {
4861 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004862 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004863 } else {
4864 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4865 __ b(eq, &heap_number);
4866 // Comparing JS objects with <=, >= is complicated.
4867 if (cc != eq) {
4868 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4869 __ b(ge, slow);
4870 // Normally here we fall through to return_equal, but undefined is
4871 // special: (undefined == undefined) == true, but
4872 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4873 if (cc == le || cc == ge) {
4874 __ cmp(r4, Operand(ODDBALL_TYPE));
4875 __ b(ne, &return_equal);
4876 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4877 __ cmp(r0, Operand(r2));
4878 __ b(ne, &return_equal);
4879 if (cc == le) {
4880 // undefined <= undefined should fail.
4881 __ mov(r0, Operand(GREATER));
4882 } else {
4883 // undefined >= undefined should fail.
4884 __ mov(r0, Operand(LESS));
4885 }
4886 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004887 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004888 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004889 }
4890 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004891
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004892 __ bind(&return_equal);
4893 if (cc == lt) {
4894 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4895 } else if (cc == gt) {
4896 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4897 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004898 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004899 }
4900 __ mov(pc, Operand(lr)); // Return.
4901
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004902 if (cc != eq || !never_nan_nan) {
4903 // For less and greater we don't have to check for NaN since the result of
4904 // x < x is false regardless. For the others here is some code to check
4905 // for NaN.
4906 if (cc != lt && cc != gt) {
4907 __ bind(&heap_number);
4908 // It is a heap number, so return non-equal if it's NaN and equal if it's
4909 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004910
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004911 // The representation of NaN values has all exponent bits (52..62) set,
4912 // and not all mantissa bits (0..51) clear.
4913 // Read top bits of double representation (second word of value).
4914 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4915 // Test that exponent bits are all set.
4916 __ and_(r3, r2, Operand(exp_mask_reg));
4917 __ cmp(r3, Operand(exp_mask_reg));
4918 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004919
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004920 // Shift out flag and all exponent bits, retaining only mantissa.
4921 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4922 // Or with all low-bits of mantissa.
4923 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4924 __ orr(r0, r3, Operand(r2), SetCC);
4925 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004926 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
4927 // not (it's a NaN). For <= and >= we need to load r0 with the failing
4928 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004929 if (cc != eq) {
4930 // All-zero means Infinity means equal.
4931 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4932 if (cc == le) {
4933 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4934 } else {
4935 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4936 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004937 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004938 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004939 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004940 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004941 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004942
4943 __ bind(&not_identical);
4944}
4945
4946
4947// See comment at call site.
4948static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004949 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004950 Label* slow,
4951 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004952 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004953 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004954 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004955
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004956 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004957 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4958 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004959 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004960 // succeed. Return non-equal (r0 is already not zero)
4961 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4962 } else {
4963 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4964 // the runtime.
4965 __ b(ne, slow);
4966 }
4967
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004968 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004969 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004970 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004971 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004972 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
4973 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004974 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004975 // Load the double from rhs, tagged HeapNumber r0, to d6.
4976 __ sub(r7, r0, Operand(kHeapObjectTag));
4977 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004978 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004979 __ push(lr);
4980 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004981 __ mov(r7, Operand(r1));
4982 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4983 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004984 // Load rhs to a double in r0, r1.
4985 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4986 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4987 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004988 }
4989
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004990 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004991 // since it's a smi.
4992 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004993
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004994 __ bind(&rhs_is_smi);
4995 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004996 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4997 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004998 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004999 // succeed. Return non-equal.
5000 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
5001 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5002 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005003 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005004 // the runtime.
5005 __ b(ne, slow);
5006 }
5007
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005008 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005009 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005010 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005011 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005012 // Load the double from lhs, tagged HeapNumber r1, to d7.
5013 __ sub(r7, r1, Operand(kHeapObjectTag));
5014 __ vldr(d7, r7, HeapNumber::kValueOffset);
5015 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5016 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005017 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005018 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005019 __ push(lr);
5020 // Load lhs to a double in r2, r3.
5021 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5022 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5023 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005024 __ mov(r7, Operand(r0));
5025 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5026 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005027 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005028 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005029 // Fall through to both_loaded_as_doubles.
5030}
5031
5032
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005033void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005034 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005035 Register rhs_exponent = exp_first ? r0 : r1;
5036 Register lhs_exponent = exp_first ? r2 : r3;
5037 Register rhs_mantissa = exp_first ? r1 : r0;
5038 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005039 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005040 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005041
5042 Register exp_mask_reg = r5;
5043
5044 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005045 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5046 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005047 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005048 __ mov(r4,
5049 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5050 SetCC);
5051 __ b(ne, &one_is_nan);
5052 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005053 __ b(ne, &one_is_nan);
5054
5055 __ bind(lhs_not_nan);
5056 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5057 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5058 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5059 __ cmp(r4, Operand(exp_mask_reg));
5060 __ b(ne, &neither_is_nan);
5061 __ mov(r4,
5062 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5063 SetCC);
5064 __ b(ne, &one_is_nan);
5065 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005066 __ b(eq, &neither_is_nan);
5067
5068 __ bind(&one_is_nan);
5069 // NaN comparisons always fail.
5070 // Load whatever we need in r0 to make the comparison fail.
5071 if (cc == lt || cc == le) {
5072 __ mov(r0, Operand(GREATER));
5073 } else {
5074 __ mov(r0, Operand(LESS));
5075 }
5076 __ mov(pc, Operand(lr)); // Return.
5077
5078 __ bind(&neither_is_nan);
5079}
5080
5081
5082// See comment at call site.
5083static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5084 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005085 Register rhs_exponent = exp_first ? r0 : r1;
5086 Register lhs_exponent = exp_first ? r2 : r3;
5087 Register rhs_mantissa = exp_first ? r1 : r0;
5088 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005089
5090 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5091 if (cc == eq) {
5092 // Doubles are not equal unless they have the same bit pattern.
5093 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005094 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5095 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005096 // Return non-zero if the numbers are unequal.
5097 __ mov(pc, Operand(lr), LeaveCC, ne);
5098
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005099 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005100 // If exponents are equal then return 0.
5101 __ mov(pc, Operand(lr), LeaveCC, eq);
5102
5103 // Exponents are unequal. The only way we can return that the numbers
5104 // are equal is if one is -0 and the other is 0. We already dealt
5105 // with the case where both are -0 or both are 0.
5106 // We start by seeing if the mantissas (that are equal) or the bottom
5107 // 31 bits of the rhs exponent are non-zero. If so we return not
5108 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005109 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005110 __ mov(r0, Operand(r4), LeaveCC, ne);
5111 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5112 // Now they are equal if and only if the lhs exponent is zero in its
5113 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005114 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005115 __ mov(pc, Operand(lr));
5116 } else {
5117 // Call a native function to do a comparison between two non-NaNs.
5118 // Call C routine that may not cause GC or other trouble.
5119 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5120 __ Jump(r5); // Tail call.
5121 }
5122}
5123
5124
5125// See comment at call site.
5126static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5127 // If either operand is a JSObject or an oddball value, then they are
5128 // not equal since their pointers are different.
5129 // There is no test for undetectability in strict equality.
5130 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5131 Label first_non_object;
5132 // Get the type of the first operand into r2 and compare it with
5133 // FIRST_JS_OBJECT_TYPE.
5134 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5135 __ b(lt, &first_non_object);
5136
5137 // Return non-zero (r0 is not zero)
5138 Label return_not_equal;
5139 __ bind(&return_not_equal);
5140 __ mov(pc, Operand(lr)); // Return.
5141
5142 __ bind(&first_non_object);
5143 // Check for oddballs: true, false, null, undefined.
5144 __ cmp(r2, Operand(ODDBALL_TYPE));
5145 __ b(eq, &return_not_equal);
5146
5147 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5148 __ b(ge, &return_not_equal);
5149
5150 // Check for oddballs: true, false, null, undefined.
5151 __ cmp(r3, Operand(ODDBALL_TYPE));
5152 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005153
5154 // Now that we have the types we might as well check for symbol-symbol.
5155 // Ensure that no non-strings have the symbol bit set.
5156 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5157 ASSERT(kSymbolTag != 0);
5158 __ and_(r2, r2, Operand(r3));
5159 __ tst(r2, Operand(kIsSymbolMask));
5160 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005161}
5162
5163
5164// See comment at call site.
5165static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5166 Label* both_loaded_as_doubles,
5167 Label* not_heap_numbers,
5168 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005169 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005170 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005171 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5172 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005173 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5174
5175 // Both are heap numbers. Load them up then jump to the code we have
5176 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005177 if (CpuFeatures::IsSupported(VFP3)) {
5178 CpuFeatures::Scope scope(VFP3);
5179 __ sub(r7, r0, Operand(kHeapObjectTag));
5180 __ vldr(d6, r7, HeapNumber::kValueOffset);
5181 __ sub(r7, r1, Operand(kHeapObjectTag));
5182 __ vldr(d7, r7, HeapNumber::kValueOffset);
5183 } else {
5184 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5185 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5186 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5187 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5188 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005189 __ jmp(both_loaded_as_doubles);
5190}
5191
5192
5193// Fast negative check for symbol-to-symbol equality.
5194static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5195 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005196 // Ensure that no non-strings have the symbol bit set.
5197 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5198 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005199 __ tst(r2, Operand(kIsSymbolMask));
5200 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005201 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5202 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005203 __ tst(r3, Operand(kIsSymbolMask));
5204 __ b(eq, slow);
5205
5206 // Both are symbols. We already checked they weren't the same pointer
5207 // so they are not equal.
5208 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5209 __ mov(pc, Operand(lr)); // Return.
5210}
5211
5212
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005213void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
5214 Register object,
5215 Register result,
5216 Register scratch1,
5217 Register scratch2,
5218 bool object_is_smi,
5219 Label* not_found) {
5220 // Currently only lookup for smis. Check for smi if object is not known to be
5221 // a smi.
5222 if (!object_is_smi) {
5223 ASSERT(kSmiTag == 0);
5224 __ tst(object, Operand(kSmiTagMask));
5225 __ b(ne, not_found);
5226 }
5227
5228 // Use of registers. Register result is used as a temporary.
5229 Register number_string_cache = result;
5230 Register mask = scratch1;
5231 Register scratch = scratch2;
5232
5233 // Load the number string cache.
5234 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
5235
5236 // Make the hash mask from the length of the number string cache. It
5237 // contains two elements (number and string) for each cache entry.
5238 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
5239 // Divide length by two (length is not a smi).
5240 __ mov(mask, Operand(mask, ASR, 1));
5241 __ sub(mask, mask, Operand(1)); // Make mask.
5242
5243 // Calculate the entry in the number string cache. The hash value in the
5244 // number string cache for smis is just the smi value.
5245 __ and_(scratch, mask, Operand(object, ASR, 1));
5246
5247 // Calculate address of entry in string cache: each entry consists
5248 // of two pointer sized fields.
5249 __ add(scratch,
5250 number_string_cache,
5251 Operand(scratch, LSL, kPointerSizeLog2 + 1));
5252
5253 // Check if the entry is the smi we are looking for.
5254 Register object1 = scratch1;
5255 __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize));
5256 __ cmp(object, object1);
5257 __ b(ne, not_found);
5258
5259 // Get the result from the cache.
5260 __ ldr(result,
5261 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
5262
5263 __ IncrementCounter(&Counters::number_to_string_native,
5264 1,
5265 scratch1,
5266 scratch2);
5267}
5268
5269
5270void NumberToStringStub::Generate(MacroAssembler* masm) {
5271 Label runtime;
5272
5273 __ ldr(r1, MemOperand(sp, 0));
5274
5275 // Generate code to lookup number in the number string cache.
5276 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime);
5277 __ add(sp, sp, Operand(1 * kPointerSize));
5278 __ Ret();
5279
5280 __ bind(&runtime);
5281 // Handle number to string in the runtime system if not found in the cache.
5282 __ TailCallRuntime(Runtime::kNumberToString, 1, 1);
5283}
5284
5285
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005286// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5287// On exit r0 is 0, positive or negative to indicate the result of
5288// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005289void CompareStub::Generate(MacroAssembler* masm) {
5290 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005291 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005292
5293 // NOTICE! This code is only reached after a smi-fast-case check, so
5294 // it is certain that at least one operand isn't a smi.
5295
5296 // Handle the case where the objects are identical. Either returns the answer
5297 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005298 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005299
5300 // If either is a Smi (we know that not both are), then they can only
5301 // be strictly equal if the other is a HeapNumber.
5302 ASSERT_EQ(0, kSmiTag);
5303 ASSERT_EQ(0, Smi::FromInt(0));
5304 __ and_(r2, r0, Operand(r1));
5305 __ tst(r2, Operand(kSmiTagMask));
5306 __ b(ne, &not_smis);
5307 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5308 // 1) Return the answer.
5309 // 2) Go to slow.
5310 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005311 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005312 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005313 // comparison. If VFP3 is supported the double values of the numbers have
5314 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5315 // into r0, r1, r2, and r3.
5316 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005317
5318 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005319 // The arguments have been converted to doubles and stored in d6 and d7, if
5320 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005321 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005322 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005323 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005324 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005325 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005326 __ vcmp(d7, d6);
5327 __ vmrs(pc); // Move vector status bits to normal status bits.
5328 Label nan;
5329 __ b(vs, &nan);
5330 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5331 __ mov(r0, Operand(LESS), LeaveCC, lt);
5332 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5333 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005334
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005335 __ bind(&nan);
5336 // If one of the sides was a NaN then the v flag is set. Load r0 with
5337 // whatever it takes to make the comparison fail, since comparisons with NaN
5338 // always fail.
5339 if (cc_ == lt || cc_ == le) {
5340 __ mov(r0, Operand(GREATER));
5341 } else {
5342 __ mov(r0, Operand(LESS));
5343 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005344 __ mov(pc, Operand(lr));
5345 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005346 // Checks for NaN in the doubles we have loaded. Can return the answer or
5347 // fall through if neither is a NaN. Also binds lhs_not_nan.
5348 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005349 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5350 // answer. Never falls through.
5351 EmitTwoNonNanDoubleComparison(masm, cc_);
5352 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005353
5354 __ bind(&not_smis);
5355 // At this point we know we are dealing with two different objects,
5356 // and neither of them is a Smi. The objects are in r0 and r1.
5357 if (strict_) {
5358 // This returns non-equal for some object types, or falls through if it
5359 // was not lucky.
5360 EmitStrictTwoHeapObjectCompare(masm);
5361 }
5362
5363 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005364 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005365 // Check for heap-number-heap-number comparison. Can jump to slow case,
5366 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5367 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005368 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005369 EmitCheckForTwoHeapNumbers(masm,
5370 &both_loaded_as_doubles,
5371 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005372 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005373
5374 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005375 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5376 // symbols.
5377 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005378 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5379 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005380 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005381 }
5382
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005383 // Check for both being sequential ASCII strings, and inline if that is the
5384 // case.
5385 __ bind(&flat_string_check);
5386
5387 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5388
5389 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5390 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5391 r1,
5392 r0,
5393 r2,
5394 r3,
5395 r4,
5396 r5);
5397 // Never falls through to here.
5398
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005399 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005400
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005401 __ push(r1);
5402 __ push(r0);
5403 // Figure out which native to call and setup the arguments.
5404 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005405 if (cc_ == eq) {
5406 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5407 } else {
5408 native = Builtins::COMPARE;
5409 int ncr; // NaN compare result
5410 if (cc_ == lt || cc_ == le) {
5411 ncr = GREATER;
5412 } else {
5413 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5414 ncr = LESS;
5415 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005416 __ mov(r0, Operand(Smi::FromInt(ncr)));
5417 __ push(r0);
5418 }
5419
5420 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5421 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005422 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005423}
5424
5425
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005426// We fall into this code if the operands were Smis, but the result was
5427// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005428// the operands were not both Smi. The operands are in r0 and r1. In order
5429// to call the C-implemented binary fp operation routines we need to end up
5430// with the double precision floating point operands in r0 and r1 (for the
5431// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005432static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5433 Label* not_smi,
5434 const Builtins::JavaScript& builtin,
5435 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005436 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005437 Label slow, slow_pop_2_first, do_the_call;
5438 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5439 // Smi-smi case (overflow).
5440 // Since both are Smis there is no heap number to overwrite, so allocate.
5441 // The new heap number is in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005442 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005443
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005444 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5445 // using registers d7 and d6 for the double values.
5446 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) &&
5447 Token::MOD != operation;
5448 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005449 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005450 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5451 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005452 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005453 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5454 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005455 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005456 } else {
5457 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5458 __ mov(r7, Operand(r0));
5459 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5460 __ push(lr);
5461 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5462 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5463 __ mov(r7, Operand(r1));
5464 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5465 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5466 __ pop(lr);
5467 }
5468
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005469 __ jmp(&do_the_call); // Tail call. No return.
5470
5471 // We jump to here if something goes wrong (one param is not a number of any
5472 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005473 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005474
5475 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005476 __ push(r1);
5477 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005478
5479 if (Token::ADD == operation) {
5480 // Test for string arguments before calling runtime.
5481 // r1 : first argument
5482 // r0 : second argument
5483 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00005484 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005485
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005486 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005487 __ tst(r1, Operand(kSmiTagMask));
5488 __ b(eq, &not_string1);
5489 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5490 __ b(ge, &not_string1);
5491
5492 // First argument is a a string, test second.
5493 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005494 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005495 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5496 __ b(ge, &string1);
5497
5498 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005499 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
5500 __ TailCallStub(&string_add_stub);
5501
5502 __ bind(&string1_smi2);
5503 // First argument is a string, second is a smi. Try to lookup the number
5504 // string for the smi in the number string cache.
5505 NumberToStringStub::GenerateLookupNumberStringCache(
5506 masm, r0, r2, r4, r5, true, &string1);
5507
5508 // Replace second argument on stack and tailcall string add stub to make
5509 // the result.
5510 __ str(r2, MemOperand(sp, 0));
5511 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005512
5513 // Only first argument is a string.
5514 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005515 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5516
5517 // First argument was not a string, test second.
5518 __ bind(&not_string1);
5519 __ tst(r0, Operand(kSmiTagMask));
5520 __ b(eq, &not_strings);
5521 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5522 __ b(ge, &not_strings);
5523
5524 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005525 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5526
5527 __ bind(&not_strings);
5528 }
5529
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005530 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005531
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005532 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005533 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005534 if (mode == NO_OVERWRITE) {
5535 // In the case where there is no chance of an overwritable float we may as
5536 // well do the allocation immediately while r0 and r1 are untouched.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005537 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005538 }
5539
5540 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005541 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005542 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5543 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005544 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005545 if (mode == OVERWRITE_RIGHT) {
5546 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5547 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005548 if (use_fp_registers) {
5549 CpuFeatures::Scope scope(VFP3);
5550 // Load the double from tagged HeapNumber r0 to d7.
5551 __ sub(r7, r0, Operand(kHeapObjectTag));
5552 __ vldr(d7, r7, HeapNumber::kValueOffset);
5553 } else {
5554 // Calling convention says that second double is in r2 and r3.
5555 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5556 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5557 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005558 __ jmp(&finished_loading_r0);
5559 __ bind(&r0_is_smi);
5560 if (mode == OVERWRITE_RIGHT) {
5561 // We can't overwrite a Smi so get address of new heap number into r5.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005562 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005563 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005564
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005565 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005566 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005567 // Convert smi in r0 to double in d7.
5568 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5569 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005570 __ vcvt_f64_s32(d7, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005571 } else {
5572 // Write Smi from r0 to r3 and r2 in double format.
5573 __ mov(r7, Operand(r0));
5574 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5575 __ push(lr);
5576 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5577 __ pop(lr);
5578 }
5579
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005580 __ bind(&finished_loading_r0);
5581
5582 // Move r1 to a double in r0-r1.
5583 __ tst(r1, Operand(kSmiTagMask));
5584 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5585 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5586 __ b(ne, &slow);
5587 if (mode == OVERWRITE_LEFT) {
5588 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005589 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005590 if (use_fp_registers) {
5591 CpuFeatures::Scope scope(VFP3);
5592 // Load the double from tagged HeapNumber r1 to d6.
5593 __ sub(r7, r1, Operand(kHeapObjectTag));
5594 __ vldr(d6, r7, HeapNumber::kValueOffset);
5595 } else {
5596 // Calling convention says that first double is in r0 and r1.
5597 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
5598 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5599 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005600 __ jmp(&finished_loading_r1);
5601 __ bind(&r1_is_smi);
5602 if (mode == OVERWRITE_LEFT) {
5603 // We can't overwrite a Smi so get address of new heap number into r5.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005604 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005605 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005606
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005607 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005608 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005609 // Convert smi in r1 to double in d6.
5610 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5611 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005612 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005613 } else {
5614 // Write Smi from r1 to r1 and r0 in double format.
5615 __ mov(r7, Operand(r1));
5616 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5617 __ push(lr);
5618 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5619 __ pop(lr);
5620 }
5621
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005622 __ bind(&finished_loading_r1);
5623
5624 __ bind(&do_the_call);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005625 // If we are inlining the operation using VFP3 instructions for
5626 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
5627 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005628 CpuFeatures::Scope scope(VFP3);
5629 // ARMv7 VFP3 instructions to implement
5630 // double precision, add, subtract, multiply, divide.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005631
5632 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005633 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005634 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005635 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005636 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005637 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005638 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005639 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005640 } else {
5641 UNREACHABLE();
5642 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005643 __ sub(r0, r5, Operand(kHeapObjectTag));
5644 __ vstr(d5, r0, HeapNumber::kValueOffset);
5645 __ add(r0, r0, Operand(kHeapObjectTag));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005646 __ mov(pc, lr);
5647 return;
5648 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005649
5650 // If we did not inline the operation, then the arguments are in:
5651 // r0: Left value (least significant part of mantissa).
5652 // r1: Left value (sign, exponent, top of mantissa).
5653 // r2: Right value (least significant part of mantissa).
5654 // r3: Right value (sign, exponent, top of mantissa).
5655 // r5: Address of heap number for result.
5656
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005657 __ push(lr); // For later.
5658 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005659 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005660 // Call C routine that may not cause GC or other trouble.
5661 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005662 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005663 __ pop(r4); // Address of heap number.
5664 __ cmp(r4, Operand(Smi::FromInt(0)));
5665 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005666 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005667#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005668 // Double returned in fp coprocessor register 0 and 1, encoded as register
5669 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5670 // substract the tag from r4.
5671 __ sub(r5, r4, Operand(kHeapObjectTag));
5672 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5673#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005674 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005675 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005676 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005677#endif
5678 __ mov(r0, Operand(r4));
5679 // And we are done.
5680 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005681}
5682
5683
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005684// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005685// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005686// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5687// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005688// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5689// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005690static void GetInt32(MacroAssembler* masm,
5691 Register source,
5692 Register dest,
5693 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005694 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005695 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005696 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005697 // Get exponent word.
5698 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5699 // Get exponent alone in scratch2.
5700 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005701 // Load dest with zero. We use this either for the final shift or
5702 // for the answer.
5703 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005704 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005705 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5706 // the exponent that we are fastest at and also the highest exponent we can
5707 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005708 const uint32_t non_smi_exponent =
5709 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5710 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005711 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5712 __ b(eq, &right_exponent);
5713 // If the exponent is higher than that then go to slow case. This catches
5714 // numbers that don't fit in a signed int32, infinities and NaNs.
5715 __ b(gt, slow);
5716
5717 // We know the exponent is smaller than 30 (biased). If it is less than
5718 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5719 // it rounds to zero.
5720 const uint32_t zero_exponent =
5721 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5722 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5723 // Dest already has a Smi zero.
5724 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005725 if (!CpuFeatures::IsSupported(VFP3)) {
5726 // We have a shifted exponent between 0 and 30 in scratch2.
5727 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5728 // We now have the exponent in dest. Subtract from 30 to get
5729 // how much to shift down.
5730 __ rsb(dest, dest, Operand(30));
5731 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005732 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005733 if (CpuFeatures::IsSupported(VFP3)) {
5734 CpuFeatures::Scope scope(VFP3);
5735 // ARMv7 VFP3 instructions implementing double precision to integer
5736 // conversion using round to zero.
5737 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005738 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005739 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005740 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005741 } else {
5742 // Get the top bits of the mantissa.
5743 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5744 // Put back the implicit 1.
5745 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5746 // Shift up the mantissa bits to take up the space the exponent used to
5747 // take. We just orred in the implicit bit so that took care of one and
5748 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5749 // distance.
5750 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5751 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5752 // Put sign in zero flag.
5753 __ tst(scratch, Operand(HeapNumber::kSignMask));
5754 // Get the second half of the double. For some exponents we don't
5755 // actually need this because the bits get shifted out again, but
5756 // it's probably slower to test than just to do it.
5757 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5758 // Shift down 22 bits to get the last 10 bits.
5759 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5760 // Move down according to the exponent.
5761 __ mov(dest, Operand(scratch, LSR, dest));
5762 // Fix sign if sign bit was set.
5763 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5764 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005765 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005766}
5767
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005768// For bitwise ops where the inputs are not both Smis we here try to determine
5769// whether both inputs are either Smis or at least heap numbers that can be
5770// represented by a 32 bit signed value. We truncate towards zero as required
5771// by the ES spec. If this is the case we do the bitwise op and see if the
5772// result is a Smi. If so, great, otherwise we try to find a heap number to
5773// write the answer into (either by allocating or by overwriting).
5774// On entry the operands are in r0 and r1. On exit the answer is in r0.
5775void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5776 Label slow, result_not_a_smi;
5777 Label r0_is_smi, r1_is_smi;
5778 Label done_checking_r0, done_checking_r1;
5779
5780 __ tst(r1, Operand(kSmiTagMask));
5781 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5782 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5783 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005784 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005785 __ jmp(&done_checking_r1);
5786 __ bind(&r1_is_smi);
5787 __ mov(r3, Operand(r1, ASR, 1));
5788 __ bind(&done_checking_r1);
5789
5790 __ tst(r0, Operand(kSmiTagMask));
5791 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5792 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5793 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005794 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005795 __ jmp(&done_checking_r0);
5796 __ bind(&r0_is_smi);
5797 __ mov(r2, Operand(r0, ASR, 1));
5798 __ bind(&done_checking_r0);
5799
5800 // r0 and r1: Original operands (Smi or heap numbers).
5801 // r2 and r3: Signed int32 operands.
5802 switch (op_) {
5803 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5804 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5805 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5806 case Token::SAR:
5807 // Use only the 5 least significant bits of the shift count.
5808 __ and_(r2, r2, Operand(0x1f));
5809 __ mov(r2, Operand(r3, ASR, r2));
5810 break;
5811 case Token::SHR:
5812 // Use only the 5 least significant bits of the shift count.
5813 __ and_(r2, r2, Operand(0x1f));
5814 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5815 // SHR is special because it is required to produce a positive answer.
5816 // The code below for writing into heap numbers isn't capable of writing
5817 // the register as an unsigned int so we go to slow case if we hit this
5818 // case.
5819 __ b(mi, &slow);
5820 break;
5821 case Token::SHL:
5822 // Use only the 5 least significant bits of the shift count.
5823 __ and_(r2, r2, Operand(0x1f));
5824 __ mov(r2, Operand(r3, LSL, r2));
5825 break;
5826 default: UNREACHABLE();
5827 }
5828 // check that the *signed* result fits in a smi
5829 __ add(r3, r2, Operand(0x40000000), SetCC);
5830 __ b(mi, &result_not_a_smi);
5831 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5832 __ Ret();
5833
5834 Label have_to_allocate, got_a_heap_number;
5835 __ bind(&result_not_a_smi);
5836 switch (mode_) {
5837 case OVERWRITE_RIGHT: {
5838 __ tst(r0, Operand(kSmiTagMask));
5839 __ b(eq, &have_to_allocate);
5840 __ mov(r5, Operand(r0));
5841 break;
5842 }
5843 case OVERWRITE_LEFT: {
5844 __ tst(r1, Operand(kSmiTagMask));
5845 __ b(eq, &have_to_allocate);
5846 __ mov(r5, Operand(r1));
5847 break;
5848 }
5849 case NO_OVERWRITE: {
5850 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005851 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005852 }
5853 default: break;
5854 }
5855 __ bind(&got_a_heap_number);
5856 // r2: Answer as signed int32.
5857 // r5: Heap number to write answer into.
5858
5859 // Nothing can go wrong now, so move the heap number to r0, which is the
5860 // result.
5861 __ mov(r0, Operand(r5));
5862
5863 // Tail call that writes the int32 in r2 to the heap number in r0, using
5864 // r3 as scratch. r0 is preserved and returned.
5865 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5866 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5867
5868 if (mode_ != NO_OVERWRITE) {
5869 __ bind(&have_to_allocate);
5870 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005871 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005872 __ jmp(&got_a_heap_number);
5873 }
5874
5875 // If all else failed then we go to the runtime system.
5876 __ bind(&slow);
5877 __ push(r1); // restore stack
5878 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005879 switch (op_) {
5880 case Token::BIT_OR:
5881 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5882 break;
5883 case Token::BIT_AND:
5884 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5885 break;
5886 case Token::BIT_XOR:
5887 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5888 break;
5889 case Token::SAR:
5890 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5891 break;
5892 case Token::SHR:
5893 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5894 break;
5895 case Token::SHL:
5896 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5897 break;
5898 default:
5899 UNREACHABLE();
5900 }
5901}
5902
5903
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005904// Can we multiply by x with max two shifts and an add.
5905// This answers yes to all integers from 2 to 10.
5906static bool IsEasyToMultiplyBy(int x) {
5907 if (x < 2) return false; // Avoid special cases.
5908 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5909 if (IsPowerOf2(x)) return true; // Simple shift.
5910 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5911 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5912 return false;
5913}
5914
5915
5916// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5917// Source and destination may be the same register. This routine does
5918// not set carry and overflow the way a mul instruction would.
5919static void MultiplyByKnownInt(MacroAssembler* masm,
5920 Register source,
5921 Register destination,
5922 int known_int) {
5923 if (IsPowerOf2(known_int)) {
5924 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5925 } else if (PopCountLessThanEqual2(known_int)) {
5926 int first_bit = BitPosition(known_int);
5927 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5928 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5929 if (first_bit != 0) {
5930 __ mov(destination, Operand(destination, LSL, first_bit));
5931 }
5932 } else {
5933 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5934 int the_bit = BitPosition(known_int + 1);
5935 __ rsb(destination, source, Operand(source, LSL, the_bit));
5936 }
5937}
5938
5939
5940// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5941// a register for the cases where it doesn't know a good trick, and may deliver
5942// a result that needs shifting.
5943static void MultiplyByKnownInt2(
5944 MacroAssembler* masm,
5945 Register result,
5946 Register source,
5947 Register known_int_register, // Smi tagged.
5948 int known_int,
5949 int* required_shift) { // Including Smi tag shift
5950 switch (known_int) {
5951 case 3:
5952 __ add(result, source, Operand(source, LSL, 1));
5953 *required_shift = 1;
5954 break;
5955 case 5:
5956 __ add(result, source, Operand(source, LSL, 2));
5957 *required_shift = 1;
5958 break;
5959 case 6:
5960 __ add(result, source, Operand(source, LSL, 1));
5961 *required_shift = 2;
5962 break;
5963 case 7:
5964 __ rsb(result, source, Operand(source, LSL, 3));
5965 *required_shift = 1;
5966 break;
5967 case 9:
5968 __ add(result, source, Operand(source, LSL, 3));
5969 *required_shift = 1;
5970 break;
5971 case 10:
5972 __ add(result, source, Operand(source, LSL, 2));
5973 *required_shift = 2;
5974 break;
5975 default:
5976 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5977 __ mul(result, source, known_int_register);
5978 *required_shift = 0;
5979 }
5980}
5981
5982
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005983const char* GenericBinaryOpStub::GetName() {
5984 if (name_ != NULL) return name_;
5985 const int len = 100;
5986 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5987 if (name_ == NULL) return "OOM";
5988 const char* op_name = Token::Name(op_);
5989 const char* overwrite_name;
5990 switch (mode_) {
5991 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5992 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5993 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5994 default: overwrite_name = "UnknownOverwrite"; break;
5995 }
5996
5997 OS::SNPrintF(Vector<char>(name_, len),
5998 "GenericBinaryOpStub_%s_%s%s",
5999 op_name,
6000 overwrite_name,
6001 specialized_on_rhs_ ? "_ConstantRhs" : 0);
6002 return name_;
6003}
6004
6005
ager@chromium.org5c838252010-02-19 08:53:10 +00006006
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006007void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
6008 // r1 : x
6009 // r0 : y
6010 // result : r0
6011
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006012 // All ops need to know whether we are dealing with two Smis. Set up r2 to
6013 // tell us that.
6014 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
6015
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006016 switch (op_) {
6017 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006018 Label not_smi;
6019 // Fast path.
6020 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006021 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006022 __ b(ne, &not_smi);
6023 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
6024 // Return if no overflow.
6025 __ Ret(vc);
6026 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
6027
6028 HandleBinaryOpSlowCases(masm,
6029 &not_smi,
6030 Builtins::ADD,
6031 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006032 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006033 break;
6034 }
6035
6036 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006037 Label not_smi;
6038 // Fast path.
6039 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006040 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006041 __ b(ne, &not_smi);
6042 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
6043 // Return if no overflow.
6044 __ Ret(vc);
6045 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
6046
6047 HandleBinaryOpSlowCases(masm,
6048 &not_smi,
6049 Builtins::SUB,
6050 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006051 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006052 break;
6053 }
6054
6055 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006056 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006057 ASSERT(kSmiTag == 0); // adjust code below
6058 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006059 __ b(ne, &not_smi);
6060 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006061 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006062 // Do multiplication
6063 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
6064 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006065 __ mov(ip, Operand(r3, ASR, 31));
6066 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
6067 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006068 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006069 __ tst(r3, Operand(r3));
6070 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006071 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006072 // We need -0 if we were multiplying a negative number with 0 to get 0.
6073 // We know one of them was zero.
6074 __ add(r2, r0, Operand(r1), SetCC);
6075 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
6076 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
6077 // Slow case. We fall through here if we multiplied a negative number
6078 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006079 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006080
6081 HandleBinaryOpSlowCases(masm,
6082 &not_smi,
6083 Builtins::MUL,
6084 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006085 mode_);
6086 break;
6087 }
6088
6089 case Token::DIV:
6090 case Token::MOD: {
6091 Label not_smi;
6092 if (specialized_on_rhs_) {
6093 Label smi_is_unsuitable;
6094 __ BranchOnNotSmi(r1, &not_smi);
6095 if (IsPowerOf2(constant_rhs_)) {
6096 if (op_ == Token::MOD) {
6097 __ and_(r0,
6098 r1,
6099 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6100 SetCC);
6101 // We now have the answer, but if the input was negative we also
6102 // have the sign bit. Our work is done if the result is
6103 // positive or zero:
6104 __ Ret(pl);
6105 // A mod of a negative left hand side must return a negative number.
6106 // Unfortunately if the answer is 0 then we must return -0. And we
6107 // already optimistically trashed r0 so we may need to restore it.
6108 __ eor(r0, r0, Operand(0x80000000u), SetCC);
6109 // Next two instructions are conditional on the answer being -0.
6110 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
6111 __ b(eq, &smi_is_unsuitable);
6112 // We need to subtract the dividend. Eg. -3 % 4 == -3.
6113 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
6114 } else {
6115 ASSERT(op_ == Token::DIV);
6116 __ tst(r1,
6117 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6118 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6119 int shift = 0;
6120 int d = constant_rhs_;
6121 while ((d & 1) == 0) {
6122 d >>= 1;
6123 shift++;
6124 }
6125 __ mov(r0, Operand(r1, LSR, shift));
6126 __ bic(r0, r0, Operand(kSmiTagMask));
6127 }
6128 } else {
6129 // Not a power of 2.
6130 __ tst(r1, Operand(0x80000000u));
6131 __ b(ne, &smi_is_unsuitable);
6132 // Find a fixed point reciprocal of the divisor so we can divide by
6133 // multiplying.
6134 double divisor = 1.0 / constant_rhs_;
6135 int shift = 32;
6136 double scale = 4294967296.0; // 1 << 32.
6137 uint32_t mul;
6138 // Maximise the precision of the fixed point reciprocal.
6139 while (true) {
6140 mul = static_cast<uint32_t>(scale * divisor);
6141 if (mul >= 0x7fffffff) break;
6142 scale *= 2.0;
6143 shift++;
6144 }
6145 mul++;
6146 __ mov(r2, Operand(mul));
6147 __ umull(r3, r2, r2, r1);
6148 __ mov(r2, Operand(r2, LSR, shift - 31));
6149 // r2 is r1 / rhs. r2 is not Smi tagged.
6150 // r0 is still the known rhs. r0 is Smi tagged.
6151 // r1 is still the unkown lhs. r1 is Smi tagged.
6152 int required_r4_shift = 0; // Including the Smi tag shift of 1.
6153 // r4 = r2 * r0.
6154 MultiplyByKnownInt2(masm,
6155 r4,
6156 r2,
6157 r0,
6158 constant_rhs_,
6159 &required_r4_shift);
6160 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
6161 if (op_ == Token::DIV) {
6162 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
6163 __ b(ne, &smi_is_unsuitable); // There was a remainder.
6164 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6165 } else {
6166 ASSERT(op_ == Token::MOD);
6167 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
6168 }
6169 }
6170 __ Ret();
6171 __ bind(&smi_is_unsuitable);
6172 } else {
6173 __ jmp(&not_smi);
6174 }
6175 HandleBinaryOpSlowCases(masm,
6176 &not_smi,
6177 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
6178 op_,
6179 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006180 break;
6181 }
6182
6183 case Token::BIT_OR:
6184 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006185 case Token::BIT_XOR:
6186 case Token::SAR:
6187 case Token::SHR:
6188 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006189 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006190 ASSERT(kSmiTag == 0); // adjust code below
6191 __ tst(r2, Operand(kSmiTagMask));
6192 __ b(ne, &slow);
6193 switch (op_) {
6194 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
6195 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
6196 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006197 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006198 // Remove tags from right operand.
ager@chromium.org5c838252010-02-19 08:53:10 +00006199 __ GetLeastBitsFromSmi(r2, r0, 5);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006200 __ mov(r0, Operand(r1, ASR, r2));
6201 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006202 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006203 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006204 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006205 // Remove tags from operands. We can't do this on a 31 bit number
6206 // because then the 0s get shifted into bit 30 instead of bit 31.
6207 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006208 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006209 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006210 // Unsigned shift is not allowed to produce a negative number, so
6211 // check the sign bit and the sign bit after Smi tagging.
6212 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006213 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006214 // Smi tag result.
6215 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006216 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006217 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006218 // Remove tags from operands.
6219 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006220 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006221 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006222 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006223 __ add(r2, r3, Operand(0x40000000), SetCC);
6224 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006225 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006226 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006227 default: UNREACHABLE();
6228 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006229 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006230 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006231 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006232 break;
6233 }
6234
6235 default: UNREACHABLE();
6236 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006237 // This code should be unreachable.
6238 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006239}
6240
6241
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006242Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
6243 return Handle<Code>::null();
6244}
6245
6246
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006247void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006248 // Do tail-call to runtime routine. Runtime routines expect at least one
6249 // argument, so give it a Smi.
6250 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006251 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006252 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006253
6254 __ StubReturn(1);
6255}
6256
6257
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006258void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006259 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006260
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006261 if (op_ == Token::SUB) {
6262 // Check whether the value is a smi.
6263 Label try_float;
6264 __ tst(r0, Operand(kSmiTagMask));
6265 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006266
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006267 // Go slow case if the value of the expression is zero
6268 // to make sure that we switch between 0 and -0.
6269 __ cmp(r0, Operand(0));
6270 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006271
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006272 // The value of the expression is a smi that is not zero. Try
6273 // optimistic subtraction '0 - value'.
6274 __ rsb(r1, r0, Operand(0), SetCC);
6275 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006276
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006277 __ mov(r0, Operand(r1)); // Set r0 to result.
6278 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006280 __ bind(&try_float);
6281 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6282 __ b(ne, &slow);
6283 // r0 is a heap number. Get a new heap number in r1.
6284 if (overwrite_) {
6285 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6286 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6287 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6288 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006289 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006290 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6291 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6292 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6293 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6294 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6295 __ mov(r0, Operand(r1));
6296 }
6297 } else if (op_ == Token::BIT_NOT) {
6298 // Check if the operand is a heap number.
6299 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6300 __ b(ne, &slow);
6301
6302 // Convert the heap number is r0 to an untagged integer in r1.
6303 GetInt32(masm, r0, r1, r2, r3, &slow);
6304
6305 // Do the bitwise operation (move negated) and check if the result
6306 // fits in a smi.
6307 Label try_float;
6308 __ mvn(r1, Operand(r1));
6309 __ add(r2, r1, Operand(0x40000000), SetCC);
6310 __ b(mi, &try_float);
6311 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6312 __ b(&done);
6313
6314 __ bind(&try_float);
6315 if (!overwrite_) {
6316 // Allocate a fresh heap number, but don't overwrite r0 until
6317 // we're sure we can do it without going through the slow case
6318 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006319 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006320 __ mov(r0, Operand(r2));
6321 }
6322
6323 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6324 // have to set up a frame.
6325 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6326 __ push(lr);
6327 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6328 __ pop(lr);
6329 } else {
6330 UNIMPLEMENTED();
6331 }
6332
6333 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006334 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006335
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006336 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006337 __ bind(&slow);
6338 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006339 switch (op_) {
6340 case Token::SUB:
6341 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6342 break;
6343 case Token::BIT_NOT:
6344 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6345 break;
6346 default:
6347 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006348 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006349}
6350
6351
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006352void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006353 // r0 holds the exception.
6354
6355 // Adjust this code if not the case.
6356 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6357
6358 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006359 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6360 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006361
6362 // Restore the next handler and frame pointer, discard handler state.
6363 ASSERT(StackHandlerConstants::kNextOffset == 0);
6364 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006365 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006366 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6367 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6368
6369 // Before returning we restore the context from the frame pointer if
6370 // not NULL. The frame pointer is NULL in the exception handler of a
6371 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006372 __ cmp(fp, Operand(0));
6373 // Set cp to NULL if fp is NULL.
6374 __ mov(cp, Operand(0), LeaveCC, eq);
6375 // Restore cp otherwise.
6376 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006377#ifdef DEBUG
6378 if (FLAG_debug_code) {
6379 __ mov(lr, Operand(pc));
6380 }
6381#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006382 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006383 __ pop(pc);
6384}
6385
6386
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006387void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6388 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006389 // Adjust this code if not the case.
6390 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6391
6392 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006393 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006394 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006395
6396 // Unwind the handlers until the ENTRY handler is found.
6397 Label loop, done;
6398 __ bind(&loop);
6399 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006400 const int kStateOffset = StackHandlerConstants::kStateOffset;
6401 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006402 __ cmp(r2, Operand(StackHandler::ENTRY));
6403 __ b(eq, &done);
6404 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006405 const int kNextOffset = StackHandlerConstants::kNextOffset;
6406 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006407 __ jmp(&loop);
6408 __ bind(&done);
6409
6410 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006411 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006412 __ pop(r2);
6413 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006414
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006415 if (type == OUT_OF_MEMORY) {
6416 // Set external caught exception to false.
6417 ExternalReference external_caught(Top::k_external_caught_exception_address);
6418 __ mov(r0, Operand(false));
6419 __ mov(r2, Operand(external_caught));
6420 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006421
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006422 // Set pending exception and r0 to out of memory exception.
6423 Failure* out_of_memory = Failure::OutOfMemoryException();
6424 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6425 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6426 __ str(r0, MemOperand(r2));
6427 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006428
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006429 // Stack layout at this point. See also StackHandlerConstants.
6430 // sp -> state (ENTRY)
6431 // fp
6432 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006433
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006434 // Discard handler state (r2 is not used) and restore frame pointer.
6435 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6436 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6437 // Before returning we restore the context from the frame pointer if
6438 // not NULL. The frame pointer is NULL in the exception handler of a
6439 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006440 __ cmp(fp, Operand(0));
6441 // Set cp to NULL if fp is NULL.
6442 __ mov(cp, Operand(0), LeaveCC, eq);
6443 // Restore cp otherwise.
6444 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006445#ifdef DEBUG
6446 if (FLAG_debug_code) {
6447 __ mov(lr, Operand(pc));
6448 }
6449#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006450 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006451 __ pop(pc);
6452}
6453
6454
6455void CEntryStub::GenerateCore(MacroAssembler* masm,
6456 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006457 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006458 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006459 bool do_gc,
6460 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006461 // r0: result parameter for PerformGC, if any
6462 // r4: number of arguments including receiver (C callee-saved)
6463 // r5: pointer to builtin function (C callee-saved)
6464 // r6: pointer to the first argument (C callee-saved)
6465
6466 if (do_gc) {
6467 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006468 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6469 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006470 }
6471
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006472 ExternalReference scope_depth =
6473 ExternalReference::heap_always_allocate_scope_depth();
6474 if (always_allocate) {
6475 __ mov(r0, Operand(scope_depth));
6476 __ ldr(r1, MemOperand(r0));
6477 __ add(r1, r1, Operand(1));
6478 __ str(r1, MemOperand(r0));
6479 }
6480
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006481 // Call C built-in.
6482 // r0 = argc, r1 = argv
6483 __ mov(r0, Operand(r4));
6484 __ mov(r1, Operand(r6));
6485
6486 // TODO(1242173): To let the GC traverse the return address of the exit
6487 // frames, we need to know where the return address is. Right now,
6488 // we push it on the stack to be able to find it again, but we never
6489 // restore from it in case of changes, which makes it impossible to
6490 // support moving the C entry code stub. This should be fixed, but currently
6491 // this is OK because the CEntryStub gets generated so early in the V8 boot
6492 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006493 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6494 masm->push(lr);
6495 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006496
6497 if (always_allocate) {
6498 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6499 // though (contain the result).
6500 __ mov(r2, Operand(scope_depth));
6501 __ ldr(r3, MemOperand(r2));
6502 __ sub(r3, r3, Operand(1));
6503 __ str(r3, MemOperand(r2));
6504 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006505
6506 // check for failure result
6507 Label failure_returned;
6508 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6509 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6510 __ add(r2, r0, Operand(1));
6511 __ tst(r2, Operand(kFailureTagMask));
6512 __ b(eq, &failure_returned);
6513
6514 // Exit C frame and return.
6515 // r0:r1: result
6516 // sp: stack pointer
6517 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006518 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006519
6520 // check if we should retry or throw exception
6521 Label retry;
6522 __ bind(&failure_returned);
6523 ASSERT(Failure::RETRY_AFTER_GC == 0);
6524 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6525 __ b(eq, &retry);
6526
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006527 // Special handling of out of memory exceptions.
6528 Failure* out_of_memory = Failure::OutOfMemoryException();
6529 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6530 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006531
6532 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006533 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006534 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006535 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006536 __ ldr(r0, MemOperand(ip));
6537 __ str(r3, MemOperand(ip));
6538
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006539 // Special handling of termination exceptions which are uncatchable
6540 // by javascript code.
6541 __ cmp(r0, Operand(Factory::termination_exception()));
6542 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006543
6544 // Handle normal exception.
6545 __ jmp(throw_normal_exception);
6546
6547 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6548}
6549
6550
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006551void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006552 // Called from JavaScript; parameters are on stack as if calling JS function
6553 // r0: number of arguments including receiver
6554 // r1: pointer to builtin function
6555 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006556 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006557 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006558
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006559 // Result returned in r0 or r0+r1 by default.
6560
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006561 // NOTE: Invocations of builtins may return failure objects
6562 // instead of a proper result. The builtin entry handles
6563 // this by performing a garbage collection and retrying the
6564 // builtin once.
6565
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006566 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006567 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006568
6569 // r4: number of arguments (C callee-saved)
6570 // r5: pointer to builtin function (C callee-saved)
6571 // r6: pointer to first argument (C callee-saved)
6572
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006573 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006574 Label throw_termination_exception;
6575 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006576
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006577 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006578 GenerateCore(masm,
6579 &throw_normal_exception,
6580 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006581 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006582 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006583 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006584
6585 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006586 GenerateCore(masm,
6587 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006588 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006589 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006590 true,
6591 false);
6592
6593 // Do full GC and retry runtime call one final time.
6594 Failure* failure = Failure::InternalError();
6595 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6596 GenerateCore(masm,
6597 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006598 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006599 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006600 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006601 true);
6602
6603 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006604 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6605
6606 __ bind(&throw_termination_exception);
6607 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006608
6609 __ bind(&throw_normal_exception);
6610 GenerateThrowTOS(masm);
6611}
6612
6613
6614void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6615 // r0: code entry
6616 // r1: function
6617 // r2: receiver
6618 // r3: argc
6619 // [sp+0]: argv
6620
6621 Label invoke, exit;
6622
6623 // Called from C, so do not pop argc and args on exit (preserve sp)
6624 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006625 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006626 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6627
6628 // Get address of argv, see stm above.
6629 // r0: code entry
6630 // r1: function
6631 // r2: receiver
6632 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00006633 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006634
6635 // Push a frame with special values setup to mark it as an entry frame.
6636 // r0: code entry
6637 // r1: function
6638 // r2: receiver
6639 // r3: argc
6640 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006641 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006642 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6643 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006644 __ mov(r6, Operand(Smi::FromInt(marker)));
6645 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6646 __ ldr(r5, MemOperand(r5));
6647 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6648
6649 // Setup frame pointer for the frame to be pushed.
6650 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6651
6652 // Call a faked try-block that does the invoke.
6653 __ bl(&invoke);
6654
6655 // Caught exception: Store result (exception) in the pending
6656 // exception field in the JSEnv and return a failure sentinel.
6657 // Coming in here the fp will be invalid because the PushTryHandler below
6658 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006659 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006660 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006661 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006662 __ b(&exit);
6663
6664 // Invoke: Link this frame into the handler chain.
6665 __ bind(&invoke);
6666 // Must preserve r0-r4, r5-r7 are available.
6667 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006668 // If an exception not caught by another handler occurs, this handler
6669 // returns control to the code after the bl(&invoke) above, which
6670 // restores all kCalleeSaved registers (including cp and fp) to their
6671 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006672
6673 // Clear any pending exceptions.
6674 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6675 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006676 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006677 __ str(r5, MemOperand(ip));
6678
6679 // Invoke the function by calling through JS entry trampoline builtin.
6680 // Notice that we cannot store a reference to the trampoline code directly in
6681 // this stub, because runtime stubs are not traversed when doing GC.
6682
6683 // Expected registers by Builtins::JSEntryTrampoline
6684 // r0: code entry
6685 // r1: function
6686 // r2: receiver
6687 // r3: argc
6688 // r4: argv
6689 if (is_construct) {
6690 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6691 __ mov(ip, Operand(construct_entry));
6692 } else {
6693 ExternalReference entry(Builtins::JSEntryTrampoline);
6694 __ mov(ip, Operand(entry));
6695 }
6696 __ ldr(ip, MemOperand(ip)); // deref address
6697
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006698 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6699 // macro for the add instruction because we don't want the coverage tool
6700 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006701 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006702 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006703
6704 // Unlink this frame from the handler chain. When reading the
6705 // address of the next handler, there is no need to use the address
6706 // displacement since the current stack pointer (sp) points directly
6707 // to the stack handler.
6708 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6709 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6710 __ str(r3, MemOperand(ip));
6711 // No need to restore registers
6712 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6713
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006714
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006715 __ bind(&exit); // r0 holds result
6716 // Restore the top frame descriptors from the stack.
6717 __ pop(r3);
6718 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6719 __ str(r3, MemOperand(ip));
6720
6721 // Reset the stack to the callee saved registers.
6722 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6723
6724 // Restore callee-saved registers and return.
6725#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006726 if (FLAG_debug_code) {
6727 __ mov(lr, Operand(pc));
6728 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006729#endif
6730 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6731}
6732
6733
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006734// This stub performs an instanceof, calling the builtin function if
6735// necessary. Uses r1 for the object, r0 for the function that it may
6736// be an instance of (these are fetched from the stack).
6737void InstanceofStub::Generate(MacroAssembler* masm) {
6738 // Get the object - slow case for smis (we may need to throw an exception
6739 // depending on the rhs).
6740 Label slow, loop, is_instance, is_not_instance;
6741 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6742 __ BranchOnSmi(r0, &slow);
6743
6744 // Check that the left hand is a JS object and put map in r3.
6745 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6746 __ b(lt, &slow);
6747 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6748 __ b(gt, &slow);
6749
6750 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00006751 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006752 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6753
6754 // Check that the function prototype is a JS object.
6755 __ BranchOnSmi(r4, &slow);
6756 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6757 __ b(lt, &slow);
6758 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6759 __ b(gt, &slow);
6760
6761 // Register mapping: r3 is object map and r4 is function prototype.
6762 // Get prototype of object into r2.
6763 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6764
6765 // Loop through the prototype chain looking for the function prototype.
6766 __ bind(&loop);
6767 __ cmp(r2, Operand(r4));
6768 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006769 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6770 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006771 __ b(eq, &is_not_instance);
6772 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6773 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6774 __ jmp(&loop);
6775
6776 __ bind(&is_instance);
6777 __ mov(r0, Operand(Smi::FromInt(0)));
6778 __ pop();
6779 __ pop();
6780 __ mov(pc, Operand(lr)); // Return.
6781
6782 __ bind(&is_not_instance);
6783 __ mov(r0, Operand(Smi::FromInt(1)));
6784 __ pop();
6785 __ pop();
6786 __ mov(pc, Operand(lr)); // Return.
6787
6788 // Slow-case. Tail call builtin.
6789 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006790 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6791}
6792
6793
ager@chromium.org7c537e22008-10-16 08:43:32 +00006794void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006795 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006796 Label adaptor;
6797 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6798 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006799 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006800 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006801
ager@chromium.org7c537e22008-10-16 08:43:32 +00006802 // Nothing to do: The formal number of parameters has already been
6803 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006804 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006805
ager@chromium.org7c537e22008-10-16 08:43:32 +00006806 // Arguments adaptor case: Read the arguments length from the
6807 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006808 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006809 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006810 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006811}
6812
6813
ager@chromium.org7c537e22008-10-16 08:43:32 +00006814void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6815 // The displacement is the offset of the last parameter (if any)
6816 // relative to the frame pointer.
6817 static const int kDisplacement =
6818 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006819
ager@chromium.org7c537e22008-10-16 08:43:32 +00006820 // Check that the key is a smi.
6821 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006822 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006823
ager@chromium.org7c537e22008-10-16 08:43:32 +00006824 // Check if the calling frame is an arguments adaptor frame.
6825 Label adaptor;
6826 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6827 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006828 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006829 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006830
ager@chromium.org7c537e22008-10-16 08:43:32 +00006831 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006832 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006833 // check for free.
6834 __ cmp(r1, r0);
6835 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006836
ager@chromium.org7c537e22008-10-16 08:43:32 +00006837 // Read the argument from the stack and return it.
6838 __ sub(r3, r0, r1);
6839 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6840 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006841 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006842
6843 // Arguments adaptor case: Check index against actual arguments
6844 // limit found in the arguments adaptor frame. Use unsigned
6845 // comparison to get negative check for free.
6846 __ bind(&adaptor);
6847 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6848 __ cmp(r1, r0);
6849 __ b(cs, &slow);
6850
6851 // Read the argument from the adaptor frame and return it.
6852 __ sub(r3, r0, r1);
6853 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6854 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006855 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006856
6857 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6858 // by calling the runtime system.
6859 __ bind(&slow);
6860 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006861 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006862}
6863
6864
6865void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00006866 // sp[0] : number of parameters
6867 // sp[4] : receiver displacement
6868 // sp[8] : function
6869
ager@chromium.org7c537e22008-10-16 08:43:32 +00006870 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00006871 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00006872 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6873 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006874 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00006875 __ b(eq, &adaptor_frame);
6876
6877 // Get the length from the frame.
6878 __ ldr(r1, MemOperand(sp, 0));
6879 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006880
6881 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00006882 __ bind(&adaptor_frame);
6883 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6884 __ str(r1, MemOperand(sp, 0));
6885 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006886 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6887 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6888
ager@chromium.org5c838252010-02-19 08:53:10 +00006889 // Try the new space allocation. Start out with computing the size
6890 // of the arguments object and the elements array (in words, not
6891 // bytes because AllocateInNewSpace expects words).
6892 Label add_arguments_object;
6893 __ bind(&try_allocate);
6894 __ cmp(r1, Operand(0));
6895 __ b(eq, &add_arguments_object);
6896 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6897 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
6898 __ bind(&add_arguments_object);
6899 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
6900
6901 // Do the allocation of both objects in one go.
6902 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
6903
6904 // Get the arguments boilerplate from the current (global) context.
6905 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
6906 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6907 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
6908 __ ldr(r4, MemOperand(r4, offset));
6909
6910 // Copy the JS object part.
6911 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
6912 __ ldr(r3, FieldMemOperand(r4, i));
6913 __ str(r3, FieldMemOperand(r0, i));
6914 }
6915
6916 // Setup the callee in-object property.
6917 ASSERT(Heap::arguments_callee_index == 0);
6918 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6919 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
6920
6921 // Get the length (smi tagged) and set that as an in-object property too.
6922 ASSERT(Heap::arguments_length_index == 1);
6923 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6924 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
6925
6926 // If there are no actual arguments, we're done.
6927 Label done;
6928 __ cmp(r1, Operand(0));
6929 __ b(eq, &done);
6930
6931 // Get the parameters pointer from the stack and untag the length.
6932 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
6933 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6934
6935 // Setup the elements pointer in the allocated arguments object and
6936 // initialize the header in the elements fixed array.
6937 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
6938 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
6939 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
6940 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
6941 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
6942
6943 // Copy the fixed array slots.
6944 Label loop;
6945 // Setup r4 to point to the first array slot.
6946 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6947 __ bind(&loop);
6948 // Pre-decrement r2 with kPointerSize on each iteration.
6949 // Pre-decrement in order to skip receiver.
6950 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
6951 // Post-increment r4 with kPointerSize on each iteration.
6952 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
6953 __ sub(r1, r1, Operand(1));
6954 __ cmp(r1, Operand(0));
6955 __ b(ne, &loop);
6956
6957 // Return and remove the on-stack parameters.
6958 __ bind(&done);
6959 __ add(sp, sp, Operand(3 * kPointerSize));
6960 __ Ret();
6961
ager@chromium.org7c537e22008-10-16 08:43:32 +00006962 // Do the runtime call to allocate the arguments object.
6963 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006964 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006965}
6966
6967
6968void CallFunctionStub::Generate(MacroAssembler* masm) {
6969 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006970
6971 // If the receiver might be a value (string, number or boolean) check for this
6972 // and box it if it is.
6973 if (ReceiverMightBeValue()) {
6974 // Get the receiver from the stack.
6975 // function, receiver [, arguments]
6976 Label receiver_is_value, receiver_is_js_object;
6977 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
6978
6979 // Check if receiver is a smi (which is a number value).
6980 __ BranchOnSmi(r1, &receiver_is_value);
6981
6982 // Check if the receiver is a valid JS object.
6983 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
6984 __ b(ge, &receiver_is_js_object);
6985
6986 // Call the runtime to box the value.
6987 __ bind(&receiver_is_value);
6988 __ EnterInternalFrame();
6989 __ push(r1);
6990 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
6991 __ LeaveInternalFrame();
6992 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
6993
6994 __ bind(&receiver_is_js_object);
6995 }
6996
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006997 // Get the function to call from the stack.
6998 // function, receiver [, arguments]
6999 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
7000
7001 // Check that the function is really a JavaScript function.
7002 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007003 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007004 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007005 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007006 __ b(ne, &slow);
7007
7008 // Fast-case: Invoke the function now.
7009 // r1: pushed function
7010 ParameterCount actual(argc_);
7011 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
7012
7013 // Slow-case: Non-function called.
7014 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00007015 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
7016 // of the original receiver from the call site).
7017 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007018 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007019 __ mov(r2, Operand(0));
7020 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
7021 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
7022 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007023}
7024
7025
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007026// Unfortunately you have to run without snapshots to see most of these
7027// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007028const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007029 if (name_ != NULL) return name_;
7030 const int kMaxNameLength = 100;
7031 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
7032 if (name_ == NULL) return "OOM";
7033
7034 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007035 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007036 case lt: cc_name = "LT"; break;
7037 case gt: cc_name = "GT"; break;
7038 case le: cc_name = "LE"; break;
7039 case ge: cc_name = "GE"; break;
7040 case eq: cc_name = "EQ"; break;
7041 case ne: cc_name = "NE"; break;
7042 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007043 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007044
7045 const char* strict_name = "";
7046 if (strict_ && (cc_ == eq || cc_ == ne)) {
7047 strict_name = "_STRICT";
7048 }
7049
7050 const char* never_nan_nan_name = "";
7051 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
7052 never_nan_nan_name = "_NO_NAN";
7053 }
7054
7055 const char* include_number_compare_name = "";
7056 if (!include_number_compare_) {
7057 include_number_compare_name = "_NO_NUMBER";
7058 }
7059
7060 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
7061 "CompareStub_%s%s%s%s",
7062 cc_name,
7063 strict_name,
7064 never_nan_nan_name,
7065 include_number_compare_name);
7066 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007067}
7068
7069
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007070int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007071 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
7072 // stubs the never NaN NaN condition is only taken into account if the
7073 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007074 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007075 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
7076 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00007077 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
7078 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007079}
7080
7081
ager@chromium.org5c838252010-02-19 08:53:10 +00007082void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
7083 Register dest,
7084 Register src,
7085 Register count,
7086 Register scratch,
7087 bool ascii) {
7088 Label loop;
7089 Label done;
7090 // This loop just copies one character at a time, as it is only used for very
7091 // short strings.
7092 if (!ascii) {
7093 __ add(count, count, Operand(count), SetCC);
7094 } else {
7095 __ cmp(count, Operand(0));
7096 }
7097 __ b(eq, &done);
7098
7099 __ bind(&loop);
7100 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
7101 // Perform sub between load and dependent store to get the load time to
7102 // complete.
7103 __ sub(count, count, Operand(1), SetCC);
7104 __ strb(scratch, MemOperand(dest, 1, PostIndex));
7105 // last iteration.
7106 __ b(gt, &loop);
7107
7108 __ bind(&done);
7109}
7110
7111
7112enum CopyCharactersFlags {
7113 COPY_ASCII = 1,
7114 DEST_ALWAYS_ALIGNED = 2
7115};
7116
7117
7118void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
7119 Register dest,
7120 Register src,
7121 Register count,
7122 Register scratch1,
7123 Register scratch2,
7124 Register scratch3,
7125 Register scratch4,
7126 Register scratch5,
7127 int flags) {
7128 bool ascii = (flags & COPY_ASCII) != 0;
7129 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
7130
7131 if (dest_always_aligned && FLAG_debug_code) {
7132 // Check that destination is actually word aligned if the flag says
7133 // that it is.
7134 __ tst(dest, Operand(kPointerAlignmentMask));
7135 __ Check(eq, "Destination of copy not aligned.");
7136 }
7137
7138 const int kReadAlignment = 4;
7139 const int kReadAlignmentMask = kReadAlignment - 1;
7140 // Ensure that reading an entire aligned word containing the last character
7141 // of a string will not read outside the allocated area (because we pad up
7142 // to kObjectAlignment).
7143 ASSERT(kObjectAlignment >= kReadAlignment);
7144 // Assumes word reads and writes are little endian.
7145 // Nothing to do for zero characters.
7146 Label done;
7147 if (!ascii) {
7148 __ add(count, count, Operand(count), SetCC);
7149 } else {
7150 __ cmp(count, Operand(0));
7151 }
7152 __ b(eq, &done);
7153
7154 // Assume that you cannot read (or write) unaligned.
7155 Label byte_loop;
7156 // Must copy at least eight bytes, otherwise just do it one byte at a time.
7157 __ cmp(count, Operand(8));
7158 __ add(count, dest, Operand(count));
7159 Register limit = count; // Read until src equals this.
7160 __ b(lt, &byte_loop);
7161
7162 if (!dest_always_aligned) {
7163 // Align dest by byte copying. Copies between zero and three bytes.
7164 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
7165 Label dest_aligned;
7166 __ b(eq, &dest_aligned);
7167 __ cmp(scratch4, Operand(2));
7168 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
7169 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
7170 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
7171 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7172 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
7173 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
7174 __ bind(&dest_aligned);
7175 }
7176
7177 Label simple_loop;
7178
7179 __ sub(scratch4, dest, Operand(src));
7180 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
7181 __ b(eq, &simple_loop);
7182 // Shift register is number of bits in a source word that
7183 // must be combined with bits in the next source word in order
7184 // to create a destination word.
7185
7186 // Complex loop for src/dst that are not aligned the same way.
7187 {
7188 Label loop;
7189 __ mov(scratch4, Operand(scratch4, LSL, 3));
7190 Register left_shift = scratch4;
7191 __ and_(src, src, Operand(~3)); // Round down to load previous word.
7192 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7193 // Store the "shift" most significant bits of scratch in the least
7194 // signficant bits (i.e., shift down by (32-shift)).
7195 __ rsb(scratch2, left_shift, Operand(32));
7196 Register right_shift = scratch2;
7197 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
7198
7199 __ bind(&loop);
7200 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
7201 __ sub(scratch5, limit, Operand(dest));
7202 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
7203 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7204 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
7205 // Loop if four or more bytes left to copy.
7206 // Compare to eight, because we did the subtract before increasing dst.
7207 __ sub(scratch5, scratch5, Operand(8), SetCC);
7208 __ b(ge, &loop);
7209 }
7210 // There is now between zero and three bytes left to copy (negative that
7211 // number is in scratch5), and between one and three bytes already read into
7212 // scratch1 (eight times that number in scratch4). We may have read past
7213 // the end of the string, but because objects are aligned, we have not read
7214 // past the end of the object.
7215 // Find the minimum of remaining characters to move and preloaded characters
7216 // and write those as bytes.
7217 __ add(scratch5, scratch5, Operand(4), SetCC);
7218 __ b(eq, &done);
7219 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
7220 // Move minimum of bytes read and bytes left to copy to scratch4.
7221 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
7222 // Between one and three (value in scratch5) characters already read into
7223 // scratch ready to write.
7224 __ cmp(scratch5, Operand(2));
7225 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7226 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
7227 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
7228 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
7229 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
7230 // Copy any remaining bytes.
7231 __ b(&byte_loop);
7232
7233 // Simple loop.
7234 // Copy words from src to dst, until less than four bytes left.
7235 // Both src and dest are word aligned.
7236 __ bind(&simple_loop);
7237 {
7238 Label loop;
7239 __ bind(&loop);
7240 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7241 __ sub(scratch3, limit, Operand(dest));
7242 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7243 // Compare to 8, not 4, because we do the substraction before increasing
7244 // dest.
7245 __ cmp(scratch3, Operand(8));
7246 __ b(ge, &loop);
7247 }
7248
7249 // Copy bytes from src to dst until dst hits limit.
7250 __ bind(&byte_loop);
7251 __ cmp(dest, Operand(limit));
7252 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
7253 __ b(ge, &done);
7254 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7255 __ b(&byte_loop);
7256
7257 __ bind(&done);
7258}
7259
7260
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007261void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
7262 Register c1,
7263 Register c2,
7264 Register scratch1,
7265 Register scratch2,
7266 Register scratch3,
7267 Register scratch4,
7268 Register scratch5,
7269 Label* not_found) {
7270 // Register scratch3 is the general scratch register in this function.
7271 Register scratch = scratch3;
7272
7273 // Make sure that both characters are not digits as such strings has a
7274 // different hash algorithm. Don't try to look for these in the symbol table.
7275 Label not_array_index;
7276 __ sub(scratch, c1, Operand(static_cast<int>('0')));
7277 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
7278 __ b(hi, &not_array_index);
7279 __ sub(scratch, c2, Operand(static_cast<int>('0')));
7280 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
7281
7282 // If check failed combine both characters into single halfword.
7283 // This is required by the contract of the method: code at the
7284 // not_found branch expects this combination in c1 register
7285 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
7286 __ b(ls, not_found);
7287
7288 __ bind(&not_array_index);
7289 // Calculate the two character string hash.
7290 Register hash = scratch1;
7291 GenerateHashInit(masm, hash, c1);
7292 GenerateHashAddCharacter(masm, hash, c2);
7293 GenerateHashGetHash(masm, hash);
7294
7295 // Collect the two characters in a register.
7296 Register chars = c1;
7297 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
7298
7299 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
7300 // hash: hash of two character string.
7301
7302 // Load symbol table
7303 // Load address of first element of the symbol table.
7304 Register symbol_table = c2;
7305 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
7306
7307 // Load undefined value
7308 Register undefined = scratch4;
7309 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
7310
7311 // Calculate capacity mask from the symbol table capacity.
7312 Register mask = scratch2;
7313 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
7314 __ mov(mask, Operand(mask, ASR, 1));
7315 __ sub(mask, mask, Operand(1));
7316
7317 // Calculate untagged address of the first element of the symbol table.
7318 Register first_symbol_table_element = symbol_table;
7319 __ add(first_symbol_table_element, symbol_table,
7320 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
7321
7322 // Registers
7323 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
7324 // hash: hash of two character string
7325 // mask: capacity mask
7326 // first_symbol_table_element: address of the first element of
7327 // the symbol table
7328 // scratch: -
7329
7330 // Perform a number of probes in the symbol table.
7331 static const int kProbes = 4;
7332 Label found_in_symbol_table;
7333 Label next_probe[kProbes];
7334 for (int i = 0; i < kProbes; i++) {
7335 Register candidate = scratch5; // Scratch register contains candidate.
7336
7337 // Calculate entry in symbol table.
7338 if (i > 0) {
7339 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
7340 } else {
7341 __ mov(candidate, hash);
7342 }
7343
7344 __ and_(candidate, candidate, Operand(mask));
7345
7346 // Load the entry from the symble table.
7347 ASSERT_EQ(1, SymbolTable::kEntrySize);
7348 __ ldr(candidate,
7349 MemOperand(first_symbol_table_element,
7350 candidate,
7351 LSL,
7352 kPointerSizeLog2));
7353
7354 // If entry is undefined no string with this hash can be found.
7355 __ cmp(candidate, undefined);
7356 __ b(eq, not_found);
7357
7358 // If length is not 2 the string is not a candidate.
7359 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
7360 __ cmp(scratch, Operand(2));
7361 __ b(ne, &next_probe[i]);
7362
7363 // Check that the candidate is a non-external ascii string.
7364 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
7365 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
7366 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
7367 &next_probe[i]);
7368
7369 // Check if the two characters match.
7370 // Assumes that word load is little endian.
7371 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
7372 __ cmp(chars, scratch);
7373 __ b(eq, &found_in_symbol_table);
7374 __ bind(&next_probe[i]);
7375 }
7376
7377 // No matching 2 character string found by probing.
7378 __ jmp(not_found);
7379
7380 // Scratch register contains result when we fall through to here.
7381 Register result = scratch;
7382 __ bind(&found_in_symbol_table);
7383 if (!result.is(r0)) {
7384 __ mov(r0, result);
7385 }
7386}
7387
7388
7389void StringStubBase::GenerateHashInit(MacroAssembler* masm,
7390 Register hash,
7391 Register character) {
7392 // hash = character + (character << 10);
7393 __ add(hash, character, Operand(character, LSL, 10));
7394 // hash ^= hash >> 6;
7395 __ eor(hash, hash, Operand(hash, ASR, 6));
7396}
7397
7398
7399void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm,
7400 Register hash,
7401 Register character) {
7402 // hash += character;
7403 __ add(hash, hash, Operand(character));
7404 // hash += hash << 10;
7405 __ add(hash, hash, Operand(hash, LSL, 10));
7406 // hash ^= hash >> 6;
7407 __ eor(hash, hash, Operand(hash, ASR, 6));
7408}
7409
7410
7411void StringStubBase::GenerateHashGetHash(MacroAssembler* masm,
7412 Register hash) {
7413 // hash += hash << 3;
7414 __ add(hash, hash, Operand(hash, LSL, 3));
7415 // hash ^= hash >> 11;
7416 __ eor(hash, hash, Operand(hash, ASR, 11));
7417 // hash += hash << 15;
7418 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
7419
7420 // if (hash == 0) hash = 27;
7421 __ mov(hash, Operand(27), LeaveCC, nz);
7422}
7423
7424
ager@chromium.org5c838252010-02-19 08:53:10 +00007425void SubStringStub::Generate(MacroAssembler* masm) {
7426 Label runtime;
7427
7428 // Stack frame on entry.
7429 // lr: return address
7430 // sp[0]: to
7431 // sp[4]: from
7432 // sp[8]: string
7433
7434 // This stub is called from the native-call %_SubString(...), so
7435 // nothing can be assumed about the arguments. It is tested that:
7436 // "string" is a sequential string,
7437 // both "from" and "to" are smis, and
7438 // 0 <= from <= to <= string.length.
7439 // If any of these assumptions fail, we call the runtime system.
7440
7441 static const int kToOffset = 0 * kPointerSize;
7442 static const int kFromOffset = 1 * kPointerSize;
7443 static const int kStringOffset = 2 * kPointerSize;
7444
7445
7446 // Check bounds and smi-ness.
7447 __ ldr(r7, MemOperand(sp, kToOffset));
7448 __ ldr(r6, MemOperand(sp, kFromOffset));
7449 ASSERT_EQ(0, kSmiTag);
7450 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7451 // I.e., arithmetic shift right by one un-smi-tags.
7452 __ mov(r2, Operand(r7, ASR, 1), SetCC);
7453 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
7454 // If either r2 or r6 had the smi tag bit set, then carry is set now.
7455 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
7456 __ b(mi, &runtime); // From is negative.
7457
7458 __ sub(r2, r2, Operand(r3), SetCC);
7459 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007460 // Special handling of sub-strings of length 1 and 2. One character strings
7461 // are handled in the runtime system (looked up in the single character
7462 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00007463 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007464 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00007465
7466 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007467 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00007468 // r6: from (smi)
7469 // r7: to (smi)
7470
7471 // Make sure first argument is a sequential (or flat) string.
7472 __ ldr(r5, MemOperand(sp, kStringOffset));
7473 ASSERT_EQ(0, kSmiTag);
7474 __ tst(r5, Operand(kSmiTagMask));
7475 __ b(eq, &runtime);
7476 Condition is_string = masm->IsObjectStringType(r5, r1);
7477 __ b(NegateCondition(is_string), &runtime);
7478
7479 // r1: instance type
7480 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007481 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00007482 // r5: string
7483 // r6: from (smi)
7484 // r7: to (smi)
7485 Label seq_string;
7486 __ and_(r4, r1, Operand(kStringRepresentationMask));
7487 ASSERT(kSeqStringTag < kConsStringTag);
7488 ASSERT(kExternalStringTag > kConsStringTag);
7489 __ cmp(r4, Operand(kConsStringTag));
7490 __ b(gt, &runtime); // External strings go to runtime.
7491 __ b(lt, &seq_string); // Sequential strings are handled directly.
7492
7493 // Cons string. Try to recurse (once) on the first substring.
7494 // (This adds a little more generality than necessary to handle flattened
7495 // cons strings, but not much).
7496 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
7497 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
7498 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7499 __ tst(r1, Operand(kStringRepresentationMask));
7500 ASSERT_EQ(0, kSeqStringTag);
7501 __ b(ne, &runtime); // Cons and External strings go to runtime.
7502
7503 // Definitly a sequential string.
7504 __ bind(&seq_string);
7505
7506 // r1: instance type.
7507 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007508 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00007509 // r5: string
7510 // r6: from (smi)
7511 // r7: to (smi)
7512 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
7513 __ cmp(r4, Operand(r7, ASR, 1));
7514 __ b(lt, &runtime); // Fail if to > length.
7515
7516 // r1: instance type.
7517 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007518 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00007519 // r5: string.
7520 // r6: from offset (smi)
7521 // Check for flat ascii string.
7522 Label non_ascii_flat;
7523 __ tst(r1, Operand(kStringEncodingMask));
7524 ASSERT_EQ(0, kTwoByteStringTag);
7525 __ b(eq, &non_ascii_flat);
7526
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007527 Label result_longer_than_two;
7528 __ cmp(r2, Operand(2));
7529 __ b(gt, &result_longer_than_two);
7530
7531 // Sub string of length 2 requested.
7532 // Get the two characters forming the sub string.
7533 __ add(r5, r5, Operand(r3));
7534 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
7535 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
7536
7537 // Try to lookup two character string in symbol table.
7538 Label make_two_character_string;
7539 GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9,
7540 &make_two_character_string);
7541 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7542 __ add(sp, sp, Operand(3 * kPointerSize));
7543 __ Ret();
7544
7545 // r2: result string length.
7546 // r3: two characters combined into halfword in little endian byte order.
7547 __ bind(&make_two_character_string);
7548 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
7549 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
7550 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7551 __ add(sp, sp, Operand(3 * kPointerSize));
7552 __ Ret();
7553
7554 __ bind(&result_longer_than_two);
7555
ager@chromium.org5c838252010-02-19 08:53:10 +00007556 // Allocate the result.
7557 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
7558
7559 // r0: result string.
7560 // r2: result string length.
7561 // r5: string.
7562 // r6: from offset (smi)
7563 // Locate first character of result.
7564 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7565 // Locate 'from' character of string.
7566 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7567 __ add(r5, r5, Operand(r6, ASR, 1));
7568
7569 // r0: result string.
7570 // r1: first character of result string.
7571 // r2: result string length.
7572 // r5: first character of sub string to copy.
7573 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
7574 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7575 COPY_ASCII | DEST_ALWAYS_ALIGNED);
7576 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7577 __ add(sp, sp, Operand(3 * kPointerSize));
7578 __ Ret();
7579
7580 __ bind(&non_ascii_flat);
7581 // r2: result string length.
7582 // r5: string.
7583 // r6: from offset (smi)
7584 // Check for flat two byte string.
7585
7586 // Allocate the result.
7587 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
7588
7589 // r0: result string.
7590 // r2: result string length.
7591 // r5: string.
7592 // Locate first character of result.
7593 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7594 // Locate 'from' character of string.
7595 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7596 // As "from" is a smi it is 2 times the value which matches the size of a two
7597 // byte character.
7598 __ add(r5, r5, Operand(r6));
7599
7600 // r0: result string.
7601 // r1: first character of result.
7602 // r2: result length.
7603 // r5: first character of string to copy.
7604 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
7605 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7606 DEST_ALWAYS_ALIGNED);
7607 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7608 __ add(sp, sp, Operand(3 * kPointerSize));
7609 __ Ret();
7610
7611 // Just jump to runtime to create the sub string.
7612 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007613 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00007614}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007615
7616
7617void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
7618 Register left,
7619 Register right,
7620 Register scratch1,
7621 Register scratch2,
7622 Register scratch3,
7623 Register scratch4) {
7624 Label compare_lengths;
7625 // Find minimum length and length difference.
7626 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
7627 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
7628 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
7629 Register length_delta = scratch3;
7630 __ mov(scratch1, scratch2, LeaveCC, gt);
7631 Register min_length = scratch1;
7632 __ tst(min_length, Operand(min_length));
7633 __ b(eq, &compare_lengths);
7634
7635 // Setup registers so that we only need to increment one register
7636 // in the loop.
7637 __ add(scratch2, min_length,
7638 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7639 __ add(left, left, Operand(scratch2));
7640 __ add(right, right, Operand(scratch2));
7641 // Registers left and right points to the min_length character of strings.
7642 __ rsb(min_length, min_length, Operand(-1));
7643 Register index = min_length;
7644 // Index starts at -min_length.
7645
7646 {
7647 // Compare loop.
7648 Label loop;
7649 __ bind(&loop);
7650 // Compare characters.
7651 __ add(index, index, Operand(1), SetCC);
7652 __ ldrb(scratch2, MemOperand(left, index), ne);
7653 __ ldrb(scratch4, MemOperand(right, index), ne);
7654 // Skip to compare lengths with eq condition true.
7655 __ b(eq, &compare_lengths);
7656 __ cmp(scratch2, scratch4);
7657 __ b(eq, &loop);
7658 // Fallthrough with eq condition false.
7659 }
7660 // Compare lengths - strings up to min-length are equal.
7661 __ bind(&compare_lengths);
7662 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
7663 // Use zero length_delta as result.
7664 __ mov(r0, Operand(length_delta), SetCC, eq);
7665 // Fall through to here if characters compare not-equal.
7666 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
7667 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
7668 __ Ret();
7669}
7670
7671
7672void StringCompareStub::Generate(MacroAssembler* masm) {
7673 Label runtime;
7674
7675 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00007676 // sp[0]: right string
7677 // sp[4]: left string
7678 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
7679 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007680
7681 Label not_same;
7682 __ cmp(r0, r1);
7683 __ b(ne, &not_same);
7684 ASSERT_EQ(0, EQUAL);
7685 ASSERT_EQ(0, kSmiTag);
7686 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
7687 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
7688 __ add(sp, sp, Operand(2 * kPointerSize));
7689 __ Ret();
7690
7691 __ bind(&not_same);
7692
7693 // Check that both objects are sequential ascii strings.
7694 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
7695
7696 // Compare flat ascii strings natively. Remove arguments from stack first.
7697 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7698 __ add(sp, sp, Operand(2 * kPointerSize));
7699 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
7700
7701 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
7702 // tagged as a small integer.
7703 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007704 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007705}
7706
7707
ager@chromium.org5c838252010-02-19 08:53:10 +00007708void StringAddStub::Generate(MacroAssembler* masm) {
7709 Label string_add_runtime;
7710 // Stack on entry:
7711 // sp[0]: second argument.
7712 // sp[4]: first argument.
7713
7714 // Load the two arguments.
7715 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
7716 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
7717
7718 // Make sure that both arguments are strings if not known in advance.
7719 if (string_check_) {
7720 ASSERT_EQ(0, kSmiTag);
7721 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
7722 // Load instance types.
7723 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7724 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7725 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7726 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7727 ASSERT_EQ(0, kStringTag);
7728 // If either is not a string, go to runtime.
7729 __ tst(r4, Operand(kIsNotStringMask));
7730 __ tst(r5, Operand(kIsNotStringMask), eq);
7731 __ b(ne, &string_add_runtime);
7732 }
7733
7734 // Both arguments are strings.
7735 // r0: first string
7736 // r1: second string
7737 // r4: first string instance type (if string_check_)
7738 // r5: second string instance type (if string_check_)
7739 {
7740 Label strings_not_empty;
7741 // Check if either of the strings are empty. In that case return the other.
7742 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
7743 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
7744 __ cmp(r2, Operand(0)); // Test if first string is empty.
7745 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
7746 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
7747 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
7748
7749 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7750 __ add(sp, sp, Operand(2 * kPointerSize));
7751 __ Ret();
7752
7753 __ bind(&strings_not_empty);
7754 }
7755
7756 // Both strings are non-empty.
7757 // r0: first string
7758 // r1: second string
7759 // r2: length of first string
7760 // r3: length of second string
7761 // r4: first string instance type (if string_check_)
7762 // r5: second string instance type (if string_check_)
7763 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007764 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00007765 // Adding two lengths can't overflow.
7766 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
7767 __ add(r6, r2, Operand(r3));
7768 // Use the runtime system when adding two one character strings, as it
7769 // contains optimizations for this specific case using the symbol table.
7770 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007771 __ b(ne, &longer_than_two);
7772
7773 // Check that both strings are non-external ascii strings.
7774 if (!string_check_) {
7775 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7776 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7777 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7778 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7779 }
7780 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
7781 &string_add_runtime);
7782
7783 // Get the two characters forming the sub string.
7784 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
7785 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
7786
7787 // Try to lookup two character string in symbol table. If it is not found
7788 // just allocate a new one.
7789 Label make_two_character_string;
7790 GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9,
7791 &make_two_character_string);
7792 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7793 __ add(sp, sp, Operand(2 * kPointerSize));
7794 __ Ret();
7795
7796 __ bind(&make_two_character_string);
7797 // Resulting string has length 2 and first chars of two strings
7798 // are combined into single halfword in r2 register.
7799 // So we can fill resulting string without two loops by a single
7800 // halfword store instruction (which assumes that processor is
7801 // in a little endian mode)
7802 __ mov(r6, Operand(2));
7803 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
7804 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
7805 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7806 __ add(sp, sp, Operand(2 * kPointerSize));
7807 __ Ret();
7808
7809 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00007810 // Check if resulting string will be flat.
7811 __ cmp(r6, Operand(String::kMinNonFlatLength));
7812 __ b(lt, &string_add_flat_result);
7813 // Handle exceptionally long strings in the runtime system.
7814 ASSERT((String::kMaxLength & 0x80000000) == 0);
7815 ASSERT(IsPowerOf2(String::kMaxLength + 1));
7816 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
7817 __ cmp(r6, Operand(String::kMaxLength + 1));
7818 __ b(hs, &string_add_runtime);
7819
7820 // If result is not supposed to be flat, allocate a cons string object.
7821 // If both strings are ascii the result is an ascii cons string.
7822 if (!string_check_) {
7823 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7824 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7825 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7826 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7827 }
7828 Label non_ascii, allocated;
7829 ASSERT_EQ(0, kTwoByteStringTag);
7830 __ tst(r4, Operand(kStringEncodingMask));
7831 __ tst(r5, Operand(kStringEncodingMask), ne);
7832 __ b(eq, &non_ascii);
7833
7834 // Allocate an ASCII cons string.
7835 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
7836 __ bind(&allocated);
7837 // Fill the fields of the cons string.
7838 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
7839 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
7840 __ mov(r0, Operand(r7));
7841 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7842 __ add(sp, sp, Operand(2 * kPointerSize));
7843 __ Ret();
7844
7845 __ bind(&non_ascii);
7846 // Allocate a two byte cons string.
7847 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
7848 __ jmp(&allocated);
7849
7850 // Handle creating a flat result. First check that both strings are
7851 // sequential and that they have the same encoding.
7852 // r0: first string
7853 // r1: second string
7854 // r2: length of first string
7855 // r3: length of second string
7856 // r4: first string instance type (if string_check_)
7857 // r5: second string instance type (if string_check_)
7858 // r6: sum of lengths.
7859 __ bind(&string_add_flat_result);
7860 if (!string_check_) {
7861 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7862 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7863 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7864 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7865 }
7866 // Check that both strings are sequential.
7867 ASSERT_EQ(0, kSeqStringTag);
7868 __ tst(r4, Operand(kStringRepresentationMask));
7869 __ tst(r5, Operand(kStringRepresentationMask), eq);
7870 __ b(ne, &string_add_runtime);
7871 // Now check if both strings have the same encoding (ASCII/Two-byte).
7872 // r0: first string.
7873 // r1: second string.
7874 // r2: length of first string.
7875 // r3: length of second string.
7876 // r6: sum of lengths..
7877 Label non_ascii_string_add_flat_result;
7878 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
7879 __ eor(r7, r4, Operand(r5));
7880 __ tst(r7, Operand(kStringEncodingMask));
7881 __ b(ne, &string_add_runtime);
7882 // And see if it's ASCII or two-byte.
7883 __ tst(r4, Operand(kStringEncodingMask));
7884 __ b(eq, &non_ascii_string_add_flat_result);
7885
7886 // Both strings are sequential ASCII strings. We also know that they are
7887 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007888 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00007889 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
7890 // Locate first character of result.
7891 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7892 // Locate first character of first argument.
7893 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7894 // r0: first character of first string.
7895 // r1: second string.
7896 // r2: length of first string.
7897 // r3: length of second string.
7898 // r6: first character of result.
7899 // r7: result string.
7900 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
7901
7902 // Load second argument and locate first character.
7903 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7904 // r1: first character of second string.
7905 // r3: length of second string.
7906 // r6: next character of result.
7907 // r7: result string.
7908 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
7909 __ mov(r0, Operand(r7));
7910 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7911 __ add(sp, sp, Operand(2 * kPointerSize));
7912 __ Ret();
7913
7914 __ bind(&non_ascii_string_add_flat_result);
7915 // Both strings are sequential two byte strings.
7916 // r0: first string.
7917 // r1: second string.
7918 // r2: length of first string.
7919 // r3: length of second string.
7920 // r6: sum of length of strings.
7921 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
7922 // r0: first string.
7923 // r1: second string.
7924 // r2: length of first string.
7925 // r3: length of second string.
7926 // r7: result string.
7927
7928 // Locate first character of result.
7929 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7930 // Locate first character of first argument.
7931 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7932
7933 // r0: first character of first string.
7934 // r1: second string.
7935 // r2: length of first string.
7936 // r3: length of second string.
7937 // r6: first character of result.
7938 // r7: result string.
7939 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
7940
7941 // Locate first character of second argument.
7942 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7943
7944 // r1: first character of second string.
7945 // r3: length of second string.
7946 // r6: next character of result (after copy of first string).
7947 // r7: result string.
7948 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
7949
7950 __ mov(r0, Operand(r7));
7951 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7952 __ add(sp, sp, Operand(2 * kPointerSize));
7953 __ Ret();
7954
7955 // Just jump to runtime to add the two strings.
7956 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007957 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00007958}
7959
7960
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007961#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007963} } // namespace v8::internal