blob: 291a763fb994466796b9e44f91721e2f36f48bbd [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
ager@chromium.org357bf652010-04-12 11:30:10 +00001011void CodeGenerator::VirtualFrameSmiOperation(Token::Value op,
1012 Handle<Object> value,
1013 bool reversed,
1014 OverwriteMode mode) {
1015 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
ager@chromium.org7c537e22008-10-16 08:43:32 +00001235void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001236 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001237 bool reversed,
1238 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001239 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 // NOTE: This is an attempt to inline (a bit) more of the code for
1241 // some possible smi operations (like + and -) when (at least) one
1242 // of the operands is a literal smi. With this optimization, the
1243 // performance of the system is increased by ~15%, and the generated
1244 // code size is increased by ~1% (measured on a combination of
1245 // different benchmarks).
1246
mads.s.ager31e71382008-08-13 09:32:07 +00001247 // sp[0] : operand
1248
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001249 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001251 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001252 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001254 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 switch (op) {
1256 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001257 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001258 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001260 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001261 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001263 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001264 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 break;
1266 }
1267
1268 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001269 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001270 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
ager@chromium.orge2902be2009-06-08 12:21:35 +00001272 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001273 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001274 } else {
1275 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 }
ager@chromium.orge2902be2009-06-08 12:21:35 +00001277 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001278 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001279 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001280 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001281 break;
1282 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001284
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001285 case Token::BIT_OR:
1286 case Token::BIT_XOR:
1287 case Token::BIT_AND: {
1288 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001289 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001290 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001291 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001292 switch (op) {
1293 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
1294 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
1295 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
1296 default: UNREACHABLE();
1297 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001298 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001299 break;
1300 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001302 case Token::SHL:
1303 case Token::SHR:
1304 case Token::SAR: {
1305 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001306 something_to_inline = false;
1307 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001308 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001309 int shift_value = int_value & 0x1f; // least significant 5 bits
1310 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001311 new DeferredInlineSmiOperation(op, shift_value, false, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001312 __ tst(r0, Operand(kSmiTagMask));
1313 deferred->Branch(ne);
1314 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
1315 switch (op) {
1316 case Token::SHL: {
1317 if (shift_value != 0) {
1318 __ mov(r2, Operand(r2, LSL, shift_value));
1319 }
1320 // check that the *unsigned* result fits in a smi
1321 __ add(r3, r2, Operand(0x40000000), SetCC);
1322 deferred->Branch(mi);
1323 break;
1324 }
1325 case Token::SHR: {
1326 // LSR by immediate 0 means shifting 32 bits.
1327 if (shift_value != 0) {
1328 __ mov(r2, Operand(r2, LSR, shift_value));
1329 }
1330 // check that the *unsigned* result fits in a smi
1331 // neither of the two high-order bits can be set:
1332 // - 0x80000000: high bit would be lost when smi tagging
1333 // - 0x40000000: this number would convert to negative when
1334 // smi tagging these two cases can only happen with shifts
1335 // by 0 or 1 when handed a valid smi
1336 __ and_(r3, r2, Operand(0xc0000000), SetCC);
1337 deferred->Branch(ne);
1338 break;
1339 }
1340 case Token::SAR: {
1341 if (shift_value != 0) {
1342 // ASR by immediate 0 means shifting 32 bits.
1343 __ mov(r2, Operand(r2, ASR, shift_value));
1344 }
1345 break;
1346 }
1347 default: UNREACHABLE();
1348 }
1349 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1350 deferred->BindExit();
1351 break;
1352 }
1353
1354 case Token::MOD: {
1355 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1356 something_to_inline = false;
1357 break;
1358 }
1359 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001360 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001361 unsigned mask = (0x80000000u | kSmiTagMask);
1362 __ tst(r0, Operand(mask));
1363 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1364 mask = (int_value << kSmiTagSize) - 1;
1365 __ and_(r0, r0, Operand(mask));
1366 deferred->BindExit();
1367 break;
1368 }
1369
1370 case Token::MUL: {
1371 if (!IsEasyToMultiplyBy(int_value)) {
1372 something_to_inline = false;
1373 break;
1374 }
1375 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001376 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001377 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1378 max_smi_that_wont_overflow <<= kSmiTagSize;
1379 unsigned mask = 0x80000000u;
1380 while ((mask & max_smi_that_wont_overflow) == 0) {
1381 mask |= mask >> 1;
1382 }
1383 mask |= kSmiTagMask;
1384 // This does a single mask that checks for a too high value in a
1385 // conservative way and for a non-Smi. It also filters out negative
1386 // numbers, unfortunately, but since this code is inline we prefer
1387 // brevity to comprehensiveness.
1388 __ tst(r0, Operand(mask));
1389 deferred->Branch(ne);
1390 MultiplyByKnownInt(masm_, r0, r0, int_value);
1391 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 break;
1393 }
1394
1395 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001396 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 break;
1398 }
1399
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001400 if (!something_to_inline) {
1401 if (!reversed) {
1402 frame_->EmitPush(r0);
1403 __ mov(r0, Operand(value));
1404 frame_->EmitPush(r0);
1405 GenericBinaryOperation(op, mode, int_value);
1406 } else {
1407 __ mov(ip, Operand(value));
1408 frame_->EmitPush(ip);
1409 frame_->EmitPush(r0);
1410 GenericBinaryOperation(op, mode, kUnknownIntValue);
1411 }
1412 }
1413
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415}
1416
1417
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001418void CodeGenerator::Comparison(Condition cc,
1419 Expression* left,
1420 Expression* right,
1421 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001422 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001423
ager@chromium.org357bf652010-04-12 11:30:10 +00001424 if (left != NULL) Load(left);
1425 if (right != NULL) Load(right);
1426
mads.s.ager31e71382008-08-13 09:32:07 +00001427 // sp[0] : y
1428 // sp[1] : x
1429 // result : cc register
1430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 // Strict only makes sense for equality comparisons.
1432 ASSERT(!strict || cc == eq);
1433
ager@chromium.org357bf652010-04-12 11:30:10 +00001434 Register lhs;
1435 Register rhs;
1436
1437 // We load the top two stack positions into registers chosen by the virtual
1438 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001439 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1440 if (cc == gt || cc == le) {
1441 cc = ReverseCondition(cc);
ager@chromium.org357bf652010-04-12 11:30:10 +00001442 lhs = frame_->PopToRegister();
1443 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001444 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001445 rhs = frame_->PopToRegister();
1446 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001447 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001448
1449 ASSERT(rhs.is(r0) || rhs.is(r1));
1450 ASSERT(lhs.is(r0) || lhs.is(r1));
1451
1452 // Now we have the two sides in r0 and r1. We flush any other registers
1453 // because the stub doesn't know about register allocation.
1454 frame_->SpillAll();
1455 Register scratch = VirtualFrame::scratch0();
1456 __ orr(scratch, lhs, Operand(rhs));
1457 __ tst(scratch, Operand(kSmiTagMask));
1458 JumpTarget smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001461 // Perform non-smi comparison by stub.
1462 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1463 // We call with 0 args because there are 0 on the stack.
ager@chromium.org357bf652010-04-12 11:30:10 +00001464 if (!rhs.is(r0)) {
1465 __ Swap(rhs, lhs, ip);
1466 }
1467
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001468 CompareStub stub(cc, strict);
1469 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001470 __ cmp(r0, Operand(0));
ager@chromium.org357bf652010-04-12 11:30:10 +00001471 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001472 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001474 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001476 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001478 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 cc_reg_ = cc;
1480}
1481
1482
mads.s.ager31e71382008-08-13 09:32:07 +00001483// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001484void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001485 CallFunctionFlags flags,
1486 int position) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001487 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001489 int arg_count = args->length();
1490 for (int i = 0; i < arg_count; i++) {
1491 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001492 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493
kasper.lund7276f142008-07-30 08:49:36 +00001494 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001495 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496
kasper.lund7276f142008-07-30 08:49:36 +00001497 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001498 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001499 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001500 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501
1502 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001503 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001504 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505}
1506
1507
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001508void CodeGenerator::CallApplyLazy(Expression* applicand,
1509 Expression* receiver,
1510 VariableProxy* arguments,
1511 int position) {
1512 // An optimized implementation of expressions of the form
1513 // x.apply(y, arguments).
1514 // If the arguments object of the scope has not been allocated,
1515 // and x.apply is Function.prototype.apply, this optimization
1516 // just copies y and the arguments of the current function on the
1517 // stack, as receiver and arguments, and calls x.
1518 // In the implementation comments, we call x the applicand
1519 // and y the receiver.
1520 VirtualFrame::SpilledScope spilled_scope(frame_);
1521
1522 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1523 ASSERT(arguments->IsArguments());
1524
1525 // Load applicand.apply onto the stack. This will usually
1526 // give us a megamorphic load site. Not super, but it works.
1527 LoadAndSpill(applicand);
1528 Handle<String> name = Factory::LookupAsciiSymbol("apply");
1529 __ mov(r2, Operand(name));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001530 __ ldr(r0, MemOperand(sp, 0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001531 frame_->CallLoadIC(RelocInfo::CODE_TARGET);
1532 frame_->EmitPush(r0);
1533
1534 // Load the receiver and the existing arguments object onto the
1535 // expression stack. Avoid allocating the arguments object here.
1536 LoadAndSpill(receiver);
1537 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1538
1539 // Emit the source position information after having loaded the
1540 // receiver and the arguments.
1541 CodeForSourcePosition(position);
1542 // Contents of the stack at this point:
1543 // sp[0]: arguments object of the current function or the hole.
1544 // sp[1]: receiver
1545 // sp[2]: applicand.apply
1546 // sp[3]: applicand.
1547
1548 // Check if the arguments object has been lazily allocated
1549 // already. If so, just use that instead of copying the arguments
1550 // from the stack. This also deals with cases where a local variable
1551 // named 'arguments' has been introduced.
1552 __ ldr(r0, MemOperand(sp, 0));
1553
1554 Label slow, done;
1555 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1556 __ cmp(ip, r0);
1557 __ b(ne, &slow);
1558
1559 Label build_args;
1560 // Get rid of the arguments object probe.
1561 frame_->Drop();
1562 // Stack now has 3 elements on it.
1563 // Contents of stack at this point:
1564 // sp[0]: receiver
1565 // sp[1]: applicand.apply
1566 // sp[2]: applicand.
1567
1568 // Check that the receiver really is a JavaScript object.
1569 __ ldr(r0, MemOperand(sp, 0));
1570 __ BranchOnSmi(r0, &build_args);
1571 // We allow all JSObjects including JSFunctions. As long as
1572 // JS_FUNCTION_TYPE is the last instance type and it is right
1573 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1574 // bound.
1575 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1576 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1577 __ CompareObjectType(r0, r1, r2, FIRST_JS_OBJECT_TYPE);
1578 __ b(lt, &build_args);
1579
1580 // Check that applicand.apply is Function.prototype.apply.
1581 __ ldr(r0, MemOperand(sp, kPointerSize));
1582 __ BranchOnSmi(r0, &build_args);
1583 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1584 __ b(ne, &build_args);
1585 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
1586 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
1587 __ ldr(r1, FieldMemOperand(r0, SharedFunctionInfo::kCodeOffset));
1588 __ cmp(r1, Operand(apply_code));
1589 __ b(ne, &build_args);
1590
1591 // Check that applicand is a function.
1592 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1593 __ BranchOnSmi(r1, &build_args);
1594 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1595 __ b(ne, &build_args);
1596
1597 // Copy the arguments to this function possibly from the
1598 // adaptor frame below it.
1599 Label invoke, adapted;
1600 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1601 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1602 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1603 __ b(eq, &adapted);
1604
1605 // No arguments adaptor frame. Copy fixed number of arguments.
1606 __ mov(r0, Operand(scope()->num_parameters()));
1607 for (int i = 0; i < scope()->num_parameters(); i++) {
1608 __ ldr(r2, frame_->ParameterAt(i));
1609 __ push(r2);
1610 }
1611 __ jmp(&invoke);
1612
1613 // Arguments adaptor frame present. Copy arguments from there, but
1614 // avoid copying too many arguments to avoid stack overflows.
1615 __ bind(&adapted);
1616 static const uint32_t kArgumentsLimit = 1 * KB;
1617 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1618 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1619 __ mov(r3, r0);
1620 __ cmp(r0, Operand(kArgumentsLimit));
1621 __ b(gt, &build_args);
1622
1623 // Loop through the arguments pushing them onto the execution
1624 // stack. We don't inform the virtual frame of the push, so we don't
1625 // have to worry about getting rid of the elements from the virtual
1626 // frame.
1627 Label loop;
1628 // r3 is a small non-negative integer, due to the test above.
1629 __ cmp(r3, Operand(0));
1630 __ b(eq, &invoke);
1631 // Compute the address of the first argument.
1632 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1633 __ add(r2, r2, Operand(kPointerSize));
1634 __ bind(&loop);
1635 // Post-decrement argument address by kPointerSize on each iteration.
1636 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1637 __ push(r4);
1638 __ sub(r3, r3, Operand(1), SetCC);
1639 __ b(gt, &loop);
1640
1641 // Invoke the function.
1642 __ bind(&invoke);
1643 ParameterCount actual(r0);
1644 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1645 // Drop applicand.apply and applicand from the stack, and push
1646 // the result of the function call, but leave the spilled frame
1647 // unchanged, with 3 elements, so it is correct when we compile the
1648 // slow-case code.
1649 __ add(sp, sp, Operand(2 * kPointerSize));
1650 __ push(r0);
1651 // Stack now has 1 element:
1652 // sp[0]: result
1653 __ jmp(&done);
1654
1655 // Slow-case: Allocate the arguments object since we know it isn't
1656 // there, and fall-through to the slow-case where we call
1657 // applicand.apply.
1658 __ bind(&build_args);
1659 // Stack now has 3 elements, because we have jumped from where:
1660 // sp[0]: receiver
1661 // sp[1]: applicand.apply
1662 // sp[2]: applicand.
1663 StoreArgumentsObject(false);
1664
1665 // Stack and frame now have 4 elements.
1666 __ bind(&slow);
1667
1668 // Generic computation of x.apply(y, args) with no special optimization.
1669 // Flip applicand.apply and applicand on the stack, so
1670 // applicand looks like the receiver of the applicand.apply call.
1671 // Then process it as a normal function call.
1672 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1673 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1674 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1675 __ str(r1, MemOperand(sp, 3 * kPointerSize));
1676
1677 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1678 frame_->CallStub(&call_function, 3);
1679 // The function and its two arguments have been dropped.
1680 frame_->Drop(); // Drop the receiver as well.
1681 frame_->EmitPush(r0);
1682 // Stack now has 1 element:
1683 // sp[0]: result
1684 __ bind(&done);
1685
1686 // Restore the context register after a call.
1687 __ ldr(cp, frame_->Context());
1688}
1689
1690
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001691void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001692 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 ASSERT(has_cc());
1694 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001695 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 cc_reg_ = al;
1697}
1698
1699
ager@chromium.org7c537e22008-10-16 08:43:32 +00001700void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001701 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001702 Comment cmnt(masm_, "[ check stack");
1703 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1704 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1705 // the implicit 8 byte offset that always applies to operations with pc and
1706 // gives a return address 12 bytes down.
1707 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1708 masm_->cmp(sp, Operand(ip));
1709 StackCheckStub stub;
1710 // Call the stub if lower.
1711 masm_->mov(pc,
1712 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1713 RelocInfo::CODE_TARGET),
1714 LeaveCC,
1715 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716}
1717
1718
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001719void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1720#ifdef DEBUG
1721 int original_height = frame_->height();
1722#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001723 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001724 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1725 VisitAndSpill(statements->at(i));
1726 }
1727 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1728}
1729
1730
ager@chromium.org7c537e22008-10-16 08:43:32 +00001731void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001732#ifdef DEBUG
1733 int original_height = frame_->height();
1734#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001735 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001737 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001738 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 VisitStatementsAndSpill(node->statements());
1740 if (node->break_target()->is_linked()) {
1741 node->break_target()->Bind();
1742 }
1743 node->break_target()->Unuse();
1744 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745}
1746
1747
ager@chromium.org7c537e22008-10-16 08:43:32 +00001748void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001749 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001750 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001751 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001753 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001754 frame_->EmitPush(r0);
1755 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001756 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757}
1758
1759
ager@chromium.org7c537e22008-10-16 08:43:32 +00001760void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001761#ifdef DEBUG
1762 int original_height = frame_->height();
1763#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001764 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 Comment cmnt(masm_, "[ Declaration");
1766 Variable* var = node->proxy()->var();
1767 ASSERT(var != NULL); // must have been resolved
1768 Slot* slot = var->slot();
1769
1770 // If it was not possible to allocate the variable at compile time,
1771 // we need to "declare" it at runtime to make sure it actually
1772 // exists in the local context.
1773 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1774 // Variables with a "LOOKUP" slot were introduced as non-locals
1775 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001776 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001779 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001780 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 // Declaration nodes are always declared in only two modes.
1782 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1783 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001784 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // Push initial value, if any.
1787 // Note: For variables we must not push an initial value (such as
1788 // 'undefined') because we may have a (legal) redeclaration and we
1789 // must not destroy the current value.
1790 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001791 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001796 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001799 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001800 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 return;
1803 }
1804
1805 ASSERT(!var->is_global());
1806
1807 // If we have a function or a constant, we need to initialize the variable.
1808 Expression* val = NULL;
1809 if (node->mode() == Variable::CONST) {
1810 val = new Literal(Factory::the_hole_value());
1811 } else {
1812 val = node->fun(); // NULL if we don't have a function
1813 }
1814
1815 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001816 {
1817 // Set initial value.
1818 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001820 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001821 }
1822 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826}
1827
1828
ager@chromium.org7c537e22008-10-16 08:43:32 +00001829void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830#ifdef DEBUG
1831 int original_height = frame_->height();
1832#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001833 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 Expression* expression = node->expression();
1837 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001838 LoadAndSpill(expression);
1839 frame_->Drop();
1840 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841}
1842
1843
ager@chromium.org7c537e22008-10-16 08:43:32 +00001844void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845#ifdef DEBUG
1846 int original_height = frame_->height();
1847#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001848 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001850 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853}
1854
1855
ager@chromium.org7c537e22008-10-16 08:43:32 +00001856void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001857#ifdef DEBUG
1858 int original_height = frame_->height();
1859#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001860 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 // Generate different code depending on which parts of the if statement
1863 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 bool has_then_stm = node->HasThenStatement();
1865 bool has_else_stm = node->HasElseStatement();
1866
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001869 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001871 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001872 JumpTarget then;
1873 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001875 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 if (frame_ != NULL) {
1877 Branch(false, &else_);
1878 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 if (frame_ != NULL || then.is_linked()) {
1881 then.Bind();
1882 VisitAndSpill(node->then_statement());
1883 }
1884 if (frame_ != NULL) {
1885 exit.Jump();
1886 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 if (else_.is_linked()) {
1889 else_.Bind();
1890 VisitAndSpill(node->else_statement());
1891 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
1893 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001894 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001896 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001898 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001899 if (frame_ != NULL) {
1900 Branch(false, &exit);
1901 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001903 if (frame_ != NULL || then.is_linked()) {
1904 then.Bind();
1905 VisitAndSpill(node->then_statement());
1906 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907
1908 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001909 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001911 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001913 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 if (frame_ != NULL) {
1915 Branch(true, &exit);
1916 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001918 if (frame_ != NULL || else_.is_linked()) {
1919 else_.Bind();
1920 VisitAndSpill(node->else_statement());
1921 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922
1923 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001924 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 ASSERT(!has_then_stm && !has_else_stm);
1926 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001927 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 if (frame_ != NULL) {
1929 if (has_cc()) {
1930 cc_reg_ = al;
1931 } else {
1932 frame_->Drop();
1933 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 }
1935 }
1936
1937 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 if (exit.is_linked()) {
1939 exit.Bind();
1940 }
1941 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942}
1943
1944
ager@chromium.org7c537e22008-10-16 08:43:32 +00001945void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001946 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001948 CodeForStatementPosition(node);
1949 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950}
1951
1952
ager@chromium.org7c537e22008-10-16 08:43:32 +00001953void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001954 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956 CodeForStatementPosition(node);
1957 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958}
1959
1960
ager@chromium.org7c537e22008-10-16 08:43:32 +00001961void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001962 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001964
ager@chromium.org4af710e2009-09-15 12:20:11 +00001965 CodeForStatementPosition(node);
1966 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 frame_->EmitPop(r0);
1969 function_return_.Jump();
1970 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001971 // Pop the result from the frame and prepare the frame for
1972 // returning thus making it easier to merge.
1973 frame_->EmitPop(r0);
1974 frame_->PrepareForReturn();
1975
1976 function_return_.Jump();
1977 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978}
1979
1980
ager@chromium.org7c537e22008-10-16 08:43:32 +00001981void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982#ifdef DEBUG
1983 int original_height = frame_->height();
1984#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001985 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001987 CodeForStatementPosition(node);
1988 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001989 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001991 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001993 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001994#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001995 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001996 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001997 verified_true.Branch(eq);
1998 __ stop("PushContext: r0 is expected to be the same as cp");
1999 verified_true.Bind();
2000#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002002 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002003 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004}
2005
2006
ager@chromium.org7c537e22008-10-16 08:43:32 +00002007void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002008#ifdef DEBUG
2009 int original_height = frame_->height();
2010#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002011 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 // Pop context.
2015 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2016 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002017 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019}
2020
2021
ager@chromium.org7c537e22008-10-16 08:43:32 +00002022void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023#ifdef DEBUG
2024 int original_height = frame_->height();
2025#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002026 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002029 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002031 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002032
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002033 JumpTarget next_test;
2034 JumpTarget fall_through;
2035 JumpTarget default_entry;
2036 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 ZoneList<CaseClause*>* cases = node->cases();
2038 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
2041 for (int i = 0; i < length; i++) {
2042 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 // Remember the default clause and compile it at the end.
2045 default_clause = clause;
2046 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 }
2048
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002049 Comment cmnt(masm_, "[ Case clause");
2050 // Compile the test.
2051 next_test.Bind();
2052 next_test.Unuse();
2053 // Duplicate TOS.
2054 __ ldr(r0, frame_->Top());
2055 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002056 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002057 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 // Before entering the body from the test, remove the switch value from
2060 // the stack.
2061 frame_->Drop();
2062
2063 // Label the body so that fall through is enabled.
2064 if (i > 0 && cases->at(i - 1)->is_default()) {
2065 default_exit.Bind();
2066 } else {
2067 fall_through.Bind();
2068 fall_through.Unuse();
2069 }
2070 VisitStatementsAndSpill(clause->statements());
2071
2072 // If control flow can fall through from the body, jump to the next body
2073 // or the end of the statement.
2074 if (frame_ != NULL) {
2075 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2076 default_entry.Jump();
2077 } else {
2078 fall_through.Jump();
2079 }
2080 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 }
2082
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 // The final "test" removes the switch value.
2084 next_test.Bind();
2085 frame_->Drop();
2086
2087 // If there is a default clause, compile it.
2088 if (default_clause != NULL) {
2089 Comment cmnt(masm_, "[ Default clause");
2090 default_entry.Bind();
2091 VisitStatementsAndSpill(default_clause->statements());
2092 // If control flow can fall out of the default and there is a case after
2093 // it, jup to that case's body.
2094 if (frame_ != NULL && default_exit.is_bound()) {
2095 default_exit.Jump();
2096 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002097 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 if (fall_through.is_linked()) {
2100 fall_through.Bind();
2101 }
2102
2103 if (node->break_target()->is_linked()) {
2104 node->break_target()->Bind();
2105 }
2106 node->break_target()->Unuse();
2107 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108}
2109
2110
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002111void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112#ifdef DEBUG
2113 int original_height = frame_->height();
2114#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002115 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002116 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002117 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002118 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002119 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002120 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002122 // Label the top of the loop for the backward CFG edge. If the test
2123 // is always true we can use the continue target, and if the test is
2124 // always false there is no need.
2125 ConditionAnalysis info = AnalyzeCondition(node->cond());
2126 switch (info) {
2127 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002128 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002129 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002130 break;
2131 case ALWAYS_FALSE:
2132 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2133 break;
2134 case DONT_KNOW:
2135 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2136 body.Bind();
2137 break;
2138 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002140 CheckStack(); // TODO(1222600): ignore if body contains calls.
2141 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002143 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002144 switch (info) {
2145 case ALWAYS_TRUE:
2146 // If control can fall off the end of the body, jump back to the
2147 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002149 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002152 case ALWAYS_FALSE:
2153 // If we have a continue in the body, we only have to bind its
2154 // jump target.
2155 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002158 break;
2159 case DONT_KNOW:
2160 // We have to compile the test expression if it can be reached by
2161 // control flow falling out of the body or via continue.
2162 if (node->continue_target()->is_linked()) {
2163 node->continue_target()->Bind();
2164 }
2165 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002166 Comment cmnt(masm_, "[ DoWhileCondition");
2167 CodeForDoWhileConditionPosition(node);
2168 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002170 // A invalid frame here indicates that control did not
2171 // fall out of the test expression.
2172 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 }
2174 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 break;
2176 }
2177
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002178 if (node->break_target()->is_linked()) {
2179 node->break_target()->Bind();
2180 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002181 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002182 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2183}
2184
2185
2186void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2187#ifdef DEBUG
2188 int original_height = frame_->height();
2189#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002190 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002191 Comment cmnt(masm_, "[ WhileStatement");
2192 CodeForStatementPosition(node);
2193
2194 // If the test is never true and has no side effects there is no need
2195 // to compile the test or body.
2196 ConditionAnalysis info = AnalyzeCondition(node->cond());
2197 if (info == ALWAYS_FALSE) return;
2198
2199 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002200 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002201
2202 // Label the top of the loop with the continue target for the backward
2203 // CFG edge.
2204 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2205 node->continue_target()->Bind();
2206
2207 if (info == DONT_KNOW) {
2208 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002209 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002210 if (has_valid_frame()) {
2211 // A NULL frame indicates that control did not fall out of the
2212 // test expression.
2213 Branch(false, node->break_target());
2214 }
2215 if (has_valid_frame() || body.is_linked()) {
2216 body.Bind();
2217 }
2218 }
2219
2220 if (has_valid_frame()) {
2221 CheckStack(); // TODO(1222600): ignore if body contains calls.
2222 VisitAndSpill(node->body());
2223
2224 // If control flow can fall out of the body, jump back to the top.
2225 if (has_valid_frame()) {
2226 node->continue_target()->Jump();
2227 }
2228 }
2229 if (node->break_target()->is_linked()) {
2230 node->break_target()->Bind();
2231 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002232 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002233 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2234}
2235
2236
2237void CodeGenerator::VisitForStatement(ForStatement* node) {
2238#ifdef DEBUG
2239 int original_height = frame_->height();
2240#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002241 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002242 Comment cmnt(masm_, "[ ForStatement");
2243 CodeForStatementPosition(node);
2244 if (node->init() != NULL) {
2245 VisitAndSpill(node->init());
2246 }
2247
2248 // If the test is never true there is no need to compile the test or
2249 // body.
2250 ConditionAnalysis info = AnalyzeCondition(node->cond());
2251 if (info == ALWAYS_FALSE) return;
2252
2253 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002254 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002255
2256 // If there is no update statement, label the top of the loop with the
2257 // continue target, otherwise with the loop target.
2258 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2259 if (node->next() == NULL) {
2260 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2261 node->continue_target()->Bind();
2262 } else {
2263 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2264 loop.Bind();
2265 }
2266
2267 // If the test is always true, there is no need to compile it.
2268 if (info == DONT_KNOW) {
2269 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002270 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002271 if (has_valid_frame()) {
2272 Branch(false, node->break_target());
2273 }
2274 if (has_valid_frame() || body.is_linked()) {
2275 body.Bind();
2276 }
2277 }
2278
2279 if (has_valid_frame()) {
2280 CheckStack(); // TODO(1222600): ignore if body contains calls.
2281 VisitAndSpill(node->body());
2282
2283 if (node->next() == NULL) {
2284 // If there is no update statement and control flow can fall out
2285 // of the loop, jump directly to the continue label.
2286 if (has_valid_frame()) {
2287 node->continue_target()->Jump();
2288 }
2289 } else {
2290 // If there is an update statement and control flow can reach it
2291 // via falling out of the body of the loop or continuing, we
2292 // compile the update statement.
2293 if (node->continue_target()->is_linked()) {
2294 node->continue_target()->Bind();
2295 }
2296 if (has_valid_frame()) {
2297 // Record source position of the statement as this code which is
2298 // after the code for the body actually belongs to the loop
2299 // statement and not the body.
2300 CodeForStatementPosition(node);
2301 VisitAndSpill(node->next());
2302 loop.Jump();
2303 }
2304 }
2305 }
2306 if (node->break_target()->is_linked()) {
2307 node->break_target()->Bind();
2308 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002309 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311}
2312
2313
ager@chromium.org7c537e22008-10-16 08:43:32 +00002314void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315#ifdef DEBUG
2316 int original_height = frame_->height();
2317#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002318 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002322 JumpTarget primitive;
2323 JumpTarget jsobject;
2324 JumpTarget fixed_array;
2325 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2326 JumpTarget end_del_check;
2327 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328
2329 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002330 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331
2332 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2333 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002334 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002335 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2336 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002338 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2339 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341
2342 // Stack layout in body:
2343 // [iteration counter (Smi)]
2344 // [length of array]
2345 // [FixedArray]
2346 // [Map or 0]
2347 // [Object]
2348
2349 // Check if enumerable is already a JSObject
2350 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002352 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 primitive.Bind();
2356 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002357 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002361 // r0: value to be iterated over
2362 frame_->EmitPush(r0); // Push the object being iterated over.
2363
2364 // Check cache validity in generated code. This is a fast case for
2365 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2366 // guarantee cache validity, call the runtime system to check cache
2367 // validity or get the property names in a fixed array.
2368 JumpTarget call_runtime;
2369 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2370 JumpTarget check_prototype;
2371 JumpTarget use_cache;
2372 __ mov(r1, Operand(r0));
2373 loop.Bind();
2374 // Check that there are no elements.
2375 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2376 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2377 __ cmp(r2, r4);
2378 call_runtime.Branch(ne);
2379 // Check that instance descriptors are not empty so that we can
2380 // check for an enum cache. Leave the map in r3 for the subsequent
2381 // prototype load.
2382 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2383 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2384 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2385 __ cmp(r2, ip);
2386 call_runtime.Branch(eq);
2387 // Check that there in an enum cache in the non-empty instance
2388 // descriptors. This is the case if the next enumeration index
2389 // field does not contain a smi.
2390 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2391 __ tst(r2, Operand(kSmiTagMask));
2392 call_runtime.Branch(eq);
2393 // For all objects but the receiver, check that the cache is empty.
2394 // r4: empty fixed array root.
2395 __ cmp(r1, r0);
2396 check_prototype.Branch(eq);
2397 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2398 __ cmp(r2, r4);
2399 call_runtime.Branch(ne);
2400 check_prototype.Bind();
2401 // Load the prototype from the map and loop if non-null.
2402 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2403 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2404 __ cmp(r1, ip);
2405 loop.Branch(ne);
2406 // The enum cache is valid. Load the map of the object being
2407 // iterated over and use the cache for the iteration.
2408 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2409 use_cache.Jump();
2410
2411 call_runtime.Bind();
2412 // Call the runtime to get the property names for the object.
2413 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002414 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002416 // If we got a map from the runtime call, we can do a fast
2417 // modification check. Otherwise, we got a fixed array, and we have
2418 // to do a slow check.
2419 // r0: map or fixed array (result from call to
2420 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 __ mov(r2, Operand(r0));
2422 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002423 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2424 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002427 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002429 // r0: map (either the result from a call to
2430 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2431 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 __ mov(r1, Operand(r0));
2433 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2434 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2435 __ ldr(r2,
2436 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2437
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002438 frame_->EmitPush(r0); // map
2439 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002440 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002442 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002443 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 frame_->EmitPush(r0);
2445 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002447 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 frame_->EmitPush(r1); // insert 0 in place of Map
2450 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451
2452 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002453 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002456 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002457 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458
2459 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002461 // sp[0] : index
2462 // sp[1] : array/enum cache length
2463 // sp[2] : array or enum cache
2464 // sp[3] : 0 or map
2465 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 // Grab the current frame's height for the break and continue
2467 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002468 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2469 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 __ ldr(r0, frame_->ElementAt(0)); // load the current count
2472 __ ldr(r1, frame_->ElementAt(1)); // load the length
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002473 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002474 node->break_target()->Branch(hs);
2475
2476 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002479 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2481 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2482
2483 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 // Check if this (still) matches the map of the enumerable.
2486 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2489 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491
2492 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2494 frame_->EmitPush(r0);
2495 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002496 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002497 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498
2499 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002500 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2501 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002502 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 end_del_check.Bind();
2505 // Store the entry in the 'each' expression and take another spin in the
2506 // loop. r3: i'th entry of the enum cache (or string there of)
2507 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 { Reference each(this, node->each());
2509 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002510 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002511 __ ldr(r0, frame_->ElementAt(each.size()));
2512 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002513 each.SetValue(NOT_CONST_INIT);
2514 frame_->Drop(2);
2515 } else {
2516 // If the reference was to a slot we rely on the convenient property
2517 // that it doesn't matter whether a value (eg, r3 pushed above) is
2518 // right on top of or right underneath a zero-sized reference.
2519 each.SetValue(NOT_CONST_INIT);
2520 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002521 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 }
2523 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002524 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002526 VisitAndSpill(node->body());
2527
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002528 // Next. Reestablish a spilled frame in case we are coming here via
2529 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002530 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002531 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002532 frame_->EmitPop(r0);
2533 __ add(r0, r0, Operand(Smi::FromInt(1)));
2534 frame_->EmitPush(r0);
2535 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002537 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2538 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002540 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541
2542 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 exit.Bind();
2544 node->continue_target()->Unuse();
2545 node->break_target()->Unuse();
2546 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547}
2548
2549
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002550void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551#ifdef DEBUG
2552 int original_height = frame_->height();
2553#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002554 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002555 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002558 JumpTarget try_block;
2559 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
2565 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002566 Variable* catch_var = node->catch_var()->var();
2567 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2568 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569
2570 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 VisitStatementsAndSpill(node->catch_block()->statements());
2574 if (frame_ != NULL) {
2575 exit.Jump();
2576 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577
2578
2579 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2583 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002585 // Shadow the labels for all escapes from the try block, including
2586 // returns. During shadowing, the original label is hidden as the
2587 // LabelShadow and operations on the original actually affect the
2588 // shadowing label.
2589 //
2590 // We should probably try to unify the escaping labels and the return
2591 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592 int nof_escapes = node->escaping_targets()->length();
2593 List<ShadowTarget*> shadows(1 + nof_escapes);
2594
2595 // Add the shadow target for the function return.
2596 static const int kReturnShadowIndex = 0;
2597 shadows.Add(new ShadowTarget(&function_return_));
2598 bool function_return_was_shadowed = function_return_is_shadowed_;
2599 function_return_is_shadowed_ = true;
2600 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2601
2602 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605 }
2606
2607 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002608 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609
2610 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002611 // After shadowing stops, the original labels are unshadowed and the
2612 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002613 bool has_unlinks = false;
2614 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616 has_unlinks = has_unlinks || shadows[i]->is_linked();
2617 }
2618 function_return_is_shadowed_ = function_return_was_shadowed;
2619
2620 // Get an external reference to the handler address.
2621 ExternalReference handler_address(Top::k_handler_address);
2622
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 // If we can fall off the end of the try block, unlink from try chain.
2624 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002625 // The next handler address is on top of the frame. Unlink from
2626 // the handler list and drop the rest of this handler from the
2627 // frame.
2628 ASSERT(StackHandlerConstants::kNextOffset == 0);
2629 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630 __ mov(r3, Operand(handler_address));
2631 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002632 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002633 if (has_unlinks) {
2634 exit.Jump();
2635 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 }
2637
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002638 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 // jumped to. Deallocate each shadow target.
2640 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002642 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002643 shadows[i]->Bind();
2644 // Because we can be jumping here (to spilled code) from unspilled
2645 // code, we need to reestablish a spilled frame at this block.
2646 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 // Reload sp from the top handler, because some statements that we
2649 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002650 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002652 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002654 ASSERT(StackHandlerConstants::kNextOffset == 0);
2655 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002657 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2660 frame_->PrepareForReturn();
2661 }
2662 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 }
2664 }
2665
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002666 exit.Bind();
2667 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668}
2669
2670
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002671void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002672#ifdef DEBUG
2673 int original_height = frame_->height();
2674#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002675 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002676 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678
2679 // State: Used to keep track of reason for entering the finally
2680 // block. Should probably be extended to hold information for
2681 // break/continue from within the try block.
2682 enum { FALLING, THROWING, JUMPING };
2683
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002684 JumpTarget try_block;
2685 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 // In case of thrown exceptions, this is where we continue.
2691 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693
2694 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002695 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2698 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002700 // Shadow the labels for all escapes from the try block, including
2701 // returns. Shadowing hides the original label as the LabelShadow and
2702 // operations on the original actually affect the shadowing label.
2703 //
2704 // We should probably try to unify the escaping labels and the return
2705 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002706 int nof_escapes = node->escaping_targets()->length();
2707 List<ShadowTarget*> shadows(1 + nof_escapes);
2708
2709 // Add the shadow target for the function return.
2710 static const int kReturnShadowIndex = 0;
2711 shadows.Add(new ShadowTarget(&function_return_));
2712 bool function_return_was_shadowed = function_return_is_shadowed_;
2713 function_return_is_shadowed_ = true;
2714 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2715
2716 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002718 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719 }
2720
2721 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002722 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002724 // Stop the introduced shadowing and count the number of required unlinks.
2725 // After shadowing stops, the original labels are unshadowed and the
2726 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002728 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 shadows[i]->StopShadowing();
2730 if (shadows[i]->is_linked()) nof_unlinks++;
2731 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002732 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002734 // Get an external reference to the handler address.
2735 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002737 // If we can fall off the end of the try block, unlink from the try
2738 // chain and set the state on the frame to FALLING.
2739 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002740 // The next handler address is on top of the frame.
2741 ASSERT(StackHandlerConstants::kNextOffset == 0);
2742 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002743 __ mov(r3, Operand(handler_address));
2744 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002745 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002746
2747 // Fake a top of stack value (unneeded when FALLING) and set the
2748 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002749 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002750 frame_->EmitPush(r0);
2751 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2752 if (nof_unlinks > 0) {
2753 finally_block.Jump();
2754 }
2755 }
2756
2757 // Generate code to unlink and set the state for the (formerly)
2758 // shadowing targets that have been jumped to.
2759 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002761 // If we have come from the shadowed return, the return value is
2762 // in (a non-refcounted reference to) r0. We must preserve it
2763 // until it is pushed.
2764 //
2765 // Because we can be jumping here (to spilled code) from
2766 // unspilled code, we need to reestablish a spilled frame at
2767 // this block.
2768 shadows[i]->Bind();
2769 frame_->SpillAll();
2770
2771 // Reload sp from the top handler, because some statements that
2772 // we break from (eg, for...in) may have left stuff on the
2773 // stack.
2774 __ mov(r3, Operand(handler_address));
2775 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002776 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777
2778 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002779 // handler address is currently on top of the frame.
2780 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002781 frame_->EmitPop(r1);
2782 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002783 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784
2785 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002786 // If this label shadowed the function return, materialize the
2787 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002788 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002789 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002790 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002791 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002792 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 }
2794 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002795 if (--nof_unlinks > 0) {
2796 // If this is not the last unlink block, jump around the next.
2797 finally_block.Jump();
2798 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 }
2800 }
2801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002803 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002805 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002806 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002807
2808 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002809 // and the state - while evaluating the finally block.
2810 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002812 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002814 if (has_valid_frame()) {
2815 // Restore state and return value or faked TOS.
2816 frame_->EmitPop(r2);
2817 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 }
2819
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002820 // Generate code to jump to the right destination for all used
2821 // formerly shadowing targets. Deallocate each shadow target.
2822 for (int i = 0; i < shadows.length(); i++) {
2823 if (has_valid_frame() && shadows[i]->is_bound()) {
2824 JumpTarget* original = shadows[i]->other_target();
2825 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2826 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002827 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002828 skip.Branch(ne);
2829 frame_->PrepareForReturn();
2830 original->Jump();
2831 skip.Bind();
2832 } else {
2833 original->Branch(eq);
2834 }
2835 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002836 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002838 if (has_valid_frame()) {
2839 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002840 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002841 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2842 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844 // Rethrow exception.
2845 frame_->EmitPush(r0);
2846 frame_->CallRuntime(Runtime::kReThrow, 1);
2847
2848 // Done.
2849 exit.Bind();
2850 }
2851 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852}
2853
2854
ager@chromium.org7c537e22008-10-16 08:43:32 +00002855void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002856#ifdef DEBUG
2857 int original_height = frame_->height();
2858#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002859 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002861 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002862#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002863 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002864#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002865 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002866 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867}
2868
2869
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002870void CodeGenerator::InstantiateFunction(
2871 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002872 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002873 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002874 // Use the fast case closure allocation code that allocates in new
2875 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002876 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002877 FastNewClosureStub stub;
2878 frame_->EmitPush(r0);
2879 frame_->CallStub(&stub, 1);
2880 frame_->EmitPush(r0);
2881 } else {
2882 // Create a new closure.
2883 frame_->EmitPush(cp);
2884 frame_->EmitPush(r0);
2885 frame_->CallRuntime(Runtime::kNewClosure, 2);
2886 frame_->EmitPush(r0);
2887 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888}
2889
2890
ager@chromium.org7c537e22008-10-16 08:43:32 +00002891void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002892#ifdef DEBUG
2893 int original_height = frame_->height();
2894#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002895 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 Comment cmnt(masm_, "[ FunctionLiteral");
2897
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002898 // Build the function info and instantiate it.
2899 Handle<SharedFunctionInfo> function_info =
2900 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002901 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 if (HasStackOverflow()) {
2903 ASSERT(frame_->height() == original_height);
2904 return;
2905 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002906 InstantiateFunction(function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908}
2909
2910
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002911void CodeGenerator::VisitSharedFunctionInfoLiteral(
2912 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913#ifdef DEBUG
2914 int original_height = frame_->height();
2915#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002916 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002917 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2918 InstantiateFunction(node->shared_function_info());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920}
2921
2922
ager@chromium.org7c537e22008-10-16 08:43:32 +00002923void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924#ifdef DEBUG
2925 int original_height = frame_->height();
2926#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002927 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002929 JumpTarget then;
2930 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002931 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002932 if (has_valid_frame()) {
2933 Branch(false, &else_);
2934 }
2935 if (has_valid_frame() || then.is_linked()) {
2936 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002937 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002938 }
2939 if (else_.is_linked()) {
2940 JumpTarget exit;
2941 if (has_valid_frame()) exit.Jump();
2942 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002943 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002944 if (exit.is_linked()) exit.Bind();
2945 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947}
2948
2949
ager@chromium.org7c537e22008-10-16 08:43:32 +00002950void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2951 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002952 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002953
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002954 // JumpTargets do not yet support merging frames so the frame must be
2955 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002956 JumpTarget slow;
2957 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002958
2959 // Generate fast-case code for variables that might be shadowed by
2960 // eval-introduced variables. Eval is used a lot without
2961 // introducing variables. In those cases, we do not want to
2962 // perform a runtime call for all variables in the scope
2963 // containing the eval.
2964 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002965 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002966 // If there was no control flow to slow, we can exit early.
2967 if (!slow.is_linked()) {
2968 frame_->EmitPush(r0);
2969 return;
2970 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002971 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002972
2973 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002974
2975 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002976 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002977 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2978 // Only generate the fast case for locals that rewrite to slots.
2979 // This rules out argument loads.
2980 if (potential_slot != NULL) {
2981 __ ldr(r0,
2982 ContextSlotOperandCheckExtensions(potential_slot,
2983 r1,
2984 r2,
2985 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002986 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002987 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2988 __ cmp(r0, ip);
2989 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002990 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002991 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002992 // ContextSlotOperandCheckExtensions so we have to jump around
2993 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002994 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002995 }
2996 }
2997
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002998 slow.Bind();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002999 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003000 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003001 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003002 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003
ager@chromium.org7c537e22008-10-16 08:43:32 +00003004 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003005 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003006 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003007 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00003009
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003010 done.Bind();
3011 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012
3013 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003014 Register scratch = VirtualFrame::scratch0();
3015 frame_->EmitPush(SlotOperand(slot, scratch));
ager@chromium.org7c537e22008-10-16 08:43:32 +00003016 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003017 // Const slots may contain 'the hole' value (the constant hasn't been
3018 // initialized yet) which needs to be converted into the 'undefined'
3019 // value.
3020 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003021 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003022 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003023 __ cmp(scratch, ip);
3024 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
3025 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003026 }
3027 }
3028}
3029
3030
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003031void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3032 TypeofState state) {
3033 LoadFromSlot(slot, state);
3034
3035 // Bail out quickly if we're not using lazy arguments allocation.
3036 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3037
3038 // ... or if the slot isn't a non-parameter arguments slot.
3039 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3040
3041 VirtualFrame::SpilledScope spilled_scope(frame_);
3042
3043 // Load the loaded value from the stack into r0 but leave it on the
3044 // stack.
3045 __ ldr(r0, MemOperand(sp, 0));
3046
3047 // If the loaded value is the sentinel that indicates that we
3048 // haven't loaded the arguments object yet, we need to do it now.
3049 JumpTarget exit;
3050 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3051 __ cmp(r0, ip);
3052 exit.Branch(ne);
3053 frame_->Drop();
3054 StoreArgumentsObject(false);
3055 exit.Bind();
3056}
3057
3058
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003059void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3060 ASSERT(slot != NULL);
3061 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003062 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003063 ASSERT(slot->var()->is_dynamic());
3064
3065 // For now, just do a runtime call.
3066 frame_->EmitPush(cp);
3067 __ mov(r0, Operand(slot->var()->name()));
3068 frame_->EmitPush(r0);
3069
3070 if (init_state == CONST_INIT) {
3071 // Same as the case for a normal store, but ignores attribute
3072 // (e.g. READ_ONLY) of context slot so that we can initialize
3073 // const properties (introduced via eval("const foo = (some
3074 // expr);")). Also, uses the current function context instead of
3075 // the top context.
3076 //
3077 // Note that we must declare the foo upon entry of eval(), via a
3078 // context slot declaration, but we cannot initialize it at the
3079 // same time, because the const declaration may be at the end of
3080 // the eval code (sigh...) and the const variable may have been
3081 // used before (where its value is 'undefined'). Thus, we can only
3082 // do the initialization when we actually encounter the expression
3083 // and when the expression operands are defined and valid, and
3084 // thus we need the split into 2 operations: declaration of the
3085 // context slot followed by initialization.
3086 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3087 } else {
3088 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3089 }
3090 // Storing a variable must keep the (new) value on the expression
3091 // stack. This is necessary for compiling assignment expressions.
3092 frame_->EmitPush(r0);
3093
3094 } else {
3095 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003096 Register scratch = VirtualFrame::scratch0();
3097 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003098
ager@chromium.org357bf652010-04-12 11:30:10 +00003099 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003100 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003101
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003102 if (init_state == CONST_INIT) {
3103 ASSERT(slot->var()->mode() == Variable::CONST);
3104 // Only the first const initialization must be executed (the slot
3105 // still contains 'the hole' value). When the assignment is
3106 // executed, the code is identical to a normal store (see below).
3107 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003108 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003109 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003110 __ cmp(scratch, ip);
3111 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003112 exit.Branch(ne);
3113 }
3114
3115 // We must execute the store. Storing a variable must keep the
3116 // (new) value on the stack. This is necessary for compiling
3117 // assignment expressions.
3118 //
3119 // Note: We will reach here even with slot->var()->mode() ==
3120 // Variable::CONST because of const declarations which will
3121 // initialize consts to 'the hole' value and by doing so, end up
3122 // calling this code. r2 may be loaded with context; used below in
3123 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003124 Register tos = frame_->Peek();
3125 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003126 if (slot->type() == Slot::CONTEXT) {
3127 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003128 __ tst(tos, Operand(kSmiTagMask));
3129 // We don't use tos any more after here.
3130 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003131 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003132 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003133 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3134 __ mov(r3, Operand(offset));
ager@chromium.org357bf652010-04-12 11:30:10 +00003135 // r1 could be identical with tos, but that doesn't matter.
3136 __ RecordWrite(scratch, r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003137 }
3138 // If we definitely did not jump over the assignment, we do not need
3139 // to bind the exit label. Doing so can defeat peephole
3140 // optimization.
3141 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003142 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003143 exit.Bind();
3144 }
3145 }
3146}
3147
3148
ager@chromium.org381abbb2009-02-25 13:23:22 +00003149void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3150 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003151 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003152 // Check that no extension objects have been created by calls to
3153 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003154 Register tmp = frame_->scratch0();
3155 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003156 Register context = cp;
3157 Scope* s = scope();
3158 while (s != NULL) {
3159 if (s->num_heap_slots() > 0) {
3160 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003161 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003162 // Check that extension is NULL.
3163 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3164 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003165 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003166 }
3167 // Load next context in chain.
3168 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3169 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3170 context = tmp;
3171 }
3172 // If no outer scope calls eval, we do not need to check more
3173 // context extensions.
3174 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3175 s = s->outer_scope();
3176 }
3177
3178 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003179 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003180 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003181 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003182 __ bind(&next);
3183 // Terminate at global context.
3184 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003185 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3186 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003187 __ b(eq, &fast);
3188 // Check that extension is NULL.
3189 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3190 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003191 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003192 // Load next context in chain.
3193 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3194 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3195 __ b(&next);
3196 __ bind(&fast);
3197 }
3198
ager@chromium.org381abbb2009-02-25 13:23:22 +00003199 // Load the global object.
3200 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003201 // Setup the name register and call load IC.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003202 frame_->SpillAllButCopyTOSToR0();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003203 __ mov(r2, Operand(slot->var()->name()));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003204 frame_->CallLoadIC(typeof_state == INSIDE_TYPEOF
3205 ? RelocInfo::CODE_TARGET
3206 : RelocInfo::CODE_TARGET_CONTEXT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003207 // Drop the global object. The result is in r0.
3208 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003209}
3210
3211
ager@chromium.org7c537e22008-10-16 08:43:32 +00003212void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003213#ifdef DEBUG
3214 int original_height = frame_->height();
3215#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003216 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003217 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003218 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003219}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220
ager@chromium.org7c537e22008-10-16 08:43:32 +00003221
3222void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223#ifdef DEBUG
3224 int original_height = frame_->height();
3225#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003226 Comment cmnt(masm_, "[ VariableProxy");
3227
3228 Variable* var = node->var();
3229 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003230 if (expr != NULL) {
3231 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003233 ASSERT(var->is_global());
3234 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003235 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003237 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238}
3239
3240
ager@chromium.org7c537e22008-10-16 08:43:32 +00003241void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003242#ifdef DEBUG
3243 int original_height = frame_->height();
3244#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003245 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003246 Register reg = frame_->GetTOSRegister();
3247 __ mov(reg, Operand(node->handle()));
3248 frame_->EmitPush(reg);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250}
3251
3252
ager@chromium.org7c537e22008-10-16 08:43:32 +00003253void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003254#ifdef DEBUG
3255 int original_height = frame_->height();
3256#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003257 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 Comment cmnt(masm_, "[ RexExp Literal");
3259
3260 // Retrieve the literal array and check the allocated entry.
3261
3262 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003263 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264
3265 // Load the literals array of the function.
3266 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3267
3268 // Load the literal at the ast saved index.
3269 int literal_offset =
3270 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3271 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3272
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003273 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003274 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3275 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277
3278 // If the entry is undefined we call the runtime system to computed
3279 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003280 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003281 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003283 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003285 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 frame_->EmitPush(r0);
3287 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003288 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003290 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 frame_->EmitPush(r2);
3293 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294}
3295
3296
ager@chromium.org7c537e22008-10-16 08:43:32 +00003297void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003298#ifdef DEBUG
3299 int original_height = frame_->height();
3300#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003301 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302 Comment cmnt(masm_, "[ ObjectLiteral");
3303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003304 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003305 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003306 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003307 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003308 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003309 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003310 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003311 __ mov(r1, Operand(node->constant_properties()));
3312 // Should the object literal have fast elements?
3313 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3314 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003315 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003316 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003317 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003318 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003319 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003320 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003322 // At the start of each iteration, the top of stack contains
3323 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324 ObjectLiteral::Property* property = node->properties()->at(i);
3325 Literal* key = property->key();
3326 Expression* value = property->value();
3327 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003328 case ObjectLiteral::Property::CONSTANT:
3329 break;
3330 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3331 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3332 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003333 case ObjectLiteral::Property::COMPUTED:
3334 if (key->handle()->IsSymbol()) {
3335 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3336 LoadAndSpill(value);
3337 frame_->EmitPop(r0);
3338 __ mov(r2, Operand(key->handle()));
3339 __ ldr(r1, frame_->Top()); // Load the receiver.
3340 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3341 break;
3342 }
3343 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003345 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003346 frame_->EmitPush(r0); // dup the result
3347 LoadAndSpill(key);
3348 LoadAndSpill(value);
3349 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 break;
3351 }
3352 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003353 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003354 frame_->EmitPush(r0);
3355 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003356 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003357 frame_->EmitPush(r0);
3358 LoadAndSpill(value);
3359 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360 break;
3361 }
3362 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003363 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003364 frame_->EmitPush(r0);
3365 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003366 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003367 frame_->EmitPush(r0);
3368 LoadAndSpill(value);
3369 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370 break;
3371 }
3372 }
3373 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003374 ASSERT(frame_->height() == original_height + 1);
3375}
3376
3377
ager@chromium.org7c537e22008-10-16 08:43:32 +00003378void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003379#ifdef DEBUG
3380 int original_height = frame_->height();
3381#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003382 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003384
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003385 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003386 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003387 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003388 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003389 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003390 __ mov(r0, Operand(node->constant_elements()));
3391 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003392 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003393 if (node->depth() > 1) {
3394 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003395 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003396 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003397 } else {
3398 FastCloneShallowArrayStub stub(length);
3399 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003400 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003401 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003402 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003403
3404 // Generate code to set the elements in the array that are not
3405 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 for (int i = 0; i < node->values()->length(); i++) {
3407 Expression* value = node->values()->at(i);
3408
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003409 // If value is a literal the property value is already set in the
3410 // boilerplate object.
3411 if (value->AsLiteral() != NULL) continue;
3412 // If value is a materialized literal the property value is already set
3413 // in the boilerplate object if it is simple.
3414 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003416 // The property must be set by generated code.
3417 LoadAndSpill(value);
3418 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003420 // Fetch the object literal.
3421 __ ldr(r1, frame_->Top());
3422 // Get the elements array.
3423 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003424
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003425 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003426 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003427 __ str(r0, FieldMemOperand(r1, offset));
3428
3429 // Update the write barrier for the array address.
3430 __ mov(r3, Operand(offset));
3431 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003433 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003434}
3435
3436
ager@chromium.org32912102009-01-16 10:38:43 +00003437void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003438#ifdef DEBUG
3439 int original_height = frame_->height();
3440#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003441 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org32912102009-01-16 10:38:43 +00003442 // Call runtime routine to allocate the catch extension object and
3443 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003444 Comment cmnt(masm_, "[ CatchExtensionObject");
3445 LoadAndSpill(node->key());
3446 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003447 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3448 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003449 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00003450}
3451
3452
ager@chromium.org7c537e22008-10-16 08:43:32 +00003453void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003454 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003455#ifdef DEBUG
3456 int original_height = frame_->height();
3457#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003458 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003459
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003460 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003461 if (target.is_illegal()) {
3462 // Fool the virtual frame into thinking that we left the assignment's
3463 // value on the frame.
ager@chromium.org357bf652010-04-12 11:30:10 +00003464 Register tos = frame_->GetTOSRegister();
3465 __ mov(tos, Operand(Smi::FromInt(0)));
3466 frame_->EmitPush(tos);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003467 ASSERT(frame_->height() == original_height + 1);
3468 return;
3469 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471 if (node->op() == Token::ASSIGN ||
3472 node->op() == Token::INIT_VAR ||
3473 node->op() == Token::INIT_CONST) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003474 Load(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00003475
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003476 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003477 // Get the old value of the lhs.
ager@chromium.org357bf652010-04-12 11:30:10 +00003478 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003479 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003480 bool overwrite =
3481 (node->value()->AsBinaryOperation() != NULL &&
3482 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003484 VirtualFrameSmiOperation(node->binary_op(),
3485 literal->handle(),
3486 false,
3487 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003488 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003489 Load(node->value());
3490 VirtualFrameBinaryOperation(node->binary_op(),
3491 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003494 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3495 if (var != NULL &&
3496 (var->mode() == Variable::CONST) &&
3497 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3498 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003499 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003501 CodeForSourcePosition(node->position());
3502 if (node->op() == Token::INIT_CONST) {
3503 // Dynamic constant initializations must use the function context
3504 // and initialize the actual constant declared. Dynamic variable
3505 // initializations are simply assignments and use SetValue.
3506 target.SetValue(CONST_INIT);
3507 } else {
3508 target.SetValue(NOT_CONST_INIT);
3509 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510 }
3511 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003512 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513}
3514
3515
ager@chromium.org7c537e22008-10-16 08:43:32 +00003516void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003517#ifdef DEBUG
3518 int original_height = frame_->height();
3519#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003520 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 Comment cmnt(masm_, "[ Throw");
3522
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003523 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003524 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003525 frame_->CallRuntime(Runtime::kThrow, 1);
3526 frame_->EmitPush(r0);
3527 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528}
3529
3530
ager@chromium.org7c537e22008-10-16 08:43:32 +00003531void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003532#ifdef DEBUG
3533 int original_height = frame_->height();
3534#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003535 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003536
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003538 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003539 }
3540 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541}
3542
3543
ager@chromium.org7c537e22008-10-16 08:43:32 +00003544void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003545#ifdef DEBUG
3546 int original_height = frame_->height();
3547#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003548 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549 Comment cmnt(masm_, "[ Call");
3550
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003551 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003552 ZoneList<Expression*>* args = node->arguments();
3553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 Variable* var = function->AsVariableProxy()->AsVariable();
3557 Property* property = function->AsProperty();
3558
3559 // ------------------------------------------------------------------------
3560 // Fast-case: Use inline caching.
3561 // ---
3562 // According to ECMA-262, section 11.2.3, page 44, the function to call
3563 // must be resolved after the arguments have been evaluated. The IC code
3564 // automatically handles this by loading the arguments before the function
3565 // is resolved in cache misses (this also holds for megamorphic calls).
3566 // ------------------------------------------------------------------------
3567
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003568 if (var != NULL && var->is_possibly_eval()) {
3569 // ----------------------------------
3570 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3571 // ----------------------------------
3572
3573 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3574 // resolve the function we need to call and the receiver of the
3575 // call. Then we call the resolved function using the given
3576 // arguments.
3577 // Prepare stack for call to resolved function.
3578 LoadAndSpill(function);
3579 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3580 frame_->EmitPush(r2); // Slot for receiver
3581 int arg_count = args->length();
3582 for (int i = 0; i < arg_count; i++) {
3583 LoadAndSpill(args->at(i));
3584 }
3585
3586 // Prepare stack for call to ResolvePossiblyDirectEval.
3587 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3588 frame_->EmitPush(r1);
3589 if (arg_count > 0) {
3590 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3591 frame_->EmitPush(r1);
3592 } else {
3593 frame_->EmitPush(r2);
3594 }
3595
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003596 // Push the receiver.
3597 __ ldr(r1, frame_->Receiver());
3598 frame_->EmitPush(r1);
3599
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003600 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003601 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003602
3603 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003604 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003605 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3606
3607 // Call the function.
3608 CodeForSourcePosition(node->position());
3609
3610 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003611 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003612 frame_->CallStub(&call_function, arg_count + 1);
3613
3614 __ ldr(cp, frame_->Context());
3615 // Remove the function from the stack.
3616 frame_->Drop();
3617 frame_->EmitPush(r0);
3618
3619 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620 // ----------------------------------
3621 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3622 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003623 // Pass the global object as the receiver and let the IC stub
3624 // patch the stack to use the global proxy as 'this' in the
3625 // invoked function.
3626 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627
3628 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003629 int arg_count = args->length();
3630 for (int i = 0; i < arg_count; i++) {
3631 LoadAndSpill(args->at(i));
3632 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633
ager@chromium.org5c838252010-02-19 08:53:10 +00003634 // Setup the name register and call the IC initialization code.
3635 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003636 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3637 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003638 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003639 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3640 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003641 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643
3644 } else if (var != NULL && var->slot() != NULL &&
3645 var->slot()->type() == Slot::LOOKUP) {
3646 // ----------------------------------
3647 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3648 // ----------------------------------
3649
3650 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003651 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003652 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003653 frame_->EmitPush(r0);
3654 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655 // r0: slot value; r1: receiver
3656
3657 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003658 frame_->EmitPush(r0); // function
3659 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660
3661 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003662 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664
3665 } else if (property != NULL) {
3666 // Check if the key is a literal string.
3667 Literal* literal = property->key()->AsLiteral();
3668
3669 if (literal != NULL && literal->handle()->IsSymbol()) {
3670 // ------------------------------------------------------------------
3671 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3672 // ------------------------------------------------------------------
3673
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003674 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003676 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
3677 name->IsEqualTo(CStrVector("apply")) &&
3678 args->length() == 2 &&
3679 args->at(1)->AsVariableProxy() != NULL &&
3680 args->at(1)->AsVariableProxy()->IsArguments()) {
3681 // Use the optimized Function.prototype.apply that avoids
3682 // allocating lazily allocated arguments objects.
3683 CallApplyLazy(property->obj(),
3684 args->at(0),
3685 args->at(1)->AsVariableProxy(),
3686 node->position());
3687
3688 } else {
3689 LoadAndSpill(property->obj()); // Receiver.
3690 // Load the arguments.
3691 int arg_count = args->length();
3692 for (int i = 0; i < arg_count; i++) {
3693 LoadAndSpill(args->at(i));
3694 }
3695
3696 // Set the name register and call the IC initialization code.
3697 __ mov(r2, Operand(name));
3698 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3699 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
3700 CodeForSourcePosition(node->position());
3701 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
3702 __ ldr(cp, frame_->Context());
3703 frame_->EmitPush(r0);
3704 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705
3706 } else {
3707 // -------------------------------------------
3708 // JavaScript example: 'array[index](1, 2, 3)'
3709 // -------------------------------------------
3710
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003711 LoadAndSpill(property->obj());
3712 LoadAndSpill(property->key());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003713 EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003714 frame_->Drop(); // key
3715 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003716 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003717 // Use the global receiver.
3718 frame_->Drop();
3719 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720 LoadGlobalReceiver(r0);
3721 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003722 frame_->EmitPop(r1); // receiver
3723 frame_->EmitPush(r0); // function
3724 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003725 }
3726
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003728 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003730 }
3731
3732 } else {
3733 // ----------------------------------
3734 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3735 // ----------------------------------
3736
3737 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003738 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003739
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003740 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003741 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003744 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003745 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003746 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003747 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003748}
3749
3750
ager@chromium.org7c537e22008-10-16 08:43:32 +00003751void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752#ifdef DEBUG
3753 int original_height = frame_->height();
3754#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003755 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 Comment cmnt(masm_, "[ CallNew");
3757
3758 // According to ECMA-262, section 11.2.2, page 44, the function
3759 // expression in new calls must be evaluated before the
3760 // arguments. This is different from ordinary calls, where the
3761 // actual function to call is resolved after the arguments have been
3762 // evaluated.
3763
3764 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003765 // receiver. There is no need to use the global proxy here because
3766 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003767 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003768 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769
3770 // Push the arguments ("left-to-right") on the stack.
3771 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003772 int arg_count = args->length();
3773 for (int i = 0; i < arg_count; i++) {
3774 LoadAndSpill(args->at(i));
3775 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003776
mads.s.ager31e71382008-08-13 09:32:07 +00003777 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003778 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003779 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003780 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003781
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003782 // Call the construct call builtin that handles allocation and
3783 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003784 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003785 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003786 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003787
3788 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003789 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003790 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791}
3792
3793
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003794void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003795 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003796 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003797 JumpTarget leave, null, function, non_function_constructor;
3798
3799 // Load the object into r0.
3800 LoadAndSpill(args->at(0));
3801 frame_->EmitPop(r0);
3802
3803 // If the object is a smi, we return null.
3804 __ tst(r0, Operand(kSmiTagMask));
3805 null.Branch(eq);
3806
3807 // Check that the object is a JS object but take special care of JS
3808 // functions to make sure they have 'Function' as their class.
3809 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3810 null.Branch(lt);
3811
3812 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3813 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3814 // LAST_JS_OBJECT_TYPE.
3815 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3816 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3817 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3818 function.Branch(eq);
3819
3820 // Check if the constructor in the map is a function.
3821 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3822 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3823 non_function_constructor.Branch(ne);
3824
3825 // The r0 register now contains the constructor function. Grab the
3826 // instance class name from there.
3827 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3828 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003829 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003830 leave.Jump();
3831
3832 // Functions have class 'Function'.
3833 function.Bind();
3834 __ mov(r0, Operand(Factory::function_class_symbol()));
3835 frame_->EmitPush(r0);
3836 leave.Jump();
3837
3838 // Objects with a non-function constructor have class 'Object'.
3839 non_function_constructor.Bind();
3840 __ mov(r0, Operand(Factory::Object_symbol()));
3841 frame_->EmitPush(r0);
3842 leave.Jump();
3843
3844 // Non-JS objects have class null.
3845 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003846 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003847 frame_->EmitPush(r0);
3848
3849 // All done.
3850 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003851}
3852
3853
ager@chromium.org7c537e22008-10-16 08:43:32 +00003854void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003855 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003857 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003858 LoadAndSpill(args->at(0));
3859 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003860 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003861 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003862 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003863 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3864 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003865 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003866 // Load the value.
3867 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868 leave.Bind();
3869 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003870}
3871
3872
ager@chromium.org7c537e22008-10-16 08:43:32 +00003873void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003874 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003876 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003877 LoadAndSpill(args->at(0)); // Load the object.
3878 LoadAndSpill(args->at(1)); // Load the value.
3879 frame_->EmitPop(r0); // r0 contains value
3880 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881 // if (object->IsSmi()) return object.
3882 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003883 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003884 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3885 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003886 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 // Store the value.
3888 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3889 // Update the write barrier.
3890 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3891 __ RecordWrite(r1, r2, r3);
3892 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003893 leave.Bind();
3894 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895}
3896
3897
ager@chromium.org7c537e22008-10-16 08:43:32 +00003898void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003899 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003900 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003901 LoadAndSpill(args->at(0));
3902 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003903 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904 cc_reg_ = eq;
3905}
3906
3907
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003908void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003909 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003910 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3911 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003912#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003913 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003914 LoadAndSpill(args->at(1));
3915 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003916 __ CallRuntime(Runtime::kLog, 2);
3917 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003918#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003919 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003921}
3922
3923
ager@chromium.org7c537e22008-10-16 08:43:32 +00003924void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003925 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003926 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003927 LoadAndSpill(args->at(0));
3928 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003929 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003930 cc_reg_ = eq;
3931}
3932
3933
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003934// Generates the Math.pow method - currently just calls runtime.
3935void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3936 ASSERT(args->length() == 2);
3937 Load(args->at(0));
3938 Load(args->at(1));
3939 frame_->CallRuntime(Runtime::kMath_pow, 2);
3940 frame_->EmitPush(r0);
3941}
3942
3943
3944// Generates the Math.sqrt method - currently just calls runtime.
3945void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
3946 ASSERT(args->length() == 1);
3947 Load(args->at(0));
3948 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
3949 frame_->EmitPush(r0);
3950}
3951
3952
kasper.lund7276f142008-07-30 08:49:36 +00003953// This should generate code that performs a charCodeAt() call or returns
3954// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3955// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003956void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003957 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00003958 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003959 Comment(masm_, "[ GenerateFastCharCodeAt");
3960
3961 LoadAndSpill(args->at(0));
3962 LoadAndSpill(args->at(1));
3963 frame_->EmitPop(r0); // Index.
3964 frame_->EmitPop(r1); // String.
3965
3966 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3967
3968 __ tst(r1, Operand(kSmiTagMask));
3969 __ b(eq, &slow); // The 'string' was a Smi.
3970
3971 ASSERT(kSmiTag == 0);
3972 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3973 __ b(ne, &slow); // The index was negative or not a Smi.
3974
3975 __ bind(&try_again_with_new_string);
3976 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3977 __ b(ge, &slow);
3978
3979 // Now r2 has the string type.
3980 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003981 // Now r3 has the length of the string. Compare with the index.
3982 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3983 __ b(le, &slow);
3984
3985 // Here we know the index is in range. Check that string is sequential.
3986 ASSERT_EQ(0, kSeqStringTag);
3987 __ tst(r2, Operand(kStringRepresentationMask));
3988 __ b(ne, &not_a_flat_string);
3989
3990 // Check whether it is an ASCII string.
3991 ASSERT_EQ(0, kTwoByteStringTag);
3992 __ tst(r2, Operand(kStringEncodingMask));
3993 __ b(ne, &ascii_string);
3994
3995 // 2-byte string. We can add without shifting since the Smi tag size is the
3996 // log2 of the number of bytes in a two-byte character.
3997 ASSERT_EQ(1, kSmiTagSize);
3998 ASSERT_EQ(0, kSmiShiftSize);
3999 __ add(r1, r1, Operand(r0));
4000 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
4001 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
4002 __ jmp(&end);
4003
4004 __ bind(&ascii_string);
4005 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
4006 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
4007 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
4008 __ jmp(&end);
4009
4010 __ bind(&not_a_flat_string);
4011 __ and_(r2, r2, Operand(kStringRepresentationMask));
4012 __ cmp(r2, Operand(kConsStringTag));
4013 __ b(ne, &slow);
4014
4015 // ConsString.
4016 // Check that the right hand side is the empty string (ie if this is really a
4017 // flat string in a cons string). If that is not the case we would rather go
4018 // to the runtime system now, to flatten the string.
4019 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
4020 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
4021 __ cmp(r2, Operand(r3));
4022 __ b(ne, &slow);
4023
4024 // Get the first of the two strings.
4025 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
4026 __ jmp(&try_again_with_new_string);
4027
4028 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004029 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004030
4031 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00004033}
4034
4035
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004036void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
4037 Comment(masm_, "[ GenerateCharFromCode");
4038 ASSERT(args->length() == 1);
4039
4040 LoadAndSpill(args->at(0));
4041 frame_->EmitPop(r0);
4042
4043 JumpTarget slow_case;
4044 JumpTarget exit;
4045
4046 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4047 ASSERT(kSmiTag == 0);
4048 ASSERT(kSmiShiftSize == 0);
4049 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4050 __ tst(r0, Operand(kSmiTagMask |
4051 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
4052 slow_case.Branch(nz);
4053
4054 ASSERT(kSmiTag == 0);
4055 __ mov(r1, Operand(Factory::single_character_string_cache()));
4056 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4057 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
4058 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4059 __ cmp(r1, ip);
4060 slow_case.Branch(eq);
4061
4062 frame_->EmitPush(r1);
4063 exit.Jump();
4064
4065 slow_case.Bind();
4066 frame_->EmitPush(r0);
4067 frame_->CallRuntime(Runtime::kCharFromCode, 1);
4068 frame_->EmitPush(r0);
4069
4070 exit.Bind();
4071}
4072
4073
ager@chromium.org7c537e22008-10-16 08:43:32 +00004074void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004075 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004076 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004077 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004078 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004079 // We need the CC bits to come out as not_equal in the case where the
4080 // object is a smi. This can't be done with the usual test opcode so
4081 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004083 __ and_(r1, r0, Operand(kSmiTagMask));
4084 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004085 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004086 // It is a heap object - get the map. Check if the object is a JS array.
4087 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004089 cc_reg_ = eq;
4090}
4091
4092
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004093void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004094 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004095 ASSERT(args->length() == 1);
4096 LoadAndSpill(args->at(0));
4097 JumpTarget answer;
4098 // We need the CC bits to come out as not_equal in the case where the
4099 // object is a smi. This can't be done with the usual test opcode so
4100 // we use XOR to get the right CC bits.
4101 frame_->EmitPop(r0);
4102 __ and_(r1, r0, Operand(kSmiTagMask));
4103 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
4104 answer.Branch(ne);
4105 // It is a heap object - get the map. Check if the object is a regexp.
4106 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4107 answer.Bind();
4108 cc_reg_ = eq;
4109}
4110
4111
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004112void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4113 // This generates a fast version of:
4114 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00004115 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004116 ASSERT(args->length() == 1);
4117 LoadAndSpill(args->at(0));
4118 frame_->EmitPop(r1);
4119 __ tst(r1, Operand(kSmiTagMask));
4120 false_target()->Branch(eq);
4121
4122 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4123 __ cmp(r1, ip);
4124 true_target()->Branch(eq);
4125
4126 Register map_reg = r2;
4127 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
4128 // Undetectable objects behave like undefined when tested with typeof.
4129 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4130 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4131 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4132 false_target()->Branch(eq);
4133
4134 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4135 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
4136 false_target()->Branch(lt);
4137 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
4138 cc_reg_ = le;
4139}
4140
4141
4142void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4143 // This generates a fast version of:
4144 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00004145 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004146 ASSERT(args->length() == 1);
4147 LoadAndSpill(args->at(0));
4148 frame_->EmitPop(r0);
4149 __ tst(r0, Operand(kSmiTagMask));
4150 false_target()->Branch(eq);
4151 Register map_reg = r2;
4152 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
4153 cc_reg_ = eq;
4154}
4155
4156
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004157void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004158 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004159 ASSERT(args->length() == 1);
4160 LoadAndSpill(args->at(0));
4161 frame_->EmitPop(r0);
4162 __ tst(r0, Operand(kSmiTagMask));
4163 false_target()->Branch(eq);
4164 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4165 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
4166 __ tst(r1, Operand(1 << Map::kIsUndetectable));
4167 cc_reg_ = ne;
4168}
4169
4170
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004171void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004172 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004173 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004174
4175 // Get the frame pointer for the calling frame.
4176 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4177
4178 // Skip the arguments adaptor frame if it exists.
4179 Label check_frame_marker;
4180 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004181 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004182 __ b(ne, &check_frame_marker);
4183 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
4184
4185 // Check the marker in the calling frame.
4186 __ bind(&check_frame_marker);
4187 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
4188 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4189 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004190}
4191
4192
ager@chromium.org7c537e22008-10-16 08:43:32 +00004193void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004194 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004195 ASSERT(args->length() == 0);
4196
lrn@chromium.org25156de2010-04-06 13:10:27 +00004197 Label exit;
4198
4199 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00004200 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201
lrn@chromium.org25156de2010-04-06 13:10:27 +00004202 // Check if the calling frame is an arguments adaptor frame.
4203 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4204 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4205 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4206 __ b(ne, &exit);
4207
4208 // Arguments adaptor case: Read the arguments length from the
4209 // adaptor frame.
4210 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4211
4212 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004213 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214}
4215
4216
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004217void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004218 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004219 ASSERT(args->length() == 1);
4220
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004221 // Satisfy contract with ArgumentsAccessStub:
4222 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004223 LoadAndSpill(args->at(0));
4224 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004225 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004226
4227 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004228 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004229 frame_->CallStub(&stub, 0);
4230 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004231}
4232
4233
ager@chromium.org357bf652010-04-12 11:30:10 +00004234void CodeGenerator::GenerateRandomHeapNumber(
4235 ZoneList<Expression*>* args) {
4236 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004237 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004238
4239 Label slow_allocate_heapnumber;
4240 Label heapnumber_allocated;
4241
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004242 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004243 __ jmp(&heapnumber_allocated);
4244
4245 __ bind(&slow_allocate_heapnumber);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004246 // To allocate a heap number, and ensure that it is not a smi, we
4247 // call the runtime function FUnaryMinus on 0, returning the double
4248 // -0.0. A new, distinct heap number is returned each time.
ager@chromium.org357bf652010-04-12 11:30:10 +00004249 __ mov(r0, Operand(Smi::FromInt(0)));
4250 __ push(r0);
4251 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004252 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004253
4254 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004255
4256 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4257 // by computing:
4258 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4259 if (CpuFeatures::IsSupported(VFP3)) {
4260 __ PrepareCallCFunction(0, r1);
4261 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4262
4263 CpuFeatures::Scope scope(VFP3);
4264 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4265 // Create this constant using mov/orr to avoid PC relative load.
4266 __ mov(r1, Operand(0x41000000));
4267 __ orr(r1, r1, Operand(0x300000));
4268 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4269 __ vmov(d7, r0, r1);
4270 // Move 0x4130000000000000 to VFP.
4271 __ mov(r0, Operand(0));
4272 __ vmov(d8, r0, r1);
4273 // Subtract and store the result in the heap number.
4274 __ vsub(d7, d7, d8);
4275 __ sub(r0, r4, Operand(kHeapObjectTag));
4276 __ vstr(d7, r0, HeapNumber::kValueOffset);
4277 frame_->EmitPush(r4);
4278 } else {
4279 __ mov(r0, Operand(r4));
4280 __ PrepareCallCFunction(1, r1);
4281 __ CallCFunction(
4282 ExternalReference::fill_heap_number_with_random_function(), 1);
4283 frame_->EmitPush(r0);
4284 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004285}
4286
4287
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004288void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4289 ASSERT_EQ(2, args->length());
4290
4291 Load(args->at(0));
4292 Load(args->at(1));
4293
ager@chromium.org5c838252010-02-19 08:53:10 +00004294 StringAddStub stub(NO_STRING_ADD_FLAGS);
4295 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004296 frame_->EmitPush(r0);
4297}
4298
4299
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004300void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4301 ASSERT_EQ(3, args->length());
4302
4303 Load(args->at(0));
4304 Load(args->at(1));
4305 Load(args->at(2));
4306
ager@chromium.org5c838252010-02-19 08:53:10 +00004307 SubStringStub stub;
4308 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004309 frame_->EmitPush(r0);
4310}
4311
4312
4313void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4314 ASSERT_EQ(2, args->length());
4315
4316 Load(args->at(0));
4317 Load(args->at(1));
4318
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004319 StringCompareStub stub;
4320 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004321 frame_->EmitPush(r0);
4322}
4323
4324
4325void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4326 ASSERT_EQ(4, args->length());
4327
4328 Load(args->at(0));
4329 Load(args->at(1));
4330 Load(args->at(2));
4331 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004332 RegExpExecStub stub;
4333 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004334 frame_->EmitPush(r0);
4335}
4336
4337
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004338void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4339 // No stub. This code only occurs a few times in regexp.js.
4340 const int kMaxInlineLength = 100;
4341 ASSERT_EQ(3, args->length());
4342 Load(args->at(0)); // Size of array, smi.
4343 Load(args->at(1)); // "index" property value.
4344 Load(args->at(2)); // "input" property value.
4345 {
4346 VirtualFrame::SpilledScope spilled_scope(frame_);
4347 Label slowcase;
4348 Label done;
4349 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4350 STATIC_ASSERT(kSmiTag == 0);
4351 STATIC_ASSERT(kSmiTagSize == 1);
4352 __ tst(r1, Operand(kSmiTagMask));
4353 __ b(ne, &slowcase);
4354 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4355 __ b(hi, &slowcase);
4356 // Smi-tagging is equivalent to multiplying by 2.
4357 // Allocate RegExpResult followed by FixedArray with size in ebx.
4358 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4359 // Elements: [Map][Length][..elements..]
4360 // Size of JSArray with two in-object properties and the header of a
4361 // FixedArray.
4362 int objects_size =
4363 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4364 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4365 __ add(r2, r5, Operand(objects_size));
4366 __ AllocateInNewSpace(r2, // In: Size, in words.
4367 r0, // Out: Start of allocation (tagged).
4368 r3, // Scratch register.
4369 r4, // Scratch register.
4370 &slowcase,
4371 TAG_OBJECT);
4372 // r0: Start of allocated area, object-tagged.
4373 // r1: Number of elements in array, as smi.
4374 // r5: Number of elements, untagged.
4375
4376 // Set JSArray map to global.regexp_result_map().
4377 // Set empty properties FixedArray.
4378 // Set elements to point to FixedArray allocated right after the JSArray.
4379 // Interleave operations for better latency.
4380 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4381 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4382 __ mov(r4, Operand(Factory::empty_fixed_array()));
4383 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4384 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4385 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4386 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4387 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4388
4389 // Set input, index and length fields from arguments.
4390 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4391 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4392 __ add(sp, sp, Operand(kPointerSize));
4393 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4394 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4395
4396 // Fill out the elements FixedArray.
4397 // r0: JSArray, tagged.
4398 // r3: FixedArray, tagged.
4399 // r5: Number of elements in array, untagged.
4400
4401 // Set map.
4402 __ mov(r2, Operand(Factory::fixed_array_map()));
4403 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4404 // Set FixedArray length.
4405 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4406 // Fill contents of fixed-array with the-hole.
4407 __ mov(r2, Operand(Factory::the_hole_value()));
4408 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4409 // Fill fixed array elements with hole.
4410 // r0: JSArray, tagged.
4411 // r2: the hole.
4412 // r3: Start of elements in FixedArray.
4413 // r5: Number of elements to fill.
4414 Label loop;
4415 __ tst(r5, Operand(r5));
4416 __ bind(&loop);
4417 __ b(le, &done); // Jump if r1 is negative or zero.
4418 __ sub(r5, r5, Operand(1), SetCC);
4419 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4420 __ jmp(&loop);
4421
4422 __ bind(&slowcase);
4423 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4424
4425 __ bind(&done);
4426 }
4427 frame_->Forget(3);
4428 frame_->EmitPush(r0);
4429}
4430
4431
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004432class DeferredSearchCache: public DeferredCode {
4433 public:
4434 DeferredSearchCache(Register dst, Register cache, Register key)
4435 : dst_(dst), cache_(cache), key_(key) {
4436 set_comment("[ DeferredSearchCache");
4437 }
4438
4439 virtual void Generate();
4440
4441 private:
4442 Register dst_, cache_, key_;
4443};
4444
4445
4446void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00004447 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004448 __ CallRuntime(Runtime::kGetFromCache, 2);
4449 if (!dst_.is(r0)) {
4450 __ mov(dst_, r0);
4451 }
4452}
4453
4454
4455void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4456 ASSERT_EQ(2, args->length());
4457
4458 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4459 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4460
4461 Handle<FixedArray> jsfunction_result_caches(
4462 Top::global_context()->jsfunction_result_caches());
4463 if (jsfunction_result_caches->length() <= cache_id) {
4464 __ Abort("Attempt to use undefined cache.");
4465 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
4466 frame_->EmitPush(r0);
4467 return;
4468 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004469
4470 Load(args->at(1));
4471 frame_->EmitPop(r2);
4472
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004473 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
4474 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
4475 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4476 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
4477
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004478 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4479
4480 const int kFingerOffset =
4481 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4482 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004483 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4484 // r0 now holds finger offset as a smi.
4485 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4486 // r3 now points to the start of fixed array elements.
4487 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4488 // Note side effect of PreIndex: r3 now points to the key of the pair.
4489 __ cmp(r2, r0);
4490 deferred->Branch(ne);
4491
4492 __ ldr(r0, MemOperand(r3, kPointerSize));
4493
4494 deferred->BindExit();
4495 frame_->EmitPush(r0);
4496}
4497
4498
ager@chromium.org5c838252010-02-19 08:53:10 +00004499void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4500 ASSERT_EQ(args->length(), 1);
4501
4502 // Load the argument on the stack and jump to the runtime.
4503 Load(args->at(0));
4504
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004505 NumberToStringStub stub;
4506 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004507 frame_->EmitPush(r0);
4508}
4509
4510
ager@chromium.org357bf652010-04-12 11:30:10 +00004511void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4512 Comment cmnt(masm_, "[ GenerateCallFunction");
4513
4514 ASSERT(args->length() >= 2);
4515
4516 int n_args = args->length() - 2; // for receiver and function.
4517 Load(args->at(0)); // receiver
4518 for (int i = 0; i < n_args; i++) {
4519 Load(args->at(i + 1));
4520 }
4521 Load(args->at(n_args + 1)); // function
4522 frame_->CallJSFunction(n_args);
4523 frame_->EmitPush(r0);
4524}
4525
4526
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004527void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4528 ASSERT_EQ(args->length(), 1);
4529 // Load the argument on the stack and jump to the runtime.
4530 Load(args->at(0));
4531 frame_->CallRuntime(Runtime::kMath_sin, 1);
4532 frame_->EmitPush(r0);
4533}
4534
4535
4536void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4537 ASSERT_EQ(args->length(), 1);
4538 // Load the argument on the stack and jump to the runtime.
4539 Load(args->at(0));
4540 frame_->CallRuntime(Runtime::kMath_cos, 1);
4541 frame_->EmitPush(r0);
4542}
4543
4544
ager@chromium.org7c537e22008-10-16 08:43:32 +00004545void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004546 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004547 ASSERT(args->length() == 2);
4548
4549 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004550 LoadAndSpill(args->at(0));
4551 LoadAndSpill(args->at(1));
4552 frame_->EmitPop(r0);
4553 frame_->EmitPop(r1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004554 __ cmp(r0, r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004555 cc_reg_ = eq;
4556}
4557
4558
ager@chromium.org7c537e22008-10-16 08:43:32 +00004559void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004560#ifdef DEBUG
4561 int original_height = frame_->height();
4562#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004563 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004564 if (CheckForInlineRuntimeCall(node)) {
4565 ASSERT((has_cc() && frame_->height() == original_height) ||
4566 (!has_cc() && frame_->height() == original_height + 1));
4567 return;
4568 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004569
4570 ZoneList<Expression*>* args = node->arguments();
4571 Comment cmnt(masm_, "[ CallRuntime");
4572 Runtime::Function* function = node->function();
4573
ager@chromium.org41826e72009-03-30 13:30:57 +00004574 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004575 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004576 // Push the builtins object found in the current global object.
4577 __ ldr(r1, GlobalObject());
4578 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004579 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004580 }
mads.s.ager31e71382008-08-13 09:32:07 +00004581
ager@chromium.org41826e72009-03-30 13:30:57 +00004582 // Push the arguments ("left-to-right").
4583 int arg_count = args->length();
4584 for (int i = 0; i < arg_count; i++) {
4585 LoadAndSpill(args->at(i));
4586 }
mads.s.ager31e71382008-08-13 09:32:07 +00004587
ager@chromium.org41826e72009-03-30 13:30:57 +00004588 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004589 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004590 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004591 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4592 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004593 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004594 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004595 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004596 } else {
4597 // Call the C runtime function.
4598 frame_->CallRuntime(function, arg_count);
4599 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004600 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004601 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004602}
4603
4604
ager@chromium.org7c537e22008-10-16 08:43:32 +00004605void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004606#ifdef DEBUG
4607 int original_height = frame_->height();
4608#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004609 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004610 Comment cmnt(masm_, "[ UnaryOperation");
4611
4612 Token::Value op = node->op();
4613
4614 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004615 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004616 false_target(),
4617 true_target(),
4618 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004619 // LoadCondition may (and usually does) leave a test and branch to
4620 // be emitted by the caller. In that case, negate the condition.
4621 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004622
4623 } else if (op == Token::DELETE) {
4624 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004625 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004626 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004627 LoadAndSpill(property->obj());
4628 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004629 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004630
mads.s.ager31e71382008-08-13 09:32:07 +00004631 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004632 Slot* slot = variable->slot();
4633 if (variable->is_global()) {
4634 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004635 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004636 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004637 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004638
4639 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4640 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004641 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004642 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004643 frame_->EmitPush(r0);
4644 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004645 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004646 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004647 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004648 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004649 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004650
mads.s.ager31e71382008-08-13 09:32:07 +00004651 } else {
4652 // Default: Result of deleting non-global, not dynamically
4653 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004654 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004655 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004656
4657 } else {
4658 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004659 LoadAndSpill(node->expression()); // may have side-effects
4660 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004661 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004662 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004663 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004664
4665 } else if (op == Token::TYPEOF) {
4666 // Special case for loading the typeof expression; see comment on
4667 // LoadTypeofExpression().
4668 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004669 frame_->CallRuntime(Runtime::kTypeof, 1);
4670 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004671
4672 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004673 bool overwrite =
4674 (node->expression()->AsBinaryOperation() != NULL &&
4675 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004676 LoadAndSpill(node->expression());
4677 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004678 switch (op) {
4679 case Token::NOT:
4680 case Token::DELETE:
4681 case Token::TYPEOF:
4682 UNREACHABLE(); // handled above
4683 break;
4684
4685 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004686 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004687 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004688 break;
4689 }
4690
4691 case Token::BIT_NOT: {
4692 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004693 JumpTarget smi_label;
4694 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004695 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004696 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004697
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004698 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4699 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004700 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004701
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004702 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004703 __ mvn(r0, Operand(r0));
4704 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004705 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004706 break;
4707 }
4708
4709 case Token::VOID:
4710 // since the stack top is cached in r0, popping and then
4711 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004712 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004713 break;
4714
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004715 case Token::ADD: {
4716 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004717 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004718 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004719 continue_label.Branch(eq);
4720 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004721 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004722 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004723 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004724 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004725 default:
4726 UNREACHABLE();
4727 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004728 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004729 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004730 ASSERT(!has_valid_frame() ||
4731 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004732 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004733}
4734
4735
ager@chromium.org7c537e22008-10-16 08:43:32 +00004736void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004737#ifdef DEBUG
4738 int original_height = frame_->height();
4739#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004740 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004741 Comment cmnt(masm_, "[ CountOperation");
4742
4743 bool is_postfix = node->is_postfix();
4744 bool is_increment = node->op() == Token::INC;
4745
4746 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4747 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4748
4749 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004750 if (is_postfix) {
4751 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004752 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004753 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004754
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004755 // A constant reference is not saved to, so a constant reference is not a
4756 // compound assignment reference.
4757 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004758 if (target.is_illegal()) {
4759 // Spoof the virtual frame to have the expected height (one higher
4760 // than on entry).
4761 if (!is_postfix) {
4762 __ mov(r0, Operand(Smi::FromInt(0)));
4763 frame_->EmitPush(r0);
4764 }
4765 ASSERT(frame_->height() == original_height + 1);
4766 return;
4767 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004768 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004769 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004770
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004771 JumpTarget slow;
4772 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004773
4774 // Load the value (1) into register r1.
4775 __ mov(r1, Operand(Smi::FromInt(1)));
4776
4777 // Check for smi operand.
4778 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004779 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004780
4781 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004782 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004783 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004784 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785
4786 // Perform optimistic increment/decrement.
4787 if (is_increment) {
4788 __ add(r0, r0, Operand(r1), SetCC);
4789 } else {
4790 __ sub(r0, r0, Operand(r1), SetCC);
4791 }
4792
4793 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004794 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004795
4796 // Revert optimistic increment/decrement.
4797 if (is_increment) {
4798 __ sub(r0, r0, Operand(r1));
4799 } else {
4800 __ add(r0, r0, Operand(r1));
4801 }
4802
4803 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004804 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004805 {
4806 // Convert the operand to a number.
4807 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004808 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004809 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004810 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004811 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004812 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004813 }
4814
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004815 // Compute the new value.
4816 __ mov(r1, Operand(Smi::FromInt(1)));
4817 frame_->EmitPush(r0);
4818 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004819 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004820 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004821 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004822 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004823 }
4824
4825 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004826 exit.Bind();
4827 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004828 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004829 }
4830
4831 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004832 if (is_postfix) frame_->EmitPop(r0);
4833 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004834}
4835
4836
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004837void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004838 // According to ECMA-262 section 11.11, page 58, the binary logical
4839 // operators must yield the result of one of the two expressions
4840 // before any ToBoolean() conversions. This means that the value
4841 // produced by a && or || operator is not necessarily a boolean.
4842
4843 // NOTE: If the left hand side produces a materialized value (not in
4844 // the CC register), we force the right hand side to do the
4845 // same. This is necessary because we may have to branch to the exit
4846 // after evaluating the left hand side (due to the shortcut
4847 // semantics), but the compiler must (statically) know if the result
4848 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004849 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004850 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004851 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004852 &is_true,
4853 false_target(),
4854 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004855 if (has_valid_frame() && !has_cc()) {
4856 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004857 JumpTarget pop_and_continue;
4858 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004859
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004860 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004861 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004862 // Avoid popping the result if it converts to 'false' using the
4863 // standard ToBoolean() conversion as described in ECMA-262,
4864 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004865 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004866 Branch(false, &exit);
4867
4868 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004869 pop_and_continue.Bind();
4870 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004871
4872 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004873 is_true.Bind();
4874 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004875
4876 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004877 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004878 } else if (has_cc() || is_true.is_linked()) {
4879 // The left-hand side is either (a) partially compiled to
4880 // control flow with a final branch left to emit or (b) fully
4881 // compiled to control flow and possibly true.
4882 if (has_cc()) {
4883 Branch(false, false_target());
4884 }
4885 is_true.Bind();
4886 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004887 true_target(),
4888 false_target(),
4889 false);
4890 } else {
4891 // Nothing to do.
4892 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004893 }
4894
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004895 } else {
4896 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004897 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004898 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004899 true_target(),
4900 &is_false,
4901 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004902 if (has_valid_frame() && !has_cc()) {
4903 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004904 JumpTarget pop_and_continue;
4905 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004906
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004907 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004908 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004909 // Avoid popping the result if it converts to 'true' using the
4910 // standard ToBoolean() conversion as described in ECMA-262,
4911 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004912 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004913 Branch(true, &exit);
4914
4915 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004916 pop_and_continue.Bind();
4917 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004918
4919 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004920 is_false.Bind();
4921 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004922
4923 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004924 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004925 } else if (has_cc() || is_false.is_linked()) {
4926 // The left-hand side is either (a) partially compiled to
4927 // control flow with a final branch left to emit or (b) fully
4928 // compiled to control flow and possibly false.
4929 if (has_cc()) {
4930 Branch(true, true_target());
4931 }
4932 is_false.Bind();
4933 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004934 true_target(),
4935 false_target(),
4936 false);
4937 } else {
4938 // Nothing to do.
4939 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004940 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004941 }
4942}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004943
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004944
4945void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4946#ifdef DEBUG
4947 int original_height = frame_->height();
4948#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004949 Comment cmnt(masm_, "[ BinaryOperation");
4950
4951 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004952 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004953 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004954 } else {
4955 // Optimize for the case where (at least) one of the expressions
4956 // is a literal small integer.
4957 Literal* lliteral = node->left()->AsLiteral();
4958 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004959 // NOTE: The code below assumes that the slow cases (calls to runtime)
4960 // never return a constant/immutable object.
4961 bool overwrite_left =
4962 (node->left()->AsBinaryOperation() != NULL &&
4963 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4964 bool overwrite_right =
4965 (node->right()->AsBinaryOperation() != NULL &&
4966 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004967
4968 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004969 VirtualFrame::RegisterAllocationScope scope(this);
4970 Load(node->left());
4971 VirtualFrameSmiOperation(
4972 node->op(),
4973 rliteral->handle(),
4974 false,
4975 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004976 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004977 VirtualFrame::RegisterAllocationScope scope(this);
4978 Load(node->right());
4979 VirtualFrameSmiOperation(node->op(),
4980 lliteral->handle(),
4981 true,
4982 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004983 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00004984 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004985 OverwriteMode overwrite_mode = NO_OVERWRITE;
4986 if (overwrite_left) {
4987 overwrite_mode = OVERWRITE_LEFT;
4988 } else if (overwrite_right) {
4989 overwrite_mode = OVERWRITE_RIGHT;
4990 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004991 Load(node->left());
4992 Load(node->right());
4993 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004994 }
4995 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004996 ASSERT(!has_valid_frame() ||
4997 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004998 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004999}
5000
5001
ager@chromium.org7c537e22008-10-16 08:43:32 +00005002void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005003#ifdef DEBUG
5004 int original_height = frame_->height();
5005#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00005006 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005007 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005008 frame_->EmitPush(r0);
5009 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005010}
5011
5012
ager@chromium.org7c537e22008-10-16 08:43:32 +00005013void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005014#ifdef DEBUG
5015 int original_height = frame_->height();
5016#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005017 Comment cmnt(masm_, "[ CompareOperation");
5018
ager@chromium.org357bf652010-04-12 11:30:10 +00005019 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021 // Get the expressions from the node.
5022 Expression* left = node->left();
5023 Expression* right = node->right();
5024 Token::Value op = node->op();
5025
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005026 // To make null checks efficient, we check if either left or right is the
5027 // literal 'null'. If so, we optimize the code by inlining a null check
5028 // instead of calling the (very) general runtime routine for checking
5029 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005031 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005032 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005033 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005034 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5035 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005036 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005037 Load(left_is_null ? right : left);
5038 Register tos = frame_->PopToRegister();
5039 // JumpTargets can't cope with register allocation yet.
5040 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005041 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005042 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005044 // The 'null' value is only equal to 'undefined' if using non-strict
5045 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005046 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005047 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005048
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005049 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005050 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005051 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052
ager@chromium.org357bf652010-04-12 11:30:10 +00005053 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005054 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005055
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005056 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005057 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5058 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5059 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5060 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061 }
5062
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005063 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005064 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005065 return;
5066 }
5067 }
5068
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005069 // To make typeof testing for natives implemented in JavaScript really
5070 // efficient, we generate special code for expressions of the form:
5071 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005072 UnaryOperation* operation = left->AsUnaryOperation();
5073 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5074 (operation != NULL && operation->op() == Token::TYPEOF) &&
5075 (right->AsLiteral() != NULL &&
5076 right->AsLiteral()->handle()->IsString())) {
5077 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5078
ager@chromium.org357bf652010-04-12 11:30:10 +00005079 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005080 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005081 Register tos = frame_->PopToRegister();
5082
5083 // JumpTargets can't cope with register allocation yet.
5084 frame_->SpillAll();
5085
5086 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005087
5088 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005089 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005090 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005091 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005092 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005093 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005094 cc_reg_ = eq;
5095
5096 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005097 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005098 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005099
ager@chromium.org357bf652010-04-12 11:30:10 +00005100 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005101
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005102 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005103 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5104 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5105 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005106 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005107
ager@chromium.org357bf652010-04-12 11:30:10 +00005108 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5109 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005110 cc_reg_ = lt;
5111
5112 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005113 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005114 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005115 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005116 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005117 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005118 cc_reg_ = eq;
5119
5120 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005121 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005122 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005123 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005124
ager@chromium.org357bf652010-04-12 11:30:10 +00005125 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005126 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005127
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005128 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005129 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5130 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5131 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5132 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005133
5134 cc_reg_ = eq;
5135
5136 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005137 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005138 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005139 Register map_reg = scratch;
5140 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005141 true_target()->Branch(eq);
5142 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00005143 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005144 cc_reg_ = eq;
5145
5146 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005147 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005148 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005149
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005150 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005151 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005152 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005153
ager@chromium.org357bf652010-04-12 11:30:10 +00005154 Register map_reg = scratch;
5155 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005156 false_target()->Branch(eq);
5157
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005158 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005159 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5160 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5161 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005162 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005163
ager@chromium.org357bf652010-04-12 11:30:10 +00005164 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5165 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005166 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00005167 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005168 cc_reg_ = le;
5169
5170 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005171 // Uncommon case: typeof testing against a string literal that is
5172 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005173 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005174 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005175 ASSERT(!has_valid_frame() ||
5176 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005177 return;
5178 }
5179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005180 switch (op) {
5181 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005182 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005183 break;
5184
5185 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005186 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005187 break;
5188
5189 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005190 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005191 break;
5192
5193 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005194 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005195 break;
5196
5197 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005198 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005199 break;
5200
5201 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005202 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005203 break;
5204
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005205 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005206 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005207 LoadAndSpill(left);
5208 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005209 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005210 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005211 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005212 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005213
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005214 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005215 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005216 LoadAndSpill(left);
5217 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005218 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005219 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005220 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005221 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005222 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005223 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005224 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005225
5226 default:
5227 UNREACHABLE();
5228 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005229 ASSERT((has_cc() && frame_->height() == original_height) ||
5230 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005231}
5232
5233
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005234class DeferredReferenceGetNamedValue: public DeferredCode {
5235 public:
5236 explicit DeferredReferenceGetNamedValue(Handle<String> name) : name_(name) {
5237 set_comment("[ DeferredReferenceGetNamedValue");
5238 }
5239
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005240 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005241
5242 private:
5243 Handle<String> name_;
5244};
5245
5246
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005247void DeferredReferenceGetNamedValue::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005248 Register scratch1 = VirtualFrame::scratch0();
5249 Register scratch2 = VirtualFrame::scratch1();
5250 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
5251 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
5252
5253 // Setup the registers and call load IC.
5254 // On entry to this deferred code, r0 is assumed to already contain the
5255 // receiver from the top of the stack.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005256 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005257
5258 // The rest of the instructions in the deferred code must be together.
5259 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5260 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
5261 __ Call(ic, RelocInfo::CODE_TARGET);
5262 // The call must be followed by a nop(1) instruction to indicate that the
5263 // in-object has been inlined.
5264 __ nop(PROPERTY_ACCESS_INLINED);
5265
5266 // Block the constant pool for one more instruction after leaving this
5267 // constant pool block scope to include the branch instruction ending the
5268 // deferred code.
5269 __ BlockConstPoolFor(1);
5270 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005271}
5272
5273
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005274class DeferredReferenceGetKeyedValue: public DeferredCode {
5275 public:
5276 DeferredReferenceGetKeyedValue() {
5277 set_comment("[ DeferredReferenceGetKeyedValue");
5278 }
5279
5280 virtual void Generate();
5281};
5282
5283
5284void DeferredReferenceGetKeyedValue::Generate() {
5285 Register scratch1 = VirtualFrame::scratch0();
5286 Register scratch2 = VirtualFrame::scratch1();
5287 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
5288 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
5289
5290 // The rest of the instructions in the deferred code must be together.
5291 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5292 // Call keyed load IC. It has all arguments on the stack.
5293 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
5294 __ Call(ic, RelocInfo::CODE_TARGET);
5295 // The call must be followed by a nop instruction to indicate that the
5296 // keyed load has been inlined.
5297 __ nop(PROPERTY_ACCESS_INLINED);
5298
5299 // Block the constant pool for one more instruction after leaving this
5300 // constant pool block scope to include the branch instruction ending the
5301 // deferred code.
5302 __ BlockConstPoolFor(1);
5303 }
5304}
5305
5306
5307class DeferredReferenceSetKeyedValue: public DeferredCode {
5308 public:
5309 DeferredReferenceSetKeyedValue() {
5310 set_comment("[ DeferredReferenceSetKeyedValue");
5311 }
5312
5313 virtual void Generate();
5314};
5315
5316
5317void DeferredReferenceSetKeyedValue::Generate() {
5318 Register scratch1 = VirtualFrame::scratch0();
5319 Register scratch2 = VirtualFrame::scratch1();
5320 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
5321 __ IncrementCounter(
5322 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
5323
5324 // The rest of the instructions in the deferred code must be together.
5325 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5326 // Call keyed load IC. It has receiver amd key on the stack and the value to
5327 // store in r0.
5328 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
5329 __ Call(ic, RelocInfo::CODE_TARGET);
5330 // The call must be followed by a nop instruction to indicate that the
5331 // keyed store has been inlined.
5332 __ nop(PROPERTY_ACCESS_INLINED);
5333
5334 // Block the constant pool for one more instruction after leaving this
5335 // constant pool block scope to include the branch instruction ending the
5336 // deferred code.
5337 __ BlockConstPoolFor(1);
5338 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005339}
5340
5341
5342void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
5343 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
5344 Comment cmnt(masm(), "[ Load from named Property");
5345 // Setup the name register and call load IC.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005346 frame_->SpillAllButCopyTOSToR0();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005347 __ mov(r2, Operand(name));
5348 frame_->CallLoadIC(is_contextual
5349 ? RelocInfo::CODE_TARGET_CONTEXT
5350 : RelocInfo::CODE_TARGET);
5351 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005352 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005353 Comment cmnt(masm(), "[ Inlined named property load");
5354
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005355 // Counter will be decremented in the deferred code. Placed here to avoid
5356 // having it in the instruction stream below where patching will occur.
5357 __ IncrementCounter(&Counters::named_load_inline, 1,
5358 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005359
5360 // The following instructions are the inlined load of an in-object property.
5361 // Parts of this code is patched, so the exact instructions generated needs
5362 // to be fixed. Therefore the instruction pool is blocked when generating
5363 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005364
5365 // Load the receiver from the stack.
5366 frame_->SpillAllButCopyTOSToR0();
5367
5368 DeferredReferenceGetNamedValue* deferred =
5369 new DeferredReferenceGetNamedValue(name);
5370
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005371#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005372 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005373 Label check_inlined_codesize;
5374 masm_->bind(&check_inlined_codesize);
5375#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005376
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005377 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005378 // Check that the receiver is a heap object.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005379 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005380 deferred->Branch(eq);
5381
5382 // Check the map. The null map used below is patched by the inline cache
5383 // code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005384 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005385 __ mov(r3, Operand(Factory::null_value()));
5386 __ cmp(r2, r3);
5387 deferred->Branch(ne);
5388
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005389 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005390 // inline cache code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005391 __ ldr(r0, MemOperand(r0, 0));
5392
5393 // Make sure that the expected number of instructions are generated.
5394 ASSERT_EQ(kInlinedNamedLoadInstructions,
5395 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005396 }
5397
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005398 deferred->BindExit();
5399 }
5400}
5401
5402
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005403void CodeGenerator::EmitKeyedLoad() {
5404 if (loop_nesting() == 0) {
5405 VirtualFrame::SpilledScope spilled(frame_);
5406 Comment cmnt(masm_, "[ Load from keyed property");
5407 frame_->CallKeyedLoadIC();
5408 } else {
5409 // Inline the keyed load.
5410 Comment cmnt(masm_, "[ Inlined load from keyed property");
5411
5412 // Counter will be decremented in the deferred code. Placed here to avoid
5413 // having it in the instruction stream below where patching will occur.
5414 __ IncrementCounter(&Counters::keyed_load_inline, 1,
5415 frame_->scratch0(), frame_->scratch1());
5416
5417 // Load the receiver and key from the stack.
5418 frame_->SpillAllButCopyTOSToR1R0();
5419 Register receiver = r0;
5420 Register key = r1;
5421 VirtualFrame::SpilledScope spilled(frame_);
5422
5423 DeferredReferenceGetKeyedValue* deferred =
5424 new DeferredReferenceGetKeyedValue();
5425
5426 // Check that the receiver is a heap object.
5427 __ tst(receiver, Operand(kSmiTagMask));
5428 deferred->Branch(eq);
5429
5430 // The following instructions are the part of the inlined load keyed
5431 // property code which can be patched. Therefore the exact number of
5432 // instructions generated need to be fixed, so the constant pool is blocked
5433 // while generating this code.
5434#ifdef DEBUG
5435 int kInlinedKeyedLoadInstructions = 19;
5436 Label check_inlined_codesize;
5437 masm_->bind(&check_inlined_codesize);
5438#endif
5439 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5440 Register scratch1 = VirtualFrame::scratch0();
5441 Register scratch2 = VirtualFrame::scratch1();
5442 // Check the map. The null map used below is patched by the inline cache
5443 // code.
5444 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
5445 __ mov(scratch2, Operand(Factory::null_value()));
5446 __ cmp(scratch1, scratch2);
5447 deferred->Branch(ne);
5448
5449 // Check that the key is a smi.
5450 __ tst(key, Operand(kSmiTagMask));
5451 deferred->Branch(ne);
5452
5453 // Get the elements array from the receiver and check that it
5454 // is not a dictionary.
5455 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
5456 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
5457 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5458 __ cmp(scratch2, ip);
5459 deferred->Branch(ne);
5460
5461 // Check that key is within bounds. Use unsigned comparison to handle
5462 // negative keys.
5463 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
5464 __ cmp(scratch2, Operand(key, ASR, kSmiTagSize));
5465 deferred->Branch(ls); // Unsigned less equal.
5466
5467 // Load and check that the result is not the hole (key is a smi).
5468 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
5469 __ add(scratch1,
5470 scratch1,
5471 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5472 __ ldr(r0,
5473 MemOperand(scratch1, key, LSL,
5474 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5475 __ cmp(r0, scratch2);
5476 // This is the only branch to deferred where r0 and r1 do not contain the
5477 // receiver and key. We can't just load undefined here because we have to
5478 // check the prototype.
5479 deferred->Branch(eq);
5480
5481 // Make sure that the expected number of instructions are generated.
5482 ASSERT_EQ(kInlinedKeyedLoadInstructions,
5483 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5484 }
5485
5486 deferred->BindExit();
5487 }
5488}
5489
5490
5491void CodeGenerator::EmitKeyedStore(StaticType* key_type) {
5492 frame_->AssertIsSpilled();
5493 // Generate inlined version of the keyed store if the code is in a loop
5494 // and the key is likely to be a smi.
5495 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
5496 // Inline the keyed store.
5497 Comment cmnt(masm_, "[ Inlined store to keyed property");
5498
5499 DeferredReferenceSetKeyedValue* deferred =
5500 new DeferredReferenceSetKeyedValue();
5501
5502 // Counter will be decremented in the deferred code. Placed here to avoid
5503 // having it in the instruction stream below where patching will occur.
5504 __ IncrementCounter(&Counters::keyed_store_inline, 1,
5505 frame_->scratch0(), frame_->scratch1());
5506
5507 // Check that the value is a smi. As this inlined code does not set the
5508 // write barrier it is only possible to store smi values.
5509 __ tst(r0, Operand(kSmiTagMask));
5510 deferred->Branch(ne);
5511
5512 // Load the key and receiver from the stack.
5513 __ ldr(r1, MemOperand(sp, 0));
5514 __ ldr(r2, MemOperand(sp, kPointerSize));
5515
5516 // Check that the key is a smi.
5517 __ tst(r1, Operand(kSmiTagMask));
5518 deferred->Branch(ne);
5519
5520 // Check that the receiver is a heap object.
5521 __ tst(r2, Operand(kSmiTagMask));
5522 deferred->Branch(eq);
5523
5524 // Check that the receiver is a JSArray.
5525 __ CompareObjectType(r2, r3, r3, JS_ARRAY_TYPE);
5526 deferred->Branch(ne);
5527
5528 // Check that the key is within bounds. Both the key and the length of
5529 // the JSArray are smis. Use unsigned comparison to handle negative keys.
5530 __ ldr(r3, FieldMemOperand(r2, JSArray::kLengthOffset));
5531 __ cmp(r3, r1);
5532 deferred->Branch(ls); // Unsigned less equal.
5533
5534 // The following instructions are the part of the inlined store keyed
5535 // property code which can be patched. Therefore the exact number of
5536 // instructions generated need to be fixed, so the constant pool is blocked
5537 // while generating this code.
5538#ifdef DEBUG
5539 int kInlinedKeyedStoreInstructions = 7;
5540 Label check_inlined_codesize;
5541 masm_->bind(&check_inlined_codesize);
5542#endif
5543 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5544 // Get the elements array from the receiver and check that it
5545 // is not a dictionary.
5546 __ ldr(r3, FieldMemOperand(r2, JSObject::kElementsOffset));
5547 __ ldr(r4, FieldMemOperand(r3, JSObject::kMapOffset));
5548 // Read the fixed array map from the constant pool (not from the root
5549 // array) so that the value can be patched. When debugging, we patch this
5550 // comparison to always fail so that we will hit the IC call in the
5551 // deferred code which will allow the debugger to break for fast case
5552 // stores.
5553 __ mov(r5, Operand(Factory::fixed_array_map()));
5554 __ cmp(r4, r5);
5555 deferred->Branch(ne);
5556
5557 // Store the value.
5558 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5559 __ str(r0, MemOperand(r3, r1, LSL,
5560 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5561
5562 // Make sure that the expected number of instructions are generated.
5563 ASSERT_EQ(kInlinedKeyedStoreInstructions,
5564 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5565 }
5566
5567 deferred->BindExit();
5568 } else {
5569 frame()->CallKeyedStoreIC();
5570 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005571}
5572
5573
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005574#ifdef DEBUG
5575bool CodeGenerator::HasValidEntryRegisters() { return true; }
5576#endif
5577
5578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005579#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005580#define __ ACCESS_MASM(masm)
5581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005582
ager@chromium.org7c537e22008-10-16 08:43:32 +00005583Handle<String> Reference::GetName() {
5584 ASSERT(type_ == NAMED);
5585 Property* property = expression_->AsProperty();
5586 if (property == NULL) {
5587 // Global variable reference treated as a named property reference.
5588 VariableProxy* proxy = expression_->AsVariableProxy();
5589 ASSERT(proxy->AsVariable() != NULL);
5590 ASSERT(proxy->AsVariable()->is_global());
5591 return proxy->name();
5592 } else {
5593 Literal* raw_name = property->key()->AsLiteral();
5594 ASSERT(raw_name != NULL);
5595 return Handle<String>(String::cast(*raw_name->handle()));
5596 }
5597}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005598
ager@chromium.org7c537e22008-10-16 08:43:32 +00005599
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005600void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005601 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005602 ASSERT(!is_illegal());
5603 ASSERT(!cgen_->has_cc());
5604 MacroAssembler* masm = cgen_->masm();
5605 Property* property = expression_->AsProperty();
5606 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005607 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005608 }
5609
5610 switch (type_) {
5611 case SLOT: {
5612 Comment cmnt(masm, "[ Load from Slot");
5613 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
5614 ASSERT(slot != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005615 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005616 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005617 }
5618
ager@chromium.org7c537e22008-10-16 08:43:32 +00005619 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00005620 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005621 bool is_global = var != NULL;
5622 ASSERT(!is_global || var->is_global());
5623 cgen_->EmitNamedLoad(GetName(), is_global);
5624 cgen_->frame()->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005625 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005626 }
5627
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005628 case KEYED: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005629 ASSERT(property != NULL);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005630 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005631 cgen_->frame()->EmitPush(r0);
5632 break;
5633 }
5634
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005635 default:
5636 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005637 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005638
5639 if (!persist_after_get_) {
5640 cgen_->UnloadReference(this);
5641 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005642}
5643
5644
ager@chromium.org7c537e22008-10-16 08:43:32 +00005645void Reference::SetValue(InitState init_state) {
5646 ASSERT(!is_illegal());
5647 ASSERT(!cgen_->has_cc());
5648 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005649 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005650 Property* property = expression_->AsProperty();
5651 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005652 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005653 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005654
ager@chromium.org7c537e22008-10-16 08:43:32 +00005655 switch (type_) {
5656 case SLOT: {
5657 Comment cmnt(masm, "[ Store to Slot");
5658 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005659 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005660 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005661 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005662 }
5663
ager@chromium.org7c537e22008-10-16 08:43:32 +00005664 case NAMED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005665 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005666 Comment cmnt(masm, "[ Store to named Property");
5667 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005668 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005669 Handle<String> name(GetName());
5670
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005671 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005672 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005673 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005674 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005675 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005676 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005677 break;
5678 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005679
ager@chromium.org7c537e22008-10-16 08:43:32 +00005680 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005681 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005682 Comment cmnt(masm, "[ Store to keyed Property");
5683 Property* property = expression_->AsProperty();
5684 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005685 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005686
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005687 frame->EmitPop(r0); // Value.
5688 cgen_->EmitKeyedStore(property->key()->type());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005689 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005690 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005691 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005692 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005693
5694 default:
5695 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005696 }
5697}
5698
5699
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005700void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005701 // Create a new closure from the given function info in new
5702 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005703 Label gc;
5704
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005705 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005706 __ pop(r3);
5707
5708 // Attempt to allocate new JSFunction in new space.
5709 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
5710 r0,
5711 r1,
5712 r2,
5713 &gc,
5714 TAG_OBJECT);
5715
5716 // Compute the function map in the current global context and set that
5717 // as the map of the allocated object.
5718 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5719 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5720 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5721 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5722
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005723 // Initialize the rest of the function. We don't have to update the
5724 // write barrier because the allocated object is in new space.
5725 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5726 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5727 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5728 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5729 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5730 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5731 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5732 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005733
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005734 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005735 __ Ret();
5736
5737 // Create a new closure through the slower runtime call.
5738 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005739 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005740 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005741}
5742
5743
5744void FastNewContextStub::Generate(MacroAssembler* masm) {
5745 // Try to allocate the context in new space.
5746 Label gc;
5747 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5748
5749 // Attempt to allocate the context in new space.
5750 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
5751 r0,
5752 r1,
5753 r2,
5754 &gc,
5755 TAG_OBJECT);
5756
5757 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005758 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005759
5760 // Setup the object header.
5761 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5762 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5763 __ mov(r2, Operand(length));
5764 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5765
5766 // Setup the fixed slots.
5767 __ mov(r1, Operand(Smi::FromInt(0)));
5768 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5769 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5770 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5771 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5772
5773 // Copy the global object from the surrounding context.
5774 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5775 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5776
5777 // Initialize the rest of the slots to undefined.
5778 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5779 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5780 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5781 }
5782
5783 // Remove the on-stack argument and return.
5784 __ mov(cp, r0);
5785 __ pop();
5786 __ Ret();
5787
5788 // Need to collect. Call into runtime system.
5789 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005790 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005791}
5792
5793
ager@chromium.org5c838252010-02-19 08:53:10 +00005794void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5795 // Stack layout on entry:
5796 //
5797 // [sp]: constant elements.
5798 // [sp + kPointerSize]: literal index.
5799 // [sp + (2 * kPointerSize)]: literals array.
5800
5801 // All sizes here are multiples of kPointerSize.
5802 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5803 int size = JSArray::kSize + elements_size;
5804
5805 // Load boilerplate object into r3 and check if we need to create a
5806 // boilerplate.
5807 Label slow_case;
5808 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5809 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5810 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5811 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5812 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5813 __ cmp(r3, ip);
5814 __ b(eq, &slow_case);
5815
5816 // Allocate both the JS array and the elements array in one big
5817 // allocation. This avoids multiple limit checks.
5818 __ AllocateInNewSpace(size / kPointerSize,
5819 r0,
5820 r1,
5821 r2,
5822 &slow_case,
5823 TAG_OBJECT);
5824
5825 // Copy the JS array part.
5826 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5827 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5828 __ ldr(r1, FieldMemOperand(r3, i));
5829 __ str(r1, FieldMemOperand(r0, i));
5830 }
5831 }
5832
5833 if (length_ > 0) {
5834 // Get hold of the elements array of the boilerplate and setup the
5835 // elements pointer in the resulting object.
5836 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5837 __ add(r2, r0, Operand(JSArray::kSize));
5838 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
5839
5840 // Copy the elements array.
5841 for (int i = 0; i < elements_size; i += kPointerSize) {
5842 __ ldr(r1, FieldMemOperand(r3, i));
5843 __ str(r1, FieldMemOperand(r2, i));
5844 }
5845 }
5846
5847 // Return and remove the on-stack parameters.
5848 __ add(sp, sp, Operand(3 * kPointerSize));
5849 __ Ret();
5850
5851 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005852 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005853}
5854
5855
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005856// Takes a Smi and converts to an IEEE 64 bit floating point value in two
5857// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
5858// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
5859// scratch register. Destroys the source register. No GC occurs during this
5860// stub so you don't have to set up the frame.
5861class ConvertToDoubleStub : public CodeStub {
5862 public:
5863 ConvertToDoubleStub(Register result_reg_1,
5864 Register result_reg_2,
5865 Register source_reg,
5866 Register scratch_reg)
5867 : result1_(result_reg_1),
5868 result2_(result_reg_2),
5869 source_(source_reg),
5870 zeros_(scratch_reg) { }
5871
5872 private:
5873 Register result1_;
5874 Register result2_;
5875 Register source_;
5876 Register zeros_;
5877
5878 // Minor key encoding in 16 bits.
5879 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
5880 class OpBits: public BitField<Token::Value, 2, 14> {};
5881
5882 Major MajorKey() { return ConvertToDouble; }
5883 int MinorKey() {
5884 // Encode the parameters in a unique 16 bit value.
5885 return result1_.code() +
5886 (result2_.code() << 4) +
5887 (source_.code() << 8) +
5888 (zeros_.code() << 12);
5889 }
5890
5891 void Generate(MacroAssembler* masm);
5892
5893 const char* GetName() { return "ConvertToDoubleStub"; }
5894
5895#ifdef DEBUG
5896 void Print() { PrintF("ConvertToDoubleStub\n"); }
5897#endif
5898};
5899
5900
5901void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
5902#ifndef BIG_ENDIAN_FLOATING_POINT
5903 Register exponent = result1_;
5904 Register mantissa = result2_;
5905#else
5906 Register exponent = result2_;
5907 Register mantissa = result1_;
5908#endif
5909 Label not_special;
5910 // Convert from Smi to integer.
5911 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
5912 // Move sign bit from source to destination. This works because the sign bit
5913 // in the exponent word of the double has the same position and polarity as
5914 // the 2's complement sign bit in a Smi.
5915 ASSERT(HeapNumber::kSignMask == 0x80000000u);
5916 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
5917 // Subtract from 0 if source was negative.
5918 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005919
5920 // We have -1, 0 or 1, which we treat specially. Register source_ contains
5921 // absolute value: it is either equal to 1 (special case of -1 and 1),
5922 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005923 __ cmp(source_, Operand(1));
5924 __ b(gt, &not_special);
5925
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005926 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
5927 static const uint32_t exponent_word_for_1 =
5928 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005929 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005930 // 1, 0 and -1 all have 0 for the second word.
5931 __ mov(mantissa, Operand(0));
5932 __ Ret();
5933
5934 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005935 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005936 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005937 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005938 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005939 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005940 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
5941 __ orr(exponent,
5942 exponent,
5943 Operand(mantissa, LSL, HeapNumber::kExponentShift));
5944 // Shift up the source chopping the top bit off.
5945 __ add(zeros_, zeros_, Operand(1));
5946 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
5947 __ mov(source_, Operand(source_, LSL, zeros_));
5948 // Compute lower part of fraction (last 12 bits).
5949 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
5950 // And the top (top 20 bits).
5951 __ orr(exponent,
5952 exponent,
5953 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
5954 __ Ret();
5955}
5956
5957
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005958// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005959void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005960 Label max_negative_int;
5961 // the_int_ has the answer which is a signed int32 but not a Smi.
5962 // We test for the special value that has a different exponent. This test
5963 // has the neat side effect of setting the flags according to the sign.
5964 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005965 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005966 __ b(eq, &max_negative_int);
5967 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
5968 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
5969 uint32_t non_smi_exponent =
5970 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5971 __ mov(scratch_, Operand(non_smi_exponent));
5972 // Set the sign bit in scratch_ if the value was negative.
5973 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
5974 // Subtract from 0 if the value was negative.
5975 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
5976 // We should be masking the implict first digit of the mantissa away here,
5977 // but it just ends up combining harmlessly with the last digit of the
5978 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
5979 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
5980 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
5981 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5982 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
5983 __ str(scratch_, FieldMemOperand(the_heap_number_,
5984 HeapNumber::kExponentOffset));
5985 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
5986 __ str(scratch_, FieldMemOperand(the_heap_number_,
5987 HeapNumber::kMantissaOffset));
5988 __ Ret();
5989
5990 __ bind(&max_negative_int);
5991 // The max negative int32 is stored as a positive number in the mantissa of
5992 // a double because it uses a sign bit instead of using two's complement.
5993 // The actual mantissa bits stored are all 0 because the implicit most
5994 // significant 1 bit is not stored.
5995 non_smi_exponent += 1 << HeapNumber::kExponentShift;
5996 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
5997 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
5998 __ mov(ip, Operand(0));
5999 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
6000 __ Ret();
6001}
6002
6003
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006004// Handle the case where the lhs and rhs are the same object.
6005// Equality is almost reflexive (everything but NaN), so this is a test
6006// for "identity and not NaN".
6007static void EmitIdenticalObjectComparison(MacroAssembler* masm,
6008 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006009 Condition cc,
6010 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006011 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006012 Label heap_number, return_equal;
6013 Register exp_mask_reg = r5;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006014 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006015 __ b(ne, &not_identical);
6016
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006017 // The two objects are identical. If we know that one of them isn't NaN then
6018 // we now know they test equal.
6019 if (cc != eq || !never_nan_nan) {
6020 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006021
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006022 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
6023 // so we do the second best thing - test it ourselves.
6024 // They are both equal and they are not both Smis so both of them are not
6025 // Smis. If it's not a heap number, then return equal.
6026 if (cc == lt || cc == gt) {
6027 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006028 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006029 } else {
6030 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6031 __ b(eq, &heap_number);
6032 // Comparing JS objects with <=, >= is complicated.
6033 if (cc != eq) {
6034 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
6035 __ b(ge, slow);
6036 // Normally here we fall through to return_equal, but undefined is
6037 // special: (undefined == undefined) == true, but
6038 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
6039 if (cc == le || cc == ge) {
6040 __ cmp(r4, Operand(ODDBALL_TYPE));
6041 __ b(ne, &return_equal);
6042 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006043 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006044 __ b(ne, &return_equal);
6045 if (cc == le) {
6046 // undefined <= undefined should fail.
6047 __ mov(r0, Operand(GREATER));
6048 } else {
6049 // undefined >= undefined should fail.
6050 __ mov(r0, Operand(LESS));
6051 }
6052 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006053 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006054 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006055 }
6056 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006057
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006058 __ bind(&return_equal);
6059 if (cc == lt) {
6060 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
6061 } else if (cc == gt) {
6062 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
6063 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006064 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006065 }
6066 __ mov(pc, Operand(lr)); // Return.
6067
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006068 if (cc != eq || !never_nan_nan) {
6069 // For less and greater we don't have to check for NaN since the result of
6070 // x < x is false regardless. For the others here is some code to check
6071 // for NaN.
6072 if (cc != lt && cc != gt) {
6073 __ bind(&heap_number);
6074 // It is a heap number, so return non-equal if it's NaN and equal if it's
6075 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006076
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006077 // The representation of NaN values has all exponent bits (52..62) set,
6078 // and not all mantissa bits (0..51) clear.
6079 // Read top bits of double representation (second word of value).
6080 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6081 // Test that exponent bits are all set.
6082 __ and_(r3, r2, Operand(exp_mask_reg));
6083 __ cmp(r3, Operand(exp_mask_reg));
6084 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006085
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006086 // Shift out flag and all exponent bits, retaining only mantissa.
6087 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
6088 // Or with all low-bits of mantissa.
6089 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6090 __ orr(r0, r3, Operand(r2), SetCC);
6091 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006092 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
6093 // not (it's a NaN). For <= and >= we need to load r0 with the failing
6094 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006095 if (cc != eq) {
6096 // All-zero means Infinity means equal.
6097 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
6098 if (cc == le) {
6099 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
6100 } else {
6101 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
6102 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006103 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006104 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006105 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006106 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006107 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006108
6109 __ bind(&not_identical);
6110}
6111
6112
6113// See comment at call site.
6114static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006115 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006116 Label* slow,
6117 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006118 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006119 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006120 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006121
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006122 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006123 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6124 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006125 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006126 // succeed. Return non-equal (r0 is already not zero)
6127 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6128 } else {
6129 // Smi compared non-strictly with a non-Smi non-heap-number. Call
6130 // the runtime.
6131 __ b(ne, slow);
6132 }
6133
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006134 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006135 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006136 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006137 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006138 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6139 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006140 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006141 // Load the double from rhs, tagged HeapNumber r0, to d6.
6142 __ sub(r7, r0, Operand(kHeapObjectTag));
6143 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006144 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006145 __ push(lr);
6146 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006147 __ mov(r7, Operand(r1));
6148 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6149 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006150 // Load rhs to a double in r0, r1.
6151 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
6152 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
6153 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006154 }
6155
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006156 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006157 // since it's a smi.
6158 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006159
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006160 __ bind(&rhs_is_smi);
6161 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006162 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6163 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006164 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006165 // succeed. Return non-equal.
6166 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
6167 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6168 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006169 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006170 // the runtime.
6171 __ b(ne, slow);
6172 }
6173
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006174 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006175 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006176 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006177 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006178 // Load the double from lhs, tagged HeapNumber r1, to d7.
6179 __ sub(r7, r1, Operand(kHeapObjectTag));
6180 __ vldr(d7, r7, HeapNumber::kValueOffset);
6181 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6182 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006183 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006184 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006185 __ push(lr);
6186 // Load lhs to a double in r2, r3.
6187 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6188 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6189 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006190 __ mov(r7, Operand(r0));
6191 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6192 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006193 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006194 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006195 // Fall through to both_loaded_as_doubles.
6196}
6197
6198
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006199void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006200 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006201 Register rhs_exponent = exp_first ? r0 : r1;
6202 Register lhs_exponent = exp_first ? r2 : r3;
6203 Register rhs_mantissa = exp_first ? r1 : r0;
6204 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006205 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006206 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006207
6208 Register exp_mask_reg = r5;
6209
6210 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006211 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
6212 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006213 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006214 __ mov(r4,
6215 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6216 SetCC);
6217 __ b(ne, &one_is_nan);
6218 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006219 __ b(ne, &one_is_nan);
6220
6221 __ bind(lhs_not_nan);
6222 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
6223 __ bind(&lhs_not_nan_exp_mask_is_loaded);
6224 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
6225 __ cmp(r4, Operand(exp_mask_reg));
6226 __ b(ne, &neither_is_nan);
6227 __ mov(r4,
6228 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6229 SetCC);
6230 __ b(ne, &one_is_nan);
6231 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006232 __ b(eq, &neither_is_nan);
6233
6234 __ bind(&one_is_nan);
6235 // NaN comparisons always fail.
6236 // Load whatever we need in r0 to make the comparison fail.
6237 if (cc == lt || cc == le) {
6238 __ mov(r0, Operand(GREATER));
6239 } else {
6240 __ mov(r0, Operand(LESS));
6241 }
6242 __ mov(pc, Operand(lr)); // Return.
6243
6244 __ bind(&neither_is_nan);
6245}
6246
6247
6248// See comment at call site.
6249static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
6250 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006251 Register rhs_exponent = exp_first ? r0 : r1;
6252 Register lhs_exponent = exp_first ? r2 : r3;
6253 Register rhs_mantissa = exp_first ? r1 : r0;
6254 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006255
6256 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
6257 if (cc == eq) {
6258 // Doubles are not equal unless they have the same bit pattern.
6259 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006260 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
6261 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006262 // Return non-zero if the numbers are unequal.
6263 __ mov(pc, Operand(lr), LeaveCC, ne);
6264
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006265 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006266 // If exponents are equal then return 0.
6267 __ mov(pc, Operand(lr), LeaveCC, eq);
6268
6269 // Exponents are unequal. The only way we can return that the numbers
6270 // are equal is if one is -0 and the other is 0. We already dealt
6271 // with the case where both are -0 or both are 0.
6272 // We start by seeing if the mantissas (that are equal) or the bottom
6273 // 31 bits of the rhs exponent are non-zero. If so we return not
6274 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006275 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006276 __ mov(r0, Operand(r4), LeaveCC, ne);
6277 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
6278 // Now they are equal if and only if the lhs exponent is zero in its
6279 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006280 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006281 __ mov(pc, Operand(lr));
6282 } else {
6283 // Call a native function to do a comparison between two non-NaNs.
6284 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006285 __ push(lr);
6286 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
6287 __ CallCFunction(ExternalReference::compare_doubles(), 4);
6288 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006289 }
6290}
6291
6292
6293// See comment at call site.
6294static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
6295 // If either operand is a JSObject or an oddball value, then they are
6296 // not equal since their pointers are different.
6297 // There is no test for undetectability in strict equality.
6298 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
6299 Label first_non_object;
6300 // Get the type of the first operand into r2 and compare it with
6301 // FIRST_JS_OBJECT_TYPE.
6302 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
6303 __ b(lt, &first_non_object);
6304
6305 // Return non-zero (r0 is not zero)
6306 Label return_not_equal;
6307 __ bind(&return_not_equal);
6308 __ mov(pc, Operand(lr)); // Return.
6309
6310 __ bind(&first_non_object);
6311 // Check for oddballs: true, false, null, undefined.
6312 __ cmp(r2, Operand(ODDBALL_TYPE));
6313 __ b(eq, &return_not_equal);
6314
6315 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
6316 __ b(ge, &return_not_equal);
6317
6318 // Check for oddballs: true, false, null, undefined.
6319 __ cmp(r3, Operand(ODDBALL_TYPE));
6320 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006321
6322 // Now that we have the types we might as well check for symbol-symbol.
6323 // Ensure that no non-strings have the symbol bit set.
6324 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6325 ASSERT(kSymbolTag != 0);
6326 __ and_(r2, r2, Operand(r3));
6327 __ tst(r2, Operand(kIsSymbolMask));
6328 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006329}
6330
6331
6332// See comment at call site.
6333static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
6334 Label* both_loaded_as_doubles,
6335 Label* not_heap_numbers,
6336 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006337 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006338 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006339 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
6340 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006341 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
6342
6343 // Both are heap numbers. Load them up then jump to the code we have
6344 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006345 if (CpuFeatures::IsSupported(VFP3)) {
6346 CpuFeatures::Scope scope(VFP3);
6347 __ sub(r7, r0, Operand(kHeapObjectTag));
6348 __ vldr(d6, r7, HeapNumber::kValueOffset);
6349 __ sub(r7, r1, Operand(kHeapObjectTag));
6350 __ vldr(d7, r7, HeapNumber::kValueOffset);
6351 } else {
6352 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6353 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6354 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
6355 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
6356 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006357 __ jmp(both_loaded_as_doubles);
6358}
6359
6360
6361// Fast negative check for symbol-to-symbol equality.
6362static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
6363 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006364 // Ensure that no non-strings have the symbol bit set.
6365 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6366 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006367 __ tst(r2, Operand(kIsSymbolMask));
6368 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006369 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
6370 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006371 __ tst(r3, Operand(kIsSymbolMask));
6372 __ b(eq, slow);
6373
6374 // Both are symbols. We already checked they weren't the same pointer
6375 // so they are not equal.
6376 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
6377 __ mov(pc, Operand(lr)); // Return.
6378}
6379
6380
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006381void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
6382 Register object,
6383 Register result,
6384 Register scratch1,
6385 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006386 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006387 bool object_is_smi,
6388 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006389 // Use of registers. Register result is used as a temporary.
6390 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006391 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006392
6393 // Load the number string cache.
6394 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
6395
6396 // Make the hash mask from the length of the number string cache. It
6397 // contains two elements (number and string) for each cache entry.
6398 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
6399 // Divide length by two (length is not a smi).
6400 __ mov(mask, Operand(mask, ASR, 1));
6401 __ sub(mask, mask, Operand(1)); // Make mask.
6402
6403 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006404 // number string cache for smis is just the smi value, and the hash for
6405 // doubles is the xor of the upper and lower words. See
6406 // Heap::GetNumberStringCache.
6407 Label is_smi;
6408 Label load_result_from_cache;
6409 if (!object_is_smi) {
6410 __ BranchOnSmi(object, &is_smi);
6411 if (CpuFeatures::IsSupported(VFP3)) {
6412 CpuFeatures::Scope scope(VFP3);
6413 __ CheckMap(object,
6414 scratch1,
6415 Factory::heap_number_map(),
6416 not_found,
6417 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006418
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006419 ASSERT_EQ(8, kDoubleSize);
6420 __ add(scratch1,
6421 object,
6422 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
6423 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
6424 __ eor(scratch1, scratch1, Operand(scratch2));
6425 __ and_(scratch1, scratch1, Operand(mask));
6426
6427 // Calculate address of entry in string cache: each entry consists
6428 // of two pointer sized fields.
6429 __ add(scratch1,
6430 number_string_cache,
6431 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
6432
6433 Register probe = mask;
6434 __ ldr(probe,
6435 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
6436 __ BranchOnSmi(probe, not_found);
6437 __ sub(scratch2, object, Operand(kHeapObjectTag));
6438 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
6439 __ sub(probe, probe, Operand(kHeapObjectTag));
6440 __ vldr(d1, probe, HeapNumber::kValueOffset);
6441 __ vcmp(d0, d1);
6442 __ vmrs(pc);
6443 __ b(ne, not_found); // The cache did not contain this value.
6444 __ b(&load_result_from_cache);
6445 } else {
6446 __ b(not_found);
6447 }
6448 }
6449
6450 __ bind(&is_smi);
6451 Register scratch = scratch1;
6452 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006453 // Calculate address of entry in string cache: each entry consists
6454 // of two pointer sized fields.
6455 __ add(scratch,
6456 number_string_cache,
6457 Operand(scratch, LSL, kPointerSizeLog2 + 1));
6458
6459 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006460 Register probe = mask;
6461 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
6462 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006463 __ b(ne, not_found);
6464
6465 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006466 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006467 __ ldr(result,
6468 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006469 __ IncrementCounter(&Counters::number_to_string_native,
6470 1,
6471 scratch1,
6472 scratch2);
6473}
6474
6475
6476void NumberToStringStub::Generate(MacroAssembler* masm) {
6477 Label runtime;
6478
6479 __ ldr(r1, MemOperand(sp, 0));
6480
6481 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006482 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006483 __ add(sp, sp, Operand(1 * kPointerSize));
6484 __ Ret();
6485
6486 __ bind(&runtime);
6487 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006488 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006489}
6490
6491
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006492// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
6493// On exit r0 is 0, positive or negative to indicate the result of
6494// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006495void CompareStub::Generate(MacroAssembler* masm) {
6496 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006497 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006498
6499 // NOTICE! This code is only reached after a smi-fast-case check, so
6500 // it is certain that at least one operand isn't a smi.
6501
6502 // Handle the case where the objects are identical. Either returns the answer
6503 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006504 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006505
6506 // If either is a Smi (we know that not both are), then they can only
6507 // be strictly equal if the other is a HeapNumber.
6508 ASSERT_EQ(0, kSmiTag);
6509 ASSERT_EQ(0, Smi::FromInt(0));
6510 __ and_(r2, r0, Operand(r1));
6511 __ tst(r2, Operand(kSmiTagMask));
6512 __ b(ne, &not_smis);
6513 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
6514 // 1) Return the answer.
6515 // 2) Go to slow.
6516 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006517 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006518 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006519 // comparison. If VFP3 is supported the double values of the numbers have
6520 // been loaded into d7 and d6. Otherwise, the double values have been loaded
6521 // into r0, r1, r2, and r3.
6522 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006523
6524 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006525 // The arguments have been converted to doubles and stored in d6 and d7, if
6526 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006527 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006528 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006529 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006530 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006531 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006532 __ vcmp(d7, d6);
6533 __ vmrs(pc); // Move vector status bits to normal status bits.
6534 Label nan;
6535 __ b(vs, &nan);
6536 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6537 __ mov(r0, Operand(LESS), LeaveCC, lt);
6538 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6539 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006540
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006541 __ bind(&nan);
6542 // If one of the sides was a NaN then the v flag is set. Load r0 with
6543 // whatever it takes to make the comparison fail, since comparisons with NaN
6544 // always fail.
6545 if (cc_ == lt || cc_ == le) {
6546 __ mov(r0, Operand(GREATER));
6547 } else {
6548 __ mov(r0, Operand(LESS));
6549 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006550 __ mov(pc, Operand(lr));
6551 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006552 // Checks for NaN in the doubles we have loaded. Can return the answer or
6553 // fall through if neither is a NaN. Also binds lhs_not_nan.
6554 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006555 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
6556 // answer. Never falls through.
6557 EmitTwoNonNanDoubleComparison(masm, cc_);
6558 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006559
6560 __ bind(&not_smis);
6561 // At this point we know we are dealing with two different objects,
6562 // and neither of them is a Smi. The objects are in r0 and r1.
6563 if (strict_) {
6564 // This returns non-equal for some object types, or falls through if it
6565 // was not lucky.
6566 EmitStrictTwoHeapObjectCompare(masm);
6567 }
6568
6569 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006570 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006571 // Check for heap-number-heap-number comparison. Can jump to slow case,
6572 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
6573 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006574 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006575 EmitCheckForTwoHeapNumbers(masm,
6576 &both_loaded_as_doubles,
6577 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006578 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006579
6580 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006581 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
6582 // symbols.
6583 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006584 // Either jumps to slow or returns the answer. Assumes that r2 is the type
6585 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006586 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006587 }
6588
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006589 // Check for both being sequential ASCII strings, and inline if that is the
6590 // case.
6591 __ bind(&flat_string_check);
6592
6593 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
6594
6595 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
6596 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
6597 r1,
6598 r0,
6599 r2,
6600 r3,
6601 r4,
6602 r5);
6603 // Never falls through to here.
6604
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006605 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006606
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006607 __ Push(r1, r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006608 // Figure out which native to call and setup the arguments.
6609 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006610 if (cc_ == eq) {
6611 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
6612 } else {
6613 native = Builtins::COMPARE;
6614 int ncr; // NaN compare result
6615 if (cc_ == lt || cc_ == le) {
6616 ncr = GREATER;
6617 } else {
6618 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
6619 ncr = LESS;
6620 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006621 __ mov(r0, Operand(Smi::FromInt(ncr)));
6622 __ push(r0);
6623 }
6624
6625 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
6626 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006627 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006628}
6629
6630
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00006631// We fall into this code if the operands were Smis, but the result was
6632// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006633// the operands were not both Smi. The operands are in r0 and r1. In order
6634// to call the C-implemented binary fp operation routines we need to end up
6635// with the double precision floating point operands in r0 and r1 (for the
6636// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00006637void GenericBinaryOpStub::HandleBinaryOpSlowCases(
6638 MacroAssembler* masm,
6639 Label* not_smi,
6640 Register lhs,
6641 Register rhs,
6642 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006643 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00006644 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006645
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006646 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ager@chromium.org357bf652010-04-12 11:30:10 +00006647
6648 if (ShouldGenerateSmiCode()) {
6649 // Smi-smi case (overflow).
6650 // Since both are Smis there is no heap number to overwrite, so allocate.
6651 // The new heap number is in r5. r6 and r7 are scratch.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006652 __ AllocateHeapNumber(r5, r6, r7, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006653
6654 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
6655 // using registers d7 and d6 for the double values.
6656 if (use_fp_registers) {
6657 CpuFeatures::Scope scope(VFP3);
6658 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
6659 __ vmov(s15, r7);
6660 __ vcvt_f64_s32(d7, s15);
6661 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
6662 __ vmov(s13, r7);
6663 __ vcvt_f64_s32(d6, s13);
6664 } else {
6665 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
6666 __ mov(r7, Operand(rhs));
6667 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6668 __ push(lr);
6669 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
6670 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
6671 __ mov(r7, Operand(lhs));
6672 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6673 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
6674 __ pop(lr);
6675 }
6676 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006677 }
6678
ager@chromium.org357bf652010-04-12 11:30:10 +00006679 // We branch here if at least one of r0 and r1 is not a Smi.
6680 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006681
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006682 // After this point we have the left hand side in r1 and the right hand side
6683 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006684 if (lhs.is(r0)) {
6685 __ Swap(r0, r1, ip);
6686 }
6687
6688 if (ShouldGenerateFPCode()) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006689 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
6690
ager@chromium.org357bf652010-04-12 11:30:10 +00006691 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
6692 switch (op_) {
6693 case Token::ADD:
6694 case Token::SUB:
6695 case Token::MUL:
6696 case Token::DIV:
6697 GenerateTypeTransition(masm);
6698 break;
6699
6700 default:
6701 break;
6702 }
6703 }
6704
6705 if (mode_ == NO_OVERWRITE) {
6706 // In the case where there is no chance of an overwritable float we may as
6707 // well do the allocation immediately while r0 and r1 are untouched.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006708 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006709 }
6710
6711 // Move r0 to a double in r2-r3.
6712 __ tst(r0, Operand(kSmiTagMask));
6713 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
6714 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6715 __ b(ne, &slow);
6716 if (mode_ == OVERWRITE_RIGHT) {
6717 __ mov(r5, Operand(r0)); // Overwrite this heap number.
6718 }
6719 if (use_fp_registers) {
6720 CpuFeatures::Scope scope(VFP3);
6721 // Load the double from tagged HeapNumber r0 to d7.
6722 __ sub(r7, r0, Operand(kHeapObjectTag));
6723 __ vldr(d7, r7, HeapNumber::kValueOffset);
6724 } else {
6725 // Calling convention says that second double is in r2 and r3.
6726 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
6727 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
6728 }
6729 __ jmp(&finished_loading_r0);
6730 __ bind(&r0_is_smi);
6731 if (mode_ == OVERWRITE_RIGHT) {
6732 // We can't overwrite a Smi so get address of new heap number into r5.
6733 __ AllocateHeapNumber(r5, r6, r7, &slow);
6734 }
6735
6736 if (use_fp_registers) {
6737 CpuFeatures::Scope scope(VFP3);
6738 // Convert smi in r0 to double in d7.
6739 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6740 __ vmov(s15, r7);
6741 __ vcvt_f64_s32(d7, s15);
6742 } else {
6743 // Write Smi from r0 to r3 and r2 in double format.
6744 __ mov(r7, Operand(r0));
6745 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6746 __ push(lr);
6747 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6748 __ pop(lr);
6749 }
6750
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006751 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
6752 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
6753 Label r1_is_not_smi;
6754 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
6755 __ tst(r1, Operand(kSmiTagMask));
6756 __ b(ne, &r1_is_not_smi);
6757 GenerateTypeTransition(masm);
6758 __ jmp(&r1_is_smi);
6759 }
6760
ager@chromium.org357bf652010-04-12 11:30:10 +00006761 __ bind(&finished_loading_r0);
6762
6763 // Move r1 to a double in r0-r1.
6764 __ tst(r1, Operand(kSmiTagMask));
6765 __ 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 +00006766 __ bind(&r1_is_not_smi);
ager@chromium.org357bf652010-04-12 11:30:10 +00006767 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6768 __ b(ne, &slow);
6769 if (mode_ == OVERWRITE_LEFT) {
6770 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6771 }
6772 if (use_fp_registers) {
6773 CpuFeatures::Scope scope(VFP3);
6774 // Load the double from tagged HeapNumber r1 to d6.
6775 __ sub(r7, r1, Operand(kHeapObjectTag));
6776 __ vldr(d6, r7, HeapNumber::kValueOffset);
6777 } else {
6778 // Calling convention says that first double is in r0 and r1.
6779 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
6780 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
6781 }
6782 __ jmp(&finished_loading_r1);
6783 __ bind(&r1_is_smi);
6784 if (mode_ == OVERWRITE_LEFT) {
6785 // We can't overwrite a Smi so get address of new heap number into r5.
6786 __ AllocateHeapNumber(r5, r6, r7, &slow);
6787 }
6788
6789 if (use_fp_registers) {
6790 CpuFeatures::Scope scope(VFP3);
6791 // Convert smi in r1 to double in d6.
6792 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6793 __ vmov(s13, r7);
6794 __ vcvt_f64_s32(d6, s13);
6795 } else {
6796 // Write Smi from r1 to r1 and r0 in double format.
6797 __ mov(r7, Operand(r1));
6798 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6799 __ push(lr);
6800 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6801 __ pop(lr);
6802 }
6803
6804 __ bind(&finished_loading_r1);
6805
6806 __ bind(&do_the_call);
6807 // If we are inlining the operation using VFP3 instructions for
6808 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6809 if (use_fp_registers) {
6810 CpuFeatures::Scope scope(VFP3);
6811 // ARMv7 VFP3 instructions to implement
6812 // double precision, add, subtract, multiply, divide.
6813
6814 if (Token::MUL == op_) {
6815 __ vmul(d5, d6, d7);
6816 } else if (Token::DIV == op_) {
6817 __ vdiv(d5, d6, d7);
6818 } else if (Token::ADD == op_) {
6819 __ vadd(d5, d6, d7);
6820 } else if (Token::SUB == op_) {
6821 __ vsub(d5, d6, d7);
6822 } else {
6823 UNREACHABLE();
6824 }
6825 __ sub(r0, r5, Operand(kHeapObjectTag));
6826 __ vstr(d5, r0, HeapNumber::kValueOffset);
6827 __ add(r0, r0, Operand(kHeapObjectTag));
6828 __ mov(pc, lr);
6829 } else {
6830 // If we did not inline the operation, then the arguments are in:
6831 // r0: Left value (least significant part of mantissa).
6832 // r1: Left value (sign, exponent, top of mantissa).
6833 // r2: Right value (least significant part of mantissa).
6834 // r3: Right value (sign, exponent, top of mantissa).
6835 // r5: Address of heap number for result.
6836
6837 __ push(lr); // For later.
6838 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
6839 // Call C routine that may not cause GC or other trouble. r5 is callee
6840 // save.
6841 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
6842 // Store answer in the overwritable heap number.
6843 #if !defined(USE_ARM_EABI)
6844 // Double returned in fp coprocessor register 0 and 1, encoded as register
6845 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
6846 // substract the tag from r5.
6847 __ sub(r4, r5, Operand(kHeapObjectTag));
6848 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
6849 #else
6850 // Double returned in registers 0 and 1.
6851 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
6852 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
6853 #endif
6854 __ mov(r0, Operand(r5));
6855 // And we are done.
6856 __ pop(pc);
6857 }
6858 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006859
6860
6861 if (lhs.is(r0)) {
6862 __ b(&slow);
6863 __ bind(&slow_reverse);
6864 __ Swap(r0, r1, ip);
6865 }
6866
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006867 // We jump to here if something goes wrong (one param is not a number of any
6868 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006869 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006870
6871 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006872 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006873
ager@chromium.org357bf652010-04-12 11:30:10 +00006874 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006875 // Test for string arguments before calling runtime.
6876 // r1 : first argument
6877 // r0 : second argument
6878 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00006879 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006880
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006881 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006882 __ tst(r1, Operand(kSmiTagMask));
6883 __ b(eq, &not_string1);
6884 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
6885 __ b(ge, &not_string1);
6886
6887 // First argument is a a string, test second.
6888 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006889 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006890 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6891 __ b(ge, &string1);
6892
6893 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006894 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
6895 __ TailCallStub(&string_add_stub);
6896
6897 __ bind(&string1_smi2);
6898 // First argument is a string, second is a smi. Try to lookup the number
6899 // string for the smi in the number string cache.
6900 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006901 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006902
6903 // Replace second argument on stack and tailcall string add stub to make
6904 // the result.
6905 __ str(r2, MemOperand(sp, 0));
6906 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006907
6908 // Only first argument is a string.
6909 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006910 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
6911
6912 // First argument was not a string, test second.
6913 __ bind(&not_string1);
6914 __ tst(r0, Operand(kSmiTagMask));
6915 __ b(eq, &not_strings);
6916 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6917 __ b(ge, &not_strings);
6918
6919 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006920 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
6921
6922 __ bind(&not_strings);
6923 }
6924
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006925 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006926}
6927
6928
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006929// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006930// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006931// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
6932// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006933// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
6934// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006935static void GetInt32(MacroAssembler* masm,
6936 Register source,
6937 Register dest,
6938 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006939 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006940 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006941 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006942 // Get exponent word.
6943 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
6944 // Get exponent alone in scratch2.
6945 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006946 // Load dest with zero. We use this either for the final shift or
6947 // for the answer.
6948 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006949 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006950 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
6951 // the exponent that we are fastest at and also the highest exponent we can
6952 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006953 const uint32_t non_smi_exponent =
6954 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6955 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006956 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
6957 __ b(eq, &right_exponent);
6958 // If the exponent is higher than that then go to slow case. This catches
6959 // numbers that don't fit in a signed int32, infinities and NaNs.
6960 __ b(gt, slow);
6961
6962 // We know the exponent is smaller than 30 (biased). If it is less than
6963 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
6964 // it rounds to zero.
6965 const uint32_t zero_exponent =
6966 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
6967 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
6968 // Dest already has a Smi zero.
6969 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006970 if (!CpuFeatures::IsSupported(VFP3)) {
6971 // We have a shifted exponent between 0 and 30 in scratch2.
6972 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
6973 // We now have the exponent in dest. Subtract from 30 to get
6974 // how much to shift down.
6975 __ rsb(dest, dest, Operand(30));
6976 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006977 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006978 if (CpuFeatures::IsSupported(VFP3)) {
6979 CpuFeatures::Scope scope(VFP3);
6980 // ARMv7 VFP3 instructions implementing double precision to integer
6981 // conversion using round to zero.
6982 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006983 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006984 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006985 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006986 } else {
6987 // Get the top bits of the mantissa.
6988 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
6989 // Put back the implicit 1.
6990 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
6991 // Shift up the mantissa bits to take up the space the exponent used to
6992 // take. We just orred in the implicit bit so that took care of one and
6993 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
6994 // distance.
6995 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6996 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
6997 // Put sign in zero flag.
6998 __ tst(scratch, Operand(HeapNumber::kSignMask));
6999 // Get the second half of the double. For some exponents we don't
7000 // actually need this because the bits get shifted out again, but
7001 // it's probably slower to test than just to do it.
7002 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
7003 // Shift down 22 bits to get the last 10 bits.
7004 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
7005 // Move down according to the exponent.
7006 __ mov(dest, Operand(scratch, LSR, dest));
7007 // Fix sign if sign bit was set.
7008 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
7009 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007010 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007011}
7012
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007013// For bitwise ops where the inputs are not both Smis we here try to determine
7014// whether both inputs are either Smis or at least heap numbers that can be
7015// represented by a 32 bit signed value. We truncate towards zero as required
7016// by the ES spec. If this is the case we do the bitwise op and see if the
7017// result is a Smi. If so, great, otherwise we try to find a heap number to
7018// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00007019// On entry the operands are in lhs and rhs. On exit the answer is in r0.
7020void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
7021 Register lhs,
7022 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007023 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007024 Label rhs_is_smi, lhs_is_smi;
7025 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007026
ager@chromium.org357bf652010-04-12 11:30:10 +00007027 __ tst(lhs, Operand(kSmiTagMask));
7028 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
7029 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007030 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007031 GetInt32(masm, lhs, r3, r5, r4, &slow);
7032 __ jmp(&done_checking_lhs);
7033 __ bind(&lhs_is_smi);
7034 __ mov(r3, Operand(lhs, ASR, 1));
7035 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007036
ager@chromium.org357bf652010-04-12 11:30:10 +00007037 __ tst(rhs, Operand(kSmiTagMask));
7038 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
7039 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007040 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007041 GetInt32(masm, rhs, r2, r5, r4, &slow);
7042 __ jmp(&done_checking_rhs);
7043 __ bind(&rhs_is_smi);
7044 __ mov(r2, Operand(rhs, ASR, 1));
7045 __ bind(&done_checking_rhs);
7046
7047 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007048
7049 // r0 and r1: Original operands (Smi or heap numbers).
7050 // r2 and r3: Signed int32 operands.
7051 switch (op_) {
7052 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
7053 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
7054 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
7055 case Token::SAR:
7056 // Use only the 5 least significant bits of the shift count.
7057 __ and_(r2, r2, Operand(0x1f));
7058 __ mov(r2, Operand(r3, ASR, r2));
7059 break;
7060 case Token::SHR:
7061 // Use only the 5 least significant bits of the shift count.
7062 __ and_(r2, r2, Operand(0x1f));
7063 __ mov(r2, Operand(r3, LSR, r2), SetCC);
7064 // SHR is special because it is required to produce a positive answer.
7065 // The code below for writing into heap numbers isn't capable of writing
7066 // the register as an unsigned int so we go to slow case if we hit this
7067 // case.
7068 __ b(mi, &slow);
7069 break;
7070 case Token::SHL:
7071 // Use only the 5 least significant bits of the shift count.
7072 __ and_(r2, r2, Operand(0x1f));
7073 __ mov(r2, Operand(r3, LSL, r2));
7074 break;
7075 default: UNREACHABLE();
7076 }
7077 // check that the *signed* result fits in a smi
7078 __ add(r3, r2, Operand(0x40000000), SetCC);
7079 __ b(mi, &result_not_a_smi);
7080 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
7081 __ Ret();
7082
7083 Label have_to_allocate, got_a_heap_number;
7084 __ bind(&result_not_a_smi);
7085 switch (mode_) {
7086 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007087 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007088 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007089 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007090 break;
7091 }
7092 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007093 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007094 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007095 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007096 break;
7097 }
7098 case NO_OVERWRITE: {
7099 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007100 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007101 }
7102 default: break;
7103 }
7104 __ bind(&got_a_heap_number);
7105 // r2: Answer as signed int32.
7106 // r5: Heap number to write answer into.
7107
7108 // Nothing can go wrong now, so move the heap number to r0, which is the
7109 // result.
7110 __ mov(r0, Operand(r5));
7111
7112 // Tail call that writes the int32 in r2 to the heap number in r0, using
7113 // r3 as scratch. r0 is preserved and returned.
7114 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
7115 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
7116
7117 if (mode_ != NO_OVERWRITE) {
7118 __ bind(&have_to_allocate);
7119 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007120 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007121 __ jmp(&got_a_heap_number);
7122 }
7123
7124 // If all else failed then we go to the runtime system.
7125 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007126 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007127 switch (op_) {
7128 case Token::BIT_OR:
7129 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
7130 break;
7131 case Token::BIT_AND:
7132 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
7133 break;
7134 case Token::BIT_XOR:
7135 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
7136 break;
7137 case Token::SAR:
7138 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
7139 break;
7140 case Token::SHR:
7141 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
7142 break;
7143 case Token::SHL:
7144 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
7145 break;
7146 default:
7147 UNREACHABLE();
7148 }
7149}
7150
7151
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007152// Can we multiply by x with max two shifts and an add.
7153// This answers yes to all integers from 2 to 10.
7154static bool IsEasyToMultiplyBy(int x) {
7155 if (x < 2) return false; // Avoid special cases.
7156 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
7157 if (IsPowerOf2(x)) return true; // Simple shift.
7158 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
7159 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
7160 return false;
7161}
7162
7163
7164// Can multiply by anything that IsEasyToMultiplyBy returns true for.
7165// Source and destination may be the same register. This routine does
7166// not set carry and overflow the way a mul instruction would.
7167static void MultiplyByKnownInt(MacroAssembler* masm,
7168 Register source,
7169 Register destination,
7170 int known_int) {
7171 if (IsPowerOf2(known_int)) {
7172 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
7173 } else if (PopCountLessThanEqual2(known_int)) {
7174 int first_bit = BitPosition(known_int);
7175 int second_bit = BitPosition(known_int ^ (1 << first_bit));
7176 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
7177 if (first_bit != 0) {
7178 __ mov(destination, Operand(destination, LSL, first_bit));
7179 }
7180 } else {
7181 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
7182 int the_bit = BitPosition(known_int + 1);
7183 __ rsb(destination, source, Operand(source, LSL, the_bit));
7184 }
7185}
7186
7187
7188// This function (as opposed to MultiplyByKnownInt) takes the known int in a
7189// a register for the cases where it doesn't know a good trick, and may deliver
7190// a result that needs shifting.
7191static void MultiplyByKnownInt2(
7192 MacroAssembler* masm,
7193 Register result,
7194 Register source,
7195 Register known_int_register, // Smi tagged.
7196 int known_int,
7197 int* required_shift) { // Including Smi tag shift
7198 switch (known_int) {
7199 case 3:
7200 __ add(result, source, Operand(source, LSL, 1));
7201 *required_shift = 1;
7202 break;
7203 case 5:
7204 __ add(result, source, Operand(source, LSL, 2));
7205 *required_shift = 1;
7206 break;
7207 case 6:
7208 __ add(result, source, Operand(source, LSL, 1));
7209 *required_shift = 2;
7210 break;
7211 case 7:
7212 __ rsb(result, source, Operand(source, LSL, 3));
7213 *required_shift = 1;
7214 break;
7215 case 9:
7216 __ add(result, source, Operand(source, LSL, 3));
7217 *required_shift = 1;
7218 break;
7219 case 10:
7220 __ add(result, source, Operand(source, LSL, 2));
7221 *required_shift = 2;
7222 break;
7223 default:
7224 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
7225 __ mul(result, source, known_int_register);
7226 *required_shift = 0;
7227 }
7228}
7229
7230
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007231const char* GenericBinaryOpStub::GetName() {
7232 if (name_ != NULL) return name_;
7233 const int len = 100;
7234 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7235 if (name_ == NULL) return "OOM";
7236 const char* op_name = Token::Name(op_);
7237 const char* overwrite_name;
7238 switch (mode_) {
7239 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7240 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7241 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7242 default: overwrite_name = "UnknownOverwrite"; break;
7243 }
7244
7245 OS::SNPrintF(Vector<char>(name_, len),
7246 "GenericBinaryOpStub_%s_%s%s",
7247 op_name,
7248 overwrite_name,
7249 specialized_on_rhs_ ? "_ConstantRhs" : 0);
7250 return name_;
7251}
7252
7253
ager@chromium.org5c838252010-02-19 08:53:10 +00007254
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007255void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007256 // lhs_ : x
7257 // rhs_ : y
7258 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007259
ager@chromium.org357bf652010-04-12 11:30:10 +00007260 Register result = r0;
7261 Register lhs = lhs_;
7262 Register rhs = rhs_;
7263
7264 // This code can't cope with other register allocations yet.
7265 ASSERT(result.is(r0) &&
7266 ((lhs.is(r0) && rhs.is(r1)) ||
7267 (lhs.is(r1) && rhs.is(r0))));
7268
7269 Register smi_test_reg = VirtualFrame::scratch0();
7270 Register scratch = VirtualFrame::scratch1();
7271
7272 // All ops need to know whether we are dealing with two Smis. Set up
7273 // smi_test_reg to tell us that.
7274 if (ShouldGenerateSmiCode()) {
7275 __ orr(smi_test_reg, lhs, Operand(rhs));
7276 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007277
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007278 switch (op_) {
7279 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007280 Label not_smi;
7281 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007282 if (ShouldGenerateSmiCode()) {
7283 ASSERT(kSmiTag == 0); // Adjust code below.
7284 __ tst(smi_test_reg, Operand(kSmiTagMask));
7285 __ b(ne, &not_smi);
7286 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
7287 // Return if no overflow.
7288 __ Ret(vc);
7289 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
7290 }
7291 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007292 break;
7293 }
7294
7295 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007296 Label not_smi;
7297 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007298 if (ShouldGenerateSmiCode()) {
7299 ASSERT(kSmiTag == 0); // Adjust code below.
7300 __ tst(smi_test_reg, Operand(kSmiTagMask));
7301 __ b(ne, &not_smi);
7302 if (lhs.is(r1)) {
7303 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
7304 // Return if no overflow.
7305 __ Ret(vc);
7306 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
7307 } else {
7308 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
7309 // Return if no overflow.
7310 __ Ret(vc);
7311 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
7312 }
7313 }
7314 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007315 break;
7316 }
7317
7318 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007319 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00007320 if (ShouldGenerateSmiCode()) {
7321 ASSERT(kSmiTag == 0); // adjust code below
7322 __ tst(smi_test_reg, Operand(kSmiTagMask));
7323 Register scratch2 = smi_test_reg;
7324 smi_test_reg = no_reg;
7325 __ b(ne, &not_smi);
7326 // Remove tag from one operand (but keep sign), so that result is Smi.
7327 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
7328 // Do multiplication
7329 // scratch = lower 32 bits of ip * lhs.
7330 __ smull(scratch, scratch2, lhs, ip);
7331 // Go slow on overflows (overflow bit is not set).
7332 __ mov(ip, Operand(scratch, ASR, 31));
7333 // No overflow if higher 33 bits are identical.
7334 __ cmp(ip, Operand(scratch2));
7335 __ b(ne, &slow);
7336 // Go slow on zero result to handle -0.
7337 __ tst(scratch, Operand(scratch));
7338 __ mov(result, Operand(scratch), LeaveCC, ne);
7339 __ Ret(ne);
7340 // We need -0 if we were multiplying a negative number with 0 to get 0.
7341 // We know one of them was zero.
7342 __ add(scratch2, rhs, Operand(lhs), SetCC);
7343 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
7344 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
7345 // Slow case. We fall through here if we multiplied a negative number
7346 // with 0, because that would mean we should produce -0.
7347 __ bind(&slow);
7348 }
7349 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007350 break;
7351 }
7352
7353 case Token::DIV:
7354 case Token::MOD: {
7355 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007356 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007357 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00007358 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007359 if (IsPowerOf2(constant_rhs_)) {
7360 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007361 __ and_(rhs,
7362 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007363 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
7364 SetCC);
7365 // We now have the answer, but if the input was negative we also
7366 // have the sign bit. Our work is done if the result is
7367 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00007368 if (!rhs.is(r0)) {
7369 __ mov(r0, rhs, LeaveCC, pl);
7370 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007371 __ Ret(pl);
7372 // A mod of a negative left hand side must return a negative number.
7373 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00007374 // already optimistically trashed rhs so we may need to restore it.
7375 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007376 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007377 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007378 __ b(eq, &smi_is_unsuitable);
7379 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00007380 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007381 } else {
7382 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00007383 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007384 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
7385 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
7386 int shift = 0;
7387 int d = constant_rhs_;
7388 while ((d & 1) == 0) {
7389 d >>= 1;
7390 shift++;
7391 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007392 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007393 __ bic(r0, r0, Operand(kSmiTagMask));
7394 }
7395 } else {
7396 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00007397 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007398 __ b(ne, &smi_is_unsuitable);
7399 // Find a fixed point reciprocal of the divisor so we can divide by
7400 // multiplying.
7401 double divisor = 1.0 / constant_rhs_;
7402 int shift = 32;
7403 double scale = 4294967296.0; // 1 << 32.
7404 uint32_t mul;
7405 // Maximise the precision of the fixed point reciprocal.
7406 while (true) {
7407 mul = static_cast<uint32_t>(scale * divisor);
7408 if (mul >= 0x7fffffff) break;
7409 scale *= 2.0;
7410 shift++;
7411 }
7412 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00007413 Register scratch2 = smi_test_reg;
7414 smi_test_reg = no_reg;
7415 __ mov(scratch2, Operand(mul));
7416 __ umull(scratch, scratch2, scratch2, lhs);
7417 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
7418 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
7419 // rhs is still the known rhs. rhs is Smi tagged.
7420 // lhs is still the unkown lhs. lhs is Smi tagged.
7421 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
7422 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007423 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00007424 scratch,
7425 scratch2,
7426 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007427 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00007428 &required_scratch_shift);
7429 // scratch << required_scratch_shift is now the Smi tagged rhs *
7430 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007431 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007432 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007433 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00007434 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007435 } else {
7436 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00007437 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007438 }
7439 }
7440 __ Ret();
7441 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007442 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007443 HandleBinaryOpSlowCases(
7444 masm,
7445 &not_smi,
7446 lhs,
7447 rhs,
7448 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007449 break;
7450 }
7451
7452 case Token::BIT_OR:
7453 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007454 case Token::BIT_XOR:
7455 case Token::SAR:
7456 case Token::SHR:
7457 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007458 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007459 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00007460 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007461 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007462 Register scratch2 = smi_test_reg;
7463 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007464 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007465 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
7466 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
7467 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007468 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007469 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00007470 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7471 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007472 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007473 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007474 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007475 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007476 // Remove tags from operands. We can't do this on a 31 bit number
7477 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00007478 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7479 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7480 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007481 // Unsigned shift is not allowed to produce a negative number, so
7482 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00007483 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007484 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007485 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007486 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007487 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007488 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007489 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00007490 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7491 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7492 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007493 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00007494 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007495 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007496 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007497 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007498 default: UNREACHABLE();
7499 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007500 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007501 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007502 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007503 break;
7504 }
7505
7506 default: UNREACHABLE();
7507 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007508 // This code should be unreachable.
7509 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00007510
7511 // Generate an unreachable reference to the DEFAULT stub so that it can be
7512 // found at the end of this stub when clearing ICs at GC.
7513 // TODO(kaznacheev): Check performance impact and get rid of this.
7514 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
7515 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
7516 __ CallStub(&uninit);
7517 }
7518}
7519
7520
7521void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
7522 Label get_result;
7523
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007524 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00007525
7526 // Internal frame is necessary to handle exceptions properly.
7527 __ EnterInternalFrame();
7528 // Call the stub proper to get the result in r0.
7529 __ Call(&get_result);
7530 __ LeaveInternalFrame();
7531
7532 __ push(r0);
7533
7534 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
7535 __ push(r0);
7536 __ mov(r0, Operand(Smi::FromInt(op_)));
7537 __ push(r0);
7538 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
7539 __ push(r0);
7540
7541 __ TailCallExternalReference(
7542 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
7543 6,
7544 1);
7545
7546 // The entry point for the result calculation is assumed to be immediately
7547 // after this sequence.
7548 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007549}
7550
7551
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007552Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007553 GenericBinaryOpStub stub(key, type_info);
7554 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007555}
7556
7557
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007558void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00007559 // Do tail-call to runtime routine. Runtime routines expect at least one
7560 // argument, so give it a Smi.
7561 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007562 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007563 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007564
7565 __ StubReturn(1);
7566}
7567
7568
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007569void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007570 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007571
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007572 if (op_ == Token::SUB) {
7573 // Check whether the value is a smi.
7574 Label try_float;
7575 __ tst(r0, Operand(kSmiTagMask));
7576 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007577
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007578 // Go slow case if the value of the expression is zero
7579 // to make sure that we switch between 0 and -0.
7580 __ cmp(r0, Operand(0));
7581 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007582
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007583 // The value of the expression is a smi that is not zero. Try
7584 // optimistic subtraction '0 - value'.
7585 __ rsb(r1, r0, Operand(0), SetCC);
7586 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007587
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007588 __ mov(r0, Operand(r1)); // Set r0 to result.
7589 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007590
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007591 __ bind(&try_float);
7592 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7593 __ b(ne, &slow);
7594 // r0 is a heap number. Get a new heap number in r1.
7595 if (overwrite_) {
7596 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7597 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7598 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7599 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007600 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007601 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7602 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7603 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
7604 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7605 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
7606 __ mov(r0, Operand(r1));
7607 }
7608 } else if (op_ == Token::BIT_NOT) {
7609 // Check if the operand is a heap number.
7610 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7611 __ b(ne, &slow);
7612
7613 // Convert the heap number is r0 to an untagged integer in r1.
7614 GetInt32(masm, r0, r1, r2, r3, &slow);
7615
7616 // Do the bitwise operation (move negated) and check if the result
7617 // fits in a smi.
7618 Label try_float;
7619 __ mvn(r1, Operand(r1));
7620 __ add(r2, r1, Operand(0x40000000), SetCC);
7621 __ b(mi, &try_float);
7622 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
7623 __ b(&done);
7624
7625 __ bind(&try_float);
7626 if (!overwrite_) {
7627 // Allocate a fresh heap number, but don't overwrite r0 until
7628 // we're sure we can do it without going through the slow case
7629 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007630 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007631 __ mov(r0, Operand(r2));
7632 }
7633
7634 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
7635 // have to set up a frame.
7636 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
7637 __ push(lr);
7638 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
7639 __ pop(lr);
7640 } else {
7641 UNIMPLEMENTED();
7642 }
7643
7644 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007645 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007646
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007647 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007648 __ bind(&slow);
7649 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007650 switch (op_) {
7651 case Token::SUB:
7652 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
7653 break;
7654 case Token::BIT_NOT:
7655 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
7656 break;
7657 default:
7658 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007659 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00007660}
7661
7662
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007663void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007664 // r0 holds the exception.
7665
7666 // Adjust this code if not the case.
7667 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7668
7669 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007670 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
7671 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007672
7673 // Restore the next handler and frame pointer, discard handler state.
7674 ASSERT(StackHandlerConstants::kNextOffset == 0);
7675 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007676 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007677 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7678 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
7679
7680 // Before returning we restore the context from the frame pointer if
7681 // not NULL. The frame pointer is NULL in the exception handler of a
7682 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007683 __ cmp(fp, Operand(0));
7684 // Set cp to NULL if fp is NULL.
7685 __ mov(cp, Operand(0), LeaveCC, eq);
7686 // Restore cp otherwise.
7687 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007688#ifdef DEBUG
7689 if (FLAG_debug_code) {
7690 __ mov(lr, Operand(pc));
7691 }
7692#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007693 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007694 __ pop(pc);
7695}
7696
7697
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007698void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
7699 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007700 // Adjust this code if not the case.
7701 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7702
7703 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007704 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007705 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007706
7707 // Unwind the handlers until the ENTRY handler is found.
7708 Label loop, done;
7709 __ bind(&loop);
7710 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007711 const int kStateOffset = StackHandlerConstants::kStateOffset;
7712 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007713 __ cmp(r2, Operand(StackHandler::ENTRY));
7714 __ b(eq, &done);
7715 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007716 const int kNextOffset = StackHandlerConstants::kNextOffset;
7717 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007718 __ jmp(&loop);
7719 __ bind(&done);
7720
7721 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007722 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007723 __ pop(r2);
7724 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007725
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007726 if (type == OUT_OF_MEMORY) {
7727 // Set external caught exception to false.
7728 ExternalReference external_caught(Top::k_external_caught_exception_address);
7729 __ mov(r0, Operand(false));
7730 __ mov(r2, Operand(external_caught));
7731 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007732
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007733 // Set pending exception and r0 to out of memory exception.
7734 Failure* out_of_memory = Failure::OutOfMemoryException();
7735 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7736 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
7737 __ str(r0, MemOperand(r2));
7738 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007739
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007740 // Stack layout at this point. See also StackHandlerConstants.
7741 // sp -> state (ENTRY)
7742 // fp
7743 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007744
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007745 // Discard handler state (r2 is not used) and restore frame pointer.
7746 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7747 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
7748 // Before returning we restore the context from the frame pointer if
7749 // not NULL. The frame pointer is NULL in the exception handler of a
7750 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007751 __ cmp(fp, Operand(0));
7752 // Set cp to NULL if fp is NULL.
7753 __ mov(cp, Operand(0), LeaveCC, eq);
7754 // Restore cp otherwise.
7755 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007756#ifdef DEBUG
7757 if (FLAG_debug_code) {
7758 __ mov(lr, Operand(pc));
7759 }
7760#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007761 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007762 __ pop(pc);
7763}
7764
7765
7766void CEntryStub::GenerateCore(MacroAssembler* masm,
7767 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007768 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007769 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007770 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007771 bool always_allocate,
7772 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007773 // r0: result parameter for PerformGC, if any
7774 // r4: number of arguments including receiver (C callee-saved)
7775 // r5: pointer to builtin function (C callee-saved)
7776 // r6: pointer to the first argument (C callee-saved)
7777
7778 if (do_gc) {
7779 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007780 __ PrepareCallCFunction(1, r1);
7781 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007782 }
7783
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007784 ExternalReference scope_depth =
7785 ExternalReference::heap_always_allocate_scope_depth();
7786 if (always_allocate) {
7787 __ mov(r0, Operand(scope_depth));
7788 __ ldr(r1, MemOperand(r0));
7789 __ add(r1, r1, Operand(1));
7790 __ str(r1, MemOperand(r0));
7791 }
7792
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007793 // Call C built-in.
7794 // r0 = argc, r1 = argv
7795 __ mov(r0, Operand(r4));
7796 __ mov(r1, Operand(r6));
7797
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007798 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
7799 int frame_alignment_mask = frame_alignment - 1;
7800#if defined(V8_HOST_ARCH_ARM)
7801 if (FLAG_debug_code) {
7802 if (frame_alignment > kPointerSize) {
7803 Label alignment_as_expected;
7804 ASSERT(IsPowerOf2(frame_alignment));
7805 __ sub(r2, sp, Operand(frame_alignment_skew));
7806 __ tst(r2, Operand(frame_alignment_mask));
7807 __ b(eq, &alignment_as_expected);
7808 // Don't use Check here, as it will call Runtime_Abort re-entering here.
7809 __ stop("Unexpected alignment");
7810 __ bind(&alignment_as_expected);
7811 }
7812 }
7813#endif
7814
7815 // Just before the call (jump) below lr is pushed, so the actual alignment is
7816 // adding one to the current skew.
7817 int alignment_before_call =
7818 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
7819 if (alignment_before_call > 0) {
7820 // Push until the alignment before the call is met.
7821 __ mov(r2, Operand(0));
7822 for (int i = alignment_before_call;
7823 (i & frame_alignment_mask) != 0;
7824 i += kPointerSize) {
7825 __ push(r2);
7826 }
7827 }
7828
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007829 // TODO(1242173): To let the GC traverse the return address of the exit
7830 // frames, we need to know where the return address is. Right now,
7831 // we push it on the stack to be able to find it again, but we never
7832 // restore from it in case of changes, which makes it impossible to
7833 // support moving the C entry code stub. This should be fixed, but currently
7834 // this is OK because the CEntryStub gets generated so early in the V8 boot
7835 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007836 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007837 masm->push(lr);
7838 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007839
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007840 // Restore sp back to before aligning the stack.
7841 if (alignment_before_call > 0) {
7842 __ add(sp, sp, Operand(alignment_before_call));
7843 }
7844
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007845 if (always_allocate) {
7846 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
7847 // though (contain the result).
7848 __ mov(r2, Operand(scope_depth));
7849 __ ldr(r3, MemOperand(r2));
7850 __ sub(r3, r3, Operand(1));
7851 __ str(r3, MemOperand(r2));
7852 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007853
7854 // check for failure result
7855 Label failure_returned;
7856 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
7857 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
7858 __ add(r2, r0, Operand(1));
7859 __ tst(r2, Operand(kFailureTagMask));
7860 __ b(eq, &failure_returned);
7861
7862 // Exit C frame and return.
7863 // r0:r1: result
7864 // sp: stack pointer
7865 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007866 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007867
7868 // check if we should retry or throw exception
7869 Label retry;
7870 __ bind(&failure_returned);
7871 ASSERT(Failure::RETRY_AFTER_GC == 0);
7872 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
7873 __ b(eq, &retry);
7874
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007875 // Special handling of out of memory exceptions.
7876 Failure* out_of_memory = Failure::OutOfMemoryException();
7877 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7878 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007879
7880 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00007881 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007882 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007883 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007884 __ ldr(r0, MemOperand(ip));
7885 __ str(r3, MemOperand(ip));
7886
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007887 // Special handling of termination exceptions which are uncatchable
7888 // by javascript code.
7889 __ cmp(r0, Operand(Factory::termination_exception()));
7890 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007891
7892 // Handle normal exception.
7893 __ jmp(throw_normal_exception);
7894
7895 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
7896}
7897
7898
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007899void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007900 // Called from JavaScript; parameters are on stack as if calling JS function
7901 // r0: number of arguments including receiver
7902 // r1: pointer to builtin function
7903 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007904 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007905 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007906
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007907 // Result returned in r0 or r0+r1 by default.
7908
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007909 // NOTE: Invocations of builtins may return failure objects
7910 // instead of a proper result. The builtin entry handles
7911 // this by performing a garbage collection and retrying the
7912 // builtin once.
7913
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007914 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007915 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007916
7917 // r4: number of arguments (C callee-saved)
7918 // r5: pointer to builtin function (C callee-saved)
7919 // r6: pointer to first argument (C callee-saved)
7920
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007921 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007922 Label throw_termination_exception;
7923 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007924
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007925 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007926 GenerateCore(masm,
7927 &throw_normal_exception,
7928 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007929 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007930 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007931 false,
7932 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007933
7934 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007935 GenerateCore(masm,
7936 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007937 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007938 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007939 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007940 false,
7941 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007942
7943 // Do full GC and retry runtime call one final time.
7944 Failure* failure = Failure::InternalError();
7945 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
7946 GenerateCore(masm,
7947 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007948 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007949 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007950 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007951 true,
7952 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007953
7954 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007955 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
7956
7957 __ bind(&throw_termination_exception);
7958 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007959
7960 __ bind(&throw_normal_exception);
7961 GenerateThrowTOS(masm);
7962}
7963
7964
7965void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
7966 // r0: code entry
7967 // r1: function
7968 // r2: receiver
7969 // r3: argc
7970 // [sp+0]: argv
7971
7972 Label invoke, exit;
7973
7974 // Called from C, so do not pop argc and args on exit (preserve sp)
7975 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007976 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007977 __ stm(db_w, sp, kCalleeSaved | lr.bit());
7978
7979 // Get address of argv, see stm above.
7980 // r0: code entry
7981 // r1: function
7982 // r2: receiver
7983 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00007984 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007985
7986 // Push a frame with special values setup to mark it as an entry frame.
7987 // r0: code entry
7988 // r1: function
7989 // r2: receiver
7990 // r3: argc
7991 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007992 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007993 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
7994 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007995 __ mov(r6, Operand(Smi::FromInt(marker)));
7996 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7997 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007998 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007999
8000 // Setup frame pointer for the frame to be pushed.
8001 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8002
8003 // Call a faked try-block that does the invoke.
8004 __ bl(&invoke);
8005
8006 // Caught exception: Store result (exception) in the pending
8007 // exception field in the JSEnv and return a failure sentinel.
8008 // Coming in here the fp will be invalid because the PushTryHandler below
8009 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00008010 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008011 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008012 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008013 __ b(&exit);
8014
8015 // Invoke: Link this frame into the handler chain.
8016 __ bind(&invoke);
8017 // Must preserve r0-r4, r5-r7 are available.
8018 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008019 // If an exception not caught by another handler occurs, this handler
8020 // returns control to the code after the bl(&invoke) above, which
8021 // restores all kCalleeSaved registers (including cp and fp) to their
8022 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008023
8024 // Clear any pending exceptions.
8025 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
8026 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008027 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008028 __ str(r5, MemOperand(ip));
8029
8030 // Invoke the function by calling through JS entry trampoline builtin.
8031 // Notice that we cannot store a reference to the trampoline code directly in
8032 // this stub, because runtime stubs are not traversed when doing GC.
8033
8034 // Expected registers by Builtins::JSEntryTrampoline
8035 // r0: code entry
8036 // r1: function
8037 // r2: receiver
8038 // r3: argc
8039 // r4: argv
8040 if (is_construct) {
8041 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
8042 __ mov(ip, Operand(construct_entry));
8043 } else {
8044 ExternalReference entry(Builtins::JSEntryTrampoline);
8045 __ mov(ip, Operand(entry));
8046 }
8047 __ ldr(ip, MemOperand(ip)); // deref address
8048
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008049 // Branch and link to JSEntryTrampoline. We don't use the double underscore
8050 // macro for the add instruction because we don't want the coverage tool
8051 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008052 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008053 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008054
8055 // Unlink this frame from the handler chain. When reading the
8056 // address of the next handler, there is no need to use the address
8057 // displacement since the current stack pointer (sp) points directly
8058 // to the stack handler.
8059 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
8060 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
8061 __ str(r3, MemOperand(ip));
8062 // No need to restore registers
8063 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
8064
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008065
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008066 __ bind(&exit); // r0 holds result
8067 // Restore the top frame descriptors from the stack.
8068 __ pop(r3);
8069 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8070 __ str(r3, MemOperand(ip));
8071
8072 // Reset the stack to the callee saved registers.
8073 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8074
8075 // Restore callee-saved registers and return.
8076#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008077 if (FLAG_debug_code) {
8078 __ mov(lr, Operand(pc));
8079 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008080#endif
8081 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
8082}
8083
8084
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008085// This stub performs an instanceof, calling the builtin function if
8086// necessary. Uses r1 for the object, r0 for the function that it may
8087// be an instance of (these are fetched from the stack).
8088void InstanceofStub::Generate(MacroAssembler* masm) {
8089 // Get the object - slow case for smis (we may need to throw an exception
8090 // depending on the rhs).
8091 Label slow, loop, is_instance, is_not_instance;
8092 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
8093 __ BranchOnSmi(r0, &slow);
8094
8095 // Check that the left hand is a JS object and put map in r3.
8096 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
8097 __ b(lt, &slow);
8098 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
8099 __ b(gt, &slow);
8100
8101 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00008102 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008103 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
8104
8105 // Check that the function prototype is a JS object.
8106 __ BranchOnSmi(r4, &slow);
8107 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
8108 __ b(lt, &slow);
8109 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
8110 __ b(gt, &slow);
8111
8112 // Register mapping: r3 is object map and r4 is function prototype.
8113 // Get prototype of object into r2.
8114 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
8115
8116 // Loop through the prototype chain looking for the function prototype.
8117 __ bind(&loop);
8118 __ cmp(r2, Operand(r4));
8119 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00008120 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8121 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008122 __ b(eq, &is_not_instance);
8123 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
8124 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
8125 __ jmp(&loop);
8126
8127 __ bind(&is_instance);
8128 __ mov(r0, Operand(Smi::FromInt(0)));
8129 __ pop();
8130 __ pop();
8131 __ mov(pc, Operand(lr)); // Return.
8132
8133 __ bind(&is_not_instance);
8134 __ mov(r0, Operand(Smi::FromInt(1)));
8135 __ pop();
8136 __ pop();
8137 __ mov(pc, Operand(lr)); // Return.
8138
8139 // Slow-case. Tail call builtin.
8140 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008141 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
8142}
8143
8144
ager@chromium.org7c537e22008-10-16 08:43:32 +00008145void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
8146 // The displacement is the offset of the last parameter (if any)
8147 // relative to the frame pointer.
8148 static const int kDisplacement =
8149 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008150
ager@chromium.org7c537e22008-10-16 08:43:32 +00008151 // Check that the key is a smi.
8152 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008153 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008154
ager@chromium.org7c537e22008-10-16 08:43:32 +00008155 // Check if the calling frame is an arguments adaptor frame.
8156 Label adaptor;
8157 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8158 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008159 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008160 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008161
ager@chromium.org7c537e22008-10-16 08:43:32 +00008162 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008163 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00008164 // check for free.
8165 __ cmp(r1, r0);
8166 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008167
ager@chromium.org7c537e22008-10-16 08:43:32 +00008168 // Read the argument from the stack and return it.
8169 __ sub(r3, r0, r1);
8170 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8171 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008172 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008173
8174 // Arguments adaptor case: Check index against actual arguments
8175 // limit found in the arguments adaptor frame. Use unsigned
8176 // comparison to get negative check for free.
8177 __ bind(&adaptor);
8178 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8179 __ cmp(r1, r0);
8180 __ b(cs, &slow);
8181
8182 // Read the argument from the adaptor frame and return it.
8183 __ sub(r3, r0, r1);
8184 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8185 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008186 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008187
8188 // Slow-case: Handle non-smi or out-of-bounds access to arguments
8189 // by calling the runtime system.
8190 __ bind(&slow);
8191 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008192 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008193}
8194
8195
8196void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00008197 // sp[0] : number of parameters
8198 // sp[4] : receiver displacement
8199 // sp[8] : function
8200
ager@chromium.org7c537e22008-10-16 08:43:32 +00008201 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00008202 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00008203 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8204 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008205 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00008206 __ b(eq, &adaptor_frame);
8207
8208 // Get the length from the frame.
8209 __ ldr(r1, MemOperand(sp, 0));
8210 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008211
8212 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00008213 __ bind(&adaptor_frame);
8214 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8215 __ str(r1, MemOperand(sp, 0));
8216 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008217 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
8218 __ str(r3, MemOperand(sp, 1 * kPointerSize));
8219
ager@chromium.org5c838252010-02-19 08:53:10 +00008220 // Try the new space allocation. Start out with computing the size
8221 // of the arguments object and the elements array (in words, not
8222 // bytes because AllocateInNewSpace expects words).
8223 Label add_arguments_object;
8224 __ bind(&try_allocate);
8225 __ cmp(r1, Operand(0));
8226 __ b(eq, &add_arguments_object);
8227 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8228 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
8229 __ bind(&add_arguments_object);
8230 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
8231
8232 // Do the allocation of both objects in one go.
8233 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
8234
8235 // Get the arguments boilerplate from the current (global) context.
8236 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
8237 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
8238 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
8239 __ ldr(r4, MemOperand(r4, offset));
8240
8241 // Copy the JS object part.
8242 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
8243 __ ldr(r3, FieldMemOperand(r4, i));
8244 __ str(r3, FieldMemOperand(r0, i));
8245 }
8246
8247 // Setup the callee in-object property.
8248 ASSERT(Heap::arguments_callee_index == 0);
8249 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
8250 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
8251
8252 // Get the length (smi tagged) and set that as an in-object property too.
8253 ASSERT(Heap::arguments_length_index == 1);
8254 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
8255 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
8256
8257 // If there are no actual arguments, we're done.
8258 Label done;
8259 __ cmp(r1, Operand(0));
8260 __ b(eq, &done);
8261
8262 // Get the parameters pointer from the stack and untag the length.
8263 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
8264 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8265
8266 // Setup the elements pointer in the allocated arguments object and
8267 // initialize the header in the elements fixed array.
8268 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
8269 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
8270 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
8271 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
8272 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
8273
8274 // Copy the fixed array slots.
8275 Label loop;
8276 // Setup r4 to point to the first array slot.
8277 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
8278 __ bind(&loop);
8279 // Pre-decrement r2 with kPointerSize on each iteration.
8280 // Pre-decrement in order to skip receiver.
8281 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
8282 // Post-increment r4 with kPointerSize on each iteration.
8283 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
8284 __ sub(r1, r1, Operand(1));
8285 __ cmp(r1, Operand(0));
8286 __ b(ne, &loop);
8287
8288 // Return and remove the on-stack parameters.
8289 __ bind(&done);
8290 __ add(sp, sp, Operand(3 * kPointerSize));
8291 __ Ret();
8292
ager@chromium.org7c537e22008-10-16 08:43:32 +00008293 // Do the runtime call to allocate the arguments object.
8294 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008295 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008296}
8297
8298
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008299void RegExpExecStub::Generate(MacroAssembler* masm) {
8300 // Just jump directly to runtime if native RegExp is not selected at compile
8301 // time or if regexp entry in generated code is turned off runtime switch or
8302 // at compilation.
8303#ifndef V8_NATIVE_REGEXP
8304 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8305#else // V8_NATIVE_REGEXP
8306 if (!FLAG_regexp_entry_native) {
8307 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8308 return;
8309 }
8310
8311 // Stack frame on entry.
8312 // sp[0]: last_match_info (expected JSArray)
8313 // sp[4]: previous index
8314 // sp[8]: subject string
8315 // sp[12]: JSRegExp object
8316
8317 static const int kLastMatchInfoOffset = 0 * kPointerSize;
8318 static const int kPreviousIndexOffset = 1 * kPointerSize;
8319 static const int kSubjectOffset = 2 * kPointerSize;
8320 static const int kJSRegExpOffset = 3 * kPointerSize;
8321
8322 Label runtime, invoke_regexp;
8323
8324 // Allocation of registers for this function. These are in callee save
8325 // registers and will be preserved by the call to the native RegExp code, as
8326 // this code is called using the normal C calling convention. When calling
8327 // directly from generated code the native RegExp code will not do a GC and
8328 // therefore the content of these registers are safe to use after the call.
8329 Register subject = r4;
8330 Register regexp_data = r5;
8331 Register last_match_info_elements = r6;
8332
8333 // Ensure that a RegExp stack is allocated.
8334 ExternalReference address_of_regexp_stack_memory_address =
8335 ExternalReference::address_of_regexp_stack_memory_address();
8336 ExternalReference address_of_regexp_stack_memory_size =
8337 ExternalReference::address_of_regexp_stack_memory_size();
8338 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
8339 __ ldr(r0, MemOperand(r0, 0));
8340 __ tst(r0, Operand(r0));
8341 __ b(eq, &runtime);
8342
8343 // Check that the first argument is a JSRegExp object.
8344 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
8345 ASSERT_EQ(0, kSmiTag);
8346 __ tst(r0, Operand(kSmiTagMask));
8347 __ b(eq, &runtime);
8348 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
8349 __ b(ne, &runtime);
8350
8351 // Check that the RegExp has been compiled (data contains a fixed array).
8352 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
8353 if (FLAG_debug_code) {
8354 __ tst(regexp_data, Operand(kSmiTagMask));
8355 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
8356 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
8357 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
8358 }
8359
8360 // regexp_data: RegExp data (FixedArray)
8361 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
8362 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
8363 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
8364 __ b(ne, &runtime);
8365
8366 // regexp_data: RegExp data (FixedArray)
8367 // Check that the number of captures fit in the static offsets vector buffer.
8368 __ ldr(r2,
8369 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8370 // Calculate number of capture registers (number_of_captures + 1) * 2. This
8371 // uses the asumption that smis are 2 * their untagged value.
8372 ASSERT_EQ(0, kSmiTag);
8373 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8374 __ add(r2, r2, Operand(2)); // r2 was a smi.
8375 // Check that the static offsets vector buffer is large enough.
8376 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
8377 __ b(hi, &runtime);
8378
8379 // r2: Number of capture registers
8380 // regexp_data: RegExp data (FixedArray)
8381 // Check that the second argument is a string.
8382 __ ldr(subject, MemOperand(sp, kSubjectOffset));
8383 __ tst(subject, Operand(kSmiTagMask));
8384 __ b(eq, &runtime);
8385 Condition is_string = masm->IsObjectStringType(subject, r0);
8386 __ b(NegateCondition(is_string), &runtime);
8387 // Get the length of the string to r3.
8388 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
8389
8390 // r2: Number of capture registers
8391 // r3: Length of subject string
8392 // subject: Subject string
8393 // regexp_data: RegExp data (FixedArray)
8394 // Check that the third argument is a positive smi less than the subject
8395 // string length. A negative value will be greater (unsigned comparison).
8396 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
8397 __ cmp(r3, Operand(r0, ASR, kSmiTagSize + kSmiShiftSize));
8398 __ b(ls, &runtime);
8399
8400 // r2: Number of capture registers
8401 // subject: Subject string
8402 // regexp_data: RegExp data (FixedArray)
8403 // Check that the fourth object is a JSArray object.
8404 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8405 __ tst(r0, Operand(kSmiTagMask));
8406 __ b(eq, &runtime);
8407 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
8408 __ b(ne, &runtime);
8409 // Check that the JSArray is in fast case.
8410 __ ldr(last_match_info_elements,
8411 FieldMemOperand(r0, JSArray::kElementsOffset));
8412 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008413 __ LoadRoot(ip, kFixedArrayMapRootIndex);
8414 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008415 __ b(ne, &runtime);
8416 // Check that the last match info has space for the capture registers and the
8417 // additional information.
8418 __ ldr(r0,
8419 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
8420 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
8421 __ cmp(r2, r0);
8422 __ b(gt, &runtime);
8423
8424 // subject: Subject string
8425 // regexp_data: RegExp data (FixedArray)
8426 // Check the representation and encoding of the subject string.
8427 Label seq_string;
8428 const int kStringRepresentationEncodingMask =
8429 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
8430 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8431 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8432 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8433 // First check for sequential string.
8434 ASSERT_EQ(0, kStringTag);
8435 ASSERT_EQ(0, kSeqStringTag);
8436 __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask));
8437 __ b(eq, &seq_string);
8438
8439 // subject: Subject string
8440 // regexp_data: RegExp data (FixedArray)
8441 // Check for flat cons string.
8442 // A flat cons string is a cons string where the second part is the empty
8443 // string. In that case the subject string is just the first part of the cons
8444 // string. Also in this case the first part of the cons string is known to be
8445 // a sequential string or an external string.
8446 __ and_(r0, r0, Operand(kStringRepresentationMask));
8447 __ cmp(r0, Operand(kConsStringTag));
8448 __ b(ne, &runtime);
8449 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
8450 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
8451 __ cmp(r0, r1);
8452 __ b(ne, &runtime);
8453 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
8454 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8455 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8456 ASSERT_EQ(0, kSeqStringTag);
8457 __ tst(r0, Operand(kStringRepresentationMask));
8458 __ b(nz, &runtime);
8459 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8460
8461 __ bind(&seq_string);
8462 // r1: suject string type & kStringRepresentationEncodingMask
8463 // subject: Subject string
8464 // regexp_data: RegExp data (FixedArray)
8465 // Check that the irregexp code has been generated for an ascii string. If
8466 // it has, the field contains a code object otherwise it contains the hole.
8467#ifdef DEBUG
8468 const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag;
8469 const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag;
8470 CHECK_EQ(4, kSeqAsciiString);
8471 CHECK_EQ(0, kSeqTwoByteString);
8472#endif
8473 // Find the code object based on the assumptions above.
8474 __ mov(r3, Operand(r1, ASR, 2), SetCC);
8475 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
8476 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
8477
8478 // Check that the irregexp code has been generated for the actual string
8479 // encoding. If it has, the field contains a code object otherwise it contains
8480 // the hole.
8481 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
8482 __ b(ne, &runtime);
8483
8484 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8485 // r7: code
8486 // subject: Subject string
8487 // regexp_data: RegExp data (FixedArray)
8488 // Load used arguments before starting to push arguments for call to native
8489 // RegExp code to avoid handling changing stack height.
8490 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
8491 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
8492
8493 // r1: previous index
8494 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8495 // r7: code
8496 // subject: Subject string
8497 // regexp_data: RegExp data (FixedArray)
8498 // All checks done. Now push arguments for native regexp code.
8499 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
8500
8501 static const int kRegExpExecuteArguments = 7;
8502 __ push(lr);
8503 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
8504
8505 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
8506 __ mov(r0, Operand(1));
8507 __ str(r0, MemOperand(sp, 2 * kPointerSize));
8508
8509 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
8510 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
8511 __ ldr(r0, MemOperand(r0, 0));
8512 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
8513 __ ldr(r2, MemOperand(r2, 0));
8514 __ add(r0, r0, Operand(r2));
8515 __ str(r0, MemOperand(sp, 1 * kPointerSize));
8516
8517 // Argument 5 (sp[0]): static offsets vector buffer.
8518 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
8519 __ str(r0, MemOperand(sp, 0 * kPointerSize));
8520
8521 // For arguments 4 and 3 get string length, calculate start of string data and
8522 // calculate the shift of the index (0 for ASCII and 1 for two byte).
8523 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
8524 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
8525 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8526 __ eor(r3, r3, Operand(1));
8527 // Argument 4 (r3): End of string data
8528 // Argument 3 (r2): Start of string data
8529 __ add(r2, r9, Operand(r1, LSL, r3));
8530 __ add(r3, r9, Operand(r0, LSL, r3));
8531
8532 // Argument 2 (r1): Previous index.
8533 // Already there
8534
8535 // Argument 1 (r0): Subject string.
8536 __ mov(r0, subject);
8537
8538 // Locate the code entry and call it.
8539 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
8540 __ CallCFunction(r7, kRegExpExecuteArguments);
8541 __ pop(lr);
8542
8543 // r0: result
8544 // subject: subject string (callee saved)
8545 // regexp_data: RegExp data (callee saved)
8546 // last_match_info_elements: Last match info elements (callee saved)
8547
8548 // Check the result.
8549 Label success;
8550 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
8551 __ b(eq, &success);
8552 Label failure;
8553 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
8554 __ b(eq, &failure);
8555 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
8556 // If not exception it can only be retry. Handle that in the runtime system.
8557 __ b(ne, &runtime);
8558 // Result must now be exception. If there is no pending exception already a
8559 // stack overflow (on the backtrack stack) was detected in RegExp code but
8560 // haven't created the exception yet. Handle that in the runtime system.
8561 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
8562 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
8563 __ ldr(r0, MemOperand(r0, 0));
8564 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
8565 __ ldr(r1, MemOperand(r1, 0));
8566 __ cmp(r0, r1);
8567 __ b(eq, &runtime);
8568 __ bind(&failure);
8569 // For failure and exception return null.
8570 __ mov(r0, Operand(Factory::null_value()));
8571 __ add(sp, sp, Operand(4 * kPointerSize));
8572 __ Ret();
8573
8574 // Process the result from the native regexp code.
8575 __ bind(&success);
8576 __ ldr(r1,
8577 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8578 // Calculate number of capture registers (number_of_captures + 1) * 2.
8579 ASSERT_EQ(0, kSmiTag);
8580 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8581 __ add(r1, r1, Operand(2)); // r1 was a smi.
8582
8583 // r1: number of capture registers
8584 // r4: subject string
8585 // Store the capture count.
8586 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
8587 __ str(r2, FieldMemOperand(last_match_info_elements,
8588 RegExpImpl::kLastCaptureCountOffset));
8589 // Store last subject and last input.
8590 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
8591 __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto.
8592 __ str(subject,
8593 FieldMemOperand(last_match_info_elements,
8594 RegExpImpl::kLastSubjectOffset));
8595 __ RecordWrite(r3, r2, r7);
8596 __ str(subject,
8597 FieldMemOperand(last_match_info_elements,
8598 RegExpImpl::kLastInputOffset));
8599 __ mov(r3, last_match_info_elements);
8600 __ mov(r2, Operand(RegExpImpl::kLastInputOffset));
8601 __ RecordWrite(r3, r2, r7);
8602
8603 // Get the static offsets vector filled by the native regexp code.
8604 ExternalReference address_of_static_offsets_vector =
8605 ExternalReference::address_of_static_offsets_vector();
8606 __ mov(r2, Operand(address_of_static_offsets_vector));
8607
8608 // r1: number of capture registers
8609 // r2: offsets vector
8610 Label next_capture, done;
8611 // Capture register counter starts from number of capture registers and
8612 // counts down until wraping after zero.
8613 __ add(r0,
8614 last_match_info_elements,
8615 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
8616 __ bind(&next_capture);
8617 __ sub(r1, r1, Operand(1), SetCC);
8618 __ b(mi, &done);
8619 // Read the value from the static offsets vector buffer.
8620 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
8621 // Store the smi value in the last match info.
8622 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
8623 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
8624 __ jmp(&next_capture);
8625 __ bind(&done);
8626
8627 // Return last match info.
8628 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8629 __ add(sp, sp, Operand(4 * kPointerSize));
8630 __ Ret();
8631
8632 // Do the runtime call to execute the regexp.
8633 __ bind(&runtime);
8634 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8635#endif // V8_NATIVE_REGEXP
8636}
8637
8638
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008639void CallFunctionStub::Generate(MacroAssembler* masm) {
8640 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008641
8642 // If the receiver might be a value (string, number or boolean) check for this
8643 // and box it if it is.
8644 if (ReceiverMightBeValue()) {
8645 // Get the receiver from the stack.
8646 // function, receiver [, arguments]
8647 Label receiver_is_value, receiver_is_js_object;
8648 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
8649
8650 // Check if receiver is a smi (which is a number value).
8651 __ BranchOnSmi(r1, &receiver_is_value);
8652
8653 // Check if the receiver is a valid JS object.
8654 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
8655 __ b(ge, &receiver_is_js_object);
8656
8657 // Call the runtime to box the value.
8658 __ bind(&receiver_is_value);
8659 __ EnterInternalFrame();
8660 __ push(r1);
8661 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
8662 __ LeaveInternalFrame();
8663 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
8664
8665 __ bind(&receiver_is_js_object);
8666 }
8667
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008668 // Get the function to call from the stack.
8669 // function, receiver [, arguments]
8670 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
8671
8672 // Check that the function is really a JavaScript function.
8673 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008674 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008675 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008676 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008677 __ b(ne, &slow);
8678
8679 // Fast-case: Invoke the function now.
8680 // r1: pushed function
8681 ParameterCount actual(argc_);
8682 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
8683
8684 // Slow-case: Non-function called.
8685 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00008686 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
8687 // of the original receiver from the call site).
8688 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008689 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008690 __ mov(r2, Operand(0));
8691 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
8692 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
8693 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008694}
8695
8696
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008697// Unfortunately you have to run without snapshots to see most of these
8698// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008699const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008700 if (name_ != NULL) return name_;
8701 const int kMaxNameLength = 100;
8702 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
8703 if (name_ == NULL) return "OOM";
8704
8705 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008706 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008707 case lt: cc_name = "LT"; break;
8708 case gt: cc_name = "GT"; break;
8709 case le: cc_name = "LE"; break;
8710 case ge: cc_name = "GE"; break;
8711 case eq: cc_name = "EQ"; break;
8712 case ne: cc_name = "NE"; break;
8713 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008714 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008715
8716 const char* strict_name = "";
8717 if (strict_ && (cc_ == eq || cc_ == ne)) {
8718 strict_name = "_STRICT";
8719 }
8720
8721 const char* never_nan_nan_name = "";
8722 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
8723 never_nan_nan_name = "_NO_NAN";
8724 }
8725
8726 const char* include_number_compare_name = "";
8727 if (!include_number_compare_) {
8728 include_number_compare_name = "_NO_NUMBER";
8729 }
8730
8731 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
8732 "CompareStub_%s%s%s%s",
8733 cc_name,
8734 strict_name,
8735 never_nan_nan_name,
8736 include_number_compare_name);
8737 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008738}
8739
8740
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008741int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008742 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
8743 // stubs the never NaN NaN condition is only taken into account if the
8744 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008745 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008746 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
8747 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008748 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
8749 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008750}
8751
8752
ager@chromium.org5c838252010-02-19 08:53:10 +00008753void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
8754 Register dest,
8755 Register src,
8756 Register count,
8757 Register scratch,
8758 bool ascii) {
8759 Label loop;
8760 Label done;
8761 // This loop just copies one character at a time, as it is only used for very
8762 // short strings.
8763 if (!ascii) {
8764 __ add(count, count, Operand(count), SetCC);
8765 } else {
8766 __ cmp(count, Operand(0));
8767 }
8768 __ b(eq, &done);
8769
8770 __ bind(&loop);
8771 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
8772 // Perform sub between load and dependent store to get the load time to
8773 // complete.
8774 __ sub(count, count, Operand(1), SetCC);
8775 __ strb(scratch, MemOperand(dest, 1, PostIndex));
8776 // last iteration.
8777 __ b(gt, &loop);
8778
8779 __ bind(&done);
8780}
8781
8782
8783enum CopyCharactersFlags {
8784 COPY_ASCII = 1,
8785 DEST_ALWAYS_ALIGNED = 2
8786};
8787
8788
8789void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
8790 Register dest,
8791 Register src,
8792 Register count,
8793 Register scratch1,
8794 Register scratch2,
8795 Register scratch3,
8796 Register scratch4,
8797 Register scratch5,
8798 int flags) {
8799 bool ascii = (flags & COPY_ASCII) != 0;
8800 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
8801
8802 if (dest_always_aligned && FLAG_debug_code) {
8803 // Check that destination is actually word aligned if the flag says
8804 // that it is.
8805 __ tst(dest, Operand(kPointerAlignmentMask));
8806 __ Check(eq, "Destination of copy not aligned.");
8807 }
8808
8809 const int kReadAlignment = 4;
8810 const int kReadAlignmentMask = kReadAlignment - 1;
8811 // Ensure that reading an entire aligned word containing the last character
8812 // of a string will not read outside the allocated area (because we pad up
8813 // to kObjectAlignment).
8814 ASSERT(kObjectAlignment >= kReadAlignment);
8815 // Assumes word reads and writes are little endian.
8816 // Nothing to do for zero characters.
8817 Label done;
8818 if (!ascii) {
8819 __ add(count, count, Operand(count), SetCC);
8820 } else {
8821 __ cmp(count, Operand(0));
8822 }
8823 __ b(eq, &done);
8824
8825 // Assume that you cannot read (or write) unaligned.
8826 Label byte_loop;
8827 // Must copy at least eight bytes, otherwise just do it one byte at a time.
8828 __ cmp(count, Operand(8));
8829 __ add(count, dest, Operand(count));
8830 Register limit = count; // Read until src equals this.
8831 __ b(lt, &byte_loop);
8832
8833 if (!dest_always_aligned) {
8834 // Align dest by byte copying. Copies between zero and three bytes.
8835 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
8836 Label dest_aligned;
8837 __ b(eq, &dest_aligned);
8838 __ cmp(scratch4, Operand(2));
8839 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
8840 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
8841 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
8842 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8843 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
8844 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
8845 __ bind(&dest_aligned);
8846 }
8847
8848 Label simple_loop;
8849
8850 __ sub(scratch4, dest, Operand(src));
8851 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
8852 __ b(eq, &simple_loop);
8853 // Shift register is number of bits in a source word that
8854 // must be combined with bits in the next source word in order
8855 // to create a destination word.
8856
8857 // Complex loop for src/dst that are not aligned the same way.
8858 {
8859 Label loop;
8860 __ mov(scratch4, Operand(scratch4, LSL, 3));
8861 Register left_shift = scratch4;
8862 __ and_(src, src, Operand(~3)); // Round down to load previous word.
8863 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8864 // Store the "shift" most significant bits of scratch in the least
8865 // signficant bits (i.e., shift down by (32-shift)).
8866 __ rsb(scratch2, left_shift, Operand(32));
8867 Register right_shift = scratch2;
8868 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
8869
8870 __ bind(&loop);
8871 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
8872 __ sub(scratch5, limit, Operand(dest));
8873 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
8874 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8875 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
8876 // Loop if four or more bytes left to copy.
8877 // Compare to eight, because we did the subtract before increasing dst.
8878 __ sub(scratch5, scratch5, Operand(8), SetCC);
8879 __ b(ge, &loop);
8880 }
8881 // There is now between zero and three bytes left to copy (negative that
8882 // number is in scratch5), and between one and three bytes already read into
8883 // scratch1 (eight times that number in scratch4). We may have read past
8884 // the end of the string, but because objects are aligned, we have not read
8885 // past the end of the object.
8886 // Find the minimum of remaining characters to move and preloaded characters
8887 // and write those as bytes.
8888 __ add(scratch5, scratch5, Operand(4), SetCC);
8889 __ b(eq, &done);
8890 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
8891 // Move minimum of bytes read and bytes left to copy to scratch4.
8892 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
8893 // Between one and three (value in scratch5) characters already read into
8894 // scratch ready to write.
8895 __ cmp(scratch5, Operand(2));
8896 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8897 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
8898 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
8899 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
8900 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
8901 // Copy any remaining bytes.
8902 __ b(&byte_loop);
8903
8904 // Simple loop.
8905 // Copy words from src to dst, until less than four bytes left.
8906 // Both src and dest are word aligned.
8907 __ bind(&simple_loop);
8908 {
8909 Label loop;
8910 __ bind(&loop);
8911 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8912 __ sub(scratch3, limit, Operand(dest));
8913 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8914 // Compare to 8, not 4, because we do the substraction before increasing
8915 // dest.
8916 __ cmp(scratch3, Operand(8));
8917 __ b(ge, &loop);
8918 }
8919
8920 // Copy bytes from src to dst until dst hits limit.
8921 __ bind(&byte_loop);
8922 __ cmp(dest, Operand(limit));
8923 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
8924 __ b(ge, &done);
8925 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8926 __ b(&byte_loop);
8927
8928 __ bind(&done);
8929}
8930
8931
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008932void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
8933 Register c1,
8934 Register c2,
8935 Register scratch1,
8936 Register scratch2,
8937 Register scratch3,
8938 Register scratch4,
8939 Register scratch5,
8940 Label* not_found) {
8941 // Register scratch3 is the general scratch register in this function.
8942 Register scratch = scratch3;
8943
8944 // Make sure that both characters are not digits as such strings has a
8945 // different hash algorithm. Don't try to look for these in the symbol table.
8946 Label not_array_index;
8947 __ sub(scratch, c1, Operand(static_cast<int>('0')));
8948 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8949 __ b(hi, &not_array_index);
8950 __ sub(scratch, c2, Operand(static_cast<int>('0')));
8951 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8952
8953 // If check failed combine both characters into single halfword.
8954 // This is required by the contract of the method: code at the
8955 // not_found branch expects this combination in c1 register
8956 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
8957 __ b(ls, not_found);
8958
8959 __ bind(&not_array_index);
8960 // Calculate the two character string hash.
8961 Register hash = scratch1;
8962 GenerateHashInit(masm, hash, c1);
8963 GenerateHashAddCharacter(masm, hash, c2);
8964 GenerateHashGetHash(masm, hash);
8965
8966 // Collect the two characters in a register.
8967 Register chars = c1;
8968 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
8969
8970 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8971 // hash: hash of two character string.
8972
8973 // Load symbol table
8974 // Load address of first element of the symbol table.
8975 Register symbol_table = c2;
8976 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
8977
8978 // Load undefined value
8979 Register undefined = scratch4;
8980 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
8981
8982 // Calculate capacity mask from the symbol table capacity.
8983 Register mask = scratch2;
8984 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
8985 __ mov(mask, Operand(mask, ASR, 1));
8986 __ sub(mask, mask, Operand(1));
8987
8988 // Calculate untagged address of the first element of the symbol table.
8989 Register first_symbol_table_element = symbol_table;
8990 __ add(first_symbol_table_element, symbol_table,
8991 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
8992
8993 // Registers
8994 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8995 // hash: hash of two character string
8996 // mask: capacity mask
8997 // first_symbol_table_element: address of the first element of
8998 // the symbol table
8999 // scratch: -
9000
9001 // Perform a number of probes in the symbol table.
9002 static const int kProbes = 4;
9003 Label found_in_symbol_table;
9004 Label next_probe[kProbes];
9005 for (int i = 0; i < kProbes; i++) {
9006 Register candidate = scratch5; // Scratch register contains candidate.
9007
9008 // Calculate entry in symbol table.
9009 if (i > 0) {
9010 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
9011 } else {
9012 __ mov(candidate, hash);
9013 }
9014
9015 __ and_(candidate, candidate, Operand(mask));
9016
9017 // Load the entry from the symble table.
9018 ASSERT_EQ(1, SymbolTable::kEntrySize);
9019 __ ldr(candidate,
9020 MemOperand(first_symbol_table_element,
9021 candidate,
9022 LSL,
9023 kPointerSizeLog2));
9024
9025 // If entry is undefined no string with this hash can be found.
9026 __ cmp(candidate, undefined);
9027 __ b(eq, not_found);
9028
9029 // If length is not 2 the string is not a candidate.
9030 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
9031 __ cmp(scratch, Operand(2));
9032 __ b(ne, &next_probe[i]);
9033
9034 // Check that the candidate is a non-external ascii string.
9035 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
9036 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
9037 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
9038 &next_probe[i]);
9039
9040 // Check if the two characters match.
9041 // Assumes that word load is little endian.
9042 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
9043 __ cmp(chars, scratch);
9044 __ b(eq, &found_in_symbol_table);
9045 __ bind(&next_probe[i]);
9046 }
9047
9048 // No matching 2 character string found by probing.
9049 __ jmp(not_found);
9050
9051 // Scratch register contains result when we fall through to here.
9052 Register result = scratch;
9053 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00009054 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009055}
9056
9057
9058void StringStubBase::GenerateHashInit(MacroAssembler* masm,
9059 Register hash,
9060 Register character) {
9061 // hash = character + (character << 10);
9062 __ add(hash, character, Operand(character, LSL, 10));
9063 // hash ^= hash >> 6;
9064 __ eor(hash, hash, Operand(hash, ASR, 6));
9065}
9066
9067
9068void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm,
9069 Register hash,
9070 Register character) {
9071 // hash += character;
9072 __ add(hash, hash, Operand(character));
9073 // hash += hash << 10;
9074 __ add(hash, hash, Operand(hash, LSL, 10));
9075 // hash ^= hash >> 6;
9076 __ eor(hash, hash, Operand(hash, ASR, 6));
9077}
9078
9079
9080void StringStubBase::GenerateHashGetHash(MacroAssembler* masm,
9081 Register hash) {
9082 // hash += hash << 3;
9083 __ add(hash, hash, Operand(hash, LSL, 3));
9084 // hash ^= hash >> 11;
9085 __ eor(hash, hash, Operand(hash, ASR, 11));
9086 // hash += hash << 15;
9087 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
9088
9089 // if (hash == 0) hash = 27;
9090 __ mov(hash, Operand(27), LeaveCC, nz);
9091}
9092
9093
ager@chromium.org5c838252010-02-19 08:53:10 +00009094void SubStringStub::Generate(MacroAssembler* masm) {
9095 Label runtime;
9096
9097 // Stack frame on entry.
9098 // lr: return address
9099 // sp[0]: to
9100 // sp[4]: from
9101 // sp[8]: string
9102
9103 // This stub is called from the native-call %_SubString(...), so
9104 // nothing can be assumed about the arguments. It is tested that:
9105 // "string" is a sequential string,
9106 // both "from" and "to" are smis, and
9107 // 0 <= from <= to <= string.length.
9108 // If any of these assumptions fail, we call the runtime system.
9109
9110 static const int kToOffset = 0 * kPointerSize;
9111 static const int kFromOffset = 1 * kPointerSize;
9112 static const int kStringOffset = 2 * kPointerSize;
9113
9114
9115 // Check bounds and smi-ness.
9116 __ ldr(r7, MemOperand(sp, kToOffset));
9117 __ ldr(r6, MemOperand(sp, kFromOffset));
9118 ASSERT_EQ(0, kSmiTag);
9119 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
9120 // I.e., arithmetic shift right by one un-smi-tags.
9121 __ mov(r2, Operand(r7, ASR, 1), SetCC);
9122 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
9123 // If either r2 or r6 had the smi tag bit set, then carry is set now.
9124 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
9125 __ b(mi, &runtime); // From is negative.
9126
9127 __ sub(r2, r2, Operand(r3), SetCC);
9128 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009129 // Special handling of sub-strings of length 1 and 2. One character strings
9130 // are handled in the runtime system (looked up in the single character
9131 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00009132 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009133 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00009134
9135 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009136 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009137 // r6: from (smi)
9138 // r7: to (smi)
9139
9140 // Make sure first argument is a sequential (or flat) string.
9141 __ ldr(r5, MemOperand(sp, kStringOffset));
9142 ASSERT_EQ(0, kSmiTag);
9143 __ tst(r5, Operand(kSmiTagMask));
9144 __ b(eq, &runtime);
9145 Condition is_string = masm->IsObjectStringType(r5, r1);
9146 __ b(NegateCondition(is_string), &runtime);
9147
9148 // r1: instance type
9149 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009150 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009151 // r5: string
9152 // r6: from (smi)
9153 // r7: to (smi)
9154 Label seq_string;
9155 __ and_(r4, r1, Operand(kStringRepresentationMask));
9156 ASSERT(kSeqStringTag < kConsStringTag);
9157 ASSERT(kExternalStringTag > kConsStringTag);
9158 __ cmp(r4, Operand(kConsStringTag));
9159 __ b(gt, &runtime); // External strings go to runtime.
9160 __ b(lt, &seq_string); // Sequential strings are handled directly.
9161
9162 // Cons string. Try to recurse (once) on the first substring.
9163 // (This adds a little more generality than necessary to handle flattened
9164 // cons strings, but not much).
9165 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
9166 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
9167 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9168 __ tst(r1, Operand(kStringRepresentationMask));
9169 ASSERT_EQ(0, kSeqStringTag);
9170 __ b(ne, &runtime); // Cons and External strings go to runtime.
9171
9172 // Definitly a sequential string.
9173 __ bind(&seq_string);
9174
9175 // r1: instance type.
9176 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009177 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009178 // r5: string
9179 // r6: from (smi)
9180 // r7: to (smi)
9181 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
9182 __ cmp(r4, Operand(r7, ASR, 1));
9183 __ b(lt, &runtime); // Fail if to > length.
9184
9185 // r1: instance type.
9186 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009187 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009188 // r5: string.
9189 // r6: from offset (smi)
9190 // Check for flat ascii string.
9191 Label non_ascii_flat;
9192 __ tst(r1, Operand(kStringEncodingMask));
9193 ASSERT_EQ(0, kTwoByteStringTag);
9194 __ b(eq, &non_ascii_flat);
9195
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009196 Label result_longer_than_two;
9197 __ cmp(r2, Operand(2));
9198 __ b(gt, &result_longer_than_two);
9199
9200 // Sub string of length 2 requested.
9201 // Get the two characters forming the sub string.
9202 __ add(r5, r5, Operand(r3));
9203 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
9204 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
9205
9206 // Try to lookup two character string in symbol table.
9207 Label make_two_character_string;
9208 GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9,
9209 &make_two_character_string);
9210 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9211 __ add(sp, sp, Operand(3 * kPointerSize));
9212 __ Ret();
9213
9214 // r2: result string length.
9215 // r3: two characters combined into halfword in little endian byte order.
9216 __ bind(&make_two_character_string);
9217 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
9218 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9219 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9220 __ add(sp, sp, Operand(3 * kPointerSize));
9221 __ Ret();
9222
9223 __ bind(&result_longer_than_two);
9224
ager@chromium.org5c838252010-02-19 08:53:10 +00009225 // Allocate the result.
9226 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
9227
9228 // r0: result string.
9229 // r2: result string length.
9230 // r5: string.
9231 // r6: from offset (smi)
9232 // Locate first character of result.
9233 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9234 // Locate 'from' character of string.
9235 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9236 __ add(r5, r5, Operand(r6, ASR, 1));
9237
9238 // r0: result string.
9239 // r1: first character of result string.
9240 // r2: result string length.
9241 // r5: first character of sub string to copy.
9242 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
9243 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9244 COPY_ASCII | DEST_ALWAYS_ALIGNED);
9245 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9246 __ add(sp, sp, Operand(3 * kPointerSize));
9247 __ Ret();
9248
9249 __ bind(&non_ascii_flat);
9250 // r2: result string length.
9251 // r5: string.
9252 // r6: from offset (smi)
9253 // Check for flat two byte string.
9254
9255 // Allocate the result.
9256 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
9257
9258 // r0: result string.
9259 // r2: result string length.
9260 // r5: string.
9261 // Locate first character of result.
9262 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9263 // Locate 'from' character of string.
9264 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9265 // As "from" is a smi it is 2 times the value which matches the size of a two
9266 // byte character.
9267 __ add(r5, r5, Operand(r6));
9268
9269 // r0: result string.
9270 // r1: first character of result.
9271 // r2: result length.
9272 // r5: first character of string to copy.
9273 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
9274 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9275 DEST_ALWAYS_ALIGNED);
9276 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9277 __ add(sp, sp, Operand(3 * kPointerSize));
9278 __ Ret();
9279
9280 // Just jump to runtime to create the sub string.
9281 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009282 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009283}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009284
9285
9286void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
9287 Register left,
9288 Register right,
9289 Register scratch1,
9290 Register scratch2,
9291 Register scratch3,
9292 Register scratch4) {
9293 Label compare_lengths;
9294 // Find minimum length and length difference.
9295 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
9296 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
9297 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
9298 Register length_delta = scratch3;
9299 __ mov(scratch1, scratch2, LeaveCC, gt);
9300 Register min_length = scratch1;
9301 __ tst(min_length, Operand(min_length));
9302 __ b(eq, &compare_lengths);
9303
9304 // Setup registers so that we only need to increment one register
9305 // in the loop.
9306 __ add(scratch2, min_length,
9307 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9308 __ add(left, left, Operand(scratch2));
9309 __ add(right, right, Operand(scratch2));
9310 // Registers left and right points to the min_length character of strings.
9311 __ rsb(min_length, min_length, Operand(-1));
9312 Register index = min_length;
9313 // Index starts at -min_length.
9314
9315 {
9316 // Compare loop.
9317 Label loop;
9318 __ bind(&loop);
9319 // Compare characters.
9320 __ add(index, index, Operand(1), SetCC);
9321 __ ldrb(scratch2, MemOperand(left, index), ne);
9322 __ ldrb(scratch4, MemOperand(right, index), ne);
9323 // Skip to compare lengths with eq condition true.
9324 __ b(eq, &compare_lengths);
9325 __ cmp(scratch2, scratch4);
9326 __ b(eq, &loop);
9327 // Fallthrough with eq condition false.
9328 }
9329 // Compare lengths - strings up to min-length are equal.
9330 __ bind(&compare_lengths);
9331 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
9332 // Use zero length_delta as result.
9333 __ mov(r0, Operand(length_delta), SetCC, eq);
9334 // Fall through to here if characters compare not-equal.
9335 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
9336 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
9337 __ Ret();
9338}
9339
9340
9341void StringCompareStub::Generate(MacroAssembler* masm) {
9342 Label runtime;
9343
9344 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00009345 // sp[0]: right string
9346 // sp[4]: left string
9347 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
9348 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009349
9350 Label not_same;
9351 __ cmp(r0, r1);
9352 __ b(ne, &not_same);
9353 ASSERT_EQ(0, EQUAL);
9354 ASSERT_EQ(0, kSmiTag);
9355 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
9356 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
9357 __ add(sp, sp, Operand(2 * kPointerSize));
9358 __ Ret();
9359
9360 __ bind(&not_same);
9361
9362 // Check that both objects are sequential ascii strings.
9363 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
9364
9365 // Compare flat ascii strings natively. Remove arguments from stack first.
9366 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
9367 __ add(sp, sp, Operand(2 * kPointerSize));
9368 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
9369
9370 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
9371 // tagged as a small integer.
9372 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009373 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009374}
9375
9376
ager@chromium.org5c838252010-02-19 08:53:10 +00009377void StringAddStub::Generate(MacroAssembler* masm) {
9378 Label string_add_runtime;
9379 // Stack on entry:
9380 // sp[0]: second argument.
9381 // sp[4]: first argument.
9382
9383 // Load the two arguments.
9384 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
9385 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
9386
9387 // Make sure that both arguments are strings if not known in advance.
9388 if (string_check_) {
9389 ASSERT_EQ(0, kSmiTag);
9390 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
9391 // Load instance types.
9392 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9393 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9394 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9395 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9396 ASSERT_EQ(0, kStringTag);
9397 // If either is not a string, go to runtime.
9398 __ tst(r4, Operand(kIsNotStringMask));
9399 __ tst(r5, Operand(kIsNotStringMask), eq);
9400 __ b(ne, &string_add_runtime);
9401 }
9402
9403 // Both arguments are strings.
9404 // r0: first string
9405 // r1: second string
9406 // r4: first string instance type (if string_check_)
9407 // r5: second string instance type (if string_check_)
9408 {
9409 Label strings_not_empty;
9410 // Check if either of the strings are empty. In that case return the other.
9411 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
9412 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
9413 __ cmp(r2, Operand(0)); // Test if first string is empty.
9414 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
9415 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
9416 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
9417
9418 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9419 __ add(sp, sp, Operand(2 * kPointerSize));
9420 __ Ret();
9421
9422 __ bind(&strings_not_empty);
9423 }
9424
9425 // Both strings are non-empty.
9426 // r0: first string
9427 // r1: second string
9428 // r2: length of first string
9429 // r3: length of second string
9430 // r4: first string instance type (if string_check_)
9431 // r5: second string instance type (if string_check_)
9432 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009433 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00009434 // Adding two lengths can't overflow.
9435 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
9436 __ add(r6, r2, Operand(r3));
9437 // Use the runtime system when adding two one character strings, as it
9438 // contains optimizations for this specific case using the symbol table.
9439 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009440 __ b(ne, &longer_than_two);
9441
9442 // Check that both strings are non-external ascii strings.
9443 if (!string_check_) {
9444 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9445 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9446 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9447 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9448 }
9449 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
9450 &string_add_runtime);
9451
9452 // Get the two characters forming the sub string.
9453 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9454 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
9455
9456 // Try to lookup two character string in symbol table. If it is not found
9457 // just allocate a new one.
9458 Label make_two_character_string;
9459 GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9,
9460 &make_two_character_string);
9461 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9462 __ add(sp, sp, Operand(2 * kPointerSize));
9463 __ Ret();
9464
9465 __ bind(&make_two_character_string);
9466 // Resulting string has length 2 and first chars of two strings
9467 // are combined into single halfword in r2 register.
9468 // So we can fill resulting string without two loops by a single
9469 // halfword store instruction (which assumes that processor is
9470 // in a little endian mode)
9471 __ mov(r6, Operand(2));
9472 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
9473 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9474 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9475 __ add(sp, sp, Operand(2 * kPointerSize));
9476 __ Ret();
9477
9478 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00009479 // Check if resulting string will be flat.
9480 __ cmp(r6, Operand(String::kMinNonFlatLength));
9481 __ b(lt, &string_add_flat_result);
9482 // Handle exceptionally long strings in the runtime system.
9483 ASSERT((String::kMaxLength & 0x80000000) == 0);
9484 ASSERT(IsPowerOf2(String::kMaxLength + 1));
9485 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
9486 __ cmp(r6, Operand(String::kMaxLength + 1));
9487 __ b(hs, &string_add_runtime);
9488
9489 // If result is not supposed to be flat, allocate a cons string object.
9490 // If both strings are ascii the result is an ascii cons string.
9491 if (!string_check_) {
9492 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9493 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9494 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9495 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9496 }
9497 Label non_ascii, allocated;
9498 ASSERT_EQ(0, kTwoByteStringTag);
9499 __ tst(r4, Operand(kStringEncodingMask));
9500 __ tst(r5, Operand(kStringEncodingMask), ne);
9501 __ b(eq, &non_ascii);
9502
9503 // Allocate an ASCII cons string.
9504 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
9505 __ bind(&allocated);
9506 // Fill the fields of the cons string.
9507 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
9508 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
9509 __ mov(r0, Operand(r7));
9510 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9511 __ add(sp, sp, Operand(2 * kPointerSize));
9512 __ Ret();
9513
9514 __ bind(&non_ascii);
9515 // Allocate a two byte cons string.
9516 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
9517 __ jmp(&allocated);
9518
9519 // Handle creating a flat result. First check that both strings are
9520 // sequential and that they have the same encoding.
9521 // r0: first string
9522 // r1: second string
9523 // r2: length of first string
9524 // r3: length of second string
9525 // r4: first string instance type (if string_check_)
9526 // r5: second string instance type (if string_check_)
9527 // r6: sum of lengths.
9528 __ bind(&string_add_flat_result);
9529 if (!string_check_) {
9530 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9531 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9532 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9533 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9534 }
9535 // Check that both strings are sequential.
9536 ASSERT_EQ(0, kSeqStringTag);
9537 __ tst(r4, Operand(kStringRepresentationMask));
9538 __ tst(r5, Operand(kStringRepresentationMask), eq);
9539 __ b(ne, &string_add_runtime);
9540 // Now check if both strings have the same encoding (ASCII/Two-byte).
9541 // r0: first string.
9542 // r1: second string.
9543 // r2: length of first string.
9544 // r3: length of second string.
9545 // r6: sum of lengths..
9546 Label non_ascii_string_add_flat_result;
9547 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
9548 __ eor(r7, r4, Operand(r5));
9549 __ tst(r7, Operand(kStringEncodingMask));
9550 __ b(ne, &string_add_runtime);
9551 // And see if it's ASCII or two-byte.
9552 __ tst(r4, Operand(kStringEncodingMask));
9553 __ b(eq, &non_ascii_string_add_flat_result);
9554
9555 // Both strings are sequential ASCII strings. We also know that they are
9556 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009557 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00009558 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
9559 // Locate first character of result.
9560 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9561 // Locate first character of first argument.
9562 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9563 // r0: first character of first string.
9564 // r1: second string.
9565 // r2: length of first string.
9566 // r3: length of second string.
9567 // r6: first character of result.
9568 // r7: result string.
9569 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
9570
9571 // Load second argument and locate first character.
9572 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9573 // r1: first character of second string.
9574 // r3: length of second string.
9575 // r6: next character of result.
9576 // r7: result string.
9577 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
9578 __ mov(r0, Operand(r7));
9579 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9580 __ add(sp, sp, Operand(2 * kPointerSize));
9581 __ Ret();
9582
9583 __ bind(&non_ascii_string_add_flat_result);
9584 // Both strings are sequential two byte strings.
9585 // r0: first string.
9586 // r1: second string.
9587 // r2: length of first string.
9588 // r3: length of second string.
9589 // r6: sum of length of strings.
9590 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
9591 // r0: first string.
9592 // r1: second string.
9593 // r2: length of first string.
9594 // r3: length of second string.
9595 // r7: result string.
9596
9597 // Locate first character of result.
9598 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9599 // Locate first character of first argument.
9600 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9601
9602 // r0: first character of first string.
9603 // r1: second string.
9604 // r2: length of first string.
9605 // r3: length of second string.
9606 // r6: first character of result.
9607 // r7: result string.
9608 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
9609
9610 // Locate first character of second argument.
9611 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9612
9613 // r1: first character of second string.
9614 // r3: length of second string.
9615 // r6: next character of result (after copy of first string).
9616 // r7: result string.
9617 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
9618
9619 __ mov(r0, Operand(r7));
9620 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9621 __ add(sp, sp, Operand(2 * kPointerSize));
9622 __ Ret();
9623
9624 // Just jump to runtime to add the two strings.
9625 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009626 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009627}
9628
9629
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009630#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009632} } // namespace v8::internal