blob: 90428a79555fb59defab2bb24a44c4d243b4b966 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000032#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "debug.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000034#include "ic-inl.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000035#include "jsregexp.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "parser.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000037#include "regexp-macro-assembler.h"
38#include "regexp-stack.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000039#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000041#include "scopes.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000042#include "virtual-frame-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
ager@chromium.org65dad4b2009-04-23 08:48:43 +000047#define __ ACCESS_MASM(masm_)
48
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049static void EmitIdenticalObjectComparison(MacroAssembler* masm,
50 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000051 Condition cc,
52 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000053static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000054 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000055 Label* slow,
56 bool strict);
57static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
58static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000059static void MultiplyByKnownInt(MacroAssembler* masm,
60 Register source,
61 Register destination,
62 int known_int);
63static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000064
65
66
ager@chromium.orge2902be2009-06-08 12:21:35 +000067// -------------------------------------------------------------------------
68// Platform-specific DeferredCode functions.
69
70void DeferredCode::SaveRegisters() {
71 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
72 int action = registers_[i];
73 if (action == kPush) {
74 __ push(RegisterAllocator::ToRegister(i));
75 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
76 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
77 }
78 }
79}
80
81
82void DeferredCode::RestoreRegisters() {
83 // Restore registers in reverse order due to the stack.
84 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
85 int action = registers_[i];
86 if (action == kPush) {
87 __ pop(RegisterAllocator::ToRegister(i));
88 } else if (action != kIgnore) {
89 action &= ~kSyncedFlag;
90 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
91 }
92 }
93}
94
ager@chromium.org3bf7b912008-11-17 09:09:45 +000095
96// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097// CodeGenState implementation.
98
ager@chromium.org7c537e22008-10-16 08:43:32 +000099CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000100 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000101 true_target_(NULL),
102 false_target_(NULL),
103 previous_(NULL) {
104 owner_->set_state(this);
105}
106
107
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108CodeGenState::CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000109 JumpTarget* true_target,
110 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000111 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000112 true_target_(true_target),
113 false_target_(false_target),
114 previous_(owner->state()) {
115 owner_->set_state(this);
116}
117
118
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000119CodeGenState::~CodeGenState() {
120 ASSERT(owner_->state() == this);
121 owner_->set_state(previous_);
122}
123
124
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000125// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000126// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
ager@chromium.org5c838252010-02-19 08:53:10 +0000128CodeGenerator::CodeGenerator(MacroAssembler* masm)
129 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000130 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000131 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 cc_reg_(al),
135 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000136 loop_nesting_(0),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000137 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138}
139
140
141// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000144// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145// cp: callee's context
146
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000147void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000149 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000150 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151
152 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000153 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 ASSERT(allocator_ == NULL);
155 RegisterAllocator register_allocator(this);
156 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000158 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000159 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000160
161 // Adjust for function-level loop nesting.
162 ASSERT_EQ(0, loop_nesting_);
163 loop_nesting_ = info->loop_nesting();
164
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000165 {
166 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 // Entry:
169 // Stack: receiver, arguments
170 // lr: return address
171 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000175 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177#ifdef DEBUG
178 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000180 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000181 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 }
183#endif
184
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000185 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000186 frame_->Enter();
187 // tos: code slot
188
189 // Allocate space for locals and initialize them. This also checks
190 // for stack overflow.
191 frame_->AllocateStackSlots();
192
ager@chromium.org357bf652010-04-12 11:30:10 +0000193 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +0000194 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000195 if (heap_slots > 0) {
196 // Allocate local context.
197 // Get outer context and create a new context based on it.
198 __ ldr(r0, frame_->Function());
199 frame_->EmitPush(r0);
200 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
201 FastNewContextStub stub(heap_slots);
202 frame_->CallStub(&stub, 1);
203 } else {
204 frame_->CallRuntime(Runtime::kNewContext, 1);
205 }
206
207#ifdef DEBUG
208 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000209 __ cmp(r0, cp);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000210 verified_true.Branch(eq);
211 __ stop("NewContext: r0 is expected to be the same as cp");
212 verified_true.Bind();
213#endif
214 // Update context local.
215 __ str(cp, frame_->Context());
216 }
217
218 // TODO(1241774): Improve this code:
219 // 1) only needed if we have a context
220 // 2) no need to recompute context ptr every single time
221 // 3) don't copy parameter operand code from SlotOperand!
222 {
223 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000224 // Note that iteration order is relevant here! If we have the same
225 // parameter twice (e.g., function (x, y, x)), and that parameter
226 // needs to be copied into the context, it must be the last argument
227 // passed to the parameter that needs to be copied. This is a rare
228 // case so we don't check for it, instead we rely on the copying
229 // order: such a parameter is copied repeatedly into the same
230 // context location and thus the last value is what is seen inside
231 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000232 for (int i = 0; i < scope()->num_parameters(); i++) {
233 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234 Slot* slot = par->slot();
235 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000236 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000237 __ ldr(r1, frame_->ParameterAt(i));
238 // Loads r2 with context; used below in RecordWrite.
239 __ str(r1, SlotOperand(slot, r2));
240 // Load the offset into r3.
241 int slot_offset =
242 FixedArray::kHeaderSize + slot->index() * kPointerSize;
243 __ mov(r3, Operand(slot_offset));
244 __ RecordWrite(r2, r3, r1);
245 }
246 }
247 }
248
249 // Store the arguments object. This must happen after context
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000250 // initialization because the arguments object may be stored in
251 // the context.
252 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
253 StoreArgumentsObject(true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000254 }
255
256 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000257 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000258 __ mov(ip, Operand(Factory::the_hole_value()));
259 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000260 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000261 }
262 } else {
263 // When used as the secondary compiler for splitting, r1, cp,
264 // fp, and lr have been pushed on the stack. Adjust the virtual
265 // frame to match this state.
266 frame_->Adjust(4);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000267
268 // Bind all the bailout labels to the beginning of the function.
269 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
270 for (int i = 0; i < bailouts->length(); i++) {
271 __ bind(bailouts->at(i)->label());
272 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000273 }
274
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000275 // Initialize the function return target after the locals are set
276 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000277 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000278 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000280 // Generate code to 'execute' declarations and initialize functions
281 // (source elements). In case of an illegal redeclaration we need to
282 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000283 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000285 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 } else {
287 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000288 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000289 // Bail out if a stack-overflow exception occurred when processing
290 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000291 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 }
293
mads.s.ager31e71382008-08-13 09:32:07 +0000294 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000296 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298
299 // Compile the body of the function in a vanilla state. Don't
300 // bother compiling all the code if the scope has an illegal
301 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000302 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 Comment cmnt(masm_, "[ function body");
304#ifdef DEBUG
305 bool is_builtin = Bootstrapper::IsActive();
306 bool should_trace =
307 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000308 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000309 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000310 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000311 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000313 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 }
316
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000317 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000318 if (has_valid_frame() || function_return_.is_linked()) {
319 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000320 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000321 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000322 // exit
323 // r0: result
324 // sp: stack pointer
325 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000326 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000327 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000328
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 function_return_.Bind();
330 if (FLAG_trace) {
331 // Push the return value on the stack as the parameter.
332 // Runtime::TraceExit returns the parameter as it is.
333 frame_->EmitPush(r0);
334 frame_->CallRuntime(Runtime::kTraceExit, 1);
335 }
336
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000337#ifdef DEBUG
ager@chromium.org4af710e2009-09-15 12:20:11 +0000338 // Add a label for checking the size of the code used for returning.
339 Label check_exit_codesize;
340 masm_->bind(&check_exit_codesize);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000341#endif
342 // Make sure that the constant pool is not emitted inside of the return
343 // sequence.
344 { Assembler::BlockConstPoolScope block_const_pool(masm_);
345 // Tear down the frame which will restore the caller's frame pointer and
346 // the link register.
347 frame_->Exit();
ager@chromium.org4af710e2009-09-15 12:20:11 +0000348
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000349 // Here we use masm_-> instead of the __ macro to avoid the code coverage
350 // tool from instrumenting as we rely on the code size here.
351 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
352 masm_->add(sp, sp, Operand(sp_delta));
353 masm_->Jump(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000354
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000355#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000356 // Check that the size of the code used for returning matches what is
357 // expected by the debugger. If the sp_delts above cannot be encoded in
358 // the add instruction the add will generate two instructions.
359 int return_sequence_length =
360 masm_->InstructionsGeneratedSince(&check_exit_codesize);
361 CHECK(return_sequence_length == Assembler::kJSReturnSequenceLength ||
362 return_sequence_length == Assembler::kJSReturnSequenceLength + 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000363#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000364 }
mads.s.ager31e71382008-08-13 09:32:07 +0000365 }
366
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000367 // Adjust for function-level loop nesting.
368 ASSERT(loop_nesting_ == info->loop_nesting());
369 loop_nesting_ = 0;
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 ASSERT(!has_cc());
373 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000374 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 ASSERT(!function_return_is_shadowed_);
376 function_return_.Unuse();
377 DeleteFrame();
378
379 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000380 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000381 ProcessDeferred();
382 }
383
384 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385}
386
387
ager@chromium.org7c537e22008-10-16 08:43:32 +0000388MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
389 // Currently, this assertion will fail if we try to assign to
390 // a constant variable that is constant because it is read-only
391 // (such as the variable referring to a named function expression).
392 // We need to implement assignments to read-only variables.
393 // Ideally, we should do this during AST generation (by converting
394 // such assignments into expression statements); however, in general
395 // we may not be able to make the decision until past AST generation,
396 // that is when the entire program is known.
397 ASSERT(slot != NULL);
398 int index = slot->index();
399 switch (slot->type()) {
400 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000401 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000402
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000403 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000404 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000405
406 case Slot::CONTEXT: {
407 // Follow the context chain if necessary.
408 ASSERT(!tmp.is(cp)); // do not overwrite context register
409 Register context = cp;
410 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000411 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000412 // Load the closure.
413 // (All contexts, even 'with' contexts, have a closure,
414 // and it is the same for all contexts inside a function.
415 // There is no need to go to the function context first.)
416 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
417 // Load the function context (which is the incoming, outer context).
418 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
419 context = tmp;
420 }
421 // We may have a 'with' context now. Get the function context.
422 // (In fact this mov may never be the needed, since the scope analysis
423 // may not permit a direct context access in this case and thus we are
424 // always at a function context. However it is safe to dereference be-
425 // cause the function context of a function context is itself. Before
426 // deleting this mov we should try to create a counter-example first,
427 // though...)
428 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
429 return ContextOperand(tmp, index);
430 }
431
432 default:
433 UNREACHABLE();
434 return MemOperand(r0, 0);
435 }
436}
437
438
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000439MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
440 Slot* slot,
441 Register tmp,
442 Register tmp2,
443 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000444 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000445 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000446
ager@chromium.org381abbb2009-02-25 13:23:22 +0000447 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
448 if (s->num_heap_slots() > 0) {
449 if (s->calls_eval()) {
450 // Check that extension is NULL.
451 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
452 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000453 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000454 }
455 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
456 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
457 context = tmp;
458 }
459 }
460 // Check that last 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 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000465 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000466}
467
468
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000469// Loads a value on TOS. If it is a boolean value, the result may have been
470// (partially) translated into branches, or it may have set the condition
471// code register. If force_cc is set, the value is forced to set the
472// condition code register and no value is pushed. If the condition code
473// register was set, has_cc() is true and cc_reg_ contains the condition to
474// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000475void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000476 JumpTarget* true_target,
477 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000478 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000479 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000482 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000483 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000484
485 // If we hit a stack overflow, we may not have actually visited
486 // the expression. In that case, we ensure that we have a
487 // valid-looking frame state because we will continue to generate
488 // code as we unwind the C++ stack.
489 //
490 // It's possible to have both a stack overflow and a valid frame
491 // state (eg, a subexpression overflowed, visiting it returned
492 // with a dummied frame state, and visiting this expression
493 // returned with a normal-looking state).
494 if (HasStackOverflow() &&
495 has_valid_frame() &&
496 !has_cc() &&
497 frame_->height() == original_height) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000498 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 true_target->Jump();
500 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000501 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000503 // Convert the TOS value to a boolean in the condition code register.
504 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506 ASSERT(!force_cc || !has_valid_frame() || has_cc());
507 ASSERT(!has_valid_frame() ||
508 (has_cc() && frame_->height() == original_height) ||
509 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510}
511
512
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000513void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514#ifdef DEBUG
515 int original_height = frame_->height();
516#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000517 JumpTarget true_target;
518 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000519 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
521 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000522 // Convert cc_reg_ into a boolean value.
ager@chromium.org357bf652010-04-12 11:30:10 +0000523 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000524 JumpTarget loaded;
525 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000527 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
529 loaded.Jump();
530 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000531 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 frame_->EmitPush(r0);
533 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 cc_reg_ = al;
535 }
536
537 if (true_target.is_linked() || false_target.is_linked()) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000538 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000539 // We have at least one condition value that has been "translated"
540 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000541 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 if (frame_ != NULL) {
543 loaded.Jump(); // Don't lose the current TOS.
544 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000548 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000549 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000552 // If both "true" and "false" need to be loaded jump across the code for
553 // "false".
554 if (both) {
555 loaded.Jump();
556 }
557 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000559 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000560 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 // A value is loaded on all paths reaching this point.
564 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000566 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569}
570
571
ager@chromium.org7c537e22008-10-16 08:43:32 +0000572void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000573 Register reg = frame_->GetTOSRegister();
574 __ ldr(reg, GlobalObject());
575 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000579void CodeGenerator::LoadGlobalReceiver(Register scratch) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000580 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000581 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
582 __ ldr(scratch,
583 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000585}
586
587
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000588ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
589 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
590 ASSERT(scope()->arguments_shadow() != NULL);
591 // We don't want to do lazy arguments allocation for functions that
592 // have heap-allocated contexts, because it interfers with the
593 // uninitialized const tracking in the context objects.
594 return (scope()->num_heap_slots() > 0)
595 ? EAGER_ARGUMENTS_ALLOCATION
596 : LAZY_ARGUMENTS_ALLOCATION;
597}
598
599
600void CodeGenerator::StoreArgumentsObject(bool initial) {
601 VirtualFrame::SpilledScope spilled_scope(frame_);
602
603 ArgumentsAllocationMode mode = ArgumentsMode();
604 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
605
606 Comment cmnt(masm_, "[ store arguments object");
607 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
608 // When using lazy arguments allocation, we store the hole value
609 // as a sentinel indicating that the arguments object hasn't been
610 // allocated yet.
611 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
612 frame_->EmitPush(ip);
613 } else {
614 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
615 __ ldr(r2, frame_->Function());
616 // The receiver is below the arguments, the return address, and the
617 // frame pointer on the stack.
618 const int kReceiverDisplacement = 2 + scope()->num_parameters();
619 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
620 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
621 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000622 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000623 frame_->CallStub(&stub, 3);
624 frame_->EmitPush(r0);
625 }
626
627 Variable* arguments = scope()->arguments()->var();
628 Variable* shadow = scope()->arguments_shadow()->var();
629 ASSERT(arguments != NULL && arguments->slot() != NULL);
630 ASSERT(shadow != NULL && shadow->slot() != NULL);
631 JumpTarget done;
632 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
633 // We have to skip storing into the arguments slot if it has
634 // already been written to. This can happen if the a function
635 // has a local variable named 'arguments'.
636 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
637 frame_->EmitPop(r0);
638 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
639 __ cmp(r0, ip);
640 done.Branch(ne);
641 }
642 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
643 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
644 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
645}
646
647
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000648void CodeGenerator::LoadTypeofExpression(Expression* expr) {
649 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.org357bf652010-04-12 11:30:10 +0000650 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000651 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000653 // For a global variable we build the property reference
654 // <global>.<variable> and perform a (regular non-contextual) property
655 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
657 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000658 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000659 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000660 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000661 } else if (variable != NULL && variable->slot() != NULL) {
662 // For a variable that rewrites to a slot, we signal it is the immediate
663 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000664 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000665 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000667 // Anything else can be handled normally.
668 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 }
670}
671
672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673Reference::Reference(CodeGenerator* cgen,
674 Expression* expression,
675 bool persist_after_get)
676 : cgen_(cgen),
677 expression_(expression),
678 type_(ILLEGAL),
679 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 cgen->LoadReference(this);
681}
682
683
684Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000685 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
ager@chromium.org7c537e22008-10-16 08:43:32 +0000689void CodeGenerator::LoadReference(Reference* ref) {
690 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 Expression* e = ref->expression();
692 Property* property = e->AsProperty();
693 Variable* var = e->AsVariableProxy()->AsVariable();
694
695 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000696 // The expression is either a property or a variable proxy that rewrites
697 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000698 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000699 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 ref->set_type(Reference::NAMED);
701 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000702 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 ref->set_type(Reference::KEYED);
704 }
705 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000706 // The expression is a variable proxy that does not rewrite to a
707 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 LoadGlobal();
710 ref->set_type(Reference::NAMED);
711 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000712 ASSERT(var->slot() != NULL);
713 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 }
715 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000716 // Anything else is a runtime error.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000717 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000718 LoadAndSpill(e);
719 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 }
721}
722
723
ager@chromium.org7c537e22008-10-16 08:43:32 +0000724void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000727 if (size == 0) return;
728
729 // Pop a reference from the stack while preserving TOS.
730 VirtualFrame::RegisterAllocationScope scope(this);
731 Comment cmnt(masm_, "[ UnloadReference");
732 if (size > 0) {
733 Register tos = frame_->PopToRegister();
734 frame_->Drop(size);
735 frame_->EmitPush(tos);
736 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737}
738
739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
741// register to a boolean in the condition code register. The code
742// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743void CodeGenerator::ToBoolean(JumpTarget* true_target,
744 JumpTarget* false_target) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000745 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000746 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
750 // Fast case checks
751
mads.s.ager31e71382008-08-13 09:32:07 +0000752 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000753 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
754 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
mads.s.ager31e71382008-08-13 09:32:07 +0000757 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000758 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
759 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
mads.s.ager31e71382008-08-13 09:32:07 +0000762 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000763 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
764 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000765 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766
mads.s.ager31e71382008-08-13 09:32:07 +0000767 // Check if the value is a smi.
768 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000770 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000771 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
773 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000774 frame_->EmitPush(r0);
775 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000776 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000777 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
778 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779
780 cc_reg_ = ne;
781}
782
783
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000784void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000785 OverwriteMode overwrite_mode,
786 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000787 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000788 // sp[0] : y
789 // sp[1] : x
790 // result : r0
791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 // Stub is entered with a call: 'return address' is in lr.
793 switch (op) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000794 case Token::ADD:
795 case Token::SUB:
796 case Token::MUL:
797 case Token::DIV:
798 case Token::MOD:
799 case Token::BIT_OR:
800 case Token::BIT_AND:
801 case Token::BIT_XOR:
802 case Token::SHL:
803 case Token::SHR:
804 case Token::SAR: {
805 frame_->EmitPop(r0); // r0 : y
806 frame_->EmitPop(r1); // r1 : x
807 GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs);
808 frame_->CallStub(&stub, 0);
809 break;
810 }
811
812 case Token::COMMA:
813 frame_->EmitPop(r0);
814 // Simply discard left value.
815 frame_->Drop();
816 break;
817
818 default:
819 // Other cases should have been handled before this point.
820 UNREACHABLE();
821 break;
822 }
823}
824
825
826void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op,
827 OverwriteMode overwrite_mode,
828 int constant_rhs) {
829 // top of virtual frame: y
830 // 2nd elt. on virtual frame : x
831 // result : top of virtual frame
832
833 // Stub is entered with a call: 'return address' is in lr.
834 switch (op) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 case Token::ADD: // fall through.
836 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000837 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000838 case Token::DIV:
839 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000840 case Token::BIT_OR:
841 case Token::BIT_AND:
842 case Token::BIT_XOR:
843 case Token::SHL:
844 case Token::SHR:
845 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000846 Register rhs = frame_->PopToRegister();
847 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
848 {
849 VirtualFrame::SpilledScope spilled_scope(frame_);
850 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
851 frame_->CallStub(&stub, 0);
852 }
853 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 break;
855 }
856
ager@chromium.org357bf652010-04-12 11:30:10 +0000857 case Token::COMMA: {
858 Register scratch = frame_->PopToRegister();
859 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000860 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000861 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865 default:
866 // Other cases should have been handled before this point.
867 UNREACHABLE();
868 break;
869 }
870}
871
872
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000873class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000874 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000875 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000876 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000877 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000878 OverwriteMode overwrite_mode,
879 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000880 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000881 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000882 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000883 overwrite_mode_(overwrite_mode),
884 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 set_comment("[ DeferredInlinedSmiOperation");
886 }
887
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000888 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889
890 private:
891 Token::Value op_;
892 int value_;
893 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000894 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000895 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896};
897
898
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000899void DeferredInlineSmiOperation::Generate() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000900 Register lhs = r1;
901 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902 switch (op_) {
903 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000906 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 __ mov(r1, Operand(Smi::FromInt(value_)));
908 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000909 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000910 __ mov(r0, Operand(Smi::FromInt(value_)));
911 }
912 break;
913 }
914
915 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000916 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000917 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000918 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000919 __ mov(r1, Operand(Smi::FromInt(value_)));
920 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000921 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000922 __ mov(r0, Operand(Smi::FromInt(value_)));
923 }
924 break;
925 }
926
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000927 // For these operations there is no optimistic operation that needs to be
928 // reverted.
929 case Token::MUL:
930 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931 case Token::BIT_OR:
932 case Token::BIT_XOR:
933 case Token::BIT_AND: {
934 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000935 if (tos_register_.is(r0)) {
936 __ mov(r1, Operand(Smi::FromInt(value_)));
937 } else {
938 ASSERT(tos_register_.is(r1));
939 __ mov(r0, Operand(Smi::FromInt(value_)));
940 lhs = r0;
941 rhs = r1;
942 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000943 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000944 if (tos_register_.is(r1)) {
945 __ mov(r0, Operand(Smi::FromInt(value_)));
946 } else {
947 ASSERT(tos_register_.is(r0));
948 __ mov(r1, Operand(Smi::FromInt(value_)));
949 lhs = r0;
950 rhs = r1;
951 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000952 }
953 break;
954 }
955
956 case Token::SHL:
957 case Token::SHR:
958 case Token::SAR: {
959 if (!reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000960 if (tos_register_.is(r1)) {
961 __ mov(r0, Operand(Smi::FromInt(value_)));
962 } else {
963 ASSERT(tos_register_.is(r0));
964 __ mov(r1, Operand(Smi::FromInt(value_)));
965 lhs = r0;
966 rhs = r1;
967 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000968 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000969 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000970 }
971 break;
972 }
973
974 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000975 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000976 UNREACHABLE();
977 break;
978 }
979
ager@chromium.org357bf652010-04-12 11:30:10 +0000980 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000981 __ CallStub(&stub);
ager@chromium.org357bf652010-04-12 11:30:10 +0000982 // The generic stub returns its value in r0, but that's not
983 // necessarily what we want. We want whatever the inlined code
984 // expected, which is that the answer is in the same register as
985 // the operand was.
986 __ Move(tos_register_, r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000987}
988
989
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000990static bool PopCountLessThanEqual2(unsigned int x) {
991 x &= x - 1;
992 return (x & (x - 1)) == 0;
993}
994
995
996// Returns the index of the lowest bit set.
997static int BitPosition(unsigned x) {
998 int bit_posn = 0;
999 while ((x & 0xf) == 0) {
1000 bit_posn += 4;
1001 x >>= 4;
1002 }
1003 while ((x & 1) == 0) {
1004 bit_posn++;
1005 x >>= 1;
1006 }
1007 return bit_posn;
1008}
1009
1010
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001011void CodeGenerator::SmiOperation(Token::Value op,
1012 Handle<Object> value,
1013 bool reversed,
1014 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001015 int int_value = Smi::cast(*value)->value();
1016
1017 bool something_to_inline;
1018 switch (op) {
1019 case Token::ADD:
1020 case Token::SUB:
1021 case Token::BIT_AND:
1022 case Token::BIT_OR:
1023 case Token::BIT_XOR: {
1024 something_to_inline = true;
1025 break;
1026 }
1027 case Token::SHL:
1028 case Token::SHR:
1029 case Token::SAR: {
1030 if (reversed) {
1031 something_to_inline = false;
1032 } else {
1033 something_to_inline = true;
1034 }
1035 break;
1036 }
1037 case Token::MOD: {
1038 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1039 something_to_inline = false;
1040 } else {
1041 something_to_inline = true;
1042 }
1043 break;
1044 }
1045 case Token::MUL: {
1046 if (!IsEasyToMultiplyBy(int_value)) {
1047 something_to_inline = false;
1048 } else {
1049 something_to_inline = true;
1050 }
1051 break;
1052 }
1053 default: {
1054 something_to_inline = false;
1055 break;
1056 }
1057 }
1058
1059 if (!something_to_inline) {
1060 if (!reversed) {
1061 // Push the rhs onto the virtual frame by putting it in a TOS register.
1062 Register rhs = frame_->GetTOSRegister();
1063 __ mov(rhs, Operand(value));
1064 frame_->EmitPush(rhs);
1065 VirtualFrameBinaryOperation(op, mode, int_value);
1066 } else {
1067 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1068 // at most one pop, the rest takes place in TOS registers.
1069 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1070 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1071 __ mov(lhs, Operand(value));
1072 frame_->EmitPush(lhs);
1073 frame_->EmitPush(rhs);
1074 VirtualFrameBinaryOperation(op, mode, kUnknownIntValue);
1075 }
1076 return;
1077 }
1078
1079 // We move the top of stack to a register (normally no move is invoved).
1080 Register tos = frame_->PopToRegister();
1081 // All other registers are spilled. The deferred code expects one argument
1082 // in a register and all other values are flushed to the stack. The
1083 // answer is returned in the same register that the top of stack argument was
1084 // in.
1085 frame_->SpillAll();
1086
1087 switch (op) {
1088 case Token::ADD: {
1089 DeferredCode* deferred =
1090 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1091
1092 __ add(tos, tos, Operand(value), SetCC);
1093 deferred->Branch(vs);
1094 __ tst(tos, Operand(kSmiTagMask));
1095 deferred->Branch(ne);
1096 deferred->BindExit();
1097 frame_->EmitPush(tos);
1098 break;
1099 }
1100
1101 case Token::SUB: {
1102 DeferredCode* deferred =
1103 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1104
1105 if (reversed) {
1106 __ rsb(tos, tos, Operand(value), SetCC);
1107 } else {
1108 __ sub(tos, tos, Operand(value), SetCC);
1109 }
1110 deferred->Branch(vs);
1111 __ tst(tos, Operand(kSmiTagMask));
1112 deferred->Branch(ne);
1113 deferred->BindExit();
1114 frame_->EmitPush(tos);
1115 break;
1116 }
1117
1118
1119 case Token::BIT_OR:
1120 case Token::BIT_XOR:
1121 case Token::BIT_AND: {
1122 DeferredCode* deferred =
1123 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1124 __ tst(tos, Operand(kSmiTagMask));
1125 deferred->Branch(ne);
1126 switch (op) {
1127 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1128 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
1129 case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break;
1130 default: UNREACHABLE();
1131 }
1132 deferred->BindExit();
1133 frame_->EmitPush(tos);
1134 break;
1135 }
1136
1137 case Token::SHL:
1138 case Token::SHR:
1139 case Token::SAR: {
1140 ASSERT(!reversed);
1141 Register scratch = VirtualFrame::scratch0();
1142 Register scratch2 = VirtualFrame::scratch1();
1143 int shift_value = int_value & 0x1f; // least significant 5 bits
1144 DeferredCode* deferred =
1145 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
1146 __ tst(tos, Operand(kSmiTagMask));
1147 deferred->Branch(ne);
1148 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags
1149 switch (op) {
1150 case Token::SHL: {
1151 if (shift_value != 0) {
1152 __ mov(scratch, Operand(scratch, LSL, shift_value));
1153 }
1154 // check that the *signed* result fits in a smi
1155 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1156 deferred->Branch(mi);
1157 break;
1158 }
1159 case Token::SHR: {
1160 // LSR by immediate 0 means shifting 32 bits.
1161 if (shift_value != 0) {
1162 __ mov(scratch, Operand(scratch, LSR, shift_value));
1163 }
1164 // check that the *unsigned* result fits in a smi
1165 // neither of the two high-order bits can be set:
1166 // - 0x80000000: high bit would be lost when smi tagging
1167 // - 0x40000000: this number would convert to negative when
1168 // smi tagging these two cases can only happen with shifts
1169 // by 0 or 1 when handed a valid smi
1170 __ tst(scratch, Operand(0xc0000000));
1171 deferred->Branch(ne);
1172 break;
1173 }
1174 case Token::SAR: {
1175 if (shift_value != 0) {
1176 // ASR by immediate 0 means shifting 32 bits.
1177 __ mov(scratch, Operand(scratch, ASR, shift_value));
1178 }
1179 break;
1180 }
1181 default: UNREACHABLE();
1182 }
1183 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1184 deferred->BindExit();
1185 frame_->EmitPush(tos);
1186 break;
1187 }
1188
1189 case Token::MOD: {
1190 ASSERT(!reversed);
1191 ASSERT(int_value >= 2);
1192 ASSERT(IsPowerOf2(int_value));
1193 DeferredCode* deferred =
1194 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1195 unsigned mask = (0x80000000u | kSmiTagMask);
1196 __ tst(tos, Operand(mask));
1197 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1198 mask = (int_value << kSmiTagSize) - 1;
1199 __ and_(tos, tos, Operand(mask));
1200 deferred->BindExit();
1201 frame_->EmitPush(tos);
1202 break;
1203 }
1204
1205 case Token::MUL: {
1206 ASSERT(IsEasyToMultiplyBy(int_value));
1207 DeferredCode* deferred =
1208 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1209 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1210 max_smi_that_wont_overflow <<= kSmiTagSize;
1211 unsigned mask = 0x80000000u;
1212 while ((mask & max_smi_that_wont_overflow) == 0) {
1213 mask |= mask >> 1;
1214 }
1215 mask |= kSmiTagMask;
1216 // This does a single mask that checks for a too high value in a
1217 // conservative way and for a non-Smi. It also filters out negative
1218 // numbers, unfortunately, but since this code is inline we prefer
1219 // brevity to comprehensiveness.
1220 __ tst(tos, Operand(mask));
1221 deferred->Branch(ne);
1222 MultiplyByKnownInt(masm_, tos, tos, int_value);
1223 deferred->BindExit();
1224 frame_->EmitPush(tos);
1225 break;
1226 }
1227
1228 default:
1229 UNREACHABLE();
1230 break;
1231 }
1232}
1233
1234
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001235void CodeGenerator::Comparison(Condition cc,
1236 Expression* left,
1237 Expression* right,
1238 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001239 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001240
ager@chromium.org357bf652010-04-12 11:30:10 +00001241 if (left != NULL) Load(left);
1242 if (right != NULL) Load(right);
1243
mads.s.ager31e71382008-08-13 09:32:07 +00001244 // sp[0] : y
1245 // sp[1] : x
1246 // result : cc register
1247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 // Strict only makes sense for equality comparisons.
1249 ASSERT(!strict || cc == eq);
1250
ager@chromium.org357bf652010-04-12 11:30:10 +00001251 Register lhs;
1252 Register rhs;
1253
1254 // We load the top two stack positions into registers chosen by the virtual
1255 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001256 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1257 if (cc == gt || cc == le) {
1258 cc = ReverseCondition(cc);
ager@chromium.org357bf652010-04-12 11:30:10 +00001259 lhs = frame_->PopToRegister();
1260 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001261 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001262 rhs = frame_->PopToRegister();
1263 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001264 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001265
1266 ASSERT(rhs.is(r0) || rhs.is(r1));
1267 ASSERT(lhs.is(r0) || lhs.is(r1));
1268
1269 // Now we have the two sides in r0 and r1. We flush any other registers
1270 // because the stub doesn't know about register allocation.
1271 frame_->SpillAll();
1272 Register scratch = VirtualFrame::scratch0();
1273 __ orr(scratch, lhs, Operand(rhs));
1274 __ tst(scratch, Operand(kSmiTagMask));
1275 JumpTarget smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001276 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001278 // Perform non-smi comparison by stub.
1279 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1280 // We call with 0 args because there are 0 on the stack.
ager@chromium.org357bf652010-04-12 11:30:10 +00001281 if (!rhs.is(r0)) {
1282 __ Swap(rhs, lhs, ip);
1283 }
1284
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001285 CompareStub stub(cc, strict);
1286 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001287 __ cmp(r0, Operand(0));
ager@chromium.org357bf652010-04-12 11:30:10 +00001288 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001291 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001292 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001293 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001295 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 cc_reg_ = cc;
1297}
1298
1299
mads.s.ager31e71382008-08-13 09:32:07 +00001300// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001301void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001302 CallFunctionFlags flags,
1303 int position) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001304 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 int arg_count = args->length();
1307 for (int i = 0; i < arg_count; i++) {
1308 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
kasper.lund7276f142008-07-30 08:49:36 +00001311 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001312 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
kasper.lund7276f142008-07-30 08:49:36 +00001314 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001315 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001316 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001317 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318
1319 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001320 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322}
1323
1324
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001325void CodeGenerator::CallApplyLazy(Expression* applicand,
1326 Expression* receiver,
1327 VariableProxy* arguments,
1328 int position) {
1329 // An optimized implementation of expressions of the form
1330 // x.apply(y, arguments).
1331 // If the arguments object of the scope has not been allocated,
1332 // and x.apply is Function.prototype.apply, this optimization
1333 // just copies y and the arguments of the current function on the
1334 // stack, as receiver and arguments, and calls x.
1335 // In the implementation comments, we call x the applicand
1336 // and y the receiver.
1337 VirtualFrame::SpilledScope spilled_scope(frame_);
1338
1339 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1340 ASSERT(arguments->IsArguments());
1341
1342 // Load applicand.apply onto the stack. This will usually
1343 // give us a megamorphic load site. Not super, but it works.
1344 LoadAndSpill(applicand);
1345 Handle<String> name = Factory::LookupAsciiSymbol("apply");
1346 __ mov(r2, Operand(name));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001347 __ ldr(r0, MemOperand(sp, 0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001348 frame_->CallLoadIC(RelocInfo::CODE_TARGET);
1349 frame_->EmitPush(r0);
1350
1351 // Load the receiver and the existing arguments object onto the
1352 // expression stack. Avoid allocating the arguments object here.
1353 LoadAndSpill(receiver);
1354 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1355
1356 // Emit the source position information after having loaded the
1357 // receiver and the arguments.
1358 CodeForSourcePosition(position);
1359 // Contents of the stack at this point:
1360 // sp[0]: arguments object of the current function or the hole.
1361 // sp[1]: receiver
1362 // sp[2]: applicand.apply
1363 // sp[3]: applicand.
1364
1365 // Check if the arguments object has been lazily allocated
1366 // already. If so, just use that instead of copying the arguments
1367 // from the stack. This also deals with cases where a local variable
1368 // named 'arguments' has been introduced.
1369 __ ldr(r0, MemOperand(sp, 0));
1370
1371 Label slow, done;
1372 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1373 __ cmp(ip, r0);
1374 __ b(ne, &slow);
1375
1376 Label build_args;
1377 // Get rid of the arguments object probe.
1378 frame_->Drop();
1379 // Stack now has 3 elements on it.
1380 // Contents of stack at this point:
1381 // sp[0]: receiver
1382 // sp[1]: applicand.apply
1383 // sp[2]: applicand.
1384
1385 // Check that the receiver really is a JavaScript object.
1386 __ ldr(r0, MemOperand(sp, 0));
1387 __ BranchOnSmi(r0, &build_args);
1388 // We allow all JSObjects including JSFunctions. As long as
1389 // JS_FUNCTION_TYPE is the last instance type and it is right
1390 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1391 // bound.
1392 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1393 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1394 __ CompareObjectType(r0, r1, r2, FIRST_JS_OBJECT_TYPE);
1395 __ b(lt, &build_args);
1396
1397 // Check that applicand.apply is Function.prototype.apply.
1398 __ ldr(r0, MemOperand(sp, kPointerSize));
1399 __ BranchOnSmi(r0, &build_args);
1400 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1401 __ b(ne, &build_args);
1402 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
1403 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
1404 __ ldr(r1, FieldMemOperand(r0, SharedFunctionInfo::kCodeOffset));
1405 __ cmp(r1, Operand(apply_code));
1406 __ b(ne, &build_args);
1407
1408 // Check that applicand is a function.
1409 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1410 __ BranchOnSmi(r1, &build_args);
1411 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1412 __ b(ne, &build_args);
1413
1414 // Copy the arguments to this function possibly from the
1415 // adaptor frame below it.
1416 Label invoke, adapted;
1417 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1418 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1419 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1420 __ b(eq, &adapted);
1421
1422 // No arguments adaptor frame. Copy fixed number of arguments.
1423 __ mov(r0, Operand(scope()->num_parameters()));
1424 for (int i = 0; i < scope()->num_parameters(); i++) {
1425 __ ldr(r2, frame_->ParameterAt(i));
1426 __ push(r2);
1427 }
1428 __ jmp(&invoke);
1429
1430 // Arguments adaptor frame present. Copy arguments from there, but
1431 // avoid copying too many arguments to avoid stack overflows.
1432 __ bind(&adapted);
1433 static const uint32_t kArgumentsLimit = 1 * KB;
1434 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1435 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1436 __ mov(r3, r0);
1437 __ cmp(r0, Operand(kArgumentsLimit));
1438 __ b(gt, &build_args);
1439
1440 // Loop through the arguments pushing them onto the execution
1441 // stack. We don't inform the virtual frame of the push, so we don't
1442 // have to worry about getting rid of the elements from the virtual
1443 // frame.
1444 Label loop;
1445 // r3 is a small non-negative integer, due to the test above.
1446 __ cmp(r3, Operand(0));
1447 __ b(eq, &invoke);
1448 // Compute the address of the first argument.
1449 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1450 __ add(r2, r2, Operand(kPointerSize));
1451 __ bind(&loop);
1452 // Post-decrement argument address by kPointerSize on each iteration.
1453 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1454 __ push(r4);
1455 __ sub(r3, r3, Operand(1), SetCC);
1456 __ b(gt, &loop);
1457
1458 // Invoke the function.
1459 __ bind(&invoke);
1460 ParameterCount actual(r0);
1461 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1462 // Drop applicand.apply and applicand from the stack, and push
1463 // the result of the function call, but leave the spilled frame
1464 // unchanged, with 3 elements, so it is correct when we compile the
1465 // slow-case code.
1466 __ add(sp, sp, Operand(2 * kPointerSize));
1467 __ push(r0);
1468 // Stack now has 1 element:
1469 // sp[0]: result
1470 __ jmp(&done);
1471
1472 // Slow-case: Allocate the arguments object since we know it isn't
1473 // there, and fall-through to the slow-case where we call
1474 // applicand.apply.
1475 __ bind(&build_args);
1476 // Stack now has 3 elements, because we have jumped from where:
1477 // sp[0]: receiver
1478 // sp[1]: applicand.apply
1479 // sp[2]: applicand.
1480 StoreArgumentsObject(false);
1481
1482 // Stack and frame now have 4 elements.
1483 __ bind(&slow);
1484
1485 // Generic computation of x.apply(y, args) with no special optimization.
1486 // Flip applicand.apply and applicand on the stack, so
1487 // applicand looks like the receiver of the applicand.apply call.
1488 // Then process it as a normal function call.
1489 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1490 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1491 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1492 __ str(r1, MemOperand(sp, 3 * kPointerSize));
1493
1494 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1495 frame_->CallStub(&call_function, 3);
1496 // The function and its two arguments have been dropped.
1497 frame_->Drop(); // Drop the receiver as well.
1498 frame_->EmitPush(r0);
1499 // Stack now has 1 element:
1500 // sp[0]: result
1501 __ bind(&done);
1502
1503 // Restore the context register after a call.
1504 __ ldr(cp, frame_->Context());
1505}
1506
1507
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001508void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001509 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 ASSERT(has_cc());
1511 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001512 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 cc_reg_ = al;
1514}
1515
1516
ager@chromium.org7c537e22008-10-16 08:43:32 +00001517void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001518 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001519 Comment cmnt(masm_, "[ check stack");
1520 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1521 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1522 // the implicit 8 byte offset that always applies to operations with pc and
1523 // gives a return address 12 bytes down.
1524 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1525 masm_->cmp(sp, Operand(ip));
1526 StackCheckStub stub;
1527 // Call the stub if lower.
1528 masm_->mov(pc,
1529 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1530 RelocInfo::CODE_TARGET),
1531 LeaveCC,
1532 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533}
1534
1535
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001536void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1537#ifdef DEBUG
1538 int original_height = frame_->height();
1539#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001540 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001541 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1542 VisitAndSpill(statements->at(i));
1543 }
1544 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1545}
1546
1547
ager@chromium.org7c537e22008-10-16 08:43:32 +00001548void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001549#ifdef DEBUG
1550 int original_height = frame_->height();
1551#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001552 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001554 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001555 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 VisitStatementsAndSpill(node->statements());
1557 if (node->break_target()->is_linked()) {
1558 node->break_target()->Bind();
1559 }
1560 node->break_target()->Unuse();
1561 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562}
1563
1564
ager@chromium.org7c537e22008-10-16 08:43:32 +00001565void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001566 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001567 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001568 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001569 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001570 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001571 frame_->EmitPush(r0);
1572 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001573 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574}
1575
1576
ager@chromium.org7c537e22008-10-16 08:43:32 +00001577void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001578#ifdef DEBUG
1579 int original_height = frame_->height();
1580#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001581 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 Comment cmnt(masm_, "[ Declaration");
1583 Variable* var = node->proxy()->var();
1584 ASSERT(var != NULL); // must have been resolved
1585 Slot* slot = var->slot();
1586
1587 // If it was not possible to allocate the variable at compile time,
1588 // we need to "declare" it at runtime to make sure it actually
1589 // exists in the local context.
1590 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1591 // Variables with a "LOOKUP" slot were introduced as non-locals
1592 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001593 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001596 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 // Declaration nodes are always declared in only two modes.
1599 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1600 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001601 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001602 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 // Push initial value, if any.
1604 // Note: For variables we must not push an initial value (such as
1605 // 'undefined') because we may have a (legal) redeclaration and we
1606 // must not destroy the current value.
1607 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001608 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001609 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001611 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001613 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001614 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001617 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001618 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 return;
1620 }
1621
1622 ASSERT(!var->is_global());
1623
1624 // If we have a function or a constant, we need to initialize the variable.
1625 Expression* val = NULL;
1626 if (node->mode() == Variable::CONST) {
1627 val = new Literal(Factory::the_hole_value());
1628 } else {
1629 val = node->fun(); // NULL if we don't have a function
1630 }
1631
1632 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001633 {
1634 // Set initial value.
1635 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001636 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001637 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001638 }
1639 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001642 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643}
1644
1645
ager@chromium.org7c537e22008-10-16 08:43:32 +00001646void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001647#ifdef DEBUG
1648 int original_height = frame_->height();
1649#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001650 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001652 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 Expression* expression = node->expression();
1654 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001655 LoadAndSpill(expression);
1656 frame_->Drop();
1657 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658}
1659
1660
ager@chromium.org7c537e22008-10-16 08:43:32 +00001661void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001662#ifdef DEBUG
1663 int original_height = frame_->height();
1664#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001665 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001667 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001669 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670}
1671
1672
ager@chromium.org7c537e22008-10-16 08:43:32 +00001673void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001674#ifdef DEBUG
1675 int original_height = frame_->height();
1676#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001677 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679 // Generate different code depending on which parts of the if statement
1680 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 bool has_then_stm = node->HasThenStatement();
1682 bool has_else_stm = node->HasElseStatement();
1683
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001684 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001686 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001688 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001689 JumpTarget then;
1690 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001692 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001693 if (frame_ != NULL) {
1694 Branch(false, &else_);
1695 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001697 if (frame_ != NULL || then.is_linked()) {
1698 then.Bind();
1699 VisitAndSpill(node->then_statement());
1700 }
1701 if (frame_ != NULL) {
1702 exit.Jump();
1703 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001705 if (else_.is_linked()) {
1706 else_.Bind();
1707 VisitAndSpill(node->else_statement());
1708 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
1710 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001711 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001713 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001715 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001716 if (frame_ != NULL) {
1717 Branch(false, &exit);
1718 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001720 if (frame_ != NULL || then.is_linked()) {
1721 then.Bind();
1722 VisitAndSpill(node->then_statement());
1723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
1725 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001726 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001728 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001730 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001731 if (frame_ != NULL) {
1732 Branch(true, &exit);
1733 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001735 if (frame_ != NULL || else_.is_linked()) {
1736 else_.Bind();
1737 VisitAndSpill(node->else_statement());
1738 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
1740 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001741 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 ASSERT(!has_then_stm && !has_else_stm);
1743 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001744 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001745 if (frame_ != NULL) {
1746 if (has_cc()) {
1747 cc_reg_ = al;
1748 } else {
1749 frame_->Drop();
1750 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 }
1752 }
1753
1754 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001755 if (exit.is_linked()) {
1756 exit.Bind();
1757 }
1758 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759}
1760
1761
ager@chromium.org7c537e22008-10-16 08:43:32 +00001762void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001763 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001765 CodeForStatementPosition(node);
1766 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767}
1768
1769
ager@chromium.org7c537e22008-10-16 08:43:32 +00001770void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001771 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 CodeForStatementPosition(node);
1774 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775}
1776
1777
ager@chromium.org7c537e22008-10-16 08:43:32 +00001778void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001779 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001781
ager@chromium.org4af710e2009-09-15 12:20:11 +00001782 CodeForStatementPosition(node);
1783 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001784 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 frame_->EmitPop(r0);
1786 function_return_.Jump();
1787 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 // Pop the result from the frame and prepare the frame for
1789 // returning thus making it easier to merge.
1790 frame_->EmitPop(r0);
1791 frame_->PrepareForReturn();
1792
1793 function_return_.Jump();
1794 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795}
1796
1797
ager@chromium.org7c537e22008-10-16 08:43:32 +00001798void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001799#ifdef DEBUG
1800 int original_height = frame_->height();
1801#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001802 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001804 CodeForStatementPosition(node);
1805 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001806 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001808 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001810 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001811#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001812 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001813 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001814 verified_true.Branch(eq);
1815 __ stop("PushContext: r0 is expected to be the same as cp");
1816 verified_true.Bind();
1817#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001819 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001820 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821}
1822
1823
ager@chromium.org7c537e22008-10-16 08:43:32 +00001824void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825#ifdef DEBUG
1826 int original_height = frame_->height();
1827#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001828 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 // Pop context.
1832 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1833 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001834 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836}
1837
1838
ager@chromium.org7c537e22008-10-16 08:43:32 +00001839void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001840#ifdef DEBUG
1841 int original_height = frame_->height();
1842#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001843 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001846 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001848 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001849
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001850 JumpTarget next_test;
1851 JumpTarget fall_through;
1852 JumpTarget default_entry;
1853 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 ZoneList<CaseClause*>* cases = node->cases();
1855 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857
1858 for (int i = 0; i < length; i++) {
1859 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 // Remember the default clause and compile it at the end.
1862 default_clause = clause;
1863 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 }
1865
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001866 Comment cmnt(masm_, "[ Case clause");
1867 // Compile the test.
1868 next_test.Bind();
1869 next_test.Unuse();
1870 // Duplicate TOS.
1871 __ ldr(r0, frame_->Top());
1872 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001873 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 // Before entering the body from the test, remove the switch value from
1877 // the stack.
1878 frame_->Drop();
1879
1880 // Label the body so that fall through is enabled.
1881 if (i > 0 && cases->at(i - 1)->is_default()) {
1882 default_exit.Bind();
1883 } else {
1884 fall_through.Bind();
1885 fall_through.Unuse();
1886 }
1887 VisitStatementsAndSpill(clause->statements());
1888
1889 // If control flow can fall through from the body, jump to the next body
1890 // or the end of the statement.
1891 if (frame_ != NULL) {
1892 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1893 default_entry.Jump();
1894 } else {
1895 fall_through.Jump();
1896 }
1897 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 }
1899
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001900 // The final "test" removes the switch value.
1901 next_test.Bind();
1902 frame_->Drop();
1903
1904 // If there is a default clause, compile it.
1905 if (default_clause != NULL) {
1906 Comment cmnt(masm_, "[ Default clause");
1907 default_entry.Bind();
1908 VisitStatementsAndSpill(default_clause->statements());
1909 // If control flow can fall out of the default and there is a case after
1910 // it, jup to that case's body.
1911 if (frame_ != NULL && default_exit.is_bound()) {
1912 default_exit.Jump();
1913 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001916 if (fall_through.is_linked()) {
1917 fall_through.Bind();
1918 }
1919
1920 if (node->break_target()->is_linked()) {
1921 node->break_target()->Bind();
1922 }
1923 node->break_target()->Unuse();
1924 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925}
1926
1927
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001928void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929#ifdef DEBUG
1930 int original_height = frame_->height();
1931#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001932 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001933 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001934 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001935 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001936 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001937 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001939 // Label the top of the loop for the backward CFG edge. If the test
1940 // is always true we can use the continue target, and if the test is
1941 // always false there is no need.
1942 ConditionAnalysis info = AnalyzeCondition(node->cond());
1943 switch (info) {
1944 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001945 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001946 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001947 break;
1948 case ALWAYS_FALSE:
1949 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1950 break;
1951 case DONT_KNOW:
1952 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1953 body.Bind();
1954 break;
1955 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001957 CheckStack(); // TODO(1222600): ignore if body contains calls.
1958 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001960 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001961 switch (info) {
1962 case ALWAYS_TRUE:
1963 // If control can fall off the end of the body, jump back to the
1964 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001965 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001966 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001969 case ALWAYS_FALSE:
1970 // If we have a continue in the body, we only have to bind its
1971 // jump target.
1972 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001973 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001974 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001975 break;
1976 case DONT_KNOW:
1977 // We have to compile the test expression if it can be reached by
1978 // control flow falling out of the body or via continue.
1979 if (node->continue_target()->is_linked()) {
1980 node->continue_target()->Bind();
1981 }
1982 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001983 Comment cmnt(masm_, "[ DoWhileCondition");
1984 CodeForDoWhileConditionPosition(node);
1985 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001987 // A invalid frame here indicates that control did not
1988 // fall out of the test expression.
1989 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 }
1991 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 break;
1993 }
1994
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 if (node->break_target()->is_linked()) {
1996 node->break_target()->Bind();
1997 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001998 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001999 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2000}
2001
2002
2003void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2004#ifdef DEBUG
2005 int original_height = frame_->height();
2006#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002007 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002008 Comment cmnt(masm_, "[ WhileStatement");
2009 CodeForStatementPosition(node);
2010
2011 // If the test is never true and has no side effects there is no need
2012 // to compile the test or body.
2013 ConditionAnalysis info = AnalyzeCondition(node->cond());
2014 if (info == ALWAYS_FALSE) return;
2015
2016 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002017 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002018
2019 // Label the top of the loop with the continue target for the backward
2020 // CFG edge.
2021 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2022 node->continue_target()->Bind();
2023
2024 if (info == DONT_KNOW) {
2025 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002026 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002027 if (has_valid_frame()) {
2028 // A NULL frame indicates that control did not fall out of the
2029 // test expression.
2030 Branch(false, node->break_target());
2031 }
2032 if (has_valid_frame() || body.is_linked()) {
2033 body.Bind();
2034 }
2035 }
2036
2037 if (has_valid_frame()) {
2038 CheckStack(); // TODO(1222600): ignore if body contains calls.
2039 VisitAndSpill(node->body());
2040
2041 // If control flow can fall out of the body, jump back to the top.
2042 if (has_valid_frame()) {
2043 node->continue_target()->Jump();
2044 }
2045 }
2046 if (node->break_target()->is_linked()) {
2047 node->break_target()->Bind();
2048 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002049 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002050 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2051}
2052
2053
2054void CodeGenerator::VisitForStatement(ForStatement* node) {
2055#ifdef DEBUG
2056 int original_height = frame_->height();
2057#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002058 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002059 Comment cmnt(masm_, "[ ForStatement");
2060 CodeForStatementPosition(node);
2061 if (node->init() != NULL) {
2062 VisitAndSpill(node->init());
2063 }
2064
2065 // If the test is never true there is no need to compile the test or
2066 // body.
2067 ConditionAnalysis info = AnalyzeCondition(node->cond());
2068 if (info == ALWAYS_FALSE) return;
2069
2070 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002071 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002072
2073 // If there is no update statement, label the top of the loop with the
2074 // continue target, otherwise with the loop target.
2075 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2076 if (node->next() == NULL) {
2077 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2078 node->continue_target()->Bind();
2079 } else {
2080 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2081 loop.Bind();
2082 }
2083
2084 // If the test is always true, there is no need to compile it.
2085 if (info == DONT_KNOW) {
2086 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002087 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002088 if (has_valid_frame()) {
2089 Branch(false, node->break_target());
2090 }
2091 if (has_valid_frame() || body.is_linked()) {
2092 body.Bind();
2093 }
2094 }
2095
2096 if (has_valid_frame()) {
2097 CheckStack(); // TODO(1222600): ignore if body contains calls.
2098 VisitAndSpill(node->body());
2099
2100 if (node->next() == NULL) {
2101 // If there is no update statement and control flow can fall out
2102 // of the loop, jump directly to the continue label.
2103 if (has_valid_frame()) {
2104 node->continue_target()->Jump();
2105 }
2106 } else {
2107 // If there is an update statement and control flow can reach it
2108 // via falling out of the body of the loop or continuing, we
2109 // compile the update statement.
2110 if (node->continue_target()->is_linked()) {
2111 node->continue_target()->Bind();
2112 }
2113 if (has_valid_frame()) {
2114 // Record source position of the statement as this code which is
2115 // after the code for the body actually belongs to the loop
2116 // statement and not the body.
2117 CodeForStatementPosition(node);
2118 VisitAndSpill(node->next());
2119 loop.Jump();
2120 }
2121 }
2122 }
2123 if (node->break_target()->is_linked()) {
2124 node->break_target()->Bind();
2125 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002126 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128}
2129
2130
ager@chromium.org7c537e22008-10-16 08:43:32 +00002131void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132#ifdef DEBUG
2133 int original_height = frame_->height();
2134#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002135 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002139 JumpTarget primitive;
2140 JumpTarget jsobject;
2141 JumpTarget fixed_array;
2142 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2143 JumpTarget end_del_check;
2144 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145
2146 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002147 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148
2149 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2150 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002152 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2153 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002154 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002155 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2156 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158
2159 // Stack layout in body:
2160 // [iteration counter (Smi)]
2161 // [length of array]
2162 // [FixedArray]
2163 // [Map or 0]
2164 // [Object]
2165
2166 // Check if enumerable is already a JSObject
2167 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002169 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 primitive.Bind();
2173 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002174 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002178 // r0: value to be iterated over
2179 frame_->EmitPush(r0); // Push the object being iterated over.
2180
2181 // Check cache validity in generated code. This is a fast case for
2182 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2183 // guarantee cache validity, call the runtime system to check cache
2184 // validity or get the property names in a fixed array.
2185 JumpTarget call_runtime;
2186 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2187 JumpTarget check_prototype;
2188 JumpTarget use_cache;
2189 __ mov(r1, Operand(r0));
2190 loop.Bind();
2191 // Check that there are no elements.
2192 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2193 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2194 __ cmp(r2, r4);
2195 call_runtime.Branch(ne);
2196 // Check that instance descriptors are not empty so that we can
2197 // check for an enum cache. Leave the map in r3 for the subsequent
2198 // prototype load.
2199 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2200 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2201 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2202 __ cmp(r2, ip);
2203 call_runtime.Branch(eq);
2204 // Check that there in an enum cache in the non-empty instance
2205 // descriptors. This is the case if the next enumeration index
2206 // field does not contain a smi.
2207 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2208 __ tst(r2, Operand(kSmiTagMask));
2209 call_runtime.Branch(eq);
2210 // For all objects but the receiver, check that the cache is empty.
2211 // r4: empty fixed array root.
2212 __ cmp(r1, r0);
2213 check_prototype.Branch(eq);
2214 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2215 __ cmp(r2, r4);
2216 call_runtime.Branch(ne);
2217 check_prototype.Bind();
2218 // Load the prototype from the map and loop if non-null.
2219 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2220 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2221 __ cmp(r1, ip);
2222 loop.Branch(ne);
2223 // The enum cache is valid. Load the map of the object being
2224 // iterated over and use the cache for the iteration.
2225 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2226 use_cache.Jump();
2227
2228 call_runtime.Bind();
2229 // Call the runtime to get the property names for the object.
2230 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002231 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002233 // If we got a map from the runtime call, we can do a fast
2234 // modification check. Otherwise, we got a fixed array, and we have
2235 // to do a slow check.
2236 // r0: map or fixed array (result from call to
2237 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 __ mov(r2, Operand(r0));
2239 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002240 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2241 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002244 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002246 // r0: map (either the result from a call to
2247 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2248 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 __ mov(r1, Operand(r0));
2250 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2251 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2252 __ ldr(r2,
2253 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2254
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002255 frame_->EmitPush(r0); // map
2256 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002257 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002260 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 frame_->EmitPush(r0);
2262 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002264 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266 frame_->EmitPush(r1); // insert 0 in place of Map
2267 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268
2269 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002270 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002273 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002274 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275
2276 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002278 // sp[0] : index
2279 // sp[1] : array/enum cache length
2280 // sp[2] : array or enum cache
2281 // sp[3] : 0 or map
2282 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002283 // Grab the current frame's height for the break and continue
2284 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002285 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2286 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002288 __ ldr(r0, frame_->ElementAt(0)); // load the current count
2289 __ ldr(r1, frame_->ElementAt(1)); // load the length
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002290 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002291 node->break_target()->Branch(hs);
2292
2293 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002296 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2298 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2299
2300 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 // Check if this (still) matches the map of the enumerable.
2303 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2306 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002307 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308
2309 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2311 frame_->EmitPush(r0);
2312 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002313 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002314 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315
2316 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002317 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2318 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002319 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 end_del_check.Bind();
2322 // Store the entry in the 'each' expression and take another spin in the
2323 // loop. r3: i'th entry of the enum cache (or string there of)
2324 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 { Reference each(this, node->each());
2326 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002327 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 __ ldr(r0, frame_->ElementAt(each.size()));
2329 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002330 each.SetValue(NOT_CONST_INIT);
2331 frame_->Drop(2);
2332 } else {
2333 // If the reference was to a slot we rely on the convenient property
2334 // that it doesn't matter whether a value (eg, r3 pushed above) is
2335 // right on top of or right underneath a zero-sized reference.
2336 each.SetValue(NOT_CONST_INIT);
2337 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002338 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 }
2340 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 VisitAndSpill(node->body());
2344
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002345 // Next. Reestablish a spilled frame in case we are coming here via
2346 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002348 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 frame_->EmitPop(r0);
2350 __ add(r0, r0, Operand(Smi::FromInt(1)));
2351 frame_->EmitPush(r0);
2352 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002354 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2355 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002356 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002357 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
2359 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002360 exit.Bind();
2361 node->continue_target()->Unuse();
2362 node->break_target()->Unuse();
2363 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002367void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368#ifdef DEBUG
2369 int original_height = frame_->height();
2370#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002371 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002372 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002375 JumpTarget try_block;
2376 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002378 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002380 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381
2382 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002383 Variable* catch_var = node->catch_var()->var();
2384 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2385 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386
2387 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002388 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 VisitStatementsAndSpill(node->catch_block()->statements());
2391 if (frame_ != NULL) {
2392 exit.Jump();
2393 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394
2395
2396 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002397 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002399 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2400 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002402 // Shadow the labels for all escapes from the try block, including
2403 // returns. During shadowing, the original label is hidden as the
2404 // LabelShadow and operations on the original actually affect the
2405 // shadowing label.
2406 //
2407 // We should probably try to unify the escaping labels and the return
2408 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002409 int nof_escapes = node->escaping_targets()->length();
2410 List<ShadowTarget*> shadows(1 + nof_escapes);
2411
2412 // Add the shadow target for the function return.
2413 static const int kReturnShadowIndex = 0;
2414 shadows.Add(new ShadowTarget(&function_return_));
2415 bool function_return_was_shadowed = function_return_is_shadowed_;
2416 function_return_is_shadowed_ = true;
2417 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2418
2419 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002421 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 }
2423
2424 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426
2427 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002428 // After shadowing stops, the original labels are unshadowed and the
2429 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002430 bool has_unlinks = false;
2431 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002433 has_unlinks = has_unlinks || shadows[i]->is_linked();
2434 }
2435 function_return_is_shadowed_ = function_return_was_shadowed;
2436
2437 // Get an external reference to the handler address.
2438 ExternalReference handler_address(Top::k_handler_address);
2439
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 // If we can fall off the end of the try block, unlink from try chain.
2441 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002442 // The next handler address is on top of the frame. Unlink from
2443 // the handler list and drop the rest of this handler from the
2444 // frame.
2445 ASSERT(StackHandlerConstants::kNextOffset == 0);
2446 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002447 __ mov(r3, Operand(handler_address));
2448 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002449 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 if (has_unlinks) {
2451 exit.Jump();
2452 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453 }
2454
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002455 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 // jumped to. Deallocate each shadow target.
2457 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002459 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 shadows[i]->Bind();
2461 // Because we can be jumping here (to spilled code) from unspilled
2462 // code, we need to reestablish a spilled frame at this block.
2463 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465 // Reload sp from the top handler, because some statements that we
2466 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002469 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002471 ASSERT(StackHandlerConstants::kNextOffset == 0);
2472 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002474 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2477 frame_->PrepareForReturn();
2478 }
2479 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 }
2481 }
2482
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002483 exit.Bind();
2484 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485}
2486
2487
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002488void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002489#ifdef DEBUG
2490 int original_height = frame_->height();
2491#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002492 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002493 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002494 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495
2496 // State: Used to keep track of reason for entering the finally
2497 // block. Should probably be extended to hold information for
2498 // break/continue from within the try block.
2499 enum { FALLING, THROWING, JUMPING };
2500
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002501 JumpTarget try_block;
2502 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002506 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 // In case of thrown exceptions, this is where we continue.
2508 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510
2511 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002512 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002514 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2515 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002517 // Shadow the labels for all escapes from the try block, including
2518 // returns. Shadowing hides the original label as the LabelShadow and
2519 // operations on the original actually affect the shadowing label.
2520 //
2521 // We should probably try to unify the escaping labels and the return
2522 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002523 int nof_escapes = node->escaping_targets()->length();
2524 List<ShadowTarget*> shadows(1 + nof_escapes);
2525
2526 // Add the shadow target for the function return.
2527 static const int kReturnShadowIndex = 0;
2528 shadows.Add(new ShadowTarget(&function_return_));
2529 bool function_return_was_shadowed = function_return_is_shadowed_;
2530 function_return_is_shadowed_ = true;
2531 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2532
2533 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002535 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 }
2537
2538 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002541 // Stop the introduced shadowing and count the number of required unlinks.
2542 // After shadowing stops, the original labels are unshadowed and the
2543 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002545 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 shadows[i]->StopShadowing();
2547 if (shadows[i]->is_linked()) nof_unlinks++;
2548 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551 // Get an external reference to the handler address.
2552 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 // If we can fall off the end of the try block, unlink from the try
2555 // chain and set the state on the frame to FALLING.
2556 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002557 // The next handler address is on top of the frame.
2558 ASSERT(StackHandlerConstants::kNextOffset == 0);
2559 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 __ mov(r3, Operand(handler_address));
2561 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002562 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563
2564 // Fake a top of stack value (unneeded when FALLING) and set the
2565 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002566 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 frame_->EmitPush(r0);
2568 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2569 if (nof_unlinks > 0) {
2570 finally_block.Jump();
2571 }
2572 }
2573
2574 // Generate code to unlink and set the state for the (formerly)
2575 // shadowing targets that have been jumped to.
2576 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 // If we have come from the shadowed return, the return value is
2579 // in (a non-refcounted reference to) r0. We must preserve it
2580 // until it is pushed.
2581 //
2582 // Because we can be jumping here (to spilled code) from
2583 // unspilled code, we need to reestablish a spilled frame at
2584 // this block.
2585 shadows[i]->Bind();
2586 frame_->SpillAll();
2587
2588 // Reload sp from the top handler, because some statements that
2589 // we break from (eg, for...in) may have left stuff on the
2590 // stack.
2591 __ mov(r3, Operand(handler_address));
2592 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002593 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002594
2595 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002596 // handler address is currently on top of the frame.
2597 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002598 frame_->EmitPop(r1);
2599 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002600 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601
2602 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002603 // If this label shadowed the function return, materialize the
2604 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002605 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002606 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002607 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002608 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002609 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 }
2611 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002612 if (--nof_unlinks > 0) {
2613 // If this is not the last unlink block, jump around the next.
2614 finally_block.Jump();
2615 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 }
2617 }
2618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002622 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002624
2625 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626 // and the state - while evaluating the finally block.
2627 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002631 if (has_valid_frame()) {
2632 // Restore state and return value or faked TOS.
2633 frame_->EmitPop(r2);
2634 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 }
2636
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002637 // Generate code to jump to the right destination for all used
2638 // formerly shadowing targets. Deallocate each shadow target.
2639 for (int i = 0; i < shadows.length(); i++) {
2640 if (has_valid_frame() && shadows[i]->is_bound()) {
2641 JumpTarget* original = shadows[i]->other_target();
2642 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2643 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002644 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 skip.Branch(ne);
2646 frame_->PrepareForReturn();
2647 original->Jump();
2648 skip.Bind();
2649 } else {
2650 original->Branch(eq);
2651 }
2652 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655 if (has_valid_frame()) {
2656 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002657 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002658 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2659 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 // Rethrow exception.
2662 frame_->EmitPush(r0);
2663 frame_->CallRuntime(Runtime::kReThrow, 1);
2664
2665 // Done.
2666 exit.Bind();
2667 }
2668 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669}
2670
2671
ager@chromium.org7c537e22008-10-16 08:43:32 +00002672void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002673#ifdef DEBUG
2674 int original_height = frame_->height();
2675#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002676 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002679#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002680 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002681#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002682 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684}
2685
2686
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002687void CodeGenerator::InstantiateFunction(
2688 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002689 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002690 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002691 // Use the fast case closure allocation code that allocates in new
2692 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002693 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002694 FastNewClosureStub stub;
2695 frame_->EmitPush(r0);
2696 frame_->CallStub(&stub, 1);
2697 frame_->EmitPush(r0);
2698 } else {
2699 // Create a new closure.
2700 frame_->EmitPush(cp);
2701 frame_->EmitPush(r0);
2702 frame_->CallRuntime(Runtime::kNewClosure, 2);
2703 frame_->EmitPush(r0);
2704 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705}
2706
2707
ager@chromium.org7c537e22008-10-16 08:43:32 +00002708void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002709#ifdef DEBUG
2710 int original_height = frame_->height();
2711#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002712 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 Comment cmnt(masm_, "[ FunctionLiteral");
2714
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002715 // Build the function info and instantiate it.
2716 Handle<SharedFunctionInfo> function_info =
2717 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002718 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002719 if (HasStackOverflow()) {
2720 ASSERT(frame_->height() == original_height);
2721 return;
2722 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002723 InstantiateFunction(function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725}
2726
2727
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002728void CodeGenerator::VisitSharedFunctionInfoLiteral(
2729 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730#ifdef DEBUG
2731 int original_height = frame_->height();
2732#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002733 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002734 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2735 InstantiateFunction(node->shared_function_info());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002736 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737}
2738
2739
ager@chromium.org7c537e22008-10-16 08:43:32 +00002740void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002741#ifdef DEBUG
2742 int original_height = frame_->height();
2743#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002744 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002745 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002746 JumpTarget then;
2747 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002748 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002749 if (has_valid_frame()) {
2750 Branch(false, &else_);
2751 }
2752 if (has_valid_frame() || then.is_linked()) {
2753 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002754 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002755 }
2756 if (else_.is_linked()) {
2757 JumpTarget exit;
2758 if (has_valid_frame()) exit.Jump();
2759 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002760 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002761 if (exit.is_linked()) exit.Bind();
2762 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002763 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764}
2765
2766
ager@chromium.org7c537e22008-10-16 08:43:32 +00002767void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2768 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002769 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002771 // JumpTargets do not yet support merging frames so the frame must be
2772 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002773 JumpTarget slow;
2774 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002775
2776 // Generate fast-case code for variables that might be shadowed by
2777 // eval-introduced variables. Eval is used a lot without
2778 // introducing variables. In those cases, we do not want to
2779 // perform a runtime call for all variables in the scope
2780 // containing the eval.
2781 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002782 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783 // If there was no control flow to slow, we can exit early.
2784 if (!slow.is_linked()) {
2785 frame_->EmitPush(r0);
2786 return;
2787 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002788 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002789
2790 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002791
2792 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002793 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002794 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2795 // Only generate the fast case for locals that rewrite to slots.
2796 // This rules out argument loads.
2797 if (potential_slot != NULL) {
2798 __ ldr(r0,
2799 ContextSlotOperandCheckExtensions(potential_slot,
2800 r1,
2801 r2,
2802 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002803 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002804 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2805 __ cmp(r0, ip);
2806 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002807 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002808 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002809 // ContextSlotOperandCheckExtensions so we have to jump around
2810 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002811 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002812 }
2813 }
2814
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002815 slow.Bind();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002816 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002817 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002818 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002819 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820
ager@chromium.org7c537e22008-10-16 08:43:32 +00002821 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002822 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002823 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002826
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002827 done.Bind();
2828 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829
2830 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002831 Register scratch = VirtualFrame::scratch0();
2832 frame_->EmitPush(SlotOperand(slot, scratch));
ager@chromium.org7c537e22008-10-16 08:43:32 +00002833 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002834 // Const slots may contain 'the hole' value (the constant hasn't been
2835 // initialized yet) which needs to be converted into the 'undefined'
2836 // value.
2837 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002838 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002839 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002840 __ cmp(scratch, ip);
2841 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
2842 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 }
2844 }
2845}
2846
2847
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002848void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
2849 TypeofState state) {
2850 LoadFromSlot(slot, state);
2851
2852 // Bail out quickly if we're not using lazy arguments allocation.
2853 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
2854
2855 // ... or if the slot isn't a non-parameter arguments slot.
2856 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
2857
2858 VirtualFrame::SpilledScope spilled_scope(frame_);
2859
2860 // Load the loaded value from the stack into r0 but leave it on the
2861 // stack.
2862 __ ldr(r0, MemOperand(sp, 0));
2863
2864 // If the loaded value is the sentinel that indicates that we
2865 // haven't loaded the arguments object yet, we need to do it now.
2866 JumpTarget exit;
2867 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2868 __ cmp(r0, ip);
2869 exit.Branch(ne);
2870 frame_->Drop();
2871 StoreArgumentsObject(false);
2872 exit.Bind();
2873}
2874
2875
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002876void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2877 ASSERT(slot != NULL);
2878 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002879 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002880 ASSERT(slot->var()->is_dynamic());
2881
2882 // For now, just do a runtime call.
2883 frame_->EmitPush(cp);
2884 __ mov(r0, Operand(slot->var()->name()));
2885 frame_->EmitPush(r0);
2886
2887 if (init_state == CONST_INIT) {
2888 // Same as the case for a normal store, but ignores attribute
2889 // (e.g. READ_ONLY) of context slot so that we can initialize
2890 // const properties (introduced via eval("const foo = (some
2891 // expr);")). Also, uses the current function context instead of
2892 // the top context.
2893 //
2894 // Note that we must declare the foo upon entry of eval(), via a
2895 // context slot declaration, but we cannot initialize it at the
2896 // same time, because the const declaration may be at the end of
2897 // the eval code (sigh...) and the const variable may have been
2898 // used before (where its value is 'undefined'). Thus, we can only
2899 // do the initialization when we actually encounter the expression
2900 // and when the expression operands are defined and valid, and
2901 // thus we need the split into 2 operations: declaration of the
2902 // context slot followed by initialization.
2903 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2904 } else {
2905 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2906 }
2907 // Storing a variable must keep the (new) value on the expression
2908 // stack. This is necessary for compiling assignment expressions.
2909 frame_->EmitPush(r0);
2910
2911 } else {
2912 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00002913 Register scratch = VirtualFrame::scratch0();
2914 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002915
ager@chromium.org357bf652010-04-12 11:30:10 +00002916 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002917 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00002918
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002919 if (init_state == CONST_INIT) {
2920 ASSERT(slot->var()->mode() == Variable::CONST);
2921 // Only the first const initialization must be executed (the slot
2922 // still contains 'the hole' value). When the assignment is
2923 // executed, the code is identical to a normal store (see below).
2924 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002925 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002926 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002927 __ cmp(scratch, ip);
2928 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002929 exit.Branch(ne);
2930 }
2931
2932 // We must execute the store. Storing a variable must keep the
2933 // (new) value on the stack. This is necessary for compiling
2934 // assignment expressions.
2935 //
2936 // Note: We will reach here even with slot->var()->mode() ==
2937 // Variable::CONST because of const declarations which will
2938 // initialize consts to 'the hole' value and by doing so, end up
2939 // calling this code. r2 may be loaded with context; used below in
2940 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00002941 Register tos = frame_->Peek();
2942 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002943 if (slot->type() == Slot::CONTEXT) {
2944 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00002945 __ tst(tos, Operand(kSmiTagMask));
2946 // We don't use tos any more after here.
2947 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002948 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00002949 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002950 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2951 __ mov(r3, Operand(offset));
ager@chromium.org357bf652010-04-12 11:30:10 +00002952 // r1 could be identical with tos, but that doesn't matter.
2953 __ RecordWrite(scratch, r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002954 }
2955 // If we definitely did not jump over the assignment, we do not need
2956 // to bind the exit label. Doing so can defeat peephole
2957 // optimization.
2958 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002959 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002960 exit.Bind();
2961 }
2962 }
2963}
2964
2965
ager@chromium.org381abbb2009-02-25 13:23:22 +00002966void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2967 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002968 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002969 // Check that no extension objects have been created by calls to
2970 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002971 Register tmp = frame_->scratch0();
2972 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002973 Register context = cp;
2974 Scope* s = scope();
2975 while (s != NULL) {
2976 if (s->num_heap_slots() > 0) {
2977 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002978 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002979 // Check that extension is NULL.
2980 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2981 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002982 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002983 }
2984 // Load next context in chain.
2985 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2986 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2987 context = tmp;
2988 }
2989 // If no outer scope calls eval, we do not need to check more
2990 // context extensions.
2991 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2992 s = s->outer_scope();
2993 }
2994
2995 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002996 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002997 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00002998 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002999 __ bind(&next);
3000 // Terminate at global context.
3001 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003002 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3003 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003004 __ b(eq, &fast);
3005 // Check that extension is NULL.
3006 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3007 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003008 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003009 // Load next context in chain.
3010 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3011 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3012 __ b(&next);
3013 __ bind(&fast);
3014 }
3015
ager@chromium.org381abbb2009-02-25 13:23:22 +00003016 // Load the global object.
3017 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003018 // Setup the name register and call load IC.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003019 frame_->SpillAllButCopyTOSToR0();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003020 __ mov(r2, Operand(slot->var()->name()));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003021 frame_->CallLoadIC(typeof_state == INSIDE_TYPEOF
3022 ? RelocInfo::CODE_TARGET
3023 : RelocInfo::CODE_TARGET_CONTEXT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003024 // Drop the global object. The result is in r0.
3025 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003026}
3027
3028
ager@chromium.org7c537e22008-10-16 08:43:32 +00003029void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003030#ifdef DEBUG
3031 int original_height = frame_->height();
3032#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003033 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003034 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003036}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037
ager@chromium.org7c537e22008-10-16 08:43:32 +00003038
3039void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003040#ifdef DEBUG
3041 int original_height = frame_->height();
3042#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003043 Comment cmnt(masm_, "[ VariableProxy");
3044
3045 Variable* var = node->var();
3046 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003047 if (expr != NULL) {
3048 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003050 ASSERT(var->is_global());
3051 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003052 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003054 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003055}
3056
3057
ager@chromium.org7c537e22008-10-16 08:43:32 +00003058void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003059#ifdef DEBUG
3060 int original_height = frame_->height();
3061#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003062 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003063 Register reg = frame_->GetTOSRegister();
3064 __ mov(reg, Operand(node->handle()));
3065 frame_->EmitPush(reg);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003066 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067}
3068
3069
ager@chromium.org7c537e22008-10-16 08:43:32 +00003070void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003071#ifdef DEBUG
3072 int original_height = frame_->height();
3073#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003074 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 Comment cmnt(masm_, "[ RexExp Literal");
3076
3077 // Retrieve the literal array and check the allocated entry.
3078
3079 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003080 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081
3082 // Load the literals array of the function.
3083 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3084
3085 // Load the literal at the ast saved index.
3086 int literal_offset =
3087 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3088 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3089
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003090 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003091 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3092 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003093 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094
3095 // If the entry is undefined we call the runtime system to computed
3096 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003098 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003099 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003100 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003101 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003102 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003103 frame_->EmitPush(r0);
3104 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003105 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003107 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003108 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 frame_->EmitPush(r2);
3110 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111}
3112
3113
ager@chromium.org7c537e22008-10-16 08:43:32 +00003114void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003115#ifdef DEBUG
3116 int original_height = frame_->height();
3117#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003118 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119 Comment cmnt(masm_, "[ ObjectLiteral");
3120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003122 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003123 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003124 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003125 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003126 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003127 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003128 __ mov(r1, Operand(node->constant_properties()));
3129 // Should the object literal have fast elements?
3130 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3131 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003132 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003133 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003134 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003135 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003136 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003139 // At the start of each iteration, the top of stack contains
3140 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141 ObjectLiteral::Property* property = node->properties()->at(i);
3142 Literal* key = property->key();
3143 Expression* value = property->value();
3144 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003145 case ObjectLiteral::Property::CONSTANT:
3146 break;
3147 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3148 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3149 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003150 case ObjectLiteral::Property::COMPUTED:
3151 if (key->handle()->IsSymbol()) {
3152 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3153 LoadAndSpill(value);
3154 frame_->EmitPop(r0);
3155 __ mov(r2, Operand(key->handle()));
3156 __ ldr(r1, frame_->Top()); // Load the receiver.
3157 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3158 break;
3159 }
3160 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003161 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003162 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003163 frame_->EmitPush(r0); // dup the result
3164 LoadAndSpill(key);
3165 LoadAndSpill(value);
3166 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167 break;
3168 }
3169 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003170 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003171 frame_->EmitPush(r0);
3172 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003173 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003174 frame_->EmitPush(r0);
3175 LoadAndSpill(value);
3176 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003177 break;
3178 }
3179 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003180 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003181 frame_->EmitPush(r0);
3182 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003183 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003184 frame_->EmitPush(r0);
3185 LoadAndSpill(value);
3186 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187 break;
3188 }
3189 }
3190 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003191 ASSERT(frame_->height() == original_height + 1);
3192}
3193
3194
ager@chromium.org7c537e22008-10-16 08:43:32 +00003195void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003196#ifdef DEBUG
3197 int original_height = frame_->height();
3198#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003199 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003200 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003201
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003202 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003203 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003204 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003205 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003206 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003207 __ mov(r0, Operand(node->constant_elements()));
3208 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003209 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003210 if (node->depth() > 1) {
3211 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003212 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003213 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003214 } else {
3215 FastCloneShallowArrayStub stub(length);
3216 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003217 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003218 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003219 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003220
3221 // Generate code to set the elements in the array that are not
3222 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223 for (int i = 0; i < node->values()->length(); i++) {
3224 Expression* value = node->values()->at(i);
3225
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003226 // If value is a literal the property value is already set in the
3227 // boilerplate object.
3228 if (value->AsLiteral() != NULL) continue;
3229 // If value is a materialized literal the property value is already set
3230 // in the boilerplate object if it is simple.
3231 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003233 // The property must be set by generated code.
3234 LoadAndSpill(value);
3235 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003237 // Fetch the object literal.
3238 __ ldr(r1, frame_->Top());
3239 // Get the elements array.
3240 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003242 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003243 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003244 __ str(r0, FieldMemOperand(r1, offset));
3245
3246 // Update the write barrier for the array address.
3247 __ mov(r3, Operand(offset));
3248 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003250 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251}
3252
3253
ager@chromium.org32912102009-01-16 10:38:43 +00003254void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255#ifdef DEBUG
3256 int original_height = frame_->height();
3257#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003258 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org32912102009-01-16 10:38:43 +00003259 // Call runtime routine to allocate the catch extension object and
3260 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261 Comment cmnt(masm_, "[ CatchExtensionObject");
3262 LoadAndSpill(node->key());
3263 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003264 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3265 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003266 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00003267}
3268
3269
ager@chromium.org7c537e22008-10-16 08:43:32 +00003270void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003271 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003272#ifdef DEBUG
3273 int original_height = frame_->height();
3274#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003275 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003276
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003277 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003278 if (target.is_illegal()) {
3279 // Fool the virtual frame into thinking that we left the assignment's
3280 // value on the frame.
ager@chromium.org357bf652010-04-12 11:30:10 +00003281 Register tos = frame_->GetTOSRegister();
3282 __ mov(tos, Operand(Smi::FromInt(0)));
3283 frame_->EmitPush(tos);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284 ASSERT(frame_->height() == original_height + 1);
3285 return;
3286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003288 if (node->op() == Token::ASSIGN ||
3289 node->op() == Token::INIT_VAR ||
3290 node->op() == Token::INIT_CONST) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003291 Load(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00003292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003293 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003294 // Get the old value of the lhs.
ager@chromium.org357bf652010-04-12 11:30:10 +00003295 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003296 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003297 bool overwrite =
3298 (node->value()->AsBinaryOperation() != NULL &&
3299 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003300 if (literal != NULL && literal->handle()->IsSmi()) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00003301 SmiOperation(node->binary_op(),
3302 literal->handle(),
3303 false,
3304 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003305 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003306 Load(node->value());
3307 VirtualFrameBinaryOperation(node->binary_op(),
3308 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003310 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003311 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3312 if (var != NULL &&
3313 (var->mode() == Variable::CONST) &&
3314 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3315 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003316 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003317 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003318 CodeForSourcePosition(node->position());
3319 if (node->op() == Token::INIT_CONST) {
3320 // Dynamic constant initializations must use the function context
3321 // and initialize the actual constant declared. Dynamic variable
3322 // initializations are simply assignments and use SetValue.
3323 target.SetValue(CONST_INIT);
3324 } else {
3325 target.SetValue(NOT_CONST_INIT);
3326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 }
3328 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003329 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003330}
3331
3332
ager@chromium.org7c537e22008-10-16 08:43:32 +00003333void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003334#ifdef DEBUG
3335 int original_height = frame_->height();
3336#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003337 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003338 Comment cmnt(masm_, "[ Throw");
3339
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003340 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003341 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003342 frame_->CallRuntime(Runtime::kThrow, 1);
3343 frame_->EmitPush(r0);
3344 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345}
3346
3347
ager@chromium.org7c537e22008-10-16 08:43:32 +00003348void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003349#ifdef DEBUG
3350 int original_height = frame_->height();
3351#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003353
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003354 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003355 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003356 }
3357 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358}
3359
3360
ager@chromium.org7c537e22008-10-16 08:43:32 +00003361void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003362#ifdef DEBUG
3363 int original_height = frame_->height();
3364#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003365 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 Comment cmnt(masm_, "[ Call");
3367
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003368 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003369 ZoneList<Expression*>* args = node->arguments();
3370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003371 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003373 Variable* var = function->AsVariableProxy()->AsVariable();
3374 Property* property = function->AsProperty();
3375
3376 // ------------------------------------------------------------------------
3377 // Fast-case: Use inline caching.
3378 // ---
3379 // According to ECMA-262, section 11.2.3, page 44, the function to call
3380 // must be resolved after the arguments have been evaluated. The IC code
3381 // automatically handles this by loading the arguments before the function
3382 // is resolved in cache misses (this also holds for megamorphic calls).
3383 // ------------------------------------------------------------------------
3384
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003385 if (var != NULL && var->is_possibly_eval()) {
3386 // ----------------------------------
3387 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3388 // ----------------------------------
3389
3390 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3391 // resolve the function we need to call and the receiver of the
3392 // call. Then we call the resolved function using the given
3393 // arguments.
3394 // Prepare stack for call to resolved function.
3395 LoadAndSpill(function);
3396 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3397 frame_->EmitPush(r2); // Slot for receiver
3398 int arg_count = args->length();
3399 for (int i = 0; i < arg_count; i++) {
3400 LoadAndSpill(args->at(i));
3401 }
3402
3403 // Prepare stack for call to ResolvePossiblyDirectEval.
3404 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3405 frame_->EmitPush(r1);
3406 if (arg_count > 0) {
3407 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3408 frame_->EmitPush(r1);
3409 } else {
3410 frame_->EmitPush(r2);
3411 }
3412
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003413 // Push the receiver.
3414 __ ldr(r1, frame_->Receiver());
3415 frame_->EmitPush(r1);
3416
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003417 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003418 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003419
3420 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003421 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003422 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3423
3424 // Call the function.
3425 CodeForSourcePosition(node->position());
3426
3427 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003428 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003429 frame_->CallStub(&call_function, arg_count + 1);
3430
3431 __ ldr(cp, frame_->Context());
3432 // Remove the function from the stack.
3433 frame_->Drop();
3434 frame_->EmitPush(r0);
3435
3436 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 // ----------------------------------
3438 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3439 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003440 // Pass the global object as the receiver and let the IC stub
3441 // patch the stack to use the global proxy as 'this' in the
3442 // invoked function.
3443 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003444
3445 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003446 int arg_count = args->length();
3447 for (int i = 0; i < arg_count; i++) {
3448 LoadAndSpill(args->at(i));
3449 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450
ager@chromium.org5c838252010-02-19 08:53:10 +00003451 // Setup the name register and call the IC initialization code.
3452 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003453 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3454 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003455 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003456 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3457 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003458 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003459 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460
3461 } else if (var != NULL && var->slot() != NULL &&
3462 var->slot()->type() == Slot::LOOKUP) {
3463 // ----------------------------------
3464 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3465 // ----------------------------------
3466
3467 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003468 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003469 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003470 frame_->EmitPush(r0);
3471 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003472 // r0: slot value; r1: receiver
3473
3474 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003475 frame_->EmitPush(r0); // function
3476 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477
3478 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003479 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003480 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481
3482 } else if (property != NULL) {
3483 // Check if the key is a literal string.
3484 Literal* literal = property->key()->AsLiteral();
3485
3486 if (literal != NULL && literal->handle()->IsSymbol()) {
3487 // ------------------------------------------------------------------
3488 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3489 // ------------------------------------------------------------------
3490
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003491 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003493 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
3494 name->IsEqualTo(CStrVector("apply")) &&
3495 args->length() == 2 &&
3496 args->at(1)->AsVariableProxy() != NULL &&
3497 args->at(1)->AsVariableProxy()->IsArguments()) {
3498 // Use the optimized Function.prototype.apply that avoids
3499 // allocating lazily allocated arguments objects.
3500 CallApplyLazy(property->obj(),
3501 args->at(0),
3502 args->at(1)->AsVariableProxy(),
3503 node->position());
3504
3505 } else {
3506 LoadAndSpill(property->obj()); // Receiver.
3507 // Load the arguments.
3508 int arg_count = args->length();
3509 for (int i = 0; i < arg_count; i++) {
3510 LoadAndSpill(args->at(i));
3511 }
3512
3513 // Set the name register and call the IC initialization code.
3514 __ mov(r2, Operand(name));
3515 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3516 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
3517 CodeForSourcePosition(node->position());
3518 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
3519 __ ldr(cp, frame_->Context());
3520 frame_->EmitPush(r0);
3521 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522
3523 } else {
3524 // -------------------------------------------
3525 // JavaScript example: 'array[index](1, 2, 3)'
3526 // -------------------------------------------
3527
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003528 LoadAndSpill(property->obj());
3529 LoadAndSpill(property->key());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003530 EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003531 frame_->Drop(); // key
3532 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003533 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003534 // Use the global receiver.
3535 frame_->Drop();
3536 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 LoadGlobalReceiver(r0);
3538 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003539 frame_->EmitPop(r1); // receiver
3540 frame_->EmitPush(r0); // function
3541 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003542 }
3543
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003544 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003545 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003546 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547 }
3548
3549 } else {
3550 // ----------------------------------
3551 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3552 // ----------------------------------
3553
3554 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003555 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003556
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003557 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003558 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003561 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003562 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565}
3566
3567
ager@chromium.org7c537e22008-10-16 08:43:32 +00003568void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003569#ifdef DEBUG
3570 int original_height = frame_->height();
3571#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003572 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 Comment cmnt(masm_, "[ CallNew");
3574
3575 // According to ECMA-262, section 11.2.2, page 44, the function
3576 // expression in new calls must be evaluated before the
3577 // arguments. This is different from ordinary calls, where the
3578 // actual function to call is resolved after the arguments have been
3579 // evaluated.
3580
3581 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003582 // receiver. There is no need to use the global proxy here because
3583 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003585 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586
3587 // Push the arguments ("left-to-right") on the stack.
3588 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003589 int arg_count = args->length();
3590 for (int i = 0; i < arg_count; i++) {
3591 LoadAndSpill(args->at(i));
3592 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593
mads.s.ager31e71382008-08-13 09:32:07 +00003594 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003595 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003596 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003597 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599 // Call the construct call builtin that handles allocation and
3600 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003601 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003602 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003603 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003604
3605 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003606 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003607 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608}
3609
3610
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003611void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003612 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003613 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003614 JumpTarget leave, null, function, non_function_constructor;
3615
3616 // Load the object into r0.
3617 LoadAndSpill(args->at(0));
3618 frame_->EmitPop(r0);
3619
3620 // If the object is a smi, we return null.
3621 __ tst(r0, Operand(kSmiTagMask));
3622 null.Branch(eq);
3623
3624 // Check that the object is a JS object but take special care of JS
3625 // functions to make sure they have 'Function' as their class.
3626 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3627 null.Branch(lt);
3628
3629 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3630 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3631 // LAST_JS_OBJECT_TYPE.
3632 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3633 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3634 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3635 function.Branch(eq);
3636
3637 // Check if the constructor in the map is a function.
3638 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3639 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3640 non_function_constructor.Branch(ne);
3641
3642 // The r0 register now contains the constructor function. Grab the
3643 // instance class name from there.
3644 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3645 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003646 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003647 leave.Jump();
3648
3649 // Functions have class 'Function'.
3650 function.Bind();
3651 __ mov(r0, Operand(Factory::function_class_symbol()));
3652 frame_->EmitPush(r0);
3653 leave.Jump();
3654
3655 // Objects with a non-function constructor have class 'Object'.
3656 non_function_constructor.Bind();
3657 __ mov(r0, Operand(Factory::Object_symbol()));
3658 frame_->EmitPush(r0);
3659 leave.Jump();
3660
3661 // Non-JS objects have class null.
3662 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003663 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003664 frame_->EmitPush(r0);
3665
3666 // All done.
3667 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003668}
3669
3670
ager@chromium.org7c537e22008-10-16 08:43:32 +00003671void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003672 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003674 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003675 LoadAndSpill(args->at(0));
3676 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003677 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003678 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003679 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003680 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3681 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003682 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683 // Load the value.
3684 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003685 leave.Bind();
3686 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687}
3688
3689
ager@chromium.org7c537e22008-10-16 08:43:32 +00003690void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003691 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003693 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003694 LoadAndSpill(args->at(0)); // Load the object.
3695 LoadAndSpill(args->at(1)); // Load the value.
3696 frame_->EmitPop(r0); // r0 contains value
3697 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698 // if (object->IsSmi()) return object.
3699 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003700 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003701 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3702 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003703 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 // Store the value.
3705 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3706 // Update the write barrier.
3707 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3708 __ RecordWrite(r1, r2, r3);
3709 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003710 leave.Bind();
3711 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712}
3713
3714
ager@chromium.org7c537e22008-10-16 08:43:32 +00003715void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003716 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003718 LoadAndSpill(args->at(0));
3719 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003720 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721 cc_reg_ = eq;
3722}
3723
3724
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003725void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003726 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003727 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3728 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003729#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003730 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 LoadAndSpill(args->at(1));
3732 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003733 __ CallRuntime(Runtime::kLog, 2);
3734 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003735#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003736 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003737 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003738}
3739
3740
ager@chromium.org7c537e22008-10-16 08:43:32 +00003741void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003742 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003743 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003744 LoadAndSpill(args->at(0));
3745 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003746 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003747 cc_reg_ = eq;
3748}
3749
3750
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003751// Generates the Math.pow method - currently just calls runtime.
3752void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3753 ASSERT(args->length() == 2);
3754 Load(args->at(0));
3755 Load(args->at(1));
3756 frame_->CallRuntime(Runtime::kMath_pow, 2);
3757 frame_->EmitPush(r0);
3758}
3759
3760
3761// Generates the Math.sqrt method - currently just calls runtime.
3762void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
3763 ASSERT(args->length() == 1);
3764 Load(args->at(0));
3765 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
3766 frame_->EmitPush(r0);
3767}
3768
3769
kasper.lund7276f142008-07-30 08:49:36 +00003770// This should generate code that performs a charCodeAt() call or returns
3771// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3772// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003773void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003774 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00003775 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003776 Comment(masm_, "[ GenerateFastCharCodeAt");
3777
3778 LoadAndSpill(args->at(0));
3779 LoadAndSpill(args->at(1));
3780 frame_->EmitPop(r0); // Index.
3781 frame_->EmitPop(r1); // String.
3782
3783 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3784
3785 __ tst(r1, Operand(kSmiTagMask));
3786 __ b(eq, &slow); // The 'string' was a Smi.
3787
3788 ASSERT(kSmiTag == 0);
3789 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3790 __ b(ne, &slow); // The index was negative or not a Smi.
3791
3792 __ bind(&try_again_with_new_string);
3793 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3794 __ b(ge, &slow);
3795
3796 // Now r2 has the string type.
3797 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00003798 // Now r3 has the length of the string. Compare with the index.
3799 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003800 __ b(le, &slow);
3801
3802 // Here we know the index is in range. Check that string is sequential.
3803 ASSERT_EQ(0, kSeqStringTag);
3804 __ tst(r2, Operand(kStringRepresentationMask));
3805 __ b(ne, &not_a_flat_string);
3806
3807 // Check whether it is an ASCII string.
3808 ASSERT_EQ(0, kTwoByteStringTag);
3809 __ tst(r2, Operand(kStringEncodingMask));
3810 __ b(ne, &ascii_string);
3811
3812 // 2-byte string. We can add without shifting since the Smi tag size is the
3813 // log2 of the number of bytes in a two-byte character.
3814 ASSERT_EQ(1, kSmiTagSize);
3815 ASSERT_EQ(0, kSmiShiftSize);
3816 __ add(r1, r1, Operand(r0));
3817 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3818 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3819 __ jmp(&end);
3820
3821 __ bind(&ascii_string);
3822 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3823 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3824 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3825 __ jmp(&end);
3826
3827 __ bind(&not_a_flat_string);
3828 __ and_(r2, r2, Operand(kStringRepresentationMask));
3829 __ cmp(r2, Operand(kConsStringTag));
3830 __ b(ne, &slow);
3831
3832 // ConsString.
3833 // Check that the right hand side is the empty string (ie if this is really a
3834 // flat string in a cons string). If that is not the case we would rather go
3835 // to the runtime system now, to flatten the string.
3836 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3837 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3838 __ cmp(r2, Operand(r3));
3839 __ b(ne, &slow);
3840
3841 // Get the first of the two strings.
3842 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3843 __ jmp(&try_again_with_new_string);
3844
3845 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003846 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003847
3848 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003850}
3851
3852
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003853void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
3854 Comment(masm_, "[ GenerateCharFromCode");
3855 ASSERT(args->length() == 1);
3856
3857 LoadAndSpill(args->at(0));
3858 frame_->EmitPop(r0);
3859
3860 JumpTarget slow_case;
3861 JumpTarget exit;
3862
3863 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3864 ASSERT(kSmiTag == 0);
3865 ASSERT(kSmiShiftSize == 0);
3866 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3867 __ tst(r0, Operand(kSmiTagMask |
3868 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3869 slow_case.Branch(nz);
3870
3871 ASSERT(kSmiTag == 0);
3872 __ mov(r1, Operand(Factory::single_character_string_cache()));
3873 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
3874 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
3875 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3876 __ cmp(r1, ip);
3877 slow_case.Branch(eq);
3878
3879 frame_->EmitPush(r1);
3880 exit.Jump();
3881
3882 slow_case.Bind();
3883 frame_->EmitPush(r0);
3884 frame_->CallRuntime(Runtime::kCharFromCode, 1);
3885 frame_->EmitPush(r0);
3886
3887 exit.Bind();
3888}
3889
3890
ager@chromium.org7c537e22008-10-16 08:43:32 +00003891void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003892 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003893 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003895 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003896 // We need the CC bits to come out as not_equal in the case where the
3897 // object is a smi. This can't be done with the usual test opcode so
3898 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003899 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003900 __ and_(r1, r0, Operand(kSmiTagMask));
3901 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003902 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003903 // It is a heap object - get the map. Check if the object is a JS array.
3904 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003905 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003906 cc_reg_ = eq;
3907}
3908
3909
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003910void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003911 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003912 ASSERT(args->length() == 1);
3913 LoadAndSpill(args->at(0));
3914 JumpTarget answer;
3915 // We need the CC bits to come out as not_equal in the case where the
3916 // object is a smi. This can't be done with the usual test opcode so
3917 // we use XOR to get the right CC bits.
3918 frame_->EmitPop(r0);
3919 __ and_(r1, r0, Operand(kSmiTagMask));
3920 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3921 answer.Branch(ne);
3922 // It is a heap object - get the map. Check if the object is a regexp.
3923 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3924 answer.Bind();
3925 cc_reg_ = eq;
3926}
3927
3928
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003929void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3930 // This generates a fast version of:
3931 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00003932 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003933 ASSERT(args->length() == 1);
3934 LoadAndSpill(args->at(0));
3935 frame_->EmitPop(r1);
3936 __ tst(r1, Operand(kSmiTagMask));
3937 false_target()->Branch(eq);
3938
3939 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3940 __ cmp(r1, ip);
3941 true_target()->Branch(eq);
3942
3943 Register map_reg = r2;
3944 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3945 // Undetectable objects behave like undefined when tested with typeof.
3946 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3947 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3948 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3949 false_target()->Branch(eq);
3950
3951 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3952 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3953 false_target()->Branch(lt);
3954 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3955 cc_reg_ = le;
3956}
3957
3958
3959void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3960 // This generates a fast version of:
3961 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00003962 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003963 ASSERT(args->length() == 1);
3964 LoadAndSpill(args->at(0));
3965 frame_->EmitPop(r0);
3966 __ tst(r0, Operand(kSmiTagMask));
3967 false_target()->Branch(eq);
3968 Register map_reg = r2;
3969 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3970 cc_reg_ = eq;
3971}
3972
3973
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003974void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003975 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003976 ASSERT(args->length() == 1);
3977 LoadAndSpill(args->at(0));
3978 frame_->EmitPop(r0);
3979 __ tst(r0, Operand(kSmiTagMask));
3980 false_target()->Branch(eq);
3981 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3982 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3983 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3984 cc_reg_ = ne;
3985}
3986
3987
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003988void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003989 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003990 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003991
3992 // Get the frame pointer for the calling frame.
3993 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3994
3995 // Skip the arguments adaptor frame if it exists.
3996 Label check_frame_marker;
3997 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003998 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003999 __ b(ne, &check_frame_marker);
4000 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
4001
4002 // Check the marker in the calling frame.
4003 __ bind(&check_frame_marker);
4004 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
4005 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4006 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004007}
4008
4009
ager@chromium.org7c537e22008-10-16 08:43:32 +00004010void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004011 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012 ASSERT(args->length() == 0);
4013
lrn@chromium.org25156de2010-04-06 13:10:27 +00004014 Label exit;
4015
4016 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00004017 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018
lrn@chromium.org25156de2010-04-06 13:10:27 +00004019 // Check if the calling frame is an arguments adaptor frame.
4020 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4021 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4022 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4023 __ b(ne, &exit);
4024
4025 // Arguments adaptor case: Read the arguments length from the
4026 // adaptor frame.
4027 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4028
4029 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004030 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031}
4032
4033
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004034void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004035 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036 ASSERT(args->length() == 1);
4037
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004038 // Satisfy contract with ArgumentsAccessStub:
4039 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004040 LoadAndSpill(args->at(0));
4041 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004042 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043
4044 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004045 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004046 frame_->CallStub(&stub, 0);
4047 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048}
4049
4050
ager@chromium.org357bf652010-04-12 11:30:10 +00004051void CodeGenerator::GenerateRandomHeapNumber(
4052 ZoneList<Expression*>* args) {
4053 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004054 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004055
4056 Label slow_allocate_heapnumber;
4057 Label heapnumber_allocated;
4058
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004059 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004060 __ jmp(&heapnumber_allocated);
4061
4062 __ bind(&slow_allocate_heapnumber);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004063 // To allocate a heap number, and ensure that it is not a smi, we
4064 // call the runtime function FUnaryMinus on 0, returning the double
4065 // -0.0. A new, distinct heap number is returned each time.
ager@chromium.org357bf652010-04-12 11:30:10 +00004066 __ mov(r0, Operand(Smi::FromInt(0)));
4067 __ push(r0);
4068 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004069 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004070
4071 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004072
4073 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4074 // by computing:
4075 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4076 if (CpuFeatures::IsSupported(VFP3)) {
4077 __ PrepareCallCFunction(0, r1);
4078 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4079
4080 CpuFeatures::Scope scope(VFP3);
4081 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4082 // Create this constant using mov/orr to avoid PC relative load.
4083 __ mov(r1, Operand(0x41000000));
4084 __ orr(r1, r1, Operand(0x300000));
4085 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4086 __ vmov(d7, r0, r1);
4087 // Move 0x4130000000000000 to VFP.
4088 __ mov(r0, Operand(0));
4089 __ vmov(d8, r0, r1);
4090 // Subtract and store the result in the heap number.
4091 __ vsub(d7, d7, d8);
4092 __ sub(r0, r4, Operand(kHeapObjectTag));
4093 __ vstr(d7, r0, HeapNumber::kValueOffset);
4094 frame_->EmitPush(r4);
4095 } else {
4096 __ mov(r0, Operand(r4));
4097 __ PrepareCallCFunction(1, r1);
4098 __ CallCFunction(
4099 ExternalReference::fill_heap_number_with_random_function(), 1);
4100 frame_->EmitPush(r0);
4101 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004102}
4103
4104
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004105void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4106 ASSERT_EQ(2, args->length());
4107
4108 Load(args->at(0));
4109 Load(args->at(1));
4110
ager@chromium.org5c838252010-02-19 08:53:10 +00004111 StringAddStub stub(NO_STRING_ADD_FLAGS);
4112 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004113 frame_->EmitPush(r0);
4114}
4115
4116
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004117void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4118 ASSERT_EQ(3, args->length());
4119
4120 Load(args->at(0));
4121 Load(args->at(1));
4122 Load(args->at(2));
4123
ager@chromium.org5c838252010-02-19 08:53:10 +00004124 SubStringStub stub;
4125 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004126 frame_->EmitPush(r0);
4127}
4128
4129
4130void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4131 ASSERT_EQ(2, args->length());
4132
4133 Load(args->at(0));
4134 Load(args->at(1));
4135
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004136 StringCompareStub stub;
4137 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004138 frame_->EmitPush(r0);
4139}
4140
4141
4142void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4143 ASSERT_EQ(4, args->length());
4144
4145 Load(args->at(0));
4146 Load(args->at(1));
4147 Load(args->at(2));
4148 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004149 RegExpExecStub stub;
4150 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004151 frame_->EmitPush(r0);
4152}
4153
4154
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004155void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4156 // No stub. This code only occurs a few times in regexp.js.
4157 const int kMaxInlineLength = 100;
4158 ASSERT_EQ(3, args->length());
4159 Load(args->at(0)); // Size of array, smi.
4160 Load(args->at(1)); // "index" property value.
4161 Load(args->at(2)); // "input" property value.
4162 {
4163 VirtualFrame::SpilledScope spilled_scope(frame_);
4164 Label slowcase;
4165 Label done;
4166 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4167 STATIC_ASSERT(kSmiTag == 0);
4168 STATIC_ASSERT(kSmiTagSize == 1);
4169 __ tst(r1, Operand(kSmiTagMask));
4170 __ b(ne, &slowcase);
4171 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4172 __ b(hi, &slowcase);
4173 // Smi-tagging is equivalent to multiplying by 2.
4174 // Allocate RegExpResult followed by FixedArray with size in ebx.
4175 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4176 // Elements: [Map][Length][..elements..]
4177 // Size of JSArray with two in-object properties and the header of a
4178 // FixedArray.
4179 int objects_size =
4180 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4181 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4182 __ add(r2, r5, Operand(objects_size));
4183 __ AllocateInNewSpace(r2, // In: Size, in words.
4184 r0, // Out: Start of allocation (tagged).
4185 r3, // Scratch register.
4186 r4, // Scratch register.
4187 &slowcase,
4188 TAG_OBJECT);
4189 // r0: Start of allocated area, object-tagged.
4190 // r1: Number of elements in array, as smi.
4191 // r5: Number of elements, untagged.
4192
4193 // Set JSArray map to global.regexp_result_map().
4194 // Set empty properties FixedArray.
4195 // Set elements to point to FixedArray allocated right after the JSArray.
4196 // Interleave operations for better latency.
4197 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4198 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4199 __ mov(r4, Operand(Factory::empty_fixed_array()));
4200 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4201 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4202 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4203 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4204 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4205
4206 // Set input, index and length fields from arguments.
4207 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4208 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4209 __ add(sp, sp, Operand(kPointerSize));
4210 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4211 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4212
4213 // Fill out the elements FixedArray.
4214 // r0: JSArray, tagged.
4215 // r3: FixedArray, tagged.
4216 // r5: Number of elements in array, untagged.
4217
4218 // Set map.
4219 __ mov(r2, Operand(Factory::fixed_array_map()));
4220 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4221 // Set FixedArray length.
4222 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4223 // Fill contents of fixed-array with the-hole.
4224 __ mov(r2, Operand(Factory::the_hole_value()));
4225 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4226 // Fill fixed array elements with hole.
4227 // r0: JSArray, tagged.
4228 // r2: the hole.
4229 // r3: Start of elements in FixedArray.
4230 // r5: Number of elements to fill.
4231 Label loop;
4232 __ tst(r5, Operand(r5));
4233 __ bind(&loop);
4234 __ b(le, &done); // Jump if r1 is negative or zero.
4235 __ sub(r5, r5, Operand(1), SetCC);
4236 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4237 __ jmp(&loop);
4238
4239 __ bind(&slowcase);
4240 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4241
4242 __ bind(&done);
4243 }
4244 frame_->Forget(3);
4245 frame_->EmitPush(r0);
4246}
4247
4248
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004249class DeferredSearchCache: public DeferredCode {
4250 public:
4251 DeferredSearchCache(Register dst, Register cache, Register key)
4252 : dst_(dst), cache_(cache), key_(key) {
4253 set_comment("[ DeferredSearchCache");
4254 }
4255
4256 virtual void Generate();
4257
4258 private:
4259 Register dst_, cache_, key_;
4260};
4261
4262
4263void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00004264 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004265 __ CallRuntime(Runtime::kGetFromCache, 2);
4266 if (!dst_.is(r0)) {
4267 __ mov(dst_, r0);
4268 }
4269}
4270
4271
4272void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4273 ASSERT_EQ(2, args->length());
4274
4275 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4276 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4277
4278 Handle<FixedArray> jsfunction_result_caches(
4279 Top::global_context()->jsfunction_result_caches());
4280 if (jsfunction_result_caches->length() <= cache_id) {
4281 __ Abort("Attempt to use undefined cache.");
4282 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
4283 frame_->EmitPush(r0);
4284 return;
4285 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004286
4287 Load(args->at(1));
4288 frame_->EmitPop(r2);
4289
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004290 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
4291 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
4292 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4293 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
4294
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004295 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4296
4297 const int kFingerOffset =
4298 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4299 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004300 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4301 // r0 now holds finger offset as a smi.
4302 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4303 // r3 now points to the start of fixed array elements.
4304 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4305 // Note side effect of PreIndex: r3 now points to the key of the pair.
4306 __ cmp(r2, r0);
4307 deferred->Branch(ne);
4308
4309 __ ldr(r0, MemOperand(r3, kPointerSize));
4310
4311 deferred->BindExit();
4312 frame_->EmitPush(r0);
4313}
4314
4315
ager@chromium.org5c838252010-02-19 08:53:10 +00004316void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4317 ASSERT_EQ(args->length(), 1);
4318
4319 // Load the argument on the stack and jump to the runtime.
4320 Load(args->at(0));
4321
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004322 NumberToStringStub stub;
4323 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004324 frame_->EmitPush(r0);
4325}
4326
4327
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004328void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
4329 Comment cmnt(masm_, "[ GenerateSwapElements");
4330
4331 ASSERT_EQ(3, args->length());
4332
4333 Load(args->at(0));
4334 Load(args->at(1));
4335 Load(args->at(2));
4336
4337 frame_->CallRuntime(Runtime::kSwapElements, 3);
4338 frame_->EmitPush(r0);
4339}
4340
4341
ager@chromium.org357bf652010-04-12 11:30:10 +00004342void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4343 Comment cmnt(masm_, "[ GenerateCallFunction");
4344
4345 ASSERT(args->length() >= 2);
4346
4347 int n_args = args->length() - 2; // for receiver and function.
4348 Load(args->at(0)); // receiver
4349 for (int i = 0; i < n_args; i++) {
4350 Load(args->at(i + 1));
4351 }
4352 Load(args->at(n_args + 1)); // function
4353 frame_->CallJSFunction(n_args);
4354 frame_->EmitPush(r0);
4355}
4356
4357
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004358void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4359 ASSERT_EQ(args->length(), 1);
4360 // Load the argument on the stack and jump to the runtime.
4361 Load(args->at(0));
4362 frame_->CallRuntime(Runtime::kMath_sin, 1);
4363 frame_->EmitPush(r0);
4364}
4365
4366
4367void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4368 ASSERT_EQ(args->length(), 1);
4369 // Load the argument on the stack and jump to the runtime.
4370 Load(args->at(0));
4371 frame_->CallRuntime(Runtime::kMath_cos, 1);
4372 frame_->EmitPush(r0);
4373}
4374
4375
ager@chromium.org7c537e22008-10-16 08:43:32 +00004376void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004377 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004378 ASSERT(args->length() == 2);
4379
4380 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004381 LoadAndSpill(args->at(0));
4382 LoadAndSpill(args->at(1));
4383 frame_->EmitPop(r0);
4384 frame_->EmitPop(r1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004385 __ cmp(r0, r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004386 cc_reg_ = eq;
4387}
4388
4389
ager@chromium.org7c537e22008-10-16 08:43:32 +00004390void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004391#ifdef DEBUG
4392 int original_height = frame_->height();
4393#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004394 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004395 if (CheckForInlineRuntimeCall(node)) {
4396 ASSERT((has_cc() && frame_->height() == original_height) ||
4397 (!has_cc() && frame_->height() == original_height + 1));
4398 return;
4399 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004400
4401 ZoneList<Expression*>* args = node->arguments();
4402 Comment cmnt(masm_, "[ CallRuntime");
4403 Runtime::Function* function = node->function();
4404
ager@chromium.org41826e72009-03-30 13:30:57 +00004405 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004406 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004407 // Push the builtins object found in the current global object.
4408 __ ldr(r1, GlobalObject());
4409 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004410 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004411 }
mads.s.ager31e71382008-08-13 09:32:07 +00004412
ager@chromium.org41826e72009-03-30 13:30:57 +00004413 // Push the arguments ("left-to-right").
4414 int arg_count = args->length();
4415 for (int i = 0; i < arg_count; i++) {
4416 LoadAndSpill(args->at(i));
4417 }
mads.s.ager31e71382008-08-13 09:32:07 +00004418
ager@chromium.org41826e72009-03-30 13:30:57 +00004419 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004420 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004421 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004422 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4423 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004424 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004425 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004426 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004427 } else {
4428 // Call the C runtime function.
4429 frame_->CallRuntime(function, arg_count);
4430 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004431 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004432 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004433}
4434
4435
ager@chromium.org7c537e22008-10-16 08:43:32 +00004436void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004437#ifdef DEBUG
4438 int original_height = frame_->height();
4439#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004440 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004441 Comment cmnt(masm_, "[ UnaryOperation");
4442
4443 Token::Value op = node->op();
4444
4445 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004446 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004447 false_target(),
4448 true_target(),
4449 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004450 // LoadCondition may (and usually does) leave a test and branch to
4451 // be emitted by the caller. In that case, negate the condition.
4452 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004453
4454 } else if (op == Token::DELETE) {
4455 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004456 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004457 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004458 LoadAndSpill(property->obj());
4459 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004460 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004461
mads.s.ager31e71382008-08-13 09:32:07 +00004462 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004463 Slot* slot = variable->slot();
4464 if (variable->is_global()) {
4465 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004466 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004467 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004468 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004469
4470 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4471 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004472 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004473 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004474 frame_->EmitPush(r0);
4475 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004476 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004477 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004478 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004479 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004480 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004481
mads.s.ager31e71382008-08-13 09:32:07 +00004482 } else {
4483 // Default: Result of deleting non-global, not dynamically
4484 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004485 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004486 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004487
4488 } else {
4489 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004490 LoadAndSpill(node->expression()); // may have side-effects
4491 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004492 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004493 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004494 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004495
4496 } else if (op == Token::TYPEOF) {
4497 // Special case for loading the typeof expression; see comment on
4498 // LoadTypeofExpression().
4499 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004500 frame_->CallRuntime(Runtime::kTypeof, 1);
4501 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004502
4503 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004504 bool overwrite =
4505 (node->expression()->AsBinaryOperation() != NULL &&
4506 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004507 LoadAndSpill(node->expression());
4508 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004509 switch (op) {
4510 case Token::NOT:
4511 case Token::DELETE:
4512 case Token::TYPEOF:
4513 UNREACHABLE(); // handled above
4514 break;
4515
4516 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004517 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004518 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004519 break;
4520 }
4521
4522 case Token::BIT_NOT: {
4523 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004524 JumpTarget smi_label;
4525 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004526 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004527 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004528
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004529 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4530 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004531 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004532
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004533 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004534 __ mvn(r0, Operand(r0));
4535 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004536 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004537 break;
4538 }
4539
4540 case Token::VOID:
4541 // since the stack top is cached in r0, popping and then
4542 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004543 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004544 break;
4545
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004546 case Token::ADD: {
4547 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004548 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004549 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004550 continue_label.Branch(eq);
4551 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004552 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004553 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004554 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004555 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004556 default:
4557 UNREACHABLE();
4558 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004559 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004560 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004561 ASSERT(!has_valid_frame() ||
4562 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004563 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004564}
4565
4566
ager@chromium.org7c537e22008-10-16 08:43:32 +00004567void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004568#ifdef DEBUG
4569 int original_height = frame_->height();
4570#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004571 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004572 Comment cmnt(masm_, "[ CountOperation");
4573
4574 bool is_postfix = node->is_postfix();
4575 bool is_increment = node->op() == Token::INC;
4576
4577 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4578 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4579
4580 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004581 if (is_postfix) {
4582 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004583 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004584 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004585
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004586 // A constant reference is not saved to, so a constant reference is not a
4587 // compound assignment reference.
4588 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004589 if (target.is_illegal()) {
4590 // Spoof the virtual frame to have the expected height (one higher
4591 // than on entry).
4592 if (!is_postfix) {
4593 __ mov(r0, Operand(Smi::FromInt(0)));
4594 frame_->EmitPush(r0);
4595 }
4596 ASSERT(frame_->height() == original_height + 1);
4597 return;
4598 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004599 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004600 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004601
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004602 JumpTarget slow;
4603 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004604
4605 // Load the value (1) into register r1.
4606 __ mov(r1, Operand(Smi::FromInt(1)));
4607
4608 // Check for smi operand.
4609 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004610 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004611
4612 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004613 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004614 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004615 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004616
4617 // Perform optimistic increment/decrement.
4618 if (is_increment) {
4619 __ add(r0, r0, Operand(r1), SetCC);
4620 } else {
4621 __ sub(r0, r0, Operand(r1), SetCC);
4622 }
4623
4624 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004625 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004626
4627 // Revert optimistic increment/decrement.
4628 if (is_increment) {
4629 __ sub(r0, r0, Operand(r1));
4630 } else {
4631 __ add(r0, r0, Operand(r1));
4632 }
4633
4634 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004635 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004636 {
4637 // Convert the operand to a number.
4638 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004639 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004641 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004642 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004643 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004644 }
4645
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004646 // Compute the new value.
4647 __ mov(r1, Operand(Smi::FromInt(1)));
4648 frame_->EmitPush(r0);
4649 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004650 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004651 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004652 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004653 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004654 }
4655
4656 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004657 exit.Bind();
4658 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004659 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004660 }
4661
4662 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004663 if (is_postfix) frame_->EmitPop(r0);
4664 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004665}
4666
4667
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004668void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004669 // According to ECMA-262 section 11.11, page 58, the binary logical
4670 // operators must yield the result of one of the two expressions
4671 // before any ToBoolean() conversions. This means that the value
4672 // produced by a && or || operator is not necessarily a boolean.
4673
4674 // NOTE: If the left hand side produces a materialized value (not in
4675 // the CC register), we force the right hand side to do the
4676 // same. This is necessary because we may have to branch to the exit
4677 // after evaluating the left hand side (due to the shortcut
4678 // semantics), but the compiler must (statically) know if the result
4679 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004680 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004681 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004682 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004683 &is_true,
4684 false_target(),
4685 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004686 if (has_valid_frame() && !has_cc()) {
4687 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004688 JumpTarget pop_and_continue;
4689 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004690
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004691 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004692 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004693 // Avoid popping the result if it converts to 'false' using the
4694 // standard ToBoolean() conversion as described in ECMA-262,
4695 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004696 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004697 Branch(false, &exit);
4698
4699 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004700 pop_and_continue.Bind();
4701 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004702
4703 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004704 is_true.Bind();
4705 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004706
4707 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004708 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004709 } else if (has_cc() || is_true.is_linked()) {
4710 // The left-hand side is either (a) partially compiled to
4711 // control flow with a final branch left to emit or (b) fully
4712 // compiled to control flow and possibly true.
4713 if (has_cc()) {
4714 Branch(false, false_target());
4715 }
4716 is_true.Bind();
4717 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004718 true_target(),
4719 false_target(),
4720 false);
4721 } else {
4722 // Nothing to do.
4723 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004724 }
4725
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004726 } else {
4727 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004728 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004729 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004730 true_target(),
4731 &is_false,
4732 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004733 if (has_valid_frame() && !has_cc()) {
4734 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004735 JumpTarget pop_and_continue;
4736 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004737
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004738 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004739 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004740 // Avoid popping the result if it converts to 'true' using the
4741 // standard ToBoolean() conversion as described in ECMA-262,
4742 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004743 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004744 Branch(true, &exit);
4745
4746 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004747 pop_and_continue.Bind();
4748 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004749
4750 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004751 is_false.Bind();
4752 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004753
4754 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004755 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004756 } else if (has_cc() || is_false.is_linked()) {
4757 // The left-hand side is either (a) partially compiled to
4758 // control flow with a final branch left to emit or (b) fully
4759 // compiled to control flow and possibly false.
4760 if (has_cc()) {
4761 Branch(true, true_target());
4762 }
4763 is_false.Bind();
4764 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004765 true_target(),
4766 false_target(),
4767 false);
4768 } else {
4769 // Nothing to do.
4770 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004771 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004772 }
4773}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004774
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004775
4776void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4777#ifdef DEBUG
4778 int original_height = frame_->height();
4779#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004780 Comment cmnt(masm_, "[ BinaryOperation");
4781
4782 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004783 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004784 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785 } else {
4786 // Optimize for the case where (at least) one of the expressions
4787 // is a literal small integer.
4788 Literal* lliteral = node->left()->AsLiteral();
4789 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004790 // NOTE: The code below assumes that the slow cases (calls to runtime)
4791 // never return a constant/immutable object.
4792 bool overwrite_left =
4793 (node->left()->AsBinaryOperation() != NULL &&
4794 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4795 bool overwrite_right =
4796 (node->right()->AsBinaryOperation() != NULL &&
4797 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004798
4799 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004800 VirtualFrame::RegisterAllocationScope scope(this);
4801 Load(node->left());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004802 SmiOperation(node->op(),
4803 rliteral->handle(),
4804 false,
4805 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004806 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004807 VirtualFrame::RegisterAllocationScope scope(this);
4808 Load(node->right());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004809 SmiOperation(node->op(),
4810 lliteral->handle(),
4811 true,
4812 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004813 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00004814 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004815 OverwriteMode overwrite_mode = NO_OVERWRITE;
4816 if (overwrite_left) {
4817 overwrite_mode = OVERWRITE_LEFT;
4818 } else if (overwrite_right) {
4819 overwrite_mode = OVERWRITE_RIGHT;
4820 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004821 Load(node->left());
4822 Load(node->right());
4823 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004824 }
4825 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004826 ASSERT(!has_valid_frame() ||
4827 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004828 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004829}
4830
4831
ager@chromium.org7c537e22008-10-16 08:43:32 +00004832void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004833#ifdef DEBUG
4834 int original_height = frame_->height();
4835#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004836 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004837 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004838 frame_->EmitPush(r0);
4839 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004840}
4841
4842
ager@chromium.org7c537e22008-10-16 08:43:32 +00004843void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004844#ifdef DEBUG
4845 int original_height = frame_->height();
4846#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004847 Comment cmnt(masm_, "[ CompareOperation");
4848
ager@chromium.org357bf652010-04-12 11:30:10 +00004849 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
4850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004851 // Get the expressions from the node.
4852 Expression* left = node->left();
4853 Expression* right = node->right();
4854 Token::Value op = node->op();
4855
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004856 // To make null checks efficient, we check if either left or right is the
4857 // literal 'null'. If so, we optimize the code by inlining a null check
4858 // instead of calling the (very) general runtime routine for checking
4859 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004860 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004861 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004862 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004863 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004864 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4865 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004866 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004867 Load(left_is_null ? right : left);
4868 Register tos = frame_->PopToRegister();
4869 // JumpTargets can't cope with register allocation yet.
4870 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004871 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004872 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004873
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004874 // The 'null' value is only equal to 'undefined' if using non-strict
4875 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004876 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004877 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004878
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004879 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004880 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004881 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004882
ager@chromium.org357bf652010-04-12 11:30:10 +00004883 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004884 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004885
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004886 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004887 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4888 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
4889 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4890 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004891 }
4892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004893 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004894 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004895 return;
4896 }
4897 }
4898
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004899 // To make typeof testing for natives implemented in JavaScript really
4900 // efficient, we generate special code for expressions of the form:
4901 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004902 UnaryOperation* operation = left->AsUnaryOperation();
4903 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4904 (operation != NULL && operation->op() == Token::TYPEOF) &&
4905 (right->AsLiteral() != NULL &&
4906 right->AsLiteral()->handle()->IsString())) {
4907 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4908
ager@chromium.org357bf652010-04-12 11:30:10 +00004909 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004910 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00004911 Register tos = frame_->PopToRegister();
4912
4913 // JumpTargets can't cope with register allocation yet.
4914 frame_->SpillAll();
4915
4916 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004917
4918 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004919 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004920 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004921 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004922 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004923 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004924 cc_reg_ = eq;
4925
4926 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004927 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004928 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004929
ager@chromium.org357bf652010-04-12 11:30:10 +00004930 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004931
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004932 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004933 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4934 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4935 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004936 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004937
ager@chromium.org357bf652010-04-12 11:30:10 +00004938 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
4939 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004940 cc_reg_ = lt;
4941
4942 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004943 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004944 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004945 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004946 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004947 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004948 cc_reg_ = eq;
4949
4950 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004951 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004952 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004953 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004954
ager@chromium.org357bf652010-04-12 11:30:10 +00004955 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004956 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004957
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004958 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004959 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4960 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4961 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4962 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004963
4964 cc_reg_ = eq;
4965
4966 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004967 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004968 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004969 Register map_reg = scratch;
4970 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004971 true_target()->Branch(eq);
4972 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00004973 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974 cc_reg_ = eq;
4975
4976 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004977 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004978 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004979
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004980 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004981 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004982 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004983
ager@chromium.org357bf652010-04-12 11:30:10 +00004984 Register map_reg = scratch;
4985 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004986 false_target()->Branch(eq);
4987
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004988 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004989 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4990 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4991 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004992 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004993
ager@chromium.org357bf652010-04-12 11:30:10 +00004994 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4995 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004996 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00004997 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004998 cc_reg_ = le;
4999
5000 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005001 // Uncommon case: typeof testing against a string literal that is
5002 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005003 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005004 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005005 ASSERT(!has_valid_frame() ||
5006 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005007 return;
5008 }
5009
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010 switch (op) {
5011 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005012 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005013 break;
5014
5015 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005016 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005017 break;
5018
5019 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005020 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021 break;
5022
5023 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005024 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005025 break;
5026
5027 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005028 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005029 break;
5030
5031 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005032 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005033 break;
5034
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005035 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005036 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005037 LoadAndSpill(left);
5038 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005039 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005040 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005041 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005042 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005044 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005045 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005046 LoadAndSpill(left);
5047 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005048 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005049 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005050 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005051 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005052 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005053 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005054 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005055
5056 default:
5057 UNREACHABLE();
5058 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005059 ASSERT((has_cc() && frame_->height() == original_height) ||
5060 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061}
5062
5063
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005064class DeferredReferenceGetNamedValue: public DeferredCode {
5065 public:
5066 explicit DeferredReferenceGetNamedValue(Handle<String> name) : name_(name) {
5067 set_comment("[ DeferredReferenceGetNamedValue");
5068 }
5069
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005070 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005071
5072 private:
5073 Handle<String> name_;
5074};
5075
5076
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005077void DeferredReferenceGetNamedValue::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005078 Register scratch1 = VirtualFrame::scratch0();
5079 Register scratch2 = VirtualFrame::scratch1();
5080 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
5081 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
5082
5083 // Setup the registers and call load IC.
5084 // On entry to this deferred code, r0 is assumed to already contain the
5085 // receiver from the top of the stack.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005086 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005087
5088 // The rest of the instructions in the deferred code must be together.
5089 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5090 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
5091 __ Call(ic, RelocInfo::CODE_TARGET);
5092 // The call must be followed by a nop(1) instruction to indicate that the
5093 // in-object has been inlined.
5094 __ nop(PROPERTY_ACCESS_INLINED);
5095
5096 // Block the constant pool for one more instruction after leaving this
5097 // constant pool block scope to include the branch instruction ending the
5098 // deferred code.
5099 __ BlockConstPoolFor(1);
5100 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005101}
5102
5103
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005104class DeferredReferenceGetKeyedValue: public DeferredCode {
5105 public:
5106 DeferredReferenceGetKeyedValue() {
5107 set_comment("[ DeferredReferenceGetKeyedValue");
5108 }
5109
5110 virtual void Generate();
5111};
5112
5113
5114void DeferredReferenceGetKeyedValue::Generate() {
5115 Register scratch1 = VirtualFrame::scratch0();
5116 Register scratch2 = VirtualFrame::scratch1();
5117 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
5118 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
5119
5120 // The rest of the instructions in the deferred code must be together.
5121 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5122 // Call keyed load IC. It has all arguments on the stack.
5123 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
5124 __ Call(ic, RelocInfo::CODE_TARGET);
5125 // The call must be followed by a nop instruction to indicate that the
5126 // keyed load has been inlined.
5127 __ nop(PROPERTY_ACCESS_INLINED);
5128
5129 // Block the constant pool for one more instruction after leaving this
5130 // constant pool block scope to include the branch instruction ending the
5131 // deferred code.
5132 __ BlockConstPoolFor(1);
5133 }
5134}
5135
5136
5137class DeferredReferenceSetKeyedValue: public DeferredCode {
5138 public:
5139 DeferredReferenceSetKeyedValue() {
5140 set_comment("[ DeferredReferenceSetKeyedValue");
5141 }
5142
5143 virtual void Generate();
5144};
5145
5146
5147void DeferredReferenceSetKeyedValue::Generate() {
5148 Register scratch1 = VirtualFrame::scratch0();
5149 Register scratch2 = VirtualFrame::scratch1();
5150 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
5151 __ IncrementCounter(
5152 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
5153
5154 // The rest of the instructions in the deferred code must be together.
5155 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5156 // Call keyed load IC. It has receiver amd key on the stack and the value to
5157 // store in r0.
5158 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
5159 __ Call(ic, RelocInfo::CODE_TARGET);
5160 // The call must be followed by a nop instruction to indicate that the
5161 // keyed store has been inlined.
5162 __ nop(PROPERTY_ACCESS_INLINED);
5163
5164 // Block the constant pool for one more instruction after leaving this
5165 // constant pool block scope to include the branch instruction ending the
5166 // deferred code.
5167 __ BlockConstPoolFor(1);
5168 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005169}
5170
5171
5172void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
5173 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
5174 Comment cmnt(masm(), "[ Load from named Property");
5175 // Setup the name register and call load IC.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005176 frame_->SpillAllButCopyTOSToR0();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005177 __ mov(r2, Operand(name));
5178 frame_->CallLoadIC(is_contextual
5179 ? RelocInfo::CODE_TARGET_CONTEXT
5180 : RelocInfo::CODE_TARGET);
5181 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005182 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005183 Comment cmnt(masm(), "[ Inlined named property load");
5184
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005185 // Counter will be decremented in the deferred code. Placed here to avoid
5186 // having it in the instruction stream below where patching will occur.
5187 __ IncrementCounter(&Counters::named_load_inline, 1,
5188 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005189
5190 // The following instructions are the inlined load of an in-object property.
5191 // Parts of this code is patched, so the exact instructions generated needs
5192 // to be fixed. Therefore the instruction pool is blocked when generating
5193 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005194
5195 // Load the receiver from the stack.
5196 frame_->SpillAllButCopyTOSToR0();
5197
5198 DeferredReferenceGetNamedValue* deferred =
5199 new DeferredReferenceGetNamedValue(name);
5200
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005201#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005202 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005203 Label check_inlined_codesize;
5204 masm_->bind(&check_inlined_codesize);
5205#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005206
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005207 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005208 // Check that the receiver is a heap object.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005209 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005210 deferred->Branch(eq);
5211
5212 // Check the map. The null map used below is patched by the inline cache
5213 // code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005214 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005215 __ mov(r3, Operand(Factory::null_value()));
5216 __ cmp(r2, r3);
5217 deferred->Branch(ne);
5218
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005219 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005220 // inline cache code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005221 __ ldr(r0, MemOperand(r0, 0));
5222
5223 // Make sure that the expected number of instructions are generated.
5224 ASSERT_EQ(kInlinedNamedLoadInstructions,
5225 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005226 }
5227
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005228 deferred->BindExit();
5229 }
5230}
5231
5232
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005233void CodeGenerator::EmitKeyedLoad() {
5234 if (loop_nesting() == 0) {
5235 VirtualFrame::SpilledScope spilled(frame_);
5236 Comment cmnt(masm_, "[ Load from keyed property");
5237 frame_->CallKeyedLoadIC();
5238 } else {
5239 // Inline the keyed load.
5240 Comment cmnt(masm_, "[ Inlined load from keyed property");
5241
5242 // Counter will be decremented in the deferred code. Placed here to avoid
5243 // having it in the instruction stream below where patching will occur.
5244 __ IncrementCounter(&Counters::keyed_load_inline, 1,
5245 frame_->scratch0(), frame_->scratch1());
5246
5247 // Load the receiver and key from the stack.
5248 frame_->SpillAllButCopyTOSToR1R0();
5249 Register receiver = r0;
5250 Register key = r1;
5251 VirtualFrame::SpilledScope spilled(frame_);
5252
5253 DeferredReferenceGetKeyedValue* deferred =
5254 new DeferredReferenceGetKeyedValue();
5255
5256 // Check that the receiver is a heap object.
5257 __ tst(receiver, Operand(kSmiTagMask));
5258 deferred->Branch(eq);
5259
5260 // The following instructions are the part of the inlined load keyed
5261 // property code which can be patched. Therefore the exact number of
5262 // instructions generated need to be fixed, so the constant pool is blocked
5263 // while generating this code.
5264#ifdef DEBUG
5265 int kInlinedKeyedLoadInstructions = 19;
5266 Label check_inlined_codesize;
5267 masm_->bind(&check_inlined_codesize);
5268#endif
5269 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5270 Register scratch1 = VirtualFrame::scratch0();
5271 Register scratch2 = VirtualFrame::scratch1();
5272 // Check the map. The null map used below is patched by the inline cache
5273 // code.
5274 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
5275 __ mov(scratch2, Operand(Factory::null_value()));
5276 __ cmp(scratch1, scratch2);
5277 deferred->Branch(ne);
5278
5279 // Check that the key is a smi.
5280 __ tst(key, Operand(kSmiTagMask));
5281 deferred->Branch(ne);
5282
5283 // Get the elements array from the receiver and check that it
5284 // is not a dictionary.
5285 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
5286 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
5287 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5288 __ cmp(scratch2, ip);
5289 deferred->Branch(ne);
5290
5291 // Check that key is within bounds. Use unsigned comparison to handle
5292 // negative keys.
5293 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
5294 __ cmp(scratch2, Operand(key, ASR, kSmiTagSize));
5295 deferred->Branch(ls); // Unsigned less equal.
5296
5297 // Load and check that the result is not the hole (key is a smi).
5298 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
5299 __ add(scratch1,
5300 scratch1,
5301 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5302 __ ldr(r0,
5303 MemOperand(scratch1, key, LSL,
5304 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5305 __ cmp(r0, scratch2);
5306 // This is the only branch to deferred where r0 and r1 do not contain the
5307 // receiver and key. We can't just load undefined here because we have to
5308 // check the prototype.
5309 deferred->Branch(eq);
5310
5311 // Make sure that the expected number of instructions are generated.
5312 ASSERT_EQ(kInlinedKeyedLoadInstructions,
5313 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5314 }
5315
5316 deferred->BindExit();
5317 }
5318}
5319
5320
5321void CodeGenerator::EmitKeyedStore(StaticType* key_type) {
5322 frame_->AssertIsSpilled();
5323 // Generate inlined version of the keyed store if the code is in a loop
5324 // and the key is likely to be a smi.
5325 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
5326 // Inline the keyed store.
5327 Comment cmnt(masm_, "[ Inlined store to keyed property");
5328
5329 DeferredReferenceSetKeyedValue* deferred =
5330 new DeferredReferenceSetKeyedValue();
5331
5332 // Counter will be decremented in the deferred code. Placed here to avoid
5333 // having it in the instruction stream below where patching will occur.
5334 __ IncrementCounter(&Counters::keyed_store_inline, 1,
5335 frame_->scratch0(), frame_->scratch1());
5336
5337 // Check that the value is a smi. As this inlined code does not set the
5338 // write barrier it is only possible to store smi values.
5339 __ tst(r0, Operand(kSmiTagMask));
5340 deferred->Branch(ne);
5341
5342 // Load the key and receiver from the stack.
5343 __ ldr(r1, MemOperand(sp, 0));
5344 __ ldr(r2, MemOperand(sp, kPointerSize));
5345
5346 // Check that the key is a smi.
5347 __ tst(r1, Operand(kSmiTagMask));
5348 deferred->Branch(ne);
5349
5350 // Check that the receiver is a heap object.
5351 __ tst(r2, Operand(kSmiTagMask));
5352 deferred->Branch(eq);
5353
5354 // Check that the receiver is a JSArray.
5355 __ CompareObjectType(r2, r3, r3, JS_ARRAY_TYPE);
5356 deferred->Branch(ne);
5357
5358 // Check that the key is within bounds. Both the key and the length of
5359 // the JSArray are smis. Use unsigned comparison to handle negative keys.
5360 __ ldr(r3, FieldMemOperand(r2, JSArray::kLengthOffset));
5361 __ cmp(r3, r1);
5362 deferred->Branch(ls); // Unsigned less equal.
5363
5364 // The following instructions are the part of the inlined store keyed
5365 // property code which can be patched. Therefore the exact number of
5366 // instructions generated need to be fixed, so the constant pool is blocked
5367 // while generating this code.
5368#ifdef DEBUG
5369 int kInlinedKeyedStoreInstructions = 7;
5370 Label check_inlined_codesize;
5371 masm_->bind(&check_inlined_codesize);
5372#endif
5373 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5374 // Get the elements array from the receiver and check that it
5375 // is not a dictionary.
5376 __ ldr(r3, FieldMemOperand(r2, JSObject::kElementsOffset));
5377 __ ldr(r4, FieldMemOperand(r3, JSObject::kMapOffset));
5378 // Read the fixed array map from the constant pool (not from the root
5379 // array) so that the value can be patched. When debugging, we patch this
5380 // comparison to always fail so that we will hit the IC call in the
5381 // deferred code which will allow the debugger to break for fast case
5382 // stores.
5383 __ mov(r5, Operand(Factory::fixed_array_map()));
5384 __ cmp(r4, r5);
5385 deferred->Branch(ne);
5386
5387 // Store the value.
5388 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5389 __ str(r0, MemOperand(r3, r1, LSL,
5390 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5391
5392 // Make sure that the expected number of instructions are generated.
5393 ASSERT_EQ(kInlinedKeyedStoreInstructions,
5394 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5395 }
5396
5397 deferred->BindExit();
5398 } else {
5399 frame()->CallKeyedStoreIC();
5400 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005401}
5402
5403
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005404#ifdef DEBUG
5405bool CodeGenerator::HasValidEntryRegisters() { return true; }
5406#endif
5407
5408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005409#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005410#define __ ACCESS_MASM(masm)
5411
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005412
ager@chromium.org7c537e22008-10-16 08:43:32 +00005413Handle<String> Reference::GetName() {
5414 ASSERT(type_ == NAMED);
5415 Property* property = expression_->AsProperty();
5416 if (property == NULL) {
5417 // Global variable reference treated as a named property reference.
5418 VariableProxy* proxy = expression_->AsVariableProxy();
5419 ASSERT(proxy->AsVariable() != NULL);
5420 ASSERT(proxy->AsVariable()->is_global());
5421 return proxy->name();
5422 } else {
5423 Literal* raw_name = property->key()->AsLiteral();
5424 ASSERT(raw_name != NULL);
5425 return Handle<String>(String::cast(*raw_name->handle()));
5426 }
5427}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005428
ager@chromium.org7c537e22008-10-16 08:43:32 +00005429
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005430void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005431 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005432 ASSERT(!is_illegal());
5433 ASSERT(!cgen_->has_cc());
5434 MacroAssembler* masm = cgen_->masm();
5435 Property* property = expression_->AsProperty();
5436 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005437 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005438 }
5439
5440 switch (type_) {
5441 case SLOT: {
5442 Comment cmnt(masm, "[ Load from Slot");
5443 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
5444 ASSERT(slot != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005445 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005446 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005447 }
5448
ager@chromium.org7c537e22008-10-16 08:43:32 +00005449 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00005450 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005451 bool is_global = var != NULL;
5452 ASSERT(!is_global || var->is_global());
5453 cgen_->EmitNamedLoad(GetName(), is_global);
5454 cgen_->frame()->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005455 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005456 }
5457
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005458 case KEYED: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005459 ASSERT(property != NULL);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005460 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005461 cgen_->frame()->EmitPush(r0);
5462 break;
5463 }
5464
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005465 default:
5466 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005467 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005468
5469 if (!persist_after_get_) {
5470 cgen_->UnloadReference(this);
5471 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005472}
5473
5474
ager@chromium.org7c537e22008-10-16 08:43:32 +00005475void Reference::SetValue(InitState init_state) {
5476 ASSERT(!is_illegal());
5477 ASSERT(!cgen_->has_cc());
5478 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005479 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005480 Property* property = expression_->AsProperty();
5481 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005482 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005483 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005484
ager@chromium.org7c537e22008-10-16 08:43:32 +00005485 switch (type_) {
5486 case SLOT: {
5487 Comment cmnt(masm, "[ Store to Slot");
5488 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005489 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005490 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005491 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005492 }
5493
ager@chromium.org7c537e22008-10-16 08:43:32 +00005494 case NAMED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005495 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005496 Comment cmnt(masm, "[ Store to named Property");
5497 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005498 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005499 Handle<String> name(GetName());
5500
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005501 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005502 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005503 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005504 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005505 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005506 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005507 break;
5508 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005509
ager@chromium.org7c537e22008-10-16 08:43:32 +00005510 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005511 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005512 Comment cmnt(masm, "[ Store to keyed Property");
5513 Property* property = expression_->AsProperty();
5514 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005515 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005516
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005517 frame->EmitPop(r0); // Value.
5518 cgen_->EmitKeyedStore(property->key()->type());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005519 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005520 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005521 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005522 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005523
5524 default:
5525 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005526 }
5527}
5528
5529
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005530void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005531 // Create a new closure from the given function info in new
5532 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005533 Label gc;
5534
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005535 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005536 __ pop(r3);
5537
5538 // Attempt to allocate new JSFunction in new space.
5539 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
5540 r0,
5541 r1,
5542 r2,
5543 &gc,
5544 TAG_OBJECT);
5545
5546 // Compute the function map in the current global context and set that
5547 // as the map of the allocated object.
5548 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5549 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5550 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5551 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5552
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005553 // Initialize the rest of the function. We don't have to update the
5554 // write barrier because the allocated object is in new space.
5555 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5556 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5557 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5558 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5559 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5560 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5561 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5562 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005563
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005564 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005565 __ Ret();
5566
5567 // Create a new closure through the slower runtime call.
5568 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005569 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005570 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005571}
5572
5573
5574void FastNewContextStub::Generate(MacroAssembler* masm) {
5575 // Try to allocate the context in new space.
5576 Label gc;
5577 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5578
5579 // Attempt to allocate the context in new space.
5580 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
5581 r0,
5582 r1,
5583 r2,
5584 &gc,
5585 TAG_OBJECT);
5586
5587 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005588 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005589
5590 // Setup the object header.
5591 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5592 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5593 __ mov(r2, Operand(length));
5594 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5595
5596 // Setup the fixed slots.
5597 __ mov(r1, Operand(Smi::FromInt(0)));
5598 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5599 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5600 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5601 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5602
5603 // Copy the global object from the surrounding context.
5604 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5605 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5606
5607 // Initialize the rest of the slots to undefined.
5608 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5609 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5610 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5611 }
5612
5613 // Remove the on-stack argument and return.
5614 __ mov(cp, r0);
5615 __ pop();
5616 __ Ret();
5617
5618 // Need to collect. Call into runtime system.
5619 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005620 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005621}
5622
5623
ager@chromium.org5c838252010-02-19 08:53:10 +00005624void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5625 // Stack layout on entry:
5626 //
5627 // [sp]: constant elements.
5628 // [sp + kPointerSize]: literal index.
5629 // [sp + (2 * kPointerSize)]: literals array.
5630
5631 // All sizes here are multiples of kPointerSize.
5632 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5633 int size = JSArray::kSize + elements_size;
5634
5635 // Load boilerplate object into r3 and check if we need to create a
5636 // boilerplate.
5637 Label slow_case;
5638 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5639 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5640 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5641 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5642 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5643 __ cmp(r3, ip);
5644 __ b(eq, &slow_case);
5645
5646 // Allocate both the JS array and the elements array in one big
5647 // allocation. This avoids multiple limit checks.
5648 __ AllocateInNewSpace(size / kPointerSize,
5649 r0,
5650 r1,
5651 r2,
5652 &slow_case,
5653 TAG_OBJECT);
5654
5655 // Copy the JS array part.
5656 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5657 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5658 __ ldr(r1, FieldMemOperand(r3, i));
5659 __ str(r1, FieldMemOperand(r0, i));
5660 }
5661 }
5662
5663 if (length_ > 0) {
5664 // Get hold of the elements array of the boilerplate and setup the
5665 // elements pointer in the resulting object.
5666 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5667 __ add(r2, r0, Operand(JSArray::kSize));
5668 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
5669
5670 // Copy the elements array.
5671 for (int i = 0; i < elements_size; i += kPointerSize) {
5672 __ ldr(r1, FieldMemOperand(r3, i));
5673 __ str(r1, FieldMemOperand(r2, i));
5674 }
5675 }
5676
5677 // Return and remove the on-stack parameters.
5678 __ add(sp, sp, Operand(3 * kPointerSize));
5679 __ Ret();
5680
5681 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005682 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005683}
5684
5685
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005686// Takes a Smi and converts to an IEEE 64 bit floating point value in two
5687// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
5688// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
5689// scratch register. Destroys the source register. No GC occurs during this
5690// stub so you don't have to set up the frame.
5691class ConvertToDoubleStub : public CodeStub {
5692 public:
5693 ConvertToDoubleStub(Register result_reg_1,
5694 Register result_reg_2,
5695 Register source_reg,
5696 Register scratch_reg)
5697 : result1_(result_reg_1),
5698 result2_(result_reg_2),
5699 source_(source_reg),
5700 zeros_(scratch_reg) { }
5701
5702 private:
5703 Register result1_;
5704 Register result2_;
5705 Register source_;
5706 Register zeros_;
5707
5708 // Minor key encoding in 16 bits.
5709 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
5710 class OpBits: public BitField<Token::Value, 2, 14> {};
5711
5712 Major MajorKey() { return ConvertToDouble; }
5713 int MinorKey() {
5714 // Encode the parameters in a unique 16 bit value.
5715 return result1_.code() +
5716 (result2_.code() << 4) +
5717 (source_.code() << 8) +
5718 (zeros_.code() << 12);
5719 }
5720
5721 void Generate(MacroAssembler* masm);
5722
5723 const char* GetName() { return "ConvertToDoubleStub"; }
5724
5725#ifdef DEBUG
5726 void Print() { PrintF("ConvertToDoubleStub\n"); }
5727#endif
5728};
5729
5730
5731void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
5732#ifndef BIG_ENDIAN_FLOATING_POINT
5733 Register exponent = result1_;
5734 Register mantissa = result2_;
5735#else
5736 Register exponent = result2_;
5737 Register mantissa = result1_;
5738#endif
5739 Label not_special;
5740 // Convert from Smi to integer.
5741 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
5742 // Move sign bit from source to destination. This works because the sign bit
5743 // in the exponent word of the double has the same position and polarity as
5744 // the 2's complement sign bit in a Smi.
5745 ASSERT(HeapNumber::kSignMask == 0x80000000u);
5746 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
5747 // Subtract from 0 if source was negative.
5748 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005749
5750 // We have -1, 0 or 1, which we treat specially. Register source_ contains
5751 // absolute value: it is either equal to 1 (special case of -1 and 1),
5752 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005753 __ cmp(source_, Operand(1));
5754 __ b(gt, &not_special);
5755
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005756 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
5757 static const uint32_t exponent_word_for_1 =
5758 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005759 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005760 // 1, 0 and -1 all have 0 for the second word.
5761 __ mov(mantissa, Operand(0));
5762 __ Ret();
5763
5764 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005765 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005766 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005767 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005768 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005769 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005770 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
5771 __ orr(exponent,
5772 exponent,
5773 Operand(mantissa, LSL, HeapNumber::kExponentShift));
5774 // Shift up the source chopping the top bit off.
5775 __ add(zeros_, zeros_, Operand(1));
5776 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
5777 __ mov(source_, Operand(source_, LSL, zeros_));
5778 // Compute lower part of fraction (last 12 bits).
5779 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
5780 // And the top (top 20 bits).
5781 __ orr(exponent,
5782 exponent,
5783 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
5784 __ Ret();
5785}
5786
5787
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005788// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005789void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005790 Label max_negative_int;
5791 // the_int_ has the answer which is a signed int32 but not a Smi.
5792 // We test for the special value that has a different exponent. This test
5793 // has the neat side effect of setting the flags according to the sign.
5794 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005795 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005796 __ b(eq, &max_negative_int);
5797 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
5798 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
5799 uint32_t non_smi_exponent =
5800 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5801 __ mov(scratch_, Operand(non_smi_exponent));
5802 // Set the sign bit in scratch_ if the value was negative.
5803 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
5804 // Subtract from 0 if the value was negative.
5805 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
5806 // We should be masking the implict first digit of the mantissa away here,
5807 // but it just ends up combining harmlessly with the last digit of the
5808 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
5809 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
5810 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
5811 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5812 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
5813 __ str(scratch_, FieldMemOperand(the_heap_number_,
5814 HeapNumber::kExponentOffset));
5815 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
5816 __ str(scratch_, FieldMemOperand(the_heap_number_,
5817 HeapNumber::kMantissaOffset));
5818 __ Ret();
5819
5820 __ bind(&max_negative_int);
5821 // The max negative int32 is stored as a positive number in the mantissa of
5822 // a double because it uses a sign bit instead of using two's complement.
5823 // The actual mantissa bits stored are all 0 because the implicit most
5824 // significant 1 bit is not stored.
5825 non_smi_exponent += 1 << HeapNumber::kExponentShift;
5826 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
5827 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
5828 __ mov(ip, Operand(0));
5829 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
5830 __ Ret();
5831}
5832
5833
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005834// Handle the case where the lhs and rhs are the same object.
5835// Equality is almost reflexive (everything but NaN), so this is a test
5836// for "identity and not NaN".
5837static void EmitIdenticalObjectComparison(MacroAssembler* masm,
5838 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005839 Condition cc,
5840 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005841 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005842 Label heap_number, return_equal;
5843 Register exp_mask_reg = r5;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005844 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005845 __ b(ne, &not_identical);
5846
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005847 // The two objects are identical. If we know that one of them isn't NaN then
5848 // we now know they test equal.
5849 if (cc != eq || !never_nan_nan) {
5850 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005851
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005852 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
5853 // so we do the second best thing - test it ourselves.
5854 // They are both equal and they are not both Smis so both of them are not
5855 // Smis. If it's not a heap number, then return equal.
5856 if (cc == lt || cc == gt) {
5857 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005858 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005859 } else {
5860 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5861 __ b(eq, &heap_number);
5862 // Comparing JS objects with <=, >= is complicated.
5863 if (cc != eq) {
5864 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
5865 __ b(ge, slow);
5866 // Normally here we fall through to return_equal, but undefined is
5867 // special: (undefined == undefined) == true, but
5868 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
5869 if (cc == le || cc == ge) {
5870 __ cmp(r4, Operand(ODDBALL_TYPE));
5871 __ b(ne, &return_equal);
5872 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005873 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005874 __ b(ne, &return_equal);
5875 if (cc == le) {
5876 // undefined <= undefined should fail.
5877 __ mov(r0, Operand(GREATER));
5878 } else {
5879 // undefined >= undefined should fail.
5880 __ mov(r0, Operand(LESS));
5881 }
5882 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005883 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005884 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005885 }
5886 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005887
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005888 __ bind(&return_equal);
5889 if (cc == lt) {
5890 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
5891 } else if (cc == gt) {
5892 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
5893 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005894 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005895 }
5896 __ mov(pc, Operand(lr)); // Return.
5897
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005898 if (cc != eq || !never_nan_nan) {
5899 // For less and greater we don't have to check for NaN since the result of
5900 // x < x is false regardless. For the others here is some code to check
5901 // for NaN.
5902 if (cc != lt && cc != gt) {
5903 __ bind(&heap_number);
5904 // It is a heap number, so return non-equal if it's NaN and equal if it's
5905 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005906
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005907 // The representation of NaN values has all exponent bits (52..62) set,
5908 // and not all mantissa bits (0..51) clear.
5909 // Read top bits of double representation (second word of value).
5910 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5911 // Test that exponent bits are all set.
5912 __ and_(r3, r2, Operand(exp_mask_reg));
5913 __ cmp(r3, Operand(exp_mask_reg));
5914 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005915
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005916 // Shift out flag and all exponent bits, retaining only mantissa.
5917 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
5918 // Or with all low-bits of mantissa.
5919 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5920 __ orr(r0, r3, Operand(r2), SetCC);
5921 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005922 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
5923 // not (it's a NaN). For <= and >= we need to load r0 with the failing
5924 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005925 if (cc != eq) {
5926 // All-zero means Infinity means equal.
5927 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
5928 if (cc == le) {
5929 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
5930 } else {
5931 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
5932 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005933 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005934 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005935 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005936 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005937 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005938
5939 __ bind(&not_identical);
5940}
5941
5942
5943// See comment at call site.
5944static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005945 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005946 Label* slow,
5947 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005948 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005949 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005950 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005951
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005952 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005953 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5954 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005955 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005956 // succeed. Return non-equal (r0 is already not zero)
5957 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5958 } else {
5959 // Smi compared non-strictly with a non-Smi non-heap-number. Call
5960 // the runtime.
5961 __ b(ne, slow);
5962 }
5963
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005964 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005965 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005966 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005967 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005968 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5969 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005970 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005971 // Load the double from rhs, tagged HeapNumber r0, to d6.
5972 __ sub(r7, r0, Operand(kHeapObjectTag));
5973 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005974 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005975 __ push(lr);
5976 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005977 __ mov(r7, Operand(r1));
5978 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5979 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005980 // Load rhs to a double in r0, r1.
5981 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5982 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5983 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005984 }
5985
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005986 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005987 // since it's a smi.
5988 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005989
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005990 __ bind(&rhs_is_smi);
5991 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005992 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5993 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005994 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005995 // succeed. Return non-equal.
5996 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
5997 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5998 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005999 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006000 // the runtime.
6001 __ b(ne, slow);
6002 }
6003
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006004 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006005 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006006 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006007 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006008 // Load the double from lhs, tagged HeapNumber r1, to d7.
6009 __ sub(r7, r1, Operand(kHeapObjectTag));
6010 __ vldr(d7, r7, HeapNumber::kValueOffset);
6011 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6012 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006013 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006014 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006015 __ push(lr);
6016 // Load lhs to a double in r2, r3.
6017 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6018 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6019 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006020 __ mov(r7, Operand(r0));
6021 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6022 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006023 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006024 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006025 // Fall through to both_loaded_as_doubles.
6026}
6027
6028
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006029void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006030 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006031 Register rhs_exponent = exp_first ? r0 : r1;
6032 Register lhs_exponent = exp_first ? r2 : r3;
6033 Register rhs_mantissa = exp_first ? r1 : r0;
6034 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006035 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006036 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006037
6038 Register exp_mask_reg = r5;
6039
6040 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006041 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
6042 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006043 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006044 __ mov(r4,
6045 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6046 SetCC);
6047 __ b(ne, &one_is_nan);
6048 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006049 __ b(ne, &one_is_nan);
6050
6051 __ bind(lhs_not_nan);
6052 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
6053 __ bind(&lhs_not_nan_exp_mask_is_loaded);
6054 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
6055 __ cmp(r4, Operand(exp_mask_reg));
6056 __ b(ne, &neither_is_nan);
6057 __ mov(r4,
6058 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6059 SetCC);
6060 __ b(ne, &one_is_nan);
6061 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006062 __ b(eq, &neither_is_nan);
6063
6064 __ bind(&one_is_nan);
6065 // NaN comparisons always fail.
6066 // Load whatever we need in r0 to make the comparison fail.
6067 if (cc == lt || cc == le) {
6068 __ mov(r0, Operand(GREATER));
6069 } else {
6070 __ mov(r0, Operand(LESS));
6071 }
6072 __ mov(pc, Operand(lr)); // Return.
6073
6074 __ bind(&neither_is_nan);
6075}
6076
6077
6078// See comment at call site.
6079static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
6080 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006081 Register rhs_exponent = exp_first ? r0 : r1;
6082 Register lhs_exponent = exp_first ? r2 : r3;
6083 Register rhs_mantissa = exp_first ? r1 : r0;
6084 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006085
6086 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
6087 if (cc == eq) {
6088 // Doubles are not equal unless they have the same bit pattern.
6089 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006090 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
6091 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006092 // Return non-zero if the numbers are unequal.
6093 __ mov(pc, Operand(lr), LeaveCC, ne);
6094
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006095 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006096 // If exponents are equal then return 0.
6097 __ mov(pc, Operand(lr), LeaveCC, eq);
6098
6099 // Exponents are unequal. The only way we can return that the numbers
6100 // are equal is if one is -0 and the other is 0. We already dealt
6101 // with the case where both are -0 or both are 0.
6102 // We start by seeing if the mantissas (that are equal) or the bottom
6103 // 31 bits of the rhs exponent are non-zero. If so we return not
6104 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006105 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006106 __ mov(r0, Operand(r4), LeaveCC, ne);
6107 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
6108 // Now they are equal if and only if the lhs exponent is zero in its
6109 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006110 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006111 __ mov(pc, Operand(lr));
6112 } else {
6113 // Call a native function to do a comparison between two non-NaNs.
6114 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006115 __ push(lr);
6116 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
6117 __ CallCFunction(ExternalReference::compare_doubles(), 4);
6118 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006119 }
6120}
6121
6122
6123// See comment at call site.
6124static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
6125 // If either operand is a JSObject or an oddball value, then they are
6126 // not equal since their pointers are different.
6127 // There is no test for undetectability in strict equality.
6128 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
6129 Label first_non_object;
6130 // Get the type of the first operand into r2 and compare it with
6131 // FIRST_JS_OBJECT_TYPE.
6132 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
6133 __ b(lt, &first_non_object);
6134
6135 // Return non-zero (r0 is not zero)
6136 Label return_not_equal;
6137 __ bind(&return_not_equal);
6138 __ mov(pc, Operand(lr)); // Return.
6139
6140 __ bind(&first_non_object);
6141 // Check for oddballs: true, false, null, undefined.
6142 __ cmp(r2, Operand(ODDBALL_TYPE));
6143 __ b(eq, &return_not_equal);
6144
6145 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
6146 __ b(ge, &return_not_equal);
6147
6148 // Check for oddballs: true, false, null, undefined.
6149 __ cmp(r3, Operand(ODDBALL_TYPE));
6150 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006151
6152 // Now that we have the types we might as well check for symbol-symbol.
6153 // Ensure that no non-strings have the symbol bit set.
6154 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6155 ASSERT(kSymbolTag != 0);
6156 __ and_(r2, r2, Operand(r3));
6157 __ tst(r2, Operand(kIsSymbolMask));
6158 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006159}
6160
6161
6162// See comment at call site.
6163static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
6164 Label* both_loaded_as_doubles,
6165 Label* not_heap_numbers,
6166 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006167 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006168 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006169 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
6170 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006171 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
6172
6173 // Both are heap numbers. Load them up then jump to the code we have
6174 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006175 if (CpuFeatures::IsSupported(VFP3)) {
6176 CpuFeatures::Scope scope(VFP3);
6177 __ sub(r7, r0, Operand(kHeapObjectTag));
6178 __ vldr(d6, r7, HeapNumber::kValueOffset);
6179 __ sub(r7, r1, Operand(kHeapObjectTag));
6180 __ vldr(d7, r7, HeapNumber::kValueOffset);
6181 } else {
6182 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6183 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6184 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
6185 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
6186 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006187 __ jmp(both_loaded_as_doubles);
6188}
6189
6190
6191// Fast negative check for symbol-to-symbol equality.
6192static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
6193 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006194 // Ensure that no non-strings have the symbol bit set.
6195 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6196 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006197 __ tst(r2, Operand(kIsSymbolMask));
6198 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006199 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
6200 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006201 __ tst(r3, Operand(kIsSymbolMask));
6202 __ b(eq, slow);
6203
6204 // Both are symbols. We already checked they weren't the same pointer
6205 // so they are not equal.
6206 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
6207 __ mov(pc, Operand(lr)); // Return.
6208}
6209
6210
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006211void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
6212 Register object,
6213 Register result,
6214 Register scratch1,
6215 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006216 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006217 bool object_is_smi,
6218 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006219 // Use of registers. Register result is used as a temporary.
6220 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006221 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006222
6223 // Load the number string cache.
6224 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
6225
6226 // Make the hash mask from the length of the number string cache. It
6227 // contains two elements (number and string) for each cache entry.
6228 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
6229 // Divide length by two (length is not a smi).
6230 __ mov(mask, Operand(mask, ASR, 1));
6231 __ sub(mask, mask, Operand(1)); // Make mask.
6232
6233 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006234 // number string cache for smis is just the smi value, and the hash for
6235 // doubles is the xor of the upper and lower words. See
6236 // Heap::GetNumberStringCache.
6237 Label is_smi;
6238 Label load_result_from_cache;
6239 if (!object_is_smi) {
6240 __ BranchOnSmi(object, &is_smi);
6241 if (CpuFeatures::IsSupported(VFP3)) {
6242 CpuFeatures::Scope scope(VFP3);
6243 __ CheckMap(object,
6244 scratch1,
6245 Factory::heap_number_map(),
6246 not_found,
6247 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006248
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006249 ASSERT_EQ(8, kDoubleSize);
6250 __ add(scratch1,
6251 object,
6252 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
6253 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
6254 __ eor(scratch1, scratch1, Operand(scratch2));
6255 __ and_(scratch1, scratch1, Operand(mask));
6256
6257 // Calculate address of entry in string cache: each entry consists
6258 // of two pointer sized fields.
6259 __ add(scratch1,
6260 number_string_cache,
6261 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
6262
6263 Register probe = mask;
6264 __ ldr(probe,
6265 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
6266 __ BranchOnSmi(probe, not_found);
6267 __ sub(scratch2, object, Operand(kHeapObjectTag));
6268 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
6269 __ sub(probe, probe, Operand(kHeapObjectTag));
6270 __ vldr(d1, probe, HeapNumber::kValueOffset);
6271 __ vcmp(d0, d1);
6272 __ vmrs(pc);
6273 __ b(ne, not_found); // The cache did not contain this value.
6274 __ b(&load_result_from_cache);
6275 } else {
6276 __ b(not_found);
6277 }
6278 }
6279
6280 __ bind(&is_smi);
6281 Register scratch = scratch1;
6282 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006283 // Calculate address of entry in string cache: each entry consists
6284 // of two pointer sized fields.
6285 __ add(scratch,
6286 number_string_cache,
6287 Operand(scratch, LSL, kPointerSizeLog2 + 1));
6288
6289 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006290 Register probe = mask;
6291 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
6292 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006293 __ b(ne, not_found);
6294
6295 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006296 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006297 __ ldr(result,
6298 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006299 __ IncrementCounter(&Counters::number_to_string_native,
6300 1,
6301 scratch1,
6302 scratch2);
6303}
6304
6305
6306void NumberToStringStub::Generate(MacroAssembler* masm) {
6307 Label runtime;
6308
6309 __ ldr(r1, MemOperand(sp, 0));
6310
6311 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006312 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006313 __ add(sp, sp, Operand(1 * kPointerSize));
6314 __ Ret();
6315
6316 __ bind(&runtime);
6317 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006318 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006319}
6320
6321
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006322// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
6323// On exit r0 is 0, positive or negative to indicate the result of
6324// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006325void CompareStub::Generate(MacroAssembler* masm) {
6326 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006327 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006328
6329 // NOTICE! This code is only reached after a smi-fast-case check, so
6330 // it is certain that at least one operand isn't a smi.
6331
6332 // Handle the case where the objects are identical. Either returns the answer
6333 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006334 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006335
6336 // If either is a Smi (we know that not both are), then they can only
6337 // be strictly equal if the other is a HeapNumber.
6338 ASSERT_EQ(0, kSmiTag);
6339 ASSERT_EQ(0, Smi::FromInt(0));
6340 __ and_(r2, r0, Operand(r1));
6341 __ tst(r2, Operand(kSmiTagMask));
6342 __ b(ne, &not_smis);
6343 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
6344 // 1) Return the answer.
6345 // 2) Go to slow.
6346 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006347 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006348 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006349 // comparison. If VFP3 is supported the double values of the numbers have
6350 // been loaded into d7 and d6. Otherwise, the double values have been loaded
6351 // into r0, r1, r2, and r3.
6352 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006353
6354 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006355 // The arguments have been converted to doubles and stored in d6 and d7, if
6356 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006357 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006358 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006359 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006360 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006361 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006362 __ vcmp(d7, d6);
6363 __ vmrs(pc); // Move vector status bits to normal status bits.
6364 Label nan;
6365 __ b(vs, &nan);
6366 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6367 __ mov(r0, Operand(LESS), LeaveCC, lt);
6368 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6369 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006370
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006371 __ bind(&nan);
6372 // If one of the sides was a NaN then the v flag is set. Load r0 with
6373 // whatever it takes to make the comparison fail, since comparisons with NaN
6374 // always fail.
6375 if (cc_ == lt || cc_ == le) {
6376 __ mov(r0, Operand(GREATER));
6377 } else {
6378 __ mov(r0, Operand(LESS));
6379 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006380 __ mov(pc, Operand(lr));
6381 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006382 // Checks for NaN in the doubles we have loaded. Can return the answer or
6383 // fall through if neither is a NaN. Also binds lhs_not_nan.
6384 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006385 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
6386 // answer. Never falls through.
6387 EmitTwoNonNanDoubleComparison(masm, cc_);
6388 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006389
6390 __ bind(&not_smis);
6391 // At this point we know we are dealing with two different objects,
6392 // and neither of them is a Smi. The objects are in r0 and r1.
6393 if (strict_) {
6394 // This returns non-equal for some object types, or falls through if it
6395 // was not lucky.
6396 EmitStrictTwoHeapObjectCompare(masm);
6397 }
6398
6399 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006400 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006401 // Check for heap-number-heap-number comparison. Can jump to slow case,
6402 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
6403 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006404 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006405 EmitCheckForTwoHeapNumbers(masm,
6406 &both_loaded_as_doubles,
6407 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006408 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006409
6410 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006411 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
6412 // symbols.
6413 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006414 // Either jumps to slow or returns the answer. Assumes that r2 is the type
6415 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006416 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006417 }
6418
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006419 // Check for both being sequential ASCII strings, and inline if that is the
6420 // case.
6421 __ bind(&flat_string_check);
6422
6423 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
6424
6425 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
6426 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
6427 r1,
6428 r0,
6429 r2,
6430 r3,
6431 r4,
6432 r5);
6433 // Never falls through to here.
6434
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006435 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006436
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006437 __ Push(r1, r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006438 // Figure out which native to call and setup the arguments.
6439 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006440 if (cc_ == eq) {
6441 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
6442 } else {
6443 native = Builtins::COMPARE;
6444 int ncr; // NaN compare result
6445 if (cc_ == lt || cc_ == le) {
6446 ncr = GREATER;
6447 } else {
6448 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
6449 ncr = LESS;
6450 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006451 __ mov(r0, Operand(Smi::FromInt(ncr)));
6452 __ push(r0);
6453 }
6454
6455 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
6456 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006457 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006458}
6459
6460
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00006461// We fall into this code if the operands were Smis, but the result was
6462// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006463// the operands were not both Smi. The operands are in r0 and r1. In order
6464// to call the C-implemented binary fp operation routines we need to end up
6465// with the double precision floating point operands in r0 and r1 (for the
6466// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00006467void GenericBinaryOpStub::HandleBinaryOpSlowCases(
6468 MacroAssembler* masm,
6469 Label* not_smi,
6470 Register lhs,
6471 Register rhs,
6472 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006473 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00006474 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006475
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006476 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ager@chromium.org357bf652010-04-12 11:30:10 +00006477
6478 if (ShouldGenerateSmiCode()) {
6479 // Smi-smi case (overflow).
6480 // Since both are Smis there is no heap number to overwrite, so allocate.
6481 // The new heap number is in r5. r6 and r7 are scratch.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006482 __ AllocateHeapNumber(r5, r6, r7, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006483
6484 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
6485 // using registers d7 and d6 for the double values.
6486 if (use_fp_registers) {
6487 CpuFeatures::Scope scope(VFP3);
6488 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
6489 __ vmov(s15, r7);
6490 __ vcvt_f64_s32(d7, s15);
6491 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
6492 __ vmov(s13, r7);
6493 __ vcvt_f64_s32(d6, s13);
6494 } else {
6495 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
6496 __ mov(r7, Operand(rhs));
6497 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6498 __ push(lr);
6499 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
6500 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
6501 __ mov(r7, Operand(lhs));
6502 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6503 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
6504 __ pop(lr);
6505 }
6506 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006507 }
6508
ager@chromium.org357bf652010-04-12 11:30:10 +00006509 // We branch here if at least one of r0 and r1 is not a Smi.
6510 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006511
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006512 // After this point we have the left hand side in r1 and the right hand side
6513 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006514 if (lhs.is(r0)) {
6515 __ Swap(r0, r1, ip);
6516 }
6517
6518 if (ShouldGenerateFPCode()) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006519 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
6520
ager@chromium.org357bf652010-04-12 11:30:10 +00006521 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
6522 switch (op_) {
6523 case Token::ADD:
6524 case Token::SUB:
6525 case Token::MUL:
6526 case Token::DIV:
6527 GenerateTypeTransition(masm);
6528 break;
6529
6530 default:
6531 break;
6532 }
6533 }
6534
6535 if (mode_ == NO_OVERWRITE) {
6536 // In the case where there is no chance of an overwritable float we may as
6537 // well do the allocation immediately while r0 and r1 are untouched.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006538 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006539 }
6540
6541 // Move r0 to a double in r2-r3.
6542 __ tst(r0, Operand(kSmiTagMask));
6543 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
6544 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6545 __ b(ne, &slow);
6546 if (mode_ == OVERWRITE_RIGHT) {
6547 __ mov(r5, Operand(r0)); // Overwrite this heap number.
6548 }
6549 if (use_fp_registers) {
6550 CpuFeatures::Scope scope(VFP3);
6551 // Load the double from tagged HeapNumber r0 to d7.
6552 __ sub(r7, r0, Operand(kHeapObjectTag));
6553 __ vldr(d7, r7, HeapNumber::kValueOffset);
6554 } else {
6555 // Calling convention says that second double is in r2 and r3.
6556 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
6557 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
6558 }
6559 __ jmp(&finished_loading_r0);
6560 __ bind(&r0_is_smi);
6561 if (mode_ == OVERWRITE_RIGHT) {
6562 // We can't overwrite a Smi so get address of new heap number into r5.
6563 __ AllocateHeapNumber(r5, r6, r7, &slow);
6564 }
6565
6566 if (use_fp_registers) {
6567 CpuFeatures::Scope scope(VFP3);
6568 // Convert smi in r0 to double in d7.
6569 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6570 __ vmov(s15, r7);
6571 __ vcvt_f64_s32(d7, s15);
6572 } else {
6573 // Write Smi from r0 to r3 and r2 in double format.
6574 __ mov(r7, Operand(r0));
6575 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6576 __ push(lr);
6577 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6578 __ pop(lr);
6579 }
6580
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006581 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
6582 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
6583 Label r1_is_not_smi;
6584 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
6585 __ tst(r1, Operand(kSmiTagMask));
6586 __ b(ne, &r1_is_not_smi);
6587 GenerateTypeTransition(masm);
6588 __ jmp(&r1_is_smi);
6589 }
6590
ager@chromium.org357bf652010-04-12 11:30:10 +00006591 __ bind(&finished_loading_r0);
6592
6593 // Move r1 to a double in r0-r1.
6594 __ tst(r1, Operand(kSmiTagMask));
6595 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006596 __ bind(&r1_is_not_smi);
ager@chromium.org357bf652010-04-12 11:30:10 +00006597 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6598 __ b(ne, &slow);
6599 if (mode_ == OVERWRITE_LEFT) {
6600 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6601 }
6602 if (use_fp_registers) {
6603 CpuFeatures::Scope scope(VFP3);
6604 // Load the double from tagged HeapNumber r1 to d6.
6605 __ sub(r7, r1, Operand(kHeapObjectTag));
6606 __ vldr(d6, r7, HeapNumber::kValueOffset);
6607 } else {
6608 // Calling convention says that first double is in r0 and r1.
6609 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
6610 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
6611 }
6612 __ jmp(&finished_loading_r1);
6613 __ bind(&r1_is_smi);
6614 if (mode_ == OVERWRITE_LEFT) {
6615 // We can't overwrite a Smi so get address of new heap number into r5.
6616 __ AllocateHeapNumber(r5, r6, r7, &slow);
6617 }
6618
6619 if (use_fp_registers) {
6620 CpuFeatures::Scope scope(VFP3);
6621 // Convert smi in r1 to double in d6.
6622 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6623 __ vmov(s13, r7);
6624 __ vcvt_f64_s32(d6, s13);
6625 } else {
6626 // Write Smi from r1 to r1 and r0 in double format.
6627 __ mov(r7, Operand(r1));
6628 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6629 __ push(lr);
6630 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6631 __ pop(lr);
6632 }
6633
6634 __ bind(&finished_loading_r1);
6635
6636 __ bind(&do_the_call);
6637 // If we are inlining the operation using VFP3 instructions for
6638 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6639 if (use_fp_registers) {
6640 CpuFeatures::Scope scope(VFP3);
6641 // ARMv7 VFP3 instructions to implement
6642 // double precision, add, subtract, multiply, divide.
6643
6644 if (Token::MUL == op_) {
6645 __ vmul(d5, d6, d7);
6646 } else if (Token::DIV == op_) {
6647 __ vdiv(d5, d6, d7);
6648 } else if (Token::ADD == op_) {
6649 __ vadd(d5, d6, d7);
6650 } else if (Token::SUB == op_) {
6651 __ vsub(d5, d6, d7);
6652 } else {
6653 UNREACHABLE();
6654 }
6655 __ sub(r0, r5, Operand(kHeapObjectTag));
6656 __ vstr(d5, r0, HeapNumber::kValueOffset);
6657 __ add(r0, r0, Operand(kHeapObjectTag));
6658 __ mov(pc, lr);
6659 } else {
6660 // If we did not inline the operation, then the arguments are in:
6661 // r0: Left value (least significant part of mantissa).
6662 // r1: Left value (sign, exponent, top of mantissa).
6663 // r2: Right value (least significant part of mantissa).
6664 // r3: Right value (sign, exponent, top of mantissa).
6665 // r5: Address of heap number for result.
6666
6667 __ push(lr); // For later.
6668 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
6669 // Call C routine that may not cause GC or other trouble. r5 is callee
6670 // save.
6671 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
6672 // Store answer in the overwritable heap number.
6673 #if !defined(USE_ARM_EABI)
6674 // Double returned in fp coprocessor register 0 and 1, encoded as register
6675 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
6676 // substract the tag from r5.
6677 __ sub(r4, r5, Operand(kHeapObjectTag));
6678 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
6679 #else
6680 // Double returned in registers 0 and 1.
6681 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
6682 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
6683 #endif
6684 __ mov(r0, Operand(r5));
6685 // And we are done.
6686 __ pop(pc);
6687 }
6688 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006689
6690
6691 if (lhs.is(r0)) {
6692 __ b(&slow);
6693 __ bind(&slow_reverse);
6694 __ Swap(r0, r1, ip);
6695 }
6696
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006697 // We jump to here if something goes wrong (one param is not a number of any
6698 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006699 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006700
6701 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006702 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006703
ager@chromium.org357bf652010-04-12 11:30:10 +00006704 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006705 // Test for string arguments before calling runtime.
6706 // r1 : first argument
6707 // r0 : second argument
6708 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00006709 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006710
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006711 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006712 __ tst(r1, Operand(kSmiTagMask));
6713 __ b(eq, &not_string1);
6714 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
6715 __ b(ge, &not_string1);
6716
6717 // First argument is a a string, test second.
6718 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006719 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006720 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6721 __ b(ge, &string1);
6722
6723 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006724 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
6725 __ TailCallStub(&string_add_stub);
6726
6727 __ bind(&string1_smi2);
6728 // First argument is a string, second is a smi. Try to lookup the number
6729 // string for the smi in the number string cache.
6730 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006731 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006732
6733 // Replace second argument on stack and tailcall string add stub to make
6734 // the result.
6735 __ str(r2, MemOperand(sp, 0));
6736 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006737
6738 // Only first argument is a string.
6739 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006740 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
6741
6742 // First argument was not a string, test second.
6743 __ bind(&not_string1);
6744 __ tst(r0, Operand(kSmiTagMask));
6745 __ b(eq, &not_strings);
6746 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6747 __ b(ge, &not_strings);
6748
6749 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006750 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
6751
6752 __ bind(&not_strings);
6753 }
6754
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006755 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006756}
6757
6758
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006759// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006760// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006761// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
6762// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006763// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
6764// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006765static void GetInt32(MacroAssembler* masm,
6766 Register source,
6767 Register dest,
6768 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006769 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006770 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006771 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006772 // Get exponent word.
6773 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
6774 // Get exponent alone in scratch2.
6775 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006776 // Load dest with zero. We use this either for the final shift or
6777 // for the answer.
6778 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006779 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006780 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
6781 // the exponent that we are fastest at and also the highest exponent we can
6782 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006783 const uint32_t non_smi_exponent =
6784 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6785 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006786 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
6787 __ b(eq, &right_exponent);
6788 // If the exponent is higher than that then go to slow case. This catches
6789 // numbers that don't fit in a signed int32, infinities and NaNs.
6790 __ b(gt, slow);
6791
6792 // We know the exponent is smaller than 30 (biased). If it is less than
6793 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
6794 // it rounds to zero.
6795 const uint32_t zero_exponent =
6796 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
6797 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
6798 // Dest already has a Smi zero.
6799 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006800 if (!CpuFeatures::IsSupported(VFP3)) {
6801 // We have a shifted exponent between 0 and 30 in scratch2.
6802 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
6803 // We now have the exponent in dest. Subtract from 30 to get
6804 // how much to shift down.
6805 __ rsb(dest, dest, Operand(30));
6806 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006807 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006808 if (CpuFeatures::IsSupported(VFP3)) {
6809 CpuFeatures::Scope scope(VFP3);
6810 // ARMv7 VFP3 instructions implementing double precision to integer
6811 // conversion using round to zero.
6812 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006813 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006814 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006815 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006816 } else {
6817 // Get the top bits of the mantissa.
6818 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
6819 // Put back the implicit 1.
6820 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
6821 // Shift up the mantissa bits to take up the space the exponent used to
6822 // take. We just orred in the implicit bit so that took care of one and
6823 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
6824 // distance.
6825 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6826 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
6827 // Put sign in zero flag.
6828 __ tst(scratch, Operand(HeapNumber::kSignMask));
6829 // Get the second half of the double. For some exponents we don't
6830 // actually need this because the bits get shifted out again, but
6831 // it's probably slower to test than just to do it.
6832 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
6833 // Shift down 22 bits to get the last 10 bits.
6834 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
6835 // Move down according to the exponent.
6836 __ mov(dest, Operand(scratch, LSR, dest));
6837 // Fix sign if sign bit was set.
6838 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
6839 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006840 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006841}
6842
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006843// For bitwise ops where the inputs are not both Smis we here try to determine
6844// whether both inputs are either Smis or at least heap numbers that can be
6845// represented by a 32 bit signed value. We truncate towards zero as required
6846// by the ES spec. If this is the case we do the bitwise op and see if the
6847// result is a Smi. If so, great, otherwise we try to find a heap number to
6848// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00006849// On entry the operands are in lhs and rhs. On exit the answer is in r0.
6850void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
6851 Register lhs,
6852 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006853 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00006854 Label rhs_is_smi, lhs_is_smi;
6855 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006856
ager@chromium.org357bf652010-04-12 11:30:10 +00006857 __ tst(lhs, Operand(kSmiTagMask));
6858 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
6859 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006860 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006861 GetInt32(masm, lhs, r3, r5, r4, &slow);
6862 __ jmp(&done_checking_lhs);
6863 __ bind(&lhs_is_smi);
6864 __ mov(r3, Operand(lhs, ASR, 1));
6865 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006866
ager@chromium.org357bf652010-04-12 11:30:10 +00006867 __ tst(rhs, Operand(kSmiTagMask));
6868 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
6869 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006870 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006871 GetInt32(masm, rhs, r2, r5, r4, &slow);
6872 __ jmp(&done_checking_rhs);
6873 __ bind(&rhs_is_smi);
6874 __ mov(r2, Operand(rhs, ASR, 1));
6875 __ bind(&done_checking_rhs);
6876
6877 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006878
6879 // r0 and r1: Original operands (Smi or heap numbers).
6880 // r2 and r3: Signed int32 operands.
6881 switch (op_) {
6882 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
6883 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
6884 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
6885 case Token::SAR:
6886 // Use only the 5 least significant bits of the shift count.
6887 __ and_(r2, r2, Operand(0x1f));
6888 __ mov(r2, Operand(r3, ASR, r2));
6889 break;
6890 case Token::SHR:
6891 // Use only the 5 least significant bits of the shift count.
6892 __ and_(r2, r2, Operand(0x1f));
6893 __ mov(r2, Operand(r3, LSR, r2), SetCC);
6894 // SHR is special because it is required to produce a positive answer.
6895 // The code below for writing into heap numbers isn't capable of writing
6896 // the register as an unsigned int so we go to slow case if we hit this
6897 // case.
6898 __ b(mi, &slow);
6899 break;
6900 case Token::SHL:
6901 // Use only the 5 least significant bits of the shift count.
6902 __ and_(r2, r2, Operand(0x1f));
6903 __ mov(r2, Operand(r3, LSL, r2));
6904 break;
6905 default: UNREACHABLE();
6906 }
6907 // check that the *signed* result fits in a smi
6908 __ add(r3, r2, Operand(0x40000000), SetCC);
6909 __ b(mi, &result_not_a_smi);
6910 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6911 __ Ret();
6912
6913 Label have_to_allocate, got_a_heap_number;
6914 __ bind(&result_not_a_smi);
6915 switch (mode_) {
6916 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006917 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006918 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006919 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006920 break;
6921 }
6922 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006923 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006924 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006925 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006926 break;
6927 }
6928 case NO_OVERWRITE: {
6929 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006930 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006931 }
6932 default: break;
6933 }
6934 __ bind(&got_a_heap_number);
6935 // r2: Answer as signed int32.
6936 // r5: Heap number to write answer into.
6937
6938 // Nothing can go wrong now, so move the heap number to r0, which is the
6939 // result.
6940 __ mov(r0, Operand(r5));
6941
6942 // Tail call that writes the int32 in r2 to the heap number in r0, using
6943 // r3 as scratch. r0 is preserved and returned.
6944 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
6945 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6946
6947 if (mode_ != NO_OVERWRITE) {
6948 __ bind(&have_to_allocate);
6949 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006950 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006951 __ jmp(&got_a_heap_number);
6952 }
6953
6954 // If all else failed then we go to the runtime system.
6955 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006956 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006957 switch (op_) {
6958 case Token::BIT_OR:
6959 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
6960 break;
6961 case Token::BIT_AND:
6962 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
6963 break;
6964 case Token::BIT_XOR:
6965 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
6966 break;
6967 case Token::SAR:
6968 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
6969 break;
6970 case Token::SHR:
6971 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
6972 break;
6973 case Token::SHL:
6974 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
6975 break;
6976 default:
6977 UNREACHABLE();
6978 }
6979}
6980
6981
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006982// Can we multiply by x with max two shifts and an add.
6983// This answers yes to all integers from 2 to 10.
6984static bool IsEasyToMultiplyBy(int x) {
6985 if (x < 2) return false; // Avoid special cases.
6986 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
6987 if (IsPowerOf2(x)) return true; // Simple shift.
6988 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
6989 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
6990 return false;
6991}
6992
6993
6994// Can multiply by anything that IsEasyToMultiplyBy returns true for.
6995// Source and destination may be the same register. This routine does
6996// not set carry and overflow the way a mul instruction would.
6997static void MultiplyByKnownInt(MacroAssembler* masm,
6998 Register source,
6999 Register destination,
7000 int known_int) {
7001 if (IsPowerOf2(known_int)) {
7002 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
7003 } else if (PopCountLessThanEqual2(known_int)) {
7004 int first_bit = BitPosition(known_int);
7005 int second_bit = BitPosition(known_int ^ (1 << first_bit));
7006 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
7007 if (first_bit != 0) {
7008 __ mov(destination, Operand(destination, LSL, first_bit));
7009 }
7010 } else {
7011 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
7012 int the_bit = BitPosition(known_int + 1);
7013 __ rsb(destination, source, Operand(source, LSL, the_bit));
7014 }
7015}
7016
7017
7018// This function (as opposed to MultiplyByKnownInt) takes the known int in a
7019// a register for the cases where it doesn't know a good trick, and may deliver
7020// a result that needs shifting.
7021static void MultiplyByKnownInt2(
7022 MacroAssembler* masm,
7023 Register result,
7024 Register source,
7025 Register known_int_register, // Smi tagged.
7026 int known_int,
7027 int* required_shift) { // Including Smi tag shift
7028 switch (known_int) {
7029 case 3:
7030 __ add(result, source, Operand(source, LSL, 1));
7031 *required_shift = 1;
7032 break;
7033 case 5:
7034 __ add(result, source, Operand(source, LSL, 2));
7035 *required_shift = 1;
7036 break;
7037 case 6:
7038 __ add(result, source, Operand(source, LSL, 1));
7039 *required_shift = 2;
7040 break;
7041 case 7:
7042 __ rsb(result, source, Operand(source, LSL, 3));
7043 *required_shift = 1;
7044 break;
7045 case 9:
7046 __ add(result, source, Operand(source, LSL, 3));
7047 *required_shift = 1;
7048 break;
7049 case 10:
7050 __ add(result, source, Operand(source, LSL, 2));
7051 *required_shift = 2;
7052 break;
7053 default:
7054 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
7055 __ mul(result, source, known_int_register);
7056 *required_shift = 0;
7057 }
7058}
7059
7060
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007061const char* GenericBinaryOpStub::GetName() {
7062 if (name_ != NULL) return name_;
7063 const int len = 100;
7064 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7065 if (name_ == NULL) return "OOM";
7066 const char* op_name = Token::Name(op_);
7067 const char* overwrite_name;
7068 switch (mode_) {
7069 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7070 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7071 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7072 default: overwrite_name = "UnknownOverwrite"; break;
7073 }
7074
7075 OS::SNPrintF(Vector<char>(name_, len),
7076 "GenericBinaryOpStub_%s_%s%s",
7077 op_name,
7078 overwrite_name,
7079 specialized_on_rhs_ ? "_ConstantRhs" : 0);
7080 return name_;
7081}
7082
7083
ager@chromium.org5c838252010-02-19 08:53:10 +00007084
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007085void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007086 // lhs_ : x
7087 // rhs_ : y
7088 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007089
ager@chromium.org357bf652010-04-12 11:30:10 +00007090 Register result = r0;
7091 Register lhs = lhs_;
7092 Register rhs = rhs_;
7093
7094 // This code can't cope with other register allocations yet.
7095 ASSERT(result.is(r0) &&
7096 ((lhs.is(r0) && rhs.is(r1)) ||
7097 (lhs.is(r1) && rhs.is(r0))));
7098
7099 Register smi_test_reg = VirtualFrame::scratch0();
7100 Register scratch = VirtualFrame::scratch1();
7101
7102 // All ops need to know whether we are dealing with two Smis. Set up
7103 // smi_test_reg to tell us that.
7104 if (ShouldGenerateSmiCode()) {
7105 __ orr(smi_test_reg, lhs, Operand(rhs));
7106 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007107
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007108 switch (op_) {
7109 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007110 Label not_smi;
7111 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007112 if (ShouldGenerateSmiCode()) {
7113 ASSERT(kSmiTag == 0); // Adjust code below.
7114 __ tst(smi_test_reg, Operand(kSmiTagMask));
7115 __ b(ne, &not_smi);
7116 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
7117 // Return if no overflow.
7118 __ Ret(vc);
7119 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
7120 }
7121 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007122 break;
7123 }
7124
7125 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007126 Label not_smi;
7127 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007128 if (ShouldGenerateSmiCode()) {
7129 ASSERT(kSmiTag == 0); // Adjust code below.
7130 __ tst(smi_test_reg, Operand(kSmiTagMask));
7131 __ b(ne, &not_smi);
7132 if (lhs.is(r1)) {
7133 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
7134 // Return if no overflow.
7135 __ Ret(vc);
7136 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
7137 } else {
7138 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
7139 // Return if no overflow.
7140 __ Ret(vc);
7141 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
7142 }
7143 }
7144 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007145 break;
7146 }
7147
7148 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007149 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00007150 if (ShouldGenerateSmiCode()) {
7151 ASSERT(kSmiTag == 0); // adjust code below
7152 __ tst(smi_test_reg, Operand(kSmiTagMask));
7153 Register scratch2 = smi_test_reg;
7154 smi_test_reg = no_reg;
7155 __ b(ne, &not_smi);
7156 // Remove tag from one operand (but keep sign), so that result is Smi.
7157 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
7158 // Do multiplication
7159 // scratch = lower 32 bits of ip * lhs.
7160 __ smull(scratch, scratch2, lhs, ip);
7161 // Go slow on overflows (overflow bit is not set).
7162 __ mov(ip, Operand(scratch, ASR, 31));
7163 // No overflow if higher 33 bits are identical.
7164 __ cmp(ip, Operand(scratch2));
7165 __ b(ne, &slow);
7166 // Go slow on zero result to handle -0.
7167 __ tst(scratch, Operand(scratch));
7168 __ mov(result, Operand(scratch), LeaveCC, ne);
7169 __ Ret(ne);
7170 // We need -0 if we were multiplying a negative number with 0 to get 0.
7171 // We know one of them was zero.
7172 __ add(scratch2, rhs, Operand(lhs), SetCC);
7173 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
7174 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
7175 // Slow case. We fall through here if we multiplied a negative number
7176 // with 0, because that would mean we should produce -0.
7177 __ bind(&slow);
7178 }
7179 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007180 break;
7181 }
7182
7183 case Token::DIV:
7184 case Token::MOD: {
7185 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007186 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007187 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00007188 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007189 if (IsPowerOf2(constant_rhs_)) {
7190 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007191 __ and_(rhs,
7192 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007193 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
7194 SetCC);
7195 // We now have the answer, but if the input was negative we also
7196 // have the sign bit. Our work is done if the result is
7197 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00007198 if (!rhs.is(r0)) {
7199 __ mov(r0, rhs, LeaveCC, pl);
7200 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007201 __ Ret(pl);
7202 // A mod of a negative left hand side must return a negative number.
7203 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00007204 // already optimistically trashed rhs so we may need to restore it.
7205 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007206 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007207 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007208 __ b(eq, &smi_is_unsuitable);
7209 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00007210 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007211 } else {
7212 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00007213 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007214 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
7215 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
7216 int shift = 0;
7217 int d = constant_rhs_;
7218 while ((d & 1) == 0) {
7219 d >>= 1;
7220 shift++;
7221 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007222 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007223 __ bic(r0, r0, Operand(kSmiTagMask));
7224 }
7225 } else {
7226 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00007227 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007228 __ b(ne, &smi_is_unsuitable);
7229 // Find a fixed point reciprocal of the divisor so we can divide by
7230 // multiplying.
7231 double divisor = 1.0 / constant_rhs_;
7232 int shift = 32;
7233 double scale = 4294967296.0; // 1 << 32.
7234 uint32_t mul;
7235 // Maximise the precision of the fixed point reciprocal.
7236 while (true) {
7237 mul = static_cast<uint32_t>(scale * divisor);
7238 if (mul >= 0x7fffffff) break;
7239 scale *= 2.0;
7240 shift++;
7241 }
7242 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00007243 Register scratch2 = smi_test_reg;
7244 smi_test_reg = no_reg;
7245 __ mov(scratch2, Operand(mul));
7246 __ umull(scratch, scratch2, scratch2, lhs);
7247 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
7248 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
7249 // rhs is still the known rhs. rhs is Smi tagged.
7250 // lhs is still the unkown lhs. lhs is Smi tagged.
7251 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
7252 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007253 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00007254 scratch,
7255 scratch2,
7256 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007257 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00007258 &required_scratch_shift);
7259 // scratch << required_scratch_shift is now the Smi tagged rhs *
7260 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007261 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007262 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007263 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00007264 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007265 } else {
7266 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00007267 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007268 }
7269 }
7270 __ Ret();
7271 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007272 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007273 HandleBinaryOpSlowCases(
7274 masm,
7275 &not_smi,
7276 lhs,
7277 rhs,
7278 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007279 break;
7280 }
7281
7282 case Token::BIT_OR:
7283 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007284 case Token::BIT_XOR:
7285 case Token::SAR:
7286 case Token::SHR:
7287 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007288 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007289 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00007290 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007291 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007292 Register scratch2 = smi_test_reg;
7293 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007294 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007295 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
7296 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
7297 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007298 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007299 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00007300 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7301 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007302 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007303 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007304 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007305 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007306 // Remove tags from operands. We can't do this on a 31 bit number
7307 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00007308 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7309 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7310 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007311 // Unsigned shift is not allowed to produce a negative number, so
7312 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00007313 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007314 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007315 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007316 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007317 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007318 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007319 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00007320 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7321 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7322 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007323 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00007324 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007325 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007326 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007327 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007328 default: UNREACHABLE();
7329 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007330 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007331 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007332 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007333 break;
7334 }
7335
7336 default: UNREACHABLE();
7337 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007338 // This code should be unreachable.
7339 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00007340
7341 // Generate an unreachable reference to the DEFAULT stub so that it can be
7342 // found at the end of this stub when clearing ICs at GC.
7343 // TODO(kaznacheev): Check performance impact and get rid of this.
7344 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
7345 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
7346 __ CallStub(&uninit);
7347 }
7348}
7349
7350
7351void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
7352 Label get_result;
7353
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007354 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00007355
7356 // Internal frame is necessary to handle exceptions properly.
7357 __ EnterInternalFrame();
7358 // Call the stub proper to get the result in r0.
7359 __ Call(&get_result);
7360 __ LeaveInternalFrame();
7361
7362 __ push(r0);
7363
7364 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
7365 __ push(r0);
7366 __ mov(r0, Operand(Smi::FromInt(op_)));
7367 __ push(r0);
7368 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
7369 __ push(r0);
7370
7371 __ TailCallExternalReference(
7372 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
7373 6,
7374 1);
7375
7376 // The entry point for the result calculation is assumed to be immediately
7377 // after this sequence.
7378 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007379}
7380
7381
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007382Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007383 GenericBinaryOpStub stub(key, type_info);
7384 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007385}
7386
7387
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007388void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00007389 // Do tail-call to runtime routine. Runtime routines expect at least one
7390 // argument, so give it a Smi.
7391 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007392 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007393 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007394
7395 __ StubReturn(1);
7396}
7397
7398
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007399void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007400 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007402 if (op_ == Token::SUB) {
7403 // Check whether the value is a smi.
7404 Label try_float;
7405 __ tst(r0, Operand(kSmiTagMask));
7406 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007407
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007408 // Go slow case if the value of the expression is zero
7409 // to make sure that we switch between 0 and -0.
7410 __ cmp(r0, Operand(0));
7411 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007412
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007413 // The value of the expression is a smi that is not zero. Try
7414 // optimistic subtraction '0 - value'.
7415 __ rsb(r1, r0, Operand(0), SetCC);
7416 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007417
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007418 __ mov(r0, Operand(r1)); // Set r0 to result.
7419 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007420
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007421 __ bind(&try_float);
7422 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7423 __ b(ne, &slow);
7424 // r0 is a heap number. Get a new heap number in r1.
7425 if (overwrite_) {
7426 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7427 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7428 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7429 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007430 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007431 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7432 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7433 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
7434 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7435 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
7436 __ mov(r0, Operand(r1));
7437 }
7438 } else if (op_ == Token::BIT_NOT) {
7439 // Check if the operand is a heap number.
7440 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7441 __ b(ne, &slow);
7442
7443 // Convert the heap number is r0 to an untagged integer in r1.
7444 GetInt32(masm, r0, r1, r2, r3, &slow);
7445
7446 // Do the bitwise operation (move negated) and check if the result
7447 // fits in a smi.
7448 Label try_float;
7449 __ mvn(r1, Operand(r1));
7450 __ add(r2, r1, Operand(0x40000000), SetCC);
7451 __ b(mi, &try_float);
7452 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
7453 __ b(&done);
7454
7455 __ bind(&try_float);
7456 if (!overwrite_) {
7457 // Allocate a fresh heap number, but don't overwrite r0 until
7458 // we're sure we can do it without going through the slow case
7459 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007460 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007461 __ mov(r0, Operand(r2));
7462 }
7463
7464 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
7465 // have to set up a frame.
7466 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
7467 __ push(lr);
7468 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
7469 __ pop(lr);
7470 } else {
7471 UNIMPLEMENTED();
7472 }
7473
7474 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007475 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007476
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007477 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007478 __ bind(&slow);
7479 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007480 switch (op_) {
7481 case Token::SUB:
7482 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
7483 break;
7484 case Token::BIT_NOT:
7485 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
7486 break;
7487 default:
7488 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007489 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00007490}
7491
7492
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007493void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007494 // r0 holds the exception.
7495
7496 // Adjust this code if not the case.
7497 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7498
7499 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007500 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
7501 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007502
7503 // Restore the next handler and frame pointer, discard handler state.
7504 ASSERT(StackHandlerConstants::kNextOffset == 0);
7505 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007506 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007507 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7508 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
7509
7510 // Before returning we restore the context from the frame pointer if
7511 // not NULL. The frame pointer is NULL in the exception handler of a
7512 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007513 __ cmp(fp, Operand(0));
7514 // Set cp to NULL if fp is NULL.
7515 __ mov(cp, Operand(0), LeaveCC, eq);
7516 // Restore cp otherwise.
7517 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007518#ifdef DEBUG
7519 if (FLAG_debug_code) {
7520 __ mov(lr, Operand(pc));
7521 }
7522#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007523 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007524 __ pop(pc);
7525}
7526
7527
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007528void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
7529 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007530 // Adjust this code if not the case.
7531 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7532
7533 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007534 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007535 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007536
7537 // Unwind the handlers until the ENTRY handler is found.
7538 Label loop, done;
7539 __ bind(&loop);
7540 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007541 const int kStateOffset = StackHandlerConstants::kStateOffset;
7542 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007543 __ cmp(r2, Operand(StackHandler::ENTRY));
7544 __ b(eq, &done);
7545 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007546 const int kNextOffset = StackHandlerConstants::kNextOffset;
7547 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007548 __ jmp(&loop);
7549 __ bind(&done);
7550
7551 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007552 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007553 __ pop(r2);
7554 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007555
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007556 if (type == OUT_OF_MEMORY) {
7557 // Set external caught exception to false.
7558 ExternalReference external_caught(Top::k_external_caught_exception_address);
7559 __ mov(r0, Operand(false));
7560 __ mov(r2, Operand(external_caught));
7561 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007562
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007563 // Set pending exception and r0 to out of memory exception.
7564 Failure* out_of_memory = Failure::OutOfMemoryException();
7565 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7566 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
7567 __ str(r0, MemOperand(r2));
7568 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007569
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007570 // Stack layout at this point. See also StackHandlerConstants.
7571 // sp -> state (ENTRY)
7572 // fp
7573 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007574
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007575 // Discard handler state (r2 is not used) and restore frame pointer.
7576 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7577 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
7578 // Before returning we restore the context from the frame pointer if
7579 // not NULL. The frame pointer is NULL in the exception handler of a
7580 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007581 __ cmp(fp, Operand(0));
7582 // Set cp to NULL if fp is NULL.
7583 __ mov(cp, Operand(0), LeaveCC, eq);
7584 // Restore cp otherwise.
7585 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007586#ifdef DEBUG
7587 if (FLAG_debug_code) {
7588 __ mov(lr, Operand(pc));
7589 }
7590#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007591 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007592 __ pop(pc);
7593}
7594
7595
7596void CEntryStub::GenerateCore(MacroAssembler* masm,
7597 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007598 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007599 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007600 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007601 bool always_allocate,
7602 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007603 // r0: result parameter for PerformGC, if any
7604 // r4: number of arguments including receiver (C callee-saved)
7605 // r5: pointer to builtin function (C callee-saved)
7606 // r6: pointer to the first argument (C callee-saved)
7607
7608 if (do_gc) {
7609 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007610 __ PrepareCallCFunction(1, r1);
7611 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007612 }
7613
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007614 ExternalReference scope_depth =
7615 ExternalReference::heap_always_allocate_scope_depth();
7616 if (always_allocate) {
7617 __ mov(r0, Operand(scope_depth));
7618 __ ldr(r1, MemOperand(r0));
7619 __ add(r1, r1, Operand(1));
7620 __ str(r1, MemOperand(r0));
7621 }
7622
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007623 // Call C built-in.
7624 // r0 = argc, r1 = argv
7625 __ mov(r0, Operand(r4));
7626 __ mov(r1, Operand(r6));
7627
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007628 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
7629 int frame_alignment_mask = frame_alignment - 1;
7630#if defined(V8_HOST_ARCH_ARM)
7631 if (FLAG_debug_code) {
7632 if (frame_alignment > kPointerSize) {
7633 Label alignment_as_expected;
7634 ASSERT(IsPowerOf2(frame_alignment));
7635 __ sub(r2, sp, Operand(frame_alignment_skew));
7636 __ tst(r2, Operand(frame_alignment_mask));
7637 __ b(eq, &alignment_as_expected);
7638 // Don't use Check here, as it will call Runtime_Abort re-entering here.
7639 __ stop("Unexpected alignment");
7640 __ bind(&alignment_as_expected);
7641 }
7642 }
7643#endif
7644
7645 // Just before the call (jump) below lr is pushed, so the actual alignment is
7646 // adding one to the current skew.
7647 int alignment_before_call =
7648 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
7649 if (alignment_before_call > 0) {
7650 // Push until the alignment before the call is met.
7651 __ mov(r2, Operand(0));
7652 for (int i = alignment_before_call;
7653 (i & frame_alignment_mask) != 0;
7654 i += kPointerSize) {
7655 __ push(r2);
7656 }
7657 }
7658
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007659 // TODO(1242173): To let the GC traverse the return address of the exit
7660 // frames, we need to know where the return address is. Right now,
7661 // we push it on the stack to be able to find it again, but we never
7662 // restore from it in case of changes, which makes it impossible to
7663 // support moving the C entry code stub. This should be fixed, but currently
7664 // this is OK because the CEntryStub gets generated so early in the V8 boot
7665 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007666 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007667 masm->push(lr);
7668 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007669
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007670 // Restore sp back to before aligning the stack.
7671 if (alignment_before_call > 0) {
7672 __ add(sp, sp, Operand(alignment_before_call));
7673 }
7674
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007675 if (always_allocate) {
7676 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
7677 // though (contain the result).
7678 __ mov(r2, Operand(scope_depth));
7679 __ ldr(r3, MemOperand(r2));
7680 __ sub(r3, r3, Operand(1));
7681 __ str(r3, MemOperand(r2));
7682 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007683
7684 // check for failure result
7685 Label failure_returned;
7686 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
7687 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
7688 __ add(r2, r0, Operand(1));
7689 __ tst(r2, Operand(kFailureTagMask));
7690 __ b(eq, &failure_returned);
7691
7692 // Exit C frame and return.
7693 // r0:r1: result
7694 // sp: stack pointer
7695 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007696 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007697
7698 // check if we should retry or throw exception
7699 Label retry;
7700 __ bind(&failure_returned);
7701 ASSERT(Failure::RETRY_AFTER_GC == 0);
7702 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
7703 __ b(eq, &retry);
7704
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007705 // Special handling of out of memory exceptions.
7706 Failure* out_of_memory = Failure::OutOfMemoryException();
7707 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7708 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007709
7710 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00007711 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007712 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007713 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007714 __ ldr(r0, MemOperand(ip));
7715 __ str(r3, MemOperand(ip));
7716
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007717 // Special handling of termination exceptions which are uncatchable
7718 // by javascript code.
7719 __ cmp(r0, Operand(Factory::termination_exception()));
7720 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007721
7722 // Handle normal exception.
7723 __ jmp(throw_normal_exception);
7724
7725 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
7726}
7727
7728
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007729void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007730 // Called from JavaScript; parameters are on stack as if calling JS function
7731 // r0: number of arguments including receiver
7732 // r1: pointer to builtin function
7733 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007734 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007735 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007736
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007737 // Result returned in r0 or r0+r1 by default.
7738
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007739 // NOTE: Invocations of builtins may return failure objects
7740 // instead of a proper result. The builtin entry handles
7741 // this by performing a garbage collection and retrying the
7742 // builtin once.
7743
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007744 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007745 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007746
7747 // r4: number of arguments (C callee-saved)
7748 // r5: pointer to builtin function (C callee-saved)
7749 // r6: pointer to first argument (C callee-saved)
7750
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007751 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007752 Label throw_termination_exception;
7753 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007754
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007755 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007756 GenerateCore(masm,
7757 &throw_normal_exception,
7758 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007759 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007760 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007761 false,
7762 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007763
7764 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007765 GenerateCore(masm,
7766 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007767 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007768 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007769 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007770 false,
7771 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007772
7773 // Do full GC and retry runtime call one final time.
7774 Failure* failure = Failure::InternalError();
7775 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
7776 GenerateCore(masm,
7777 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007778 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007779 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007780 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007781 true,
7782 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007783
7784 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007785 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
7786
7787 __ bind(&throw_termination_exception);
7788 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007789
7790 __ bind(&throw_normal_exception);
7791 GenerateThrowTOS(masm);
7792}
7793
7794
7795void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
7796 // r0: code entry
7797 // r1: function
7798 // r2: receiver
7799 // r3: argc
7800 // [sp+0]: argv
7801
7802 Label invoke, exit;
7803
7804 // Called from C, so do not pop argc and args on exit (preserve sp)
7805 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007806 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007807 __ stm(db_w, sp, kCalleeSaved | lr.bit());
7808
7809 // Get address of argv, see stm above.
7810 // r0: code entry
7811 // r1: function
7812 // r2: receiver
7813 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00007814 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007815
7816 // Push a frame with special values setup to mark it as an entry frame.
7817 // r0: code entry
7818 // r1: function
7819 // r2: receiver
7820 // r3: argc
7821 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007822 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007823 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
7824 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007825 __ mov(r6, Operand(Smi::FromInt(marker)));
7826 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7827 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007828 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007829
7830 // Setup frame pointer for the frame to be pushed.
7831 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7832
7833 // Call a faked try-block that does the invoke.
7834 __ bl(&invoke);
7835
7836 // Caught exception: Store result (exception) in the pending
7837 // exception field in the JSEnv and return a failure sentinel.
7838 // Coming in here the fp will be invalid because the PushTryHandler below
7839 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00007840 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007841 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007842 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007843 __ b(&exit);
7844
7845 // Invoke: Link this frame into the handler chain.
7846 __ bind(&invoke);
7847 // Must preserve r0-r4, r5-r7 are available.
7848 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007849 // If an exception not caught by another handler occurs, this handler
7850 // returns control to the code after the bl(&invoke) above, which
7851 // restores all kCalleeSaved registers (including cp and fp) to their
7852 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007853
7854 // Clear any pending exceptions.
7855 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
7856 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007857 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007858 __ str(r5, MemOperand(ip));
7859
7860 // Invoke the function by calling through JS entry trampoline builtin.
7861 // Notice that we cannot store a reference to the trampoline code directly in
7862 // this stub, because runtime stubs are not traversed when doing GC.
7863
7864 // Expected registers by Builtins::JSEntryTrampoline
7865 // r0: code entry
7866 // r1: function
7867 // r2: receiver
7868 // r3: argc
7869 // r4: argv
7870 if (is_construct) {
7871 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
7872 __ mov(ip, Operand(construct_entry));
7873 } else {
7874 ExternalReference entry(Builtins::JSEntryTrampoline);
7875 __ mov(ip, Operand(entry));
7876 }
7877 __ ldr(ip, MemOperand(ip)); // deref address
7878
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007879 // Branch and link to JSEntryTrampoline. We don't use the double underscore
7880 // macro for the add instruction because we don't want the coverage tool
7881 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007882 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007883 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007884
7885 // Unlink this frame from the handler chain. When reading the
7886 // address of the next handler, there is no need to use the address
7887 // displacement since the current stack pointer (sp) points directly
7888 // to the stack handler.
7889 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
7890 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
7891 __ str(r3, MemOperand(ip));
7892 // No need to restore registers
7893 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
7894
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007895
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007896 __ bind(&exit); // r0 holds result
7897 // Restore the top frame descriptors from the stack.
7898 __ pop(r3);
7899 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7900 __ str(r3, MemOperand(ip));
7901
7902 // Reset the stack to the callee saved registers.
7903 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7904
7905 // Restore callee-saved registers and return.
7906#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007907 if (FLAG_debug_code) {
7908 __ mov(lr, Operand(pc));
7909 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007910#endif
7911 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
7912}
7913
7914
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007915// This stub performs an instanceof, calling the builtin function if
7916// necessary. Uses r1 for the object, r0 for the function that it may
7917// be an instance of (these are fetched from the stack).
7918void InstanceofStub::Generate(MacroAssembler* masm) {
7919 // Get the object - slow case for smis (we may need to throw an exception
7920 // depending on the rhs).
7921 Label slow, loop, is_instance, is_not_instance;
7922 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
7923 __ BranchOnSmi(r0, &slow);
7924
7925 // Check that the left hand is a JS object and put map in r3.
7926 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
7927 __ b(lt, &slow);
7928 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
7929 __ b(gt, &slow);
7930
7931 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00007932 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007933 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
7934
7935 // Check that the function prototype is a JS object.
7936 __ BranchOnSmi(r4, &slow);
7937 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
7938 __ b(lt, &slow);
7939 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
7940 __ b(gt, &slow);
7941
7942 // Register mapping: r3 is object map and r4 is function prototype.
7943 // Get prototype of object into r2.
7944 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
7945
7946 // Loop through the prototype chain looking for the function prototype.
7947 __ bind(&loop);
7948 __ cmp(r2, Operand(r4));
7949 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00007950 __ LoadRoot(ip, Heap::kNullValueRootIndex);
7951 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007952 __ b(eq, &is_not_instance);
7953 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
7954 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
7955 __ jmp(&loop);
7956
7957 __ bind(&is_instance);
7958 __ mov(r0, Operand(Smi::FromInt(0)));
7959 __ pop();
7960 __ pop();
7961 __ mov(pc, Operand(lr)); // Return.
7962
7963 __ bind(&is_not_instance);
7964 __ mov(r0, Operand(Smi::FromInt(1)));
7965 __ pop();
7966 __ pop();
7967 __ mov(pc, Operand(lr)); // Return.
7968
7969 // Slow-case. Tail call builtin.
7970 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007971 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
7972}
7973
7974
ager@chromium.org7c537e22008-10-16 08:43:32 +00007975void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
7976 // The displacement is the offset of the last parameter (if any)
7977 // relative to the frame pointer.
7978 static const int kDisplacement =
7979 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007980
ager@chromium.org7c537e22008-10-16 08:43:32 +00007981 // Check that the key is a smi.
7982 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007983 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007984
ager@chromium.org7c537e22008-10-16 08:43:32 +00007985 // Check if the calling frame is an arguments adaptor frame.
7986 Label adaptor;
7987 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
7988 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007989 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00007990 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007991
ager@chromium.org7c537e22008-10-16 08:43:32 +00007992 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007993 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00007994 // check for free.
7995 __ cmp(r1, r0);
7996 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007997
ager@chromium.org7c537e22008-10-16 08:43:32 +00007998 // Read the argument from the stack and return it.
7999 __ sub(r3, r0, r1);
8000 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8001 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008002 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008003
8004 // Arguments adaptor case: Check index against actual arguments
8005 // limit found in the arguments adaptor frame. Use unsigned
8006 // comparison to get negative check for free.
8007 __ bind(&adaptor);
8008 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8009 __ cmp(r1, r0);
8010 __ b(cs, &slow);
8011
8012 // Read the argument from the adaptor frame and return it.
8013 __ sub(r3, r0, r1);
8014 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8015 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008016 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008017
8018 // Slow-case: Handle non-smi or out-of-bounds access to arguments
8019 // by calling the runtime system.
8020 __ bind(&slow);
8021 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008022 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008023}
8024
8025
8026void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00008027 // sp[0] : number of parameters
8028 // sp[4] : receiver displacement
8029 // sp[8] : function
8030
ager@chromium.org7c537e22008-10-16 08:43:32 +00008031 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00008032 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00008033 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8034 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008035 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00008036 __ b(eq, &adaptor_frame);
8037
8038 // Get the length from the frame.
8039 __ ldr(r1, MemOperand(sp, 0));
8040 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008041
8042 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00008043 __ bind(&adaptor_frame);
8044 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8045 __ str(r1, MemOperand(sp, 0));
8046 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008047 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
8048 __ str(r3, MemOperand(sp, 1 * kPointerSize));
8049
ager@chromium.org5c838252010-02-19 08:53:10 +00008050 // Try the new space allocation. Start out with computing the size
8051 // of the arguments object and the elements array (in words, not
8052 // bytes because AllocateInNewSpace expects words).
8053 Label add_arguments_object;
8054 __ bind(&try_allocate);
8055 __ cmp(r1, Operand(0));
8056 __ b(eq, &add_arguments_object);
8057 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8058 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
8059 __ bind(&add_arguments_object);
8060 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
8061
8062 // Do the allocation of both objects in one go.
8063 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
8064
8065 // Get the arguments boilerplate from the current (global) context.
8066 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
8067 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
8068 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
8069 __ ldr(r4, MemOperand(r4, offset));
8070
8071 // Copy the JS object part.
8072 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
8073 __ ldr(r3, FieldMemOperand(r4, i));
8074 __ str(r3, FieldMemOperand(r0, i));
8075 }
8076
8077 // Setup the callee in-object property.
8078 ASSERT(Heap::arguments_callee_index == 0);
8079 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
8080 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
8081
8082 // Get the length (smi tagged) and set that as an in-object property too.
8083 ASSERT(Heap::arguments_length_index == 1);
8084 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
8085 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
8086
8087 // If there are no actual arguments, we're done.
8088 Label done;
8089 __ cmp(r1, Operand(0));
8090 __ b(eq, &done);
8091
8092 // Get the parameters pointer from the stack and untag the length.
8093 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
8094 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8095
8096 // Setup the elements pointer in the allocated arguments object and
8097 // initialize the header in the elements fixed array.
8098 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
8099 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
8100 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
8101 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
8102 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
8103
8104 // Copy the fixed array slots.
8105 Label loop;
8106 // Setup r4 to point to the first array slot.
8107 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
8108 __ bind(&loop);
8109 // Pre-decrement r2 with kPointerSize on each iteration.
8110 // Pre-decrement in order to skip receiver.
8111 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
8112 // Post-increment r4 with kPointerSize on each iteration.
8113 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
8114 __ sub(r1, r1, Operand(1));
8115 __ cmp(r1, Operand(0));
8116 __ b(ne, &loop);
8117
8118 // Return and remove the on-stack parameters.
8119 __ bind(&done);
8120 __ add(sp, sp, Operand(3 * kPointerSize));
8121 __ Ret();
8122
ager@chromium.org7c537e22008-10-16 08:43:32 +00008123 // Do the runtime call to allocate the arguments object.
8124 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008125 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008126}
8127
8128
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008129void RegExpExecStub::Generate(MacroAssembler* masm) {
8130 // Just jump directly to runtime if native RegExp is not selected at compile
8131 // time or if regexp entry in generated code is turned off runtime switch or
8132 // at compilation.
8133#ifndef V8_NATIVE_REGEXP
8134 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8135#else // V8_NATIVE_REGEXP
8136 if (!FLAG_regexp_entry_native) {
8137 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8138 return;
8139 }
8140
8141 // Stack frame on entry.
8142 // sp[0]: last_match_info (expected JSArray)
8143 // sp[4]: previous index
8144 // sp[8]: subject string
8145 // sp[12]: JSRegExp object
8146
8147 static const int kLastMatchInfoOffset = 0 * kPointerSize;
8148 static const int kPreviousIndexOffset = 1 * kPointerSize;
8149 static const int kSubjectOffset = 2 * kPointerSize;
8150 static const int kJSRegExpOffset = 3 * kPointerSize;
8151
8152 Label runtime, invoke_regexp;
8153
8154 // Allocation of registers for this function. These are in callee save
8155 // registers and will be preserved by the call to the native RegExp code, as
8156 // this code is called using the normal C calling convention. When calling
8157 // directly from generated code the native RegExp code will not do a GC and
8158 // therefore the content of these registers are safe to use after the call.
8159 Register subject = r4;
8160 Register regexp_data = r5;
8161 Register last_match_info_elements = r6;
8162
8163 // Ensure that a RegExp stack is allocated.
8164 ExternalReference address_of_regexp_stack_memory_address =
8165 ExternalReference::address_of_regexp_stack_memory_address();
8166 ExternalReference address_of_regexp_stack_memory_size =
8167 ExternalReference::address_of_regexp_stack_memory_size();
8168 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
8169 __ ldr(r0, MemOperand(r0, 0));
8170 __ tst(r0, Operand(r0));
8171 __ b(eq, &runtime);
8172
8173 // Check that the first argument is a JSRegExp object.
8174 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
8175 ASSERT_EQ(0, kSmiTag);
8176 __ tst(r0, Operand(kSmiTagMask));
8177 __ b(eq, &runtime);
8178 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
8179 __ b(ne, &runtime);
8180
8181 // Check that the RegExp has been compiled (data contains a fixed array).
8182 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
8183 if (FLAG_debug_code) {
8184 __ tst(regexp_data, Operand(kSmiTagMask));
8185 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
8186 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
8187 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
8188 }
8189
8190 // regexp_data: RegExp data (FixedArray)
8191 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
8192 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
8193 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
8194 __ b(ne, &runtime);
8195
8196 // regexp_data: RegExp data (FixedArray)
8197 // Check that the number of captures fit in the static offsets vector buffer.
8198 __ ldr(r2,
8199 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8200 // Calculate number of capture registers (number_of_captures + 1) * 2. This
8201 // uses the asumption that smis are 2 * their untagged value.
8202 ASSERT_EQ(0, kSmiTag);
8203 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8204 __ add(r2, r2, Operand(2)); // r2 was a smi.
8205 // Check that the static offsets vector buffer is large enough.
8206 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
8207 __ b(hi, &runtime);
8208
8209 // r2: Number of capture registers
8210 // regexp_data: RegExp data (FixedArray)
8211 // Check that the second argument is a string.
8212 __ ldr(subject, MemOperand(sp, kSubjectOffset));
8213 __ tst(subject, Operand(kSmiTagMask));
8214 __ b(eq, &runtime);
8215 Condition is_string = masm->IsObjectStringType(subject, r0);
8216 __ b(NegateCondition(is_string), &runtime);
8217 // Get the length of the string to r3.
8218 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
8219
8220 // r2: Number of capture registers
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00008221 // r3: Length of subject string
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008222 // subject: Subject string
8223 // regexp_data: RegExp data (FixedArray)
8224 // Check that the third argument is a positive smi less than the subject
8225 // string length. A negative value will be greater (unsigned comparison).
8226 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00008227 __ cmp(r3, Operand(r0, ASR, kSmiTagSize + kSmiShiftSize));
8228 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008229
8230 // r2: Number of capture registers
8231 // subject: Subject string
8232 // regexp_data: RegExp data (FixedArray)
8233 // Check that the fourth object is a JSArray object.
8234 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8235 __ tst(r0, Operand(kSmiTagMask));
8236 __ b(eq, &runtime);
8237 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
8238 __ b(ne, &runtime);
8239 // Check that the JSArray is in fast case.
8240 __ ldr(last_match_info_elements,
8241 FieldMemOperand(r0, JSArray::kElementsOffset));
8242 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008243 __ LoadRoot(ip, kFixedArrayMapRootIndex);
8244 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008245 __ b(ne, &runtime);
8246 // Check that the last match info has space for the capture registers and the
8247 // additional information.
8248 __ ldr(r0,
8249 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
8250 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
8251 __ cmp(r2, r0);
8252 __ b(gt, &runtime);
8253
8254 // subject: Subject string
8255 // regexp_data: RegExp data (FixedArray)
8256 // Check the representation and encoding of the subject string.
8257 Label seq_string;
8258 const int kStringRepresentationEncodingMask =
8259 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
8260 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8261 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8262 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8263 // First check for sequential string.
8264 ASSERT_EQ(0, kStringTag);
8265 ASSERT_EQ(0, kSeqStringTag);
8266 __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask));
8267 __ b(eq, &seq_string);
8268
8269 // subject: Subject string
8270 // regexp_data: RegExp data (FixedArray)
8271 // Check for flat cons string.
8272 // A flat cons string is a cons string where the second part is the empty
8273 // string. In that case the subject string is just the first part of the cons
8274 // string. Also in this case the first part of the cons string is known to be
8275 // a sequential string or an external string.
8276 __ and_(r0, r0, Operand(kStringRepresentationMask));
8277 __ cmp(r0, Operand(kConsStringTag));
8278 __ b(ne, &runtime);
8279 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
8280 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
8281 __ cmp(r0, r1);
8282 __ b(ne, &runtime);
8283 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
8284 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8285 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8286 ASSERT_EQ(0, kSeqStringTag);
8287 __ tst(r0, Operand(kStringRepresentationMask));
8288 __ b(nz, &runtime);
8289 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8290
8291 __ bind(&seq_string);
8292 // r1: suject string type & kStringRepresentationEncodingMask
8293 // subject: Subject string
8294 // regexp_data: RegExp data (FixedArray)
8295 // Check that the irregexp code has been generated for an ascii string. If
8296 // it has, the field contains a code object otherwise it contains the hole.
8297#ifdef DEBUG
8298 const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag;
8299 const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag;
8300 CHECK_EQ(4, kSeqAsciiString);
8301 CHECK_EQ(0, kSeqTwoByteString);
8302#endif
8303 // Find the code object based on the assumptions above.
8304 __ mov(r3, Operand(r1, ASR, 2), SetCC);
8305 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
8306 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
8307
8308 // Check that the irregexp code has been generated for the actual string
8309 // encoding. If it has, the field contains a code object otherwise it contains
8310 // the hole.
8311 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
8312 __ b(ne, &runtime);
8313
8314 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8315 // r7: code
8316 // subject: Subject string
8317 // regexp_data: RegExp data (FixedArray)
8318 // Load used arguments before starting to push arguments for call to native
8319 // RegExp code to avoid handling changing stack height.
8320 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
8321 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
8322
8323 // r1: previous index
8324 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8325 // r7: code
8326 // subject: Subject string
8327 // regexp_data: RegExp data (FixedArray)
8328 // All checks done. Now push arguments for native regexp code.
8329 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
8330
8331 static const int kRegExpExecuteArguments = 7;
8332 __ push(lr);
8333 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
8334
8335 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
8336 __ mov(r0, Operand(1));
8337 __ str(r0, MemOperand(sp, 2 * kPointerSize));
8338
8339 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
8340 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
8341 __ ldr(r0, MemOperand(r0, 0));
8342 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
8343 __ ldr(r2, MemOperand(r2, 0));
8344 __ add(r0, r0, Operand(r2));
8345 __ str(r0, MemOperand(sp, 1 * kPointerSize));
8346
8347 // Argument 5 (sp[0]): static offsets vector buffer.
8348 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
8349 __ str(r0, MemOperand(sp, 0 * kPointerSize));
8350
8351 // For arguments 4 and 3 get string length, calculate start of string data and
8352 // calculate the shift of the index (0 for ASCII and 1 for two byte).
8353 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
8354 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
8355 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8356 __ eor(r3, r3, Operand(1));
8357 // Argument 4 (r3): End of string data
8358 // Argument 3 (r2): Start of string data
8359 __ add(r2, r9, Operand(r1, LSL, r3));
8360 __ add(r3, r9, Operand(r0, LSL, r3));
8361
8362 // Argument 2 (r1): Previous index.
8363 // Already there
8364
8365 // Argument 1 (r0): Subject string.
8366 __ mov(r0, subject);
8367
8368 // Locate the code entry and call it.
8369 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
8370 __ CallCFunction(r7, kRegExpExecuteArguments);
8371 __ pop(lr);
8372
8373 // r0: result
8374 // subject: subject string (callee saved)
8375 // regexp_data: RegExp data (callee saved)
8376 // last_match_info_elements: Last match info elements (callee saved)
8377
8378 // Check the result.
8379 Label success;
8380 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
8381 __ b(eq, &success);
8382 Label failure;
8383 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
8384 __ b(eq, &failure);
8385 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
8386 // If not exception it can only be retry. Handle that in the runtime system.
8387 __ b(ne, &runtime);
8388 // Result must now be exception. If there is no pending exception already a
8389 // stack overflow (on the backtrack stack) was detected in RegExp code but
8390 // haven't created the exception yet. Handle that in the runtime system.
8391 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
8392 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
8393 __ ldr(r0, MemOperand(r0, 0));
8394 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
8395 __ ldr(r1, MemOperand(r1, 0));
8396 __ cmp(r0, r1);
8397 __ b(eq, &runtime);
8398 __ bind(&failure);
8399 // For failure and exception return null.
8400 __ mov(r0, Operand(Factory::null_value()));
8401 __ add(sp, sp, Operand(4 * kPointerSize));
8402 __ Ret();
8403
8404 // Process the result from the native regexp code.
8405 __ bind(&success);
8406 __ ldr(r1,
8407 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8408 // Calculate number of capture registers (number_of_captures + 1) * 2.
8409 ASSERT_EQ(0, kSmiTag);
8410 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8411 __ add(r1, r1, Operand(2)); // r1 was a smi.
8412
8413 // r1: number of capture registers
8414 // r4: subject string
8415 // Store the capture count.
8416 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
8417 __ str(r2, FieldMemOperand(last_match_info_elements,
8418 RegExpImpl::kLastCaptureCountOffset));
8419 // Store last subject and last input.
8420 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
8421 __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto.
8422 __ str(subject,
8423 FieldMemOperand(last_match_info_elements,
8424 RegExpImpl::kLastSubjectOffset));
8425 __ RecordWrite(r3, r2, r7);
8426 __ str(subject,
8427 FieldMemOperand(last_match_info_elements,
8428 RegExpImpl::kLastInputOffset));
8429 __ mov(r3, last_match_info_elements);
8430 __ mov(r2, Operand(RegExpImpl::kLastInputOffset));
8431 __ RecordWrite(r3, r2, r7);
8432
8433 // Get the static offsets vector filled by the native regexp code.
8434 ExternalReference address_of_static_offsets_vector =
8435 ExternalReference::address_of_static_offsets_vector();
8436 __ mov(r2, Operand(address_of_static_offsets_vector));
8437
8438 // r1: number of capture registers
8439 // r2: offsets vector
8440 Label next_capture, done;
8441 // Capture register counter starts from number of capture registers and
8442 // counts down until wraping after zero.
8443 __ add(r0,
8444 last_match_info_elements,
8445 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
8446 __ bind(&next_capture);
8447 __ sub(r1, r1, Operand(1), SetCC);
8448 __ b(mi, &done);
8449 // Read the value from the static offsets vector buffer.
8450 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
8451 // Store the smi value in the last match info.
8452 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
8453 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
8454 __ jmp(&next_capture);
8455 __ bind(&done);
8456
8457 // Return last match info.
8458 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8459 __ add(sp, sp, Operand(4 * kPointerSize));
8460 __ Ret();
8461
8462 // Do the runtime call to execute the regexp.
8463 __ bind(&runtime);
8464 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8465#endif // V8_NATIVE_REGEXP
8466}
8467
8468
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008469void CallFunctionStub::Generate(MacroAssembler* masm) {
8470 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008471
8472 // If the receiver might be a value (string, number or boolean) check for this
8473 // and box it if it is.
8474 if (ReceiverMightBeValue()) {
8475 // Get the receiver from the stack.
8476 // function, receiver [, arguments]
8477 Label receiver_is_value, receiver_is_js_object;
8478 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
8479
8480 // Check if receiver is a smi (which is a number value).
8481 __ BranchOnSmi(r1, &receiver_is_value);
8482
8483 // Check if the receiver is a valid JS object.
8484 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
8485 __ b(ge, &receiver_is_js_object);
8486
8487 // Call the runtime to box the value.
8488 __ bind(&receiver_is_value);
8489 __ EnterInternalFrame();
8490 __ push(r1);
8491 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
8492 __ LeaveInternalFrame();
8493 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
8494
8495 __ bind(&receiver_is_js_object);
8496 }
8497
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008498 // Get the function to call from the stack.
8499 // function, receiver [, arguments]
8500 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
8501
8502 // Check that the function is really a JavaScript function.
8503 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008504 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008505 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008506 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008507 __ b(ne, &slow);
8508
8509 // Fast-case: Invoke the function now.
8510 // r1: pushed function
8511 ParameterCount actual(argc_);
8512 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
8513
8514 // Slow-case: Non-function called.
8515 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00008516 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
8517 // of the original receiver from the call site).
8518 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008519 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008520 __ mov(r2, Operand(0));
8521 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
8522 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
8523 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008524}
8525
8526
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008527// Unfortunately you have to run without snapshots to see most of these
8528// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008529const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008530 if (name_ != NULL) return name_;
8531 const int kMaxNameLength = 100;
8532 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
8533 if (name_ == NULL) return "OOM";
8534
8535 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008536 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008537 case lt: cc_name = "LT"; break;
8538 case gt: cc_name = "GT"; break;
8539 case le: cc_name = "LE"; break;
8540 case ge: cc_name = "GE"; break;
8541 case eq: cc_name = "EQ"; break;
8542 case ne: cc_name = "NE"; break;
8543 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008544 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008545
8546 const char* strict_name = "";
8547 if (strict_ && (cc_ == eq || cc_ == ne)) {
8548 strict_name = "_STRICT";
8549 }
8550
8551 const char* never_nan_nan_name = "";
8552 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
8553 never_nan_nan_name = "_NO_NAN";
8554 }
8555
8556 const char* include_number_compare_name = "";
8557 if (!include_number_compare_) {
8558 include_number_compare_name = "_NO_NUMBER";
8559 }
8560
8561 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
8562 "CompareStub_%s%s%s%s",
8563 cc_name,
8564 strict_name,
8565 never_nan_nan_name,
8566 include_number_compare_name);
8567 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008568}
8569
8570
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008571int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008572 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
8573 // stubs the never NaN NaN condition is only taken into account if the
8574 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008575 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008576 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
8577 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008578 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
8579 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008580}
8581
8582
ager@chromium.org5c838252010-02-19 08:53:10 +00008583void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
8584 Register dest,
8585 Register src,
8586 Register count,
8587 Register scratch,
8588 bool ascii) {
8589 Label loop;
8590 Label done;
8591 // This loop just copies one character at a time, as it is only used for very
8592 // short strings.
8593 if (!ascii) {
8594 __ add(count, count, Operand(count), SetCC);
8595 } else {
8596 __ cmp(count, Operand(0));
8597 }
8598 __ b(eq, &done);
8599
8600 __ bind(&loop);
8601 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
8602 // Perform sub between load and dependent store to get the load time to
8603 // complete.
8604 __ sub(count, count, Operand(1), SetCC);
8605 __ strb(scratch, MemOperand(dest, 1, PostIndex));
8606 // last iteration.
8607 __ b(gt, &loop);
8608
8609 __ bind(&done);
8610}
8611
8612
8613enum CopyCharactersFlags {
8614 COPY_ASCII = 1,
8615 DEST_ALWAYS_ALIGNED = 2
8616};
8617
8618
8619void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
8620 Register dest,
8621 Register src,
8622 Register count,
8623 Register scratch1,
8624 Register scratch2,
8625 Register scratch3,
8626 Register scratch4,
8627 Register scratch5,
8628 int flags) {
8629 bool ascii = (flags & COPY_ASCII) != 0;
8630 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
8631
8632 if (dest_always_aligned && FLAG_debug_code) {
8633 // Check that destination is actually word aligned if the flag says
8634 // that it is.
8635 __ tst(dest, Operand(kPointerAlignmentMask));
8636 __ Check(eq, "Destination of copy not aligned.");
8637 }
8638
8639 const int kReadAlignment = 4;
8640 const int kReadAlignmentMask = kReadAlignment - 1;
8641 // Ensure that reading an entire aligned word containing the last character
8642 // of a string will not read outside the allocated area (because we pad up
8643 // to kObjectAlignment).
8644 ASSERT(kObjectAlignment >= kReadAlignment);
8645 // Assumes word reads and writes are little endian.
8646 // Nothing to do for zero characters.
8647 Label done;
8648 if (!ascii) {
8649 __ add(count, count, Operand(count), SetCC);
8650 } else {
8651 __ cmp(count, Operand(0));
8652 }
8653 __ b(eq, &done);
8654
8655 // Assume that you cannot read (or write) unaligned.
8656 Label byte_loop;
8657 // Must copy at least eight bytes, otherwise just do it one byte at a time.
8658 __ cmp(count, Operand(8));
8659 __ add(count, dest, Operand(count));
8660 Register limit = count; // Read until src equals this.
8661 __ b(lt, &byte_loop);
8662
8663 if (!dest_always_aligned) {
8664 // Align dest by byte copying. Copies between zero and three bytes.
8665 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
8666 Label dest_aligned;
8667 __ b(eq, &dest_aligned);
8668 __ cmp(scratch4, Operand(2));
8669 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
8670 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
8671 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
8672 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8673 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
8674 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
8675 __ bind(&dest_aligned);
8676 }
8677
8678 Label simple_loop;
8679
8680 __ sub(scratch4, dest, Operand(src));
8681 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
8682 __ b(eq, &simple_loop);
8683 // Shift register is number of bits in a source word that
8684 // must be combined with bits in the next source word in order
8685 // to create a destination word.
8686
8687 // Complex loop for src/dst that are not aligned the same way.
8688 {
8689 Label loop;
8690 __ mov(scratch4, Operand(scratch4, LSL, 3));
8691 Register left_shift = scratch4;
8692 __ and_(src, src, Operand(~3)); // Round down to load previous word.
8693 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8694 // Store the "shift" most significant bits of scratch in the least
8695 // signficant bits (i.e., shift down by (32-shift)).
8696 __ rsb(scratch2, left_shift, Operand(32));
8697 Register right_shift = scratch2;
8698 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
8699
8700 __ bind(&loop);
8701 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
8702 __ sub(scratch5, limit, Operand(dest));
8703 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
8704 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8705 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
8706 // Loop if four or more bytes left to copy.
8707 // Compare to eight, because we did the subtract before increasing dst.
8708 __ sub(scratch5, scratch5, Operand(8), SetCC);
8709 __ b(ge, &loop);
8710 }
8711 // There is now between zero and three bytes left to copy (negative that
8712 // number is in scratch5), and between one and three bytes already read into
8713 // scratch1 (eight times that number in scratch4). We may have read past
8714 // the end of the string, but because objects are aligned, we have not read
8715 // past the end of the object.
8716 // Find the minimum of remaining characters to move and preloaded characters
8717 // and write those as bytes.
8718 __ add(scratch5, scratch5, Operand(4), SetCC);
8719 __ b(eq, &done);
8720 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
8721 // Move minimum of bytes read and bytes left to copy to scratch4.
8722 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
8723 // Between one and three (value in scratch5) characters already read into
8724 // scratch ready to write.
8725 __ cmp(scratch5, Operand(2));
8726 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8727 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
8728 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
8729 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
8730 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
8731 // Copy any remaining bytes.
8732 __ b(&byte_loop);
8733
8734 // Simple loop.
8735 // Copy words from src to dst, until less than four bytes left.
8736 // Both src and dest are word aligned.
8737 __ bind(&simple_loop);
8738 {
8739 Label loop;
8740 __ bind(&loop);
8741 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8742 __ sub(scratch3, limit, Operand(dest));
8743 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8744 // Compare to 8, not 4, because we do the substraction before increasing
8745 // dest.
8746 __ cmp(scratch3, Operand(8));
8747 __ b(ge, &loop);
8748 }
8749
8750 // Copy bytes from src to dst until dst hits limit.
8751 __ bind(&byte_loop);
8752 __ cmp(dest, Operand(limit));
8753 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
8754 __ b(ge, &done);
8755 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8756 __ b(&byte_loop);
8757
8758 __ bind(&done);
8759}
8760
8761
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008762void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
8763 Register c1,
8764 Register c2,
8765 Register scratch1,
8766 Register scratch2,
8767 Register scratch3,
8768 Register scratch4,
8769 Register scratch5,
8770 Label* not_found) {
8771 // Register scratch3 is the general scratch register in this function.
8772 Register scratch = scratch3;
8773
8774 // Make sure that both characters are not digits as such strings has a
8775 // different hash algorithm. Don't try to look for these in the symbol table.
8776 Label not_array_index;
8777 __ sub(scratch, c1, Operand(static_cast<int>('0')));
8778 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8779 __ b(hi, &not_array_index);
8780 __ sub(scratch, c2, Operand(static_cast<int>('0')));
8781 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8782
8783 // If check failed combine both characters into single halfword.
8784 // This is required by the contract of the method: code at the
8785 // not_found branch expects this combination in c1 register
8786 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
8787 __ b(ls, not_found);
8788
8789 __ bind(&not_array_index);
8790 // Calculate the two character string hash.
8791 Register hash = scratch1;
8792 GenerateHashInit(masm, hash, c1);
8793 GenerateHashAddCharacter(masm, hash, c2);
8794 GenerateHashGetHash(masm, hash);
8795
8796 // Collect the two characters in a register.
8797 Register chars = c1;
8798 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
8799
8800 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8801 // hash: hash of two character string.
8802
8803 // Load symbol table
8804 // Load address of first element of the symbol table.
8805 Register symbol_table = c2;
8806 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
8807
8808 // Load undefined value
8809 Register undefined = scratch4;
8810 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
8811
8812 // Calculate capacity mask from the symbol table capacity.
8813 Register mask = scratch2;
8814 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
8815 __ mov(mask, Operand(mask, ASR, 1));
8816 __ sub(mask, mask, Operand(1));
8817
8818 // Calculate untagged address of the first element of the symbol table.
8819 Register first_symbol_table_element = symbol_table;
8820 __ add(first_symbol_table_element, symbol_table,
8821 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
8822
8823 // Registers
8824 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8825 // hash: hash of two character string
8826 // mask: capacity mask
8827 // first_symbol_table_element: address of the first element of
8828 // the symbol table
8829 // scratch: -
8830
8831 // Perform a number of probes in the symbol table.
8832 static const int kProbes = 4;
8833 Label found_in_symbol_table;
8834 Label next_probe[kProbes];
8835 for (int i = 0; i < kProbes; i++) {
8836 Register candidate = scratch5; // Scratch register contains candidate.
8837
8838 // Calculate entry in symbol table.
8839 if (i > 0) {
8840 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
8841 } else {
8842 __ mov(candidate, hash);
8843 }
8844
8845 __ and_(candidate, candidate, Operand(mask));
8846
8847 // Load the entry from the symble table.
8848 ASSERT_EQ(1, SymbolTable::kEntrySize);
8849 __ ldr(candidate,
8850 MemOperand(first_symbol_table_element,
8851 candidate,
8852 LSL,
8853 kPointerSizeLog2));
8854
8855 // If entry is undefined no string with this hash can be found.
8856 __ cmp(candidate, undefined);
8857 __ b(eq, not_found);
8858
8859 // If length is not 2 the string is not a candidate.
8860 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00008861 __ cmp(scratch, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008862 __ b(ne, &next_probe[i]);
8863
8864 // Check that the candidate is a non-external ascii string.
8865 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
8866 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
8867 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
8868 &next_probe[i]);
8869
8870 // Check if the two characters match.
8871 // Assumes that word load is little endian.
8872 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
8873 __ cmp(chars, scratch);
8874 __ b(eq, &found_in_symbol_table);
8875 __ bind(&next_probe[i]);
8876 }
8877
8878 // No matching 2 character string found by probing.
8879 __ jmp(not_found);
8880
8881 // Scratch register contains result when we fall through to here.
8882 Register result = scratch;
8883 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00008884 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008885}
8886
8887
8888void StringStubBase::GenerateHashInit(MacroAssembler* masm,
8889 Register hash,
8890 Register character) {
8891 // hash = character + (character << 10);
8892 __ add(hash, character, Operand(character, LSL, 10));
8893 // hash ^= hash >> 6;
8894 __ eor(hash, hash, Operand(hash, ASR, 6));
8895}
8896
8897
8898void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm,
8899 Register hash,
8900 Register character) {
8901 // hash += character;
8902 __ add(hash, hash, Operand(character));
8903 // hash += hash << 10;
8904 __ add(hash, hash, Operand(hash, LSL, 10));
8905 // hash ^= hash >> 6;
8906 __ eor(hash, hash, Operand(hash, ASR, 6));
8907}
8908
8909
8910void StringStubBase::GenerateHashGetHash(MacroAssembler* masm,
8911 Register hash) {
8912 // hash += hash << 3;
8913 __ add(hash, hash, Operand(hash, LSL, 3));
8914 // hash ^= hash >> 11;
8915 __ eor(hash, hash, Operand(hash, ASR, 11));
8916 // hash += hash << 15;
8917 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
8918
8919 // if (hash == 0) hash = 27;
8920 __ mov(hash, Operand(27), LeaveCC, nz);
8921}
8922
8923
ager@chromium.org5c838252010-02-19 08:53:10 +00008924void SubStringStub::Generate(MacroAssembler* masm) {
8925 Label runtime;
8926
8927 // Stack frame on entry.
8928 // lr: return address
8929 // sp[0]: to
8930 // sp[4]: from
8931 // sp[8]: string
8932
8933 // This stub is called from the native-call %_SubString(...), so
8934 // nothing can be assumed about the arguments. It is tested that:
8935 // "string" is a sequential string,
8936 // both "from" and "to" are smis, and
8937 // 0 <= from <= to <= string.length.
8938 // If any of these assumptions fail, we call the runtime system.
8939
8940 static const int kToOffset = 0 * kPointerSize;
8941 static const int kFromOffset = 1 * kPointerSize;
8942 static const int kStringOffset = 2 * kPointerSize;
8943
8944
8945 // Check bounds and smi-ness.
8946 __ ldr(r7, MemOperand(sp, kToOffset));
8947 __ ldr(r6, MemOperand(sp, kFromOffset));
8948 ASSERT_EQ(0, kSmiTag);
8949 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8950 // I.e., arithmetic shift right by one un-smi-tags.
8951 __ mov(r2, Operand(r7, ASR, 1), SetCC);
8952 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
8953 // If either r2 or r6 had the smi tag bit set, then carry is set now.
8954 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
8955 __ b(mi, &runtime); // From is negative.
8956
8957 __ sub(r2, r2, Operand(r3), SetCC);
8958 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008959 // Special handling of sub-strings of length 1 and 2. One character strings
8960 // are handled in the runtime system (looked up in the single character
8961 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00008962 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008963 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00008964
8965 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008966 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008967 // r6: from (smi)
8968 // r7: to (smi)
8969
8970 // Make sure first argument is a sequential (or flat) string.
8971 __ ldr(r5, MemOperand(sp, kStringOffset));
8972 ASSERT_EQ(0, kSmiTag);
8973 __ tst(r5, Operand(kSmiTagMask));
8974 __ b(eq, &runtime);
8975 Condition is_string = masm->IsObjectStringType(r5, r1);
8976 __ b(NegateCondition(is_string), &runtime);
8977
8978 // r1: instance type
8979 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008980 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008981 // r5: string
8982 // r6: from (smi)
8983 // r7: to (smi)
8984 Label seq_string;
8985 __ and_(r4, r1, Operand(kStringRepresentationMask));
8986 ASSERT(kSeqStringTag < kConsStringTag);
8987 ASSERT(kExternalStringTag > kConsStringTag);
8988 __ cmp(r4, Operand(kConsStringTag));
8989 __ b(gt, &runtime); // External strings go to runtime.
8990 __ b(lt, &seq_string); // Sequential strings are handled directly.
8991
8992 // Cons string. Try to recurse (once) on the first substring.
8993 // (This adds a little more generality than necessary to handle flattened
8994 // cons strings, but not much).
8995 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
8996 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
8997 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8998 __ tst(r1, Operand(kStringRepresentationMask));
8999 ASSERT_EQ(0, kSeqStringTag);
9000 __ b(ne, &runtime); // Cons and External strings go to runtime.
9001
9002 // Definitly a sequential string.
9003 __ bind(&seq_string);
9004
9005 // r1: instance type.
9006 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009007 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009008 // r5: string
9009 // r6: from (smi)
9010 // r7: to (smi)
9011 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00009012 __ cmp(r4, Operand(r7, ASR, 1));
ager@chromium.org5c838252010-02-19 08:53:10 +00009013 __ b(lt, &runtime); // Fail if to > length.
9014
9015 // r1: instance type.
9016 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009017 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009018 // r5: string.
9019 // r6: from offset (smi)
9020 // Check for flat ascii string.
9021 Label non_ascii_flat;
9022 __ tst(r1, Operand(kStringEncodingMask));
9023 ASSERT_EQ(0, kTwoByteStringTag);
9024 __ b(eq, &non_ascii_flat);
9025
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009026 Label result_longer_than_two;
9027 __ cmp(r2, Operand(2));
9028 __ b(gt, &result_longer_than_two);
9029
9030 // Sub string of length 2 requested.
9031 // Get the two characters forming the sub string.
9032 __ add(r5, r5, Operand(r3));
9033 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
9034 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
9035
9036 // Try to lookup two character string in symbol table.
9037 Label make_two_character_string;
9038 GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9,
9039 &make_two_character_string);
9040 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9041 __ add(sp, sp, Operand(3 * kPointerSize));
9042 __ Ret();
9043
9044 // r2: result string length.
9045 // r3: two characters combined into halfword in little endian byte order.
9046 __ bind(&make_two_character_string);
9047 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
9048 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9049 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9050 __ add(sp, sp, Operand(3 * kPointerSize));
9051 __ Ret();
9052
9053 __ bind(&result_longer_than_two);
9054
ager@chromium.org5c838252010-02-19 08:53:10 +00009055 // Allocate the result.
9056 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
9057
9058 // r0: result string.
9059 // r2: result string length.
9060 // r5: string.
9061 // r6: from offset (smi)
9062 // Locate first character of result.
9063 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9064 // Locate 'from' character of string.
9065 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9066 __ add(r5, r5, Operand(r6, ASR, 1));
9067
9068 // r0: result string.
9069 // r1: first character of result string.
9070 // r2: result string length.
9071 // r5: first character of sub string to copy.
9072 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
9073 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9074 COPY_ASCII | DEST_ALWAYS_ALIGNED);
9075 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9076 __ add(sp, sp, Operand(3 * kPointerSize));
9077 __ Ret();
9078
9079 __ bind(&non_ascii_flat);
9080 // r2: result string length.
9081 // r5: string.
9082 // r6: from offset (smi)
9083 // Check for flat two byte string.
9084
9085 // Allocate the result.
9086 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
9087
9088 // r0: result string.
9089 // r2: result string length.
9090 // r5: string.
9091 // Locate first character of result.
9092 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9093 // Locate 'from' character of string.
9094 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9095 // As "from" is a smi it is 2 times the value which matches the size of a two
9096 // byte character.
9097 __ add(r5, r5, Operand(r6));
9098
9099 // r0: result string.
9100 // r1: first character of result.
9101 // r2: result length.
9102 // r5: first character of string to copy.
9103 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
9104 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9105 DEST_ALWAYS_ALIGNED);
9106 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9107 __ add(sp, sp, Operand(3 * kPointerSize));
9108 __ Ret();
9109
9110 // Just jump to runtime to create the sub string.
9111 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009112 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009113}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009114
9115
9116void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
9117 Register left,
9118 Register right,
9119 Register scratch1,
9120 Register scratch2,
9121 Register scratch3,
9122 Register scratch4) {
9123 Label compare_lengths;
9124 // Find minimum length and length difference.
9125 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
9126 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
9127 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
9128 Register length_delta = scratch3;
9129 __ mov(scratch1, scratch2, LeaveCC, gt);
9130 Register min_length = scratch1;
9131 __ tst(min_length, Operand(min_length));
9132 __ b(eq, &compare_lengths);
9133
9134 // Setup registers so that we only need to increment one register
9135 // in the loop.
9136 __ add(scratch2, min_length,
9137 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9138 __ add(left, left, Operand(scratch2));
9139 __ add(right, right, Operand(scratch2));
9140 // Registers left and right points to the min_length character of strings.
9141 __ rsb(min_length, min_length, Operand(-1));
9142 Register index = min_length;
9143 // Index starts at -min_length.
9144
9145 {
9146 // Compare loop.
9147 Label loop;
9148 __ bind(&loop);
9149 // Compare characters.
9150 __ add(index, index, Operand(1), SetCC);
9151 __ ldrb(scratch2, MemOperand(left, index), ne);
9152 __ ldrb(scratch4, MemOperand(right, index), ne);
9153 // Skip to compare lengths with eq condition true.
9154 __ b(eq, &compare_lengths);
9155 __ cmp(scratch2, scratch4);
9156 __ b(eq, &loop);
9157 // Fallthrough with eq condition false.
9158 }
9159 // Compare lengths - strings up to min-length are equal.
9160 __ bind(&compare_lengths);
9161 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
9162 // Use zero length_delta as result.
9163 __ mov(r0, Operand(length_delta), SetCC, eq);
9164 // Fall through to here if characters compare not-equal.
9165 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
9166 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
9167 __ Ret();
9168}
9169
9170
9171void StringCompareStub::Generate(MacroAssembler* masm) {
9172 Label runtime;
9173
9174 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00009175 // sp[0]: right string
9176 // sp[4]: left string
9177 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
9178 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009179
9180 Label not_same;
9181 __ cmp(r0, r1);
9182 __ b(ne, &not_same);
9183 ASSERT_EQ(0, EQUAL);
9184 ASSERT_EQ(0, kSmiTag);
9185 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
9186 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
9187 __ add(sp, sp, Operand(2 * kPointerSize));
9188 __ Ret();
9189
9190 __ bind(&not_same);
9191
9192 // Check that both objects are sequential ascii strings.
9193 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
9194
9195 // Compare flat ascii strings natively. Remove arguments from stack first.
9196 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
9197 __ add(sp, sp, Operand(2 * kPointerSize));
9198 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
9199
9200 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
9201 // tagged as a small integer.
9202 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009203 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009204}
9205
9206
ager@chromium.org5c838252010-02-19 08:53:10 +00009207void StringAddStub::Generate(MacroAssembler* masm) {
9208 Label string_add_runtime;
9209 // Stack on entry:
9210 // sp[0]: second argument.
9211 // sp[4]: first argument.
9212
9213 // Load the two arguments.
9214 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
9215 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
9216
9217 // Make sure that both arguments are strings if not known in advance.
9218 if (string_check_) {
9219 ASSERT_EQ(0, kSmiTag);
9220 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
9221 // Load instance types.
9222 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9223 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9224 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9225 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9226 ASSERT_EQ(0, kStringTag);
9227 // If either is not a string, go to runtime.
9228 __ tst(r4, Operand(kIsNotStringMask));
9229 __ tst(r5, Operand(kIsNotStringMask), eq);
9230 __ b(ne, &string_add_runtime);
9231 }
9232
9233 // Both arguments are strings.
9234 // r0: first string
9235 // r1: second string
9236 // r4: first string instance type (if string_check_)
9237 // r5: second string instance type (if string_check_)
9238 {
9239 Label strings_not_empty;
9240 // Check if either of the strings are empty. In that case return the other.
9241 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
9242 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00009243 __ cmp(r2, Operand(0)); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +00009244 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +00009245 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +00009246 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
9247
9248 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9249 __ add(sp, sp, Operand(2 * kPointerSize));
9250 __ Ret();
9251
9252 __ bind(&strings_not_empty);
9253 }
9254
9255 // Both strings are non-empty.
9256 // r0: first string
9257 // r1: second string
9258 // r2: length of first string
9259 // r3: length of second string
9260 // r4: first string instance type (if string_check_)
9261 // r5: second string instance type (if string_check_)
9262 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009263 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00009264 // Adding two lengths can't overflow.
9265 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
9266 __ add(r6, r2, Operand(r3));
9267 // Use the runtime system when adding two one character strings, as it
9268 // contains optimizations for this specific case using the symbol table.
9269 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009270 __ b(ne, &longer_than_two);
9271
9272 // Check that both strings are non-external ascii strings.
9273 if (!string_check_) {
9274 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9275 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9276 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9277 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9278 }
9279 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
9280 &string_add_runtime);
9281
9282 // Get the two characters forming the sub string.
9283 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9284 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
9285
9286 // Try to lookup two character string in symbol table. If it is not found
9287 // just allocate a new one.
9288 Label make_two_character_string;
9289 GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9,
9290 &make_two_character_string);
9291 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9292 __ add(sp, sp, Operand(2 * kPointerSize));
9293 __ Ret();
9294
9295 __ bind(&make_two_character_string);
9296 // Resulting string has length 2 and first chars of two strings
9297 // are combined into single halfword in r2 register.
9298 // So we can fill resulting string without two loops by a single
9299 // halfword store instruction (which assumes that processor is
9300 // in a little endian mode)
9301 __ mov(r6, Operand(2));
9302 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
9303 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9304 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9305 __ add(sp, sp, Operand(2 * kPointerSize));
9306 __ Ret();
9307
9308 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00009309 // Check if resulting string will be flat.
9310 __ cmp(r6, Operand(String::kMinNonFlatLength));
9311 __ b(lt, &string_add_flat_result);
9312 // Handle exceptionally long strings in the runtime system.
9313 ASSERT((String::kMaxLength & 0x80000000) == 0);
9314 ASSERT(IsPowerOf2(String::kMaxLength + 1));
9315 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
9316 __ cmp(r6, Operand(String::kMaxLength + 1));
9317 __ b(hs, &string_add_runtime);
9318
9319 // If result is not supposed to be flat, allocate a cons string object.
9320 // If both strings are ascii the result is an ascii cons string.
9321 if (!string_check_) {
9322 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9323 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9324 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9325 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9326 }
9327 Label non_ascii, allocated;
9328 ASSERT_EQ(0, kTwoByteStringTag);
9329 __ tst(r4, Operand(kStringEncodingMask));
9330 __ tst(r5, Operand(kStringEncodingMask), ne);
9331 __ b(eq, &non_ascii);
9332
9333 // Allocate an ASCII cons string.
9334 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
9335 __ bind(&allocated);
9336 // Fill the fields of the cons string.
9337 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
9338 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
9339 __ mov(r0, Operand(r7));
9340 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9341 __ add(sp, sp, Operand(2 * kPointerSize));
9342 __ Ret();
9343
9344 __ bind(&non_ascii);
9345 // Allocate a two byte cons string.
9346 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
9347 __ jmp(&allocated);
9348
9349 // Handle creating a flat result. First check that both strings are
9350 // sequential and that they have the same encoding.
9351 // r0: first string
9352 // r1: second string
9353 // r2: length of first string
9354 // r3: length of second string
9355 // r4: first string instance type (if string_check_)
9356 // r5: second string instance type (if string_check_)
9357 // r6: sum of lengths.
9358 __ bind(&string_add_flat_result);
9359 if (!string_check_) {
9360 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9361 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9362 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9363 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9364 }
9365 // Check that both strings are sequential.
9366 ASSERT_EQ(0, kSeqStringTag);
9367 __ tst(r4, Operand(kStringRepresentationMask));
9368 __ tst(r5, Operand(kStringRepresentationMask), eq);
9369 __ b(ne, &string_add_runtime);
9370 // Now check if both strings have the same encoding (ASCII/Two-byte).
9371 // r0: first string.
9372 // r1: second string.
9373 // r2: length of first string.
9374 // r3: length of second string.
9375 // r6: sum of lengths..
9376 Label non_ascii_string_add_flat_result;
9377 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
9378 __ eor(r7, r4, Operand(r5));
9379 __ tst(r7, Operand(kStringEncodingMask));
9380 __ b(ne, &string_add_runtime);
9381 // And see if it's ASCII or two-byte.
9382 __ tst(r4, Operand(kStringEncodingMask));
9383 __ b(eq, &non_ascii_string_add_flat_result);
9384
9385 // Both strings are sequential ASCII strings. We also know that they are
9386 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009387 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00009388 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
9389 // Locate first character of result.
9390 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9391 // Locate first character of first argument.
9392 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9393 // r0: first character of first string.
9394 // r1: second string.
9395 // r2: length of first string.
9396 // r3: length of second string.
9397 // r6: first character of result.
9398 // r7: result string.
9399 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
9400
9401 // Load second argument and locate first character.
9402 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9403 // r1: first character of second string.
9404 // r3: length of second string.
9405 // r6: next character of result.
9406 // r7: result string.
9407 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
9408 __ mov(r0, Operand(r7));
9409 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9410 __ add(sp, sp, Operand(2 * kPointerSize));
9411 __ Ret();
9412
9413 __ bind(&non_ascii_string_add_flat_result);
9414 // Both strings are sequential two byte strings.
9415 // r0: first string.
9416 // r1: second string.
9417 // r2: length of first string.
9418 // r3: length of second string.
9419 // r6: sum of length of strings.
9420 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
9421 // r0: first string.
9422 // r1: second string.
9423 // r2: length of first string.
9424 // r3: length of second string.
9425 // r7: result string.
9426
9427 // Locate first character of result.
9428 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9429 // Locate first character of first argument.
9430 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9431
9432 // r0: first character of first string.
9433 // r1: second string.
9434 // r2: length of first string.
9435 // r3: length of second string.
9436 // r6: first character of result.
9437 // r7: result string.
9438 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
9439
9440 // Locate first character of second argument.
9441 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9442
9443 // r1: first character of second string.
9444 // r3: length of second string.
9445 // r6: next character of result (after copy of first string).
9446 // r7: result string.
9447 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
9448
9449 __ mov(r0, Operand(r7));
9450 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9451 __ add(sp, sp, Operand(2 * kPointerSize));
9452 __ Ret();
9453
9454 // Just jump to runtime to add the two strings.
9455 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009456 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009457}
9458
9459
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009460#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009461
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009462} } // namespace v8::internal