blob: 30860a1f997b0ad7dce303f2ec518b832711c5cd [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());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000568 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569}
570
571
ager@chromium.org7c537e22008-10-16 08:43:32 +0000572void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000573 Register reg = frame_->GetTOSRegister();
574 __ ldr(reg, GlobalObject());
575 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576}
577
578
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000579void CodeGenerator::LoadGlobalReceiver(Register scratch) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000580 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000581 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
582 __ ldr(scratch,
583 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000585}
586
587
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000588ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
589 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
590 ASSERT(scope()->arguments_shadow() != NULL);
591 // We don't want to do lazy arguments allocation for functions that
592 // have heap-allocated contexts, because it interfers with the
593 // uninitialized const tracking in the context objects.
594 return (scope()->num_heap_slots() > 0)
595 ? EAGER_ARGUMENTS_ALLOCATION
596 : LAZY_ARGUMENTS_ALLOCATION;
597}
598
599
600void CodeGenerator::StoreArgumentsObject(bool initial) {
601 VirtualFrame::SpilledScope spilled_scope(frame_);
602
603 ArgumentsAllocationMode mode = ArgumentsMode();
604 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
605
606 Comment cmnt(masm_, "[ store arguments object");
607 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
608 // When using lazy arguments allocation, we store the hole value
609 // as a sentinel indicating that the arguments object hasn't been
610 // allocated yet.
611 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
612 frame_->EmitPush(ip);
613 } else {
614 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
615 __ ldr(r2, frame_->Function());
616 // The receiver is below the arguments, the return address, and the
617 // frame pointer on the stack.
618 const int kReceiverDisplacement = 2 + scope()->num_parameters();
619 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
620 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
621 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000622 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000623 frame_->CallStub(&stub, 3);
624 frame_->EmitPush(r0);
625 }
626
627 Variable* arguments = scope()->arguments()->var();
628 Variable* shadow = scope()->arguments_shadow()->var();
629 ASSERT(arguments != NULL && arguments->slot() != NULL);
630 ASSERT(shadow != NULL && shadow->slot() != NULL);
631 JumpTarget done;
632 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
633 // We have to skip storing into the arguments slot if it has
634 // already been written to. This can happen if the a function
635 // has a local variable named 'arguments'.
636 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
637 frame_->EmitPop(r0);
638 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
639 __ cmp(r0, ip);
640 done.Branch(ne);
641 }
642 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
643 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
644 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
645}
646
647
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000648void CodeGenerator::LoadTypeofExpression(Expression* expr) {
649 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.org357bf652010-04-12 11:30:10 +0000650 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000651 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000653 // For a global variable we build the property reference
654 // <global>.<variable> and perform a (regular non-contextual) property
655 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
657 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000658 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000659 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000660 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000661 } else if (variable != NULL && variable->slot() != NULL) {
662 // For a variable that rewrites to a slot, we signal it is the immediate
663 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000664 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000665 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000667 // Anything else can be handled normally.
668 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 }
670}
671
672
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000673Reference::Reference(CodeGenerator* cgen,
674 Expression* expression,
675 bool persist_after_get)
676 : cgen_(cgen),
677 expression_(expression),
678 type_(ILLEGAL),
679 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 cgen->LoadReference(this);
681}
682
683
684Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000685 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
ager@chromium.org7c537e22008-10-16 08:43:32 +0000689void CodeGenerator::LoadReference(Reference* ref) {
690 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 Expression* e = ref->expression();
692 Property* property = e->AsProperty();
693 Variable* var = e->AsVariableProxy()->AsVariable();
694
695 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000696 // The expression is either a property or a variable proxy that rewrites
697 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000698 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000699 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 ref->set_type(Reference::NAMED);
701 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000702 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 ref->set_type(Reference::KEYED);
704 }
705 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000706 // The expression is a variable proxy that does not rewrite to a
707 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 LoadGlobal();
710 ref->set_type(Reference::NAMED);
711 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000712 ASSERT(var->slot() != NULL);
713 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 }
715 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000716 // Anything else is a runtime error.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000717 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000718 LoadAndSpill(e);
719 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 }
721}
722
723
ager@chromium.org7c537e22008-10-16 08:43:32 +0000724void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000726 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000727 if (size == 0) return;
728
729 // Pop a reference from the stack while preserving TOS.
730 VirtualFrame::RegisterAllocationScope scope(this);
731 Comment cmnt(masm_, "[ UnloadReference");
732 if (size > 0) {
733 Register tos = frame_->PopToRegister();
734 frame_->Drop(size);
735 frame_->EmitPush(tos);
736 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737}
738
739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
741// register to a boolean in the condition code register. The code
742// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743void CodeGenerator::ToBoolean(JumpTarget* true_target,
744 JumpTarget* false_target) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000745 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000746 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
750 // Fast case checks
751
mads.s.ager31e71382008-08-13 09:32:07 +0000752 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000753 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
754 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
mads.s.ager31e71382008-08-13 09:32:07 +0000757 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000758 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
759 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
mads.s.ager31e71382008-08-13 09:32:07 +0000762 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000763 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
764 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000765 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766
mads.s.ager31e71382008-08-13 09:32:07 +0000767 // Check if the value is a smi.
768 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000770 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000771 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
773 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000774 frame_->EmitPush(r0);
775 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000776 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000777 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
778 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779
780 cc_reg_ = ne;
781}
782
783
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000784void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000785 OverwriteMode overwrite_mode,
786 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000787 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000788 // sp[0] : y
789 // sp[1] : x
790 // result : r0
791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 // Stub is entered with a call: 'return address' is in lr.
793 switch (op) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000794 case Token::ADD:
795 case Token::SUB:
796 case Token::MUL:
797 case Token::DIV:
798 case Token::MOD:
799 case Token::BIT_OR:
800 case Token::BIT_AND:
801 case Token::BIT_XOR:
802 case Token::SHL:
803 case Token::SHR:
804 case Token::SAR: {
805 frame_->EmitPop(r0); // r0 : y
806 frame_->EmitPop(r1); // r1 : x
807 GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs);
808 frame_->CallStub(&stub, 0);
809 break;
810 }
811
812 case Token::COMMA:
813 frame_->EmitPop(r0);
814 // Simply discard left value.
815 frame_->Drop();
816 break;
817
818 default:
819 // Other cases should have been handled before this point.
820 UNREACHABLE();
821 break;
822 }
823}
824
825
826void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op,
827 OverwriteMode overwrite_mode,
828 int constant_rhs) {
829 // top of virtual frame: y
830 // 2nd elt. on virtual frame : x
831 // result : top of virtual frame
832
833 // Stub is entered with a call: 'return address' is in lr.
834 switch (op) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835 case Token::ADD: // fall through.
836 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000837 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000838 case Token::DIV:
839 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000840 case Token::BIT_OR:
841 case Token::BIT_AND:
842 case Token::BIT_XOR:
843 case Token::SHL:
844 case Token::SHR:
845 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000846 Register rhs = frame_->PopToRegister();
847 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
848 {
849 VirtualFrame::SpilledScope spilled_scope(frame_);
850 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
851 frame_->CallStub(&stub, 0);
852 }
853 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854 break;
855 }
856
ager@chromium.org357bf652010-04-12 11:30:10 +0000857 case Token::COMMA: {
858 Register scratch = frame_->PopToRegister();
859 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000860 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000861 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864
865 default:
866 // Other cases should have been handled before this point.
867 UNREACHABLE();
868 break;
869 }
870}
871
872
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000873class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000874 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000875 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000876 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000877 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000878 OverwriteMode overwrite_mode,
879 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000880 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000881 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000882 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000883 overwrite_mode_(overwrite_mode),
884 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 set_comment("[ DeferredInlinedSmiOperation");
886 }
887
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000888 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889
890 private:
891 Token::Value op_;
892 int value_;
893 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000894 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000895 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896};
897
898
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000899void DeferredInlineSmiOperation::Generate() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000900 Register lhs = r1;
901 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902 switch (op_) {
903 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000906 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 __ mov(r1, Operand(Smi::FromInt(value_)));
908 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000909 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000910 __ mov(r0, Operand(Smi::FromInt(value_)));
911 }
912 break;
913 }
914
915 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000916 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000917 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000918 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000919 __ mov(r1, Operand(Smi::FromInt(value_)));
920 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000921 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000922 __ mov(r0, Operand(Smi::FromInt(value_)));
923 }
924 break;
925 }
926
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000927 // For these operations there is no optimistic operation that needs to be
928 // reverted.
929 case Token::MUL:
930 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931 case Token::BIT_OR:
932 case Token::BIT_XOR:
933 case Token::BIT_AND: {
934 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000935 if (tos_register_.is(r0)) {
936 __ mov(r1, Operand(Smi::FromInt(value_)));
937 } else {
938 ASSERT(tos_register_.is(r1));
939 __ mov(r0, Operand(Smi::FromInt(value_)));
940 lhs = r0;
941 rhs = r1;
942 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000943 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000944 if (tos_register_.is(r1)) {
945 __ mov(r0, Operand(Smi::FromInt(value_)));
946 } else {
947 ASSERT(tos_register_.is(r0));
948 __ mov(r1, Operand(Smi::FromInt(value_)));
949 lhs = r0;
950 rhs = r1;
951 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000952 }
953 break;
954 }
955
956 case Token::SHL:
957 case Token::SHR:
958 case Token::SAR: {
959 if (!reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000960 if (tos_register_.is(r1)) {
961 __ mov(r0, Operand(Smi::FromInt(value_)));
962 } else {
963 ASSERT(tos_register_.is(r0));
964 __ mov(r1, Operand(Smi::FromInt(value_)));
965 lhs = r0;
966 rhs = r1;
967 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000968 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000969 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000970 }
971 break;
972 }
973
974 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000975 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000976 UNREACHABLE();
977 break;
978 }
979
ager@chromium.org357bf652010-04-12 11:30:10 +0000980 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000981 __ CallStub(&stub);
ager@chromium.org357bf652010-04-12 11:30:10 +0000982 // The generic stub returns its value in r0, but that's not
983 // necessarily what we want. We want whatever the inlined code
984 // expected, which is that the answer is in the same register as
985 // the operand was.
986 __ Move(tos_register_, r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000987}
988
989
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000990static bool PopCountLessThanEqual2(unsigned int x) {
991 x &= x - 1;
992 return (x & (x - 1)) == 0;
993}
994
995
996// Returns the index of the lowest bit set.
997static int BitPosition(unsigned x) {
998 int bit_posn = 0;
999 while ((x & 0xf) == 0) {
1000 bit_posn += 4;
1001 x >>= 4;
1002 }
1003 while ((x & 1) == 0) {
1004 bit_posn++;
1005 x >>= 1;
1006 }
1007 return bit_posn;
1008}
1009
1010
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001011void CodeGenerator::SmiOperation(Token::Value op,
1012 Handle<Object> value,
1013 bool reversed,
1014 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001015 int int_value = Smi::cast(*value)->value();
1016
1017 bool something_to_inline;
1018 switch (op) {
1019 case Token::ADD:
1020 case Token::SUB:
1021 case Token::BIT_AND:
1022 case Token::BIT_OR:
1023 case Token::BIT_XOR: {
1024 something_to_inline = true;
1025 break;
1026 }
1027 case Token::SHL:
1028 case Token::SHR:
1029 case Token::SAR: {
1030 if (reversed) {
1031 something_to_inline = false;
1032 } else {
1033 something_to_inline = true;
1034 }
1035 break;
1036 }
1037 case Token::MOD: {
1038 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1039 something_to_inline = false;
1040 } else {
1041 something_to_inline = true;
1042 }
1043 break;
1044 }
1045 case Token::MUL: {
1046 if (!IsEasyToMultiplyBy(int_value)) {
1047 something_to_inline = false;
1048 } else {
1049 something_to_inline = true;
1050 }
1051 break;
1052 }
1053 default: {
1054 something_to_inline = false;
1055 break;
1056 }
1057 }
1058
1059 if (!something_to_inline) {
1060 if (!reversed) {
1061 // Push the rhs onto the virtual frame by putting it in a TOS register.
1062 Register rhs = frame_->GetTOSRegister();
1063 __ mov(rhs, Operand(value));
1064 frame_->EmitPush(rhs);
1065 VirtualFrameBinaryOperation(op, mode, int_value);
1066 } else {
1067 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1068 // at most one pop, the rest takes place in TOS registers.
1069 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1070 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1071 __ mov(lhs, Operand(value));
1072 frame_->EmitPush(lhs);
1073 frame_->EmitPush(rhs);
1074 VirtualFrameBinaryOperation(op, mode, kUnknownIntValue);
1075 }
1076 return;
1077 }
1078
1079 // We move the top of stack to a register (normally no move is invoved).
1080 Register tos = frame_->PopToRegister();
1081 // All other registers are spilled. The deferred code expects one argument
1082 // in a register and all other values are flushed to the stack. The
1083 // answer is returned in the same register that the top of stack argument was
1084 // in.
1085 frame_->SpillAll();
1086
1087 switch (op) {
1088 case Token::ADD: {
1089 DeferredCode* deferred =
1090 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1091
1092 __ add(tos, tos, Operand(value), SetCC);
1093 deferred->Branch(vs);
1094 __ tst(tos, Operand(kSmiTagMask));
1095 deferred->Branch(ne);
1096 deferred->BindExit();
1097 frame_->EmitPush(tos);
1098 break;
1099 }
1100
1101 case Token::SUB: {
1102 DeferredCode* deferred =
1103 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1104
1105 if (reversed) {
1106 __ rsb(tos, tos, Operand(value), SetCC);
1107 } else {
1108 __ sub(tos, tos, Operand(value), SetCC);
1109 }
1110 deferred->Branch(vs);
1111 __ tst(tos, Operand(kSmiTagMask));
1112 deferred->Branch(ne);
1113 deferred->BindExit();
1114 frame_->EmitPush(tos);
1115 break;
1116 }
1117
1118
1119 case Token::BIT_OR:
1120 case Token::BIT_XOR:
1121 case Token::BIT_AND: {
1122 DeferredCode* deferred =
1123 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1124 __ tst(tos, Operand(kSmiTagMask));
1125 deferred->Branch(ne);
1126 switch (op) {
1127 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1128 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
1129 case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break;
1130 default: UNREACHABLE();
1131 }
1132 deferred->BindExit();
1133 frame_->EmitPush(tos);
1134 break;
1135 }
1136
1137 case Token::SHL:
1138 case Token::SHR:
1139 case Token::SAR: {
1140 ASSERT(!reversed);
1141 Register scratch = VirtualFrame::scratch0();
1142 Register scratch2 = VirtualFrame::scratch1();
1143 int shift_value = int_value & 0x1f; // least significant 5 bits
1144 DeferredCode* deferred =
1145 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
1146 __ tst(tos, Operand(kSmiTagMask));
1147 deferred->Branch(ne);
1148 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags
1149 switch (op) {
1150 case Token::SHL: {
1151 if (shift_value != 0) {
1152 __ mov(scratch, Operand(scratch, LSL, shift_value));
1153 }
1154 // check that the *signed* result fits in a smi
1155 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1156 deferred->Branch(mi);
1157 break;
1158 }
1159 case Token::SHR: {
1160 // LSR by immediate 0 means shifting 32 bits.
1161 if (shift_value != 0) {
1162 __ mov(scratch, Operand(scratch, LSR, shift_value));
1163 }
1164 // check that the *unsigned* result fits in a smi
1165 // neither of the two high-order bits can be set:
1166 // - 0x80000000: high bit would be lost when smi tagging
1167 // - 0x40000000: this number would convert to negative when
1168 // smi tagging these two cases can only happen with shifts
1169 // by 0 or 1 when handed a valid smi
1170 __ tst(scratch, Operand(0xc0000000));
1171 deferred->Branch(ne);
1172 break;
1173 }
1174 case Token::SAR: {
1175 if (shift_value != 0) {
1176 // ASR by immediate 0 means shifting 32 bits.
1177 __ mov(scratch, Operand(scratch, ASR, shift_value));
1178 }
1179 break;
1180 }
1181 default: UNREACHABLE();
1182 }
1183 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1184 deferred->BindExit();
1185 frame_->EmitPush(tos);
1186 break;
1187 }
1188
1189 case Token::MOD: {
1190 ASSERT(!reversed);
1191 ASSERT(int_value >= 2);
1192 ASSERT(IsPowerOf2(int_value));
1193 DeferredCode* deferred =
1194 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1195 unsigned mask = (0x80000000u | kSmiTagMask);
1196 __ tst(tos, Operand(mask));
1197 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1198 mask = (int_value << kSmiTagSize) - 1;
1199 __ and_(tos, tos, Operand(mask));
1200 deferred->BindExit();
1201 frame_->EmitPush(tos);
1202 break;
1203 }
1204
1205 case Token::MUL: {
1206 ASSERT(IsEasyToMultiplyBy(int_value));
1207 DeferredCode* deferred =
1208 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1209 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1210 max_smi_that_wont_overflow <<= kSmiTagSize;
1211 unsigned mask = 0x80000000u;
1212 while ((mask & max_smi_that_wont_overflow) == 0) {
1213 mask |= mask >> 1;
1214 }
1215 mask |= kSmiTagMask;
1216 // This does a single mask that checks for a too high value in a
1217 // conservative way and for a non-Smi. It also filters out negative
1218 // numbers, unfortunately, but since this code is inline we prefer
1219 // brevity to comprehensiveness.
1220 __ tst(tos, Operand(mask));
1221 deferred->Branch(ne);
1222 MultiplyByKnownInt(masm_, tos, tos, int_value);
1223 deferred->BindExit();
1224 frame_->EmitPush(tos);
1225 break;
1226 }
1227
1228 default:
1229 UNREACHABLE();
1230 break;
1231 }
1232}
1233
1234
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001235void CodeGenerator::Comparison(Condition cc,
1236 Expression* left,
1237 Expression* right,
1238 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001239 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001240
ager@chromium.org357bf652010-04-12 11:30:10 +00001241 if (left != NULL) Load(left);
1242 if (right != NULL) Load(right);
1243
mads.s.ager31e71382008-08-13 09:32:07 +00001244 // sp[0] : y
1245 // sp[1] : x
1246 // result : cc register
1247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 // Strict only makes sense for equality comparisons.
1249 ASSERT(!strict || cc == eq);
1250
ager@chromium.org357bf652010-04-12 11:30:10 +00001251 Register lhs;
1252 Register rhs;
1253
1254 // We load the top two stack positions into registers chosen by the virtual
1255 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001256 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1257 if (cc == gt || cc == le) {
1258 cc = ReverseCondition(cc);
ager@chromium.org357bf652010-04-12 11:30:10 +00001259 lhs = frame_->PopToRegister();
1260 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001261 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001262 rhs = frame_->PopToRegister();
1263 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001264 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001265
1266 ASSERT(rhs.is(r0) || rhs.is(r1));
1267 ASSERT(lhs.is(r0) || lhs.is(r1));
1268
1269 // Now we have the two sides in r0 and r1. We flush any other registers
1270 // because the stub doesn't know about register allocation.
1271 frame_->SpillAll();
1272 Register scratch = VirtualFrame::scratch0();
1273 __ orr(scratch, lhs, Operand(rhs));
1274 __ tst(scratch, Operand(kSmiTagMask));
1275 JumpTarget smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001276 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001278 // Perform non-smi comparison by stub.
1279 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1280 // We call with 0 args because there are 0 on the stack.
ager@chromium.org357bf652010-04-12 11:30:10 +00001281 if (!rhs.is(r0)) {
1282 __ Swap(rhs, lhs, ip);
1283 }
1284
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001285 CompareStub stub(cc, strict);
1286 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001287 __ cmp(r0, Operand(0));
ager@chromium.org357bf652010-04-12 11:30:10 +00001288 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001291 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001292 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001293 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001295 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 cc_reg_ = cc;
1297}
1298
1299
mads.s.ager31e71382008-08-13 09:32:07 +00001300// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001301void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001302 CallFunctionFlags flags,
1303 int position) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001304 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 int arg_count = args->length();
1307 for (int i = 0; i < arg_count; i++) {
1308 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
kasper.lund7276f142008-07-30 08:49:36 +00001311 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001312 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
kasper.lund7276f142008-07-30 08:49:36 +00001314 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001315 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001316 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001317 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318
1319 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001320 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322}
1323
1324
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001325void CodeGenerator::CallApplyLazy(Expression* applicand,
1326 Expression* receiver,
1327 VariableProxy* arguments,
1328 int position) {
1329 // An optimized implementation of expressions of the form
1330 // x.apply(y, arguments).
1331 // If the arguments object of the scope has not been allocated,
1332 // and x.apply is Function.prototype.apply, this optimization
1333 // just copies y and the arguments of the current function on the
1334 // stack, as receiver and arguments, and calls x.
1335 // In the implementation comments, we call x the applicand
1336 // and y the receiver.
1337 VirtualFrame::SpilledScope spilled_scope(frame_);
1338
1339 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1340 ASSERT(arguments->IsArguments());
1341
1342 // Load applicand.apply onto the stack. This will usually
1343 // give us a megamorphic load site. Not super, but it works.
1344 LoadAndSpill(applicand);
1345 Handle<String> name = Factory::LookupAsciiSymbol("apply");
ager@chromium.orgac091b72010-05-05 07:34:42 +00001346 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001347 frame_->EmitPush(r0);
1348
1349 // Load the receiver and the existing arguments object onto the
1350 // expression stack. Avoid allocating the arguments object here.
1351 LoadAndSpill(receiver);
1352 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1353
1354 // Emit the source position information after having loaded the
1355 // receiver and the arguments.
1356 CodeForSourcePosition(position);
1357 // Contents of the stack at this point:
1358 // sp[0]: arguments object of the current function or the hole.
1359 // sp[1]: receiver
1360 // sp[2]: applicand.apply
1361 // sp[3]: applicand.
1362
1363 // Check if the arguments object has been lazily allocated
1364 // already. If so, just use that instead of copying the arguments
1365 // from the stack. This also deals with cases where a local variable
1366 // named 'arguments' has been introduced.
1367 __ ldr(r0, MemOperand(sp, 0));
1368
1369 Label slow, done;
1370 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1371 __ cmp(ip, r0);
1372 __ b(ne, &slow);
1373
1374 Label build_args;
1375 // Get rid of the arguments object probe.
1376 frame_->Drop();
1377 // Stack now has 3 elements on it.
1378 // Contents of stack at this point:
1379 // sp[0]: receiver
1380 // sp[1]: applicand.apply
1381 // sp[2]: applicand.
1382
1383 // Check that the receiver really is a JavaScript object.
1384 __ ldr(r0, MemOperand(sp, 0));
1385 __ BranchOnSmi(r0, &build_args);
1386 // We allow all JSObjects including JSFunctions. As long as
1387 // JS_FUNCTION_TYPE is the last instance type and it is right
1388 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1389 // bound.
1390 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1391 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1392 __ CompareObjectType(r0, r1, r2, FIRST_JS_OBJECT_TYPE);
1393 __ b(lt, &build_args);
1394
1395 // Check that applicand.apply is Function.prototype.apply.
1396 __ ldr(r0, MemOperand(sp, kPointerSize));
1397 __ BranchOnSmi(r0, &build_args);
1398 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1399 __ b(ne, &build_args);
1400 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
1401 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
1402 __ ldr(r1, FieldMemOperand(r0, SharedFunctionInfo::kCodeOffset));
1403 __ cmp(r1, Operand(apply_code));
1404 __ b(ne, &build_args);
1405
1406 // Check that applicand is a function.
1407 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1408 __ BranchOnSmi(r1, &build_args);
1409 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1410 __ b(ne, &build_args);
1411
1412 // Copy the arguments to this function possibly from the
1413 // adaptor frame below it.
1414 Label invoke, adapted;
1415 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1416 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1417 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1418 __ b(eq, &adapted);
1419
1420 // No arguments adaptor frame. Copy fixed number of arguments.
1421 __ mov(r0, Operand(scope()->num_parameters()));
1422 for (int i = 0; i < scope()->num_parameters(); i++) {
1423 __ ldr(r2, frame_->ParameterAt(i));
1424 __ push(r2);
1425 }
1426 __ jmp(&invoke);
1427
1428 // Arguments adaptor frame present. Copy arguments from there, but
1429 // avoid copying too many arguments to avoid stack overflows.
1430 __ bind(&adapted);
1431 static const uint32_t kArgumentsLimit = 1 * KB;
1432 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1433 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1434 __ mov(r3, r0);
1435 __ cmp(r0, Operand(kArgumentsLimit));
1436 __ b(gt, &build_args);
1437
1438 // Loop through the arguments pushing them onto the execution
1439 // stack. We don't inform the virtual frame of the push, so we don't
1440 // have to worry about getting rid of the elements from the virtual
1441 // frame.
1442 Label loop;
1443 // r3 is a small non-negative integer, due to the test above.
1444 __ cmp(r3, Operand(0));
1445 __ b(eq, &invoke);
1446 // Compute the address of the first argument.
1447 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1448 __ add(r2, r2, Operand(kPointerSize));
1449 __ bind(&loop);
1450 // Post-decrement argument address by kPointerSize on each iteration.
1451 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1452 __ push(r4);
1453 __ sub(r3, r3, Operand(1), SetCC);
1454 __ b(gt, &loop);
1455
1456 // Invoke the function.
1457 __ bind(&invoke);
1458 ParameterCount actual(r0);
1459 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1460 // Drop applicand.apply and applicand from the stack, and push
1461 // the result of the function call, but leave the spilled frame
1462 // unchanged, with 3 elements, so it is correct when we compile the
1463 // slow-case code.
1464 __ add(sp, sp, Operand(2 * kPointerSize));
1465 __ push(r0);
1466 // Stack now has 1 element:
1467 // sp[0]: result
1468 __ jmp(&done);
1469
1470 // Slow-case: Allocate the arguments object since we know it isn't
1471 // there, and fall-through to the slow-case where we call
1472 // applicand.apply.
1473 __ bind(&build_args);
1474 // Stack now has 3 elements, because we have jumped from where:
1475 // sp[0]: receiver
1476 // sp[1]: applicand.apply
1477 // sp[2]: applicand.
1478 StoreArgumentsObject(false);
1479
1480 // Stack and frame now have 4 elements.
1481 __ bind(&slow);
1482
1483 // Generic computation of x.apply(y, args) with no special optimization.
1484 // Flip applicand.apply and applicand on the stack, so
1485 // applicand looks like the receiver of the applicand.apply call.
1486 // Then process it as a normal function call.
1487 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1488 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1489 __ str(r0, MemOperand(sp, 2 * kPointerSize));
1490 __ str(r1, MemOperand(sp, 3 * kPointerSize));
1491
1492 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1493 frame_->CallStub(&call_function, 3);
1494 // The function and its two arguments have been dropped.
1495 frame_->Drop(); // Drop the receiver as well.
1496 frame_->EmitPush(r0);
1497 // Stack now has 1 element:
1498 // sp[0]: result
1499 __ bind(&done);
1500
1501 // Restore the context register after a call.
1502 __ ldr(cp, frame_->Context());
1503}
1504
1505
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001506void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001507 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 ASSERT(has_cc());
1509 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001510 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 cc_reg_ = al;
1512}
1513
1514
ager@chromium.org7c537e22008-10-16 08:43:32 +00001515void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001516 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001517 Comment cmnt(masm_, "[ check stack");
1518 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1519 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1520 // the implicit 8 byte offset that always applies to operations with pc and
1521 // gives a return address 12 bytes down.
1522 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1523 masm_->cmp(sp, Operand(ip));
1524 StackCheckStub stub;
1525 // Call the stub if lower.
1526 masm_->mov(pc,
1527 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1528 RelocInfo::CODE_TARGET),
1529 LeaveCC,
1530 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531}
1532
1533
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001534void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1535#ifdef DEBUG
1536 int original_height = frame_->height();
1537#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001538 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001539 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1540 VisitAndSpill(statements->at(i));
1541 }
1542 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1543}
1544
1545
ager@chromium.org7c537e22008-10-16 08:43:32 +00001546void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001547#ifdef DEBUG
1548 int original_height = frame_->height();
1549#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001550 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001552 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001553 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001554 VisitStatementsAndSpill(node->statements());
1555 if (node->break_target()->is_linked()) {
1556 node->break_target()->Bind();
1557 }
1558 node->break_target()->Unuse();
1559 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560}
1561
1562
ager@chromium.org7c537e22008-10-16 08:43:32 +00001563void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001564 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001565 frame_->EmitPush(Operand(pairs));
1566 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1567
1568 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001569 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001570 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571}
1572
1573
ager@chromium.org7c537e22008-10-16 08:43:32 +00001574void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001575#ifdef DEBUG
1576 int original_height = frame_->height();
1577#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 Comment cmnt(masm_, "[ Declaration");
1579 Variable* var = node->proxy()->var();
1580 ASSERT(var != NULL); // must have been resolved
1581 Slot* slot = var->slot();
1582
1583 // If it was not possible to allocate the variable at compile time,
1584 // we need to "declare" it at runtime to make sure it actually
1585 // exists in the local context.
1586 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1587 // Variables with a "LOOKUP" slot were introduced as non-locals
1588 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001589 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001591 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001592 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 // Declaration nodes are always declared in only two modes.
1594 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1595 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001596 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 // Push initial value, if any.
1598 // Note: For variables we must not push an initial value (such as
1599 // 'undefined') because we may have a (legal) redeclaration and we
1600 // must not destroy the current value.
1601 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001602 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001604 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001606 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001608
1609 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001611 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001612
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001613 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 return;
1615 }
1616
1617 ASSERT(!var->is_global());
1618
1619 // If we have a function or a constant, we need to initialize the variable.
1620 Expression* val = NULL;
1621 if (node->mode() == Variable::CONST) {
1622 val = new Literal(Factory::the_hole_value());
1623 } else {
1624 val = node->fun(); // NULL if we don't have a function
1625 }
1626
1627 if (val != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001628 // Set initial value.
1629 Reference target(this, node->proxy());
1630 Load(val);
1631 target.SetValue(NOT_CONST_INIT);
1632
iposva@chromium.org245aa852009-02-10 00:49:54 +00001633 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001634 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001636 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637}
1638
1639
ager@chromium.org7c537e22008-10-16 08:43:32 +00001640void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001641#ifdef DEBUG
1642 int original_height = frame_->height();
1643#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001644 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001646 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647 Expression* expression = node->expression();
1648 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001649 LoadAndSpill(expression);
1650 frame_->Drop();
1651 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652}
1653
1654
ager@chromium.org7c537e22008-10-16 08:43:32 +00001655void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001656#ifdef DEBUG
1657 int original_height = frame_->height();
1658#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001659 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001661 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001663 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664}
1665
1666
ager@chromium.org7c537e22008-10-16 08:43:32 +00001667void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001668#ifdef DEBUG
1669 int original_height = frame_->height();
1670#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001671 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001673 // Generate different code depending on which parts of the if statement
1674 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 bool has_then_stm = node->HasThenStatement();
1676 bool has_else_stm = node->HasElseStatement();
1677
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001678 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001680 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001682 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001683 JumpTarget then;
1684 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001686 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 if (frame_ != NULL) {
1688 Branch(false, &else_);
1689 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001691 if (frame_ != NULL || then.is_linked()) {
1692 then.Bind();
1693 VisitAndSpill(node->then_statement());
1694 }
1695 if (frame_ != NULL) {
1696 exit.Jump();
1697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001699 if (else_.is_linked()) {
1700 else_.Bind();
1701 VisitAndSpill(node->else_statement());
1702 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703
1704 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001705 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001707 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001709 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001710 if (frame_ != NULL) {
1711 Branch(false, &exit);
1712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001714 if (frame_ != NULL || then.is_linked()) {
1715 then.Bind();
1716 VisitAndSpill(node->then_statement());
1717 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718
1719 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001720 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001722 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001724 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001725 if (frame_ != NULL) {
1726 Branch(true, &exit);
1727 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001729 if (frame_ != NULL || else_.is_linked()) {
1730 else_.Bind();
1731 VisitAndSpill(node->else_statement());
1732 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733
1734 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001735 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 ASSERT(!has_then_stm && !has_else_stm);
1737 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001738 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 if (frame_ != NULL) {
1740 if (has_cc()) {
1741 cc_reg_ = al;
1742 } else {
1743 frame_->Drop();
1744 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 }
1746 }
1747
1748 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001749 if (exit.is_linked()) {
1750 exit.Bind();
1751 }
1752 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753}
1754
1755
ager@chromium.org7c537e22008-10-16 08:43:32 +00001756void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001757 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001759 CodeForStatementPosition(node);
1760 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761}
1762
1763
ager@chromium.org7c537e22008-10-16 08:43:32 +00001764void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001765 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 CodeForStatementPosition(node);
1768 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769}
1770
1771
ager@chromium.org7c537e22008-10-16 08:43:32 +00001772void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001773 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001775
ager@chromium.org4af710e2009-09-15 12:20:11 +00001776 CodeForStatementPosition(node);
1777 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 frame_->EmitPop(r0);
1780 function_return_.Jump();
1781 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 // Pop the result from the frame and prepare the frame for
1783 // returning thus making it easier to merge.
1784 frame_->EmitPop(r0);
1785 frame_->PrepareForReturn();
1786
1787 function_return_.Jump();
1788 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789}
1790
1791
ager@chromium.org7c537e22008-10-16 08:43:32 +00001792void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793#ifdef DEBUG
1794 int original_height = frame_->height();
1795#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001796 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001798 CodeForStatementPosition(node);
1799 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001800 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001802 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001804 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001805#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001806 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001807 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001808 verified_true.Branch(eq);
1809 __ stop("PushContext: r0 is expected to be the same as cp");
1810 verified_true.Bind();
1811#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001813 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815}
1816
1817
ager@chromium.org7c537e22008-10-16 08:43:32 +00001818void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819#ifdef DEBUG
1820 int original_height = frame_->height();
1821#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001822 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 // Pop context.
1826 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1827 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001828 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830}
1831
1832
ager@chromium.org7c537e22008-10-16 08:43:32 +00001833void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001834#ifdef DEBUG
1835 int original_height = frame_->height();
1836#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001837 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001840 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001842 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001843
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001844 JumpTarget next_test;
1845 JumpTarget fall_through;
1846 JumpTarget default_entry;
1847 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 ZoneList<CaseClause*>* cases = node->cases();
1849 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001850 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851
1852 for (int i = 0; i < length; i++) {
1853 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 // Remember the default clause and compile it at the end.
1856 default_clause = clause;
1857 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 }
1859
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 Comment cmnt(masm_, "[ Case clause");
1861 // Compile the test.
1862 next_test.Bind();
1863 next_test.Unuse();
1864 // Duplicate TOS.
1865 __ ldr(r0, frame_->Top());
1866 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001867 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001869
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 // Before entering the body from the test, remove the switch value from
1871 // the stack.
1872 frame_->Drop();
1873
1874 // Label the body so that fall through is enabled.
1875 if (i > 0 && cases->at(i - 1)->is_default()) {
1876 default_exit.Bind();
1877 } else {
1878 fall_through.Bind();
1879 fall_through.Unuse();
1880 }
1881 VisitStatementsAndSpill(clause->statements());
1882
1883 // If control flow can fall through from the body, jump to the next body
1884 // or the end of the statement.
1885 if (frame_ != NULL) {
1886 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1887 default_entry.Jump();
1888 } else {
1889 fall_through.Jump();
1890 }
1891 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 }
1893
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 // The final "test" removes the switch value.
1895 next_test.Bind();
1896 frame_->Drop();
1897
1898 // If there is a default clause, compile it.
1899 if (default_clause != NULL) {
1900 Comment cmnt(masm_, "[ Default clause");
1901 default_entry.Bind();
1902 VisitStatementsAndSpill(default_clause->statements());
1903 // If control flow can fall out of the default and there is a case after
1904 // it, jup to that case's body.
1905 if (frame_ != NULL && default_exit.is_bound()) {
1906 default_exit.Jump();
1907 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001908 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910 if (fall_through.is_linked()) {
1911 fall_through.Bind();
1912 }
1913
1914 if (node->break_target()->is_linked()) {
1915 node->break_target()->Bind();
1916 }
1917 node->break_target()->Unuse();
1918 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919}
1920
1921
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001922void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923#ifdef DEBUG
1924 int original_height = frame_->height();
1925#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001926 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001927 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001929 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001930 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001931 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001933 // Label the top of the loop for the backward CFG edge. If the test
1934 // is always true we can use the continue target, and if the test is
1935 // always false there is no need.
1936 ConditionAnalysis info = AnalyzeCondition(node->cond());
1937 switch (info) {
1938 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001939 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001940 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001941 break;
1942 case ALWAYS_FALSE:
1943 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1944 break;
1945 case DONT_KNOW:
1946 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1947 body.Bind();
1948 break;
1949 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001950
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001951 CheckStack(); // TODO(1222600): ignore if body contains calls.
1952 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001954 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001955 switch (info) {
1956 case ALWAYS_TRUE:
1957 // If control can fall off the end of the body, jump back to the
1958 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001960 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001963 case ALWAYS_FALSE:
1964 // If we have a continue in the body, we only have to bind its
1965 // jump target.
1966 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001969 break;
1970 case DONT_KNOW:
1971 // We have to compile the test expression if it can be reached by
1972 // control flow falling out of the body or via continue.
1973 if (node->continue_target()->is_linked()) {
1974 node->continue_target()->Bind();
1975 }
1976 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001977 Comment cmnt(masm_, "[ DoWhileCondition");
1978 CodeForDoWhileConditionPosition(node);
1979 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001980 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001981 // A invalid frame here indicates that control did not
1982 // fall out of the test expression.
1983 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001984 }
1985 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 break;
1987 }
1988
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989 if (node->break_target()->is_linked()) {
1990 node->break_target()->Bind();
1991 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001992 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001993 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1994}
1995
1996
1997void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1998#ifdef DEBUG
1999 int original_height = frame_->height();
2000#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002001 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002002 Comment cmnt(masm_, "[ WhileStatement");
2003 CodeForStatementPosition(node);
2004
2005 // If the test is never true and has no side effects there is no need
2006 // to compile the test or body.
2007 ConditionAnalysis info = AnalyzeCondition(node->cond());
2008 if (info == ALWAYS_FALSE) return;
2009
2010 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002011 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002012
2013 // Label the top of the loop with the continue target for the backward
2014 // CFG edge.
2015 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2016 node->continue_target()->Bind();
2017
2018 if (info == DONT_KNOW) {
2019 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002020 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002021 if (has_valid_frame()) {
2022 // A NULL frame indicates that control did not fall out of the
2023 // test expression.
2024 Branch(false, node->break_target());
2025 }
2026 if (has_valid_frame() || body.is_linked()) {
2027 body.Bind();
2028 }
2029 }
2030
2031 if (has_valid_frame()) {
2032 CheckStack(); // TODO(1222600): ignore if body contains calls.
2033 VisitAndSpill(node->body());
2034
2035 // If control flow can fall out of the body, jump back to the top.
2036 if (has_valid_frame()) {
2037 node->continue_target()->Jump();
2038 }
2039 }
2040 if (node->break_target()->is_linked()) {
2041 node->break_target()->Bind();
2042 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002043 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002044 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2045}
2046
2047
2048void CodeGenerator::VisitForStatement(ForStatement* node) {
2049#ifdef DEBUG
2050 int original_height = frame_->height();
2051#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002052 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002053 Comment cmnt(masm_, "[ ForStatement");
2054 CodeForStatementPosition(node);
2055 if (node->init() != NULL) {
2056 VisitAndSpill(node->init());
2057 }
2058
2059 // If the test is never true there is no need to compile the test or
2060 // body.
2061 ConditionAnalysis info = AnalyzeCondition(node->cond());
2062 if (info == ALWAYS_FALSE) return;
2063
2064 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002065 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002066
2067 // If there is no update statement, label the top of the loop with the
2068 // continue target, otherwise with the loop target.
2069 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2070 if (node->next() == NULL) {
2071 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2072 node->continue_target()->Bind();
2073 } else {
2074 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2075 loop.Bind();
2076 }
2077
2078 // If the test is always true, there is no need to compile it.
2079 if (info == DONT_KNOW) {
2080 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002081 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002082 if (has_valid_frame()) {
2083 Branch(false, node->break_target());
2084 }
2085 if (has_valid_frame() || body.is_linked()) {
2086 body.Bind();
2087 }
2088 }
2089
2090 if (has_valid_frame()) {
2091 CheckStack(); // TODO(1222600): ignore if body contains calls.
2092 VisitAndSpill(node->body());
2093
2094 if (node->next() == NULL) {
2095 // If there is no update statement and control flow can fall out
2096 // of the loop, jump directly to the continue label.
2097 if (has_valid_frame()) {
2098 node->continue_target()->Jump();
2099 }
2100 } else {
2101 // If there is an update statement and control flow can reach it
2102 // via falling out of the body of the loop or continuing, we
2103 // compile the update statement.
2104 if (node->continue_target()->is_linked()) {
2105 node->continue_target()->Bind();
2106 }
2107 if (has_valid_frame()) {
2108 // Record source position of the statement as this code which is
2109 // after the code for the body actually belongs to the loop
2110 // statement and not the body.
2111 CodeForStatementPosition(node);
2112 VisitAndSpill(node->next());
2113 loop.Jump();
2114 }
2115 }
2116 }
2117 if (node->break_target()->is_linked()) {
2118 node->break_target()->Bind();
2119 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002120 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122}
2123
2124
ager@chromium.org7c537e22008-10-16 08:43:32 +00002125void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126#ifdef DEBUG
2127 int original_height = frame_->height();
2128#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002129 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002133 JumpTarget primitive;
2134 JumpTarget jsobject;
2135 JumpTarget fixed_array;
2136 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2137 JumpTarget end_del_check;
2138 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139
2140 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002141 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142
2143 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2144 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002146 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2147 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002149 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2150 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152
2153 // Stack layout in body:
2154 // [iteration counter (Smi)]
2155 // [length of array]
2156 // [FixedArray]
2157 // [Map or 0]
2158 // [Object]
2159
2160 // Check if enumerable is already a JSObject
2161 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002162 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002163 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 primitive.Bind();
2167 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002168 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002172 // r0: value to be iterated over
2173 frame_->EmitPush(r0); // Push the object being iterated over.
2174
2175 // Check cache validity in generated code. This is a fast case for
2176 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2177 // guarantee cache validity, call the runtime system to check cache
2178 // validity or get the property names in a fixed array.
2179 JumpTarget call_runtime;
2180 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2181 JumpTarget check_prototype;
2182 JumpTarget use_cache;
2183 __ mov(r1, Operand(r0));
2184 loop.Bind();
2185 // Check that there are no elements.
2186 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2187 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2188 __ cmp(r2, r4);
2189 call_runtime.Branch(ne);
2190 // Check that instance descriptors are not empty so that we can
2191 // check for an enum cache. Leave the map in r3 for the subsequent
2192 // prototype load.
2193 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2194 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2195 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2196 __ cmp(r2, ip);
2197 call_runtime.Branch(eq);
2198 // Check that there in an enum cache in the non-empty instance
2199 // descriptors. This is the case if the next enumeration index
2200 // field does not contain a smi.
2201 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2202 __ tst(r2, Operand(kSmiTagMask));
2203 call_runtime.Branch(eq);
2204 // For all objects but the receiver, check that the cache is empty.
2205 // r4: empty fixed array root.
2206 __ cmp(r1, r0);
2207 check_prototype.Branch(eq);
2208 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2209 __ cmp(r2, r4);
2210 call_runtime.Branch(ne);
2211 check_prototype.Bind();
2212 // Load the prototype from the map and loop if non-null.
2213 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2214 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2215 __ cmp(r1, ip);
2216 loop.Branch(ne);
2217 // The enum cache is valid. Load the map of the object being
2218 // iterated over and use the cache for the iteration.
2219 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2220 use_cache.Jump();
2221
2222 call_runtime.Bind();
2223 // Call the runtime to get the property names for the object.
2224 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002227 // If we got a map from the runtime call, we can do a fast
2228 // modification check. Otherwise, we got a fixed array, and we have
2229 // to do a slow check.
2230 // r0: map or fixed array (result from call to
2231 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 __ mov(r2, Operand(r0));
2233 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002234 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2235 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002236 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002238 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002240 // r0: map (either the result from a call to
2241 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2242 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 __ mov(r1, Operand(r0));
2244 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2245 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2246 __ ldr(r2,
2247 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2248
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 frame_->EmitPush(r0); // map
2250 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002251 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002253 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002254 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002255 frame_->EmitPush(r0);
2256 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002258 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260 frame_->EmitPush(r1); // insert 0 in place of Map
2261 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262
2263 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002264 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002267 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002268 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269
2270 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002272 // sp[0] : index
2273 // sp[1] : array/enum cache length
2274 // sp[2] : array or enum cache
2275 // sp[3] : 0 or map
2276 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 // Grab the current frame's height for the break and continue
2278 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002279 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2280 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002282 __ ldr(r0, frame_->ElementAt(0)); // load the current count
2283 __ ldr(r1, frame_->ElementAt(1)); // load the length
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002284 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002285 node->break_target()->Branch(hs);
2286
2287 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2292 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2293
2294 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 // Check if this (still) matches the map of the enumerable.
2297 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002298 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2300 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302
2303 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2305 frame_->EmitPush(r0);
2306 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002307 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002308 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309
2310 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002311 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2312 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002313 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 end_del_check.Bind();
2316 // Store the entry in the 'each' expression and take another spin in the
2317 // loop. r3: i'th entry of the enum cache (or string there of)
2318 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 { Reference each(this, node->each());
2320 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002321 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002322 __ ldr(r0, frame_->ElementAt(each.size()));
2323 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002324 each.SetValue(NOT_CONST_INIT);
2325 frame_->Drop(2);
2326 } else {
2327 // If the reference was to a slot we rely on the convenient property
2328 // that it doesn't matter whether a value (eg, r3 pushed above) is
2329 // right on top of or right underneath a zero-sized reference.
2330 each.SetValue(NOT_CONST_INIT);
2331 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 }
2334 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 VisitAndSpill(node->body());
2338
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002339 // Next. Reestablish a spilled frame in case we are coming here via
2340 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002342 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 frame_->EmitPop(r0);
2344 __ add(r0, r0, Operand(Smi::FromInt(1)));
2345 frame_->EmitPush(r0);
2346 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002348 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2349 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002350 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002351 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352
2353 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002354 exit.Bind();
2355 node->continue_target()->Unuse();
2356 node->break_target()->Unuse();
2357 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358}
2359
2360
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002361void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362#ifdef DEBUG
2363 int original_height = frame_->height();
2364#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002365 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002366 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002367 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002369 JumpTarget try_block;
2370 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002372 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002374 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375
2376 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002377 Variable* catch_var = node->catch_var()->var();
2378 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2379 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380
2381 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002382 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002384 VisitStatementsAndSpill(node->catch_block()->statements());
2385 if (frame_ != NULL) {
2386 exit.Jump();
2387 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388
2389
2390 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002391 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002393 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2394 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002396 // Shadow the labels for all escapes from the try block, including
2397 // returns. During shadowing, the original label is hidden as the
2398 // LabelShadow and operations on the original actually affect the
2399 // shadowing label.
2400 //
2401 // We should probably try to unify the escaping labels and the return
2402 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002403 int nof_escapes = node->escaping_targets()->length();
2404 List<ShadowTarget*> shadows(1 + nof_escapes);
2405
2406 // Add the shadow target for the function return.
2407 static const int kReturnShadowIndex = 0;
2408 shadows.Add(new ShadowTarget(&function_return_));
2409 bool function_return_was_shadowed = function_return_is_shadowed_;
2410 function_return_is_shadowed_ = true;
2411 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2412
2413 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002415 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 }
2417
2418 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002419 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420
2421 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002422 // After shadowing stops, the original labels are unshadowed and the
2423 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002424 bool has_unlinks = false;
2425 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002427 has_unlinks = has_unlinks || shadows[i]->is_linked();
2428 }
2429 function_return_is_shadowed_ = function_return_was_shadowed;
2430
2431 // Get an external reference to the handler address.
2432 ExternalReference handler_address(Top::k_handler_address);
2433
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002434 // If we can fall off the end of the try block, unlink from try chain.
2435 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002436 // The next handler address is on top of the frame. Unlink from
2437 // the handler list and drop the rest of this handler from the
2438 // frame.
2439 ASSERT(StackHandlerConstants::kNextOffset == 0);
2440 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002441 __ mov(r3, Operand(handler_address));
2442 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002443 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 if (has_unlinks) {
2445 exit.Jump();
2446 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 }
2448
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002449 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 // jumped to. Deallocate each shadow target.
2451 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002453 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002454 shadows[i]->Bind();
2455 // Because we can be jumping here (to spilled code) from unspilled
2456 // code, we need to reestablish a spilled frame at this block.
2457 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 // Reload sp from the top handler, because some statements that we
2460 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002463 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002465 ASSERT(StackHandlerConstants::kNextOffset == 0);
2466 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002468 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2471 frame_->PrepareForReturn();
2472 }
2473 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 }
2475 }
2476
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 exit.Bind();
2478 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479}
2480
2481
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002482void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002483#ifdef DEBUG
2484 int original_height = frame_->height();
2485#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002486 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002487 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489
2490 // State: Used to keep track of reason for entering the finally
2491 // block. Should probably be extended to hold information for
2492 // break/continue from within the try block.
2493 enum { FALLING, THROWING, JUMPING };
2494
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002495 JumpTarget try_block;
2496 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002500 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501 // In case of thrown exceptions, this is where we continue.
2502 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002503 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504
2505 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002506 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002508 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2509 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002511 // Shadow the labels for all escapes from the try block, including
2512 // returns. Shadowing hides the original label as the LabelShadow and
2513 // operations on the original actually affect the shadowing label.
2514 //
2515 // We should probably try to unify the escaping labels and the return
2516 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002517 int nof_escapes = node->escaping_targets()->length();
2518 List<ShadowTarget*> shadows(1 + nof_escapes);
2519
2520 // Add the shadow target for the function return.
2521 static const int kReturnShadowIndex = 0;
2522 shadows.Add(new ShadowTarget(&function_return_));
2523 bool function_return_was_shadowed = function_return_is_shadowed_;
2524 function_return_is_shadowed_ = true;
2525 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2526
2527 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002529 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530 }
2531
2532 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002535 // Stop the introduced shadowing and count the number of required unlinks.
2536 // After shadowing stops, the original labels are unshadowed and the
2537 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 shadows[i]->StopShadowing();
2541 if (shadows[i]->is_linked()) nof_unlinks++;
2542 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002545 // Get an external reference to the handler address.
2546 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 // If we can fall off the end of the try block, unlink from the try
2549 // chain and set the state on the frame to FALLING.
2550 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002551 // The next handler address is on top of the frame.
2552 ASSERT(StackHandlerConstants::kNextOffset == 0);
2553 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 __ mov(r3, Operand(handler_address));
2555 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002556 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002557
2558 // Fake a top of stack value (unneeded when FALLING) and set the
2559 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002560 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 frame_->EmitPush(r0);
2562 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2563 if (nof_unlinks > 0) {
2564 finally_block.Jump();
2565 }
2566 }
2567
2568 // Generate code to unlink and set the state for the (formerly)
2569 // shadowing targets that have been jumped to.
2570 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 // If we have come from the shadowed return, the return value is
2573 // in (a non-refcounted reference to) r0. We must preserve it
2574 // until it is pushed.
2575 //
2576 // Because we can be jumping here (to spilled code) from
2577 // unspilled code, we need to reestablish a spilled frame at
2578 // this block.
2579 shadows[i]->Bind();
2580 frame_->SpillAll();
2581
2582 // Reload sp from the top handler, because some statements that
2583 // we break from (eg, for...in) may have left stuff on the
2584 // stack.
2585 __ mov(r3, Operand(handler_address));
2586 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002587 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588
2589 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002590 // handler address is currently on top of the frame.
2591 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592 frame_->EmitPop(r1);
2593 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002594 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595
2596 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002597 // If this label shadowed the function return, materialize the
2598 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002600 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002602 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604 }
2605 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002606 if (--nof_unlinks > 0) {
2607 // If this is not the last unlink block, jump around the next.
2608 finally_block.Jump();
2609 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 }
2611 }
2612
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002614 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002616 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002618
2619 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 // and the state - while evaluating the finally block.
2621 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002625 if (has_valid_frame()) {
2626 // Restore state and return value or faked TOS.
2627 frame_->EmitPop(r2);
2628 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 }
2630
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002631 // Generate code to jump to the right destination for all used
2632 // formerly shadowing targets. Deallocate each shadow target.
2633 for (int i = 0; i < shadows.length(); i++) {
2634 if (has_valid_frame() && shadows[i]->is_bound()) {
2635 JumpTarget* original = shadows[i]->other_target();
2636 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2637 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002638 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 skip.Branch(ne);
2640 frame_->PrepareForReturn();
2641 original->Jump();
2642 skip.Bind();
2643 } else {
2644 original->Branch(eq);
2645 }
2646 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002647 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002649 if (has_valid_frame()) {
2650 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002651 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002652 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2653 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655 // Rethrow exception.
2656 frame_->EmitPush(r0);
2657 frame_->CallRuntime(Runtime::kReThrow, 1);
2658
2659 // Done.
2660 exit.Bind();
2661 }
2662 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663}
2664
2665
ager@chromium.org7c537e22008-10-16 08:43:32 +00002666void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667#ifdef DEBUG
2668 int original_height = frame_->height();
2669#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002670 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002672 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002673#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002674 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002675#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002676 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678}
2679
2680
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002681void CodeGenerator::InstantiateFunction(
2682 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002683 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002684 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002685 // Use the fast case closure allocation code that allocates in new
2686 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002687 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002688 FastNewClosureStub stub;
2689 frame_->EmitPush(r0);
2690 frame_->CallStub(&stub, 1);
2691 frame_->EmitPush(r0);
2692 } else {
2693 // Create a new closure.
2694 frame_->EmitPush(cp);
2695 frame_->EmitPush(r0);
2696 frame_->CallRuntime(Runtime::kNewClosure, 2);
2697 frame_->EmitPush(r0);
2698 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699}
2700
2701
ager@chromium.org7c537e22008-10-16 08:43:32 +00002702void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002703#ifdef DEBUG
2704 int original_height = frame_->height();
2705#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002706 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 Comment cmnt(masm_, "[ FunctionLiteral");
2708
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002709 // Build the function info and instantiate it.
2710 Handle<SharedFunctionInfo> function_info =
2711 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002712 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002713 if (HasStackOverflow()) {
2714 ASSERT(frame_->height() == original_height);
2715 return;
2716 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002717 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002718 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719}
2720
2721
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002722void CodeGenerator::VisitSharedFunctionInfoLiteral(
2723 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724#ifdef DEBUG
2725 int original_height = frame_->height();
2726#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002727 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002728 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2729 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002730 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731}
2732
2733
ager@chromium.org7c537e22008-10-16 08:43:32 +00002734void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002735#ifdef DEBUG
2736 int original_height = frame_->height();
2737#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002738 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002740 JumpTarget then;
2741 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002742 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002743 if (has_valid_frame()) {
2744 Branch(false, &else_);
2745 }
2746 if (has_valid_frame() || then.is_linked()) {
2747 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002748 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002749 }
2750 if (else_.is_linked()) {
2751 JumpTarget exit;
2752 if (has_valid_frame()) exit.Jump();
2753 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002754 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002755 if (exit.is_linked()) exit.Bind();
2756 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002757 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758}
2759
2760
ager@chromium.org7c537e22008-10-16 08:43:32 +00002761void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2762 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002763 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002765 // JumpTargets do not yet support merging frames so the frame must be
2766 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002767 JumpTarget slow;
2768 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002769
2770 // Generate fast-case code for variables that might be shadowed by
2771 // eval-introduced variables. Eval is used a lot without
2772 // introducing variables. In those cases, we do not want to
2773 // perform a runtime call for all variables in the scope
2774 // containing the eval.
2775 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002776 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777 // If there was no control flow to slow, we can exit early.
2778 if (!slow.is_linked()) {
2779 frame_->EmitPush(r0);
2780 return;
2781 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002782 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783
2784 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002785
2786 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002787 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002788 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2789 // Only generate the fast case for locals that rewrite to slots.
2790 // This rules out argument loads.
2791 if (potential_slot != NULL) {
2792 __ ldr(r0,
2793 ContextSlotOperandCheckExtensions(potential_slot,
2794 r1,
2795 r2,
2796 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002797 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002798 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2799 __ cmp(r0, ip);
2800 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002801 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002802 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002803 // ContextSlotOperandCheckExtensions so we have to jump around
2804 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002805 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002806 }
2807 }
2808
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002809 slow.Bind();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002810 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002811 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002812 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002813 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814
ager@chromium.org7c537e22008-10-16 08:43:32 +00002815 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002816 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002817 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002818 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002820
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002821 done.Bind();
2822 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002823
2824 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002825 Register scratch = VirtualFrame::scratch0();
2826 frame_->EmitPush(SlotOperand(slot, scratch));
ager@chromium.org7c537e22008-10-16 08:43:32 +00002827 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002828 // Const slots may contain 'the hole' value (the constant hasn't been
2829 // initialized yet) which needs to be converted into the 'undefined'
2830 // value.
2831 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002832 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002833 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002834 __ cmp(scratch, ip);
2835 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
2836 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 }
2838 }
2839}
2840
2841
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002842void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
2843 TypeofState state) {
2844 LoadFromSlot(slot, state);
2845
2846 // Bail out quickly if we're not using lazy arguments allocation.
2847 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
2848
2849 // ... or if the slot isn't a non-parameter arguments slot.
2850 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
2851
2852 VirtualFrame::SpilledScope spilled_scope(frame_);
2853
2854 // Load the loaded value from the stack into r0 but leave it on the
2855 // stack.
2856 __ ldr(r0, MemOperand(sp, 0));
2857
2858 // If the loaded value is the sentinel that indicates that we
2859 // haven't loaded the arguments object yet, we need to do it now.
2860 JumpTarget exit;
2861 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2862 __ cmp(r0, ip);
2863 exit.Branch(ne);
2864 frame_->Drop();
2865 StoreArgumentsObject(false);
2866 exit.Bind();
2867}
2868
2869
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002870void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2871 ASSERT(slot != NULL);
2872 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002873 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002874 ASSERT(slot->var()->is_dynamic());
2875
2876 // For now, just do a runtime call.
2877 frame_->EmitPush(cp);
2878 __ mov(r0, Operand(slot->var()->name()));
2879 frame_->EmitPush(r0);
2880
2881 if (init_state == CONST_INIT) {
2882 // Same as the case for a normal store, but ignores attribute
2883 // (e.g. READ_ONLY) of context slot so that we can initialize
2884 // const properties (introduced via eval("const foo = (some
2885 // expr);")). Also, uses the current function context instead of
2886 // the top context.
2887 //
2888 // Note that we must declare the foo upon entry of eval(), via a
2889 // context slot declaration, but we cannot initialize it at the
2890 // same time, because the const declaration may be at the end of
2891 // the eval code (sigh...) and the const variable may have been
2892 // used before (where its value is 'undefined'). Thus, we can only
2893 // do the initialization when we actually encounter the expression
2894 // and when the expression operands are defined and valid, and
2895 // thus we need the split into 2 operations: declaration of the
2896 // context slot followed by initialization.
2897 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2898 } else {
2899 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2900 }
2901 // Storing a variable must keep the (new) value on the expression
2902 // stack. This is necessary for compiling assignment expressions.
2903 frame_->EmitPush(r0);
2904
2905 } else {
2906 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00002907 Register scratch = VirtualFrame::scratch0();
2908 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002909
ager@chromium.org357bf652010-04-12 11:30:10 +00002910 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002911 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00002912
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002913 if (init_state == CONST_INIT) {
2914 ASSERT(slot->var()->mode() == Variable::CONST);
2915 // Only the first const initialization must be executed (the slot
2916 // still contains 'the hole' value). When the assignment is
2917 // executed, the code is identical to a normal store (see below).
2918 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002919 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002920 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002921 __ cmp(scratch, ip);
2922 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002923 exit.Branch(ne);
2924 }
2925
2926 // We must execute the store. Storing a variable must keep the
2927 // (new) value on the stack. This is necessary for compiling
2928 // assignment expressions.
2929 //
2930 // Note: We will reach here even with slot->var()->mode() ==
2931 // Variable::CONST because of const declarations which will
2932 // initialize consts to 'the hole' value and by doing so, end up
2933 // calling this code. r2 may be loaded with context; used below in
2934 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00002935 Register tos = frame_->Peek();
2936 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002937 if (slot->type() == Slot::CONTEXT) {
2938 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00002939 __ tst(tos, Operand(kSmiTagMask));
2940 // We don't use tos any more after here.
2941 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002942 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00002943 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002944 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2945 __ mov(r3, Operand(offset));
ager@chromium.org357bf652010-04-12 11:30:10 +00002946 // r1 could be identical with tos, but that doesn't matter.
2947 __ RecordWrite(scratch, r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002948 }
2949 // If we definitely did not jump over the assignment, we do not need
2950 // to bind the exit label. Doing so can defeat peephole
2951 // optimization.
2952 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002953 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002954 exit.Bind();
2955 }
2956 }
2957}
2958
2959
ager@chromium.org381abbb2009-02-25 13:23:22 +00002960void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2961 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002962 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002963 // Check that no extension objects have been created by calls to
2964 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002965 Register tmp = frame_->scratch0();
2966 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002967 Register context = cp;
2968 Scope* s = scope();
2969 while (s != NULL) {
2970 if (s->num_heap_slots() > 0) {
2971 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002972 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002973 // Check that extension is NULL.
2974 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2975 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002976 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002977 }
2978 // Load next context in chain.
2979 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2980 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2981 context = tmp;
2982 }
2983 // If no outer scope calls eval, we do not need to check more
2984 // context extensions.
2985 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2986 s = s->outer_scope();
2987 }
2988
2989 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002990 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002991 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00002992 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002993 __ bind(&next);
2994 // Terminate at global context.
2995 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002996 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2997 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002998 __ b(eq, &fast);
2999 // Check that extension is NULL.
3000 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3001 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003002 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003003 // Load next context in chain.
3004 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3005 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3006 __ b(&next);
3007 __ bind(&fast);
3008 }
3009
ager@chromium.org381abbb2009-02-25 13:23:22 +00003010 // Load the global object.
3011 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003012 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003013 frame_->CallLoadIC(slot->var()->name(),
3014 typeof_state == INSIDE_TYPEOF
3015 ? RelocInfo::CODE_TARGET
3016 : RelocInfo::CODE_TARGET_CONTEXT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003017 // Drop the global object. The result is in r0.
3018 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003019}
3020
3021
ager@chromium.org7c537e22008-10-16 08:43:32 +00003022void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003023#ifdef DEBUG
3024 int original_height = frame_->height();
3025#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003026 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003027 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003028 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003029}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030
ager@chromium.org7c537e22008-10-16 08:43:32 +00003031
3032void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003033#ifdef DEBUG
3034 int original_height = frame_->height();
3035#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003036 Comment cmnt(masm_, "[ VariableProxy");
3037
3038 Variable* var = node->var();
3039 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003040 if (expr != NULL) {
3041 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003043 ASSERT(var->is_global());
3044 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003045 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003047 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048}
3049
3050
ager@chromium.org7c537e22008-10-16 08:43:32 +00003051void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052#ifdef DEBUG
3053 int original_height = frame_->height();
3054#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003055 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003056 Register reg = frame_->GetTOSRegister();
3057 __ mov(reg, Operand(node->handle()));
3058 frame_->EmitPush(reg);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003059 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003060}
3061
3062
ager@chromium.org7c537e22008-10-16 08:43:32 +00003063void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003064#ifdef DEBUG
3065 int original_height = frame_->height();
3066#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003067 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 Comment cmnt(masm_, "[ RexExp Literal");
3069
3070 // Retrieve the literal array and check the allocated entry.
3071
3072 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003073 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074
3075 // Load the literals array of the function.
3076 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3077
3078 // Load the literal at the ast saved index.
3079 int literal_offset =
3080 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3081 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3082
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003083 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003084 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3085 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003086 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087
3088 // If the entry is undefined we call the runtime system to computed
3089 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003091 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003093 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003094 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003095 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 frame_->EmitPush(r0);
3097 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003098 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003100 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003102 frame_->EmitPush(r2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003103 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003104}
3105
3106
ager@chromium.org7c537e22008-10-16 08:43:32 +00003107void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003108#ifdef DEBUG
3109 int original_height = frame_->height();
3110#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003111 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 Comment cmnt(masm_, "[ ObjectLiteral");
3113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003115 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003116 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003117 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003118 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003119 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003120 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003121 __ mov(r1, Operand(node->constant_properties()));
3122 // Should the object literal have fast elements?
3123 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3124 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003125 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003126 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003127 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003128 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003129 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003130 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003131 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003132 // At the start of each iteration, the top of stack contains
3133 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134 ObjectLiteral::Property* property = node->properties()->at(i);
3135 Literal* key = property->key();
3136 Expression* value = property->value();
3137 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003138 case ObjectLiteral::Property::CONSTANT:
3139 break;
3140 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3141 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3142 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003143 case ObjectLiteral::Property::COMPUTED:
3144 if (key->handle()->IsSymbol()) {
3145 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3146 LoadAndSpill(value);
3147 frame_->EmitPop(r0);
3148 __ mov(r2, Operand(key->handle()));
3149 __ ldr(r1, frame_->Top()); // Load the receiver.
3150 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3151 break;
3152 }
3153 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003154 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003155 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156 frame_->EmitPush(r0); // dup the result
3157 LoadAndSpill(key);
3158 LoadAndSpill(value);
3159 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 break;
3161 }
3162 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003163 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003164 frame_->EmitPush(r0);
3165 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003166 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003167 frame_->EmitPush(r0);
3168 LoadAndSpill(value);
3169 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 break;
3171 }
3172 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003173 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003174 frame_->EmitPush(r0);
3175 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003176 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003177 frame_->EmitPush(r0);
3178 LoadAndSpill(value);
3179 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180 break;
3181 }
3182 }
3183 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003184 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003185}
3186
3187
ager@chromium.org7c537e22008-10-16 08:43:32 +00003188void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003189#ifdef DEBUG
3190 int original_height = frame_->height();
3191#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003192 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003194
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003195 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003196 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003197 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003198 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003199 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003200 __ mov(r0, Operand(node->constant_elements()));
3201 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003202 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003203 if (node->depth() > 1) {
3204 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003205 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003206 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003207 } else {
3208 FastCloneShallowArrayStub stub(length);
3209 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003210 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003211 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003212 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003213
3214 // Generate code to set the elements in the array that are not
3215 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 for (int i = 0; i < node->values()->length(); i++) {
3217 Expression* value = node->values()->at(i);
3218
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003219 // If value is a literal the property value is already set in the
3220 // boilerplate object.
3221 if (value->AsLiteral() != NULL) continue;
3222 // If value is a materialized literal the property value is already set
3223 // in the boilerplate object if it is simple.
3224 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003225
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003226 // The property must be set by generated code.
3227 LoadAndSpill(value);
3228 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003230 // Fetch the object literal.
3231 __ ldr(r1, frame_->Top());
3232 // Get the elements array.
3233 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003234
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003235 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003236 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003237 __ str(r0, FieldMemOperand(r1, offset));
3238
3239 // Update the write barrier for the array address.
3240 __ mov(r3, Operand(offset));
3241 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003242 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003243 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244}
3245
3246
ager@chromium.org32912102009-01-16 10:38:43 +00003247void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248#ifdef DEBUG
3249 int original_height = frame_->height();
3250#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003251 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org32912102009-01-16 10:38:43 +00003252 // Call runtime routine to allocate the catch extension object and
3253 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003254 Comment cmnt(masm_, "[ CatchExtensionObject");
3255 LoadAndSpill(node->key());
3256 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003257 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3258 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003259 ASSERT_EQ(original_height + 1, frame_->height());
3260}
3261
3262
3263void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3264#ifdef DEBUG
3265 int original_height = frame_->height();
3266#endif
3267 Comment cmnt(masm(), "[ Variable Assignment");
3268 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3269 ASSERT(var != NULL);
3270 Slot* slot = var->slot();
3271 ASSERT(slot != NULL);
3272
3273 // Evaluate the right-hand side.
3274 if (node->is_compound()) {
3275 // For a compound assignment the right-hand side is a binary operation
3276 // between the current property value and the actual right-hand side.
3277 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3278
3279 // Perform the binary operation.
3280 Literal* literal = node->value()->AsLiteral();
3281 bool overwrite_value =
3282 (node->value()->AsBinaryOperation() != NULL &&
3283 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3284 if (literal != NULL && literal->handle()->IsSmi()) {
3285 SmiOperation(node->binary_op(),
3286 literal->handle(),
3287 false,
3288 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3289 } else {
3290 Load(node->value());
3291 VirtualFrameBinaryOperation(
3292 node->binary_op(), overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3293 }
3294 } else {
3295 Load(node->value());
3296 }
3297
3298 // Perform the assignment.
3299 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3300 CodeForSourcePosition(node->position());
3301 StoreToSlot(slot,
3302 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3303 }
3304 ASSERT_EQ(original_height + 1, frame_->height());
3305}
3306
3307
3308void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3309#ifdef DEBUG
3310 int original_height = frame_->height();
3311#endif
3312 Comment cmnt(masm(), "[ Named Property Assignment");
3313 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3314 Property* prop = node->target()->AsProperty();
3315 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3316
3317 // Initialize name and evaluate the receiver sub-expression if necessary. If
3318 // the receiver is trivial it is not placed on the stack at this point, but
3319 // loaded whenever actually needed.
3320 Handle<String> name;
3321 bool is_trivial_receiver = false;
3322 if (var != NULL) {
3323 name = var->name();
3324 } else {
3325 Literal* lit = prop->key()->AsLiteral();
3326 ASSERT_NOT_NULL(lit);
3327 name = Handle<String>::cast(lit->handle());
3328 // Do not materialize the receiver on the frame if it is trivial.
3329 is_trivial_receiver = prop->obj()->IsTrivial();
3330 if (!is_trivial_receiver) Load(prop->obj());
3331 }
3332
3333 // Change to slow case in the beginning of an initialization block to
3334 // avoid the quadratic behavior of repeatedly adding fast properties.
3335 if (node->starts_initialization_block()) {
3336 // Initialization block consists of assignments of the form expr.x = ..., so
3337 // this will never be an assignment to a variable, so there must be a
3338 // receiver object.
3339 ASSERT_EQ(NULL, var);
3340 if (is_trivial_receiver) {
3341 Load(prop->obj());
3342 } else {
3343 frame_->Dup();
3344 }
3345 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3346 }
3347
3348 // Change to fast case at the end of an initialization block. To prepare for
3349 // that add an extra copy of the receiver to the frame, so that it can be
3350 // converted back to fast case after the assignment.
3351 if (node->ends_initialization_block() && !is_trivial_receiver) {
3352 frame_->Dup();
3353 }
3354
3355 // Stack layout:
3356 // [tos] : receiver (only materialized if non-trivial)
3357 // [tos+1] : receiver if at the end of an initialization block
3358
3359 // Evaluate the right-hand side.
3360 if (node->is_compound()) {
3361 // For a compound assignment the right-hand side is a binary operation
3362 // between the current property value and the actual right-hand side.
3363 if (is_trivial_receiver) {
3364 Load(prop->obj());
3365 } else if (var != NULL) {
3366 LoadGlobal();
3367 } else {
3368 frame_->Dup();
3369 }
3370 EmitNamedLoad(name, var != NULL);
3371 frame_->Drop(); // Receiver is left on the stack.
3372 frame_->EmitPush(r0);
3373
3374 // Perform the binary operation.
3375 Literal* literal = node->value()->AsLiteral();
3376 bool overwrite_value =
3377 (node->value()->AsBinaryOperation() != NULL &&
3378 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3379 if (literal != NULL && literal->handle()->IsSmi()) {
3380 SmiOperation(node->binary_op(),
3381 literal->handle(),
3382 false,
3383 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3384 } else {
3385 Load(node->value());
3386 VirtualFrameBinaryOperation(
3387 node->binary_op(), overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3388 }
3389 } else {
3390 // For non-compound assignment just load the right-hand side.
3391 Load(node->value());
3392 }
3393
3394 // Stack layout:
3395 // [tos] : value
3396 // [tos+1] : receiver (only materialized if non-trivial)
3397 // [tos+2] : receiver if at the end of an initialization block
3398
3399 // Perform the assignment. It is safe to ignore constants here.
3400 ASSERT(var == NULL || var->mode() != Variable::CONST);
3401 ASSERT_NE(Token::INIT_CONST, node->op());
3402 if (is_trivial_receiver) {
3403 // Load the receiver and swap with the value.
3404 Load(prop->obj());
3405 Register t0 = frame_->PopToRegister();
3406 Register t1 = frame_->PopToRegister(t0);
3407 frame_->EmitPush(t0);
3408 frame_->EmitPush(t1);
3409 }
3410 CodeForSourcePosition(node->position());
3411 bool is_contextual = (var != NULL);
3412 EmitNamedStore(name, is_contextual);
3413 frame_->EmitPush(r0);
3414
3415 // Change to fast case at the end of an initialization block.
3416 if (node->ends_initialization_block()) {
3417 ASSERT_EQ(NULL, var);
3418 // The argument to the runtime call is the receiver.
3419 if (is_trivial_receiver) {
3420 Load(prop->obj());
3421 } else {
3422 // A copy of the receiver is below the value of the assignment. Swap
3423 // the receiver and the value of the assignment expression.
3424 Register t0 = frame_->PopToRegister();
3425 Register t1 = frame_->PopToRegister(t0);
3426 frame_->EmitPush(t0);
3427 frame_->EmitPush(t1);
3428 }
3429 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3430 }
3431
3432 // Stack layout:
3433 // [tos] : result
3434
3435 ASSERT_EQ(original_height + 1, frame_->height());
3436}
3437
3438
3439void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3440#ifdef DEBUG
3441 int original_height = frame_->height();
3442#endif
3443 Comment cmnt(masm_, "[ Keyed Property Assignment");
3444 Property* prop = node->target()->AsProperty();
3445 ASSERT_NOT_NULL(prop);
3446
3447 // Evaluate the receiver subexpression.
3448 Load(prop->obj());
3449
3450 // Change to slow case in the beginning of an initialization block to
3451 // avoid the quadratic behavior of repeatedly adding fast properties.
3452 if (node->starts_initialization_block()) {
3453 frame_->Dup();
3454 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3455 }
3456
3457 // Change to fast case at the end of an initialization block. To prepare for
3458 // that add an extra copy of the receiver to the frame, so that it can be
3459 // converted back to fast case after the assignment.
3460 if (node->ends_initialization_block()) {
3461 frame_->Dup();
3462 }
3463
3464 // Evaluate the key subexpression.
3465 Load(prop->key());
3466
3467 // Stack layout:
3468 // [tos] : key
3469 // [tos+1] : receiver
3470 // [tos+2] : receiver if at the end of an initialization block
3471
3472 // Evaluate the right-hand side.
3473 if (node->is_compound()) {
3474 // For a compound assignment the right-hand side is a binary operation
3475 // between the current property value and the actual right-hand side.
3476 // Load of the current value leaves receiver and key on the stack.
3477 EmitKeyedLoad();
3478 frame_->EmitPush(r0);
3479
3480 // Perform the binary operation.
3481 Literal* literal = node->value()->AsLiteral();
3482 bool overwrite_value =
3483 (node->value()->AsBinaryOperation() != NULL &&
3484 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3485 if (literal != NULL && literal->handle()->IsSmi()) {
3486 SmiOperation(node->binary_op(),
3487 literal->handle(),
3488 false,
3489 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3490 } else {
3491 Load(node->value());
3492 VirtualFrameBinaryOperation(
3493 node->binary_op(), overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3494 }
3495 } else {
3496 // For non-compound assignment just load the right-hand side.
3497 Load(node->value());
3498 }
3499
3500 // Stack layout:
3501 // [tos] : value
3502 // [tos+1] : key
3503 // [tos+2] : receiver
3504 // [tos+3] : receiver if at the end of an initialization block
3505
3506 // Perform the assignment. It is safe to ignore constants here.
3507 ASSERT(node->op() != Token::INIT_CONST);
3508 CodeForSourcePosition(node->position());
3509 frame_->PopToR0();
3510 EmitKeyedStore(prop->key()->type());
3511 frame_->Drop(2); // Key and receiver are left on the stack.
3512 frame_->EmitPush(r0);
3513
3514 // Stack layout:
3515 // [tos] : result
3516 // [tos+1] : receiver if at the end of an initialization block
3517
3518 // Change to fast case at the end of an initialization block.
3519 if (node->ends_initialization_block()) {
3520 // The argument to the runtime call is the extra copy of the receiver,
3521 // which is below the value of the assignment. Swap the receiver and
3522 // the value of the assignment expression.
3523 Register t0 = frame_->PopToRegister();
3524 Register t1 = frame_->PopToRegister(t0);
3525 frame_->EmitPush(t1);
3526 frame_->EmitPush(t0);
3527 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3528 }
3529
3530 // Stack layout:
3531 // [tos] : result
3532
3533 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003534}
3535
3536
ager@chromium.org7c537e22008-10-16 08:43:32 +00003537void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003538 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003539#ifdef DEBUG
3540 int original_height = frame_->height();
3541#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003543
ager@chromium.orgac091b72010-05-05 07:34:42 +00003544 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3545 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546
ager@chromium.orgac091b72010-05-05 07:34:42 +00003547 if (var != NULL && !var->is_global()) {
3548 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003549
ager@chromium.orgac091b72010-05-05 07:34:42 +00003550 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3551 (var != NULL && var->is_global())) {
3552 // Properties whose keys are property names and global variables are
3553 // treated as named property references. We do not need to consider
3554 // global 'this' because it is not a valid left-hand side.
3555 EmitNamedPropertyAssignment(node);
3556
3557 } else if (prop != NULL) {
3558 // Other properties (including rewritten parameters for a function that
3559 // uses arguments) are keyed property assignments.
3560 EmitKeyedPropertyAssignment(node);
3561
3562 } else {
3563 // Invalid left-hand side.
3564 Load(node->target());
3565 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3566 // The runtime call doesn't actually return but the code generator will
3567 // still generate code and expects a certain frame height.
3568 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003569 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003570 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003571}
3572
3573
ager@chromium.org7c537e22008-10-16 08:43:32 +00003574void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575#ifdef DEBUG
3576 int original_height = frame_->height();
3577#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003578 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003579 Comment cmnt(masm_, "[ Throw");
3580
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003581 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003582 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003583 frame_->CallRuntime(Runtime::kThrow, 1);
3584 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003585 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586}
3587
3588
ager@chromium.org7c537e22008-10-16 08:43:32 +00003589void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003590#ifdef DEBUG
3591 int original_height = frame_->height();
3592#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003595 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003596 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003597 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003598 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599}
3600
3601
ager@chromium.org7c537e22008-10-16 08:43:32 +00003602void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003603#ifdef DEBUG
3604 int original_height = frame_->height();
3605#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003606 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607 Comment cmnt(masm_, "[ Call");
3608
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003609 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610 ZoneList<Expression*>* args = node->arguments();
3611
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003614 Variable* var = function->AsVariableProxy()->AsVariable();
3615 Property* property = function->AsProperty();
3616
3617 // ------------------------------------------------------------------------
3618 // Fast-case: Use inline caching.
3619 // ---
3620 // According to ECMA-262, section 11.2.3, page 44, the function to call
3621 // must be resolved after the arguments have been evaluated. The IC code
3622 // automatically handles this by loading the arguments before the function
3623 // is resolved in cache misses (this also holds for megamorphic calls).
3624 // ------------------------------------------------------------------------
3625
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003626 if (var != NULL && var->is_possibly_eval()) {
3627 // ----------------------------------
3628 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3629 // ----------------------------------
3630
3631 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3632 // resolve the function we need to call and the receiver of the
3633 // call. Then we call the resolved function using the given
3634 // arguments.
3635 // Prepare stack for call to resolved function.
3636 LoadAndSpill(function);
3637 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3638 frame_->EmitPush(r2); // Slot for receiver
3639 int arg_count = args->length();
3640 for (int i = 0; i < arg_count; i++) {
3641 LoadAndSpill(args->at(i));
3642 }
3643
3644 // Prepare stack for call to ResolvePossiblyDirectEval.
3645 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3646 frame_->EmitPush(r1);
3647 if (arg_count > 0) {
3648 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3649 frame_->EmitPush(r1);
3650 } else {
3651 frame_->EmitPush(r2);
3652 }
3653
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003654 // Push the receiver.
3655 __ ldr(r1, frame_->Receiver());
3656 frame_->EmitPush(r1);
3657
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003658 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003659 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003660
3661 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003662 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003663 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3664
3665 // Call the function.
3666 CodeForSourcePosition(node->position());
3667
3668 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003669 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003670 frame_->CallStub(&call_function, arg_count + 1);
3671
3672 __ ldr(cp, frame_->Context());
3673 // Remove the function from the stack.
3674 frame_->Drop();
3675 frame_->EmitPush(r0);
3676
3677 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003678 // ----------------------------------
3679 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3680 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003681 // Pass the global object as the receiver and let the IC stub
3682 // patch the stack to use the global proxy as 'this' in the
3683 // invoked function.
3684 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685
3686 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003687 int arg_count = args->length();
3688 for (int i = 0; i < arg_count; i++) {
3689 LoadAndSpill(args->at(i));
3690 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691
ager@chromium.org5c838252010-02-19 08:53:10 +00003692 // Setup the name register and call the IC initialization code.
3693 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003694 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3695 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003696 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003697 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3698 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003699 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003700 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701
3702 } else if (var != NULL && var->slot() != NULL &&
3703 var->slot()->type() == Slot::LOOKUP) {
3704 // ----------------------------------
3705 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3706 // ----------------------------------
3707
3708 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003709 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003710 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003711 frame_->EmitPush(r0);
3712 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713 // r0: slot value; r1: receiver
3714
3715 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003716 frame_->EmitPush(r0); // function
3717 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718
3719 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003720 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003721 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722
3723 } else if (property != NULL) {
3724 // Check if the key is a literal string.
3725 Literal* literal = property->key()->AsLiteral();
3726
3727 if (literal != NULL && literal->handle()->IsSymbol()) {
3728 // ------------------------------------------------------------------
3729 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3730 // ------------------------------------------------------------------
3731
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003732 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003734 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
3735 name->IsEqualTo(CStrVector("apply")) &&
3736 args->length() == 2 &&
3737 args->at(1)->AsVariableProxy() != NULL &&
3738 args->at(1)->AsVariableProxy()->IsArguments()) {
3739 // Use the optimized Function.prototype.apply that avoids
3740 // allocating lazily allocated arguments objects.
3741 CallApplyLazy(property->obj(),
3742 args->at(0),
3743 args->at(1)->AsVariableProxy(),
3744 node->position());
3745
3746 } else {
3747 LoadAndSpill(property->obj()); // Receiver.
3748 // Load the arguments.
3749 int arg_count = args->length();
3750 for (int i = 0; i < arg_count; i++) {
3751 LoadAndSpill(args->at(i));
3752 }
3753
3754 // Set the name register and call the IC initialization code.
3755 __ mov(r2, Operand(name));
3756 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3757 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
3758 CodeForSourcePosition(node->position());
3759 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
3760 __ ldr(cp, frame_->Context());
3761 frame_->EmitPush(r0);
3762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003763
3764 } else {
3765 // -------------------------------------------
3766 // JavaScript example: 'array[index](1, 2, 3)'
3767 // -------------------------------------------
3768
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003769 LoadAndSpill(property->obj());
3770 LoadAndSpill(property->key());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003771 EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003772 frame_->Drop(); // key
3773 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003774 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003775 // Use the global receiver.
3776 frame_->Drop();
3777 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003778 LoadGlobalReceiver(r0);
3779 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003780 frame_->EmitPop(r1); // receiver
3781 frame_->EmitPush(r0); // function
3782 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 }
3784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003786 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003787 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788 }
3789
3790 } else {
3791 // ----------------------------------
3792 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3793 // ----------------------------------
3794
3795 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003796 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003797
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003798 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003799 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003802 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003803 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003805 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003806}
3807
3808
ager@chromium.org7c537e22008-10-16 08:43:32 +00003809void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810#ifdef DEBUG
3811 int original_height = frame_->height();
3812#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003813 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814 Comment cmnt(masm_, "[ CallNew");
3815
3816 // According to ECMA-262, section 11.2.2, page 44, the function
3817 // expression in new calls must be evaluated before the
3818 // arguments. This is different from ordinary calls, where the
3819 // actual function to call is resolved after the arguments have been
3820 // evaluated.
3821
3822 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003823 // receiver. There is no need to use the global proxy here because
3824 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003825 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003826 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827
3828 // Push the arguments ("left-to-right") on the stack.
3829 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003830 int arg_count = args->length();
3831 for (int i = 0; i < arg_count; i++) {
3832 LoadAndSpill(args->at(i));
3833 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834
mads.s.ager31e71382008-08-13 09:32:07 +00003835 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003836 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003837 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003838 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 // Call the construct call builtin that handles allocation and
3841 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003842 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003843 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003844 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003845
3846 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003847 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003848 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849}
3850
3851
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003852void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003853 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003854 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003855 JumpTarget leave, null, function, non_function_constructor;
3856
3857 // Load the object into r0.
3858 LoadAndSpill(args->at(0));
3859 frame_->EmitPop(r0);
3860
3861 // If the object is a smi, we return null.
3862 __ tst(r0, Operand(kSmiTagMask));
3863 null.Branch(eq);
3864
3865 // Check that the object is a JS object but take special care of JS
3866 // functions to make sure they have 'Function' as their class.
3867 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3868 null.Branch(lt);
3869
3870 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3871 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3872 // LAST_JS_OBJECT_TYPE.
3873 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3874 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3875 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3876 function.Branch(eq);
3877
3878 // Check if the constructor in the map is a function.
3879 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3880 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3881 non_function_constructor.Branch(ne);
3882
3883 // The r0 register now contains the constructor function. Grab the
3884 // instance class name from there.
3885 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3886 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003887 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003888 leave.Jump();
3889
3890 // Functions have class 'Function'.
3891 function.Bind();
3892 __ mov(r0, Operand(Factory::function_class_symbol()));
3893 frame_->EmitPush(r0);
3894 leave.Jump();
3895
3896 // Objects with a non-function constructor have class 'Object'.
3897 non_function_constructor.Bind();
3898 __ mov(r0, Operand(Factory::Object_symbol()));
3899 frame_->EmitPush(r0);
3900 leave.Jump();
3901
3902 // Non-JS objects have class null.
3903 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003904 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003905 frame_->EmitPush(r0);
3906
3907 // All done.
3908 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003909}
3910
3911
ager@chromium.org7c537e22008-10-16 08:43:32 +00003912void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003913 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003915 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003916 LoadAndSpill(args->at(0));
3917 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003918 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003921 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3922 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003923 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 // Load the value.
3925 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003926 leave.Bind();
3927 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928}
3929
3930
ager@chromium.org7c537e22008-10-16 08:43:32 +00003931void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003932 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003934 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003935 LoadAndSpill(args->at(0)); // Load the object.
3936 LoadAndSpill(args->at(1)); // Load the value.
3937 frame_->EmitPop(r0); // r0 contains value
3938 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939 // if (object->IsSmi()) return object.
3940 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003941 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003942 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3943 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003944 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003945 // Store the value.
3946 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3947 // Update the write barrier.
3948 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3949 __ RecordWrite(r1, r2, r3);
3950 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003951 leave.Bind();
3952 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953}
3954
3955
ager@chromium.org7c537e22008-10-16 08:43:32 +00003956void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003957 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003959 LoadAndSpill(args->at(0));
3960 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003961 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962 cc_reg_ = eq;
3963}
3964
3965
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003966void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003967 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003968 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3969 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003970#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003971 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003972 LoadAndSpill(args->at(1));
3973 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003974 __ CallRuntime(Runtime::kLog, 2);
3975 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003976#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003977 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003978 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003979}
3980
3981
ager@chromium.org7c537e22008-10-16 08:43:32 +00003982void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003983 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003984 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003985 LoadAndSpill(args->at(0));
3986 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003987 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003988 cc_reg_ = eq;
3989}
3990
3991
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003992// Generates the Math.pow method - currently just calls runtime.
3993void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3994 ASSERT(args->length() == 2);
3995 Load(args->at(0));
3996 Load(args->at(1));
3997 frame_->CallRuntime(Runtime::kMath_pow, 2);
3998 frame_->EmitPush(r0);
3999}
4000
4001
4002// Generates the Math.sqrt method - currently just calls runtime.
4003void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4004 ASSERT(args->length() == 1);
4005 Load(args->at(0));
4006 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4007 frame_->EmitPush(r0);
4008}
4009
4010
ager@chromium.orgac091b72010-05-05 07:34:42 +00004011// This generates code that performs a charCodeAt() call or returns
kasper.lund7276f142008-07-30 08:49:36 +00004012// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
ager@chromium.orgac091b72010-05-05 07:34:42 +00004013// It can handle flat, 8 and 16 bit characters and cons strings where the
4014// answer is found in the left hand branch of the cons. The slow case will
4015// flatten the string, which will ensure that the answer is in the left hand
4016// side the next time around.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004017void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004018 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00004019 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004020 Comment(masm_, "[ GenerateFastCharCodeAt");
4021
4022 LoadAndSpill(args->at(0));
4023 LoadAndSpill(args->at(1));
ager@chromium.orgac091b72010-05-05 07:34:42 +00004024 frame_->EmitPop(r1); // Index.
4025 frame_->EmitPop(r2); // String.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004026
ager@chromium.orgac091b72010-05-05 07:34:42 +00004027 Label slow_case;
4028 Label exit;
4029 StringHelper::GenerateFastCharCodeAt(masm_,
4030 r2,
4031 r1,
4032 r3,
4033 r0,
4034 &slow_case,
4035 &slow_case,
4036 &slow_case,
4037 &slow_case);
4038 __ jmp(&exit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004039
ager@chromium.orgac091b72010-05-05 07:34:42 +00004040 __ bind(&slow_case);
4041 // Move the undefined value into the result register, which will
4042 // trigger the slow case.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004043 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004044
ager@chromium.orgac091b72010-05-05 07:34:42 +00004045 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004046 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00004047}
4048
4049
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004050void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
4051 Comment(masm_, "[ GenerateCharFromCode");
4052 ASSERT(args->length() == 1);
4053
ager@chromium.orgac091b72010-05-05 07:34:42 +00004054 Register code = r1;
4055 Register scratch = ip;
4056 Register result = r0;
4057
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004058 LoadAndSpill(args->at(0));
ager@chromium.orgac091b72010-05-05 07:34:42 +00004059 frame_->EmitPop(code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004060
ager@chromium.orgac091b72010-05-05 07:34:42 +00004061 StringHelper::GenerateCharFromCode(masm_,
4062 code,
4063 scratch,
4064 result,
4065 CALL_FUNCTION);
4066 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004067}
4068
4069
ager@chromium.org7c537e22008-10-16 08:43:32 +00004070void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004071 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004072 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004073 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004074 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004075 // We need the CC bits to come out as not_equal in the case where the
4076 // object is a smi. This can't be done with the usual test opcode so
4077 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004079 __ and_(r1, r0, Operand(kSmiTagMask));
4080 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004081 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004082 // It is a heap object - get the map. Check if the object is a JS array.
4083 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004084 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004085 cc_reg_ = eq;
4086}
4087
4088
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004089void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004090 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004091 ASSERT(args->length() == 1);
4092 LoadAndSpill(args->at(0));
4093 JumpTarget answer;
4094 // We need the CC bits to come out as not_equal in the case where the
4095 // object is a smi. This can't be done with the usual test opcode so
4096 // we use XOR to get the right CC bits.
4097 frame_->EmitPop(r0);
4098 __ and_(r1, r0, Operand(kSmiTagMask));
4099 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
4100 answer.Branch(ne);
4101 // It is a heap object - get the map. Check if the object is a regexp.
4102 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4103 answer.Bind();
4104 cc_reg_ = eq;
4105}
4106
4107
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004108void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4109 // This generates a fast version of:
4110 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00004111 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004112 ASSERT(args->length() == 1);
4113 LoadAndSpill(args->at(0));
4114 frame_->EmitPop(r1);
4115 __ tst(r1, Operand(kSmiTagMask));
4116 false_target()->Branch(eq);
4117
4118 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4119 __ cmp(r1, ip);
4120 true_target()->Branch(eq);
4121
4122 Register map_reg = r2;
4123 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
4124 // Undetectable objects behave like undefined when tested with typeof.
4125 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4126 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4127 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4128 false_target()->Branch(eq);
4129
4130 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4131 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
4132 false_target()->Branch(lt);
4133 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
4134 cc_reg_ = le;
4135}
4136
4137
4138void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4139 // This generates a fast version of:
4140 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00004141 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004142 ASSERT(args->length() == 1);
4143 LoadAndSpill(args->at(0));
4144 frame_->EmitPop(r0);
4145 __ tst(r0, Operand(kSmiTagMask));
4146 false_target()->Branch(eq);
4147 Register map_reg = r2;
4148 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
4149 cc_reg_ = eq;
4150}
4151
4152
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004153void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004154 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004155 ASSERT(args->length() == 1);
4156 LoadAndSpill(args->at(0));
4157 frame_->EmitPop(r0);
4158 __ tst(r0, Operand(kSmiTagMask));
4159 false_target()->Branch(eq);
4160 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4161 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
4162 __ tst(r1, Operand(1 << Map::kIsUndetectable));
4163 cc_reg_ = ne;
4164}
4165
4166
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004167void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004168 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004169 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004170
4171 // Get the frame pointer for the calling frame.
4172 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4173
4174 // Skip the arguments adaptor frame if it exists.
4175 Label check_frame_marker;
4176 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004177 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004178 __ b(ne, &check_frame_marker);
4179 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
4180
4181 // Check the marker in the calling frame.
4182 __ bind(&check_frame_marker);
4183 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
4184 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4185 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004186}
4187
4188
ager@chromium.org7c537e22008-10-16 08:43:32 +00004189void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004190 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 ASSERT(args->length() == 0);
4192
lrn@chromium.org25156de2010-04-06 13:10:27 +00004193 Label exit;
4194
4195 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00004196 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197
lrn@chromium.org25156de2010-04-06 13:10:27 +00004198 // Check if the calling frame is an arguments adaptor frame.
4199 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4200 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4201 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4202 __ b(ne, &exit);
4203
4204 // Arguments adaptor case: Read the arguments length from the
4205 // adaptor frame.
4206 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4207
4208 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004209 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004210}
4211
4212
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004213void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004214 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215 ASSERT(args->length() == 1);
4216
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004217 // Satisfy contract with ArgumentsAccessStub:
4218 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004219 LoadAndSpill(args->at(0));
4220 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004221 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222
4223 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004224 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004225 frame_->CallStub(&stub, 0);
4226 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004227}
4228
4229
ager@chromium.org357bf652010-04-12 11:30:10 +00004230void CodeGenerator::GenerateRandomHeapNumber(
4231 ZoneList<Expression*>* args) {
4232 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004233 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004234
4235 Label slow_allocate_heapnumber;
4236 Label heapnumber_allocated;
4237
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004238 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004239 __ jmp(&heapnumber_allocated);
4240
4241 __ bind(&slow_allocate_heapnumber);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004242 // To allocate a heap number, and ensure that it is not a smi, we
4243 // call the runtime function FUnaryMinus on 0, returning the double
4244 // -0.0. A new, distinct heap number is returned each time.
ager@chromium.org357bf652010-04-12 11:30:10 +00004245 __ mov(r0, Operand(Smi::FromInt(0)));
4246 __ push(r0);
4247 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004248 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004249
4250 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004251
4252 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4253 // by computing:
4254 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4255 if (CpuFeatures::IsSupported(VFP3)) {
4256 __ PrepareCallCFunction(0, r1);
4257 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4258
4259 CpuFeatures::Scope scope(VFP3);
4260 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4261 // Create this constant using mov/orr to avoid PC relative load.
4262 __ mov(r1, Operand(0x41000000));
4263 __ orr(r1, r1, Operand(0x300000));
4264 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4265 __ vmov(d7, r0, r1);
4266 // Move 0x4130000000000000 to VFP.
4267 __ mov(r0, Operand(0));
4268 __ vmov(d8, r0, r1);
4269 // Subtract and store the result in the heap number.
4270 __ vsub(d7, d7, d8);
4271 __ sub(r0, r4, Operand(kHeapObjectTag));
4272 __ vstr(d7, r0, HeapNumber::kValueOffset);
4273 frame_->EmitPush(r4);
4274 } else {
4275 __ mov(r0, Operand(r4));
4276 __ PrepareCallCFunction(1, r1);
4277 __ CallCFunction(
4278 ExternalReference::fill_heap_number_with_random_function(), 1);
4279 frame_->EmitPush(r0);
4280 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004281}
4282
4283
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004284void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4285 ASSERT_EQ(2, args->length());
4286
4287 Load(args->at(0));
4288 Load(args->at(1));
4289
ager@chromium.org5c838252010-02-19 08:53:10 +00004290 StringAddStub stub(NO_STRING_ADD_FLAGS);
4291 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004292 frame_->EmitPush(r0);
4293}
4294
4295
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004296void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4297 ASSERT_EQ(3, args->length());
4298
4299 Load(args->at(0));
4300 Load(args->at(1));
4301 Load(args->at(2));
4302
ager@chromium.org5c838252010-02-19 08:53:10 +00004303 SubStringStub stub;
4304 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004305 frame_->EmitPush(r0);
4306}
4307
4308
4309void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4310 ASSERT_EQ(2, args->length());
4311
4312 Load(args->at(0));
4313 Load(args->at(1));
4314
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004315 StringCompareStub stub;
4316 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004317 frame_->EmitPush(r0);
4318}
4319
4320
4321void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4322 ASSERT_EQ(4, args->length());
4323
4324 Load(args->at(0));
4325 Load(args->at(1));
4326 Load(args->at(2));
4327 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004328 RegExpExecStub stub;
4329 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004330 frame_->EmitPush(r0);
4331}
4332
4333
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004334void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4335 // No stub. This code only occurs a few times in regexp.js.
4336 const int kMaxInlineLength = 100;
4337 ASSERT_EQ(3, args->length());
4338 Load(args->at(0)); // Size of array, smi.
4339 Load(args->at(1)); // "index" property value.
4340 Load(args->at(2)); // "input" property value.
4341 {
4342 VirtualFrame::SpilledScope spilled_scope(frame_);
4343 Label slowcase;
4344 Label done;
4345 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4346 STATIC_ASSERT(kSmiTag == 0);
4347 STATIC_ASSERT(kSmiTagSize == 1);
4348 __ tst(r1, Operand(kSmiTagMask));
4349 __ b(ne, &slowcase);
4350 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4351 __ b(hi, &slowcase);
4352 // Smi-tagging is equivalent to multiplying by 2.
4353 // Allocate RegExpResult followed by FixedArray with size in ebx.
4354 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4355 // Elements: [Map][Length][..elements..]
4356 // Size of JSArray with two in-object properties and the header of a
4357 // FixedArray.
4358 int objects_size =
4359 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4360 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4361 __ add(r2, r5, Operand(objects_size));
4362 __ AllocateInNewSpace(r2, // In: Size, in words.
4363 r0, // Out: Start of allocation (tagged).
4364 r3, // Scratch register.
4365 r4, // Scratch register.
4366 &slowcase,
4367 TAG_OBJECT);
4368 // r0: Start of allocated area, object-tagged.
4369 // r1: Number of elements in array, as smi.
4370 // r5: Number of elements, untagged.
4371
4372 // Set JSArray map to global.regexp_result_map().
4373 // Set empty properties FixedArray.
4374 // Set elements to point to FixedArray allocated right after the JSArray.
4375 // Interleave operations for better latency.
4376 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4377 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4378 __ mov(r4, Operand(Factory::empty_fixed_array()));
4379 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4380 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4381 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4382 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4383 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4384
4385 // Set input, index and length fields from arguments.
4386 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4387 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4388 __ add(sp, sp, Operand(kPointerSize));
4389 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4390 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4391
4392 // Fill out the elements FixedArray.
4393 // r0: JSArray, tagged.
4394 // r3: FixedArray, tagged.
4395 // r5: Number of elements in array, untagged.
4396
4397 // Set map.
4398 __ mov(r2, Operand(Factory::fixed_array_map()));
4399 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4400 // Set FixedArray length.
4401 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4402 // Fill contents of fixed-array with the-hole.
4403 __ mov(r2, Operand(Factory::the_hole_value()));
4404 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4405 // Fill fixed array elements with hole.
4406 // r0: JSArray, tagged.
4407 // r2: the hole.
4408 // r3: Start of elements in FixedArray.
4409 // r5: Number of elements to fill.
4410 Label loop;
4411 __ tst(r5, Operand(r5));
4412 __ bind(&loop);
4413 __ b(le, &done); // Jump if r1 is negative or zero.
4414 __ sub(r5, r5, Operand(1), SetCC);
4415 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4416 __ jmp(&loop);
4417
4418 __ bind(&slowcase);
4419 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4420
4421 __ bind(&done);
4422 }
4423 frame_->Forget(3);
4424 frame_->EmitPush(r0);
4425}
4426
4427
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004428class DeferredSearchCache: public DeferredCode {
4429 public:
4430 DeferredSearchCache(Register dst, Register cache, Register key)
4431 : dst_(dst), cache_(cache), key_(key) {
4432 set_comment("[ DeferredSearchCache");
4433 }
4434
4435 virtual void Generate();
4436
4437 private:
4438 Register dst_, cache_, key_;
4439};
4440
4441
4442void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00004443 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004444 __ CallRuntime(Runtime::kGetFromCache, 2);
4445 if (!dst_.is(r0)) {
4446 __ mov(dst_, r0);
4447 }
4448}
4449
4450
4451void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4452 ASSERT_EQ(2, args->length());
4453
4454 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4455 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4456
4457 Handle<FixedArray> jsfunction_result_caches(
4458 Top::global_context()->jsfunction_result_caches());
4459 if (jsfunction_result_caches->length() <= cache_id) {
4460 __ Abort("Attempt to use undefined cache.");
4461 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
4462 frame_->EmitPush(r0);
4463 return;
4464 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004465
4466 Load(args->at(1));
4467 frame_->EmitPop(r2);
4468
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004469 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
4470 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
4471 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4472 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
4473
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004474 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4475
4476 const int kFingerOffset =
4477 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4478 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004479 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4480 // r0 now holds finger offset as a smi.
4481 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4482 // r3 now points to the start of fixed array elements.
4483 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4484 // Note side effect of PreIndex: r3 now points to the key of the pair.
4485 __ cmp(r2, r0);
4486 deferred->Branch(ne);
4487
4488 __ ldr(r0, MemOperand(r3, kPointerSize));
4489
4490 deferred->BindExit();
4491 frame_->EmitPush(r0);
4492}
4493
4494
ager@chromium.org5c838252010-02-19 08:53:10 +00004495void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4496 ASSERT_EQ(args->length(), 1);
4497
4498 // Load the argument on the stack and jump to the runtime.
4499 Load(args->at(0));
4500
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004501 NumberToStringStub stub;
4502 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004503 frame_->EmitPush(r0);
4504}
4505
4506
ager@chromium.orgac091b72010-05-05 07:34:42 +00004507class DeferredSwapElements: public DeferredCode {
4508 public:
4509 DeferredSwapElements(Register object, Register index1, Register index2)
4510 : object_(object), index1_(index1), index2_(index2) {
4511 set_comment("[ DeferredSwapElements");
4512 }
4513
4514 virtual void Generate();
4515
4516 private:
4517 Register object_, index1_, index2_;
4518};
4519
4520
4521void DeferredSwapElements::Generate() {
4522 __ push(object_);
4523 __ push(index1_);
4524 __ push(index2_);
4525 __ CallRuntime(Runtime::kSwapElements, 3);
4526}
4527
4528
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004529void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
4530 Comment cmnt(masm_, "[ GenerateSwapElements");
4531
4532 ASSERT_EQ(3, args->length());
4533
4534 Load(args->at(0));
4535 Load(args->at(1));
4536 Load(args->at(2));
4537
ager@chromium.orgac091b72010-05-05 07:34:42 +00004538 Register index2 = r2;
4539 Register index1 = r1;
4540 Register object = r0;
4541 Register tmp1 = r3;
4542 Register tmp2 = r4;
4543
4544 frame_->EmitPop(index2);
4545 frame_->EmitPop(index1);
4546 frame_->EmitPop(object);
4547
4548 DeferredSwapElements* deferred =
4549 new DeferredSwapElements(object, index1, index2);
4550
4551 // Fetch the map and check if array is in fast case.
4552 // Check that object doesn't require security checks and
4553 // has no indexed interceptor.
4554 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
4555 deferred->Branch(lt);
4556 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
4557 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
4558 deferred->Branch(nz);
4559
4560 // Check the object's elements are in fast case.
4561 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
4562 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
4563 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4564 __ cmp(tmp2, ip);
4565 deferred->Branch(ne);
4566
4567 // Smi-tagging is equivalent to multiplying by 2.
4568 STATIC_ASSERT(kSmiTag == 0);
4569 STATIC_ASSERT(kSmiTagSize == 1);
4570
4571 // Check that both indices are smis.
4572 __ mov(tmp2, index1);
4573 __ orr(tmp2, tmp2, index2);
4574 __ tst(tmp2, Operand(kSmiTagMask));
4575 deferred->Branch(nz);
4576
4577 // Bring the offsets into the fixed array in tmp1 into index1 and
4578 // index2.
4579 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4580 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
4581 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
4582
4583 // Swap elements.
4584 Register tmp3 = object;
4585 object = no_reg;
4586 __ ldr(tmp3, MemOperand(tmp1, index1));
4587 __ ldr(tmp2, MemOperand(tmp1, index2));
4588 __ str(tmp3, MemOperand(tmp1, index2));
4589 __ str(tmp2, MemOperand(tmp1, index1));
4590
4591 Label done;
4592 __ InNewSpace(tmp1, tmp2, eq, &done);
4593 // Possible optimization: do a check that both values are Smis
4594 // (or them and test against Smi mask.)
4595
4596 __ mov(tmp2, tmp1);
4597 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
4598 __ CallStub(&recordWrite1);
4599
4600 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
4601 __ CallStub(&recordWrite2);
4602
4603 __ bind(&done);
4604
4605 deferred->BindExit();
4606 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
4607 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004608}
4609
4610
ager@chromium.org357bf652010-04-12 11:30:10 +00004611void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4612 Comment cmnt(masm_, "[ GenerateCallFunction");
4613
4614 ASSERT(args->length() >= 2);
4615
4616 int n_args = args->length() - 2; // for receiver and function.
4617 Load(args->at(0)); // receiver
4618 for (int i = 0; i < n_args; i++) {
4619 Load(args->at(i + 1));
4620 }
4621 Load(args->at(n_args + 1)); // function
4622 frame_->CallJSFunction(n_args);
4623 frame_->EmitPush(r0);
4624}
4625
4626
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004627void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4628 ASSERT_EQ(args->length(), 1);
4629 // Load the argument on the stack and jump to the runtime.
4630 Load(args->at(0));
4631 frame_->CallRuntime(Runtime::kMath_sin, 1);
4632 frame_->EmitPush(r0);
4633}
4634
4635
4636void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4637 ASSERT_EQ(args->length(), 1);
4638 // Load the argument on the stack and jump to the runtime.
4639 Load(args->at(0));
4640 frame_->CallRuntime(Runtime::kMath_cos, 1);
4641 frame_->EmitPush(r0);
4642}
4643
4644
ager@chromium.org7c537e22008-10-16 08:43:32 +00004645void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004646 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004647 ASSERT(args->length() == 2);
4648
4649 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004650 LoadAndSpill(args->at(0));
4651 LoadAndSpill(args->at(1));
4652 frame_->EmitPop(r0);
4653 frame_->EmitPop(r1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004654 __ cmp(r0, r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004655 cc_reg_ = eq;
4656}
4657
4658
ager@chromium.org7c537e22008-10-16 08:43:32 +00004659void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004660#ifdef DEBUG
4661 int original_height = frame_->height();
4662#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004663 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004664 if (CheckForInlineRuntimeCall(node)) {
4665 ASSERT((has_cc() && frame_->height() == original_height) ||
4666 (!has_cc() && frame_->height() == original_height + 1));
4667 return;
4668 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004669
4670 ZoneList<Expression*>* args = node->arguments();
4671 Comment cmnt(masm_, "[ CallRuntime");
4672 Runtime::Function* function = node->function();
4673
ager@chromium.org41826e72009-03-30 13:30:57 +00004674 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004675 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004676 // Push the builtins object found in the current global object.
4677 __ ldr(r1, GlobalObject());
4678 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004679 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004680 }
mads.s.ager31e71382008-08-13 09:32:07 +00004681
ager@chromium.org41826e72009-03-30 13:30:57 +00004682 // Push the arguments ("left-to-right").
4683 int arg_count = args->length();
4684 for (int i = 0; i < arg_count; i++) {
4685 LoadAndSpill(args->at(i));
4686 }
mads.s.ager31e71382008-08-13 09:32:07 +00004687
ager@chromium.org41826e72009-03-30 13:30:57 +00004688 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004689 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004690 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004691 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4692 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004693 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004694 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004695 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004696 } else {
4697 // Call the C runtime function.
4698 frame_->CallRuntime(function, arg_count);
4699 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004700 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004701 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004702}
4703
4704
ager@chromium.org7c537e22008-10-16 08:43:32 +00004705void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004706#ifdef DEBUG
4707 int original_height = frame_->height();
4708#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004709 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004710 Comment cmnt(masm_, "[ UnaryOperation");
4711
4712 Token::Value op = node->op();
4713
4714 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004715 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004716 false_target(),
4717 true_target(),
4718 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004719 // LoadCondition may (and usually does) leave a test and branch to
4720 // be emitted by the caller. In that case, negate the condition.
4721 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004722
4723 } else if (op == Token::DELETE) {
4724 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004725 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004726 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004727 LoadAndSpill(property->obj());
4728 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004729 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004730
mads.s.ager31e71382008-08-13 09:32:07 +00004731 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004732 Slot* slot = variable->slot();
4733 if (variable->is_global()) {
4734 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004735 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004736 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004737 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004738
4739 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4740 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004741 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004742 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004743 frame_->EmitPush(r0);
4744 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004745 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004746 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004747 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004748 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004749 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004750
mads.s.ager31e71382008-08-13 09:32:07 +00004751 } else {
4752 // Default: Result of deleting non-global, not dynamically
4753 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004754 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004755 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004756
4757 } else {
4758 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004759 LoadAndSpill(node->expression()); // may have side-effects
4760 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004761 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004762 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004763 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004764
4765 } else if (op == Token::TYPEOF) {
4766 // Special case for loading the typeof expression; see comment on
4767 // LoadTypeofExpression().
4768 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004769 frame_->CallRuntime(Runtime::kTypeof, 1);
4770 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004771
4772 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004773 bool overwrite =
4774 (node->expression()->AsBinaryOperation() != NULL &&
4775 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004776 LoadAndSpill(node->expression());
4777 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004778 switch (op) {
4779 case Token::NOT:
4780 case Token::DELETE:
4781 case Token::TYPEOF:
4782 UNREACHABLE(); // handled above
4783 break;
4784
4785 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004786 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004787 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004788 break;
4789 }
4790
4791 case Token::BIT_NOT: {
4792 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004793 JumpTarget smi_label;
4794 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004795 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004796 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004797
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004798 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4799 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004800 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004801
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004802 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004803 __ mvn(r0, Operand(r0));
4804 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004805 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004806 break;
4807 }
4808
4809 case Token::VOID:
4810 // since the stack top is cached in r0, popping and then
4811 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004812 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004813 break;
4814
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004815 case Token::ADD: {
4816 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004817 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004818 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004819 continue_label.Branch(eq);
4820 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004821 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004822 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004823 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004825 default:
4826 UNREACHABLE();
4827 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004828 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004829 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004830 ASSERT(!has_valid_frame() ||
4831 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004832 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004833}
4834
4835
ager@chromium.org7c537e22008-10-16 08:43:32 +00004836void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004837#ifdef DEBUG
4838 int original_height = frame_->height();
4839#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004840 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004841 Comment cmnt(masm_, "[ CountOperation");
4842
4843 bool is_postfix = node->is_postfix();
4844 bool is_increment = node->op() == Token::INC;
4845
4846 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4847 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4848
4849 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004850 if (is_postfix) {
4851 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004852 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004853 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004854
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004855 // A constant reference is not saved to, so a constant reference is not a
4856 // compound assignment reference.
4857 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004858 if (target.is_illegal()) {
4859 // Spoof the virtual frame to have the expected height (one higher
4860 // than on entry).
4861 if (!is_postfix) {
4862 __ mov(r0, Operand(Smi::FromInt(0)));
4863 frame_->EmitPush(r0);
4864 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004865 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004866 return;
4867 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004868 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004869 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004870
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004871 JumpTarget slow;
4872 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004873
4874 // Load the value (1) into register r1.
4875 __ mov(r1, Operand(Smi::FromInt(1)));
4876
4877 // Check for smi operand.
4878 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004879 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004880
4881 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004882 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004883 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004884 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004885
4886 // Perform optimistic increment/decrement.
4887 if (is_increment) {
4888 __ add(r0, r0, Operand(r1), SetCC);
4889 } else {
4890 __ sub(r0, r0, Operand(r1), SetCC);
4891 }
4892
4893 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004894 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004895
4896 // Revert optimistic increment/decrement.
4897 if (is_increment) {
4898 __ sub(r0, r0, Operand(r1));
4899 } else {
4900 __ add(r0, r0, Operand(r1));
4901 }
4902
4903 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004904 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004905 {
4906 // Convert the operand to a number.
4907 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004908 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004909 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004910 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004911 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004912 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004913 }
4914
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004915 // Compute the new value.
4916 __ mov(r1, Operand(Smi::FromInt(1)));
4917 frame_->EmitPush(r0);
4918 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004919 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004920 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004921 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004922 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004923 }
4924
4925 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004926 exit.Bind();
4927 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004928 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004929 }
4930
4931 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004932 if (is_postfix) frame_->EmitPop(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004933 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004934}
4935
4936
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004937void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004938 // According to ECMA-262 section 11.11, page 58, the binary logical
4939 // operators must yield the result of one of the two expressions
4940 // before any ToBoolean() conversions. This means that the value
4941 // produced by a && or || operator is not necessarily a boolean.
4942
4943 // NOTE: If the left hand side produces a materialized value (not in
4944 // the CC register), we force the right hand side to do the
4945 // same. This is necessary because we may have to branch to the exit
4946 // after evaluating the left hand side (due to the shortcut
4947 // semantics), but the compiler must (statically) know if the result
4948 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004949 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004950 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004951 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004952 &is_true,
4953 false_target(),
4954 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004955 if (has_valid_frame() && !has_cc()) {
4956 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004957 JumpTarget pop_and_continue;
4958 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004959
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004960 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004961 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004962 // Avoid popping the result if it converts to 'false' using the
4963 // standard ToBoolean() conversion as described in ECMA-262,
4964 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004965 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004966 Branch(false, &exit);
4967
4968 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004969 pop_and_continue.Bind();
4970 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004971
4972 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004973 is_true.Bind();
4974 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004975
4976 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004977 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004978 } else if (has_cc() || is_true.is_linked()) {
4979 // The left-hand side is either (a) partially compiled to
4980 // control flow with a final branch left to emit or (b) fully
4981 // compiled to control flow and possibly true.
4982 if (has_cc()) {
4983 Branch(false, false_target());
4984 }
4985 is_true.Bind();
4986 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004987 true_target(),
4988 false_target(),
4989 false);
4990 } else {
4991 // Nothing to do.
4992 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004993 }
4994
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004995 } else {
4996 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004997 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004998 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004999 true_target(),
5000 &is_false,
5001 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005002 if (has_valid_frame() && !has_cc()) {
5003 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005004 JumpTarget pop_and_continue;
5005 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005006
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005007 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005008 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005009 // Avoid popping the result if it converts to 'true' using the
5010 // standard ToBoolean() conversion as described in ECMA-262,
5011 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005012 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005013 Branch(true, &exit);
5014
5015 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005016 pop_and_continue.Bind();
5017 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005018
5019 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005020 is_false.Bind();
5021 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005022
5023 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005024 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005025 } else if (has_cc() || is_false.is_linked()) {
5026 // The left-hand side is either (a) partially compiled to
5027 // control flow with a final branch left to emit or (b) fully
5028 // compiled to control flow and possibly false.
5029 if (has_cc()) {
5030 Branch(true, true_target());
5031 }
5032 is_false.Bind();
5033 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005034 true_target(),
5035 false_target(),
5036 false);
5037 } else {
5038 // Nothing to do.
5039 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005040 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005041 }
5042}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005043
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005044
5045void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5046#ifdef DEBUG
5047 int original_height = frame_->height();
5048#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005049 Comment cmnt(masm_, "[ BinaryOperation");
5050
5051 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005052 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005053 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005054 } else {
5055 // Optimize for the case where (at least) one of the expressions
5056 // is a literal small integer.
5057 Literal* lliteral = node->left()->AsLiteral();
5058 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005059 // NOTE: The code below assumes that the slow cases (calls to runtime)
5060 // never return a constant/immutable object.
5061 bool overwrite_left =
5062 (node->left()->AsBinaryOperation() != NULL &&
5063 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5064 bool overwrite_right =
5065 (node->right()->AsBinaryOperation() != NULL &&
5066 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005067
5068 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005069 VirtualFrame::RegisterAllocationScope scope(this);
5070 Load(node->left());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005071 SmiOperation(node->op(),
5072 rliteral->handle(),
5073 false,
5074 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005075 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005076 VirtualFrame::RegisterAllocationScope scope(this);
5077 Load(node->right());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005078 SmiOperation(node->op(),
5079 lliteral->handle(),
5080 true,
5081 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005082 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00005083 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005084 OverwriteMode overwrite_mode = NO_OVERWRITE;
5085 if (overwrite_left) {
5086 overwrite_mode = OVERWRITE_LEFT;
5087 } else if (overwrite_right) {
5088 overwrite_mode = OVERWRITE_RIGHT;
5089 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005090 Load(node->left());
5091 Load(node->right());
5092 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005093 }
5094 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005095 ASSERT(!has_valid_frame() ||
5096 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005097 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005098}
5099
5100
ager@chromium.org7c537e22008-10-16 08:43:32 +00005101void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005102#ifdef DEBUG
5103 int original_height = frame_->height();
5104#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00005105 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005106 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005107 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005108 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005109}
5110
5111
ager@chromium.org7c537e22008-10-16 08:43:32 +00005112void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005113#ifdef DEBUG
5114 int original_height = frame_->height();
5115#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005116 Comment cmnt(masm_, "[ CompareOperation");
5117
ager@chromium.org357bf652010-04-12 11:30:10 +00005118 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005120 // Get the expressions from the node.
5121 Expression* left = node->left();
5122 Expression* right = node->right();
5123 Token::Value op = node->op();
5124
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005125 // To make null checks efficient, we check if either left or right is the
5126 // literal 'null'. If so, we optimize the code by inlining a null check
5127 // instead of calling the (very) general runtime routine for checking
5128 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005129 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005130 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005131 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005132 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005133 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5134 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005135 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005136 Load(left_is_null ? right : left);
5137 Register tos = frame_->PopToRegister();
5138 // JumpTargets can't cope with register allocation yet.
5139 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005140 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005141 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005142
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005143 // The 'null' value is only equal to 'undefined' if using non-strict
5144 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005145 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005146 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005147
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005148 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005149 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005150 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005151
ager@chromium.org357bf652010-04-12 11:30:10 +00005152 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005153 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005154
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005155 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005156 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5157 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5158 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5159 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005160 }
5161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005162 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005163 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005164 return;
5165 }
5166 }
5167
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005168 // To make typeof testing for natives implemented in JavaScript really
5169 // efficient, we generate special code for expressions of the form:
5170 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005171 UnaryOperation* operation = left->AsUnaryOperation();
5172 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5173 (operation != NULL && operation->op() == Token::TYPEOF) &&
5174 (right->AsLiteral() != NULL &&
5175 right->AsLiteral()->handle()->IsString())) {
5176 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5177
ager@chromium.org357bf652010-04-12 11:30:10 +00005178 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005179 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005180 Register tos = frame_->PopToRegister();
5181
5182 // JumpTargets can't cope with register allocation yet.
5183 frame_->SpillAll();
5184
5185 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005186
5187 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005188 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005189 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005190 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005191 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005192 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005193 cc_reg_ = eq;
5194
5195 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005196 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005197 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005198
ager@chromium.org357bf652010-04-12 11:30:10 +00005199 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005200
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005201 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005202 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5203 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5204 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005205 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005206
ager@chromium.org357bf652010-04-12 11:30:10 +00005207 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5208 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005209 cc_reg_ = lt;
5210
5211 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005212 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005213 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005214 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005215 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005216 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005217 cc_reg_ = eq;
5218
5219 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005220 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005221 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005222 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005223
ager@chromium.org357bf652010-04-12 11:30:10 +00005224 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005225 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005226
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005227 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005228 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5229 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5230 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5231 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005232
5233 cc_reg_ = eq;
5234
5235 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005236 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005237 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005238 Register map_reg = scratch;
5239 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005240 true_target()->Branch(eq);
5241 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00005242 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005243 cc_reg_ = eq;
5244
5245 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005246 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005247 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005248
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005249 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005250 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005251 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005252
ager@chromium.org357bf652010-04-12 11:30:10 +00005253 Register map_reg = scratch;
5254 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005255 false_target()->Branch(eq);
5256
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005257 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005258 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5259 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5260 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005261 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005262
ager@chromium.org357bf652010-04-12 11:30:10 +00005263 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5264 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005265 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00005266 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005267 cc_reg_ = le;
5268
5269 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005270 // Uncommon case: typeof testing against a string literal that is
5271 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005272 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005273 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005274 ASSERT(!has_valid_frame() ||
5275 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005276 return;
5277 }
5278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005279 switch (op) {
5280 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005281 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005282 break;
5283
5284 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005285 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005286 break;
5287
5288 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005289 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005290 break;
5291
5292 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005293 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005294 break;
5295
5296 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005297 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005298 break;
5299
5300 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005301 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005302 break;
5303
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005304 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005305 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005306 LoadAndSpill(left);
5307 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005308 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005309 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005310 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005311 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005312
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005313 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005314 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005315 LoadAndSpill(left);
5316 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005317 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005318 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005319 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005320 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005321 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005322 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005324
5325 default:
5326 UNREACHABLE();
5327 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005328 ASSERT((has_cc() && frame_->height() == original_height) ||
5329 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005330}
5331
5332
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005333class DeferredReferenceGetNamedValue: public DeferredCode {
5334 public:
5335 explicit DeferredReferenceGetNamedValue(Handle<String> name) : name_(name) {
5336 set_comment("[ DeferredReferenceGetNamedValue");
5337 }
5338
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005339 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005340
5341 private:
5342 Handle<String> name_;
5343};
5344
5345
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005346void DeferredReferenceGetNamedValue::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005347 Register scratch1 = VirtualFrame::scratch0();
5348 Register scratch2 = VirtualFrame::scratch1();
5349 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
5350 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
5351
5352 // Setup the registers and call load IC.
5353 // On entry to this deferred code, r0 is assumed to already contain the
5354 // receiver from the top of the stack.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005355 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005356
5357 // The rest of the instructions in the deferred code must be together.
5358 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5359 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
5360 __ Call(ic, RelocInfo::CODE_TARGET);
5361 // The call must be followed by a nop(1) instruction to indicate that the
5362 // in-object has been inlined.
5363 __ nop(PROPERTY_ACCESS_INLINED);
5364
5365 // Block the constant pool for one more instruction after leaving this
5366 // constant pool block scope to include the branch instruction ending the
5367 // deferred code.
5368 __ BlockConstPoolFor(1);
5369 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005370}
5371
5372
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005373class DeferredReferenceGetKeyedValue: public DeferredCode {
5374 public:
5375 DeferredReferenceGetKeyedValue() {
5376 set_comment("[ DeferredReferenceGetKeyedValue");
5377 }
5378
5379 virtual void Generate();
5380};
5381
5382
5383void DeferredReferenceGetKeyedValue::Generate() {
5384 Register scratch1 = VirtualFrame::scratch0();
5385 Register scratch2 = VirtualFrame::scratch1();
5386 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
5387 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
5388
5389 // The rest of the instructions in the deferred code must be together.
5390 { Assembler::BlockConstPoolScope block_const_pool(masm_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005391 // Call keyed load IC. It has all arguments on the stack and the key in r0.
5392 __ ldr(r0, MemOperand(sp, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005393 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
5394 __ Call(ic, RelocInfo::CODE_TARGET);
5395 // The call must be followed by a nop instruction to indicate that the
5396 // keyed load has been inlined.
5397 __ nop(PROPERTY_ACCESS_INLINED);
5398
5399 // Block the constant pool for one more instruction after leaving this
5400 // constant pool block scope to include the branch instruction ending the
5401 // deferred code.
5402 __ BlockConstPoolFor(1);
5403 }
5404}
5405
5406
5407class DeferredReferenceSetKeyedValue: public DeferredCode {
5408 public:
5409 DeferredReferenceSetKeyedValue() {
5410 set_comment("[ DeferredReferenceSetKeyedValue");
5411 }
5412
5413 virtual void Generate();
5414};
5415
5416
5417void DeferredReferenceSetKeyedValue::Generate() {
5418 Register scratch1 = VirtualFrame::scratch0();
5419 Register scratch2 = VirtualFrame::scratch1();
5420 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
5421 __ IncrementCounter(
5422 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
5423
5424 // The rest of the instructions in the deferred code must be together.
5425 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5426 // Call keyed load IC. It has receiver amd key on the stack and the value to
5427 // store in r0.
5428 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
5429 __ Call(ic, RelocInfo::CODE_TARGET);
5430 // The call must be followed by a nop instruction to indicate that the
5431 // keyed store has been inlined.
5432 __ nop(PROPERTY_ACCESS_INLINED);
5433
5434 // Block the constant pool for one more instruction after leaving this
5435 // constant pool block scope to include the branch instruction ending the
5436 // deferred code.
5437 __ BlockConstPoolFor(1);
5438 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005439}
5440
5441
5442void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
5443 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
5444 Comment cmnt(masm(), "[ Load from named Property");
5445 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005446 frame_->CallLoadIC(name,
5447 is_contextual
5448 ? RelocInfo::CODE_TARGET_CONTEXT
5449 : RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005450 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005451 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005452 Comment cmnt(masm(), "[ Inlined named property load");
5453
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005454 // Counter will be decremented in the deferred code. Placed here to avoid
5455 // having it in the instruction stream below where patching will occur.
5456 __ IncrementCounter(&Counters::named_load_inline, 1,
5457 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005458
5459 // The following instructions are the inlined load of an in-object property.
5460 // Parts of this code is patched, so the exact instructions generated needs
5461 // to be fixed. Therefore the instruction pool is blocked when generating
5462 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005463
5464 // Load the receiver from the stack.
5465 frame_->SpillAllButCopyTOSToR0();
5466
5467 DeferredReferenceGetNamedValue* deferred =
5468 new DeferredReferenceGetNamedValue(name);
5469
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005470#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005471 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005472 Label check_inlined_codesize;
5473 masm_->bind(&check_inlined_codesize);
5474#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005475
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005476 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005477 // Check that the receiver is a heap object.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005478 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005479 deferred->Branch(eq);
5480
5481 // Check the map. The null map used below is patched by the inline cache
5482 // code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005483 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005484 __ mov(r3, Operand(Factory::null_value()));
5485 __ cmp(r2, r3);
5486 deferred->Branch(ne);
5487
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005488 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005489 // inline cache code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005490 __ ldr(r0, MemOperand(r0, 0));
5491
5492 // Make sure that the expected number of instructions are generated.
5493 ASSERT_EQ(kInlinedNamedLoadInstructions,
5494 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005495 }
5496
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005497 deferred->BindExit();
5498 }
5499}
5500
5501
ager@chromium.orgac091b72010-05-05 07:34:42 +00005502void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
5503#ifdef DEBUG
5504 int expected_height = frame_->height() - (is_contextual ? 1 : 2);
5505#endif
5506 frame_->CallStoreIC(name, is_contextual);
5507
5508 ASSERT_EQ(expected_height, frame_->height());
5509}
5510
5511
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005512void CodeGenerator::EmitKeyedLoad() {
5513 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005514 Comment cmnt(masm_, "[ Load from keyed property");
5515 frame_->CallKeyedLoadIC();
5516 } else {
5517 // Inline the keyed load.
5518 Comment cmnt(masm_, "[ Inlined load from keyed property");
5519
5520 // Counter will be decremented in the deferred code. Placed here to avoid
5521 // having it in the instruction stream below where patching will occur.
5522 __ IncrementCounter(&Counters::keyed_load_inline, 1,
5523 frame_->scratch0(), frame_->scratch1());
5524
ager@chromium.orgac091b72010-05-05 07:34:42 +00005525 // Load the receiver and key from the stack.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005526 frame_->SpillAllButCopyTOSToR1R0();
5527 Register receiver = r0;
5528 Register key = r1;
5529 VirtualFrame::SpilledScope spilled(frame_);
5530
5531 DeferredReferenceGetKeyedValue* deferred =
5532 new DeferredReferenceGetKeyedValue();
5533
5534 // Check that the receiver is a heap object.
5535 __ tst(receiver, Operand(kSmiTagMask));
5536 deferred->Branch(eq);
5537
5538 // The following instructions are the part of the inlined load keyed
5539 // property code which can be patched. Therefore the exact number of
5540 // instructions generated need to be fixed, so the constant pool is blocked
5541 // while generating this code.
5542#ifdef DEBUG
5543 int kInlinedKeyedLoadInstructions = 19;
5544 Label check_inlined_codesize;
5545 masm_->bind(&check_inlined_codesize);
5546#endif
5547 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5548 Register scratch1 = VirtualFrame::scratch0();
5549 Register scratch2 = VirtualFrame::scratch1();
5550 // Check the map. The null map used below is patched by the inline cache
5551 // code.
5552 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
5553 __ mov(scratch2, Operand(Factory::null_value()));
5554 __ cmp(scratch1, scratch2);
5555 deferred->Branch(ne);
5556
5557 // Check that the key is a smi.
5558 __ tst(key, Operand(kSmiTagMask));
5559 deferred->Branch(ne);
5560
5561 // Get the elements array from the receiver and check that it
5562 // is not a dictionary.
5563 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
5564 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
5565 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5566 __ cmp(scratch2, ip);
5567 deferred->Branch(ne);
5568
5569 // Check that key is within bounds. Use unsigned comparison to handle
5570 // negative keys.
5571 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
5572 __ cmp(scratch2, Operand(key, ASR, kSmiTagSize));
5573 deferred->Branch(ls); // Unsigned less equal.
5574
5575 // Load and check that the result is not the hole (key is a smi).
5576 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
5577 __ add(scratch1,
5578 scratch1,
5579 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5580 __ ldr(r0,
5581 MemOperand(scratch1, key, LSL,
5582 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5583 __ cmp(r0, scratch2);
5584 // This is the only branch to deferred where r0 and r1 do not contain the
5585 // receiver and key. We can't just load undefined here because we have to
5586 // check the prototype.
5587 deferred->Branch(eq);
5588
5589 // Make sure that the expected number of instructions are generated.
5590 ASSERT_EQ(kInlinedKeyedLoadInstructions,
5591 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5592 }
5593
5594 deferred->BindExit();
5595 }
5596}
5597
5598
5599void CodeGenerator::EmitKeyedStore(StaticType* key_type) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00005600 VirtualFrame::SpilledScope scope(frame_);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005601 // Generate inlined version of the keyed store if the code is in a loop
5602 // and the key is likely to be a smi.
5603 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
5604 // Inline the keyed store.
5605 Comment cmnt(masm_, "[ Inlined store to keyed property");
5606
5607 DeferredReferenceSetKeyedValue* deferred =
5608 new DeferredReferenceSetKeyedValue();
5609
5610 // Counter will be decremented in the deferred code. Placed here to avoid
5611 // having it in the instruction stream below where patching will occur.
5612 __ IncrementCounter(&Counters::keyed_store_inline, 1,
5613 frame_->scratch0(), frame_->scratch1());
5614
5615 // Check that the value is a smi. As this inlined code does not set the
5616 // write barrier it is only possible to store smi values.
5617 __ tst(r0, Operand(kSmiTagMask));
5618 deferred->Branch(ne);
5619
5620 // Load the key and receiver from the stack.
5621 __ ldr(r1, MemOperand(sp, 0));
5622 __ ldr(r2, MemOperand(sp, kPointerSize));
5623
5624 // Check that the key is a smi.
5625 __ tst(r1, Operand(kSmiTagMask));
5626 deferred->Branch(ne);
5627
5628 // Check that the receiver is a heap object.
5629 __ tst(r2, Operand(kSmiTagMask));
5630 deferred->Branch(eq);
5631
5632 // Check that the receiver is a JSArray.
5633 __ CompareObjectType(r2, r3, r3, JS_ARRAY_TYPE);
5634 deferred->Branch(ne);
5635
5636 // Check that the key is within bounds. Both the key and the length of
5637 // the JSArray are smis. Use unsigned comparison to handle negative keys.
5638 __ ldr(r3, FieldMemOperand(r2, JSArray::kLengthOffset));
5639 __ cmp(r3, r1);
5640 deferred->Branch(ls); // Unsigned less equal.
5641
5642 // The following instructions are the part of the inlined store keyed
5643 // property code which can be patched. Therefore the exact number of
5644 // instructions generated need to be fixed, so the constant pool is blocked
5645 // while generating this code.
5646#ifdef DEBUG
5647 int kInlinedKeyedStoreInstructions = 7;
5648 Label check_inlined_codesize;
5649 masm_->bind(&check_inlined_codesize);
5650#endif
5651 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5652 // Get the elements array from the receiver and check that it
5653 // is not a dictionary.
5654 __ ldr(r3, FieldMemOperand(r2, JSObject::kElementsOffset));
5655 __ ldr(r4, FieldMemOperand(r3, JSObject::kMapOffset));
5656 // Read the fixed array map from the constant pool (not from the root
5657 // array) so that the value can be patched. When debugging, we patch this
5658 // comparison to always fail so that we will hit the IC call in the
5659 // deferred code which will allow the debugger to break for fast case
5660 // stores.
5661 __ mov(r5, Operand(Factory::fixed_array_map()));
5662 __ cmp(r4, r5);
5663 deferred->Branch(ne);
5664
5665 // Store the value.
5666 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5667 __ str(r0, MemOperand(r3, r1, LSL,
5668 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5669
5670 // Make sure that the expected number of instructions are generated.
5671 ASSERT_EQ(kInlinedKeyedStoreInstructions,
5672 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5673 }
5674
5675 deferred->BindExit();
5676 } else {
5677 frame()->CallKeyedStoreIC();
5678 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005679}
5680
5681
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005682#ifdef DEBUG
5683bool CodeGenerator::HasValidEntryRegisters() { return true; }
5684#endif
5685
5686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005687#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005688#define __ ACCESS_MASM(masm)
5689
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690
ager@chromium.org7c537e22008-10-16 08:43:32 +00005691Handle<String> Reference::GetName() {
5692 ASSERT(type_ == NAMED);
5693 Property* property = expression_->AsProperty();
5694 if (property == NULL) {
5695 // Global variable reference treated as a named property reference.
5696 VariableProxy* proxy = expression_->AsVariableProxy();
5697 ASSERT(proxy->AsVariable() != NULL);
5698 ASSERT(proxy->AsVariable()->is_global());
5699 return proxy->name();
5700 } else {
5701 Literal* raw_name = property->key()->AsLiteral();
5702 ASSERT(raw_name != NULL);
5703 return Handle<String>(String::cast(*raw_name->handle()));
5704 }
5705}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005706
ager@chromium.org7c537e22008-10-16 08:43:32 +00005707
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005708void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005709 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005710 ASSERT(!is_illegal());
5711 ASSERT(!cgen_->has_cc());
5712 MacroAssembler* masm = cgen_->masm();
5713 Property* property = expression_->AsProperty();
5714 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005715 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005716 }
5717
5718 switch (type_) {
5719 case SLOT: {
5720 Comment cmnt(masm, "[ Load from Slot");
5721 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
5722 ASSERT(slot != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005723 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005724 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005725 }
5726
ager@chromium.org7c537e22008-10-16 08:43:32 +00005727 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00005728 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005729 bool is_global = var != NULL;
5730 ASSERT(!is_global || var->is_global());
5731 cgen_->EmitNamedLoad(GetName(), is_global);
5732 cgen_->frame()->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005733 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005734 }
5735
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005736 case KEYED: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005737 ASSERT(property != NULL);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005738 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005739 cgen_->frame()->EmitPush(r0);
5740 break;
5741 }
5742
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005743 default:
5744 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005745 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005746
5747 if (!persist_after_get_) {
5748 cgen_->UnloadReference(this);
5749 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005750}
5751
5752
ager@chromium.org7c537e22008-10-16 08:43:32 +00005753void Reference::SetValue(InitState init_state) {
5754 ASSERT(!is_illegal());
5755 ASSERT(!cgen_->has_cc());
5756 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005757 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005758 Property* property = expression_->AsProperty();
5759 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005760 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005761 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005762
ager@chromium.org7c537e22008-10-16 08:43:32 +00005763 switch (type_) {
5764 case SLOT: {
5765 Comment cmnt(masm, "[ Store to Slot");
5766 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005767 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005768 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005769 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005770 }
5771
ager@chromium.org7c537e22008-10-16 08:43:32 +00005772 case NAMED: {
5773 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00005774 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005775 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005776 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005777 break;
5778 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005779
ager@chromium.org7c537e22008-10-16 08:43:32 +00005780 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005781 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005782 Comment cmnt(masm, "[ Store to keyed Property");
5783 Property* property = expression_->AsProperty();
5784 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005785 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005786
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005787 frame->EmitPop(r0); // Value.
5788 cgen_->EmitKeyedStore(property->key()->type());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005789 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005790 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005791 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005792 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005793
5794 default:
5795 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005796 }
5797}
5798
5799
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005800void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005801 // Create a new closure from the given function info in new
5802 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005803 Label gc;
5804
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005805 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005806 __ pop(r3);
5807
5808 // Attempt to allocate new JSFunction in new space.
5809 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
5810 r0,
5811 r1,
5812 r2,
5813 &gc,
5814 TAG_OBJECT);
5815
5816 // Compute the function map in the current global context and set that
5817 // as the map of the allocated object.
5818 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5819 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5820 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5821 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5822
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005823 // Initialize the rest of the function. We don't have to update the
5824 // write barrier because the allocated object is in new space.
5825 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5826 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5827 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5828 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5829 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5830 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5831 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5832 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005833
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005834 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005835 __ Ret();
5836
5837 // Create a new closure through the slower runtime call.
5838 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005839 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005840 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005841}
5842
5843
5844void FastNewContextStub::Generate(MacroAssembler* masm) {
5845 // Try to allocate the context in new space.
5846 Label gc;
5847 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5848
5849 // Attempt to allocate the context in new space.
5850 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
5851 r0,
5852 r1,
5853 r2,
5854 &gc,
5855 TAG_OBJECT);
5856
5857 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005858 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005859
5860 // Setup the object header.
5861 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5862 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5863 __ mov(r2, Operand(length));
5864 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5865
5866 // Setup the fixed slots.
5867 __ mov(r1, Operand(Smi::FromInt(0)));
5868 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5869 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5870 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5871 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5872
5873 // Copy the global object from the surrounding context.
5874 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5875 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5876
5877 // Initialize the rest of the slots to undefined.
5878 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5879 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5880 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5881 }
5882
5883 // Remove the on-stack argument and return.
5884 __ mov(cp, r0);
5885 __ pop();
5886 __ Ret();
5887
5888 // Need to collect. Call into runtime system.
5889 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005890 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005891}
5892
5893
ager@chromium.org5c838252010-02-19 08:53:10 +00005894void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5895 // Stack layout on entry:
5896 //
5897 // [sp]: constant elements.
5898 // [sp + kPointerSize]: literal index.
5899 // [sp + (2 * kPointerSize)]: literals array.
5900
5901 // All sizes here are multiples of kPointerSize.
5902 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5903 int size = JSArray::kSize + elements_size;
5904
5905 // Load boilerplate object into r3 and check if we need to create a
5906 // boilerplate.
5907 Label slow_case;
5908 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5909 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5910 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5911 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5912 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5913 __ cmp(r3, ip);
5914 __ b(eq, &slow_case);
5915
5916 // Allocate both the JS array and the elements array in one big
5917 // allocation. This avoids multiple limit checks.
5918 __ AllocateInNewSpace(size / kPointerSize,
5919 r0,
5920 r1,
5921 r2,
5922 &slow_case,
5923 TAG_OBJECT);
5924
5925 // Copy the JS array part.
5926 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5927 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5928 __ ldr(r1, FieldMemOperand(r3, i));
5929 __ str(r1, FieldMemOperand(r0, i));
5930 }
5931 }
5932
5933 if (length_ > 0) {
5934 // Get hold of the elements array of the boilerplate and setup the
5935 // elements pointer in the resulting object.
5936 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5937 __ add(r2, r0, Operand(JSArray::kSize));
5938 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
5939
5940 // Copy the elements array.
5941 for (int i = 0; i < elements_size; i += kPointerSize) {
5942 __ ldr(r1, FieldMemOperand(r3, i));
5943 __ str(r1, FieldMemOperand(r2, i));
5944 }
5945 }
5946
5947 // Return and remove the on-stack parameters.
5948 __ add(sp, sp, Operand(3 * kPointerSize));
5949 __ Ret();
5950
5951 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005952 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005953}
5954
5955
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005956// Takes a Smi and converts to an IEEE 64 bit floating point value in two
5957// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
5958// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
5959// scratch register. Destroys the source register. No GC occurs during this
5960// stub so you don't have to set up the frame.
5961class ConvertToDoubleStub : public CodeStub {
5962 public:
5963 ConvertToDoubleStub(Register result_reg_1,
5964 Register result_reg_2,
5965 Register source_reg,
5966 Register scratch_reg)
5967 : result1_(result_reg_1),
5968 result2_(result_reg_2),
5969 source_(source_reg),
5970 zeros_(scratch_reg) { }
5971
5972 private:
5973 Register result1_;
5974 Register result2_;
5975 Register source_;
5976 Register zeros_;
5977
5978 // Minor key encoding in 16 bits.
5979 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
5980 class OpBits: public BitField<Token::Value, 2, 14> {};
5981
5982 Major MajorKey() { return ConvertToDouble; }
5983 int MinorKey() {
5984 // Encode the parameters in a unique 16 bit value.
5985 return result1_.code() +
5986 (result2_.code() << 4) +
5987 (source_.code() << 8) +
5988 (zeros_.code() << 12);
5989 }
5990
5991 void Generate(MacroAssembler* masm);
5992
5993 const char* GetName() { return "ConvertToDoubleStub"; }
5994
5995#ifdef DEBUG
5996 void Print() { PrintF("ConvertToDoubleStub\n"); }
5997#endif
5998};
5999
6000
6001void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
6002#ifndef BIG_ENDIAN_FLOATING_POINT
6003 Register exponent = result1_;
6004 Register mantissa = result2_;
6005#else
6006 Register exponent = result2_;
6007 Register mantissa = result1_;
6008#endif
6009 Label not_special;
6010 // Convert from Smi to integer.
6011 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
6012 // Move sign bit from source to destination. This works because the sign bit
6013 // in the exponent word of the double has the same position and polarity as
6014 // the 2's complement sign bit in a Smi.
6015 ASSERT(HeapNumber::kSignMask == 0x80000000u);
6016 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
6017 // Subtract from 0 if source was negative.
6018 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006019
6020 // We have -1, 0 or 1, which we treat specially. Register source_ contains
6021 // absolute value: it is either equal to 1 (special case of -1 and 1),
6022 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006023 __ cmp(source_, Operand(1));
6024 __ b(gt, &not_special);
6025
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006026 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
6027 static const uint32_t exponent_word_for_1 =
6028 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006029 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006030 // 1, 0 and -1 all have 0 for the second word.
6031 __ mov(mantissa, Operand(0));
6032 __ Ret();
6033
6034 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006035 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006036 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006037 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006038 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006039 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006040 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
6041 __ orr(exponent,
6042 exponent,
6043 Operand(mantissa, LSL, HeapNumber::kExponentShift));
6044 // Shift up the source chopping the top bit off.
6045 __ add(zeros_, zeros_, Operand(1));
6046 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
6047 __ mov(source_, Operand(source_, LSL, zeros_));
6048 // Compute lower part of fraction (last 12 bits).
6049 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
6050 // And the top (top 20 bits).
6051 __ orr(exponent,
6052 exponent,
6053 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
6054 __ Ret();
6055}
6056
6057
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006058// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006059void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006060 Label max_negative_int;
6061 // the_int_ has the answer which is a signed int32 but not a Smi.
6062 // We test for the special value that has a different exponent. This test
6063 // has the neat side effect of setting the flags according to the sign.
6064 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006065 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006066 __ b(eq, &max_negative_int);
6067 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
6068 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
6069 uint32_t non_smi_exponent =
6070 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6071 __ mov(scratch_, Operand(non_smi_exponent));
6072 // Set the sign bit in scratch_ if the value was negative.
6073 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
6074 // Subtract from 0 if the value was negative.
6075 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
6076 // We should be masking the implict first digit of the mantissa away here,
6077 // but it just ends up combining harmlessly with the last digit of the
6078 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
6079 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
6080 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
6081 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6082 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
6083 __ str(scratch_, FieldMemOperand(the_heap_number_,
6084 HeapNumber::kExponentOffset));
6085 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
6086 __ str(scratch_, FieldMemOperand(the_heap_number_,
6087 HeapNumber::kMantissaOffset));
6088 __ Ret();
6089
6090 __ bind(&max_negative_int);
6091 // The max negative int32 is stored as a positive number in the mantissa of
6092 // a double because it uses a sign bit instead of using two's complement.
6093 // The actual mantissa bits stored are all 0 because the implicit most
6094 // significant 1 bit is not stored.
6095 non_smi_exponent += 1 << HeapNumber::kExponentShift;
6096 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
6097 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
6098 __ mov(ip, Operand(0));
6099 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
6100 __ Ret();
6101}
6102
6103
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006104// Handle the case where the lhs and rhs are the same object.
6105// Equality is almost reflexive (everything but NaN), so this is a test
6106// for "identity and not NaN".
6107static void EmitIdenticalObjectComparison(MacroAssembler* masm,
6108 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006109 Condition cc,
6110 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006111 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006112 Label heap_number, return_equal;
6113 Register exp_mask_reg = r5;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006114 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006115 __ b(ne, &not_identical);
6116
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006117 // The two objects are identical. If we know that one of them isn't NaN then
6118 // we now know they test equal.
6119 if (cc != eq || !never_nan_nan) {
6120 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006121
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006122 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
6123 // so we do the second best thing - test it ourselves.
6124 // They are both equal and they are not both Smis so both of them are not
6125 // Smis. If it's not a heap number, then return equal.
6126 if (cc == lt || cc == gt) {
6127 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006128 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006129 } else {
6130 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6131 __ b(eq, &heap_number);
6132 // Comparing JS objects with <=, >= is complicated.
6133 if (cc != eq) {
6134 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
6135 __ b(ge, slow);
6136 // Normally here we fall through to return_equal, but undefined is
6137 // special: (undefined == undefined) == true, but
6138 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
6139 if (cc == le || cc == ge) {
6140 __ cmp(r4, Operand(ODDBALL_TYPE));
6141 __ b(ne, &return_equal);
6142 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006143 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006144 __ b(ne, &return_equal);
6145 if (cc == le) {
6146 // undefined <= undefined should fail.
6147 __ mov(r0, Operand(GREATER));
6148 } else {
6149 // undefined >= undefined should fail.
6150 __ mov(r0, Operand(LESS));
6151 }
6152 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006153 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006154 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006155 }
6156 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006157
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006158 __ bind(&return_equal);
6159 if (cc == lt) {
6160 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
6161 } else if (cc == gt) {
6162 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
6163 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006164 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006165 }
6166 __ mov(pc, Operand(lr)); // Return.
6167
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006168 if (cc != eq || !never_nan_nan) {
6169 // For less and greater we don't have to check for NaN since the result of
6170 // x < x is false regardless. For the others here is some code to check
6171 // for NaN.
6172 if (cc != lt && cc != gt) {
6173 __ bind(&heap_number);
6174 // It is a heap number, so return non-equal if it's NaN and equal if it's
6175 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006176
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006177 // The representation of NaN values has all exponent bits (52..62) set,
6178 // and not all mantissa bits (0..51) clear.
6179 // Read top bits of double representation (second word of value).
6180 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6181 // Test that exponent bits are all set.
6182 __ and_(r3, r2, Operand(exp_mask_reg));
6183 __ cmp(r3, Operand(exp_mask_reg));
6184 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006185
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006186 // Shift out flag and all exponent bits, retaining only mantissa.
6187 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
6188 // Or with all low-bits of mantissa.
6189 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6190 __ orr(r0, r3, Operand(r2), SetCC);
6191 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006192 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
6193 // not (it's a NaN). For <= and >= we need to load r0 with the failing
6194 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006195 if (cc != eq) {
6196 // All-zero means Infinity means equal.
6197 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
6198 if (cc == le) {
6199 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
6200 } else {
6201 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
6202 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006203 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006204 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006205 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006206 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006207 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006208
6209 __ bind(&not_identical);
6210}
6211
6212
6213// See comment at call site.
6214static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006215 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006216 Label* slow,
6217 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006218 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006219 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006220 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006221
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006222 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006223 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6224 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006225 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006226 // succeed. Return non-equal (r0 is already not zero)
6227 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6228 } else {
6229 // Smi compared non-strictly with a non-Smi non-heap-number. Call
6230 // the runtime.
6231 __ b(ne, slow);
6232 }
6233
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006234 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006235 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006236 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006237 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006238 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6239 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006240 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006241 // Load the double from rhs, tagged HeapNumber r0, to d6.
6242 __ sub(r7, r0, Operand(kHeapObjectTag));
6243 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006244 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006245 __ push(lr);
6246 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006247 __ mov(r7, Operand(r1));
6248 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6249 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006250 // Load rhs to a double in r0, r1.
6251 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
6252 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
6253 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006254 }
6255
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006256 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006257 // since it's a smi.
6258 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006259
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006260 __ bind(&rhs_is_smi);
6261 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006262 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6263 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006264 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006265 // succeed. Return non-equal.
6266 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
6267 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6268 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006269 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006270 // the runtime.
6271 __ b(ne, slow);
6272 }
6273
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006274 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006275 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006276 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006277 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006278 // Load the double from lhs, tagged HeapNumber r1, to d7.
6279 __ sub(r7, r1, Operand(kHeapObjectTag));
6280 __ vldr(d7, r7, HeapNumber::kValueOffset);
6281 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6282 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006283 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006284 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006285 __ push(lr);
6286 // Load lhs to a double in r2, r3.
6287 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6288 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6289 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006290 __ mov(r7, Operand(r0));
6291 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6292 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006293 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006294 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006295 // Fall through to both_loaded_as_doubles.
6296}
6297
6298
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006299void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006300 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006301 Register rhs_exponent = exp_first ? r0 : r1;
6302 Register lhs_exponent = exp_first ? r2 : r3;
6303 Register rhs_mantissa = exp_first ? r1 : r0;
6304 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006305 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006306 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006307
6308 Register exp_mask_reg = r5;
6309
6310 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006311 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
6312 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006313 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006314 __ mov(r4,
6315 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6316 SetCC);
6317 __ b(ne, &one_is_nan);
6318 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006319 __ b(ne, &one_is_nan);
6320
6321 __ bind(lhs_not_nan);
6322 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
6323 __ bind(&lhs_not_nan_exp_mask_is_loaded);
6324 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
6325 __ cmp(r4, Operand(exp_mask_reg));
6326 __ b(ne, &neither_is_nan);
6327 __ mov(r4,
6328 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6329 SetCC);
6330 __ b(ne, &one_is_nan);
6331 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006332 __ b(eq, &neither_is_nan);
6333
6334 __ bind(&one_is_nan);
6335 // NaN comparisons always fail.
6336 // Load whatever we need in r0 to make the comparison fail.
6337 if (cc == lt || cc == le) {
6338 __ mov(r0, Operand(GREATER));
6339 } else {
6340 __ mov(r0, Operand(LESS));
6341 }
6342 __ mov(pc, Operand(lr)); // Return.
6343
6344 __ bind(&neither_is_nan);
6345}
6346
6347
6348// See comment at call site.
6349static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
6350 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006351 Register rhs_exponent = exp_first ? r0 : r1;
6352 Register lhs_exponent = exp_first ? r2 : r3;
6353 Register rhs_mantissa = exp_first ? r1 : r0;
6354 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006355
6356 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
6357 if (cc == eq) {
6358 // Doubles are not equal unless they have the same bit pattern.
6359 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006360 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
6361 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006362 // Return non-zero if the numbers are unequal.
6363 __ mov(pc, Operand(lr), LeaveCC, ne);
6364
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006365 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006366 // If exponents are equal then return 0.
6367 __ mov(pc, Operand(lr), LeaveCC, eq);
6368
6369 // Exponents are unequal. The only way we can return that the numbers
6370 // are equal is if one is -0 and the other is 0. We already dealt
6371 // with the case where both are -0 or both are 0.
6372 // We start by seeing if the mantissas (that are equal) or the bottom
6373 // 31 bits of the rhs exponent are non-zero. If so we return not
6374 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006375 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006376 __ mov(r0, Operand(r4), LeaveCC, ne);
6377 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
6378 // Now they are equal if and only if the lhs exponent is zero in its
6379 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006380 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006381 __ mov(pc, Operand(lr));
6382 } else {
6383 // Call a native function to do a comparison between two non-NaNs.
6384 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006385 __ push(lr);
6386 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
6387 __ CallCFunction(ExternalReference::compare_doubles(), 4);
6388 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006389 }
6390}
6391
6392
6393// See comment at call site.
6394static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
6395 // If either operand is a JSObject or an oddball value, then they are
6396 // not equal since their pointers are different.
6397 // There is no test for undetectability in strict equality.
6398 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
6399 Label first_non_object;
6400 // Get the type of the first operand into r2 and compare it with
6401 // FIRST_JS_OBJECT_TYPE.
6402 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
6403 __ b(lt, &first_non_object);
6404
6405 // Return non-zero (r0 is not zero)
6406 Label return_not_equal;
6407 __ bind(&return_not_equal);
6408 __ mov(pc, Operand(lr)); // Return.
6409
6410 __ bind(&first_non_object);
6411 // Check for oddballs: true, false, null, undefined.
6412 __ cmp(r2, Operand(ODDBALL_TYPE));
6413 __ b(eq, &return_not_equal);
6414
6415 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
6416 __ b(ge, &return_not_equal);
6417
6418 // Check for oddballs: true, false, null, undefined.
6419 __ cmp(r3, Operand(ODDBALL_TYPE));
6420 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006421
6422 // Now that we have the types we might as well check for symbol-symbol.
6423 // Ensure that no non-strings have the symbol bit set.
6424 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6425 ASSERT(kSymbolTag != 0);
6426 __ and_(r2, r2, Operand(r3));
6427 __ tst(r2, Operand(kIsSymbolMask));
6428 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006429}
6430
6431
6432// See comment at call site.
6433static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
6434 Label* both_loaded_as_doubles,
6435 Label* not_heap_numbers,
6436 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006437 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006438 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006439 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
6440 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006441 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
6442
6443 // Both are heap numbers. Load them up then jump to the code we have
6444 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006445 if (CpuFeatures::IsSupported(VFP3)) {
6446 CpuFeatures::Scope scope(VFP3);
6447 __ sub(r7, r0, Operand(kHeapObjectTag));
6448 __ vldr(d6, r7, HeapNumber::kValueOffset);
6449 __ sub(r7, r1, Operand(kHeapObjectTag));
6450 __ vldr(d7, r7, HeapNumber::kValueOffset);
6451 } else {
6452 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6453 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
6454 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
6455 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
6456 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006457 __ jmp(both_loaded_as_doubles);
6458}
6459
6460
6461// Fast negative check for symbol-to-symbol equality.
6462static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
6463 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006464 // Ensure that no non-strings have the symbol bit set.
6465 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6466 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006467 __ tst(r2, Operand(kIsSymbolMask));
6468 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006469 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
6470 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006471 __ tst(r3, Operand(kIsSymbolMask));
6472 __ b(eq, slow);
6473
6474 // Both are symbols. We already checked they weren't the same pointer
6475 // so they are not equal.
6476 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
6477 __ mov(pc, Operand(lr)); // Return.
6478}
6479
6480
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006481void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
6482 Register object,
6483 Register result,
6484 Register scratch1,
6485 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006486 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006487 bool object_is_smi,
6488 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006489 // Use of registers. Register result is used as a temporary.
6490 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006491 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006492
6493 // Load the number string cache.
6494 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
6495
6496 // Make the hash mask from the length of the number string cache. It
6497 // contains two elements (number and string) for each cache entry.
6498 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
6499 // Divide length by two (length is not a smi).
6500 __ mov(mask, Operand(mask, ASR, 1));
6501 __ sub(mask, mask, Operand(1)); // Make mask.
6502
6503 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006504 // number string cache for smis is just the smi value, and the hash for
6505 // doubles is the xor of the upper and lower words. See
6506 // Heap::GetNumberStringCache.
6507 Label is_smi;
6508 Label load_result_from_cache;
6509 if (!object_is_smi) {
6510 __ BranchOnSmi(object, &is_smi);
6511 if (CpuFeatures::IsSupported(VFP3)) {
6512 CpuFeatures::Scope scope(VFP3);
6513 __ CheckMap(object,
6514 scratch1,
6515 Factory::heap_number_map(),
6516 not_found,
6517 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006518
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006519 ASSERT_EQ(8, kDoubleSize);
6520 __ add(scratch1,
6521 object,
6522 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
6523 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
6524 __ eor(scratch1, scratch1, Operand(scratch2));
6525 __ and_(scratch1, scratch1, Operand(mask));
6526
6527 // Calculate address of entry in string cache: each entry consists
6528 // of two pointer sized fields.
6529 __ add(scratch1,
6530 number_string_cache,
6531 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
6532
6533 Register probe = mask;
6534 __ ldr(probe,
6535 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
6536 __ BranchOnSmi(probe, not_found);
6537 __ sub(scratch2, object, Operand(kHeapObjectTag));
6538 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
6539 __ sub(probe, probe, Operand(kHeapObjectTag));
6540 __ vldr(d1, probe, HeapNumber::kValueOffset);
6541 __ vcmp(d0, d1);
6542 __ vmrs(pc);
6543 __ b(ne, not_found); // The cache did not contain this value.
6544 __ b(&load_result_from_cache);
6545 } else {
6546 __ b(not_found);
6547 }
6548 }
6549
6550 __ bind(&is_smi);
6551 Register scratch = scratch1;
6552 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006553 // Calculate address of entry in string cache: each entry consists
6554 // of two pointer sized fields.
6555 __ add(scratch,
6556 number_string_cache,
6557 Operand(scratch, LSL, kPointerSizeLog2 + 1));
6558
6559 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006560 Register probe = mask;
6561 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
6562 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006563 __ b(ne, not_found);
6564
6565 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006566 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006567 __ ldr(result,
6568 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006569 __ IncrementCounter(&Counters::number_to_string_native,
6570 1,
6571 scratch1,
6572 scratch2);
6573}
6574
6575
6576void NumberToStringStub::Generate(MacroAssembler* masm) {
6577 Label runtime;
6578
6579 __ ldr(r1, MemOperand(sp, 0));
6580
6581 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006582 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006583 __ add(sp, sp, Operand(1 * kPointerSize));
6584 __ Ret();
6585
6586 __ bind(&runtime);
6587 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006588 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006589}
6590
6591
ager@chromium.orgac091b72010-05-05 07:34:42 +00006592void RecordWriteStub::Generate(MacroAssembler* masm) {
6593 __ RecordWriteHelper(object_, offset_, scratch_);
6594 __ Ret();
6595}
6596
6597
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006598// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
6599// On exit r0 is 0, positive or negative to indicate the result of
6600// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006601void CompareStub::Generate(MacroAssembler* masm) {
6602 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006603 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006604
6605 // NOTICE! This code is only reached after a smi-fast-case check, so
6606 // it is certain that at least one operand isn't a smi.
6607
6608 // Handle the case where the objects are identical. Either returns the answer
6609 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006610 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006611
6612 // If either is a Smi (we know that not both are), then they can only
6613 // be strictly equal if the other is a HeapNumber.
6614 ASSERT_EQ(0, kSmiTag);
6615 ASSERT_EQ(0, Smi::FromInt(0));
6616 __ and_(r2, r0, Operand(r1));
6617 __ tst(r2, Operand(kSmiTagMask));
6618 __ b(ne, &not_smis);
6619 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
6620 // 1) Return the answer.
6621 // 2) Go to slow.
6622 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006623 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006624 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006625 // comparison. If VFP3 is supported the double values of the numbers have
6626 // been loaded into d7 and d6. Otherwise, the double values have been loaded
6627 // into r0, r1, r2, and r3.
6628 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006629
6630 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006631 // The arguments have been converted to doubles and stored in d6 and d7, if
6632 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006633 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006634 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006635 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006636 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006637 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006638 __ vcmp(d7, d6);
6639 __ vmrs(pc); // Move vector status bits to normal status bits.
6640 Label nan;
6641 __ b(vs, &nan);
6642 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6643 __ mov(r0, Operand(LESS), LeaveCC, lt);
6644 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6645 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006646
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006647 __ bind(&nan);
6648 // If one of the sides was a NaN then the v flag is set. Load r0 with
6649 // whatever it takes to make the comparison fail, since comparisons with NaN
6650 // always fail.
6651 if (cc_ == lt || cc_ == le) {
6652 __ mov(r0, Operand(GREATER));
6653 } else {
6654 __ mov(r0, Operand(LESS));
6655 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006656 __ mov(pc, Operand(lr));
6657 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006658 // Checks for NaN in the doubles we have loaded. Can return the answer or
6659 // fall through if neither is a NaN. Also binds lhs_not_nan.
6660 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006661 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
6662 // answer. Never falls through.
6663 EmitTwoNonNanDoubleComparison(masm, cc_);
6664 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006665
6666 __ bind(&not_smis);
6667 // At this point we know we are dealing with two different objects,
6668 // and neither of them is a Smi. The objects are in r0 and r1.
6669 if (strict_) {
6670 // This returns non-equal for some object types, or falls through if it
6671 // was not lucky.
6672 EmitStrictTwoHeapObjectCompare(masm);
6673 }
6674
6675 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006676 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006677 // Check for heap-number-heap-number comparison. Can jump to slow case,
6678 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
6679 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006680 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006681 EmitCheckForTwoHeapNumbers(masm,
6682 &both_loaded_as_doubles,
6683 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006684 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006685
6686 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006687 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
6688 // symbols.
6689 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006690 // Either jumps to slow or returns the answer. Assumes that r2 is the type
6691 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006692 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006693 }
6694
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006695 // Check for both being sequential ASCII strings, and inline if that is the
6696 // case.
6697 __ bind(&flat_string_check);
6698
6699 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
6700
6701 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
6702 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
6703 r1,
6704 r0,
6705 r2,
6706 r3,
6707 r4,
6708 r5);
6709 // Never falls through to here.
6710
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006711 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006712
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006713 __ Push(r1, r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006714 // Figure out which native to call and setup the arguments.
6715 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006716 if (cc_ == eq) {
6717 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
6718 } else {
6719 native = Builtins::COMPARE;
6720 int ncr; // NaN compare result
6721 if (cc_ == lt || cc_ == le) {
6722 ncr = GREATER;
6723 } else {
6724 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
6725 ncr = LESS;
6726 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006727 __ mov(r0, Operand(Smi::FromInt(ncr)));
6728 __ push(r0);
6729 }
6730
6731 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
6732 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006733 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006734}
6735
6736
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00006737// We fall into this code if the operands were Smis, but the result was
6738// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006739// the operands were not both Smi. The operands are in r0 and r1. In order
6740// to call the C-implemented binary fp operation routines we need to end up
6741// with the double precision floating point operands in r0 and r1 (for the
6742// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00006743void GenericBinaryOpStub::HandleBinaryOpSlowCases(
6744 MacroAssembler* masm,
6745 Label* not_smi,
6746 Register lhs,
6747 Register rhs,
6748 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006749 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00006750 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006751
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006752 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ager@chromium.org357bf652010-04-12 11:30:10 +00006753
6754 if (ShouldGenerateSmiCode()) {
6755 // Smi-smi case (overflow).
6756 // Since both are Smis there is no heap number to overwrite, so allocate.
6757 // The new heap number is in r5. r6 and r7 are scratch.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006758 __ AllocateHeapNumber(r5, r6, r7, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006759
6760 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
6761 // using registers d7 and d6 for the double values.
6762 if (use_fp_registers) {
6763 CpuFeatures::Scope scope(VFP3);
6764 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
6765 __ vmov(s15, r7);
6766 __ vcvt_f64_s32(d7, s15);
6767 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
6768 __ vmov(s13, r7);
6769 __ vcvt_f64_s32(d6, s13);
6770 } else {
6771 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
6772 __ mov(r7, Operand(rhs));
6773 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6774 __ push(lr);
6775 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
6776 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
6777 __ mov(r7, Operand(lhs));
6778 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6779 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
6780 __ pop(lr);
6781 }
6782 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006783 }
6784
ager@chromium.org357bf652010-04-12 11:30:10 +00006785 // We branch here if at least one of r0 and r1 is not a Smi.
6786 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006787
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006788 // After this point we have the left hand side in r1 and the right hand side
6789 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006790 if (lhs.is(r0)) {
6791 __ Swap(r0, r1, ip);
6792 }
6793
6794 if (ShouldGenerateFPCode()) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006795 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
6796
ager@chromium.org357bf652010-04-12 11:30:10 +00006797 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
6798 switch (op_) {
6799 case Token::ADD:
6800 case Token::SUB:
6801 case Token::MUL:
6802 case Token::DIV:
6803 GenerateTypeTransition(masm);
6804 break;
6805
6806 default:
6807 break;
6808 }
6809 }
6810
6811 if (mode_ == NO_OVERWRITE) {
6812 // In the case where there is no chance of an overwritable float we may as
6813 // well do the allocation immediately while r0 and r1 are untouched.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006814 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006815 }
6816
6817 // Move r0 to a double in r2-r3.
6818 __ tst(r0, Operand(kSmiTagMask));
6819 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
6820 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6821 __ b(ne, &slow);
6822 if (mode_ == OVERWRITE_RIGHT) {
6823 __ mov(r5, Operand(r0)); // Overwrite this heap number.
6824 }
6825 if (use_fp_registers) {
6826 CpuFeatures::Scope scope(VFP3);
6827 // Load the double from tagged HeapNumber r0 to d7.
6828 __ sub(r7, r0, Operand(kHeapObjectTag));
6829 __ vldr(d7, r7, HeapNumber::kValueOffset);
6830 } else {
6831 // Calling convention says that second double is in r2 and r3.
6832 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
6833 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
6834 }
6835 __ jmp(&finished_loading_r0);
6836 __ bind(&r0_is_smi);
6837 if (mode_ == OVERWRITE_RIGHT) {
6838 // We can't overwrite a Smi so get address of new heap number into r5.
6839 __ AllocateHeapNumber(r5, r6, r7, &slow);
6840 }
6841
6842 if (use_fp_registers) {
6843 CpuFeatures::Scope scope(VFP3);
6844 // Convert smi in r0 to double in d7.
6845 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6846 __ vmov(s15, r7);
6847 __ vcvt_f64_s32(d7, s15);
6848 } else {
6849 // Write Smi from r0 to r3 and r2 in double format.
6850 __ mov(r7, Operand(r0));
6851 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6852 __ push(lr);
6853 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6854 __ pop(lr);
6855 }
6856
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006857 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
6858 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
6859 Label r1_is_not_smi;
6860 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
6861 __ tst(r1, Operand(kSmiTagMask));
6862 __ b(ne, &r1_is_not_smi);
6863 GenerateTypeTransition(masm);
6864 __ jmp(&r1_is_smi);
6865 }
6866
ager@chromium.org357bf652010-04-12 11:30:10 +00006867 __ bind(&finished_loading_r0);
6868
6869 // Move r1 to a double in r0-r1.
6870 __ tst(r1, Operand(kSmiTagMask));
6871 __ 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 +00006872 __ bind(&r1_is_not_smi);
ager@chromium.org357bf652010-04-12 11:30:10 +00006873 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6874 __ b(ne, &slow);
6875 if (mode_ == OVERWRITE_LEFT) {
6876 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6877 }
6878 if (use_fp_registers) {
6879 CpuFeatures::Scope scope(VFP3);
6880 // Load the double from tagged HeapNumber r1 to d6.
6881 __ sub(r7, r1, Operand(kHeapObjectTag));
6882 __ vldr(d6, r7, HeapNumber::kValueOffset);
6883 } else {
6884 // Calling convention says that first double is in r0 and r1.
6885 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
6886 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
6887 }
6888 __ jmp(&finished_loading_r1);
6889 __ bind(&r1_is_smi);
6890 if (mode_ == OVERWRITE_LEFT) {
6891 // We can't overwrite a Smi so get address of new heap number into r5.
6892 __ AllocateHeapNumber(r5, r6, r7, &slow);
6893 }
6894
6895 if (use_fp_registers) {
6896 CpuFeatures::Scope scope(VFP3);
6897 // Convert smi in r1 to double in d6.
6898 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6899 __ vmov(s13, r7);
6900 __ vcvt_f64_s32(d6, s13);
6901 } else {
6902 // Write Smi from r1 to r1 and r0 in double format.
6903 __ mov(r7, Operand(r1));
6904 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6905 __ push(lr);
6906 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6907 __ pop(lr);
6908 }
6909
6910 __ bind(&finished_loading_r1);
6911
6912 __ bind(&do_the_call);
6913 // If we are inlining the operation using VFP3 instructions for
6914 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6915 if (use_fp_registers) {
6916 CpuFeatures::Scope scope(VFP3);
6917 // ARMv7 VFP3 instructions to implement
6918 // double precision, add, subtract, multiply, divide.
6919
6920 if (Token::MUL == op_) {
6921 __ vmul(d5, d6, d7);
6922 } else if (Token::DIV == op_) {
6923 __ vdiv(d5, d6, d7);
6924 } else if (Token::ADD == op_) {
6925 __ vadd(d5, d6, d7);
6926 } else if (Token::SUB == op_) {
6927 __ vsub(d5, d6, d7);
6928 } else {
6929 UNREACHABLE();
6930 }
6931 __ sub(r0, r5, Operand(kHeapObjectTag));
6932 __ vstr(d5, r0, HeapNumber::kValueOffset);
6933 __ add(r0, r0, Operand(kHeapObjectTag));
6934 __ mov(pc, lr);
6935 } else {
6936 // If we did not inline the operation, then the arguments are in:
6937 // r0: Left value (least significant part of mantissa).
6938 // r1: Left value (sign, exponent, top of mantissa).
6939 // r2: Right value (least significant part of mantissa).
6940 // r3: Right value (sign, exponent, top of mantissa).
6941 // r5: Address of heap number for result.
6942
6943 __ push(lr); // For later.
6944 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
6945 // Call C routine that may not cause GC or other trouble. r5 is callee
6946 // save.
6947 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
6948 // Store answer in the overwritable heap number.
6949 #if !defined(USE_ARM_EABI)
6950 // Double returned in fp coprocessor register 0 and 1, encoded as register
6951 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
6952 // substract the tag from r5.
6953 __ sub(r4, r5, Operand(kHeapObjectTag));
6954 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
6955 #else
6956 // Double returned in registers 0 and 1.
6957 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
6958 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
6959 #endif
6960 __ mov(r0, Operand(r5));
6961 // And we are done.
6962 __ pop(pc);
6963 }
6964 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006965
6966
6967 if (lhs.is(r0)) {
6968 __ b(&slow);
6969 __ bind(&slow_reverse);
6970 __ Swap(r0, r1, ip);
6971 }
6972
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006973 // We jump to here if something goes wrong (one param is not a number of any
6974 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006975 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006976
6977 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006978 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006979
ager@chromium.org357bf652010-04-12 11:30:10 +00006980 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006981 // Test for string arguments before calling runtime.
6982 // r1 : first argument
6983 // r0 : second argument
6984 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00006985 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006986
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006987 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006988 __ tst(r1, Operand(kSmiTagMask));
6989 __ b(eq, &not_string1);
6990 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
6991 __ b(ge, &not_string1);
6992
6993 // First argument is a a string, test second.
6994 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006995 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006996 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6997 __ b(ge, &string1);
6998
6999 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007000 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
7001 __ TailCallStub(&string_add_stub);
7002
7003 __ bind(&string1_smi2);
7004 // First argument is a string, second is a smi. Try to lookup the number
7005 // string for the smi in the number string cache.
7006 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007007 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007008
7009 // Replace second argument on stack and tailcall string add stub to make
7010 // the result.
7011 __ str(r2, MemOperand(sp, 0));
7012 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007013
7014 // Only first argument is a string.
7015 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007016 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
7017
7018 // First argument was not a string, test second.
7019 __ bind(&not_string1);
7020 __ tst(r0, Operand(kSmiTagMask));
7021 __ b(eq, &not_strings);
7022 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
7023 __ b(ge, &not_strings);
7024
7025 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007026 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
7027
7028 __ bind(&not_strings);
7029 }
7030
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007031 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007032}
7033
7034
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007035// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007036// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007037// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
7038// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007039// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
7040// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007041static void GetInt32(MacroAssembler* masm,
7042 Register source,
7043 Register dest,
7044 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007045 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007046 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007047 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007048 // Get exponent word.
7049 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
7050 // Get exponent alone in scratch2.
7051 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007052 // Load dest with zero. We use this either for the final shift or
7053 // for the answer.
7054 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007055 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007056 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
7057 // the exponent that we are fastest at and also the highest exponent we can
7058 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007059 const uint32_t non_smi_exponent =
7060 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7061 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007062 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
7063 __ b(eq, &right_exponent);
7064 // If the exponent is higher than that then go to slow case. This catches
7065 // numbers that don't fit in a signed int32, infinities and NaNs.
7066 __ b(gt, slow);
7067
7068 // We know the exponent is smaller than 30 (biased). If it is less than
7069 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
7070 // it rounds to zero.
7071 const uint32_t zero_exponent =
7072 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
7073 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
7074 // Dest already has a Smi zero.
7075 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007076 if (!CpuFeatures::IsSupported(VFP3)) {
7077 // We have a shifted exponent between 0 and 30 in scratch2.
7078 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
7079 // We now have the exponent in dest. Subtract from 30 to get
7080 // how much to shift down.
7081 __ rsb(dest, dest, Operand(30));
7082 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007083 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007084 if (CpuFeatures::IsSupported(VFP3)) {
7085 CpuFeatures::Scope scope(VFP3);
7086 // ARMv7 VFP3 instructions implementing double precision to integer
7087 // conversion using round to zero.
7088 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007089 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007090 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007091 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007092 } else {
7093 // Get the top bits of the mantissa.
7094 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
7095 // Put back the implicit 1.
7096 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
7097 // Shift up the mantissa bits to take up the space the exponent used to
7098 // take. We just orred in the implicit bit so that took care of one and
7099 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
7100 // distance.
7101 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7102 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
7103 // Put sign in zero flag.
7104 __ tst(scratch, Operand(HeapNumber::kSignMask));
7105 // Get the second half of the double. For some exponents we don't
7106 // actually need this because the bits get shifted out again, but
7107 // it's probably slower to test than just to do it.
7108 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
7109 // Shift down 22 bits to get the last 10 bits.
7110 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
7111 // Move down according to the exponent.
7112 __ mov(dest, Operand(scratch, LSR, dest));
7113 // Fix sign if sign bit was set.
7114 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
7115 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007116 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007117}
7118
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007119// For bitwise ops where the inputs are not both Smis we here try to determine
7120// whether both inputs are either Smis or at least heap numbers that can be
7121// represented by a 32 bit signed value. We truncate towards zero as required
7122// by the ES spec. If this is the case we do the bitwise op and see if the
7123// result is a Smi. If so, great, otherwise we try to find a heap number to
7124// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00007125// On entry the operands are in lhs and rhs. On exit the answer is in r0.
7126void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
7127 Register lhs,
7128 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007129 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007130 Label rhs_is_smi, lhs_is_smi;
7131 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007132
ager@chromium.org357bf652010-04-12 11:30:10 +00007133 __ tst(lhs, Operand(kSmiTagMask));
7134 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
7135 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007136 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007137 GetInt32(masm, lhs, r3, r5, r4, &slow);
7138 __ jmp(&done_checking_lhs);
7139 __ bind(&lhs_is_smi);
7140 __ mov(r3, Operand(lhs, ASR, 1));
7141 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007142
ager@chromium.org357bf652010-04-12 11:30:10 +00007143 __ tst(rhs, Operand(kSmiTagMask));
7144 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
7145 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007146 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007147 GetInt32(masm, rhs, r2, r5, r4, &slow);
7148 __ jmp(&done_checking_rhs);
7149 __ bind(&rhs_is_smi);
7150 __ mov(r2, Operand(rhs, ASR, 1));
7151 __ bind(&done_checking_rhs);
7152
7153 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007154
7155 // r0 and r1: Original operands (Smi or heap numbers).
7156 // r2 and r3: Signed int32 operands.
7157 switch (op_) {
7158 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
7159 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
7160 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
7161 case Token::SAR:
7162 // Use only the 5 least significant bits of the shift count.
7163 __ and_(r2, r2, Operand(0x1f));
7164 __ mov(r2, Operand(r3, ASR, r2));
7165 break;
7166 case Token::SHR:
7167 // Use only the 5 least significant bits of the shift count.
7168 __ and_(r2, r2, Operand(0x1f));
7169 __ mov(r2, Operand(r3, LSR, r2), SetCC);
7170 // SHR is special because it is required to produce a positive answer.
7171 // The code below for writing into heap numbers isn't capable of writing
7172 // the register as an unsigned int so we go to slow case if we hit this
7173 // case.
7174 __ b(mi, &slow);
7175 break;
7176 case Token::SHL:
7177 // Use only the 5 least significant bits of the shift count.
7178 __ and_(r2, r2, Operand(0x1f));
7179 __ mov(r2, Operand(r3, LSL, r2));
7180 break;
7181 default: UNREACHABLE();
7182 }
7183 // check that the *signed* result fits in a smi
7184 __ add(r3, r2, Operand(0x40000000), SetCC);
7185 __ b(mi, &result_not_a_smi);
7186 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
7187 __ Ret();
7188
7189 Label have_to_allocate, got_a_heap_number;
7190 __ bind(&result_not_a_smi);
7191 switch (mode_) {
7192 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007193 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007194 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007195 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007196 break;
7197 }
7198 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007199 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007200 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007201 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007202 break;
7203 }
7204 case NO_OVERWRITE: {
7205 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007206 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007207 }
7208 default: break;
7209 }
7210 __ bind(&got_a_heap_number);
7211 // r2: Answer as signed int32.
7212 // r5: Heap number to write answer into.
7213
7214 // Nothing can go wrong now, so move the heap number to r0, which is the
7215 // result.
7216 __ mov(r0, Operand(r5));
7217
7218 // Tail call that writes the int32 in r2 to the heap number in r0, using
7219 // r3 as scratch. r0 is preserved and returned.
7220 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
7221 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
7222
7223 if (mode_ != NO_OVERWRITE) {
7224 __ bind(&have_to_allocate);
7225 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007226 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007227 __ jmp(&got_a_heap_number);
7228 }
7229
7230 // If all else failed then we go to the runtime system.
7231 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007232 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007233 switch (op_) {
7234 case Token::BIT_OR:
7235 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
7236 break;
7237 case Token::BIT_AND:
7238 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
7239 break;
7240 case Token::BIT_XOR:
7241 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
7242 break;
7243 case Token::SAR:
7244 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
7245 break;
7246 case Token::SHR:
7247 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
7248 break;
7249 case Token::SHL:
7250 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
7251 break;
7252 default:
7253 UNREACHABLE();
7254 }
7255}
7256
7257
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007258// Can we multiply by x with max two shifts and an add.
7259// This answers yes to all integers from 2 to 10.
7260static bool IsEasyToMultiplyBy(int x) {
7261 if (x < 2) return false; // Avoid special cases.
7262 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
7263 if (IsPowerOf2(x)) return true; // Simple shift.
7264 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
7265 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
7266 return false;
7267}
7268
7269
7270// Can multiply by anything that IsEasyToMultiplyBy returns true for.
7271// Source and destination may be the same register. This routine does
7272// not set carry and overflow the way a mul instruction would.
7273static void MultiplyByKnownInt(MacroAssembler* masm,
7274 Register source,
7275 Register destination,
7276 int known_int) {
7277 if (IsPowerOf2(known_int)) {
7278 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
7279 } else if (PopCountLessThanEqual2(known_int)) {
7280 int first_bit = BitPosition(known_int);
7281 int second_bit = BitPosition(known_int ^ (1 << first_bit));
7282 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
7283 if (first_bit != 0) {
7284 __ mov(destination, Operand(destination, LSL, first_bit));
7285 }
7286 } else {
7287 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
7288 int the_bit = BitPosition(known_int + 1);
7289 __ rsb(destination, source, Operand(source, LSL, the_bit));
7290 }
7291}
7292
7293
7294// This function (as opposed to MultiplyByKnownInt) takes the known int in a
7295// a register for the cases where it doesn't know a good trick, and may deliver
7296// a result that needs shifting.
7297static void MultiplyByKnownInt2(
7298 MacroAssembler* masm,
7299 Register result,
7300 Register source,
7301 Register known_int_register, // Smi tagged.
7302 int known_int,
7303 int* required_shift) { // Including Smi tag shift
7304 switch (known_int) {
7305 case 3:
7306 __ add(result, source, Operand(source, LSL, 1));
7307 *required_shift = 1;
7308 break;
7309 case 5:
7310 __ add(result, source, Operand(source, LSL, 2));
7311 *required_shift = 1;
7312 break;
7313 case 6:
7314 __ add(result, source, Operand(source, LSL, 1));
7315 *required_shift = 2;
7316 break;
7317 case 7:
7318 __ rsb(result, source, Operand(source, LSL, 3));
7319 *required_shift = 1;
7320 break;
7321 case 9:
7322 __ add(result, source, Operand(source, LSL, 3));
7323 *required_shift = 1;
7324 break;
7325 case 10:
7326 __ add(result, source, Operand(source, LSL, 2));
7327 *required_shift = 2;
7328 break;
7329 default:
7330 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
7331 __ mul(result, source, known_int_register);
7332 *required_shift = 0;
7333 }
7334}
7335
7336
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007337const char* GenericBinaryOpStub::GetName() {
7338 if (name_ != NULL) return name_;
7339 const int len = 100;
7340 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7341 if (name_ == NULL) return "OOM";
7342 const char* op_name = Token::Name(op_);
7343 const char* overwrite_name;
7344 switch (mode_) {
7345 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7346 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7347 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7348 default: overwrite_name = "UnknownOverwrite"; break;
7349 }
7350
7351 OS::SNPrintF(Vector<char>(name_, len),
7352 "GenericBinaryOpStub_%s_%s%s",
7353 op_name,
7354 overwrite_name,
7355 specialized_on_rhs_ ? "_ConstantRhs" : 0);
7356 return name_;
7357}
7358
7359
ager@chromium.org5c838252010-02-19 08:53:10 +00007360
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007361void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007362 // lhs_ : x
7363 // rhs_ : y
7364 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007365
ager@chromium.org357bf652010-04-12 11:30:10 +00007366 Register result = r0;
7367 Register lhs = lhs_;
7368 Register rhs = rhs_;
7369
7370 // This code can't cope with other register allocations yet.
7371 ASSERT(result.is(r0) &&
7372 ((lhs.is(r0) && rhs.is(r1)) ||
7373 (lhs.is(r1) && rhs.is(r0))));
7374
7375 Register smi_test_reg = VirtualFrame::scratch0();
7376 Register scratch = VirtualFrame::scratch1();
7377
7378 // All ops need to know whether we are dealing with two Smis. Set up
7379 // smi_test_reg to tell us that.
7380 if (ShouldGenerateSmiCode()) {
7381 __ orr(smi_test_reg, lhs, Operand(rhs));
7382 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007383
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007384 switch (op_) {
7385 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007386 Label not_smi;
7387 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007388 if (ShouldGenerateSmiCode()) {
7389 ASSERT(kSmiTag == 0); // Adjust code below.
7390 __ tst(smi_test_reg, Operand(kSmiTagMask));
7391 __ b(ne, &not_smi);
7392 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
7393 // Return if no overflow.
7394 __ Ret(vc);
7395 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
7396 }
7397 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007398 break;
7399 }
7400
7401 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007402 Label not_smi;
7403 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007404 if (ShouldGenerateSmiCode()) {
7405 ASSERT(kSmiTag == 0); // Adjust code below.
7406 __ tst(smi_test_reg, Operand(kSmiTagMask));
7407 __ b(ne, &not_smi);
7408 if (lhs.is(r1)) {
7409 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
7410 // Return if no overflow.
7411 __ Ret(vc);
7412 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
7413 } else {
7414 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
7415 // Return if no overflow.
7416 __ Ret(vc);
7417 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
7418 }
7419 }
7420 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007421 break;
7422 }
7423
7424 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007425 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00007426 if (ShouldGenerateSmiCode()) {
7427 ASSERT(kSmiTag == 0); // adjust code below
7428 __ tst(smi_test_reg, Operand(kSmiTagMask));
7429 Register scratch2 = smi_test_reg;
7430 smi_test_reg = no_reg;
7431 __ b(ne, &not_smi);
7432 // Remove tag from one operand (but keep sign), so that result is Smi.
7433 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
7434 // Do multiplication
7435 // scratch = lower 32 bits of ip * lhs.
7436 __ smull(scratch, scratch2, lhs, ip);
7437 // Go slow on overflows (overflow bit is not set).
7438 __ mov(ip, Operand(scratch, ASR, 31));
7439 // No overflow if higher 33 bits are identical.
7440 __ cmp(ip, Operand(scratch2));
7441 __ b(ne, &slow);
7442 // Go slow on zero result to handle -0.
7443 __ tst(scratch, Operand(scratch));
7444 __ mov(result, Operand(scratch), LeaveCC, ne);
7445 __ Ret(ne);
7446 // We need -0 if we were multiplying a negative number with 0 to get 0.
7447 // We know one of them was zero.
7448 __ add(scratch2, rhs, Operand(lhs), SetCC);
7449 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
7450 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
7451 // Slow case. We fall through here if we multiplied a negative number
7452 // with 0, because that would mean we should produce -0.
7453 __ bind(&slow);
7454 }
7455 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007456 break;
7457 }
7458
7459 case Token::DIV:
7460 case Token::MOD: {
7461 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007462 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007463 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00007464 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007465 if (IsPowerOf2(constant_rhs_)) {
7466 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007467 __ and_(rhs,
7468 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007469 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
7470 SetCC);
7471 // We now have the answer, but if the input was negative we also
7472 // have the sign bit. Our work is done if the result is
7473 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00007474 if (!rhs.is(r0)) {
7475 __ mov(r0, rhs, LeaveCC, pl);
7476 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007477 __ Ret(pl);
7478 // A mod of a negative left hand side must return a negative number.
7479 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00007480 // already optimistically trashed rhs so we may need to restore it.
7481 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007482 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007483 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007484 __ b(eq, &smi_is_unsuitable);
7485 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00007486 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007487 } else {
7488 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00007489 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007490 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
7491 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
7492 int shift = 0;
7493 int d = constant_rhs_;
7494 while ((d & 1) == 0) {
7495 d >>= 1;
7496 shift++;
7497 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007498 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007499 __ bic(r0, r0, Operand(kSmiTagMask));
7500 }
7501 } else {
7502 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00007503 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007504 __ b(ne, &smi_is_unsuitable);
7505 // Find a fixed point reciprocal of the divisor so we can divide by
7506 // multiplying.
7507 double divisor = 1.0 / constant_rhs_;
7508 int shift = 32;
7509 double scale = 4294967296.0; // 1 << 32.
7510 uint32_t mul;
7511 // Maximise the precision of the fixed point reciprocal.
7512 while (true) {
7513 mul = static_cast<uint32_t>(scale * divisor);
7514 if (mul >= 0x7fffffff) break;
7515 scale *= 2.0;
7516 shift++;
7517 }
7518 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00007519 Register scratch2 = smi_test_reg;
7520 smi_test_reg = no_reg;
7521 __ mov(scratch2, Operand(mul));
7522 __ umull(scratch, scratch2, scratch2, lhs);
7523 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
7524 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
7525 // rhs is still the known rhs. rhs is Smi tagged.
7526 // lhs is still the unkown lhs. lhs is Smi tagged.
7527 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
7528 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007529 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00007530 scratch,
7531 scratch2,
7532 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007533 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00007534 &required_scratch_shift);
7535 // scratch << required_scratch_shift is now the Smi tagged rhs *
7536 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007537 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007538 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007539 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00007540 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007541 } else {
7542 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00007543 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007544 }
7545 }
7546 __ Ret();
7547 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007548 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007549 HandleBinaryOpSlowCases(
7550 masm,
7551 &not_smi,
7552 lhs,
7553 rhs,
7554 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007555 break;
7556 }
7557
7558 case Token::BIT_OR:
7559 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007560 case Token::BIT_XOR:
7561 case Token::SAR:
7562 case Token::SHR:
7563 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007564 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007565 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00007566 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007567 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007568 Register scratch2 = smi_test_reg;
7569 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007570 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007571 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
7572 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
7573 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007574 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007575 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00007576 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7577 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007578 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007579 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007580 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007581 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007582 // Remove tags from operands. We can't do this on a 31 bit number
7583 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00007584 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7585 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7586 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007587 // Unsigned shift is not allowed to produce a negative number, so
7588 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00007589 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007590 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007591 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007592 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007593 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007594 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007595 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00007596 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7597 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7598 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007599 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00007600 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007601 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007602 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007603 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007604 default: UNREACHABLE();
7605 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007606 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007607 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007608 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007609 break;
7610 }
7611
7612 default: UNREACHABLE();
7613 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007614 // This code should be unreachable.
7615 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00007616
7617 // Generate an unreachable reference to the DEFAULT stub so that it can be
7618 // found at the end of this stub when clearing ICs at GC.
7619 // TODO(kaznacheev): Check performance impact and get rid of this.
7620 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
7621 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
7622 __ CallStub(&uninit);
7623 }
7624}
7625
7626
7627void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
7628 Label get_result;
7629
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007630 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00007631
7632 // Internal frame is necessary to handle exceptions properly.
7633 __ EnterInternalFrame();
7634 // Call the stub proper to get the result in r0.
7635 __ Call(&get_result);
7636 __ LeaveInternalFrame();
7637
7638 __ push(r0);
7639
7640 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
7641 __ push(r0);
7642 __ mov(r0, Operand(Smi::FromInt(op_)));
7643 __ push(r0);
7644 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
7645 __ push(r0);
7646
7647 __ TailCallExternalReference(
7648 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
7649 6,
7650 1);
7651
7652 // The entry point for the result calculation is assumed to be immediately
7653 // after this sequence.
7654 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007655}
7656
7657
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007658Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007659 GenericBinaryOpStub stub(key, type_info);
7660 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007661}
7662
7663
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007664void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00007665 // Do tail-call to runtime routine. Runtime routines expect at least one
7666 // argument, so give it a Smi.
7667 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007668 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007669 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007670
7671 __ StubReturn(1);
7672}
7673
7674
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007675void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007676 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007677
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007678 if (op_ == Token::SUB) {
7679 // Check whether the value is a smi.
7680 Label try_float;
7681 __ tst(r0, Operand(kSmiTagMask));
7682 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007683
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007684 // Go slow case if the value of the expression is zero
7685 // to make sure that we switch between 0 and -0.
7686 __ cmp(r0, Operand(0));
7687 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007688
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007689 // The value of the expression is a smi that is not zero. Try
7690 // optimistic subtraction '0 - value'.
7691 __ rsb(r1, r0, Operand(0), SetCC);
7692 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007693
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007694 __ mov(r0, Operand(r1)); // Set r0 to result.
7695 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007696
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007697 __ bind(&try_float);
7698 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7699 __ b(ne, &slow);
7700 // r0 is a heap number. Get a new heap number in r1.
7701 if (overwrite_) {
7702 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7703 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7704 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7705 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007706 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007707 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7708 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7709 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
7710 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7711 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
7712 __ mov(r0, Operand(r1));
7713 }
7714 } else if (op_ == Token::BIT_NOT) {
7715 // Check if the operand is a heap number.
7716 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7717 __ b(ne, &slow);
7718
7719 // Convert the heap number is r0 to an untagged integer in r1.
7720 GetInt32(masm, r0, r1, r2, r3, &slow);
7721
7722 // Do the bitwise operation (move negated) and check if the result
7723 // fits in a smi.
7724 Label try_float;
7725 __ mvn(r1, Operand(r1));
7726 __ add(r2, r1, Operand(0x40000000), SetCC);
7727 __ b(mi, &try_float);
7728 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
7729 __ b(&done);
7730
7731 __ bind(&try_float);
7732 if (!overwrite_) {
7733 // Allocate a fresh heap number, but don't overwrite r0 until
7734 // we're sure we can do it without going through the slow case
7735 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007736 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007737 __ mov(r0, Operand(r2));
7738 }
7739
7740 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
7741 // have to set up a frame.
7742 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
7743 __ push(lr);
7744 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
7745 __ pop(lr);
7746 } else {
7747 UNIMPLEMENTED();
7748 }
7749
7750 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007751 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007752
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007753 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007754 __ bind(&slow);
7755 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007756 switch (op_) {
7757 case Token::SUB:
7758 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
7759 break;
7760 case Token::BIT_NOT:
7761 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
7762 break;
7763 default:
7764 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007765 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00007766}
7767
7768
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007769void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007770 // r0 holds the exception.
7771
7772 // Adjust this code if not the case.
7773 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7774
7775 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007776 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
7777 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007778
7779 // Restore the next handler and frame pointer, discard handler state.
7780 ASSERT(StackHandlerConstants::kNextOffset == 0);
7781 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007782 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007783 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7784 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
7785
7786 // Before returning we restore the context from the frame pointer if
7787 // not NULL. The frame pointer is NULL in the exception handler of a
7788 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007789 __ cmp(fp, Operand(0));
7790 // Set cp to NULL if fp is NULL.
7791 __ mov(cp, Operand(0), LeaveCC, eq);
7792 // Restore cp otherwise.
7793 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007794#ifdef DEBUG
7795 if (FLAG_debug_code) {
7796 __ mov(lr, Operand(pc));
7797 }
7798#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007799 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007800 __ pop(pc);
7801}
7802
7803
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007804void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
7805 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007806 // Adjust this code if not the case.
7807 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7808
7809 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007810 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007811 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007812
7813 // Unwind the handlers until the ENTRY handler is found.
7814 Label loop, done;
7815 __ bind(&loop);
7816 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007817 const int kStateOffset = StackHandlerConstants::kStateOffset;
7818 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007819 __ cmp(r2, Operand(StackHandler::ENTRY));
7820 __ b(eq, &done);
7821 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007822 const int kNextOffset = StackHandlerConstants::kNextOffset;
7823 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007824 __ jmp(&loop);
7825 __ bind(&done);
7826
7827 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007828 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007829 __ pop(r2);
7830 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007831
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007832 if (type == OUT_OF_MEMORY) {
7833 // Set external caught exception to false.
7834 ExternalReference external_caught(Top::k_external_caught_exception_address);
7835 __ mov(r0, Operand(false));
7836 __ mov(r2, Operand(external_caught));
7837 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007838
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007839 // Set pending exception and r0 to out of memory exception.
7840 Failure* out_of_memory = Failure::OutOfMemoryException();
7841 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7842 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
7843 __ str(r0, MemOperand(r2));
7844 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007845
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007846 // Stack layout at this point. See also StackHandlerConstants.
7847 // sp -> state (ENTRY)
7848 // fp
7849 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007850
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007851 // Discard handler state (r2 is not used) and restore frame pointer.
7852 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7853 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
7854 // Before returning we restore the context from the frame pointer if
7855 // not NULL. The frame pointer is NULL in the exception handler of a
7856 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007857 __ cmp(fp, Operand(0));
7858 // Set cp to NULL if fp is NULL.
7859 __ mov(cp, Operand(0), LeaveCC, eq);
7860 // Restore cp otherwise.
7861 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007862#ifdef DEBUG
7863 if (FLAG_debug_code) {
7864 __ mov(lr, Operand(pc));
7865 }
7866#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007867 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007868 __ pop(pc);
7869}
7870
7871
7872void CEntryStub::GenerateCore(MacroAssembler* masm,
7873 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007874 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007875 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007876 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007877 bool always_allocate,
7878 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007879 // r0: result parameter for PerformGC, if any
7880 // r4: number of arguments including receiver (C callee-saved)
7881 // r5: pointer to builtin function (C callee-saved)
7882 // r6: pointer to the first argument (C callee-saved)
7883
7884 if (do_gc) {
7885 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007886 __ PrepareCallCFunction(1, r1);
7887 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007888 }
7889
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007890 ExternalReference scope_depth =
7891 ExternalReference::heap_always_allocate_scope_depth();
7892 if (always_allocate) {
7893 __ mov(r0, Operand(scope_depth));
7894 __ ldr(r1, MemOperand(r0));
7895 __ add(r1, r1, Operand(1));
7896 __ str(r1, MemOperand(r0));
7897 }
7898
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007899 // Call C built-in.
7900 // r0 = argc, r1 = argv
7901 __ mov(r0, Operand(r4));
7902 __ mov(r1, Operand(r6));
7903
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007904 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
7905 int frame_alignment_mask = frame_alignment - 1;
7906#if defined(V8_HOST_ARCH_ARM)
7907 if (FLAG_debug_code) {
7908 if (frame_alignment > kPointerSize) {
7909 Label alignment_as_expected;
7910 ASSERT(IsPowerOf2(frame_alignment));
7911 __ sub(r2, sp, Operand(frame_alignment_skew));
7912 __ tst(r2, Operand(frame_alignment_mask));
7913 __ b(eq, &alignment_as_expected);
7914 // Don't use Check here, as it will call Runtime_Abort re-entering here.
7915 __ stop("Unexpected alignment");
7916 __ bind(&alignment_as_expected);
7917 }
7918 }
7919#endif
7920
7921 // Just before the call (jump) below lr is pushed, so the actual alignment is
7922 // adding one to the current skew.
7923 int alignment_before_call =
7924 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
7925 if (alignment_before_call > 0) {
7926 // Push until the alignment before the call is met.
7927 __ mov(r2, Operand(0));
7928 for (int i = alignment_before_call;
7929 (i & frame_alignment_mask) != 0;
7930 i += kPointerSize) {
7931 __ push(r2);
7932 }
7933 }
7934
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007935 // TODO(1242173): To let the GC traverse the return address of the exit
7936 // frames, we need to know where the return address is. Right now,
7937 // we push it on the stack to be able to find it again, but we never
7938 // restore from it in case of changes, which makes it impossible to
7939 // support moving the C entry code stub. This should be fixed, but currently
7940 // this is OK because the CEntryStub gets generated so early in the V8 boot
7941 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007942 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007943 masm->push(lr);
7944 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007945
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007946 // Restore sp back to before aligning the stack.
7947 if (alignment_before_call > 0) {
7948 __ add(sp, sp, Operand(alignment_before_call));
7949 }
7950
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007951 if (always_allocate) {
7952 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
7953 // though (contain the result).
7954 __ mov(r2, Operand(scope_depth));
7955 __ ldr(r3, MemOperand(r2));
7956 __ sub(r3, r3, Operand(1));
7957 __ str(r3, MemOperand(r2));
7958 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007959
7960 // check for failure result
7961 Label failure_returned;
7962 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
7963 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
7964 __ add(r2, r0, Operand(1));
7965 __ tst(r2, Operand(kFailureTagMask));
7966 __ b(eq, &failure_returned);
7967
7968 // Exit C frame and return.
7969 // r0:r1: result
7970 // sp: stack pointer
7971 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007972 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007973
7974 // check if we should retry or throw exception
7975 Label retry;
7976 __ bind(&failure_returned);
7977 ASSERT(Failure::RETRY_AFTER_GC == 0);
7978 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
7979 __ b(eq, &retry);
7980
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007981 // Special handling of out of memory exceptions.
7982 Failure* out_of_memory = Failure::OutOfMemoryException();
7983 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7984 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007985
7986 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00007987 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007988 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007989 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007990 __ ldr(r0, MemOperand(ip));
7991 __ str(r3, MemOperand(ip));
7992
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007993 // Special handling of termination exceptions which are uncatchable
7994 // by javascript code.
7995 __ cmp(r0, Operand(Factory::termination_exception()));
7996 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007997
7998 // Handle normal exception.
7999 __ jmp(throw_normal_exception);
8000
8001 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
8002}
8003
8004
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008005void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008006 // Called from JavaScript; parameters are on stack as if calling JS function
8007 // r0: number of arguments including receiver
8008 // r1: pointer to builtin function
8009 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008010 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008011 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008012
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008013 // Result returned in r0 or r0+r1 by default.
8014
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008015 // NOTE: Invocations of builtins may return failure objects
8016 // instead of a proper result. The builtin entry handles
8017 // this by performing a garbage collection and retrying the
8018 // builtin once.
8019
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008020 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008021 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008022
8023 // r4: number of arguments (C callee-saved)
8024 // r5: pointer to builtin function (C callee-saved)
8025 // r6: pointer to first argument (C callee-saved)
8026
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008027 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008028 Label throw_termination_exception;
8029 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008030
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008031 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008032 GenerateCore(masm,
8033 &throw_normal_exception,
8034 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008035 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008036 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008037 false,
8038 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008039
8040 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008041 GenerateCore(masm,
8042 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008043 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008044 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008045 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008046 false,
8047 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008048
8049 // Do full GC and retry runtime call one final time.
8050 Failure* failure = Failure::InternalError();
8051 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
8052 GenerateCore(masm,
8053 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008054 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008055 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008056 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008057 true,
8058 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008059
8060 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008061 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
8062
8063 __ bind(&throw_termination_exception);
8064 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008065
8066 __ bind(&throw_normal_exception);
8067 GenerateThrowTOS(masm);
8068}
8069
8070
8071void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
8072 // r0: code entry
8073 // r1: function
8074 // r2: receiver
8075 // r3: argc
8076 // [sp+0]: argv
8077
8078 Label invoke, exit;
8079
8080 // Called from C, so do not pop argc and args on exit (preserve sp)
8081 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008082 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008083 __ stm(db_w, sp, kCalleeSaved | lr.bit());
8084
8085 // Get address of argv, see stm above.
8086 // r0: code entry
8087 // r1: function
8088 // r2: receiver
8089 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00008090 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008091
8092 // Push a frame with special values setup to mark it as an entry frame.
8093 // r0: code entry
8094 // r1: function
8095 // r2: receiver
8096 // r3: argc
8097 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008098 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008099 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
8100 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008101 __ mov(r6, Operand(Smi::FromInt(marker)));
8102 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8103 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008104 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008105
8106 // Setup frame pointer for the frame to be pushed.
8107 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8108
8109 // Call a faked try-block that does the invoke.
8110 __ bl(&invoke);
8111
8112 // Caught exception: Store result (exception) in the pending
8113 // exception field in the JSEnv and return a failure sentinel.
8114 // Coming in here the fp will be invalid because the PushTryHandler below
8115 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00008116 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008117 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008118 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008119 __ b(&exit);
8120
8121 // Invoke: Link this frame into the handler chain.
8122 __ bind(&invoke);
8123 // Must preserve r0-r4, r5-r7 are available.
8124 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008125 // If an exception not caught by another handler occurs, this handler
8126 // returns control to the code after the bl(&invoke) above, which
8127 // restores all kCalleeSaved registers (including cp and fp) to their
8128 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008129
8130 // Clear any pending exceptions.
8131 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
8132 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008133 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008134 __ str(r5, MemOperand(ip));
8135
8136 // Invoke the function by calling through JS entry trampoline builtin.
8137 // Notice that we cannot store a reference to the trampoline code directly in
8138 // this stub, because runtime stubs are not traversed when doing GC.
8139
8140 // Expected registers by Builtins::JSEntryTrampoline
8141 // r0: code entry
8142 // r1: function
8143 // r2: receiver
8144 // r3: argc
8145 // r4: argv
8146 if (is_construct) {
8147 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
8148 __ mov(ip, Operand(construct_entry));
8149 } else {
8150 ExternalReference entry(Builtins::JSEntryTrampoline);
8151 __ mov(ip, Operand(entry));
8152 }
8153 __ ldr(ip, MemOperand(ip)); // deref address
8154
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008155 // Branch and link to JSEntryTrampoline. We don't use the double underscore
8156 // macro for the add instruction because we don't want the coverage tool
8157 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008158 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008159 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008160
8161 // Unlink this frame from the handler chain. When reading the
8162 // address of the next handler, there is no need to use the address
8163 // displacement since the current stack pointer (sp) points directly
8164 // to the stack handler.
8165 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
8166 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
8167 __ str(r3, MemOperand(ip));
8168 // No need to restore registers
8169 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
8170
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008171
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008172 __ bind(&exit); // r0 holds result
8173 // Restore the top frame descriptors from the stack.
8174 __ pop(r3);
8175 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8176 __ str(r3, MemOperand(ip));
8177
8178 // Reset the stack to the callee saved registers.
8179 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8180
8181 // Restore callee-saved registers and return.
8182#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008183 if (FLAG_debug_code) {
8184 __ mov(lr, Operand(pc));
8185 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008186#endif
8187 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
8188}
8189
8190
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008191// This stub performs an instanceof, calling the builtin function if
8192// necessary. Uses r1 for the object, r0 for the function that it may
8193// be an instance of (these are fetched from the stack).
8194void InstanceofStub::Generate(MacroAssembler* masm) {
8195 // Get the object - slow case for smis (we may need to throw an exception
8196 // depending on the rhs).
8197 Label slow, loop, is_instance, is_not_instance;
8198 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
8199 __ BranchOnSmi(r0, &slow);
8200
8201 // Check that the left hand is a JS object and put map in r3.
8202 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
8203 __ b(lt, &slow);
8204 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
8205 __ b(gt, &slow);
8206
8207 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00008208 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008209 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
8210
8211 // Check that the function prototype is a JS object.
8212 __ BranchOnSmi(r4, &slow);
8213 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
8214 __ b(lt, &slow);
8215 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
8216 __ b(gt, &slow);
8217
8218 // Register mapping: r3 is object map and r4 is function prototype.
8219 // Get prototype of object into r2.
8220 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
8221
8222 // Loop through the prototype chain looking for the function prototype.
8223 __ bind(&loop);
8224 __ cmp(r2, Operand(r4));
8225 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00008226 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8227 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008228 __ b(eq, &is_not_instance);
8229 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
8230 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
8231 __ jmp(&loop);
8232
8233 __ bind(&is_instance);
8234 __ mov(r0, Operand(Smi::FromInt(0)));
8235 __ pop();
8236 __ pop();
8237 __ mov(pc, Operand(lr)); // Return.
8238
8239 __ bind(&is_not_instance);
8240 __ mov(r0, Operand(Smi::FromInt(1)));
8241 __ pop();
8242 __ pop();
8243 __ mov(pc, Operand(lr)); // Return.
8244
8245 // Slow-case. Tail call builtin.
8246 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008247 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
8248}
8249
8250
ager@chromium.org7c537e22008-10-16 08:43:32 +00008251void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
8252 // The displacement is the offset of the last parameter (if any)
8253 // relative to the frame pointer.
8254 static const int kDisplacement =
8255 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008256
ager@chromium.org7c537e22008-10-16 08:43:32 +00008257 // Check that the key is a smi.
8258 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008259 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008260
ager@chromium.org7c537e22008-10-16 08:43:32 +00008261 // Check if the calling frame is an arguments adaptor frame.
8262 Label adaptor;
8263 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8264 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008265 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008266 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008267
ager@chromium.org7c537e22008-10-16 08:43:32 +00008268 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008269 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00008270 // check for free.
8271 __ cmp(r1, r0);
8272 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008273
ager@chromium.org7c537e22008-10-16 08:43:32 +00008274 // Read the argument from the stack and return it.
8275 __ sub(r3, r0, r1);
8276 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8277 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008278 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008279
8280 // Arguments adaptor case: Check index against actual arguments
8281 // limit found in the arguments adaptor frame. Use unsigned
8282 // comparison to get negative check for free.
8283 __ bind(&adaptor);
8284 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8285 __ cmp(r1, r0);
8286 __ b(cs, &slow);
8287
8288 // Read the argument from the adaptor frame and return it.
8289 __ sub(r3, r0, r1);
8290 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8291 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008292 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008293
8294 // Slow-case: Handle non-smi or out-of-bounds access to arguments
8295 // by calling the runtime system.
8296 __ bind(&slow);
8297 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008298 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008299}
8300
8301
8302void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00008303 // sp[0] : number of parameters
8304 // sp[4] : receiver displacement
8305 // sp[8] : function
8306
ager@chromium.org7c537e22008-10-16 08:43:32 +00008307 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00008308 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00008309 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8310 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008311 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00008312 __ b(eq, &adaptor_frame);
8313
8314 // Get the length from the frame.
8315 __ ldr(r1, MemOperand(sp, 0));
8316 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008317
8318 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00008319 __ bind(&adaptor_frame);
8320 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8321 __ str(r1, MemOperand(sp, 0));
8322 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008323 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
8324 __ str(r3, MemOperand(sp, 1 * kPointerSize));
8325
ager@chromium.org5c838252010-02-19 08:53:10 +00008326 // Try the new space allocation. Start out with computing the size
8327 // of the arguments object and the elements array (in words, not
8328 // bytes because AllocateInNewSpace expects words).
8329 Label add_arguments_object;
8330 __ bind(&try_allocate);
8331 __ cmp(r1, Operand(0));
8332 __ b(eq, &add_arguments_object);
8333 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8334 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
8335 __ bind(&add_arguments_object);
8336 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
8337
8338 // Do the allocation of both objects in one go.
8339 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
8340
8341 // Get the arguments boilerplate from the current (global) context.
8342 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
8343 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
8344 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
8345 __ ldr(r4, MemOperand(r4, offset));
8346
8347 // Copy the JS object part.
8348 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
8349 __ ldr(r3, FieldMemOperand(r4, i));
8350 __ str(r3, FieldMemOperand(r0, i));
8351 }
8352
8353 // Setup the callee in-object property.
8354 ASSERT(Heap::arguments_callee_index == 0);
8355 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
8356 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
8357
8358 // Get the length (smi tagged) and set that as an in-object property too.
8359 ASSERT(Heap::arguments_length_index == 1);
8360 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
8361 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
8362
8363 // If there are no actual arguments, we're done.
8364 Label done;
8365 __ cmp(r1, Operand(0));
8366 __ b(eq, &done);
8367
8368 // Get the parameters pointer from the stack and untag the length.
8369 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
8370 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8371
8372 // Setup the elements pointer in the allocated arguments object and
8373 // initialize the header in the elements fixed array.
8374 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
8375 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
8376 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
8377 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
8378 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
8379
8380 // Copy the fixed array slots.
8381 Label loop;
8382 // Setup r4 to point to the first array slot.
8383 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
8384 __ bind(&loop);
8385 // Pre-decrement r2 with kPointerSize on each iteration.
8386 // Pre-decrement in order to skip receiver.
8387 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
8388 // Post-increment r4 with kPointerSize on each iteration.
8389 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
8390 __ sub(r1, r1, Operand(1));
8391 __ cmp(r1, Operand(0));
8392 __ b(ne, &loop);
8393
8394 // Return and remove the on-stack parameters.
8395 __ bind(&done);
8396 __ add(sp, sp, Operand(3 * kPointerSize));
8397 __ Ret();
8398
ager@chromium.org7c537e22008-10-16 08:43:32 +00008399 // Do the runtime call to allocate the arguments object.
8400 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008401 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008402}
8403
8404
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008405void RegExpExecStub::Generate(MacroAssembler* masm) {
8406 // Just jump directly to runtime if native RegExp is not selected at compile
8407 // time or if regexp entry in generated code is turned off runtime switch or
8408 // at compilation.
8409#ifndef V8_NATIVE_REGEXP
8410 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8411#else // V8_NATIVE_REGEXP
8412 if (!FLAG_regexp_entry_native) {
8413 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8414 return;
8415 }
8416
8417 // Stack frame on entry.
8418 // sp[0]: last_match_info (expected JSArray)
8419 // sp[4]: previous index
8420 // sp[8]: subject string
8421 // sp[12]: JSRegExp object
8422
8423 static const int kLastMatchInfoOffset = 0 * kPointerSize;
8424 static const int kPreviousIndexOffset = 1 * kPointerSize;
8425 static const int kSubjectOffset = 2 * kPointerSize;
8426 static const int kJSRegExpOffset = 3 * kPointerSize;
8427
8428 Label runtime, invoke_regexp;
8429
8430 // Allocation of registers for this function. These are in callee save
8431 // registers and will be preserved by the call to the native RegExp code, as
8432 // this code is called using the normal C calling convention. When calling
8433 // directly from generated code the native RegExp code will not do a GC and
8434 // therefore the content of these registers are safe to use after the call.
8435 Register subject = r4;
8436 Register regexp_data = r5;
8437 Register last_match_info_elements = r6;
8438
8439 // Ensure that a RegExp stack is allocated.
8440 ExternalReference address_of_regexp_stack_memory_address =
8441 ExternalReference::address_of_regexp_stack_memory_address();
8442 ExternalReference address_of_regexp_stack_memory_size =
8443 ExternalReference::address_of_regexp_stack_memory_size();
8444 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
8445 __ ldr(r0, MemOperand(r0, 0));
8446 __ tst(r0, Operand(r0));
8447 __ b(eq, &runtime);
8448
8449 // Check that the first argument is a JSRegExp object.
8450 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
8451 ASSERT_EQ(0, kSmiTag);
8452 __ tst(r0, Operand(kSmiTagMask));
8453 __ b(eq, &runtime);
8454 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
8455 __ b(ne, &runtime);
8456
8457 // Check that the RegExp has been compiled (data contains a fixed array).
8458 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
8459 if (FLAG_debug_code) {
8460 __ tst(regexp_data, Operand(kSmiTagMask));
8461 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
8462 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
8463 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
8464 }
8465
8466 // regexp_data: RegExp data (FixedArray)
8467 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
8468 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
8469 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
8470 __ b(ne, &runtime);
8471
8472 // regexp_data: RegExp data (FixedArray)
8473 // Check that the number of captures fit in the static offsets vector buffer.
8474 __ ldr(r2,
8475 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8476 // Calculate number of capture registers (number_of_captures + 1) * 2. This
8477 // uses the asumption that smis are 2 * their untagged value.
8478 ASSERT_EQ(0, kSmiTag);
8479 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8480 __ add(r2, r2, Operand(2)); // r2 was a smi.
8481 // Check that the static offsets vector buffer is large enough.
8482 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
8483 __ b(hi, &runtime);
8484
8485 // r2: Number of capture registers
8486 // regexp_data: RegExp data (FixedArray)
8487 // Check that the second argument is a string.
8488 __ ldr(subject, MemOperand(sp, kSubjectOffset));
8489 __ tst(subject, Operand(kSmiTagMask));
8490 __ b(eq, &runtime);
8491 Condition is_string = masm->IsObjectStringType(subject, r0);
8492 __ b(NegateCondition(is_string), &runtime);
8493 // Get the length of the string to r3.
8494 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
8495
8496 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00008497 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008498 // subject: Subject string
8499 // regexp_data: RegExp data (FixedArray)
8500 // Check that the third argument is a positive smi less than the subject
8501 // string length. A negative value will be greater (unsigned comparison).
8502 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00008503 __ tst(r0, Operand(kSmiTagMask));
8504 __ b(eq, &runtime);
8505 __ cmp(r3, Operand(r0));
8506 __ b(le, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008507
8508 // r2: Number of capture registers
8509 // subject: Subject string
8510 // regexp_data: RegExp data (FixedArray)
8511 // Check that the fourth object is a JSArray object.
8512 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8513 __ tst(r0, Operand(kSmiTagMask));
8514 __ b(eq, &runtime);
8515 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
8516 __ b(ne, &runtime);
8517 // Check that the JSArray is in fast case.
8518 __ ldr(last_match_info_elements,
8519 FieldMemOperand(r0, JSArray::kElementsOffset));
8520 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008521 __ LoadRoot(ip, kFixedArrayMapRootIndex);
8522 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008523 __ b(ne, &runtime);
8524 // Check that the last match info has space for the capture registers and the
8525 // additional information.
8526 __ ldr(r0,
8527 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
8528 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
8529 __ cmp(r2, r0);
8530 __ b(gt, &runtime);
8531
8532 // subject: Subject string
8533 // regexp_data: RegExp data (FixedArray)
8534 // Check the representation and encoding of the subject string.
8535 Label seq_string;
8536 const int kStringRepresentationEncodingMask =
8537 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
8538 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8539 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8540 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8541 // First check for sequential string.
8542 ASSERT_EQ(0, kStringTag);
8543 ASSERT_EQ(0, kSeqStringTag);
8544 __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask));
8545 __ b(eq, &seq_string);
8546
8547 // subject: Subject string
8548 // regexp_data: RegExp data (FixedArray)
8549 // Check for flat cons string.
8550 // A flat cons string is a cons string where the second part is the empty
8551 // string. In that case the subject string is just the first part of the cons
8552 // string. Also in this case the first part of the cons string is known to be
8553 // a sequential string or an external string.
8554 __ and_(r0, r0, Operand(kStringRepresentationMask));
8555 __ cmp(r0, Operand(kConsStringTag));
8556 __ b(ne, &runtime);
8557 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
8558 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
8559 __ cmp(r0, r1);
8560 __ b(ne, &runtime);
8561 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
8562 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8563 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8564 ASSERT_EQ(0, kSeqStringTag);
8565 __ tst(r0, Operand(kStringRepresentationMask));
8566 __ b(nz, &runtime);
8567 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8568
8569 __ bind(&seq_string);
8570 // r1: suject string type & kStringRepresentationEncodingMask
8571 // subject: Subject string
8572 // regexp_data: RegExp data (FixedArray)
8573 // Check that the irregexp code has been generated for an ascii string. If
8574 // it has, the field contains a code object otherwise it contains the hole.
8575#ifdef DEBUG
8576 const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag;
8577 const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag;
8578 CHECK_EQ(4, kSeqAsciiString);
8579 CHECK_EQ(0, kSeqTwoByteString);
8580#endif
8581 // Find the code object based on the assumptions above.
8582 __ mov(r3, Operand(r1, ASR, 2), SetCC);
8583 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
8584 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
8585
8586 // Check that the irregexp code has been generated for the actual string
8587 // encoding. If it has, the field contains a code object otherwise it contains
8588 // the hole.
8589 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
8590 __ b(ne, &runtime);
8591
8592 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8593 // r7: code
8594 // subject: Subject string
8595 // regexp_data: RegExp data (FixedArray)
8596 // Load used arguments before starting to push arguments for call to native
8597 // RegExp code to avoid handling changing stack height.
8598 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
8599 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
8600
8601 // r1: previous index
8602 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8603 // r7: code
8604 // subject: Subject string
8605 // regexp_data: RegExp data (FixedArray)
8606 // All checks done. Now push arguments for native regexp code.
8607 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
8608
8609 static const int kRegExpExecuteArguments = 7;
8610 __ push(lr);
8611 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
8612
8613 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
8614 __ mov(r0, Operand(1));
8615 __ str(r0, MemOperand(sp, 2 * kPointerSize));
8616
8617 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
8618 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
8619 __ ldr(r0, MemOperand(r0, 0));
8620 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
8621 __ ldr(r2, MemOperand(r2, 0));
8622 __ add(r0, r0, Operand(r2));
8623 __ str(r0, MemOperand(sp, 1 * kPointerSize));
8624
8625 // Argument 5 (sp[0]): static offsets vector buffer.
8626 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
8627 __ str(r0, MemOperand(sp, 0 * kPointerSize));
8628
8629 // For arguments 4 and 3 get string length, calculate start of string data and
8630 // calculate the shift of the index (0 for ASCII and 1 for two byte).
8631 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00008632 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008633 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
8634 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8635 __ eor(r3, r3, Operand(1));
8636 // Argument 4 (r3): End of string data
8637 // Argument 3 (r2): Start of string data
8638 __ add(r2, r9, Operand(r1, LSL, r3));
8639 __ add(r3, r9, Operand(r0, LSL, r3));
8640
8641 // Argument 2 (r1): Previous index.
8642 // Already there
8643
8644 // Argument 1 (r0): Subject string.
8645 __ mov(r0, subject);
8646
8647 // Locate the code entry and call it.
8648 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
8649 __ CallCFunction(r7, kRegExpExecuteArguments);
8650 __ pop(lr);
8651
8652 // r0: result
8653 // subject: subject string (callee saved)
8654 // regexp_data: RegExp data (callee saved)
8655 // last_match_info_elements: Last match info elements (callee saved)
8656
8657 // Check the result.
8658 Label success;
8659 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
8660 __ b(eq, &success);
8661 Label failure;
8662 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
8663 __ b(eq, &failure);
8664 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
8665 // If not exception it can only be retry. Handle that in the runtime system.
8666 __ b(ne, &runtime);
8667 // Result must now be exception. If there is no pending exception already a
8668 // stack overflow (on the backtrack stack) was detected in RegExp code but
8669 // haven't created the exception yet. Handle that in the runtime system.
8670 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
8671 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
8672 __ ldr(r0, MemOperand(r0, 0));
8673 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
8674 __ ldr(r1, MemOperand(r1, 0));
8675 __ cmp(r0, r1);
8676 __ b(eq, &runtime);
8677 __ bind(&failure);
8678 // For failure and exception return null.
8679 __ mov(r0, Operand(Factory::null_value()));
8680 __ add(sp, sp, Operand(4 * kPointerSize));
8681 __ Ret();
8682
8683 // Process the result from the native regexp code.
8684 __ bind(&success);
8685 __ ldr(r1,
8686 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8687 // Calculate number of capture registers (number_of_captures + 1) * 2.
8688 ASSERT_EQ(0, kSmiTag);
8689 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8690 __ add(r1, r1, Operand(2)); // r1 was a smi.
8691
8692 // r1: number of capture registers
8693 // r4: subject string
8694 // Store the capture count.
8695 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
8696 __ str(r2, FieldMemOperand(last_match_info_elements,
8697 RegExpImpl::kLastCaptureCountOffset));
8698 // Store last subject and last input.
8699 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
8700 __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto.
8701 __ str(subject,
8702 FieldMemOperand(last_match_info_elements,
8703 RegExpImpl::kLastSubjectOffset));
8704 __ RecordWrite(r3, r2, r7);
8705 __ str(subject,
8706 FieldMemOperand(last_match_info_elements,
8707 RegExpImpl::kLastInputOffset));
8708 __ mov(r3, last_match_info_elements);
8709 __ mov(r2, Operand(RegExpImpl::kLastInputOffset));
8710 __ RecordWrite(r3, r2, r7);
8711
8712 // Get the static offsets vector filled by the native regexp code.
8713 ExternalReference address_of_static_offsets_vector =
8714 ExternalReference::address_of_static_offsets_vector();
8715 __ mov(r2, Operand(address_of_static_offsets_vector));
8716
8717 // r1: number of capture registers
8718 // r2: offsets vector
8719 Label next_capture, done;
8720 // Capture register counter starts from number of capture registers and
8721 // counts down until wraping after zero.
8722 __ add(r0,
8723 last_match_info_elements,
8724 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
8725 __ bind(&next_capture);
8726 __ sub(r1, r1, Operand(1), SetCC);
8727 __ b(mi, &done);
8728 // Read the value from the static offsets vector buffer.
8729 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
8730 // Store the smi value in the last match info.
8731 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
8732 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
8733 __ jmp(&next_capture);
8734 __ bind(&done);
8735
8736 // Return last match info.
8737 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8738 __ add(sp, sp, Operand(4 * kPointerSize));
8739 __ Ret();
8740
8741 // Do the runtime call to execute the regexp.
8742 __ bind(&runtime);
8743 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8744#endif // V8_NATIVE_REGEXP
8745}
8746
8747
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008748void CallFunctionStub::Generate(MacroAssembler* masm) {
8749 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008750
8751 // If the receiver might be a value (string, number or boolean) check for this
8752 // and box it if it is.
8753 if (ReceiverMightBeValue()) {
8754 // Get the receiver from the stack.
8755 // function, receiver [, arguments]
8756 Label receiver_is_value, receiver_is_js_object;
8757 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
8758
8759 // Check if receiver is a smi (which is a number value).
8760 __ BranchOnSmi(r1, &receiver_is_value);
8761
8762 // Check if the receiver is a valid JS object.
8763 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
8764 __ b(ge, &receiver_is_js_object);
8765
8766 // Call the runtime to box the value.
8767 __ bind(&receiver_is_value);
8768 __ EnterInternalFrame();
8769 __ push(r1);
8770 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
8771 __ LeaveInternalFrame();
8772 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
8773
8774 __ bind(&receiver_is_js_object);
8775 }
8776
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008777 // Get the function to call from the stack.
8778 // function, receiver [, arguments]
8779 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
8780
8781 // Check that the function is really a JavaScript function.
8782 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008783 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008784 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008785 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008786 __ b(ne, &slow);
8787
8788 // Fast-case: Invoke the function now.
8789 // r1: pushed function
8790 ParameterCount actual(argc_);
8791 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
8792
8793 // Slow-case: Non-function called.
8794 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00008795 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
8796 // of the original receiver from the call site).
8797 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008798 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008799 __ mov(r2, Operand(0));
8800 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
8801 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
8802 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008803}
8804
8805
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008806// Unfortunately you have to run without snapshots to see most of these
8807// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008808const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008809 if (name_ != NULL) return name_;
8810 const int kMaxNameLength = 100;
8811 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
8812 if (name_ == NULL) return "OOM";
8813
8814 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008815 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008816 case lt: cc_name = "LT"; break;
8817 case gt: cc_name = "GT"; break;
8818 case le: cc_name = "LE"; break;
8819 case ge: cc_name = "GE"; break;
8820 case eq: cc_name = "EQ"; break;
8821 case ne: cc_name = "NE"; break;
8822 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008823 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008824
8825 const char* strict_name = "";
8826 if (strict_ && (cc_ == eq || cc_ == ne)) {
8827 strict_name = "_STRICT";
8828 }
8829
8830 const char* never_nan_nan_name = "";
8831 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
8832 never_nan_nan_name = "_NO_NAN";
8833 }
8834
8835 const char* include_number_compare_name = "";
8836 if (!include_number_compare_) {
8837 include_number_compare_name = "_NO_NUMBER";
8838 }
8839
8840 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
8841 "CompareStub_%s%s%s%s",
8842 cc_name,
8843 strict_name,
8844 never_nan_nan_name,
8845 include_number_compare_name);
8846 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008847}
8848
8849
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008850int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008851 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
8852 // stubs the never NaN NaN condition is only taken into account if the
8853 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008854 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008855 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
8856 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008857 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
8858 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008859}
8860
8861
ager@chromium.orgac091b72010-05-05 07:34:42 +00008862void StringHelper::GenerateFastCharCodeAt(MacroAssembler* masm,
8863 Register object,
8864 Register index,
8865 Register scratch,
8866 Register result,
8867 Label* receiver_not_string,
8868 Label* index_not_smi,
8869 Label* index_out_of_range,
8870 Label* slow_case) {
8871 Label not_a_flat_string;
8872 Label try_again_with_new_string;
8873 Label ascii_string;
8874 Label got_char_code;
8875
8876 // If the receiver is a smi trigger the non-string case.
8877 __ BranchOnSmi(object, receiver_not_string);
8878
8879 // Fetch the instance type of the receiver into result register.
8880 __ ldr(result, FieldMemOperand(object, HeapObject::kMapOffset));
8881 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
8882 // If the receiver is not a string trigger the non-string case.
8883 __ tst(result, Operand(kIsNotStringMask));
8884 __ b(ne, receiver_not_string);
8885
8886 // If the index is non-smi trigger the non-smi case.
8887 __ BranchOnNotSmi(index, index_not_smi);
8888
8889 // Check for index out of range.
8890 __ ldr(scratch, FieldMemOperand(object, String::kLengthOffset));
8891 // Now scratch has the length of the string. Compare with the index.
8892 __ cmp(scratch, Operand(index));
8893 __ b(ls, index_out_of_range);
8894
8895 __ bind(&try_again_with_new_string);
8896 // ----------- S t a t e -------------
8897 // -- object : string to access
8898 // -- result : instance type of the string
8899 // -- scratch : non-negative index < length
8900 // -----------------------------------
8901
8902 // We need special handling for non-flat strings.
8903 ASSERT_EQ(0, kSeqStringTag);
8904 __ tst(result, Operand(kStringRepresentationMask));
8905 __ b(ne, &not_a_flat_string);
8906
8907 // Check for 1-byte or 2-byte string.
8908 ASSERT_EQ(0, kTwoByteStringTag);
8909 __ tst(result, Operand(kStringEncodingMask));
8910 __ b(ne, &ascii_string);
8911
8912 // 2-byte string. We can add without shifting since the Smi tag size is the
8913 // log2 of the number of bytes in a two-byte character.
8914 ASSERT_EQ(1, kSmiTagSize);
8915 ASSERT_EQ(0, kSmiShiftSize);
8916 __ add(scratch, object, Operand(index));
8917 __ ldrh(result, FieldMemOperand(scratch, SeqTwoByteString::kHeaderSize));
8918 __ jmp(&got_char_code);
8919
8920 // Handle non-flat strings.
8921 __ bind(&not_a_flat_string);
8922 __ and_(result, result, Operand(kStringRepresentationMask));
8923 __ cmp(result, Operand(kConsStringTag));
8924 __ b(ne, slow_case);
8925
8926 // ConsString.
8927 // Check whether the right hand side is the empty string (i.e. if
8928 // this is really a flat string in a cons string). If that is not
8929 // the case we would rather go to the runtime system now to flatten
8930 // the string.
8931 __ ldr(result, FieldMemOperand(object, ConsString::kSecondOffset));
8932 __ LoadRoot(scratch, Heap::kEmptyStringRootIndex);
8933 __ cmp(result, Operand(scratch));
8934 __ b(ne, slow_case);
8935
8936 // Get the first of the two strings and load its instance type.
8937 __ ldr(object, FieldMemOperand(object, ConsString::kFirstOffset));
8938 __ ldr(result, FieldMemOperand(object, HeapObject::kMapOffset));
8939 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
8940 __ jmp(&try_again_with_new_string);
8941
8942 // ASCII string.
8943 __ bind(&ascii_string);
8944 __ add(scratch, object, Operand(index, LSR, kSmiTagSize));
8945 __ ldrb(result, FieldMemOperand(scratch, SeqAsciiString::kHeaderSize));
8946
8947 __ bind(&got_char_code);
8948 __ mov(result, Operand(result, LSL, kSmiTagSize));
8949}
8950
8951
8952void StringHelper::GenerateCharFromCode(MacroAssembler* masm,
8953 Register code,
8954 Register scratch,
8955 Register result,
8956 InvokeFlag flag) {
8957 ASSERT(!code.is(result));
8958
8959 Label slow_case;
8960 Label exit;
8961
8962 // Fast case of Heap::LookupSingleCharacterStringFromCode.
8963 ASSERT(kSmiTag == 0);
8964 ASSERT(kSmiShiftSize == 0);
8965 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
8966 __ tst(code, Operand(kSmiTagMask |
8967 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
8968 __ b(nz, &slow_case);
8969
8970 ASSERT(kSmiTag == 0);
8971 __ mov(result, Operand(Factory::single_character_string_cache()));
8972 __ add(result, result, Operand(code, LSL, kPointerSizeLog2 - kSmiTagSize));
8973 __ ldr(result, MemOperand(result, FixedArray::kHeaderSize - kHeapObjectTag));
8974 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
8975 __ cmp(result, scratch);
8976 __ b(eq, &slow_case);
8977 __ b(&exit);
8978
8979 __ bind(&slow_case);
8980 if (flag == CALL_FUNCTION) {
8981 __ push(code);
8982 __ CallRuntime(Runtime::kCharFromCode, 1);
8983 if (!result.is(r0)) {
8984 __ mov(result, r0);
8985 }
8986 } else {
8987 ASSERT(flag == JUMP_FUNCTION);
8988 ASSERT(result.is(r0));
8989 __ push(code);
8990 __ TailCallRuntime(Runtime::kCharFromCode, 1, 1);
8991 }
8992
8993 __ bind(&exit);
8994 if (flag == JUMP_FUNCTION) {
8995 ASSERT(result.is(r0));
8996 __ Ret();
8997 }
8998}
8999
9000
9001void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
9002 Register dest,
9003 Register src,
9004 Register count,
9005 Register scratch,
9006 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009007 Label loop;
9008 Label done;
9009 // This loop just copies one character at a time, as it is only used for very
9010 // short strings.
9011 if (!ascii) {
9012 __ add(count, count, Operand(count), SetCC);
9013 } else {
9014 __ cmp(count, Operand(0));
9015 }
9016 __ b(eq, &done);
9017
9018 __ bind(&loop);
9019 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
9020 // Perform sub between load and dependent store to get the load time to
9021 // complete.
9022 __ sub(count, count, Operand(1), SetCC);
9023 __ strb(scratch, MemOperand(dest, 1, PostIndex));
9024 // last iteration.
9025 __ b(gt, &loop);
9026
9027 __ bind(&done);
9028}
9029
9030
9031enum CopyCharactersFlags {
9032 COPY_ASCII = 1,
9033 DEST_ALWAYS_ALIGNED = 2
9034};
9035
9036
ager@chromium.orgac091b72010-05-05 07:34:42 +00009037void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
9038 Register dest,
9039 Register src,
9040 Register count,
9041 Register scratch1,
9042 Register scratch2,
9043 Register scratch3,
9044 Register scratch4,
9045 Register scratch5,
9046 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009047 bool ascii = (flags & COPY_ASCII) != 0;
9048 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
9049
9050 if (dest_always_aligned && FLAG_debug_code) {
9051 // Check that destination is actually word aligned if the flag says
9052 // that it is.
9053 __ tst(dest, Operand(kPointerAlignmentMask));
9054 __ Check(eq, "Destination of copy not aligned.");
9055 }
9056
9057 const int kReadAlignment = 4;
9058 const int kReadAlignmentMask = kReadAlignment - 1;
9059 // Ensure that reading an entire aligned word containing the last character
9060 // of a string will not read outside the allocated area (because we pad up
9061 // to kObjectAlignment).
9062 ASSERT(kObjectAlignment >= kReadAlignment);
9063 // Assumes word reads and writes are little endian.
9064 // Nothing to do for zero characters.
9065 Label done;
9066 if (!ascii) {
9067 __ add(count, count, Operand(count), SetCC);
9068 } else {
9069 __ cmp(count, Operand(0));
9070 }
9071 __ b(eq, &done);
9072
9073 // Assume that you cannot read (or write) unaligned.
9074 Label byte_loop;
9075 // Must copy at least eight bytes, otherwise just do it one byte at a time.
9076 __ cmp(count, Operand(8));
9077 __ add(count, dest, Operand(count));
9078 Register limit = count; // Read until src equals this.
9079 __ b(lt, &byte_loop);
9080
9081 if (!dest_always_aligned) {
9082 // Align dest by byte copying. Copies between zero and three bytes.
9083 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
9084 Label dest_aligned;
9085 __ b(eq, &dest_aligned);
9086 __ cmp(scratch4, Operand(2));
9087 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
9088 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
9089 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
9090 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9091 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
9092 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
9093 __ bind(&dest_aligned);
9094 }
9095
9096 Label simple_loop;
9097
9098 __ sub(scratch4, dest, Operand(src));
9099 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
9100 __ b(eq, &simple_loop);
9101 // Shift register is number of bits in a source word that
9102 // must be combined with bits in the next source word in order
9103 // to create a destination word.
9104
9105 // Complex loop for src/dst that are not aligned the same way.
9106 {
9107 Label loop;
9108 __ mov(scratch4, Operand(scratch4, LSL, 3));
9109 Register left_shift = scratch4;
9110 __ and_(src, src, Operand(~3)); // Round down to load previous word.
9111 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9112 // Store the "shift" most significant bits of scratch in the least
9113 // signficant bits (i.e., shift down by (32-shift)).
9114 __ rsb(scratch2, left_shift, Operand(32));
9115 Register right_shift = scratch2;
9116 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
9117
9118 __ bind(&loop);
9119 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
9120 __ sub(scratch5, limit, Operand(dest));
9121 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
9122 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9123 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
9124 // Loop if four or more bytes left to copy.
9125 // Compare to eight, because we did the subtract before increasing dst.
9126 __ sub(scratch5, scratch5, Operand(8), SetCC);
9127 __ b(ge, &loop);
9128 }
9129 // There is now between zero and three bytes left to copy (negative that
9130 // number is in scratch5), and between one and three bytes already read into
9131 // scratch1 (eight times that number in scratch4). We may have read past
9132 // the end of the string, but because objects are aligned, we have not read
9133 // past the end of the object.
9134 // Find the minimum of remaining characters to move and preloaded characters
9135 // and write those as bytes.
9136 __ add(scratch5, scratch5, Operand(4), SetCC);
9137 __ b(eq, &done);
9138 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
9139 // Move minimum of bytes read and bytes left to copy to scratch4.
9140 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
9141 // Between one and three (value in scratch5) characters already read into
9142 // scratch ready to write.
9143 __ cmp(scratch5, Operand(2));
9144 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9145 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
9146 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
9147 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
9148 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
9149 // Copy any remaining bytes.
9150 __ b(&byte_loop);
9151
9152 // Simple loop.
9153 // Copy words from src to dst, until less than four bytes left.
9154 // Both src and dest are word aligned.
9155 __ bind(&simple_loop);
9156 {
9157 Label loop;
9158 __ bind(&loop);
9159 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9160 __ sub(scratch3, limit, Operand(dest));
9161 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9162 // Compare to 8, not 4, because we do the substraction before increasing
9163 // dest.
9164 __ cmp(scratch3, Operand(8));
9165 __ b(ge, &loop);
9166 }
9167
9168 // Copy bytes from src to dst until dst hits limit.
9169 __ bind(&byte_loop);
9170 __ cmp(dest, Operand(limit));
9171 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
9172 __ b(ge, &done);
9173 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9174 __ b(&byte_loop);
9175
9176 __ bind(&done);
9177}
9178
9179
ager@chromium.orgac091b72010-05-05 07:34:42 +00009180void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
9181 Register c1,
9182 Register c2,
9183 Register scratch1,
9184 Register scratch2,
9185 Register scratch3,
9186 Register scratch4,
9187 Register scratch5,
9188 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009189 // Register scratch3 is the general scratch register in this function.
9190 Register scratch = scratch3;
9191
9192 // Make sure that both characters are not digits as such strings has a
9193 // different hash algorithm. Don't try to look for these in the symbol table.
9194 Label not_array_index;
9195 __ sub(scratch, c1, Operand(static_cast<int>('0')));
9196 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
9197 __ b(hi, &not_array_index);
9198 __ sub(scratch, c2, Operand(static_cast<int>('0')));
9199 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
9200
9201 // If check failed combine both characters into single halfword.
9202 // This is required by the contract of the method: code at the
9203 // not_found branch expects this combination in c1 register
9204 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
9205 __ b(ls, not_found);
9206
9207 __ bind(&not_array_index);
9208 // Calculate the two character string hash.
9209 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009210 StringHelper::GenerateHashInit(masm, hash, c1);
9211 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
9212 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009213
9214 // Collect the two characters in a register.
9215 Register chars = c1;
9216 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
9217
9218 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
9219 // hash: hash of two character string.
9220
9221 // Load symbol table
9222 // Load address of first element of the symbol table.
9223 Register symbol_table = c2;
9224 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
9225
9226 // Load undefined value
9227 Register undefined = scratch4;
9228 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
9229
9230 // Calculate capacity mask from the symbol table capacity.
9231 Register mask = scratch2;
9232 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
9233 __ mov(mask, Operand(mask, ASR, 1));
9234 __ sub(mask, mask, Operand(1));
9235
9236 // Calculate untagged address of the first element of the symbol table.
9237 Register first_symbol_table_element = symbol_table;
9238 __ add(first_symbol_table_element, symbol_table,
9239 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
9240
9241 // Registers
9242 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
9243 // hash: hash of two character string
9244 // mask: capacity mask
9245 // first_symbol_table_element: address of the first element of
9246 // the symbol table
9247 // scratch: -
9248
9249 // Perform a number of probes in the symbol table.
9250 static const int kProbes = 4;
9251 Label found_in_symbol_table;
9252 Label next_probe[kProbes];
9253 for (int i = 0; i < kProbes; i++) {
9254 Register candidate = scratch5; // Scratch register contains candidate.
9255
9256 // Calculate entry in symbol table.
9257 if (i > 0) {
9258 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
9259 } else {
9260 __ mov(candidate, hash);
9261 }
9262
9263 __ and_(candidate, candidate, Operand(mask));
9264
9265 // Load the entry from the symble table.
9266 ASSERT_EQ(1, SymbolTable::kEntrySize);
9267 __ ldr(candidate,
9268 MemOperand(first_symbol_table_element,
9269 candidate,
9270 LSL,
9271 kPointerSizeLog2));
9272
9273 // If entry is undefined no string with this hash can be found.
9274 __ cmp(candidate, undefined);
9275 __ b(eq, not_found);
9276
9277 // If length is not 2 the string is not a candidate.
9278 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009279 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009280 __ b(ne, &next_probe[i]);
9281
9282 // Check that the candidate is a non-external ascii string.
9283 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
9284 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
9285 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
9286 &next_probe[i]);
9287
9288 // Check if the two characters match.
9289 // Assumes that word load is little endian.
9290 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
9291 __ cmp(chars, scratch);
9292 __ b(eq, &found_in_symbol_table);
9293 __ bind(&next_probe[i]);
9294 }
9295
9296 // No matching 2 character string found by probing.
9297 __ jmp(not_found);
9298
9299 // Scratch register contains result when we fall through to here.
9300 Register result = scratch;
9301 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00009302 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009303}
9304
9305
ager@chromium.orgac091b72010-05-05 07:34:42 +00009306void StringHelper::GenerateHashInit(MacroAssembler* masm,
9307 Register hash,
9308 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009309 // hash = character + (character << 10);
9310 __ add(hash, character, Operand(character, LSL, 10));
9311 // hash ^= hash >> 6;
9312 __ eor(hash, hash, Operand(hash, ASR, 6));
9313}
9314
9315
ager@chromium.orgac091b72010-05-05 07:34:42 +00009316void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
9317 Register hash,
9318 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009319 // hash += character;
9320 __ add(hash, hash, Operand(character));
9321 // hash += hash << 10;
9322 __ add(hash, hash, Operand(hash, LSL, 10));
9323 // hash ^= hash >> 6;
9324 __ eor(hash, hash, Operand(hash, ASR, 6));
9325}
9326
9327
ager@chromium.orgac091b72010-05-05 07:34:42 +00009328void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
9329 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009330 // hash += hash << 3;
9331 __ add(hash, hash, Operand(hash, LSL, 3));
9332 // hash ^= hash >> 11;
9333 __ eor(hash, hash, Operand(hash, ASR, 11));
9334 // hash += hash << 15;
9335 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
9336
9337 // if (hash == 0) hash = 27;
9338 __ mov(hash, Operand(27), LeaveCC, nz);
9339}
9340
9341
ager@chromium.org5c838252010-02-19 08:53:10 +00009342void SubStringStub::Generate(MacroAssembler* masm) {
9343 Label runtime;
9344
9345 // Stack frame on entry.
9346 // lr: return address
9347 // sp[0]: to
9348 // sp[4]: from
9349 // sp[8]: string
9350
9351 // This stub is called from the native-call %_SubString(...), so
9352 // nothing can be assumed about the arguments. It is tested that:
9353 // "string" is a sequential string,
9354 // both "from" and "to" are smis, and
9355 // 0 <= from <= to <= string.length.
9356 // If any of these assumptions fail, we call the runtime system.
9357
9358 static const int kToOffset = 0 * kPointerSize;
9359 static const int kFromOffset = 1 * kPointerSize;
9360 static const int kStringOffset = 2 * kPointerSize;
9361
9362
9363 // Check bounds and smi-ness.
9364 __ ldr(r7, MemOperand(sp, kToOffset));
9365 __ ldr(r6, MemOperand(sp, kFromOffset));
9366 ASSERT_EQ(0, kSmiTag);
9367 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
9368 // I.e., arithmetic shift right by one un-smi-tags.
9369 __ mov(r2, Operand(r7, ASR, 1), SetCC);
9370 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
9371 // If either r2 or r6 had the smi tag bit set, then carry is set now.
9372 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
9373 __ b(mi, &runtime); // From is negative.
9374
9375 __ sub(r2, r2, Operand(r3), SetCC);
9376 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009377 // Special handling of sub-strings of length 1 and 2. One character strings
9378 // are handled in the runtime system (looked up in the single character
9379 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00009380 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009381 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00009382
9383 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009384 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009385 // r6: from (smi)
9386 // r7: to (smi)
9387
9388 // Make sure first argument is a sequential (or flat) string.
9389 __ ldr(r5, MemOperand(sp, kStringOffset));
9390 ASSERT_EQ(0, kSmiTag);
9391 __ tst(r5, Operand(kSmiTagMask));
9392 __ b(eq, &runtime);
9393 Condition is_string = masm->IsObjectStringType(r5, r1);
9394 __ b(NegateCondition(is_string), &runtime);
9395
9396 // r1: instance type
9397 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009398 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009399 // r5: string
9400 // r6: from (smi)
9401 // r7: to (smi)
9402 Label seq_string;
9403 __ and_(r4, r1, Operand(kStringRepresentationMask));
9404 ASSERT(kSeqStringTag < kConsStringTag);
9405 ASSERT(kExternalStringTag > kConsStringTag);
9406 __ cmp(r4, Operand(kConsStringTag));
9407 __ b(gt, &runtime); // External strings go to runtime.
9408 __ b(lt, &seq_string); // Sequential strings are handled directly.
9409
9410 // Cons string. Try to recurse (once) on the first substring.
9411 // (This adds a little more generality than necessary to handle flattened
9412 // cons strings, but not much).
9413 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
9414 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
9415 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9416 __ tst(r1, Operand(kStringRepresentationMask));
9417 ASSERT_EQ(0, kSeqStringTag);
9418 __ b(ne, &runtime); // Cons and External strings go to runtime.
9419
9420 // Definitly a sequential string.
9421 __ bind(&seq_string);
9422
9423 // r1: instance type.
9424 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009425 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009426 // r5: string
9427 // r6: from (smi)
9428 // r7: to (smi)
9429 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009430 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +00009431 __ b(lt, &runtime); // Fail if to > length.
9432
9433 // r1: instance type.
9434 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009435 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009436 // r5: string.
9437 // r6: from offset (smi)
9438 // Check for flat ascii string.
9439 Label non_ascii_flat;
9440 __ tst(r1, Operand(kStringEncodingMask));
9441 ASSERT_EQ(0, kTwoByteStringTag);
9442 __ b(eq, &non_ascii_flat);
9443
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009444 Label result_longer_than_two;
9445 __ cmp(r2, Operand(2));
9446 __ b(gt, &result_longer_than_two);
9447
9448 // Sub string of length 2 requested.
9449 // Get the two characters forming the sub string.
9450 __ add(r5, r5, Operand(r3));
9451 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
9452 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
9453
9454 // Try to lookup two character string in symbol table.
9455 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009456 StringHelper::GenerateTwoCharacterSymbolTableProbe(
9457 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009458 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9459 __ add(sp, sp, Operand(3 * kPointerSize));
9460 __ Ret();
9461
9462 // r2: result string length.
9463 // r3: two characters combined into halfword in little endian byte order.
9464 __ bind(&make_two_character_string);
9465 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
9466 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9467 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9468 __ add(sp, sp, Operand(3 * kPointerSize));
9469 __ Ret();
9470
9471 __ bind(&result_longer_than_two);
9472
ager@chromium.org5c838252010-02-19 08:53:10 +00009473 // Allocate the result.
9474 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
9475
9476 // r0: result string.
9477 // r2: result string length.
9478 // r5: string.
9479 // r6: from offset (smi)
9480 // Locate first character of result.
9481 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9482 // Locate 'from' character of string.
9483 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9484 __ add(r5, r5, Operand(r6, ASR, 1));
9485
9486 // r0: result string.
9487 // r1: first character of result string.
9488 // r2: result string length.
9489 // r5: first character of sub string to copy.
9490 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009491 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9492 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +00009493 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9494 __ add(sp, sp, Operand(3 * kPointerSize));
9495 __ Ret();
9496
9497 __ bind(&non_ascii_flat);
9498 // r2: result string length.
9499 // r5: string.
9500 // r6: from offset (smi)
9501 // Check for flat two byte string.
9502
9503 // Allocate the result.
9504 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
9505
9506 // r0: result string.
9507 // r2: result string length.
9508 // r5: string.
9509 // Locate first character of result.
9510 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9511 // Locate 'from' character of string.
9512 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9513 // As "from" is a smi it is 2 times the value which matches the size of a two
9514 // byte character.
9515 __ add(r5, r5, Operand(r6));
9516
9517 // r0: result string.
9518 // r1: first character of result.
9519 // r2: result length.
9520 // r5: first character of string to copy.
9521 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009522 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9523 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +00009524 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9525 __ add(sp, sp, Operand(3 * kPointerSize));
9526 __ Ret();
9527
9528 // Just jump to runtime to create the sub string.
9529 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009530 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009531}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009532
9533
9534void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
9535 Register left,
9536 Register right,
9537 Register scratch1,
9538 Register scratch2,
9539 Register scratch3,
9540 Register scratch4) {
9541 Label compare_lengths;
9542 // Find minimum length and length difference.
9543 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
9544 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
9545 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
9546 Register length_delta = scratch3;
9547 __ mov(scratch1, scratch2, LeaveCC, gt);
9548 Register min_length = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009549 ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009550 __ tst(min_length, Operand(min_length));
9551 __ b(eq, &compare_lengths);
9552
ager@chromium.orgac091b72010-05-05 07:34:42 +00009553 // Untag smi.
9554 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
9555
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009556 // Setup registers so that we only need to increment one register
9557 // in the loop.
9558 __ add(scratch2, min_length,
9559 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9560 __ add(left, left, Operand(scratch2));
9561 __ add(right, right, Operand(scratch2));
9562 // Registers left and right points to the min_length character of strings.
9563 __ rsb(min_length, min_length, Operand(-1));
9564 Register index = min_length;
9565 // Index starts at -min_length.
9566
9567 {
9568 // Compare loop.
9569 Label loop;
9570 __ bind(&loop);
9571 // Compare characters.
9572 __ add(index, index, Operand(1), SetCC);
9573 __ ldrb(scratch2, MemOperand(left, index), ne);
9574 __ ldrb(scratch4, MemOperand(right, index), ne);
9575 // Skip to compare lengths with eq condition true.
9576 __ b(eq, &compare_lengths);
9577 __ cmp(scratch2, scratch4);
9578 __ b(eq, &loop);
9579 // Fallthrough with eq condition false.
9580 }
9581 // Compare lengths - strings up to min-length are equal.
9582 __ bind(&compare_lengths);
9583 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
9584 // Use zero length_delta as result.
9585 __ mov(r0, Operand(length_delta), SetCC, eq);
9586 // Fall through to here if characters compare not-equal.
9587 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
9588 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
9589 __ Ret();
9590}
9591
9592
9593void StringCompareStub::Generate(MacroAssembler* masm) {
9594 Label runtime;
9595
9596 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00009597 // sp[0]: right string
9598 // sp[4]: left string
9599 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
9600 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009601
9602 Label not_same;
9603 __ cmp(r0, r1);
9604 __ b(ne, &not_same);
9605 ASSERT_EQ(0, EQUAL);
9606 ASSERT_EQ(0, kSmiTag);
9607 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
9608 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
9609 __ add(sp, sp, Operand(2 * kPointerSize));
9610 __ Ret();
9611
9612 __ bind(&not_same);
9613
9614 // Check that both objects are sequential ascii strings.
9615 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
9616
9617 // Compare flat ascii strings natively. Remove arguments from stack first.
9618 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
9619 __ add(sp, sp, Operand(2 * kPointerSize));
9620 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
9621
9622 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
9623 // tagged as a small integer.
9624 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009625 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009626}
9627
9628
ager@chromium.org5c838252010-02-19 08:53:10 +00009629void StringAddStub::Generate(MacroAssembler* masm) {
9630 Label string_add_runtime;
9631 // Stack on entry:
9632 // sp[0]: second argument.
9633 // sp[4]: first argument.
9634
9635 // Load the two arguments.
9636 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
9637 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
9638
9639 // Make sure that both arguments are strings if not known in advance.
9640 if (string_check_) {
9641 ASSERT_EQ(0, kSmiTag);
9642 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
9643 // Load instance types.
9644 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9645 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9646 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9647 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9648 ASSERT_EQ(0, kStringTag);
9649 // If either is not a string, go to runtime.
9650 __ tst(r4, Operand(kIsNotStringMask));
9651 __ tst(r5, Operand(kIsNotStringMask), eq);
9652 __ b(ne, &string_add_runtime);
9653 }
9654
9655 // Both arguments are strings.
9656 // r0: first string
9657 // r1: second string
9658 // r4: first string instance type (if string_check_)
9659 // r5: second string instance type (if string_check_)
9660 {
9661 Label strings_not_empty;
9662 // Check if either of the strings are empty. In that case return the other.
9663 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
9664 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009665 ASSERT(kSmiTag == 0);
9666 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +00009667 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009668 ASSERT(kSmiTag == 0);
9669 // Else test if second string is empty.
9670 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +00009671 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
9672
9673 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9674 __ add(sp, sp, Operand(2 * kPointerSize));
9675 __ Ret();
9676
9677 __ bind(&strings_not_empty);
9678 }
9679
ager@chromium.orgac091b72010-05-05 07:34:42 +00009680 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
9681 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009682 // Both strings are non-empty.
9683 // r0: first string
9684 // r1: second string
9685 // r2: length of first string
9686 // r3: length of second string
9687 // r4: first string instance type (if string_check_)
9688 // r5: second string instance type (if string_check_)
9689 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009690 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00009691 // Adding two lengths can't overflow.
9692 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
9693 __ add(r6, r2, Operand(r3));
9694 // Use the runtime system when adding two one character strings, as it
9695 // contains optimizations for this specific case using the symbol table.
9696 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009697 __ b(ne, &longer_than_two);
9698
9699 // Check that both strings are non-external ascii strings.
9700 if (!string_check_) {
9701 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9702 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9703 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9704 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9705 }
9706 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
9707 &string_add_runtime);
9708
9709 // Get the two characters forming the sub string.
9710 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9711 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
9712
9713 // Try to lookup two character string in symbol table. If it is not found
9714 // just allocate a new one.
9715 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009716 StringHelper::GenerateTwoCharacterSymbolTableProbe(
9717 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009718 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9719 __ add(sp, sp, Operand(2 * kPointerSize));
9720 __ Ret();
9721
9722 __ bind(&make_two_character_string);
9723 // Resulting string has length 2 and first chars of two strings
9724 // are combined into single halfword in r2 register.
9725 // So we can fill resulting string without two loops by a single
9726 // halfword store instruction (which assumes that processor is
9727 // in a little endian mode)
9728 __ mov(r6, Operand(2));
9729 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
9730 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9731 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9732 __ add(sp, sp, Operand(2 * kPointerSize));
9733 __ Ret();
9734
9735 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00009736 // Check if resulting string will be flat.
9737 __ cmp(r6, Operand(String::kMinNonFlatLength));
9738 __ b(lt, &string_add_flat_result);
9739 // Handle exceptionally long strings in the runtime system.
9740 ASSERT((String::kMaxLength & 0x80000000) == 0);
9741 ASSERT(IsPowerOf2(String::kMaxLength + 1));
9742 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
9743 __ cmp(r6, Operand(String::kMaxLength + 1));
9744 __ b(hs, &string_add_runtime);
9745
9746 // If result is not supposed to be flat, allocate a cons string object.
9747 // If both strings are ascii the result is an ascii cons string.
9748 if (!string_check_) {
9749 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9750 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9751 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9752 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9753 }
9754 Label non_ascii, allocated;
9755 ASSERT_EQ(0, kTwoByteStringTag);
9756 __ tst(r4, Operand(kStringEncodingMask));
9757 __ tst(r5, Operand(kStringEncodingMask), ne);
9758 __ b(eq, &non_ascii);
9759
9760 // Allocate an ASCII cons string.
9761 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
9762 __ bind(&allocated);
9763 // Fill the fields of the cons string.
9764 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
9765 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
9766 __ mov(r0, Operand(r7));
9767 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9768 __ add(sp, sp, Operand(2 * kPointerSize));
9769 __ Ret();
9770
9771 __ bind(&non_ascii);
9772 // Allocate a two byte cons string.
9773 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
9774 __ jmp(&allocated);
9775
9776 // Handle creating a flat result. First check that both strings are
9777 // sequential and that they have the same encoding.
9778 // r0: first string
9779 // r1: second string
9780 // r2: length of first string
9781 // r3: length of second string
9782 // r4: first string instance type (if string_check_)
9783 // r5: second string instance type (if string_check_)
9784 // r6: sum of lengths.
9785 __ bind(&string_add_flat_result);
9786 if (!string_check_) {
9787 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9788 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9789 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9790 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9791 }
9792 // Check that both strings are sequential.
9793 ASSERT_EQ(0, kSeqStringTag);
9794 __ tst(r4, Operand(kStringRepresentationMask));
9795 __ tst(r5, Operand(kStringRepresentationMask), eq);
9796 __ b(ne, &string_add_runtime);
9797 // Now check if both strings have the same encoding (ASCII/Two-byte).
9798 // r0: first string.
9799 // r1: second string.
9800 // r2: length of first string.
9801 // r3: length of second string.
9802 // r6: sum of lengths..
9803 Label non_ascii_string_add_flat_result;
9804 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
9805 __ eor(r7, r4, Operand(r5));
9806 __ tst(r7, Operand(kStringEncodingMask));
9807 __ b(ne, &string_add_runtime);
9808 // And see if it's ASCII or two-byte.
9809 __ tst(r4, Operand(kStringEncodingMask));
9810 __ b(eq, &non_ascii_string_add_flat_result);
9811
9812 // Both strings are sequential ASCII strings. We also know that they are
9813 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009814 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00009815 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
9816 // Locate first character of result.
9817 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9818 // Locate first character of first argument.
9819 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9820 // r0: first character of first string.
9821 // r1: second string.
9822 // r2: length of first string.
9823 // r3: length of second string.
9824 // r6: first character of result.
9825 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009826 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +00009827
9828 // Load second argument and locate first character.
9829 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9830 // r1: first character of second string.
9831 // r3: length of second string.
9832 // r6: next character of result.
9833 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009834 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +00009835 __ mov(r0, Operand(r7));
9836 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9837 __ add(sp, sp, Operand(2 * kPointerSize));
9838 __ Ret();
9839
9840 __ bind(&non_ascii_string_add_flat_result);
9841 // Both strings are sequential two byte strings.
9842 // r0: first string.
9843 // r1: second string.
9844 // r2: length of first string.
9845 // r3: length of second string.
9846 // r6: sum of length of strings.
9847 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
9848 // r0: first string.
9849 // r1: second string.
9850 // r2: length of first string.
9851 // r3: length of second string.
9852 // r7: result string.
9853
9854 // Locate first character of result.
9855 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9856 // Locate first character of first argument.
9857 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9858
9859 // r0: first character of first string.
9860 // r1: second string.
9861 // r2: length of first string.
9862 // r3: length of second string.
9863 // r6: first character of result.
9864 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009865 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +00009866
9867 // Locate first character of second argument.
9868 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9869
9870 // r1: first character of second string.
9871 // r3: length of second string.
9872 // r6: next character of result (after copy of first string).
9873 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009874 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +00009875
9876 __ mov(r0, Operand(r7));
9877 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9878 __ add(sp, sp, Operand(2 * kPointerSize));
9879 __ Ret();
9880
9881 // Just jump to runtime to add the two strings.
9882 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009883 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009884}
9885
9886
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009887#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009889} } // namespace v8::internal