blob: 5509830b307d1b8ea7c2b18fd9fe53f0caec1677 [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_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000194 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_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));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001489 __ strd(r0, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001490
1491 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1492 frame_->CallStub(&call_function, 3);
1493 // The function and its two arguments have been dropped.
1494 frame_->Drop(); // Drop the receiver as well.
1495 frame_->EmitPush(r0);
1496 // Stack now has 1 element:
1497 // sp[0]: result
1498 __ bind(&done);
1499
1500 // Restore the context register after a call.
1501 __ ldr(cp, frame_->Context());
1502}
1503
1504
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001506 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 ASSERT(has_cc());
1508 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001509 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 cc_reg_ = al;
1511}
1512
1513
ager@chromium.org7c537e22008-10-16 08:43:32 +00001514void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001515 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001516 Comment cmnt(masm_, "[ check stack");
1517 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1518 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1519 // the implicit 8 byte offset that always applies to operations with pc and
1520 // gives a return address 12 bytes down.
1521 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1522 masm_->cmp(sp, Operand(ip));
1523 StackCheckStub stub;
1524 // Call the stub if lower.
1525 masm_->mov(pc,
1526 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1527 RelocInfo::CODE_TARGET),
1528 LeaveCC,
1529 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530}
1531
1532
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001533void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1534#ifdef DEBUG
1535 int original_height = frame_->height();
1536#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001537 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001538 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1539 VisitAndSpill(statements->at(i));
1540 }
1541 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1542}
1543
1544
ager@chromium.org7c537e22008-10-16 08:43:32 +00001545void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546#ifdef DEBUG
1547 int original_height = frame_->height();
1548#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001549 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001552 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001553 VisitStatementsAndSpill(node->statements());
1554 if (node->break_target()->is_linked()) {
1555 node->break_target()->Bind();
1556 }
1557 node->break_target()->Unuse();
1558 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559}
1560
1561
ager@chromium.org7c537e22008-10-16 08:43:32 +00001562void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001563 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001564 frame_->EmitPush(Operand(pairs));
1565 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1566
1567 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001568 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001569 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570}
1571
1572
ager@chromium.org7c537e22008-10-16 08:43:32 +00001573void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001574#ifdef DEBUG
1575 int original_height = frame_->height();
1576#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 Comment cmnt(masm_, "[ Declaration");
1578 Variable* var = node->proxy()->var();
1579 ASSERT(var != NULL); // must have been resolved
1580 Slot* slot = var->slot();
1581
1582 // If it was not possible to allocate the variable at compile time,
1583 // we need to "declare" it at runtime to make sure it actually
1584 // exists in the local context.
1585 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1586 // Variables with a "LOOKUP" slot were introduced as non-locals
1587 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001588 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001590 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001591 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 // Declaration nodes are always declared in only two modes.
1593 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1594 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001595 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 // Push initial value, if any.
1597 // Note: For variables we must not push an initial value (such as
1598 // 'undefined') because we may have a (legal) redeclaration and we
1599 // must not destroy the current value.
1600 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001601 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001603 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001605 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001607
1608 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001609 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001610 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001611
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001612 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 return;
1614 }
1615
1616 ASSERT(!var->is_global());
1617
1618 // If we have a function or a constant, we need to initialize the variable.
1619 Expression* val = NULL;
1620 if (node->mode() == Variable::CONST) {
1621 val = new Literal(Factory::the_hole_value());
1622 } else {
1623 val = node->fun(); // NULL if we don't have a function
1624 }
1625
1626 if (val != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001627 // Set initial value.
1628 Reference target(this, node->proxy());
1629 Load(val);
1630 target.SetValue(NOT_CONST_INIT);
1631
iposva@chromium.org245aa852009-02-10 00:49:54 +00001632 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001633 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636}
1637
1638
ager@chromium.org7c537e22008-10-16 08:43:32 +00001639void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640#ifdef DEBUG
1641 int original_height = frame_->height();
1642#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001643 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001645 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 Expression* expression = node->expression();
1647 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001648 LoadAndSpill(expression);
1649 frame_->Drop();
1650 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651}
1652
1653
ager@chromium.org7c537e22008-10-16 08:43:32 +00001654void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001655#ifdef DEBUG
1656 int original_height = frame_->height();
1657#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001658 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001660 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001662 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663}
1664
1665
ager@chromium.org7c537e22008-10-16 08:43:32 +00001666void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001667#ifdef DEBUG
1668 int original_height = frame_->height();
1669#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001670 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001672 // Generate different code depending on which parts of the if statement
1673 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 bool has_then_stm = node->HasThenStatement();
1675 bool has_else_stm = node->HasElseStatement();
1676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001677 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001679 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001681 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001682 JumpTarget then;
1683 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001685 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001686 if (frame_ != NULL) {
1687 Branch(false, &else_);
1688 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 if (frame_ != NULL || then.is_linked()) {
1691 then.Bind();
1692 VisitAndSpill(node->then_statement());
1693 }
1694 if (frame_ != NULL) {
1695 exit.Jump();
1696 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001698 if (else_.is_linked()) {
1699 else_.Bind();
1700 VisitAndSpill(node->else_statement());
1701 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702
1703 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001704 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001706 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001708 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001709 if (frame_ != NULL) {
1710 Branch(false, &exit);
1711 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001713 if (frame_ != NULL || then.is_linked()) {
1714 then.Bind();
1715 VisitAndSpill(node->then_statement());
1716 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
1718 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001719 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001721 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001723 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001724 if (frame_ != NULL) {
1725 Branch(true, &exit);
1726 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001728 if (frame_ != NULL || else_.is_linked()) {
1729 else_.Bind();
1730 VisitAndSpill(node->else_statement());
1731 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732
1733 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001734 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 ASSERT(!has_then_stm && !has_else_stm);
1736 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001737 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001738 if (frame_ != NULL) {
1739 if (has_cc()) {
1740 cc_reg_ = al;
1741 } else {
1742 frame_->Drop();
1743 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 }
1745 }
1746
1747 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748 if (exit.is_linked()) {
1749 exit.Bind();
1750 }
1751 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752}
1753
1754
ager@chromium.org7c537e22008-10-16 08:43:32 +00001755void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001756 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 CodeForStatementPosition(node);
1759 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760}
1761
1762
ager@chromium.org7c537e22008-10-16 08:43:32 +00001763void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001764 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001766 CodeForStatementPosition(node);
1767 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768}
1769
1770
ager@chromium.org7c537e22008-10-16 08:43:32 +00001771void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001772 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001774
ager@chromium.org4af710e2009-09-15 12:20:11 +00001775 CodeForStatementPosition(node);
1776 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 frame_->EmitPop(r0);
1779 function_return_.Jump();
1780 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 // Pop the result from the frame and prepare the frame for
1782 // returning thus making it easier to merge.
1783 frame_->EmitPop(r0);
1784 frame_->PrepareForReturn();
1785
1786 function_return_.Jump();
1787 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788}
1789
1790
ager@chromium.org7c537e22008-10-16 08:43:32 +00001791void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792#ifdef DEBUG
1793 int original_height = frame_->height();
1794#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001795 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 CodeForStatementPosition(node);
1798 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001799 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001800 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001801 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001802 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001803 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001804#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001805 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001806 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001807 verified_true.Branch(eq);
1808 __ stop("PushContext: r0 is expected to be the same as cp");
1809 verified_true.Bind();
1810#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001812 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001813 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814}
1815
1816
ager@chromium.org7c537e22008-10-16 08:43:32 +00001817void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001818#ifdef DEBUG
1819 int original_height = frame_->height();
1820#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001821 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 // Pop context.
1825 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1826 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001827 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829}
1830
1831
ager@chromium.org7c537e22008-10-16 08:43:32 +00001832void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001833#ifdef DEBUG
1834 int original_height = frame_->height();
1835#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001836 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001838 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001839 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001842
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001843 JumpTarget next_test;
1844 JumpTarget fall_through;
1845 JumpTarget default_entry;
1846 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 ZoneList<CaseClause*>* cases = node->cases();
1848 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001849 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851 for (int i = 0; i < length; i++) {
1852 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 // Remember the default clause and compile it at the end.
1855 default_clause = clause;
1856 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 }
1858
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 Comment cmnt(masm_, "[ Case clause");
1860 // Compile the test.
1861 next_test.Bind();
1862 next_test.Unuse();
1863 // Duplicate TOS.
1864 __ ldr(r0, frame_->Top());
1865 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001866 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 // Before entering the body from the test, remove the switch value from
1870 // the stack.
1871 frame_->Drop();
1872
1873 // Label the body so that fall through is enabled.
1874 if (i > 0 && cases->at(i - 1)->is_default()) {
1875 default_exit.Bind();
1876 } else {
1877 fall_through.Bind();
1878 fall_through.Unuse();
1879 }
1880 VisitStatementsAndSpill(clause->statements());
1881
1882 // If control flow can fall through from the body, jump to the next body
1883 // or the end of the statement.
1884 if (frame_ != NULL) {
1885 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1886 default_entry.Jump();
1887 } else {
1888 fall_through.Jump();
1889 }
1890 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 }
1892
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 // The final "test" removes the switch value.
1894 next_test.Bind();
1895 frame_->Drop();
1896
1897 // If there is a default clause, compile it.
1898 if (default_clause != NULL) {
1899 Comment cmnt(masm_, "[ Default clause");
1900 default_entry.Bind();
1901 VisitStatementsAndSpill(default_clause->statements());
1902 // If control flow can fall out of the default and there is a case after
1903 // it, jup to that case's body.
1904 if (frame_ != NULL && default_exit.is_bound()) {
1905 default_exit.Jump();
1906 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001907 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 if (fall_through.is_linked()) {
1910 fall_through.Bind();
1911 }
1912
1913 if (node->break_target()->is_linked()) {
1914 node->break_target()->Bind();
1915 }
1916 node->break_target()->Unuse();
1917 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918}
1919
1920
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001921void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001922#ifdef DEBUG
1923 int original_height = frame_->height();
1924#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001925 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001926 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001927 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001928 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001929 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001930 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001932 // Label the top of the loop for the backward CFG edge. If the test
1933 // is always true we can use the continue target, and if the test is
1934 // always false there is no need.
1935 ConditionAnalysis info = AnalyzeCondition(node->cond());
1936 switch (info) {
1937 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001938 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001940 break;
1941 case ALWAYS_FALSE:
1942 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1943 break;
1944 case DONT_KNOW:
1945 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1946 body.Bind();
1947 break;
1948 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001950 CheckStack(); // TODO(1222600): ignore if body contains calls.
1951 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001952
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001953 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001954 switch (info) {
1955 case ALWAYS_TRUE:
1956 // If control can fall off the end of the body, jump back to the
1957 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001958 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001959 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001960 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001962 case ALWAYS_FALSE:
1963 // If we have a continue in the body, we only have to bind its
1964 // jump target.
1965 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001968 break;
1969 case DONT_KNOW:
1970 // We have to compile the test expression if it can be reached by
1971 // control flow falling out of the body or via continue.
1972 if (node->continue_target()->is_linked()) {
1973 node->continue_target()->Bind();
1974 }
1975 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001976 Comment cmnt(masm_, "[ DoWhileCondition");
1977 CodeForDoWhileConditionPosition(node);
1978 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001980 // A invalid frame here indicates that control did not
1981 // fall out of the test expression.
1982 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001983 }
1984 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985 break;
1986 }
1987
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001988 if (node->break_target()->is_linked()) {
1989 node->break_target()->Bind();
1990 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001991 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001992 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1993}
1994
1995
1996void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1997#ifdef DEBUG
1998 int original_height = frame_->height();
1999#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002000 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002001 Comment cmnt(masm_, "[ WhileStatement");
2002 CodeForStatementPosition(node);
2003
2004 // If the test is never true and has no side effects there is no need
2005 // to compile the test or body.
2006 ConditionAnalysis info = AnalyzeCondition(node->cond());
2007 if (info == ALWAYS_FALSE) return;
2008
2009 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002010 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002011
2012 // Label the top of the loop with the continue target for the backward
2013 // CFG edge.
2014 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2015 node->continue_target()->Bind();
2016
2017 if (info == DONT_KNOW) {
2018 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002019 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002020 if (has_valid_frame()) {
2021 // A NULL frame indicates that control did not fall out of the
2022 // test expression.
2023 Branch(false, node->break_target());
2024 }
2025 if (has_valid_frame() || body.is_linked()) {
2026 body.Bind();
2027 }
2028 }
2029
2030 if (has_valid_frame()) {
2031 CheckStack(); // TODO(1222600): ignore if body contains calls.
2032 VisitAndSpill(node->body());
2033
2034 // If control flow can fall out of the body, jump back to the top.
2035 if (has_valid_frame()) {
2036 node->continue_target()->Jump();
2037 }
2038 }
2039 if (node->break_target()->is_linked()) {
2040 node->break_target()->Bind();
2041 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002042 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002043 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2044}
2045
2046
2047void CodeGenerator::VisitForStatement(ForStatement* node) {
2048#ifdef DEBUG
2049 int original_height = frame_->height();
2050#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002051 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002052 Comment cmnt(masm_, "[ ForStatement");
2053 CodeForStatementPosition(node);
2054 if (node->init() != NULL) {
2055 VisitAndSpill(node->init());
2056 }
2057
2058 // If the test is never true there is no need to compile the test or
2059 // body.
2060 ConditionAnalysis info = AnalyzeCondition(node->cond());
2061 if (info == ALWAYS_FALSE) return;
2062
2063 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002064 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002065
2066 // If there is no update statement, label the top of the loop with the
2067 // continue target, otherwise with the loop target.
2068 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2069 if (node->next() == NULL) {
2070 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2071 node->continue_target()->Bind();
2072 } else {
2073 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2074 loop.Bind();
2075 }
2076
2077 // If the test is always true, there is no need to compile it.
2078 if (info == DONT_KNOW) {
2079 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002080 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002081 if (has_valid_frame()) {
2082 Branch(false, node->break_target());
2083 }
2084 if (has_valid_frame() || body.is_linked()) {
2085 body.Bind();
2086 }
2087 }
2088
2089 if (has_valid_frame()) {
2090 CheckStack(); // TODO(1222600): ignore if body contains calls.
2091 VisitAndSpill(node->body());
2092
2093 if (node->next() == NULL) {
2094 // If there is no update statement and control flow can fall out
2095 // of the loop, jump directly to the continue label.
2096 if (has_valid_frame()) {
2097 node->continue_target()->Jump();
2098 }
2099 } else {
2100 // If there is an update statement and control flow can reach it
2101 // via falling out of the body of the loop or continuing, we
2102 // compile the update statement.
2103 if (node->continue_target()->is_linked()) {
2104 node->continue_target()->Bind();
2105 }
2106 if (has_valid_frame()) {
2107 // Record source position of the statement as this code which is
2108 // after the code for the body actually belongs to the loop
2109 // statement and not the body.
2110 CodeForStatementPosition(node);
2111 VisitAndSpill(node->next());
2112 loop.Jump();
2113 }
2114 }
2115 }
2116 if (node->break_target()->is_linked()) {
2117 node->break_target()->Bind();
2118 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002119 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002120 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121}
2122
2123
ager@chromium.org7c537e22008-10-16 08:43:32 +00002124void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125#ifdef DEBUG
2126 int original_height = frame_->height();
2127#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002128 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002132 JumpTarget primitive;
2133 JumpTarget jsobject;
2134 JumpTarget fixed_array;
2135 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2136 JumpTarget end_del_check;
2137 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
2139 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002140 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
2142 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2143 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002144 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002145 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2146 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002147 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002148 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2149 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151
2152 // Stack layout in body:
2153 // [iteration counter (Smi)]
2154 // [length of array]
2155 // [FixedArray]
2156 // [Map or 0]
2157 // [Object]
2158
2159 // Check if enumerable is already a JSObject
2160 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002162 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002163 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165 primitive.Bind();
2166 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002167 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002171 // r0: value to be iterated over
2172 frame_->EmitPush(r0); // Push the object being iterated over.
2173
2174 // Check cache validity in generated code. This is a fast case for
2175 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2176 // guarantee cache validity, call the runtime system to check cache
2177 // validity or get the property names in a fixed array.
2178 JumpTarget call_runtime;
2179 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2180 JumpTarget check_prototype;
2181 JumpTarget use_cache;
2182 __ mov(r1, Operand(r0));
2183 loop.Bind();
2184 // Check that there are no elements.
2185 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2186 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2187 __ cmp(r2, r4);
2188 call_runtime.Branch(ne);
2189 // Check that instance descriptors are not empty so that we can
2190 // check for an enum cache. Leave the map in r3 for the subsequent
2191 // prototype load.
2192 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2193 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2194 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2195 __ cmp(r2, ip);
2196 call_runtime.Branch(eq);
2197 // Check that there in an enum cache in the non-empty instance
2198 // descriptors. This is the case if the next enumeration index
2199 // field does not contain a smi.
2200 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2201 __ tst(r2, Operand(kSmiTagMask));
2202 call_runtime.Branch(eq);
2203 // For all objects but the receiver, check that the cache is empty.
2204 // r4: empty fixed array root.
2205 __ cmp(r1, r0);
2206 check_prototype.Branch(eq);
2207 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2208 __ cmp(r2, r4);
2209 call_runtime.Branch(ne);
2210 check_prototype.Bind();
2211 // Load the prototype from the map and loop if non-null.
2212 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2213 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2214 __ cmp(r1, ip);
2215 loop.Branch(ne);
2216 // The enum cache is valid. Load the map of the object being
2217 // iterated over and use the cache for the iteration.
2218 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2219 use_cache.Jump();
2220
2221 call_runtime.Bind();
2222 // Call the runtime to get the property names for the object.
2223 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002224 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002226 // If we got a map from the runtime call, we can do a fast
2227 // modification check. Otherwise, we got a fixed array, and we have
2228 // to do a slow check.
2229 // r0: map or fixed array (result from call to
2230 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 __ mov(r2, Operand(r0));
2232 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002233 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2234 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002235 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002237 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002239 // r0: map (either the result from a call to
2240 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2241 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 __ mov(r1, Operand(r0));
2243 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2244 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2245 __ ldr(r2,
2246 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2247
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 frame_->EmitPush(r0); // map
2249 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002250 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002252 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002253 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002254 frame_->EmitPush(r0);
2255 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 frame_->EmitPush(r1); // insert 0 in place of Map
2260 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261
2262 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002263 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002265 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002266 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268
2269 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002271 // sp[0] : index
2272 // sp[1] : array/enum cache length
2273 // sp[2] : array or enum cache
2274 // sp[3] : 0 or map
2275 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002276 // Grab the current frame's height for the break and continue
2277 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002278 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2279 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002281 // Load the current count to r0, load the length to r1.
2282 __ ldrd(r0, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002283 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002284 node->break_target()->Branch(hs);
2285
2286 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2291 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2292
2293 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002294 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 // Check if this (still) matches the map of the enumerable.
2296 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002297 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2299 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002300 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301
2302 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002303 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2304 frame_->EmitPush(r0);
2305 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002306 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002307 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308
2309 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002310 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2311 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002312 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002314 end_del_check.Bind();
2315 // Store the entry in the 'each' expression and take another spin in the
2316 // loop. r3: i'th entry of the enum cache (or string there of)
2317 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 { Reference each(this, node->each());
2319 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002320 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 __ ldr(r0, frame_->ElementAt(each.size()));
2322 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002323 each.SetValue(NOT_CONST_INIT);
2324 frame_->Drop(2);
2325 } else {
2326 // If the reference was to a slot we rely on the convenient property
2327 // that it doesn't matter whether a value (eg, r3 pushed above) is
2328 // right on top of or right underneath a zero-sized reference.
2329 each.SetValue(NOT_CONST_INIT);
2330 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332 }
2333 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002334 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002336 VisitAndSpill(node->body());
2337
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002338 // Next. Reestablish a spilled frame in case we are coming here via
2339 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002341 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002342 frame_->EmitPop(r0);
2343 __ add(r0, r0, Operand(Smi::FromInt(1)));
2344 frame_->EmitPush(r0);
2345 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002347 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2348 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002350 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351
2352 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353 exit.Bind();
2354 node->continue_target()->Unuse();
2355 node->break_target()->Unuse();
2356 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357}
2358
2359
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002360void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002361#ifdef DEBUG
2362 int original_height = frame_->height();
2363#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002364 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002365 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002366 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002368 JumpTarget try_block;
2369 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374
2375 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002376 Variable* catch_var = node->catch_var()->var();
2377 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2378 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379
2380 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002381 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002383 VisitStatementsAndSpill(node->catch_block()->statements());
2384 if (frame_ != NULL) {
2385 exit.Jump();
2386 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
2388
2389 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002392 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2393 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002395 // Shadow the labels for all escapes from the try block, including
2396 // returns. During shadowing, the original label is hidden as the
2397 // LabelShadow and operations on the original actually affect the
2398 // shadowing label.
2399 //
2400 // We should probably try to unify the escaping labels and the return
2401 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002402 int nof_escapes = node->escaping_targets()->length();
2403 List<ShadowTarget*> shadows(1 + nof_escapes);
2404
2405 // Add the shadow target for the function return.
2406 static const int kReturnShadowIndex = 0;
2407 shadows.Add(new ShadowTarget(&function_return_));
2408 bool function_return_was_shadowed = function_return_is_shadowed_;
2409 function_return_is_shadowed_ = true;
2410 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2411
2412 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002414 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415 }
2416
2417 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002418 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419
2420 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002421 // After shadowing stops, the original labels are unshadowed and the
2422 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002423 bool has_unlinks = false;
2424 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002426 has_unlinks = has_unlinks || shadows[i]->is_linked();
2427 }
2428 function_return_is_shadowed_ = function_return_was_shadowed;
2429
2430 // Get an external reference to the handler address.
2431 ExternalReference handler_address(Top::k_handler_address);
2432
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002433 // If we can fall off the end of the try block, unlink from try chain.
2434 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002435 // The next handler address is on top of the frame. Unlink from
2436 // the handler list and drop the rest of this handler from the
2437 // frame.
2438 ASSERT(StackHandlerConstants::kNextOffset == 0);
2439 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 __ mov(r3, Operand(handler_address));
2441 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002442 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002443 if (has_unlinks) {
2444 exit.Jump();
2445 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446 }
2447
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002448 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 // jumped to. Deallocate each shadow target.
2450 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002451 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002452 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002453 shadows[i]->Bind();
2454 // Because we can be jumping here (to spilled code) from unspilled
2455 // code, we need to reestablish a spilled frame at this block.
2456 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 // Reload sp from the top handler, because some statements that we
2459 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002462 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002464 ASSERT(StackHandlerConstants::kNextOffset == 0);
2465 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002467 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002469 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2470 frame_->PrepareForReturn();
2471 }
2472 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 }
2474 }
2475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 exit.Bind();
2477 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478}
2479
2480
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002481void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482#ifdef DEBUG
2483 int original_height = frame_->height();
2484#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002485 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002486 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488
2489 // State: Used to keep track of reason for entering the finally
2490 // block. Should probably be extended to hold information for
2491 // break/continue from within the try block.
2492 enum { FALLING, THROWING, JUMPING };
2493
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002494 JumpTarget try_block;
2495 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002497 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 // In case of thrown exceptions, this is where we continue.
2501 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002502 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503
2504 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002505 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002507 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2508 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002510 // Shadow the labels for all escapes from the try block, including
2511 // returns. Shadowing hides the original label as the LabelShadow and
2512 // operations on the original actually affect the shadowing label.
2513 //
2514 // We should probably try to unify the escaping labels and the return
2515 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002516 int nof_escapes = node->escaping_targets()->length();
2517 List<ShadowTarget*> shadows(1 + nof_escapes);
2518
2519 // Add the shadow target for the function return.
2520 static const int kReturnShadowIndex = 0;
2521 shadows.Add(new ShadowTarget(&function_return_));
2522 bool function_return_was_shadowed = function_return_is_shadowed_;
2523 function_return_is_shadowed_ = true;
2524 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2525
2526 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002528 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529 }
2530
2531 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002532 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002534 // Stop the introduced shadowing and count the number of required unlinks.
2535 // After shadowing stops, the original labels are unshadowed and the
2536 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002538 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 shadows[i]->StopShadowing();
2540 if (shadows[i]->is_linked()) nof_unlinks++;
2541 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544 // Get an external reference to the handler address.
2545 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002547 // If we can fall off the end of the try block, unlink from the try
2548 // chain and set the state on the frame to FALLING.
2549 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002550 // The next handler address is on top of the frame.
2551 ASSERT(StackHandlerConstants::kNextOffset == 0);
2552 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002553 __ mov(r3, Operand(handler_address));
2554 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002555 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556
2557 // Fake a top of stack value (unneeded when FALLING) and set the
2558 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002559 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 frame_->EmitPush(r0);
2561 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2562 if (nof_unlinks > 0) {
2563 finally_block.Jump();
2564 }
2565 }
2566
2567 // Generate code to unlink and set the state for the (formerly)
2568 // shadowing targets that have been jumped to.
2569 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571 // If we have come from the shadowed return, the return value is
2572 // in (a non-refcounted reference to) r0. We must preserve it
2573 // until it is pushed.
2574 //
2575 // Because we can be jumping here (to spilled code) from
2576 // unspilled code, we need to reestablish a spilled frame at
2577 // this block.
2578 shadows[i]->Bind();
2579 frame_->SpillAll();
2580
2581 // Reload sp from the top handler, because some statements that
2582 // we break from (eg, for...in) may have left stuff on the
2583 // stack.
2584 __ mov(r3, Operand(handler_address));
2585 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002586 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002587
2588 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002589 // handler address is currently on top of the frame.
2590 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 frame_->EmitPop(r1);
2592 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002593 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002594
2595 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002596 // If this label shadowed the function return, materialize the
2597 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002598 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002599 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002600 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002601 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002602 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 }
2604 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002605 if (--nof_unlinks > 0) {
2606 // If this is not the last unlink block, jump around the next.
2607 finally_block.Jump();
2608 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 }
2610 }
2611
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002613 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002615 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002617
2618 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619 // and the state - while evaluating the finally block.
2620 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002622 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002624 if (has_valid_frame()) {
2625 // Restore state and return value or faked TOS.
2626 frame_->EmitPop(r2);
2627 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 }
2629
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630 // Generate code to jump to the right destination for all used
2631 // formerly shadowing targets. Deallocate each shadow target.
2632 for (int i = 0; i < shadows.length(); i++) {
2633 if (has_valid_frame() && shadows[i]->is_bound()) {
2634 JumpTarget* original = shadows[i]->other_target();
2635 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2636 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002637 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002638 skip.Branch(ne);
2639 frame_->PrepareForReturn();
2640 original->Jump();
2641 skip.Bind();
2642 } else {
2643 original->Branch(eq);
2644 }
2645 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002648 if (has_valid_frame()) {
2649 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002650 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2652 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 // Rethrow exception.
2655 frame_->EmitPush(r0);
2656 frame_->CallRuntime(Runtime::kReThrow, 1);
2657
2658 // Done.
2659 exit.Bind();
2660 }
2661 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662}
2663
2664
ager@chromium.org7c537e22008-10-16 08:43:32 +00002665void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002666#ifdef DEBUG
2667 int original_height = frame_->height();
2668#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002669 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002671 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002672#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002673 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002674#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002675 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002676 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677}
2678
2679
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002680void CodeGenerator::InstantiateFunction(
2681 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002682 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002683 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002684 // Use the fast case closure allocation code that allocates in new
2685 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002686 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002687 FastNewClosureStub stub;
2688 frame_->EmitPush(r0);
2689 frame_->CallStub(&stub, 1);
2690 frame_->EmitPush(r0);
2691 } else {
2692 // Create a new closure.
2693 frame_->EmitPush(cp);
2694 frame_->EmitPush(r0);
2695 frame_->CallRuntime(Runtime::kNewClosure, 2);
2696 frame_->EmitPush(r0);
2697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698}
2699
2700
ager@chromium.org7c537e22008-10-16 08:43:32 +00002701void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002702#ifdef DEBUG
2703 int original_height = frame_->height();
2704#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002705 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 Comment cmnt(masm_, "[ FunctionLiteral");
2707
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002708 // Build the function info and instantiate it.
2709 Handle<SharedFunctionInfo> function_info =
2710 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002711 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002712 if (HasStackOverflow()) {
2713 ASSERT(frame_->height() == original_height);
2714 return;
2715 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002716 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002717 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718}
2719
2720
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002721void CodeGenerator::VisitSharedFunctionInfoLiteral(
2722 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002723#ifdef DEBUG
2724 int original_height = frame_->height();
2725#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002726 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002727 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2728 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002729 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730}
2731
2732
ager@chromium.org7c537e22008-10-16 08:43:32 +00002733void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002734#ifdef DEBUG
2735 int original_height = frame_->height();
2736#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002737 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002739 JumpTarget then;
2740 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002741 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002742 if (has_valid_frame()) {
2743 Branch(false, &else_);
2744 }
2745 if (has_valid_frame() || then.is_linked()) {
2746 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002747 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002748 }
2749 if (else_.is_linked()) {
2750 JumpTarget exit;
2751 if (has_valid_frame()) exit.Jump();
2752 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002753 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002754 if (exit.is_linked()) exit.Bind();
2755 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002756 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757}
2758
2759
ager@chromium.org7c537e22008-10-16 08:43:32 +00002760void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2761 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002762 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002764 // JumpTargets do not yet support merging frames so the frame must be
2765 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002766 JumpTarget slow;
2767 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002768
2769 // Generate fast-case code for variables that might be shadowed by
2770 // eval-introduced variables. Eval is used a lot without
2771 // introducing variables. In those cases, we do not want to
2772 // perform a runtime call for all variables in the scope
2773 // containing the eval.
2774 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002775 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002776 // If there was no control flow to slow, we can exit early.
2777 if (!slow.is_linked()) {
2778 frame_->EmitPush(r0);
2779 return;
2780 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002781 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002782
2783 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002784
2785 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002786 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002787 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2788 // Only generate the fast case for locals that rewrite to slots.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002789 // This rules out argument loads because eval forces arguments
2790 // access to be through the arguments object.
ager@chromium.org381abbb2009-02-25 13:23:22 +00002791 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.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003476 // Duplicate receiver and key for loading the current property value.
3477 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003478 EmitKeyedLoad();
3479 frame_->EmitPush(r0);
3480
3481 // Perform the binary operation.
3482 Literal* literal = node->value()->AsLiteral();
3483 bool overwrite_value =
3484 (node->value()->AsBinaryOperation() != NULL &&
3485 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3486 if (literal != NULL && literal->handle()->IsSmi()) {
3487 SmiOperation(node->binary_op(),
3488 literal->handle(),
3489 false,
3490 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3491 } else {
3492 Load(node->value());
3493 VirtualFrameBinaryOperation(
3494 node->binary_op(), overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3495 }
3496 } else {
3497 // For non-compound assignment just load the right-hand side.
3498 Load(node->value());
3499 }
3500
3501 // Stack layout:
3502 // [tos] : value
3503 // [tos+1] : key
3504 // [tos+2] : receiver
3505 // [tos+3] : receiver if at the end of an initialization block
3506
3507 // Perform the assignment. It is safe to ignore constants here.
3508 ASSERT(node->op() != Token::INIT_CONST);
3509 CodeForSourcePosition(node->position());
3510 frame_->PopToR0();
3511 EmitKeyedStore(prop->key()->type());
3512 frame_->Drop(2); // Key and receiver are left on the stack.
3513 frame_->EmitPush(r0);
3514
3515 // Stack layout:
3516 // [tos] : result
3517 // [tos+1] : receiver if at the end of an initialization block
3518
3519 // Change to fast case at the end of an initialization block.
3520 if (node->ends_initialization_block()) {
3521 // The argument to the runtime call is the extra copy of the receiver,
3522 // which is below the value of the assignment. Swap the receiver and
3523 // the value of the assignment expression.
3524 Register t0 = frame_->PopToRegister();
3525 Register t1 = frame_->PopToRegister(t0);
3526 frame_->EmitPush(t1);
3527 frame_->EmitPush(t0);
3528 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3529 }
3530
3531 // Stack layout:
3532 // [tos] : result
3533
3534 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003535}
3536
3537
ager@chromium.org7c537e22008-10-16 08:43:32 +00003538void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003539 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003540#ifdef DEBUG
3541 int original_height = frame_->height();
3542#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003544
ager@chromium.orgac091b72010-05-05 07:34:42 +00003545 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3546 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547
ager@chromium.orgac091b72010-05-05 07:34:42 +00003548 if (var != NULL && !var->is_global()) {
3549 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003550
ager@chromium.orgac091b72010-05-05 07:34:42 +00003551 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3552 (var != NULL && var->is_global())) {
3553 // Properties whose keys are property names and global variables are
3554 // treated as named property references. We do not need to consider
3555 // global 'this' because it is not a valid left-hand side.
3556 EmitNamedPropertyAssignment(node);
3557
3558 } else if (prop != NULL) {
3559 // Other properties (including rewritten parameters for a function that
3560 // uses arguments) are keyed property assignments.
3561 EmitKeyedPropertyAssignment(node);
3562
3563 } else {
3564 // Invalid left-hand side.
3565 Load(node->target());
3566 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3567 // The runtime call doesn't actually return but the code generator will
3568 // still generate code and expects a certain frame height.
3569 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003571 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572}
3573
3574
ager@chromium.org7c537e22008-10-16 08:43:32 +00003575void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003576#ifdef DEBUG
3577 int original_height = frame_->height();
3578#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003579 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580 Comment cmnt(masm_, "[ Throw");
3581
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003582 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003583 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 frame_->CallRuntime(Runtime::kThrow, 1);
3585 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003586 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587}
3588
3589
ager@chromium.org7c537e22008-10-16 08:43:32 +00003590void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003591#ifdef DEBUG
3592 int original_height = frame_->height();
3593#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003595
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003596 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003597 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003598 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003599 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003600}
3601
3602
ager@chromium.org7c537e22008-10-16 08:43:32 +00003603void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604#ifdef DEBUG
3605 int original_height = frame_->height();
3606#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003607 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 Comment cmnt(masm_, "[ Call");
3609
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003610 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611 ZoneList<Expression*>* args = node->arguments();
3612
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003614 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615 Variable* var = function->AsVariableProxy()->AsVariable();
3616 Property* property = function->AsProperty();
3617
3618 // ------------------------------------------------------------------------
3619 // Fast-case: Use inline caching.
3620 // ---
3621 // According to ECMA-262, section 11.2.3, page 44, the function to call
3622 // must be resolved after the arguments have been evaluated. The IC code
3623 // automatically handles this by loading the arguments before the function
3624 // is resolved in cache misses (this also holds for megamorphic calls).
3625 // ------------------------------------------------------------------------
3626
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003627 if (var != NULL && var->is_possibly_eval()) {
3628 // ----------------------------------
3629 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3630 // ----------------------------------
3631
3632 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3633 // resolve the function we need to call and the receiver of the
3634 // call. Then we call the resolved function using the given
3635 // arguments.
3636 // Prepare stack for call to resolved function.
3637 LoadAndSpill(function);
3638 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3639 frame_->EmitPush(r2); // Slot for receiver
3640 int arg_count = args->length();
3641 for (int i = 0; i < arg_count; i++) {
3642 LoadAndSpill(args->at(i));
3643 }
3644
3645 // Prepare stack for call to ResolvePossiblyDirectEval.
3646 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3647 frame_->EmitPush(r1);
3648 if (arg_count > 0) {
3649 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3650 frame_->EmitPush(r1);
3651 } else {
3652 frame_->EmitPush(r2);
3653 }
3654
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003655 // Push the receiver.
3656 __ ldr(r1, frame_->Receiver());
3657 frame_->EmitPush(r1);
3658
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003659 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003660 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003661
3662 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003663 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003664 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3665
3666 // Call the function.
3667 CodeForSourcePosition(node->position());
3668
3669 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003670 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003671 frame_->CallStub(&call_function, arg_count + 1);
3672
3673 __ ldr(cp, frame_->Context());
3674 // Remove the function from the stack.
3675 frame_->Drop();
3676 frame_->EmitPush(r0);
3677
3678 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679 // ----------------------------------
3680 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3681 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003682 // Pass the global object as the receiver and let the IC stub
3683 // patch the stack to use the global proxy as 'this' in the
3684 // invoked function.
3685 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686
3687 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003688 int arg_count = args->length();
3689 for (int i = 0; i < arg_count; i++) {
3690 LoadAndSpill(args->at(i));
3691 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692
ager@chromium.org5c838252010-02-19 08:53:10 +00003693 // Setup the name register and call the IC initialization code.
3694 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003695 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3696 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003697 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3699 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003700 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003701 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003702
3703 } else if (var != NULL && var->slot() != NULL &&
3704 var->slot()->type() == Slot::LOOKUP) {
3705 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003706 // JavaScript examples:
3707 //
3708 // with (obj) foo(1, 2, 3) // foo is in obj
3709 //
3710 // function f() {};
3711 // function g() {
3712 // eval(...);
3713 // f(); // f could be in extension object
3714 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715 // ----------------------------------
3716
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003717 // JumpTargets do not yet support merging frames so the frame must be
3718 // spilled when jumping to these targets.
3719 JumpTarget slow;
3720 JumpTarget done;
3721
3722 // Generate fast-case code for variables that might be shadowed by
3723 // eval-introduced variables. Eval is used a lot without
3724 // introducing variables. In those cases, we do not want to
3725 // perform a runtime call for all variables in the scope
3726 // containing the eval.
3727 if (var->mode() == Variable::DYNAMIC_GLOBAL) {
3728 LoadFromGlobalSlotCheckExtensions(var->slot(), NOT_INSIDE_TYPEOF, &slow);
3729 frame_->EmitPush(r0);
3730 LoadGlobalReceiver(r1);
3731 done.Jump();
3732
3733 } else if (var->mode() == Variable::DYNAMIC_LOCAL) {
3734 Slot* potential_slot = var->local_if_not_shadowed()->slot();
3735 // Only generate the fast case for locals that rewrite to slots.
3736 // This rules out argument loads because eval forces arguments
3737 // access to be through the arguments object.
3738 if (potential_slot != NULL) {
3739 __ ldr(r0,
3740 ContextSlotOperandCheckExtensions(potential_slot,
3741 r1,
3742 r2,
3743 &slow));
3744 if (potential_slot->var()->mode() == Variable::CONST) {
3745 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3746 __ cmp(r0, ip);
3747 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3748 }
3749 frame_->EmitPush(r0);
3750 LoadGlobalReceiver(r1);
3751 done.Jump();
3752 }
3753 }
3754
3755 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003757 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003758 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003759 frame_->EmitPush(r0);
3760 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003761 // r0: slot value; r1: receiver
3762
3763 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 frame_->EmitPush(r0); // function
3765 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003767 done.Bind();
3768 // Call the function. At this point, everything is spilled but the
3769 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003770 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003771 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772
3773 } else if (property != NULL) {
3774 // Check if the key is a literal string.
3775 Literal* literal = property->key()->AsLiteral();
3776
3777 if (literal != NULL && literal->handle()->IsSymbol()) {
3778 // ------------------------------------------------------------------
3779 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3780 // ------------------------------------------------------------------
3781
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003782 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003783
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003784 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
3785 name->IsEqualTo(CStrVector("apply")) &&
3786 args->length() == 2 &&
3787 args->at(1)->AsVariableProxy() != NULL &&
3788 args->at(1)->AsVariableProxy()->IsArguments()) {
3789 // Use the optimized Function.prototype.apply that avoids
3790 // allocating lazily allocated arguments objects.
3791 CallApplyLazy(property->obj(),
3792 args->at(0),
3793 args->at(1)->AsVariableProxy(),
3794 node->position());
3795
3796 } else {
3797 LoadAndSpill(property->obj()); // Receiver.
3798 // Load the arguments.
3799 int arg_count = args->length();
3800 for (int i = 0; i < arg_count; i++) {
3801 LoadAndSpill(args->at(i));
3802 }
3803
3804 // Set the name register and call the IC initialization code.
3805 __ mov(r2, Operand(name));
3806 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3807 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
3808 CodeForSourcePosition(node->position());
3809 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
3810 __ ldr(cp, frame_->Context());
3811 frame_->EmitPush(r0);
3812 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813
3814 } else {
3815 // -------------------------------------------
3816 // JavaScript example: 'array[index](1, 2, 3)'
3817 // -------------------------------------------
3818
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003819 LoadAndSpill(property->obj());
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003820 if (!property->is_synthetic()) {
3821 // Duplicate receiver for later use.
3822 __ ldr(r0, MemOperand(sp, 0));
3823 frame_->EmitPush(r0);
3824 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003825 LoadAndSpill(property->key());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003826 EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003827 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003828 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003829 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003830 frame_->EmitPush(r0); // Function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 LoadGlobalReceiver(r0);
3832 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003833 // Switch receiver and function.
3834 frame_->EmitPop(r1); // Receiver.
3835 frame_->EmitPush(r0); // Function.
3836 frame_->EmitPush(r1); // Receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003837 }
3838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003839 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003840 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003841 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842 }
3843
3844 } else {
3845 // ----------------------------------
3846 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3847 // ----------------------------------
3848
3849 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003850 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003851
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003852 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003853 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003854
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003856 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003857 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003859 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860}
3861
3862
ager@chromium.org7c537e22008-10-16 08:43:32 +00003863void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864#ifdef DEBUG
3865 int original_height = frame_->height();
3866#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003867 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868 Comment cmnt(masm_, "[ CallNew");
3869
3870 // According to ECMA-262, section 11.2.2, page 44, the function
3871 // expression in new calls must be evaluated before the
3872 // arguments. This is different from ordinary calls, where the
3873 // actual function to call is resolved after the arguments have been
3874 // evaluated.
3875
3876 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003877 // receiver. There is no need to use the global proxy here because
3878 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003879 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003880 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881
3882 // Push the arguments ("left-to-right") on the stack.
3883 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003884 int arg_count = args->length();
3885 for (int i = 0; i < arg_count; i++) {
3886 LoadAndSpill(args->at(i));
3887 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888
mads.s.ager31e71382008-08-13 09:32:07 +00003889 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003890 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003891 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003892 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003894 // Call the construct call builtin that handles allocation and
3895 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003896 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003897 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003898 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003899
3900 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003901 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003902 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903}
3904
3905
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003906void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003907 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003908 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003909 JumpTarget leave, null, function, non_function_constructor;
3910
3911 // Load the object into r0.
3912 LoadAndSpill(args->at(0));
3913 frame_->EmitPop(r0);
3914
3915 // If the object is a smi, we return null.
3916 __ tst(r0, Operand(kSmiTagMask));
3917 null.Branch(eq);
3918
3919 // Check that the object is a JS object but take special care of JS
3920 // functions to make sure they have 'Function' as their class.
3921 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3922 null.Branch(lt);
3923
3924 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3925 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3926 // LAST_JS_OBJECT_TYPE.
3927 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3928 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3929 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3930 function.Branch(eq);
3931
3932 // Check if the constructor in the map is a function.
3933 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3934 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3935 non_function_constructor.Branch(ne);
3936
3937 // The r0 register now contains the constructor function. Grab the
3938 // instance class name from there.
3939 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3940 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003941 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003942 leave.Jump();
3943
3944 // Functions have class 'Function'.
3945 function.Bind();
3946 __ mov(r0, Operand(Factory::function_class_symbol()));
3947 frame_->EmitPush(r0);
3948 leave.Jump();
3949
3950 // Objects with a non-function constructor have class 'Object'.
3951 non_function_constructor.Bind();
3952 __ mov(r0, Operand(Factory::Object_symbol()));
3953 frame_->EmitPush(r0);
3954 leave.Jump();
3955
3956 // Non-JS objects have class null.
3957 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003958 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003959 frame_->EmitPush(r0);
3960
3961 // All done.
3962 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003963}
3964
3965
ager@chromium.org7c537e22008-10-16 08:43:32 +00003966void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003967 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003968 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003969 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003970 LoadAndSpill(args->at(0));
3971 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003972 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003973 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003974 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003975 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3976 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003977 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003978 // Load the value.
3979 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003980 leave.Bind();
3981 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982}
3983
3984
ager@chromium.org7c537e22008-10-16 08:43:32 +00003985void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003986 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003988 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 LoadAndSpill(args->at(0)); // Load the object.
3990 LoadAndSpill(args->at(1)); // Load the value.
3991 frame_->EmitPop(r0); // r0 contains value
3992 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003993 // if (object->IsSmi()) return object.
3994 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003995 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003996 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3997 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003998 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999 // Store the value.
4000 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
4001 // Update the write barrier.
4002 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
4003 __ RecordWrite(r1, r2, r3);
4004 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004005 leave.Bind();
4006 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004007}
4008
4009
ager@chromium.org7c537e22008-10-16 08:43:32 +00004010void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004011 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004013 LoadAndSpill(args->at(0));
4014 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004015 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016 cc_reg_ = eq;
4017}
4018
4019
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004020void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004021 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004022 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4023 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004024#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004025 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004026 LoadAndSpill(args->at(1));
4027 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004028 __ CallRuntime(Runtime::kLog, 2);
4029 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004030#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004031 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004033}
4034
4035
ager@chromium.org7c537e22008-10-16 08:43:32 +00004036void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004037 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004038 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004039 LoadAndSpill(args->at(0));
4040 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004041 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004042 cc_reg_ = eq;
4043}
4044
4045
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004046// Generates the Math.pow method - currently just calls runtime.
4047void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4048 ASSERT(args->length() == 2);
4049 Load(args->at(0));
4050 Load(args->at(1));
4051 frame_->CallRuntime(Runtime::kMath_pow, 2);
4052 frame_->EmitPush(r0);
4053}
4054
4055
4056// Generates the Math.sqrt method - currently just calls runtime.
4057void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4058 ASSERT(args->length() == 1);
4059 Load(args->at(0));
4060 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4061 frame_->EmitPush(r0);
4062}
4063
4064
ager@chromium.orgac091b72010-05-05 07:34:42 +00004065// This generates code that performs a charCodeAt() call or returns
kasper.lund7276f142008-07-30 08:49:36 +00004066// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
ager@chromium.orgac091b72010-05-05 07:34:42 +00004067// It can handle flat, 8 and 16 bit characters and cons strings where the
4068// answer is found in the left hand branch of the cons. The slow case will
4069// flatten the string, which will ensure that the answer is in the left hand
4070// side the next time around.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004071void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004072 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00004073 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004074 Comment(masm_, "[ GenerateFastCharCodeAt");
4075
4076 LoadAndSpill(args->at(0));
4077 LoadAndSpill(args->at(1));
ager@chromium.orgac091b72010-05-05 07:34:42 +00004078 frame_->EmitPop(r1); // Index.
4079 frame_->EmitPop(r2); // String.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004080
ager@chromium.orgac091b72010-05-05 07:34:42 +00004081 Label slow_case;
4082 Label exit;
4083 StringHelper::GenerateFastCharCodeAt(masm_,
4084 r2,
4085 r1,
4086 r3,
4087 r0,
4088 &slow_case,
4089 &slow_case,
4090 &slow_case,
4091 &slow_case);
4092 __ jmp(&exit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004093
ager@chromium.orgac091b72010-05-05 07:34:42 +00004094 __ bind(&slow_case);
4095 // Move the undefined value into the result register, which will
4096 // trigger the slow case.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004097 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004098
ager@chromium.orgac091b72010-05-05 07:34:42 +00004099 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004100 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00004101}
4102
4103
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004104void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
4105 Comment(masm_, "[ GenerateCharFromCode");
4106 ASSERT(args->length() == 1);
4107
ager@chromium.orgac091b72010-05-05 07:34:42 +00004108 Register code = r1;
4109 Register scratch = ip;
4110 Register result = r0;
4111
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004112 LoadAndSpill(args->at(0));
ager@chromium.orgac091b72010-05-05 07:34:42 +00004113 frame_->EmitPop(code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004114
ager@chromium.orgac091b72010-05-05 07:34:42 +00004115 StringHelper::GenerateCharFromCode(masm_,
4116 code,
4117 scratch,
4118 result,
4119 CALL_FUNCTION);
4120 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004121}
4122
4123
ager@chromium.org7c537e22008-10-16 08:43:32 +00004124void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004125 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004126 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004127 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004128 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004129 // We need the CC bits to come out as not_equal in the case where the
4130 // object is a smi. This can't be done with the usual test opcode so
4131 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004132 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004133 __ and_(r1, r0, Operand(kSmiTagMask));
4134 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004135 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004136 // It is a heap object - get the map. Check if the object is a JS array.
4137 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139 cc_reg_ = eq;
4140}
4141
4142
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004143void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004144 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004145 ASSERT(args->length() == 1);
4146 LoadAndSpill(args->at(0));
4147 JumpTarget answer;
4148 // We need the CC bits to come out as not_equal in the case where the
4149 // object is a smi. This can't be done with the usual test opcode so
4150 // we use XOR to get the right CC bits.
4151 frame_->EmitPop(r0);
4152 __ and_(r1, r0, Operand(kSmiTagMask));
4153 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
4154 answer.Branch(ne);
4155 // It is a heap object - get the map. Check if the object is a regexp.
4156 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4157 answer.Bind();
4158 cc_reg_ = eq;
4159}
4160
4161
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004162void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4163 // This generates a fast version of:
4164 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00004165 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004166 ASSERT(args->length() == 1);
4167 LoadAndSpill(args->at(0));
4168 frame_->EmitPop(r1);
4169 __ tst(r1, Operand(kSmiTagMask));
4170 false_target()->Branch(eq);
4171
4172 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4173 __ cmp(r1, ip);
4174 true_target()->Branch(eq);
4175
4176 Register map_reg = r2;
4177 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
4178 // Undetectable objects behave like undefined when tested with typeof.
4179 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4180 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4181 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
4182 false_target()->Branch(eq);
4183
4184 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4185 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
4186 false_target()->Branch(lt);
4187 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
4188 cc_reg_ = le;
4189}
4190
4191
4192void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4193 // This generates a fast version of:
4194 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00004195 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004196 ASSERT(args->length() == 1);
4197 LoadAndSpill(args->at(0));
4198 frame_->EmitPop(r0);
4199 __ tst(r0, Operand(kSmiTagMask));
4200 false_target()->Branch(eq);
4201 Register map_reg = r2;
4202 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
4203 cc_reg_ = eq;
4204}
4205
4206
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004207void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004208 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004209 ASSERT(args->length() == 1);
4210 LoadAndSpill(args->at(0));
4211 frame_->EmitPop(r0);
4212 __ tst(r0, Operand(kSmiTagMask));
4213 false_target()->Branch(eq);
4214 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4215 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
4216 __ tst(r1, Operand(1 << Map::kIsUndetectable));
4217 cc_reg_ = ne;
4218}
4219
4220
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004221void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004222 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004223 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004224
4225 // Get the frame pointer for the calling frame.
4226 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4227
4228 // Skip the arguments adaptor frame if it exists.
4229 Label check_frame_marker;
4230 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004231 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004232 __ b(ne, &check_frame_marker);
4233 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
4234
4235 // Check the marker in the calling frame.
4236 __ bind(&check_frame_marker);
4237 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
4238 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
4239 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004240}
4241
4242
ager@chromium.org7c537e22008-10-16 08:43:32 +00004243void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004244 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004245 ASSERT(args->length() == 0);
4246
lrn@chromium.org25156de2010-04-06 13:10:27 +00004247 Label exit;
4248
4249 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00004250 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251
lrn@chromium.org25156de2010-04-06 13:10:27 +00004252 // Check if the calling frame is an arguments adaptor frame.
4253 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4254 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4255 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4256 __ b(ne, &exit);
4257
4258 // Arguments adaptor case: Read the arguments length from the
4259 // adaptor frame.
4260 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4261
4262 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004263 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004264}
4265
4266
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004267void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004268 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004269 ASSERT(args->length() == 1);
4270
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004271 // Satisfy contract with ArgumentsAccessStub:
4272 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004273 LoadAndSpill(args->at(0));
4274 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004275 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276
4277 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004278 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004279 frame_->CallStub(&stub, 0);
4280 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281}
4282
4283
ager@chromium.org357bf652010-04-12 11:30:10 +00004284void CodeGenerator::GenerateRandomHeapNumber(
4285 ZoneList<Expression*>* args) {
4286 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004287 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004288
4289 Label slow_allocate_heapnumber;
4290 Label heapnumber_allocated;
4291
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004292 __ AllocateHeapNumber(r4, r1, r2, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004293 __ jmp(&heapnumber_allocated);
4294
4295 __ bind(&slow_allocate_heapnumber);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004296 // To allocate a heap number, and ensure that it is not a smi, we
4297 // call the runtime function FUnaryMinus on 0, returning the double
4298 // -0.0. A new, distinct heap number is returned each time.
ager@chromium.org357bf652010-04-12 11:30:10 +00004299 __ mov(r0, Operand(Smi::FromInt(0)));
4300 __ push(r0);
4301 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004302 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004303
4304 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004305
4306 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4307 // by computing:
4308 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4309 if (CpuFeatures::IsSupported(VFP3)) {
4310 __ PrepareCallCFunction(0, r1);
4311 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4312
4313 CpuFeatures::Scope scope(VFP3);
4314 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4315 // Create this constant using mov/orr to avoid PC relative load.
4316 __ mov(r1, Operand(0x41000000));
4317 __ orr(r1, r1, Operand(0x300000));
4318 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4319 __ vmov(d7, r0, r1);
4320 // Move 0x4130000000000000 to VFP.
4321 __ mov(r0, Operand(0));
4322 __ vmov(d8, r0, r1);
4323 // Subtract and store the result in the heap number.
4324 __ vsub(d7, d7, d8);
4325 __ sub(r0, r4, Operand(kHeapObjectTag));
4326 __ vstr(d7, r0, HeapNumber::kValueOffset);
4327 frame_->EmitPush(r4);
4328 } else {
4329 __ mov(r0, Operand(r4));
4330 __ PrepareCallCFunction(1, r1);
4331 __ CallCFunction(
4332 ExternalReference::fill_heap_number_with_random_function(), 1);
4333 frame_->EmitPush(r0);
4334 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004335}
4336
4337
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004338void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4339 ASSERT_EQ(2, args->length());
4340
4341 Load(args->at(0));
4342 Load(args->at(1));
4343
ager@chromium.org5c838252010-02-19 08:53:10 +00004344 StringAddStub stub(NO_STRING_ADD_FLAGS);
4345 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004346 frame_->EmitPush(r0);
4347}
4348
4349
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004350void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4351 ASSERT_EQ(3, args->length());
4352
4353 Load(args->at(0));
4354 Load(args->at(1));
4355 Load(args->at(2));
4356
ager@chromium.org5c838252010-02-19 08:53:10 +00004357 SubStringStub stub;
4358 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004359 frame_->EmitPush(r0);
4360}
4361
4362
4363void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4364 ASSERT_EQ(2, args->length());
4365
4366 Load(args->at(0));
4367 Load(args->at(1));
4368
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004369 StringCompareStub stub;
4370 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004371 frame_->EmitPush(r0);
4372}
4373
4374
4375void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4376 ASSERT_EQ(4, args->length());
4377
4378 Load(args->at(0));
4379 Load(args->at(1));
4380 Load(args->at(2));
4381 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004382 RegExpExecStub stub;
4383 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004384 frame_->EmitPush(r0);
4385}
4386
4387
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004388void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4389 // No stub. This code only occurs a few times in regexp.js.
4390 const int kMaxInlineLength = 100;
4391 ASSERT_EQ(3, args->length());
4392 Load(args->at(0)); // Size of array, smi.
4393 Load(args->at(1)); // "index" property value.
4394 Load(args->at(2)); // "input" property value.
4395 {
4396 VirtualFrame::SpilledScope spilled_scope(frame_);
4397 Label slowcase;
4398 Label done;
4399 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4400 STATIC_ASSERT(kSmiTag == 0);
4401 STATIC_ASSERT(kSmiTagSize == 1);
4402 __ tst(r1, Operand(kSmiTagMask));
4403 __ b(ne, &slowcase);
4404 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4405 __ b(hi, &slowcase);
4406 // Smi-tagging is equivalent to multiplying by 2.
4407 // Allocate RegExpResult followed by FixedArray with size in ebx.
4408 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4409 // Elements: [Map][Length][..elements..]
4410 // Size of JSArray with two in-object properties and the header of a
4411 // FixedArray.
4412 int objects_size =
4413 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4414 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4415 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004416 __ AllocateInNewSpace(
4417 r2, // In: Size, in words.
4418 r0, // Out: Start of allocation (tagged).
4419 r3, // Scratch register.
4420 r4, // Scratch register.
4421 &slowcase,
4422 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004423 // r0: Start of allocated area, object-tagged.
4424 // r1: Number of elements in array, as smi.
4425 // r5: Number of elements, untagged.
4426
4427 // Set JSArray map to global.regexp_result_map().
4428 // Set empty properties FixedArray.
4429 // Set elements to point to FixedArray allocated right after the JSArray.
4430 // Interleave operations for better latency.
4431 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4432 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4433 __ mov(r4, Operand(Factory::empty_fixed_array()));
4434 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4435 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4436 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4437 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4438 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4439
4440 // Set input, index and length fields from arguments.
4441 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4442 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4443 __ add(sp, sp, Operand(kPointerSize));
4444 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4445 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4446
4447 // Fill out the elements FixedArray.
4448 // r0: JSArray, tagged.
4449 // r3: FixedArray, tagged.
4450 // r5: Number of elements in array, untagged.
4451
4452 // Set map.
4453 __ mov(r2, Operand(Factory::fixed_array_map()));
4454 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4455 // Set FixedArray length.
4456 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4457 // Fill contents of fixed-array with the-hole.
4458 __ mov(r2, Operand(Factory::the_hole_value()));
4459 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4460 // Fill fixed array elements with hole.
4461 // r0: JSArray, tagged.
4462 // r2: the hole.
4463 // r3: Start of elements in FixedArray.
4464 // r5: Number of elements to fill.
4465 Label loop;
4466 __ tst(r5, Operand(r5));
4467 __ bind(&loop);
4468 __ b(le, &done); // Jump if r1 is negative or zero.
4469 __ sub(r5, r5, Operand(1), SetCC);
4470 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4471 __ jmp(&loop);
4472
4473 __ bind(&slowcase);
4474 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4475
4476 __ bind(&done);
4477 }
4478 frame_->Forget(3);
4479 frame_->EmitPush(r0);
4480}
4481
4482
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004483class DeferredSearchCache: public DeferredCode {
4484 public:
4485 DeferredSearchCache(Register dst, Register cache, Register key)
4486 : dst_(dst), cache_(cache), key_(key) {
4487 set_comment("[ DeferredSearchCache");
4488 }
4489
4490 virtual void Generate();
4491
4492 private:
4493 Register dst_, cache_, key_;
4494};
4495
4496
4497void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00004498 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004499 __ CallRuntime(Runtime::kGetFromCache, 2);
4500 if (!dst_.is(r0)) {
4501 __ mov(dst_, r0);
4502 }
4503}
4504
4505
4506void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4507 ASSERT_EQ(2, args->length());
4508
4509 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4510 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4511
4512 Handle<FixedArray> jsfunction_result_caches(
4513 Top::global_context()->jsfunction_result_caches());
4514 if (jsfunction_result_caches->length() <= cache_id) {
4515 __ Abort("Attempt to use undefined cache.");
4516 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
4517 frame_->EmitPush(r0);
4518 return;
4519 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004520
4521 Load(args->at(1));
4522 frame_->EmitPop(r2);
4523
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004524 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
4525 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
4526 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4527 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
4528
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004529 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4530
4531 const int kFingerOffset =
4532 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4533 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004534 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4535 // r0 now holds finger offset as a smi.
4536 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4537 // r3 now points to the start of fixed array elements.
4538 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4539 // Note side effect of PreIndex: r3 now points to the key of the pair.
4540 __ cmp(r2, r0);
4541 deferred->Branch(ne);
4542
4543 __ ldr(r0, MemOperand(r3, kPointerSize));
4544
4545 deferred->BindExit();
4546 frame_->EmitPush(r0);
4547}
4548
4549
ager@chromium.org5c838252010-02-19 08:53:10 +00004550void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4551 ASSERT_EQ(args->length(), 1);
4552
4553 // Load the argument on the stack and jump to the runtime.
4554 Load(args->at(0));
4555
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004556 NumberToStringStub stub;
4557 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004558 frame_->EmitPush(r0);
4559}
4560
4561
ager@chromium.orgac091b72010-05-05 07:34:42 +00004562class DeferredSwapElements: public DeferredCode {
4563 public:
4564 DeferredSwapElements(Register object, Register index1, Register index2)
4565 : object_(object), index1_(index1), index2_(index2) {
4566 set_comment("[ DeferredSwapElements");
4567 }
4568
4569 virtual void Generate();
4570
4571 private:
4572 Register object_, index1_, index2_;
4573};
4574
4575
4576void DeferredSwapElements::Generate() {
4577 __ push(object_);
4578 __ push(index1_);
4579 __ push(index2_);
4580 __ CallRuntime(Runtime::kSwapElements, 3);
4581}
4582
4583
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004584void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
4585 Comment cmnt(masm_, "[ GenerateSwapElements");
4586
4587 ASSERT_EQ(3, args->length());
4588
4589 Load(args->at(0));
4590 Load(args->at(1));
4591 Load(args->at(2));
4592
ager@chromium.orgac091b72010-05-05 07:34:42 +00004593 Register index2 = r2;
4594 Register index1 = r1;
4595 Register object = r0;
4596 Register tmp1 = r3;
4597 Register tmp2 = r4;
4598
4599 frame_->EmitPop(index2);
4600 frame_->EmitPop(index1);
4601 frame_->EmitPop(object);
4602
4603 DeferredSwapElements* deferred =
4604 new DeferredSwapElements(object, index1, index2);
4605
4606 // Fetch the map and check if array is in fast case.
4607 // Check that object doesn't require security checks and
4608 // has no indexed interceptor.
4609 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
4610 deferred->Branch(lt);
4611 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
4612 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
4613 deferred->Branch(nz);
4614
4615 // Check the object's elements are in fast case.
4616 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
4617 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
4618 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4619 __ cmp(tmp2, ip);
4620 deferred->Branch(ne);
4621
4622 // Smi-tagging is equivalent to multiplying by 2.
4623 STATIC_ASSERT(kSmiTag == 0);
4624 STATIC_ASSERT(kSmiTagSize == 1);
4625
4626 // Check that both indices are smis.
4627 __ mov(tmp2, index1);
4628 __ orr(tmp2, tmp2, index2);
4629 __ tst(tmp2, Operand(kSmiTagMask));
4630 deferred->Branch(nz);
4631
4632 // Bring the offsets into the fixed array in tmp1 into index1 and
4633 // index2.
4634 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4635 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
4636 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
4637
4638 // Swap elements.
4639 Register tmp3 = object;
4640 object = no_reg;
4641 __ ldr(tmp3, MemOperand(tmp1, index1));
4642 __ ldr(tmp2, MemOperand(tmp1, index2));
4643 __ str(tmp3, MemOperand(tmp1, index2));
4644 __ str(tmp2, MemOperand(tmp1, index1));
4645
4646 Label done;
4647 __ InNewSpace(tmp1, tmp2, eq, &done);
4648 // Possible optimization: do a check that both values are Smis
4649 // (or them and test against Smi mask.)
4650
4651 __ mov(tmp2, tmp1);
4652 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
4653 __ CallStub(&recordWrite1);
4654
4655 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
4656 __ CallStub(&recordWrite2);
4657
4658 __ bind(&done);
4659
4660 deferred->BindExit();
4661 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
4662 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004663}
4664
4665
ager@chromium.org357bf652010-04-12 11:30:10 +00004666void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4667 Comment cmnt(masm_, "[ GenerateCallFunction");
4668
4669 ASSERT(args->length() >= 2);
4670
4671 int n_args = args->length() - 2; // for receiver and function.
4672 Load(args->at(0)); // receiver
4673 for (int i = 0; i < n_args; i++) {
4674 Load(args->at(i + 1));
4675 }
4676 Load(args->at(n_args + 1)); // function
4677 frame_->CallJSFunction(n_args);
4678 frame_->EmitPush(r0);
4679}
4680
4681
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004682void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4683 ASSERT_EQ(args->length(), 1);
4684 // Load the argument on the stack and jump to the runtime.
4685 Load(args->at(0));
4686 frame_->CallRuntime(Runtime::kMath_sin, 1);
4687 frame_->EmitPush(r0);
4688}
4689
4690
4691void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4692 ASSERT_EQ(args->length(), 1);
4693 // Load the argument on the stack and jump to the runtime.
4694 Load(args->at(0));
4695 frame_->CallRuntime(Runtime::kMath_cos, 1);
4696 frame_->EmitPush(r0);
4697}
4698
4699
ager@chromium.org7c537e22008-10-16 08:43:32 +00004700void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004701 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004702 ASSERT(args->length() == 2);
4703
4704 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004705 LoadAndSpill(args->at(0));
4706 LoadAndSpill(args->at(1));
4707 frame_->EmitPop(r0);
4708 frame_->EmitPop(r1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004709 __ cmp(r0, r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004710 cc_reg_ = eq;
4711}
4712
4713
ager@chromium.org7c537e22008-10-16 08:43:32 +00004714void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004715#ifdef DEBUG
4716 int original_height = frame_->height();
4717#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004718 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004719 if (CheckForInlineRuntimeCall(node)) {
4720 ASSERT((has_cc() && frame_->height() == original_height) ||
4721 (!has_cc() && frame_->height() == original_height + 1));
4722 return;
4723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004724
4725 ZoneList<Expression*>* args = node->arguments();
4726 Comment cmnt(masm_, "[ CallRuntime");
4727 Runtime::Function* function = node->function();
4728
ager@chromium.org41826e72009-03-30 13:30:57 +00004729 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004730 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004731 // Push the builtins object found in the current global object.
4732 __ ldr(r1, GlobalObject());
4733 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004734 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004735 }
mads.s.ager31e71382008-08-13 09:32:07 +00004736
ager@chromium.org41826e72009-03-30 13:30:57 +00004737 // Push the arguments ("left-to-right").
4738 int arg_count = args->length();
4739 for (int i = 0; i < arg_count; i++) {
4740 LoadAndSpill(args->at(i));
4741 }
mads.s.ager31e71382008-08-13 09:32:07 +00004742
ager@chromium.org41826e72009-03-30 13:30:57 +00004743 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004744 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004745 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004746 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4747 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004748 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004749 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004750 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004751 } else {
4752 // Call the C runtime function.
4753 frame_->CallRuntime(function, arg_count);
4754 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004755 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004756 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004757}
4758
4759
ager@chromium.org7c537e22008-10-16 08:43:32 +00004760void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004761#ifdef DEBUG
4762 int original_height = frame_->height();
4763#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004764 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004765 Comment cmnt(masm_, "[ UnaryOperation");
4766
4767 Token::Value op = node->op();
4768
4769 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004770 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004771 false_target(),
4772 true_target(),
4773 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004774 // LoadCondition may (and usually does) leave a test and branch to
4775 // be emitted by the caller. In that case, negate the condition.
4776 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004777
4778 } else if (op == Token::DELETE) {
4779 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004780 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004781 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004782 LoadAndSpill(property->obj());
4783 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004784 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004785
mads.s.ager31e71382008-08-13 09:32:07 +00004786 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004787 Slot* slot = variable->slot();
4788 if (variable->is_global()) {
4789 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004790 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004791 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004792 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004793
4794 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4795 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004796 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004797 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004798 frame_->EmitPush(r0);
4799 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004800 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004801 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004802 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004803 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004804 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004805
mads.s.ager31e71382008-08-13 09:32:07 +00004806 } else {
4807 // Default: Result of deleting non-global, not dynamically
4808 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004809 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004810 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004811
4812 } else {
4813 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004814 LoadAndSpill(node->expression()); // may have side-effects
4815 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004816 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004817 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004818 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004819
4820 } else if (op == Token::TYPEOF) {
4821 // Special case for loading the typeof expression; see comment on
4822 // LoadTypeofExpression().
4823 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004824 frame_->CallRuntime(Runtime::kTypeof, 1);
4825 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004826
4827 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004828 bool overwrite =
4829 (node->expression()->AsBinaryOperation() != NULL &&
4830 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004831 LoadAndSpill(node->expression());
4832 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004833 switch (op) {
4834 case Token::NOT:
4835 case Token::DELETE:
4836 case Token::TYPEOF:
4837 UNREACHABLE(); // handled above
4838 break;
4839
4840 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004841 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004842 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004843 break;
4844 }
4845
4846 case Token::BIT_NOT: {
4847 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004848 JumpTarget smi_label;
4849 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004850 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004851 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004852
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004853 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4854 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004855 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004856
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004857 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004858 __ mvn(r0, Operand(r0));
4859 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004860 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004861 break;
4862 }
4863
4864 case Token::VOID:
4865 // since the stack top is cached in r0, popping and then
4866 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004867 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004868 break;
4869
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004870 case Token::ADD: {
4871 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004872 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004873 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004874 continue_label.Branch(eq);
4875 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004876 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004877 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004878 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004879 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004880 default:
4881 UNREACHABLE();
4882 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004883 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004884 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004885 ASSERT(!has_valid_frame() ||
4886 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004887 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004888}
4889
4890
ager@chromium.org7c537e22008-10-16 08:43:32 +00004891void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004892#ifdef DEBUG
4893 int original_height = frame_->height();
4894#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004895 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004896 Comment cmnt(masm_, "[ CountOperation");
4897
4898 bool is_postfix = node->is_postfix();
4899 bool is_increment = node->op() == Token::INC;
4900
4901 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4902 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4903
4904 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004905 if (is_postfix) {
4906 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004907 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004908 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004909
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004910 // A constant reference is not saved to, so a constant reference is not a
4911 // compound assignment reference.
4912 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004913 if (target.is_illegal()) {
4914 // Spoof the virtual frame to have the expected height (one higher
4915 // than on entry).
4916 if (!is_postfix) {
4917 __ mov(r0, Operand(Smi::FromInt(0)));
4918 frame_->EmitPush(r0);
4919 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004920 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004921 return;
4922 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004923 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004924 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004925
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004926 JumpTarget slow;
4927 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004928
4929 // Load the value (1) into register r1.
4930 __ mov(r1, Operand(Smi::FromInt(1)));
4931
4932 // Check for smi operand.
4933 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004934 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004935
4936 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004937 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004938 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004939 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004940
4941 // Perform optimistic increment/decrement.
4942 if (is_increment) {
4943 __ add(r0, r0, Operand(r1), SetCC);
4944 } else {
4945 __ sub(r0, r0, Operand(r1), SetCC);
4946 }
4947
4948 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004949 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004950
4951 // Revert optimistic increment/decrement.
4952 if (is_increment) {
4953 __ sub(r0, r0, Operand(r1));
4954 } else {
4955 __ add(r0, r0, Operand(r1));
4956 }
4957
4958 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004959 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004960 {
4961 // Convert the operand to a number.
4962 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004963 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004965 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004966 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004967 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004968 }
4969
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004970 // Compute the new value.
4971 __ mov(r1, Operand(Smi::FromInt(1)));
4972 frame_->EmitPush(r0);
4973 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004975 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004976 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004977 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004978 }
4979
4980 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004981 exit.Bind();
4982 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004983 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004984 }
4985
4986 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004987 if (is_postfix) frame_->EmitPop(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00004988 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004989}
4990
4991
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004992void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004993 // According to ECMA-262 section 11.11, page 58, the binary logical
4994 // operators must yield the result of one of the two expressions
4995 // before any ToBoolean() conversions. This means that the value
4996 // produced by a && or || operator is not necessarily a boolean.
4997
4998 // NOTE: If the left hand side produces a materialized value (not in
4999 // the CC register), we force the right hand side to do the
5000 // same. This is necessary because we may have to branch to the exit
5001 // after evaluating the left hand side (due to the shortcut
5002 // semantics), but the compiler must (statically) know if the result
5003 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005004 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005005 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005006 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005007 &is_true,
5008 false_target(),
5009 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005010 if (has_valid_frame() && !has_cc()) {
5011 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005012 JumpTarget pop_and_continue;
5013 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005014
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005015 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005016 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005017 // Avoid popping the result if it converts to 'false' using the
5018 // standard ToBoolean() conversion as described in ECMA-262,
5019 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005020 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021 Branch(false, &exit);
5022
5023 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005024 pop_and_continue.Bind();
5025 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005026
5027 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005028 is_true.Bind();
5029 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005030
5031 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005032 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005033 } else if (has_cc() || is_true.is_linked()) {
5034 // The left-hand side is either (a) partially compiled to
5035 // control flow with a final branch left to emit or (b) fully
5036 // compiled to control flow and possibly true.
5037 if (has_cc()) {
5038 Branch(false, false_target());
5039 }
5040 is_true.Bind();
5041 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005042 true_target(),
5043 false_target(),
5044 false);
5045 } else {
5046 // Nothing to do.
5047 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005048 }
5049
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005050 } else {
5051 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005052 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005053 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005054 true_target(),
5055 &is_false,
5056 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005057 if (has_valid_frame() && !has_cc()) {
5058 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005059 JumpTarget pop_and_continue;
5060 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005062 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005063 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005064 // Avoid popping the result if it converts to 'true' using the
5065 // standard ToBoolean() conversion as described in ECMA-262,
5066 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005067 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005068 Branch(true, &exit);
5069
5070 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005071 pop_and_continue.Bind();
5072 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005073
5074 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005075 is_false.Bind();
5076 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005077
5078 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005079 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005080 } else if (has_cc() || is_false.is_linked()) {
5081 // The left-hand side is either (a) partially compiled to
5082 // control flow with a final branch left to emit or (b) fully
5083 // compiled to control flow and possibly false.
5084 if (has_cc()) {
5085 Branch(true, true_target());
5086 }
5087 is_false.Bind();
5088 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005089 true_target(),
5090 false_target(),
5091 false);
5092 } else {
5093 // Nothing to do.
5094 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005095 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005096 }
5097}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005098
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005099
5100void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5101#ifdef DEBUG
5102 int original_height = frame_->height();
5103#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005104 Comment cmnt(masm_, "[ BinaryOperation");
5105
5106 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005107 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005108 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005109 } else {
5110 // Optimize for the case where (at least) one of the expressions
5111 // is a literal small integer.
5112 Literal* lliteral = node->left()->AsLiteral();
5113 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005114 // NOTE: The code below assumes that the slow cases (calls to runtime)
5115 // never return a constant/immutable object.
5116 bool overwrite_left =
5117 (node->left()->AsBinaryOperation() != NULL &&
5118 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5119 bool overwrite_right =
5120 (node->right()->AsBinaryOperation() != NULL &&
5121 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005122
5123 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005124 VirtualFrame::RegisterAllocationScope scope(this);
5125 Load(node->left());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005126 SmiOperation(node->op(),
5127 rliteral->handle(),
5128 false,
5129 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005130 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005131 VirtualFrame::RegisterAllocationScope scope(this);
5132 Load(node->right());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005133 SmiOperation(node->op(),
5134 lliteral->handle(),
5135 true,
5136 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005137 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00005138 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005139 OverwriteMode overwrite_mode = NO_OVERWRITE;
5140 if (overwrite_left) {
5141 overwrite_mode = OVERWRITE_LEFT;
5142 } else if (overwrite_right) {
5143 overwrite_mode = OVERWRITE_RIGHT;
5144 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005145 Load(node->left());
5146 Load(node->right());
5147 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005148 }
5149 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005150 ASSERT(!has_valid_frame() ||
5151 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005152 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005153}
5154
5155
ager@chromium.org7c537e22008-10-16 08:43:32 +00005156void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005157#ifdef DEBUG
5158 int original_height = frame_->height();
5159#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00005160 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005161 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005162 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005163 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005164}
5165
5166
ager@chromium.org7c537e22008-10-16 08:43:32 +00005167void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005168#ifdef DEBUG
5169 int original_height = frame_->height();
5170#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005171 Comment cmnt(masm_, "[ CompareOperation");
5172
ager@chromium.org357bf652010-04-12 11:30:10 +00005173 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005175 // Get the expressions from the node.
5176 Expression* left = node->left();
5177 Expression* right = node->right();
5178 Token::Value op = node->op();
5179
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005180 // To make null checks efficient, we check if either left or right is the
5181 // literal 'null'. If so, we optimize the code by inlining a null check
5182 // instead of calling the (very) general runtime routine for checking
5183 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005184 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005185 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005186 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005187 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005188 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5189 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005190 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005191 Load(left_is_null ? right : left);
5192 Register tos = frame_->PopToRegister();
5193 // JumpTargets can't cope with register allocation yet.
5194 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005195 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005196 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005197
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005198 // The 'null' value is only equal to 'undefined' if using non-strict
5199 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005200 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005201 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005202
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005203 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005204 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005205 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005206
ager@chromium.org357bf652010-04-12 11:30:10 +00005207 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005208 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005209
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005210 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005211 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5212 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5213 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5214 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005215 }
5216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005217 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005218 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005219 return;
5220 }
5221 }
5222
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005223 // To make typeof testing for natives implemented in JavaScript really
5224 // efficient, we generate special code for expressions of the form:
5225 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005226 UnaryOperation* operation = left->AsUnaryOperation();
5227 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5228 (operation != NULL && operation->op() == Token::TYPEOF) &&
5229 (right->AsLiteral() != NULL &&
5230 right->AsLiteral()->handle()->IsString())) {
5231 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5232
ager@chromium.org357bf652010-04-12 11:30:10 +00005233 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005234 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005235 Register tos = frame_->PopToRegister();
5236
5237 // JumpTargets can't cope with register allocation yet.
5238 frame_->SpillAll();
5239
5240 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005241
5242 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005243 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005244 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005245 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005246 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005247 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005248 cc_reg_ = eq;
5249
5250 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005251 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005252 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005253
ager@chromium.org357bf652010-04-12 11:30:10 +00005254 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005255
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005256 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005257 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5258 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5259 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005260 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005261
ager@chromium.org357bf652010-04-12 11:30:10 +00005262 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5263 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005264 cc_reg_ = lt;
5265
5266 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005267 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005268 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005269 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005270 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005271 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005272 cc_reg_ = eq;
5273
5274 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005275 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005276 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005277 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005278
ager@chromium.org357bf652010-04-12 11:30:10 +00005279 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005280 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005281
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005282 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005283 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5284 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5285 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5286 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005287
5288 cc_reg_ = eq;
5289
5290 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005291 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005292 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005293 Register map_reg = scratch;
5294 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005295 true_target()->Branch(eq);
5296 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00005297 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005298 cc_reg_ = eq;
5299
5300 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005301 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005302 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005303
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005304 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005305 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005306 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005307
ager@chromium.org357bf652010-04-12 11:30:10 +00005308 Register map_reg = scratch;
5309 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005310 false_target()->Branch(eq);
5311
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005312 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005313 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5314 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5315 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005316 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005317
ager@chromium.org357bf652010-04-12 11:30:10 +00005318 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5319 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005320 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00005321 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005322 cc_reg_ = le;
5323
5324 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005325 // Uncommon case: typeof testing against a string literal that is
5326 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005327 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005328 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005329 ASSERT(!has_valid_frame() ||
5330 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005331 return;
5332 }
5333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005334 switch (op) {
5335 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005336 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005337 break;
5338
5339 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005340 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005341 break;
5342
5343 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005344 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005345 break;
5346
5347 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005348 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005349 break;
5350
5351 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005352 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005353 break;
5354
5355 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005356 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005357 break;
5358
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005359 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005360 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005361 LoadAndSpill(left);
5362 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005363 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005364 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005365 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005366 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005367
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005368 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005369 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005370 LoadAndSpill(left);
5371 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005372 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005373 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005374 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005375 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005376 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005377 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005378 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005379
5380 default:
5381 UNREACHABLE();
5382 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005383 ASSERT((has_cc() && frame_->height() == original_height) ||
5384 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005385}
5386
5387
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005388class DeferredReferenceGetNamedValue: public DeferredCode {
5389 public:
5390 explicit DeferredReferenceGetNamedValue(Handle<String> name) : name_(name) {
5391 set_comment("[ DeferredReferenceGetNamedValue");
5392 }
5393
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005394 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005395
5396 private:
5397 Handle<String> name_;
5398};
5399
5400
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005401void DeferredReferenceGetNamedValue::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005402 Register scratch1 = VirtualFrame::scratch0();
5403 Register scratch2 = VirtualFrame::scratch1();
5404 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
5405 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
5406
5407 // Setup the registers and call load IC.
5408 // On entry to this deferred code, r0 is assumed to already contain the
5409 // receiver from the top of the stack.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005410 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005411
5412 // The rest of the instructions in the deferred code must be together.
5413 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5414 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
5415 __ Call(ic, RelocInfo::CODE_TARGET);
5416 // The call must be followed by a nop(1) instruction to indicate that the
5417 // in-object has been inlined.
5418 __ nop(PROPERTY_ACCESS_INLINED);
5419
5420 // Block the constant pool for one more instruction after leaving this
5421 // constant pool block scope to include the branch instruction ending the
5422 // deferred code.
5423 __ BlockConstPoolFor(1);
5424 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005425}
5426
5427
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005428class DeferredReferenceGetKeyedValue: public DeferredCode {
5429 public:
5430 DeferredReferenceGetKeyedValue() {
5431 set_comment("[ DeferredReferenceGetKeyedValue");
5432 }
5433
5434 virtual void Generate();
5435};
5436
5437
5438void DeferredReferenceGetKeyedValue::Generate() {
5439 Register scratch1 = VirtualFrame::scratch0();
5440 Register scratch2 = VirtualFrame::scratch1();
5441 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
5442 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
5443
5444 // The rest of the instructions in the deferred code must be together.
5445 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005446 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005447 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
5448 __ Call(ic, RelocInfo::CODE_TARGET);
5449 // The call must be followed by a nop instruction to indicate that the
5450 // keyed load has been inlined.
5451 __ nop(PROPERTY_ACCESS_INLINED);
5452
5453 // Block the constant pool for one more instruction after leaving this
5454 // constant pool block scope to include the branch instruction ending the
5455 // deferred code.
5456 __ BlockConstPoolFor(1);
5457 }
5458}
5459
5460
5461class DeferredReferenceSetKeyedValue: public DeferredCode {
5462 public:
5463 DeferredReferenceSetKeyedValue() {
5464 set_comment("[ DeferredReferenceSetKeyedValue");
5465 }
5466
5467 virtual void Generate();
5468};
5469
5470
5471void DeferredReferenceSetKeyedValue::Generate() {
5472 Register scratch1 = VirtualFrame::scratch0();
5473 Register scratch2 = VirtualFrame::scratch1();
5474 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
5475 __ IncrementCounter(
5476 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
5477
5478 // The rest of the instructions in the deferred code must be together.
5479 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5480 // Call keyed load IC. It has receiver amd key on the stack and the value to
5481 // store in r0.
5482 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
5483 __ Call(ic, RelocInfo::CODE_TARGET);
5484 // The call must be followed by a nop instruction to indicate that the
5485 // keyed store has been inlined.
5486 __ nop(PROPERTY_ACCESS_INLINED);
5487
5488 // Block the constant pool for one more instruction after leaving this
5489 // constant pool block scope to include the branch instruction ending the
5490 // deferred code.
5491 __ BlockConstPoolFor(1);
5492 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005493}
5494
5495
5496void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
5497 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
5498 Comment cmnt(masm(), "[ Load from named Property");
5499 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005500 frame_->CallLoadIC(name,
5501 is_contextual
5502 ? RelocInfo::CODE_TARGET_CONTEXT
5503 : RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005504 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005505 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005506 Comment cmnt(masm(), "[ Inlined named property load");
5507
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005508 // Counter will be decremented in the deferred code. Placed here to avoid
5509 // having it in the instruction stream below where patching will occur.
5510 __ IncrementCounter(&Counters::named_load_inline, 1,
5511 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005512
5513 // The following instructions are the inlined load of an in-object property.
5514 // Parts of this code is patched, so the exact instructions generated needs
5515 // to be fixed. Therefore the instruction pool is blocked when generating
5516 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005517
5518 // Load the receiver from the stack.
5519 frame_->SpillAllButCopyTOSToR0();
5520
5521 DeferredReferenceGetNamedValue* deferred =
5522 new DeferredReferenceGetNamedValue(name);
5523
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005524#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005525 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005526 Label check_inlined_codesize;
5527 masm_->bind(&check_inlined_codesize);
5528#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005529
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005530 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005531 // Check that the receiver is a heap object.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005532 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005533 deferred->Branch(eq);
5534
5535 // Check the map. The null map used below is patched by the inline cache
5536 // code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005537 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005538 __ mov(r3, Operand(Factory::null_value()));
5539 __ cmp(r2, r3);
5540 deferred->Branch(ne);
5541
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005542 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005543 // inline cache code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005544 __ ldr(r0, MemOperand(r0, 0));
5545
5546 // Make sure that the expected number of instructions are generated.
5547 ASSERT_EQ(kInlinedNamedLoadInstructions,
5548 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005549 }
5550
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005551 deferred->BindExit();
5552 }
5553}
5554
5555
ager@chromium.orgac091b72010-05-05 07:34:42 +00005556void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
5557#ifdef DEBUG
5558 int expected_height = frame_->height() - (is_contextual ? 1 : 2);
5559#endif
5560 frame_->CallStoreIC(name, is_contextual);
5561
5562 ASSERT_EQ(expected_height, frame_->height());
5563}
5564
5565
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005566void CodeGenerator::EmitKeyedLoad() {
5567 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005568 Comment cmnt(masm_, "[ Load from keyed property");
5569 frame_->CallKeyedLoadIC();
5570 } else {
5571 // Inline the keyed load.
5572 Comment cmnt(masm_, "[ Inlined load from keyed property");
5573
5574 // Counter will be decremented in the deferred code. Placed here to avoid
5575 // having it in the instruction stream below where patching will occur.
5576 __ IncrementCounter(&Counters::keyed_load_inline, 1,
5577 frame_->scratch0(), frame_->scratch1());
5578
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005579 // Load the key and receiver from the stack to r0 and r1.
5580 frame_->PopToR1R0();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005581 Register receiver = r0;
5582 Register key = r1;
5583 VirtualFrame::SpilledScope spilled(frame_);
5584
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005585 // The deferred code expects key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005586 DeferredReferenceGetKeyedValue* deferred =
5587 new DeferredReferenceGetKeyedValue();
5588
5589 // Check that the receiver is a heap object.
5590 __ tst(receiver, Operand(kSmiTagMask));
5591 deferred->Branch(eq);
5592
5593 // The following instructions are the part of the inlined load keyed
5594 // property code which can be patched. Therefore the exact number of
5595 // instructions generated need to be fixed, so the constant pool is blocked
5596 // while generating this code.
5597#ifdef DEBUG
5598 int kInlinedKeyedLoadInstructions = 19;
5599 Label check_inlined_codesize;
5600 masm_->bind(&check_inlined_codesize);
5601#endif
5602 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5603 Register scratch1 = VirtualFrame::scratch0();
5604 Register scratch2 = VirtualFrame::scratch1();
5605 // Check the map. The null map used below is patched by the inline cache
5606 // code.
5607 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
5608 __ mov(scratch2, Operand(Factory::null_value()));
5609 __ cmp(scratch1, scratch2);
5610 deferred->Branch(ne);
5611
5612 // Check that the key is a smi.
5613 __ tst(key, Operand(kSmiTagMask));
5614 deferred->Branch(ne);
5615
5616 // Get the elements array from the receiver and check that it
5617 // is not a dictionary.
5618 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
5619 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
5620 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5621 __ cmp(scratch2, ip);
5622 deferred->Branch(ne);
5623
5624 // Check that key is within bounds. Use unsigned comparison to handle
5625 // negative keys.
5626 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
5627 __ cmp(scratch2, Operand(key, ASR, kSmiTagSize));
5628 deferred->Branch(ls); // Unsigned less equal.
5629
5630 // Load and check that the result is not the hole (key is a smi).
5631 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
5632 __ add(scratch1,
5633 scratch1,
5634 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5635 __ ldr(r0,
5636 MemOperand(scratch1, key, LSL,
5637 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5638 __ cmp(r0, scratch2);
5639 // This is the only branch to deferred where r0 and r1 do not contain the
5640 // receiver and key. We can't just load undefined here because we have to
5641 // check the prototype.
5642 deferred->Branch(eq);
5643
5644 // Make sure that the expected number of instructions are generated.
5645 ASSERT_EQ(kInlinedKeyedLoadInstructions,
5646 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5647 }
5648
5649 deferred->BindExit();
5650 }
5651}
5652
5653
5654void CodeGenerator::EmitKeyedStore(StaticType* key_type) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00005655 VirtualFrame::SpilledScope scope(frame_);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005656 // Generate inlined version of the keyed store if the code is in a loop
5657 // and the key is likely to be a smi.
5658 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
5659 // Inline the keyed store.
5660 Comment cmnt(masm_, "[ Inlined store to keyed property");
5661
5662 DeferredReferenceSetKeyedValue* deferred =
5663 new DeferredReferenceSetKeyedValue();
5664
5665 // Counter will be decremented in the deferred code. Placed here to avoid
5666 // having it in the instruction stream below where patching will occur.
5667 __ IncrementCounter(&Counters::keyed_store_inline, 1,
5668 frame_->scratch0(), frame_->scratch1());
5669
5670 // Check that the value is a smi. As this inlined code does not set the
5671 // write barrier it is only possible to store smi values.
5672 __ tst(r0, Operand(kSmiTagMask));
5673 deferred->Branch(ne);
5674
5675 // Load the key and receiver from the stack.
5676 __ ldr(r1, MemOperand(sp, 0));
5677 __ ldr(r2, MemOperand(sp, kPointerSize));
5678
5679 // Check that the key is a smi.
5680 __ tst(r1, Operand(kSmiTagMask));
5681 deferred->Branch(ne);
5682
5683 // Check that the receiver is a heap object.
5684 __ tst(r2, Operand(kSmiTagMask));
5685 deferred->Branch(eq);
5686
5687 // Check that the receiver is a JSArray.
5688 __ CompareObjectType(r2, r3, r3, JS_ARRAY_TYPE);
5689 deferred->Branch(ne);
5690
5691 // Check that the key is within bounds. Both the key and the length of
5692 // the JSArray are smis. Use unsigned comparison to handle negative keys.
5693 __ ldr(r3, FieldMemOperand(r2, JSArray::kLengthOffset));
5694 __ cmp(r3, r1);
5695 deferred->Branch(ls); // Unsigned less equal.
5696
5697 // The following instructions are the part of the inlined store keyed
5698 // property code which can be patched. Therefore the exact number of
5699 // instructions generated need to be fixed, so the constant pool is blocked
5700 // while generating this code.
5701#ifdef DEBUG
5702 int kInlinedKeyedStoreInstructions = 7;
5703 Label check_inlined_codesize;
5704 masm_->bind(&check_inlined_codesize);
5705#endif
5706 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5707 // Get the elements array from the receiver and check that it
5708 // is not a dictionary.
5709 __ ldr(r3, FieldMemOperand(r2, JSObject::kElementsOffset));
5710 __ ldr(r4, FieldMemOperand(r3, JSObject::kMapOffset));
5711 // Read the fixed array map from the constant pool (not from the root
5712 // array) so that the value can be patched. When debugging, we patch this
5713 // comparison to always fail so that we will hit the IC call in the
5714 // deferred code which will allow the debugger to break for fast case
5715 // stores.
5716 __ mov(r5, Operand(Factory::fixed_array_map()));
5717 __ cmp(r4, r5);
5718 deferred->Branch(ne);
5719
5720 // Store the value.
5721 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5722 __ str(r0, MemOperand(r3, r1, LSL,
5723 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
5724
5725 // Make sure that the expected number of instructions are generated.
5726 ASSERT_EQ(kInlinedKeyedStoreInstructions,
5727 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
5728 }
5729
5730 deferred->BindExit();
5731 } else {
5732 frame()->CallKeyedStoreIC();
5733 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005734}
5735
5736
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005737#ifdef DEBUG
5738bool CodeGenerator::HasValidEntryRegisters() { return true; }
5739#endif
5740
5741
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005742#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005743#define __ ACCESS_MASM(masm)
5744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005745
ager@chromium.org7c537e22008-10-16 08:43:32 +00005746Handle<String> Reference::GetName() {
5747 ASSERT(type_ == NAMED);
5748 Property* property = expression_->AsProperty();
5749 if (property == NULL) {
5750 // Global variable reference treated as a named property reference.
5751 VariableProxy* proxy = expression_->AsVariableProxy();
5752 ASSERT(proxy->AsVariable() != NULL);
5753 ASSERT(proxy->AsVariable()->is_global());
5754 return proxy->name();
5755 } else {
5756 Literal* raw_name = property->key()->AsLiteral();
5757 ASSERT(raw_name != NULL);
5758 return Handle<String>(String::cast(*raw_name->handle()));
5759 }
5760}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005761
ager@chromium.org7c537e22008-10-16 08:43:32 +00005762
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005763void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005764 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005765 ASSERT(!is_illegal());
5766 ASSERT(!cgen_->has_cc());
5767 MacroAssembler* masm = cgen_->masm();
5768 Property* property = expression_->AsProperty();
5769 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005770 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00005771 }
5772
5773 switch (type_) {
5774 case SLOT: {
5775 Comment cmnt(masm, "[ Load from Slot");
5776 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
5777 ASSERT(slot != NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005778 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005779 if (!persist_after_get_) {
5780 cgen_->UnloadReference(this);
5781 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005782 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005783 }
5784
ager@chromium.org7c537e22008-10-16 08:43:32 +00005785 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00005786 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005787 bool is_global = var != NULL;
5788 ASSERT(!is_global || var->is_global());
5789 cgen_->EmitNamedLoad(GetName(), is_global);
5790 cgen_->frame()->EmitPush(r0);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005791 if (!persist_after_get_) {
5792 cgen_->UnloadReference(this);
5793 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005794 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005795 }
5796
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005797 case KEYED: {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005798 if (persist_after_get_) {
5799 cgen_->frame()->Dup2();
5800 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005801 ASSERT(property != NULL);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005802 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005803 cgen_->frame()->EmitPush(r0);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005804 if (!persist_after_get_) set_unloaded();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005805 break;
5806 }
5807
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005808 default:
5809 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005810 }
5811}
5812
5813
ager@chromium.org7c537e22008-10-16 08:43:32 +00005814void Reference::SetValue(InitState init_state) {
5815 ASSERT(!is_illegal());
5816 ASSERT(!cgen_->has_cc());
5817 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005818 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005819 Property* property = expression_->AsProperty();
5820 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005821 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005822 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005823
ager@chromium.org7c537e22008-10-16 08:43:32 +00005824 switch (type_) {
5825 case SLOT: {
5826 Comment cmnt(masm, "[ Store to Slot");
5827 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005828 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00005829 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005830 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005831 }
5832
ager@chromium.org7c537e22008-10-16 08:43:32 +00005833 case NAMED: {
5834 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00005835 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005836 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005837 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005838 break;
5839 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005840
ager@chromium.org7c537e22008-10-16 08:43:32 +00005841 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005842 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005843 Comment cmnt(masm, "[ Store to keyed Property");
5844 Property* property = expression_->AsProperty();
5845 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005846 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005847
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005848 frame->EmitPop(r0); // Value.
5849 cgen_->EmitKeyedStore(property->key()->type());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005850 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005851 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005852 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005853 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005854
5855 default:
5856 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005857 }
5858}
5859
5860
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005861void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005862 // Create a new closure from the given function info in new
5863 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005864 Label gc;
5865
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005866 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005867 __ pop(r3);
5868
5869 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005870 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005871 r0,
5872 r1,
5873 r2,
5874 &gc,
5875 TAG_OBJECT);
5876
5877 // Compute the function map in the current global context and set that
5878 // as the map of the allocated object.
5879 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5880 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5881 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5882 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5883
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005884 // Initialize the rest of the function. We don't have to update the
5885 // write barrier because the allocated object is in new space.
5886 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5887 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5888 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5889 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5890 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5891 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5892 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5893 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005894
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005895 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005896 __ Ret();
5897
5898 // Create a new closure through the slower runtime call.
5899 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005900 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005901 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005902}
5903
5904
5905void FastNewContextStub::Generate(MacroAssembler* masm) {
5906 // Try to allocate the context in new space.
5907 Label gc;
5908 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5909
5910 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005911 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005912 r0,
5913 r1,
5914 r2,
5915 &gc,
5916 TAG_OBJECT);
5917
5918 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005919 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005920
5921 // Setup the object header.
5922 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5923 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5924 __ mov(r2, Operand(length));
5925 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5926
5927 // Setup the fixed slots.
5928 __ mov(r1, Operand(Smi::FromInt(0)));
5929 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5930 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5931 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5932 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5933
5934 // Copy the global object from the surrounding context.
5935 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5936 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5937
5938 // Initialize the rest of the slots to undefined.
5939 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5940 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5941 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5942 }
5943
5944 // Remove the on-stack argument and return.
5945 __ mov(cp, r0);
5946 __ pop();
5947 __ Ret();
5948
5949 // Need to collect. Call into runtime system.
5950 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005951 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005952}
5953
5954
ager@chromium.org5c838252010-02-19 08:53:10 +00005955void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5956 // Stack layout on entry:
5957 //
5958 // [sp]: constant elements.
5959 // [sp + kPointerSize]: literal index.
5960 // [sp + (2 * kPointerSize)]: literals array.
5961
5962 // All sizes here are multiples of kPointerSize.
5963 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5964 int size = JSArray::kSize + elements_size;
5965
5966 // Load boilerplate object into r3 and check if we need to create a
5967 // boilerplate.
5968 Label slow_case;
5969 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5970 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5971 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5972 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5973 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5974 __ cmp(r3, ip);
5975 __ b(eq, &slow_case);
5976
5977 // Allocate both the JS array and the elements array in one big
5978 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005979 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00005980 r0,
5981 r1,
5982 r2,
5983 &slow_case,
5984 TAG_OBJECT);
5985
5986 // Copy the JS array part.
5987 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5988 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5989 __ ldr(r1, FieldMemOperand(r3, i));
5990 __ str(r1, FieldMemOperand(r0, i));
5991 }
5992 }
5993
5994 if (length_ > 0) {
5995 // Get hold of the elements array of the boilerplate and setup the
5996 // elements pointer in the resulting object.
5997 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5998 __ add(r2, r0, Operand(JSArray::kSize));
5999 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
6000
6001 // Copy the elements array.
6002 for (int i = 0; i < elements_size; i += kPointerSize) {
6003 __ ldr(r1, FieldMemOperand(r3, i));
6004 __ str(r1, FieldMemOperand(r2, i));
6005 }
6006 }
6007
6008 // Return and remove the on-stack parameters.
6009 __ add(sp, sp, Operand(3 * kPointerSize));
6010 __ Ret();
6011
6012 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006013 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00006014}
6015
6016
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006017// Takes a Smi and converts to an IEEE 64 bit floating point value in two
6018// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
6019// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
6020// scratch register. Destroys the source register. No GC occurs during this
6021// stub so you don't have to set up the frame.
6022class ConvertToDoubleStub : public CodeStub {
6023 public:
6024 ConvertToDoubleStub(Register result_reg_1,
6025 Register result_reg_2,
6026 Register source_reg,
6027 Register scratch_reg)
6028 : result1_(result_reg_1),
6029 result2_(result_reg_2),
6030 source_(source_reg),
6031 zeros_(scratch_reg) { }
6032
6033 private:
6034 Register result1_;
6035 Register result2_;
6036 Register source_;
6037 Register zeros_;
6038
6039 // Minor key encoding in 16 bits.
6040 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
6041 class OpBits: public BitField<Token::Value, 2, 14> {};
6042
6043 Major MajorKey() { return ConvertToDouble; }
6044 int MinorKey() {
6045 // Encode the parameters in a unique 16 bit value.
6046 return result1_.code() +
6047 (result2_.code() << 4) +
6048 (source_.code() << 8) +
6049 (zeros_.code() << 12);
6050 }
6051
6052 void Generate(MacroAssembler* masm);
6053
6054 const char* GetName() { return "ConvertToDoubleStub"; }
6055
6056#ifdef DEBUG
6057 void Print() { PrintF("ConvertToDoubleStub\n"); }
6058#endif
6059};
6060
6061
6062void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
6063#ifndef BIG_ENDIAN_FLOATING_POINT
6064 Register exponent = result1_;
6065 Register mantissa = result2_;
6066#else
6067 Register exponent = result2_;
6068 Register mantissa = result1_;
6069#endif
6070 Label not_special;
6071 // Convert from Smi to integer.
6072 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
6073 // Move sign bit from source to destination. This works because the sign bit
6074 // in the exponent word of the double has the same position and polarity as
6075 // the 2's complement sign bit in a Smi.
6076 ASSERT(HeapNumber::kSignMask == 0x80000000u);
6077 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
6078 // Subtract from 0 if source was negative.
6079 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006080
6081 // We have -1, 0 or 1, which we treat specially. Register source_ contains
6082 // absolute value: it is either equal to 1 (special case of -1 and 1),
6083 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006084 __ cmp(source_, Operand(1));
6085 __ b(gt, &not_special);
6086
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006087 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
6088 static const uint32_t exponent_word_for_1 =
6089 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006090 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006091 // 1, 0 and -1 all have 0 for the second word.
6092 __ mov(mantissa, Operand(0));
6093 __ Ret();
6094
6095 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006096 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006097 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006098 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006099 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006100 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006101 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
6102 __ orr(exponent,
6103 exponent,
6104 Operand(mantissa, LSL, HeapNumber::kExponentShift));
6105 // Shift up the source chopping the top bit off.
6106 __ add(zeros_, zeros_, Operand(1));
6107 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
6108 __ mov(source_, Operand(source_, LSL, zeros_));
6109 // Compute lower part of fraction (last 12 bits).
6110 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
6111 // And the top (top 20 bits).
6112 __ orr(exponent,
6113 exponent,
6114 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
6115 __ Ret();
6116}
6117
6118
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006119// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006120void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006121 Label max_negative_int;
6122 // the_int_ has the answer which is a signed int32 but not a Smi.
6123 // We test for the special value that has a different exponent. This test
6124 // has the neat side effect of setting the flags according to the sign.
6125 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006126 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006127 __ b(eq, &max_negative_int);
6128 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
6129 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
6130 uint32_t non_smi_exponent =
6131 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6132 __ mov(scratch_, Operand(non_smi_exponent));
6133 // Set the sign bit in scratch_ if the value was negative.
6134 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
6135 // Subtract from 0 if the value was negative.
6136 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
6137 // We should be masking the implict first digit of the mantissa away here,
6138 // but it just ends up combining harmlessly with the last digit of the
6139 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
6140 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
6141 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
6142 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6143 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
6144 __ str(scratch_, FieldMemOperand(the_heap_number_,
6145 HeapNumber::kExponentOffset));
6146 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
6147 __ str(scratch_, FieldMemOperand(the_heap_number_,
6148 HeapNumber::kMantissaOffset));
6149 __ Ret();
6150
6151 __ bind(&max_negative_int);
6152 // The max negative int32 is stored as a positive number in the mantissa of
6153 // a double because it uses a sign bit instead of using two's complement.
6154 // The actual mantissa bits stored are all 0 because the implicit most
6155 // significant 1 bit is not stored.
6156 non_smi_exponent += 1 << HeapNumber::kExponentShift;
6157 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
6158 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
6159 __ mov(ip, Operand(0));
6160 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
6161 __ Ret();
6162}
6163
6164
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006165// Handle the case where the lhs and rhs are the same object.
6166// Equality is almost reflexive (everything but NaN), so this is a test
6167// for "identity and not NaN".
6168static void EmitIdenticalObjectComparison(MacroAssembler* masm,
6169 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006170 Condition cc,
6171 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006172 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006173 Label heap_number, return_equal;
6174 Register exp_mask_reg = r5;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006175 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006176 __ b(ne, &not_identical);
6177
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006178 // The two objects are identical. If we know that one of them isn't NaN then
6179 // we now know they test equal.
6180 if (cc != eq || !never_nan_nan) {
6181 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006182
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006183 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
6184 // so we do the second best thing - test it ourselves.
6185 // They are both equal and they are not both Smis so both of them are not
6186 // Smis. If it's not a heap number, then return equal.
6187 if (cc == lt || cc == gt) {
6188 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006189 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006190 } else {
6191 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6192 __ b(eq, &heap_number);
6193 // Comparing JS objects with <=, >= is complicated.
6194 if (cc != eq) {
6195 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
6196 __ b(ge, slow);
6197 // Normally here we fall through to return_equal, but undefined is
6198 // special: (undefined == undefined) == true, but
6199 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
6200 if (cc == le || cc == ge) {
6201 __ cmp(r4, Operand(ODDBALL_TYPE));
6202 __ b(ne, &return_equal);
6203 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006204 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006205 __ b(ne, &return_equal);
6206 if (cc == le) {
6207 // undefined <= undefined should fail.
6208 __ mov(r0, Operand(GREATER));
6209 } else {
6210 // undefined >= undefined should fail.
6211 __ mov(r0, Operand(LESS));
6212 }
6213 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006214 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006215 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006216 }
6217 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006218
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006219 __ bind(&return_equal);
6220 if (cc == lt) {
6221 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
6222 } else if (cc == gt) {
6223 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
6224 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006225 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006226 }
6227 __ mov(pc, Operand(lr)); // Return.
6228
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006229 if (cc != eq || !never_nan_nan) {
6230 // For less and greater we don't have to check for NaN since the result of
6231 // x < x is false regardless. For the others here is some code to check
6232 // for NaN.
6233 if (cc != lt && cc != gt) {
6234 __ bind(&heap_number);
6235 // It is a heap number, so return non-equal if it's NaN and equal if it's
6236 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006237
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006238 // The representation of NaN values has all exponent bits (52..62) set,
6239 // and not all mantissa bits (0..51) clear.
6240 // Read top bits of double representation (second word of value).
6241 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6242 // Test that exponent bits are all set.
6243 __ and_(r3, r2, Operand(exp_mask_reg));
6244 __ cmp(r3, Operand(exp_mask_reg));
6245 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006246
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006247 // Shift out flag and all exponent bits, retaining only mantissa.
6248 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
6249 // Or with all low-bits of mantissa.
6250 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6251 __ orr(r0, r3, Operand(r2), SetCC);
6252 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006253 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
6254 // not (it's a NaN). For <= and >= we need to load r0 with the failing
6255 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006256 if (cc != eq) {
6257 // All-zero means Infinity means equal.
6258 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
6259 if (cc == le) {
6260 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
6261 } else {
6262 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
6263 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006264 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006265 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006266 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006267 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006268 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006269
6270 __ bind(&not_identical);
6271}
6272
6273
6274// See comment at call site.
6275static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006276 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006277 Label* slow,
6278 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006279 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006280 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006281 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006282
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006283 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006284 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6285 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006286 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006287 // succeed. Return non-equal (r0 is already not zero)
6288 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6289 } else {
6290 // Smi compared non-strictly with a non-Smi non-heap-number. Call
6291 // the runtime.
6292 __ b(ne, slow);
6293 }
6294
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006295 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006296 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006297 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006298 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006299 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6300 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006301 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006302 // Load the double from rhs, tagged HeapNumber r0, to d6.
6303 __ sub(r7, r0, Operand(kHeapObjectTag));
6304 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006305 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006306 __ push(lr);
6307 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006308 __ mov(r7, Operand(r1));
6309 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6310 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006311 // Load rhs to a double in r0, r1.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006312 __ ldrd(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006313 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006314 }
6315
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006316 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006317 // since it's a smi.
6318 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006319
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006320 __ bind(&rhs_is_smi);
6321 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006322 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6323 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006324 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006325 // succeed. Return non-equal.
6326 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
6327 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6328 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006329 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006330 // the runtime.
6331 __ b(ne, slow);
6332 }
6333
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006334 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006335 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006336 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006337 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006338 // Load the double from lhs, tagged HeapNumber r1, to d7.
6339 __ sub(r7, r1, Operand(kHeapObjectTag));
6340 __ vldr(d7, r7, HeapNumber::kValueOffset);
6341 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6342 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006343 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006344 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006345 __ push(lr);
6346 // Load lhs to a double in r2, r3.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006347 __ ldrd(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006348 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006349 __ mov(r7, Operand(r0));
6350 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6351 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006352 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006353 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006354 // Fall through to both_loaded_as_doubles.
6355}
6356
6357
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006358void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006359 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006360 Register rhs_exponent = exp_first ? r0 : r1;
6361 Register lhs_exponent = exp_first ? r2 : r3;
6362 Register rhs_mantissa = exp_first ? r1 : r0;
6363 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006364 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006365 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006366
6367 Register exp_mask_reg = r5;
6368
6369 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006370 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
6371 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006372 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006373 __ mov(r4,
6374 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6375 SetCC);
6376 __ b(ne, &one_is_nan);
6377 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006378 __ b(ne, &one_is_nan);
6379
6380 __ bind(lhs_not_nan);
6381 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
6382 __ bind(&lhs_not_nan_exp_mask_is_loaded);
6383 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
6384 __ cmp(r4, Operand(exp_mask_reg));
6385 __ b(ne, &neither_is_nan);
6386 __ mov(r4,
6387 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6388 SetCC);
6389 __ b(ne, &one_is_nan);
6390 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006391 __ b(eq, &neither_is_nan);
6392
6393 __ bind(&one_is_nan);
6394 // NaN comparisons always fail.
6395 // Load whatever we need in r0 to make the comparison fail.
6396 if (cc == lt || cc == le) {
6397 __ mov(r0, Operand(GREATER));
6398 } else {
6399 __ mov(r0, Operand(LESS));
6400 }
6401 __ mov(pc, Operand(lr)); // Return.
6402
6403 __ bind(&neither_is_nan);
6404}
6405
6406
6407// See comment at call site.
6408static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
6409 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006410 Register rhs_exponent = exp_first ? r0 : r1;
6411 Register lhs_exponent = exp_first ? r2 : r3;
6412 Register rhs_mantissa = exp_first ? r1 : r0;
6413 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006414
6415 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
6416 if (cc == eq) {
6417 // Doubles are not equal unless they have the same bit pattern.
6418 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006419 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
6420 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006421 // Return non-zero if the numbers are unequal.
6422 __ mov(pc, Operand(lr), LeaveCC, ne);
6423
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006424 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006425 // If exponents are equal then return 0.
6426 __ mov(pc, Operand(lr), LeaveCC, eq);
6427
6428 // Exponents are unequal. The only way we can return that the numbers
6429 // are equal is if one is -0 and the other is 0. We already dealt
6430 // with the case where both are -0 or both are 0.
6431 // We start by seeing if the mantissas (that are equal) or the bottom
6432 // 31 bits of the rhs exponent are non-zero. If so we return not
6433 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006434 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006435 __ mov(r0, Operand(r4), LeaveCC, ne);
6436 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
6437 // Now they are equal if and only if the lhs exponent is zero in its
6438 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006439 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006440 __ mov(pc, Operand(lr));
6441 } else {
6442 // Call a native function to do a comparison between two non-NaNs.
6443 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006444 __ push(lr);
6445 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
6446 __ CallCFunction(ExternalReference::compare_doubles(), 4);
6447 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006448 }
6449}
6450
6451
6452// See comment at call site.
6453static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
6454 // If either operand is a JSObject or an oddball value, then they are
6455 // not equal since their pointers are different.
6456 // There is no test for undetectability in strict equality.
6457 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
6458 Label first_non_object;
6459 // Get the type of the first operand into r2 and compare it with
6460 // FIRST_JS_OBJECT_TYPE.
6461 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
6462 __ b(lt, &first_non_object);
6463
6464 // Return non-zero (r0 is not zero)
6465 Label return_not_equal;
6466 __ bind(&return_not_equal);
6467 __ mov(pc, Operand(lr)); // Return.
6468
6469 __ bind(&first_non_object);
6470 // Check for oddballs: true, false, null, undefined.
6471 __ cmp(r2, Operand(ODDBALL_TYPE));
6472 __ b(eq, &return_not_equal);
6473
6474 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
6475 __ b(ge, &return_not_equal);
6476
6477 // Check for oddballs: true, false, null, undefined.
6478 __ cmp(r3, Operand(ODDBALL_TYPE));
6479 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006480
6481 // Now that we have the types we might as well check for symbol-symbol.
6482 // Ensure that no non-strings have the symbol bit set.
6483 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6484 ASSERT(kSymbolTag != 0);
6485 __ and_(r2, r2, Operand(r3));
6486 __ tst(r2, Operand(kIsSymbolMask));
6487 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006488}
6489
6490
6491// See comment at call site.
6492static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
6493 Label* both_loaded_as_doubles,
6494 Label* not_heap_numbers,
6495 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006496 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006497 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006498 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
6499 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006500 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
6501
6502 // Both are heap numbers. Load them up then jump to the code we have
6503 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006504 if (CpuFeatures::IsSupported(VFP3)) {
6505 CpuFeatures::Scope scope(VFP3);
6506 __ sub(r7, r0, Operand(kHeapObjectTag));
6507 __ vldr(d6, r7, HeapNumber::kValueOffset);
6508 __ sub(r7, r1, Operand(kHeapObjectTag));
6509 __ vldr(d7, r7, HeapNumber::kValueOffset);
6510 } else {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006511 __ ldrd(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
6512 __ ldrd(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006513 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006514 __ jmp(both_loaded_as_doubles);
6515}
6516
6517
6518// Fast negative check for symbol-to-symbol equality.
6519static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
6520 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006521 // Ensure that no non-strings have the symbol bit set.
6522 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
6523 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006524 __ tst(r2, Operand(kIsSymbolMask));
6525 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006526 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
6527 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006528 __ tst(r3, Operand(kIsSymbolMask));
6529 __ b(eq, slow);
6530
6531 // Both are symbols. We already checked they weren't the same pointer
6532 // so they are not equal.
6533 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
6534 __ mov(pc, Operand(lr)); // Return.
6535}
6536
6537
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006538void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
6539 Register object,
6540 Register result,
6541 Register scratch1,
6542 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006543 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006544 bool object_is_smi,
6545 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006546 // Use of registers. Register result is used as a temporary.
6547 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006548 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006549
6550 // Load the number string cache.
6551 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
6552
6553 // Make the hash mask from the length of the number string cache. It
6554 // contains two elements (number and string) for each cache entry.
6555 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
6556 // Divide length by two (length is not a smi).
6557 __ mov(mask, Operand(mask, ASR, 1));
6558 __ sub(mask, mask, Operand(1)); // Make mask.
6559
6560 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006561 // number string cache for smis is just the smi value, and the hash for
6562 // doubles is the xor of the upper and lower words. See
6563 // Heap::GetNumberStringCache.
6564 Label is_smi;
6565 Label load_result_from_cache;
6566 if (!object_is_smi) {
6567 __ BranchOnSmi(object, &is_smi);
6568 if (CpuFeatures::IsSupported(VFP3)) {
6569 CpuFeatures::Scope scope(VFP3);
6570 __ CheckMap(object,
6571 scratch1,
6572 Factory::heap_number_map(),
6573 not_found,
6574 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006575
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006576 ASSERT_EQ(8, kDoubleSize);
6577 __ add(scratch1,
6578 object,
6579 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
6580 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
6581 __ eor(scratch1, scratch1, Operand(scratch2));
6582 __ and_(scratch1, scratch1, Operand(mask));
6583
6584 // Calculate address of entry in string cache: each entry consists
6585 // of two pointer sized fields.
6586 __ add(scratch1,
6587 number_string_cache,
6588 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
6589
6590 Register probe = mask;
6591 __ ldr(probe,
6592 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
6593 __ BranchOnSmi(probe, not_found);
6594 __ sub(scratch2, object, Operand(kHeapObjectTag));
6595 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
6596 __ sub(probe, probe, Operand(kHeapObjectTag));
6597 __ vldr(d1, probe, HeapNumber::kValueOffset);
6598 __ vcmp(d0, d1);
6599 __ vmrs(pc);
6600 __ b(ne, not_found); // The cache did not contain this value.
6601 __ b(&load_result_from_cache);
6602 } else {
6603 __ b(not_found);
6604 }
6605 }
6606
6607 __ bind(&is_smi);
6608 Register scratch = scratch1;
6609 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006610 // Calculate address of entry in string cache: each entry consists
6611 // of two pointer sized fields.
6612 __ add(scratch,
6613 number_string_cache,
6614 Operand(scratch, LSL, kPointerSizeLog2 + 1));
6615
6616 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006617 Register probe = mask;
6618 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
6619 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006620 __ b(ne, not_found);
6621
6622 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006623 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006624 __ ldr(result,
6625 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006626 __ IncrementCounter(&Counters::number_to_string_native,
6627 1,
6628 scratch1,
6629 scratch2);
6630}
6631
6632
6633void NumberToStringStub::Generate(MacroAssembler* masm) {
6634 Label runtime;
6635
6636 __ ldr(r1, MemOperand(sp, 0));
6637
6638 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006639 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006640 __ add(sp, sp, Operand(1 * kPointerSize));
6641 __ Ret();
6642
6643 __ bind(&runtime);
6644 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006645 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006646}
6647
6648
ager@chromium.orgac091b72010-05-05 07:34:42 +00006649void RecordWriteStub::Generate(MacroAssembler* masm) {
6650 __ RecordWriteHelper(object_, offset_, scratch_);
6651 __ Ret();
6652}
6653
6654
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006655// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
6656// On exit r0 is 0, positive or negative to indicate the result of
6657// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006658void CompareStub::Generate(MacroAssembler* masm) {
6659 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006660 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006661
6662 // NOTICE! This code is only reached after a smi-fast-case check, so
6663 // it is certain that at least one operand isn't a smi.
6664
6665 // Handle the case where the objects are identical. Either returns the answer
6666 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006667 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006668
6669 // If either is a Smi (we know that not both are), then they can only
6670 // be strictly equal if the other is a HeapNumber.
6671 ASSERT_EQ(0, kSmiTag);
6672 ASSERT_EQ(0, Smi::FromInt(0));
6673 __ and_(r2, r0, Operand(r1));
6674 __ tst(r2, Operand(kSmiTagMask));
6675 __ b(ne, &not_smis);
6676 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
6677 // 1) Return the answer.
6678 // 2) Go to slow.
6679 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006680 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006681 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006682 // comparison. If VFP3 is supported the double values of the numbers have
6683 // been loaded into d7 and d6. Otherwise, the double values have been loaded
6684 // into r0, r1, r2, and r3.
6685 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006686
6687 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006688 // The arguments have been converted to doubles and stored in d6 and d7, if
6689 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006690 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006691 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006692 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006693 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006694 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006695 __ vcmp(d7, d6);
6696 __ vmrs(pc); // Move vector status bits to normal status bits.
6697 Label nan;
6698 __ b(vs, &nan);
6699 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6700 __ mov(r0, Operand(LESS), LeaveCC, lt);
6701 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6702 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006703
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006704 __ bind(&nan);
6705 // If one of the sides was a NaN then the v flag is set. Load r0 with
6706 // whatever it takes to make the comparison fail, since comparisons with NaN
6707 // always fail.
6708 if (cc_ == lt || cc_ == le) {
6709 __ mov(r0, Operand(GREATER));
6710 } else {
6711 __ mov(r0, Operand(LESS));
6712 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006713 __ mov(pc, Operand(lr));
6714 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006715 // Checks for NaN in the doubles we have loaded. Can return the answer or
6716 // fall through if neither is a NaN. Also binds lhs_not_nan.
6717 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006718 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
6719 // answer. Never falls through.
6720 EmitTwoNonNanDoubleComparison(masm, cc_);
6721 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006722
6723 __ bind(&not_smis);
6724 // At this point we know we are dealing with two different objects,
6725 // and neither of them is a Smi. The objects are in r0 and r1.
6726 if (strict_) {
6727 // This returns non-equal for some object types, or falls through if it
6728 // was not lucky.
6729 EmitStrictTwoHeapObjectCompare(masm);
6730 }
6731
6732 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006733 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006734 // Check for heap-number-heap-number comparison. Can jump to slow case,
6735 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
6736 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006737 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006738 EmitCheckForTwoHeapNumbers(masm,
6739 &both_loaded_as_doubles,
6740 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006741 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006742
6743 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006744 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
6745 // symbols.
6746 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006747 // Either jumps to slow or returns the answer. Assumes that r2 is the type
6748 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006749 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006750 }
6751
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006752 // Check for both being sequential ASCII strings, and inline if that is the
6753 // case.
6754 __ bind(&flat_string_check);
6755
6756 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
6757
6758 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
6759 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
6760 r1,
6761 r0,
6762 r2,
6763 r3,
6764 r4,
6765 r5);
6766 // Never falls through to here.
6767
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006768 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006769
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006770 __ Push(r1, r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006771 // Figure out which native to call and setup the arguments.
6772 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006773 if (cc_ == eq) {
6774 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
6775 } else {
6776 native = Builtins::COMPARE;
6777 int ncr; // NaN compare result
6778 if (cc_ == lt || cc_ == le) {
6779 ncr = GREATER;
6780 } else {
6781 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
6782 ncr = LESS;
6783 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006784 __ mov(r0, Operand(Smi::FromInt(ncr)));
6785 __ push(r0);
6786 }
6787
6788 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
6789 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006790 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006791}
6792
6793
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00006794// We fall into this code if the operands were Smis, but the result was
6795// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006796// the operands were not both Smi. The operands are in r0 and r1. In order
6797// to call the C-implemented binary fp operation routines we need to end up
6798// with the double precision floating point operands in r0 and r1 (for the
6799// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00006800void GenericBinaryOpStub::HandleBinaryOpSlowCases(
6801 MacroAssembler* masm,
6802 Label* not_smi,
6803 Register lhs,
6804 Register rhs,
6805 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006806 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00006807 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006808
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00006809 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ager@chromium.org357bf652010-04-12 11:30:10 +00006810
6811 if (ShouldGenerateSmiCode()) {
6812 // Smi-smi case (overflow).
6813 // Since both are Smis there is no heap number to overwrite, so allocate.
6814 // The new heap number is in r5. r6 and r7 are scratch.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006815 __ AllocateHeapNumber(r5, r6, r7, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006816
6817 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
6818 // using registers d7 and d6 for the double values.
6819 if (use_fp_registers) {
6820 CpuFeatures::Scope scope(VFP3);
6821 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
6822 __ vmov(s15, r7);
6823 __ vcvt_f64_s32(d7, s15);
6824 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
6825 __ vmov(s13, r7);
6826 __ vcvt_f64_s32(d6, s13);
6827 } else {
6828 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
6829 __ mov(r7, Operand(rhs));
6830 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6831 __ push(lr);
6832 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
6833 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
6834 __ mov(r7, Operand(lhs));
6835 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6836 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
6837 __ pop(lr);
6838 }
6839 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006840 }
6841
ager@chromium.org357bf652010-04-12 11:30:10 +00006842 // We branch here if at least one of r0 and r1 is not a Smi.
6843 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006844
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006845 // After this point we have the left hand side in r1 and the right hand side
6846 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006847 if (lhs.is(r0)) {
6848 __ Swap(r0, r1, ip);
6849 }
6850
6851 if (ShouldGenerateFPCode()) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006852 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
6853
ager@chromium.org357bf652010-04-12 11:30:10 +00006854 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
6855 switch (op_) {
6856 case Token::ADD:
6857 case Token::SUB:
6858 case Token::MUL:
6859 case Token::DIV:
6860 GenerateTypeTransition(masm);
6861 break;
6862
6863 default:
6864 break;
6865 }
6866 }
6867
6868 if (mode_ == NO_OVERWRITE) {
6869 // In the case where there is no chance of an overwritable float we may as
6870 // well do the allocation immediately while r0 and r1 are untouched.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006871 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006872 }
6873
6874 // Move r0 to a double in r2-r3.
6875 __ tst(r0, Operand(kSmiTagMask));
6876 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
6877 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6878 __ b(ne, &slow);
6879 if (mode_ == OVERWRITE_RIGHT) {
6880 __ mov(r5, Operand(r0)); // Overwrite this heap number.
6881 }
6882 if (use_fp_registers) {
6883 CpuFeatures::Scope scope(VFP3);
6884 // Load the double from tagged HeapNumber r0 to d7.
6885 __ sub(r7, r0, Operand(kHeapObjectTag));
6886 __ vldr(d7, r7, HeapNumber::kValueOffset);
6887 } else {
6888 // Calling convention says that second double is in r2 and r3.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006889 __ ldrd(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00006890 }
6891 __ jmp(&finished_loading_r0);
6892 __ bind(&r0_is_smi);
6893 if (mode_ == OVERWRITE_RIGHT) {
6894 // We can't overwrite a Smi so get address of new heap number into r5.
6895 __ AllocateHeapNumber(r5, r6, r7, &slow);
6896 }
6897
6898 if (use_fp_registers) {
6899 CpuFeatures::Scope scope(VFP3);
6900 // Convert smi in r0 to double in d7.
6901 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6902 __ vmov(s15, r7);
6903 __ vcvt_f64_s32(d7, s15);
6904 } else {
6905 // Write Smi from r0 to r3 and r2 in double format.
6906 __ mov(r7, Operand(r0));
6907 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6908 __ push(lr);
6909 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6910 __ pop(lr);
6911 }
6912
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006913 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
6914 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
6915 Label r1_is_not_smi;
6916 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
6917 __ tst(r1, Operand(kSmiTagMask));
6918 __ b(ne, &r1_is_not_smi);
6919 GenerateTypeTransition(masm);
6920 __ jmp(&r1_is_smi);
6921 }
6922
ager@chromium.org357bf652010-04-12 11:30:10 +00006923 __ bind(&finished_loading_r0);
6924
6925 // Move r1 to a double in r0-r1.
6926 __ tst(r1, Operand(kSmiTagMask));
6927 __ 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 +00006928 __ bind(&r1_is_not_smi);
ager@chromium.org357bf652010-04-12 11:30:10 +00006929 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6930 __ b(ne, &slow);
6931 if (mode_ == OVERWRITE_LEFT) {
6932 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6933 }
6934 if (use_fp_registers) {
6935 CpuFeatures::Scope scope(VFP3);
6936 // Load the double from tagged HeapNumber r1 to d6.
6937 __ sub(r7, r1, Operand(kHeapObjectTag));
6938 __ vldr(d6, r7, HeapNumber::kValueOffset);
6939 } else {
6940 // Calling convention says that first double is in r0 and r1.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006941 __ ldrd(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00006942 }
6943 __ jmp(&finished_loading_r1);
6944 __ bind(&r1_is_smi);
6945 if (mode_ == OVERWRITE_LEFT) {
6946 // We can't overwrite a Smi so get address of new heap number into r5.
6947 __ AllocateHeapNumber(r5, r6, r7, &slow);
6948 }
6949
6950 if (use_fp_registers) {
6951 CpuFeatures::Scope scope(VFP3);
6952 // Convert smi in r1 to double in d6.
6953 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6954 __ vmov(s13, r7);
6955 __ vcvt_f64_s32(d6, s13);
6956 } else {
6957 // Write Smi from r1 to r1 and r0 in double format.
6958 __ mov(r7, Operand(r1));
6959 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6960 __ push(lr);
6961 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6962 __ pop(lr);
6963 }
6964
6965 __ bind(&finished_loading_r1);
6966
6967 __ bind(&do_the_call);
6968 // If we are inlining the operation using VFP3 instructions for
6969 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6970 if (use_fp_registers) {
6971 CpuFeatures::Scope scope(VFP3);
6972 // ARMv7 VFP3 instructions to implement
6973 // double precision, add, subtract, multiply, divide.
6974
6975 if (Token::MUL == op_) {
6976 __ vmul(d5, d6, d7);
6977 } else if (Token::DIV == op_) {
6978 __ vdiv(d5, d6, d7);
6979 } else if (Token::ADD == op_) {
6980 __ vadd(d5, d6, d7);
6981 } else if (Token::SUB == op_) {
6982 __ vsub(d5, d6, d7);
6983 } else {
6984 UNREACHABLE();
6985 }
6986 __ sub(r0, r5, Operand(kHeapObjectTag));
6987 __ vstr(d5, r0, HeapNumber::kValueOffset);
6988 __ add(r0, r0, Operand(kHeapObjectTag));
6989 __ mov(pc, lr);
6990 } else {
6991 // If we did not inline the operation, then the arguments are in:
6992 // r0: Left value (least significant part of mantissa).
6993 // r1: Left value (sign, exponent, top of mantissa).
6994 // r2: Right value (least significant part of mantissa).
6995 // r3: Right value (sign, exponent, top of mantissa).
6996 // r5: Address of heap number for result.
6997
6998 __ push(lr); // For later.
6999 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
7000 // Call C routine that may not cause GC or other trouble. r5 is callee
7001 // save.
7002 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
7003 // Store answer in the overwritable heap number.
7004 #if !defined(USE_ARM_EABI)
7005 // Double returned in fp coprocessor register 0 and 1, encoded as register
7006 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
7007 // substract the tag from r5.
7008 __ sub(r4, r5, Operand(kHeapObjectTag));
7009 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
7010 #else
7011 // Double returned in registers 0 and 1.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007012 __ strd(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007013 #endif
7014 __ mov(r0, Operand(r5));
7015 // And we are done.
7016 __ pop(pc);
7017 }
7018 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007019
7020
7021 if (lhs.is(r0)) {
7022 __ b(&slow);
7023 __ bind(&slow_reverse);
7024 __ Swap(r0, r1, ip);
7025 }
7026
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007027 // We jump to here if something goes wrong (one param is not a number of any
7028 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007029 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007030
7031 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007032 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007033
ager@chromium.org357bf652010-04-12 11:30:10 +00007034 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007035 // Test for string arguments before calling runtime.
7036 // r1 : first argument
7037 // r0 : second argument
7038 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00007039 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007040
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007041 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007042 __ tst(r1, Operand(kSmiTagMask));
7043 __ b(eq, &not_string1);
7044 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
7045 __ b(ge, &not_string1);
7046
7047 // First argument is a a string, test second.
7048 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007049 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007050 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
7051 __ b(ge, &string1);
7052
7053 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007054 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
7055 __ TailCallStub(&string_add_stub);
7056
7057 __ bind(&string1_smi2);
7058 // First argument is a string, second is a smi. Try to lookup the number
7059 // string for the smi in the number string cache.
7060 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007061 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007062
7063 // Replace second argument on stack and tailcall string add stub to make
7064 // the result.
7065 __ str(r2, MemOperand(sp, 0));
7066 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007067
7068 // Only first argument is a string.
7069 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007070 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
7071
7072 // First argument was not a string, test second.
7073 __ bind(&not_string1);
7074 __ tst(r0, Operand(kSmiTagMask));
7075 __ b(eq, &not_strings);
7076 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
7077 __ b(ge, &not_strings);
7078
7079 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007080 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
7081
7082 __ bind(&not_strings);
7083 }
7084
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007085 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007086}
7087
7088
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007089// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007090// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007091// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
7092// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007093// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
7094// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007095static void GetInt32(MacroAssembler* masm,
7096 Register source,
7097 Register dest,
7098 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007099 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007100 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007101 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007102 // Get exponent word.
7103 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
7104 // Get exponent alone in scratch2.
7105 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007106 // Load dest with zero. We use this either for the final shift or
7107 // for the answer.
7108 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007109 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007110 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
7111 // the exponent that we are fastest at and also the highest exponent we can
7112 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007113 const uint32_t non_smi_exponent =
7114 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7115 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007116 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
7117 __ b(eq, &right_exponent);
7118 // If the exponent is higher than that then go to slow case. This catches
7119 // numbers that don't fit in a signed int32, infinities and NaNs.
7120 __ b(gt, slow);
7121
7122 // We know the exponent is smaller than 30 (biased). If it is less than
7123 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
7124 // it rounds to zero.
7125 const uint32_t zero_exponent =
7126 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
7127 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
7128 // Dest already has a Smi zero.
7129 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007130 if (!CpuFeatures::IsSupported(VFP3)) {
7131 // We have a shifted exponent between 0 and 30 in scratch2.
7132 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
7133 // We now have the exponent in dest. Subtract from 30 to get
7134 // how much to shift down.
7135 __ rsb(dest, dest, Operand(30));
7136 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007137 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007138 if (CpuFeatures::IsSupported(VFP3)) {
7139 CpuFeatures::Scope scope(VFP3);
7140 // ARMv7 VFP3 instructions implementing double precision to integer
7141 // conversion using round to zero.
7142 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007143 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007144 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007145 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007146 } else {
7147 // Get the top bits of the mantissa.
7148 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
7149 // Put back the implicit 1.
7150 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
7151 // Shift up the mantissa bits to take up the space the exponent used to
7152 // take. We just orred in the implicit bit so that took care of one and
7153 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
7154 // distance.
7155 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7156 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
7157 // Put sign in zero flag.
7158 __ tst(scratch, Operand(HeapNumber::kSignMask));
7159 // Get the second half of the double. For some exponents we don't
7160 // actually need this because the bits get shifted out again, but
7161 // it's probably slower to test than just to do it.
7162 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
7163 // Shift down 22 bits to get the last 10 bits.
7164 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
7165 // Move down according to the exponent.
7166 __ mov(dest, Operand(scratch, LSR, dest));
7167 // Fix sign if sign bit was set.
7168 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
7169 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007170 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007171}
7172
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007173// For bitwise ops where the inputs are not both Smis we here try to determine
7174// whether both inputs are either Smis or at least heap numbers that can be
7175// represented by a 32 bit signed value. We truncate towards zero as required
7176// by the ES spec. If this is the case we do the bitwise op and see if the
7177// result is a Smi. If so, great, otherwise we try to find a heap number to
7178// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00007179// On entry the operands are in lhs and rhs. On exit the answer is in r0.
7180void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
7181 Register lhs,
7182 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007183 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007184 Label rhs_is_smi, lhs_is_smi;
7185 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007186
ager@chromium.org357bf652010-04-12 11:30:10 +00007187 __ tst(lhs, Operand(kSmiTagMask));
7188 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
7189 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007190 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007191 GetInt32(masm, lhs, r3, r5, r4, &slow);
7192 __ jmp(&done_checking_lhs);
7193 __ bind(&lhs_is_smi);
7194 __ mov(r3, Operand(lhs, ASR, 1));
7195 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007196
ager@chromium.org357bf652010-04-12 11:30:10 +00007197 __ tst(rhs, Operand(kSmiTagMask));
7198 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
7199 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007200 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007201 GetInt32(masm, rhs, r2, r5, r4, &slow);
7202 __ jmp(&done_checking_rhs);
7203 __ bind(&rhs_is_smi);
7204 __ mov(r2, Operand(rhs, ASR, 1));
7205 __ bind(&done_checking_rhs);
7206
7207 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007208
7209 // r0 and r1: Original operands (Smi or heap numbers).
7210 // r2 and r3: Signed int32 operands.
7211 switch (op_) {
7212 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
7213 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
7214 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
7215 case Token::SAR:
7216 // Use only the 5 least significant bits of the shift count.
7217 __ and_(r2, r2, Operand(0x1f));
7218 __ mov(r2, Operand(r3, ASR, r2));
7219 break;
7220 case Token::SHR:
7221 // Use only the 5 least significant bits of the shift count.
7222 __ and_(r2, r2, Operand(0x1f));
7223 __ mov(r2, Operand(r3, LSR, r2), SetCC);
7224 // SHR is special because it is required to produce a positive answer.
7225 // The code below for writing into heap numbers isn't capable of writing
7226 // the register as an unsigned int so we go to slow case if we hit this
7227 // case.
7228 __ b(mi, &slow);
7229 break;
7230 case Token::SHL:
7231 // Use only the 5 least significant bits of the shift count.
7232 __ and_(r2, r2, Operand(0x1f));
7233 __ mov(r2, Operand(r3, LSL, r2));
7234 break;
7235 default: UNREACHABLE();
7236 }
7237 // check that the *signed* result fits in a smi
7238 __ add(r3, r2, Operand(0x40000000), SetCC);
7239 __ b(mi, &result_not_a_smi);
7240 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
7241 __ Ret();
7242
7243 Label have_to_allocate, got_a_heap_number;
7244 __ bind(&result_not_a_smi);
7245 switch (mode_) {
7246 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007247 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007248 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007249 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007250 break;
7251 }
7252 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007253 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007254 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007255 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007256 break;
7257 }
7258 case NO_OVERWRITE: {
7259 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007260 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007261 }
7262 default: break;
7263 }
7264 __ bind(&got_a_heap_number);
7265 // r2: Answer as signed int32.
7266 // r5: Heap number to write answer into.
7267
7268 // Nothing can go wrong now, so move the heap number to r0, which is the
7269 // result.
7270 __ mov(r0, Operand(r5));
7271
7272 // Tail call that writes the int32 in r2 to the heap number in r0, using
7273 // r3 as scratch. r0 is preserved and returned.
7274 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
7275 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
7276
7277 if (mode_ != NO_OVERWRITE) {
7278 __ bind(&have_to_allocate);
7279 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007280 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007281 __ jmp(&got_a_heap_number);
7282 }
7283
7284 // If all else failed then we go to the runtime system.
7285 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007286 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007287 switch (op_) {
7288 case Token::BIT_OR:
7289 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
7290 break;
7291 case Token::BIT_AND:
7292 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
7293 break;
7294 case Token::BIT_XOR:
7295 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
7296 break;
7297 case Token::SAR:
7298 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
7299 break;
7300 case Token::SHR:
7301 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
7302 break;
7303 case Token::SHL:
7304 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
7305 break;
7306 default:
7307 UNREACHABLE();
7308 }
7309}
7310
7311
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007312// Can we multiply by x with max two shifts and an add.
7313// This answers yes to all integers from 2 to 10.
7314static bool IsEasyToMultiplyBy(int x) {
7315 if (x < 2) return false; // Avoid special cases.
7316 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
7317 if (IsPowerOf2(x)) return true; // Simple shift.
7318 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
7319 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
7320 return false;
7321}
7322
7323
7324// Can multiply by anything that IsEasyToMultiplyBy returns true for.
7325// Source and destination may be the same register. This routine does
7326// not set carry and overflow the way a mul instruction would.
7327static void MultiplyByKnownInt(MacroAssembler* masm,
7328 Register source,
7329 Register destination,
7330 int known_int) {
7331 if (IsPowerOf2(known_int)) {
7332 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
7333 } else if (PopCountLessThanEqual2(known_int)) {
7334 int first_bit = BitPosition(known_int);
7335 int second_bit = BitPosition(known_int ^ (1 << first_bit));
7336 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
7337 if (first_bit != 0) {
7338 __ mov(destination, Operand(destination, LSL, first_bit));
7339 }
7340 } else {
7341 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
7342 int the_bit = BitPosition(known_int + 1);
7343 __ rsb(destination, source, Operand(source, LSL, the_bit));
7344 }
7345}
7346
7347
7348// This function (as opposed to MultiplyByKnownInt) takes the known int in a
7349// a register for the cases where it doesn't know a good trick, and may deliver
7350// a result that needs shifting.
7351static void MultiplyByKnownInt2(
7352 MacroAssembler* masm,
7353 Register result,
7354 Register source,
7355 Register known_int_register, // Smi tagged.
7356 int known_int,
7357 int* required_shift) { // Including Smi tag shift
7358 switch (known_int) {
7359 case 3:
7360 __ add(result, source, Operand(source, LSL, 1));
7361 *required_shift = 1;
7362 break;
7363 case 5:
7364 __ add(result, source, Operand(source, LSL, 2));
7365 *required_shift = 1;
7366 break;
7367 case 6:
7368 __ add(result, source, Operand(source, LSL, 1));
7369 *required_shift = 2;
7370 break;
7371 case 7:
7372 __ rsb(result, source, Operand(source, LSL, 3));
7373 *required_shift = 1;
7374 break;
7375 case 9:
7376 __ add(result, source, Operand(source, LSL, 3));
7377 *required_shift = 1;
7378 break;
7379 case 10:
7380 __ add(result, source, Operand(source, LSL, 2));
7381 *required_shift = 2;
7382 break;
7383 default:
7384 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
7385 __ mul(result, source, known_int_register);
7386 *required_shift = 0;
7387 }
7388}
7389
7390
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007391const char* GenericBinaryOpStub::GetName() {
7392 if (name_ != NULL) return name_;
7393 const int len = 100;
7394 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7395 if (name_ == NULL) return "OOM";
7396 const char* op_name = Token::Name(op_);
7397 const char* overwrite_name;
7398 switch (mode_) {
7399 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7400 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7401 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7402 default: overwrite_name = "UnknownOverwrite"; break;
7403 }
7404
7405 OS::SNPrintF(Vector<char>(name_, len),
7406 "GenericBinaryOpStub_%s_%s%s",
7407 op_name,
7408 overwrite_name,
7409 specialized_on_rhs_ ? "_ConstantRhs" : 0);
7410 return name_;
7411}
7412
7413
ager@chromium.org5c838252010-02-19 08:53:10 +00007414
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007415void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007416 // lhs_ : x
7417 // rhs_ : y
7418 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007419
ager@chromium.org357bf652010-04-12 11:30:10 +00007420 Register result = r0;
7421 Register lhs = lhs_;
7422 Register rhs = rhs_;
7423
7424 // This code can't cope with other register allocations yet.
7425 ASSERT(result.is(r0) &&
7426 ((lhs.is(r0) && rhs.is(r1)) ||
7427 (lhs.is(r1) && rhs.is(r0))));
7428
7429 Register smi_test_reg = VirtualFrame::scratch0();
7430 Register scratch = VirtualFrame::scratch1();
7431
7432 // All ops need to know whether we are dealing with two Smis. Set up
7433 // smi_test_reg to tell us that.
7434 if (ShouldGenerateSmiCode()) {
7435 __ orr(smi_test_reg, lhs, Operand(rhs));
7436 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007437
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007438 switch (op_) {
7439 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007440 Label not_smi;
7441 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007442 if (ShouldGenerateSmiCode()) {
7443 ASSERT(kSmiTag == 0); // Adjust code below.
7444 __ tst(smi_test_reg, Operand(kSmiTagMask));
7445 __ b(ne, &not_smi);
7446 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
7447 // Return if no overflow.
7448 __ Ret(vc);
7449 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
7450 }
7451 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007452 break;
7453 }
7454
7455 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007456 Label not_smi;
7457 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00007458 if (ShouldGenerateSmiCode()) {
7459 ASSERT(kSmiTag == 0); // Adjust code below.
7460 __ tst(smi_test_reg, Operand(kSmiTagMask));
7461 __ b(ne, &not_smi);
7462 if (lhs.is(r1)) {
7463 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
7464 // Return if no overflow.
7465 __ Ret(vc);
7466 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
7467 } else {
7468 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
7469 // Return if no overflow.
7470 __ Ret(vc);
7471 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
7472 }
7473 }
7474 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007475 break;
7476 }
7477
7478 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007479 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00007480 if (ShouldGenerateSmiCode()) {
7481 ASSERT(kSmiTag == 0); // adjust code below
7482 __ tst(smi_test_reg, Operand(kSmiTagMask));
7483 Register scratch2 = smi_test_reg;
7484 smi_test_reg = no_reg;
7485 __ b(ne, &not_smi);
7486 // Remove tag from one operand (but keep sign), so that result is Smi.
7487 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
7488 // Do multiplication
7489 // scratch = lower 32 bits of ip * lhs.
7490 __ smull(scratch, scratch2, lhs, ip);
7491 // Go slow on overflows (overflow bit is not set).
7492 __ mov(ip, Operand(scratch, ASR, 31));
7493 // No overflow if higher 33 bits are identical.
7494 __ cmp(ip, Operand(scratch2));
7495 __ b(ne, &slow);
7496 // Go slow on zero result to handle -0.
7497 __ tst(scratch, Operand(scratch));
7498 __ mov(result, Operand(scratch), LeaveCC, ne);
7499 __ Ret(ne);
7500 // We need -0 if we were multiplying a negative number with 0 to get 0.
7501 // We know one of them was zero.
7502 __ add(scratch2, rhs, Operand(lhs), SetCC);
7503 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
7504 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
7505 // Slow case. We fall through here if we multiplied a negative number
7506 // with 0, because that would mean we should produce -0.
7507 __ bind(&slow);
7508 }
7509 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007510 break;
7511 }
7512
7513 case Token::DIV:
7514 case Token::MOD: {
7515 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007516 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007517 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00007518 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007519 if (IsPowerOf2(constant_rhs_)) {
7520 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007521 __ and_(rhs,
7522 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007523 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
7524 SetCC);
7525 // We now have the answer, but if the input was negative we also
7526 // have the sign bit. Our work is done if the result is
7527 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00007528 if (!rhs.is(r0)) {
7529 __ mov(r0, rhs, LeaveCC, pl);
7530 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007531 __ Ret(pl);
7532 // A mod of a negative left hand side must return a negative number.
7533 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00007534 // already optimistically trashed rhs so we may need to restore it.
7535 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007536 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007537 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007538 __ b(eq, &smi_is_unsuitable);
7539 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00007540 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007541 } else {
7542 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00007543 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007544 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
7545 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
7546 int shift = 0;
7547 int d = constant_rhs_;
7548 while ((d & 1) == 0) {
7549 d >>= 1;
7550 shift++;
7551 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007552 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007553 __ bic(r0, r0, Operand(kSmiTagMask));
7554 }
7555 } else {
7556 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00007557 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007558 __ b(ne, &smi_is_unsuitable);
7559 // Find a fixed point reciprocal of the divisor so we can divide by
7560 // multiplying.
7561 double divisor = 1.0 / constant_rhs_;
7562 int shift = 32;
7563 double scale = 4294967296.0; // 1 << 32.
7564 uint32_t mul;
7565 // Maximise the precision of the fixed point reciprocal.
7566 while (true) {
7567 mul = static_cast<uint32_t>(scale * divisor);
7568 if (mul >= 0x7fffffff) break;
7569 scale *= 2.0;
7570 shift++;
7571 }
7572 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00007573 Register scratch2 = smi_test_reg;
7574 smi_test_reg = no_reg;
7575 __ mov(scratch2, Operand(mul));
7576 __ umull(scratch, scratch2, scratch2, lhs);
7577 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
7578 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
7579 // rhs is still the known rhs. rhs is Smi tagged.
7580 // lhs is still the unkown lhs. lhs is Smi tagged.
7581 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
7582 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007583 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00007584 scratch,
7585 scratch2,
7586 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007587 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00007588 &required_scratch_shift);
7589 // scratch << required_scratch_shift is now the Smi tagged rhs *
7590 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007591 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007592 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007593 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00007594 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007595 } else {
7596 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00007597 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007598 }
7599 }
7600 __ Ret();
7601 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007602 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007603 HandleBinaryOpSlowCases(
7604 masm,
7605 &not_smi,
7606 lhs,
7607 rhs,
7608 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007609 break;
7610 }
7611
7612 case Token::BIT_OR:
7613 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007614 case Token::BIT_XOR:
7615 case Token::SAR:
7616 case Token::SHR:
7617 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007618 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007619 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00007620 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007621 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007622 Register scratch2 = smi_test_reg;
7623 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007624 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007625 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
7626 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
7627 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007628 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007629 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00007630 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7631 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007632 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007633 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007634 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007635 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007636 // Remove tags from operands. We can't do this on a 31 bit number
7637 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00007638 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7639 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7640 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007641 // Unsigned shift is not allowed to produce a negative number, so
7642 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00007643 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007644 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007645 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00007646 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007647 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007648 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007649 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00007650 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
7651 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
7652 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007653 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00007654 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007655 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007656 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007657 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007658 default: UNREACHABLE();
7659 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007660 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007661 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007662 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007663 break;
7664 }
7665
7666 default: UNREACHABLE();
7667 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007668 // This code should be unreachable.
7669 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00007670
7671 // Generate an unreachable reference to the DEFAULT stub so that it can be
7672 // found at the end of this stub when clearing ICs at GC.
7673 // TODO(kaznacheev): Check performance impact and get rid of this.
7674 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
7675 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
7676 __ CallStub(&uninit);
7677 }
7678}
7679
7680
7681void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
7682 Label get_result;
7683
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007684 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00007685
7686 // Internal frame is necessary to handle exceptions properly.
7687 __ EnterInternalFrame();
7688 // Call the stub proper to get the result in r0.
7689 __ Call(&get_result);
7690 __ LeaveInternalFrame();
7691
7692 __ push(r0);
7693
7694 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
7695 __ push(r0);
7696 __ mov(r0, Operand(Smi::FromInt(op_)));
7697 __ push(r0);
7698 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
7699 __ push(r0);
7700
7701 __ TailCallExternalReference(
7702 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
7703 6,
7704 1);
7705
7706 // The entry point for the result calculation is assumed to be immediately
7707 // after this sequence.
7708 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007709}
7710
7711
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007712Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007713 GenericBinaryOpStub stub(key, type_info);
7714 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007715}
7716
7717
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007718void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00007719 // Do tail-call to runtime routine. Runtime routines expect at least one
7720 // argument, so give it a Smi.
7721 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007722 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007723 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007724
7725 __ StubReturn(1);
7726}
7727
7728
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007729void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007730 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007731
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007732 if (op_ == Token::SUB) {
7733 // Check whether the value is a smi.
7734 Label try_float;
7735 __ tst(r0, Operand(kSmiTagMask));
7736 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007737
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007738 // Go slow case if the value of the expression is zero
7739 // to make sure that we switch between 0 and -0.
7740 __ cmp(r0, Operand(0));
7741 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007742
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007743 // The value of the expression is a smi that is not zero. Try
7744 // optimistic subtraction '0 - value'.
7745 __ rsb(r1, r0, Operand(0), SetCC);
7746 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007747
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007748 __ mov(r0, Operand(r1)); // Set r0 to result.
7749 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007750
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007751 __ bind(&try_float);
7752 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7753 __ b(ne, &slow);
7754 // r0 is a heap number. Get a new heap number in r1.
7755 if (overwrite_) {
7756 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7757 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7758 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7759 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007760 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007761 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7762 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7763 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
7764 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
7765 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
7766 __ mov(r0, Operand(r1));
7767 }
7768 } else if (op_ == Token::BIT_NOT) {
7769 // Check if the operand is a heap number.
7770 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
7771 __ b(ne, &slow);
7772
7773 // Convert the heap number is r0 to an untagged integer in r1.
7774 GetInt32(masm, r0, r1, r2, r3, &slow);
7775
7776 // Do the bitwise operation (move negated) and check if the result
7777 // fits in a smi.
7778 Label try_float;
7779 __ mvn(r1, Operand(r1));
7780 __ add(r2, r1, Operand(0x40000000), SetCC);
7781 __ b(mi, &try_float);
7782 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
7783 __ b(&done);
7784
7785 __ bind(&try_float);
7786 if (!overwrite_) {
7787 // Allocate a fresh heap number, but don't overwrite r0 until
7788 // we're sure we can do it without going through the slow case
7789 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007790 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007791 __ mov(r0, Operand(r2));
7792 }
7793
7794 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
7795 // have to set up a frame.
7796 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
7797 __ push(lr);
7798 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
7799 __ pop(lr);
7800 } else {
7801 UNIMPLEMENTED();
7802 }
7803
7804 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007805 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007806
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007807 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007808 __ bind(&slow);
7809 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007810 switch (op_) {
7811 case Token::SUB:
7812 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
7813 break;
7814 case Token::BIT_NOT:
7815 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
7816 break;
7817 default:
7818 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007819 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00007820}
7821
7822
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007823void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007824 // r0 holds the exception.
7825
7826 // Adjust this code if not the case.
7827 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7828
7829 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007830 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
7831 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007832
7833 // Restore the next handler and frame pointer, discard handler state.
7834 ASSERT(StackHandlerConstants::kNextOffset == 0);
7835 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007836 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007837 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7838 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
7839
7840 // Before returning we restore the context from the frame pointer if
7841 // not NULL. The frame pointer is NULL in the exception handler of a
7842 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007843 __ cmp(fp, Operand(0));
7844 // Set cp to NULL if fp is NULL.
7845 __ mov(cp, Operand(0), LeaveCC, eq);
7846 // Restore cp otherwise.
7847 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007848#ifdef DEBUG
7849 if (FLAG_debug_code) {
7850 __ mov(lr, Operand(pc));
7851 }
7852#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007853 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007854 __ pop(pc);
7855}
7856
7857
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007858void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
7859 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007860 // Adjust this code if not the case.
7861 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7862
7863 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007864 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007865 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007866
7867 // Unwind the handlers until the ENTRY handler is found.
7868 Label loop, done;
7869 __ bind(&loop);
7870 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007871 const int kStateOffset = StackHandlerConstants::kStateOffset;
7872 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007873 __ cmp(r2, Operand(StackHandler::ENTRY));
7874 __ b(eq, &done);
7875 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007876 const int kNextOffset = StackHandlerConstants::kNextOffset;
7877 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007878 __ jmp(&loop);
7879 __ bind(&done);
7880
7881 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007882 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007883 __ pop(r2);
7884 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007885
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007886 if (type == OUT_OF_MEMORY) {
7887 // Set external caught exception to false.
7888 ExternalReference external_caught(Top::k_external_caught_exception_address);
7889 __ mov(r0, Operand(false));
7890 __ mov(r2, Operand(external_caught));
7891 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007892
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007893 // Set pending exception and r0 to out of memory exception.
7894 Failure* out_of_memory = Failure::OutOfMemoryException();
7895 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7896 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
7897 __ str(r0, MemOperand(r2));
7898 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007899
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007900 // Stack layout at this point. See also StackHandlerConstants.
7901 // sp -> state (ENTRY)
7902 // fp
7903 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007904
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007905 // Discard handler state (r2 is not used) and restore frame pointer.
7906 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7907 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
7908 // Before returning we restore the context from the frame pointer if
7909 // not NULL. The frame pointer is NULL in the exception handler of a
7910 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007911 __ cmp(fp, Operand(0));
7912 // Set cp to NULL if fp is NULL.
7913 __ mov(cp, Operand(0), LeaveCC, eq);
7914 // Restore cp otherwise.
7915 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007916#ifdef DEBUG
7917 if (FLAG_debug_code) {
7918 __ mov(lr, Operand(pc));
7919 }
7920#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007921 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007922 __ pop(pc);
7923}
7924
7925
7926void CEntryStub::GenerateCore(MacroAssembler* masm,
7927 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007928 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007929 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007930 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007931 bool always_allocate,
7932 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007933 // r0: result parameter for PerformGC, if any
7934 // r4: number of arguments including receiver (C callee-saved)
7935 // r5: pointer to builtin function (C callee-saved)
7936 // r6: pointer to the first argument (C callee-saved)
7937
7938 if (do_gc) {
7939 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007940 __ PrepareCallCFunction(1, r1);
7941 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007942 }
7943
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007944 ExternalReference scope_depth =
7945 ExternalReference::heap_always_allocate_scope_depth();
7946 if (always_allocate) {
7947 __ mov(r0, Operand(scope_depth));
7948 __ ldr(r1, MemOperand(r0));
7949 __ add(r1, r1, Operand(1));
7950 __ str(r1, MemOperand(r0));
7951 }
7952
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007953 // Call C built-in.
7954 // r0 = argc, r1 = argv
7955 __ mov(r0, Operand(r4));
7956 __ mov(r1, Operand(r6));
7957
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007958 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
7959 int frame_alignment_mask = frame_alignment - 1;
7960#if defined(V8_HOST_ARCH_ARM)
7961 if (FLAG_debug_code) {
7962 if (frame_alignment > kPointerSize) {
7963 Label alignment_as_expected;
7964 ASSERT(IsPowerOf2(frame_alignment));
7965 __ sub(r2, sp, Operand(frame_alignment_skew));
7966 __ tst(r2, Operand(frame_alignment_mask));
7967 __ b(eq, &alignment_as_expected);
7968 // Don't use Check here, as it will call Runtime_Abort re-entering here.
7969 __ stop("Unexpected alignment");
7970 __ bind(&alignment_as_expected);
7971 }
7972 }
7973#endif
7974
7975 // Just before the call (jump) below lr is pushed, so the actual alignment is
7976 // adding one to the current skew.
7977 int alignment_before_call =
7978 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
7979 if (alignment_before_call > 0) {
7980 // Push until the alignment before the call is met.
7981 __ mov(r2, Operand(0));
7982 for (int i = alignment_before_call;
7983 (i & frame_alignment_mask) != 0;
7984 i += kPointerSize) {
7985 __ push(r2);
7986 }
7987 }
7988
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007989 // TODO(1242173): To let the GC traverse the return address of the exit
7990 // frames, we need to know where the return address is. Right now,
7991 // we push it on the stack to be able to find it again, but we never
7992 // restore from it in case of changes, which makes it impossible to
7993 // support moving the C entry code stub. This should be fixed, but currently
7994 // this is OK because the CEntryStub gets generated so early in the V8 boot
7995 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007996 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007997 masm->push(lr);
7998 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007999
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008000 // Restore sp back to before aligning the stack.
8001 if (alignment_before_call > 0) {
8002 __ add(sp, sp, Operand(alignment_before_call));
8003 }
8004
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008005 if (always_allocate) {
8006 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
8007 // though (contain the result).
8008 __ mov(r2, Operand(scope_depth));
8009 __ ldr(r3, MemOperand(r2));
8010 __ sub(r3, r3, Operand(1));
8011 __ str(r3, MemOperand(r2));
8012 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008013
8014 // check for failure result
8015 Label failure_returned;
8016 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
8017 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
8018 __ add(r2, r0, Operand(1));
8019 __ tst(r2, Operand(kFailureTagMask));
8020 __ b(eq, &failure_returned);
8021
8022 // Exit C frame and return.
8023 // r0:r1: result
8024 // sp: stack pointer
8025 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008026 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008027
8028 // check if we should retry or throw exception
8029 Label retry;
8030 __ bind(&failure_returned);
8031 ASSERT(Failure::RETRY_AFTER_GC == 0);
8032 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
8033 __ b(eq, &retry);
8034
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008035 // Special handling of out of memory exceptions.
8036 Failure* out_of_memory = Failure::OutOfMemoryException();
8037 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
8038 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008039
8040 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00008041 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008042 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008043 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008044 __ ldr(r0, MemOperand(ip));
8045 __ str(r3, MemOperand(ip));
8046
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008047 // Special handling of termination exceptions which are uncatchable
8048 // by javascript code.
8049 __ cmp(r0, Operand(Factory::termination_exception()));
8050 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008051
8052 // Handle normal exception.
8053 __ jmp(throw_normal_exception);
8054
8055 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
8056}
8057
8058
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008059void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008060 // Called from JavaScript; parameters are on stack as if calling JS function
8061 // r0: number of arguments including receiver
8062 // r1: pointer to builtin function
8063 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008064 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008065 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008066
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008067 // Result returned in r0 or r0+r1 by default.
8068
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008069 // NOTE: Invocations of builtins may return failure objects
8070 // instead of a proper result. The builtin entry handles
8071 // this by performing a garbage collection and retrying the
8072 // builtin once.
8073
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008074 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008075 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008076
8077 // r4: number of arguments (C callee-saved)
8078 // r5: pointer to builtin function (C callee-saved)
8079 // r6: pointer to first argument (C callee-saved)
8080
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008081 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008082 Label throw_termination_exception;
8083 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008084
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008085 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008086 GenerateCore(masm,
8087 &throw_normal_exception,
8088 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008089 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008090 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008091 false,
8092 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008093
8094 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008095 GenerateCore(masm,
8096 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008097 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008098 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008099 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008100 false,
8101 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008102
8103 // Do full GC and retry runtime call one final time.
8104 Failure* failure = Failure::InternalError();
8105 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
8106 GenerateCore(masm,
8107 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008108 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008109 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008110 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008111 true,
8112 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008113
8114 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008115 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
8116
8117 __ bind(&throw_termination_exception);
8118 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008119
8120 __ bind(&throw_normal_exception);
8121 GenerateThrowTOS(masm);
8122}
8123
8124
8125void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
8126 // r0: code entry
8127 // r1: function
8128 // r2: receiver
8129 // r3: argc
8130 // [sp+0]: argv
8131
8132 Label invoke, exit;
8133
8134 // Called from C, so do not pop argc and args on exit (preserve sp)
8135 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008136 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008137 __ stm(db_w, sp, kCalleeSaved | lr.bit());
8138
8139 // Get address of argv, see stm above.
8140 // r0: code entry
8141 // r1: function
8142 // r2: receiver
8143 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00008144 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008145
8146 // Push a frame with special values setup to mark it as an entry frame.
8147 // r0: code entry
8148 // r1: function
8149 // r2: receiver
8150 // r3: argc
8151 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008152 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008153 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
8154 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008155 __ mov(r6, Operand(Smi::FromInt(marker)));
8156 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8157 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008158 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008159
8160 // Setup frame pointer for the frame to be pushed.
8161 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8162
8163 // Call a faked try-block that does the invoke.
8164 __ bl(&invoke);
8165
8166 // Caught exception: Store result (exception) in the pending
8167 // exception field in the JSEnv and return a failure sentinel.
8168 // Coming in here the fp will be invalid because the PushTryHandler below
8169 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00008170 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008171 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008172 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008173 __ b(&exit);
8174
8175 // Invoke: Link this frame into the handler chain.
8176 __ bind(&invoke);
8177 // Must preserve r0-r4, r5-r7 are available.
8178 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008179 // If an exception not caught by another handler occurs, this handler
8180 // returns control to the code after the bl(&invoke) above, which
8181 // restores all kCalleeSaved registers (including cp and fp) to their
8182 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008183
8184 // Clear any pending exceptions.
8185 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
8186 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008187 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008188 __ str(r5, MemOperand(ip));
8189
8190 // Invoke the function by calling through JS entry trampoline builtin.
8191 // Notice that we cannot store a reference to the trampoline code directly in
8192 // this stub, because runtime stubs are not traversed when doing GC.
8193
8194 // Expected registers by Builtins::JSEntryTrampoline
8195 // r0: code entry
8196 // r1: function
8197 // r2: receiver
8198 // r3: argc
8199 // r4: argv
8200 if (is_construct) {
8201 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
8202 __ mov(ip, Operand(construct_entry));
8203 } else {
8204 ExternalReference entry(Builtins::JSEntryTrampoline);
8205 __ mov(ip, Operand(entry));
8206 }
8207 __ ldr(ip, MemOperand(ip)); // deref address
8208
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008209 // Branch and link to JSEntryTrampoline. We don't use the double underscore
8210 // macro for the add instruction because we don't want the coverage tool
8211 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008212 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008213 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008214
8215 // Unlink this frame from the handler chain. When reading the
8216 // address of the next handler, there is no need to use the address
8217 // displacement since the current stack pointer (sp) points directly
8218 // to the stack handler.
8219 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
8220 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
8221 __ str(r3, MemOperand(ip));
8222 // No need to restore registers
8223 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
8224
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008225
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008226 __ bind(&exit); // r0 holds result
8227 // Restore the top frame descriptors from the stack.
8228 __ pop(r3);
8229 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8230 __ str(r3, MemOperand(ip));
8231
8232 // Reset the stack to the callee saved registers.
8233 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8234
8235 // Restore callee-saved registers and return.
8236#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008237 if (FLAG_debug_code) {
8238 __ mov(lr, Operand(pc));
8239 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008240#endif
8241 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
8242}
8243
8244
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008245// This stub performs an instanceof, calling the builtin function if
8246// necessary. Uses r1 for the object, r0 for the function that it may
8247// be an instance of (these are fetched from the stack).
8248void InstanceofStub::Generate(MacroAssembler* masm) {
8249 // Get the object - slow case for smis (we may need to throw an exception
8250 // depending on the rhs).
8251 Label slow, loop, is_instance, is_not_instance;
8252 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
8253 __ BranchOnSmi(r0, &slow);
8254
8255 // Check that the left hand is a JS object and put map in r3.
8256 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
8257 __ b(lt, &slow);
8258 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
8259 __ b(gt, &slow);
8260
8261 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00008262 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008263 // r1 is function, r3 is map.
8264
8265 // Look up the function and the map in the instanceof cache.
8266 Label miss;
8267 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
8268 __ cmp(r1, ip);
8269 __ b(ne, &miss);
8270 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
8271 __ cmp(r3, ip);
8272 __ b(ne, &miss);
8273 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
8274 __ pop();
8275 __ pop();
8276 __ mov(pc, Operand(lr));
8277
8278 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008279 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
8280
8281 // Check that the function prototype is a JS object.
8282 __ BranchOnSmi(r4, &slow);
8283 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
8284 __ b(lt, &slow);
8285 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
8286 __ b(gt, &slow);
8287
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008288 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
8289 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
8290
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008291 // Register mapping: r3 is object map and r4 is function prototype.
8292 // Get prototype of object into r2.
8293 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
8294
8295 // Loop through the prototype chain looking for the function prototype.
8296 __ bind(&loop);
8297 __ cmp(r2, Operand(r4));
8298 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00008299 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8300 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008301 __ b(eq, &is_not_instance);
8302 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
8303 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
8304 __ jmp(&loop);
8305
8306 __ bind(&is_instance);
8307 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008308 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008309 __ pop();
8310 __ pop();
8311 __ mov(pc, Operand(lr)); // Return.
8312
8313 __ bind(&is_not_instance);
8314 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008315 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008316 __ pop();
8317 __ pop();
8318 __ mov(pc, Operand(lr)); // Return.
8319
8320 // Slow-case. Tail call builtin.
8321 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008322 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
8323}
8324
8325
ager@chromium.org7c537e22008-10-16 08:43:32 +00008326void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
8327 // The displacement is the offset of the last parameter (if any)
8328 // relative to the frame pointer.
8329 static const int kDisplacement =
8330 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008331
ager@chromium.org7c537e22008-10-16 08:43:32 +00008332 // Check that the key is a smi.
8333 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008334 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008335
ager@chromium.org7c537e22008-10-16 08:43:32 +00008336 // Check if the calling frame is an arguments adaptor frame.
8337 Label adaptor;
8338 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8339 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008340 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008341 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008342
ager@chromium.org7c537e22008-10-16 08:43:32 +00008343 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008344 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00008345 // check for free.
8346 __ cmp(r1, r0);
8347 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008348
ager@chromium.org7c537e22008-10-16 08:43:32 +00008349 // Read the argument from the stack and return it.
8350 __ sub(r3, r0, r1);
8351 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8352 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008353 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008354
8355 // Arguments adaptor case: Check index against actual arguments
8356 // limit found in the arguments adaptor frame. Use unsigned
8357 // comparison to get negative check for free.
8358 __ bind(&adaptor);
8359 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8360 __ cmp(r1, r0);
8361 __ b(cs, &slow);
8362
8363 // Read the argument from the adaptor frame and return it.
8364 __ sub(r3, r0, r1);
8365 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
8366 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00008367 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008368
8369 // Slow-case: Handle non-smi or out-of-bounds access to arguments
8370 // by calling the runtime system.
8371 __ bind(&slow);
8372 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008373 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008374}
8375
8376
8377void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00008378 // sp[0] : number of parameters
8379 // sp[4] : receiver displacement
8380 // sp[8] : function
8381
ager@chromium.org7c537e22008-10-16 08:43:32 +00008382 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00008383 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00008384 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
8385 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008386 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00008387 __ b(eq, &adaptor_frame);
8388
8389 // Get the length from the frame.
8390 __ ldr(r1, MemOperand(sp, 0));
8391 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00008392
8393 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00008394 __ bind(&adaptor_frame);
8395 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
8396 __ str(r1, MemOperand(sp, 0));
8397 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00008398 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
8399 __ str(r3, MemOperand(sp, 1 * kPointerSize));
8400
ager@chromium.org5c838252010-02-19 08:53:10 +00008401 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008402 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +00008403 Label add_arguments_object;
8404 __ bind(&try_allocate);
8405 __ cmp(r1, Operand(0));
8406 __ b(eq, &add_arguments_object);
8407 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8408 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
8409 __ bind(&add_arguments_object);
8410 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
8411
8412 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008413 __ AllocateInNewSpace(
8414 r1,
8415 r0,
8416 r2,
8417 r3,
8418 &runtime,
8419 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +00008420
8421 // Get the arguments boilerplate from the current (global) context.
8422 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
8423 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
8424 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
8425 __ ldr(r4, MemOperand(r4, offset));
8426
8427 // Copy the JS object part.
8428 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
8429 __ ldr(r3, FieldMemOperand(r4, i));
8430 __ str(r3, FieldMemOperand(r0, i));
8431 }
8432
8433 // Setup the callee in-object property.
8434 ASSERT(Heap::arguments_callee_index == 0);
8435 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
8436 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
8437
8438 // Get the length (smi tagged) and set that as an in-object property too.
8439 ASSERT(Heap::arguments_length_index == 1);
8440 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
8441 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
8442
8443 // If there are no actual arguments, we're done.
8444 Label done;
8445 __ cmp(r1, Operand(0));
8446 __ b(eq, &done);
8447
8448 // Get the parameters pointer from the stack and untag the length.
8449 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
8450 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
8451
8452 // Setup the elements pointer in the allocated arguments object and
8453 // initialize the header in the elements fixed array.
8454 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
8455 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
8456 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
8457 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
8458 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
8459
8460 // Copy the fixed array slots.
8461 Label loop;
8462 // Setup r4 to point to the first array slot.
8463 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
8464 __ bind(&loop);
8465 // Pre-decrement r2 with kPointerSize on each iteration.
8466 // Pre-decrement in order to skip receiver.
8467 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
8468 // Post-increment r4 with kPointerSize on each iteration.
8469 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
8470 __ sub(r1, r1, Operand(1));
8471 __ cmp(r1, Operand(0));
8472 __ b(ne, &loop);
8473
8474 // Return and remove the on-stack parameters.
8475 __ bind(&done);
8476 __ add(sp, sp, Operand(3 * kPointerSize));
8477 __ Ret();
8478
ager@chromium.org7c537e22008-10-16 08:43:32 +00008479 // Do the runtime call to allocate the arguments object.
8480 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008481 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008482}
8483
8484
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008485void RegExpExecStub::Generate(MacroAssembler* masm) {
8486 // Just jump directly to runtime if native RegExp is not selected at compile
8487 // time or if regexp entry in generated code is turned off runtime switch or
8488 // at compilation.
8489#ifndef V8_NATIVE_REGEXP
8490 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8491#else // V8_NATIVE_REGEXP
8492 if (!FLAG_regexp_entry_native) {
8493 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8494 return;
8495 }
8496
8497 // Stack frame on entry.
8498 // sp[0]: last_match_info (expected JSArray)
8499 // sp[4]: previous index
8500 // sp[8]: subject string
8501 // sp[12]: JSRegExp object
8502
8503 static const int kLastMatchInfoOffset = 0 * kPointerSize;
8504 static const int kPreviousIndexOffset = 1 * kPointerSize;
8505 static const int kSubjectOffset = 2 * kPointerSize;
8506 static const int kJSRegExpOffset = 3 * kPointerSize;
8507
8508 Label runtime, invoke_regexp;
8509
8510 // Allocation of registers for this function. These are in callee save
8511 // registers and will be preserved by the call to the native RegExp code, as
8512 // this code is called using the normal C calling convention. When calling
8513 // directly from generated code the native RegExp code will not do a GC and
8514 // therefore the content of these registers are safe to use after the call.
8515 Register subject = r4;
8516 Register regexp_data = r5;
8517 Register last_match_info_elements = r6;
8518
8519 // Ensure that a RegExp stack is allocated.
8520 ExternalReference address_of_regexp_stack_memory_address =
8521 ExternalReference::address_of_regexp_stack_memory_address();
8522 ExternalReference address_of_regexp_stack_memory_size =
8523 ExternalReference::address_of_regexp_stack_memory_size();
8524 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
8525 __ ldr(r0, MemOperand(r0, 0));
8526 __ tst(r0, Operand(r0));
8527 __ b(eq, &runtime);
8528
8529 // Check that the first argument is a JSRegExp object.
8530 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
8531 ASSERT_EQ(0, kSmiTag);
8532 __ tst(r0, Operand(kSmiTagMask));
8533 __ b(eq, &runtime);
8534 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
8535 __ b(ne, &runtime);
8536
8537 // Check that the RegExp has been compiled (data contains a fixed array).
8538 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
8539 if (FLAG_debug_code) {
8540 __ tst(regexp_data, Operand(kSmiTagMask));
8541 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
8542 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
8543 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
8544 }
8545
8546 // regexp_data: RegExp data (FixedArray)
8547 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
8548 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
8549 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
8550 __ b(ne, &runtime);
8551
8552 // regexp_data: RegExp data (FixedArray)
8553 // Check that the number of captures fit in the static offsets vector buffer.
8554 __ ldr(r2,
8555 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8556 // Calculate number of capture registers (number_of_captures + 1) * 2. This
8557 // uses the asumption that smis are 2 * their untagged value.
8558 ASSERT_EQ(0, kSmiTag);
8559 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8560 __ add(r2, r2, Operand(2)); // r2 was a smi.
8561 // Check that the static offsets vector buffer is large enough.
8562 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
8563 __ b(hi, &runtime);
8564
8565 // r2: Number of capture registers
8566 // regexp_data: RegExp data (FixedArray)
8567 // Check that the second argument is a string.
8568 __ ldr(subject, MemOperand(sp, kSubjectOffset));
8569 __ tst(subject, Operand(kSmiTagMask));
8570 __ b(eq, &runtime);
8571 Condition is_string = masm->IsObjectStringType(subject, r0);
8572 __ b(NegateCondition(is_string), &runtime);
8573 // Get the length of the string to r3.
8574 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
8575
8576 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00008577 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008578 // subject: Subject string
8579 // regexp_data: RegExp data (FixedArray)
8580 // Check that the third argument is a positive smi less than the subject
8581 // string length. A negative value will be greater (unsigned comparison).
8582 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00008583 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008584 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +00008585 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008586 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008587
8588 // r2: Number of capture registers
8589 // subject: Subject string
8590 // regexp_data: RegExp data (FixedArray)
8591 // Check that the fourth object is a JSArray object.
8592 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8593 __ tst(r0, Operand(kSmiTagMask));
8594 __ b(eq, &runtime);
8595 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
8596 __ b(ne, &runtime);
8597 // Check that the JSArray is in fast case.
8598 __ ldr(last_match_info_elements,
8599 FieldMemOperand(r0, JSArray::kElementsOffset));
8600 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008601 __ LoadRoot(ip, kFixedArrayMapRootIndex);
8602 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008603 __ b(ne, &runtime);
8604 // Check that the last match info has space for the capture registers and the
8605 // additional information.
8606 __ ldr(r0,
8607 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
8608 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
8609 __ cmp(r2, r0);
8610 __ b(gt, &runtime);
8611
8612 // subject: Subject string
8613 // regexp_data: RegExp data (FixedArray)
8614 // Check the representation and encoding of the subject string.
8615 Label seq_string;
8616 const int kStringRepresentationEncodingMask =
8617 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
8618 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8619 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8620 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8621 // First check for sequential string.
8622 ASSERT_EQ(0, kStringTag);
8623 ASSERT_EQ(0, kSeqStringTag);
8624 __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask));
8625 __ b(eq, &seq_string);
8626
8627 // subject: Subject string
8628 // regexp_data: RegExp data (FixedArray)
8629 // Check for flat cons string.
8630 // A flat cons string is a cons string where the second part is the empty
8631 // string. In that case the subject string is just the first part of the cons
8632 // string. Also in this case the first part of the cons string is known to be
8633 // a sequential string or an external string.
8634 __ and_(r0, r0, Operand(kStringRepresentationMask));
8635 __ cmp(r0, Operand(kConsStringTag));
8636 __ b(ne, &runtime);
8637 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
8638 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
8639 __ cmp(r0, r1);
8640 __ b(ne, &runtime);
8641 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
8642 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
8643 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
8644 ASSERT_EQ(0, kSeqStringTag);
8645 __ tst(r0, Operand(kStringRepresentationMask));
8646 __ b(nz, &runtime);
8647 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
8648
8649 __ bind(&seq_string);
8650 // r1: suject string type & kStringRepresentationEncodingMask
8651 // subject: Subject string
8652 // regexp_data: RegExp data (FixedArray)
8653 // Check that the irregexp code has been generated for an ascii string. If
8654 // it has, the field contains a code object otherwise it contains the hole.
8655#ifdef DEBUG
8656 const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag;
8657 const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag;
8658 CHECK_EQ(4, kSeqAsciiString);
8659 CHECK_EQ(0, kSeqTwoByteString);
8660#endif
8661 // Find the code object based on the assumptions above.
8662 __ mov(r3, Operand(r1, ASR, 2), SetCC);
8663 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
8664 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
8665
8666 // Check that the irregexp code has been generated for the actual string
8667 // encoding. If it has, the field contains a code object otherwise it contains
8668 // the hole.
8669 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
8670 __ b(ne, &runtime);
8671
8672 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8673 // r7: code
8674 // subject: Subject string
8675 // regexp_data: RegExp data (FixedArray)
8676 // Load used arguments before starting to push arguments for call to native
8677 // RegExp code to avoid handling changing stack height.
8678 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
8679 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
8680
8681 // r1: previous index
8682 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
8683 // r7: code
8684 // subject: Subject string
8685 // regexp_data: RegExp data (FixedArray)
8686 // All checks done. Now push arguments for native regexp code.
8687 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
8688
8689 static const int kRegExpExecuteArguments = 7;
8690 __ push(lr);
8691 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
8692
8693 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
8694 __ mov(r0, Operand(1));
8695 __ str(r0, MemOperand(sp, 2 * kPointerSize));
8696
8697 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
8698 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
8699 __ ldr(r0, MemOperand(r0, 0));
8700 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
8701 __ ldr(r2, MemOperand(r2, 0));
8702 __ add(r0, r0, Operand(r2));
8703 __ str(r0, MemOperand(sp, 1 * kPointerSize));
8704
8705 // Argument 5 (sp[0]): static offsets vector buffer.
8706 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
8707 __ str(r0, MemOperand(sp, 0 * kPointerSize));
8708
8709 // For arguments 4 and 3 get string length, calculate start of string data and
8710 // calculate the shift of the index (0 for ASCII and 1 for two byte).
8711 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00008712 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008713 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
8714 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8715 __ eor(r3, r3, Operand(1));
8716 // Argument 4 (r3): End of string data
8717 // Argument 3 (r2): Start of string data
8718 __ add(r2, r9, Operand(r1, LSL, r3));
8719 __ add(r3, r9, Operand(r0, LSL, r3));
8720
8721 // Argument 2 (r1): Previous index.
8722 // Already there
8723
8724 // Argument 1 (r0): Subject string.
8725 __ mov(r0, subject);
8726
8727 // Locate the code entry and call it.
8728 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
8729 __ CallCFunction(r7, kRegExpExecuteArguments);
8730 __ pop(lr);
8731
8732 // r0: result
8733 // subject: subject string (callee saved)
8734 // regexp_data: RegExp data (callee saved)
8735 // last_match_info_elements: Last match info elements (callee saved)
8736
8737 // Check the result.
8738 Label success;
8739 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
8740 __ b(eq, &success);
8741 Label failure;
8742 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
8743 __ b(eq, &failure);
8744 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
8745 // If not exception it can only be retry. Handle that in the runtime system.
8746 __ b(ne, &runtime);
8747 // Result must now be exception. If there is no pending exception already a
8748 // stack overflow (on the backtrack stack) was detected in RegExp code but
8749 // haven't created the exception yet. Handle that in the runtime system.
8750 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
8751 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
8752 __ ldr(r0, MemOperand(r0, 0));
8753 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
8754 __ ldr(r1, MemOperand(r1, 0));
8755 __ cmp(r0, r1);
8756 __ b(eq, &runtime);
8757 __ bind(&failure);
8758 // For failure and exception return null.
8759 __ mov(r0, Operand(Factory::null_value()));
8760 __ add(sp, sp, Operand(4 * kPointerSize));
8761 __ Ret();
8762
8763 // Process the result from the native regexp code.
8764 __ bind(&success);
8765 __ ldr(r1,
8766 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
8767 // Calculate number of capture registers (number_of_captures + 1) * 2.
8768 ASSERT_EQ(0, kSmiTag);
8769 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8770 __ add(r1, r1, Operand(2)); // r1 was a smi.
8771
8772 // r1: number of capture registers
8773 // r4: subject string
8774 // Store the capture count.
8775 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
8776 __ str(r2, FieldMemOperand(last_match_info_elements,
8777 RegExpImpl::kLastCaptureCountOffset));
8778 // Store last subject and last input.
8779 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
8780 __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto.
8781 __ str(subject,
8782 FieldMemOperand(last_match_info_elements,
8783 RegExpImpl::kLastSubjectOffset));
8784 __ RecordWrite(r3, r2, r7);
8785 __ str(subject,
8786 FieldMemOperand(last_match_info_elements,
8787 RegExpImpl::kLastInputOffset));
8788 __ mov(r3, last_match_info_elements);
8789 __ mov(r2, Operand(RegExpImpl::kLastInputOffset));
8790 __ RecordWrite(r3, r2, r7);
8791
8792 // Get the static offsets vector filled by the native regexp code.
8793 ExternalReference address_of_static_offsets_vector =
8794 ExternalReference::address_of_static_offsets_vector();
8795 __ mov(r2, Operand(address_of_static_offsets_vector));
8796
8797 // r1: number of capture registers
8798 // r2: offsets vector
8799 Label next_capture, done;
8800 // Capture register counter starts from number of capture registers and
8801 // counts down until wraping after zero.
8802 __ add(r0,
8803 last_match_info_elements,
8804 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
8805 __ bind(&next_capture);
8806 __ sub(r1, r1, Operand(1), SetCC);
8807 __ b(mi, &done);
8808 // Read the value from the static offsets vector buffer.
8809 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
8810 // Store the smi value in the last match info.
8811 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
8812 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
8813 __ jmp(&next_capture);
8814 __ bind(&done);
8815
8816 // Return last match info.
8817 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
8818 __ add(sp, sp, Operand(4 * kPointerSize));
8819 __ Ret();
8820
8821 // Do the runtime call to execute the regexp.
8822 __ bind(&runtime);
8823 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
8824#endif // V8_NATIVE_REGEXP
8825}
8826
8827
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008828void CallFunctionStub::Generate(MacroAssembler* masm) {
8829 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008830
8831 // If the receiver might be a value (string, number or boolean) check for this
8832 // and box it if it is.
8833 if (ReceiverMightBeValue()) {
8834 // Get the receiver from the stack.
8835 // function, receiver [, arguments]
8836 Label receiver_is_value, receiver_is_js_object;
8837 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
8838
8839 // Check if receiver is a smi (which is a number value).
8840 __ BranchOnSmi(r1, &receiver_is_value);
8841
8842 // Check if the receiver is a valid JS object.
8843 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
8844 __ b(ge, &receiver_is_js_object);
8845
8846 // Call the runtime to box the value.
8847 __ bind(&receiver_is_value);
8848 __ EnterInternalFrame();
8849 __ push(r1);
8850 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
8851 __ LeaveInternalFrame();
8852 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
8853
8854 __ bind(&receiver_is_js_object);
8855 }
8856
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008857 // Get the function to call from the stack.
8858 // function, receiver [, arguments]
8859 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
8860
8861 // Check that the function is really a JavaScript function.
8862 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008863 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008864 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008865 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008866 __ b(ne, &slow);
8867
8868 // Fast-case: Invoke the function now.
8869 // r1: pushed function
8870 ParameterCount actual(argc_);
8871 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
8872
8873 // Slow-case: Non-function called.
8874 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00008875 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
8876 // of the original receiver from the call site).
8877 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008878 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008879 __ mov(r2, Operand(0));
8880 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
8881 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
8882 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008883}
8884
8885
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008886// Unfortunately you have to run without snapshots to see most of these
8887// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008888const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008889 if (name_ != NULL) return name_;
8890 const int kMaxNameLength = 100;
8891 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
8892 if (name_ == NULL) return "OOM";
8893
8894 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008895 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008896 case lt: cc_name = "LT"; break;
8897 case gt: cc_name = "GT"; break;
8898 case le: cc_name = "LE"; break;
8899 case ge: cc_name = "GE"; break;
8900 case eq: cc_name = "EQ"; break;
8901 case ne: cc_name = "NE"; break;
8902 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008903 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008904
8905 const char* strict_name = "";
8906 if (strict_ && (cc_ == eq || cc_ == ne)) {
8907 strict_name = "_STRICT";
8908 }
8909
8910 const char* never_nan_nan_name = "";
8911 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
8912 never_nan_nan_name = "_NO_NAN";
8913 }
8914
8915 const char* include_number_compare_name = "";
8916 if (!include_number_compare_) {
8917 include_number_compare_name = "_NO_NUMBER";
8918 }
8919
8920 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
8921 "CompareStub_%s%s%s%s",
8922 cc_name,
8923 strict_name,
8924 never_nan_nan_name,
8925 include_number_compare_name);
8926 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008927}
8928
8929
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008930int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008931 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
8932 // stubs the never NaN NaN condition is only taken into account if the
8933 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008934 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008935 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
8936 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008937 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
8938 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008939}
8940
8941
ager@chromium.orgac091b72010-05-05 07:34:42 +00008942void StringHelper::GenerateFastCharCodeAt(MacroAssembler* masm,
8943 Register object,
8944 Register index,
8945 Register scratch,
8946 Register result,
8947 Label* receiver_not_string,
8948 Label* index_not_smi,
8949 Label* index_out_of_range,
8950 Label* slow_case) {
8951 Label not_a_flat_string;
8952 Label try_again_with_new_string;
8953 Label ascii_string;
8954 Label got_char_code;
8955
8956 // If the receiver is a smi trigger the non-string case.
8957 __ BranchOnSmi(object, receiver_not_string);
8958
8959 // Fetch the instance type of the receiver into result register.
8960 __ ldr(result, FieldMemOperand(object, HeapObject::kMapOffset));
8961 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
8962 // If the receiver is not a string trigger the non-string case.
8963 __ tst(result, Operand(kIsNotStringMask));
8964 __ b(ne, receiver_not_string);
8965
8966 // If the index is non-smi trigger the non-smi case.
8967 __ BranchOnNotSmi(index, index_not_smi);
8968
8969 // Check for index out of range.
8970 __ ldr(scratch, FieldMemOperand(object, String::kLengthOffset));
8971 // Now scratch has the length of the string. Compare with the index.
8972 __ cmp(scratch, Operand(index));
8973 __ b(ls, index_out_of_range);
8974
8975 __ bind(&try_again_with_new_string);
8976 // ----------- S t a t e -------------
8977 // -- object : string to access
8978 // -- result : instance type of the string
8979 // -- scratch : non-negative index < length
8980 // -----------------------------------
8981
8982 // We need special handling for non-flat strings.
8983 ASSERT_EQ(0, kSeqStringTag);
8984 __ tst(result, Operand(kStringRepresentationMask));
8985 __ b(ne, &not_a_flat_string);
8986
8987 // Check for 1-byte or 2-byte string.
8988 ASSERT_EQ(0, kTwoByteStringTag);
8989 __ tst(result, Operand(kStringEncodingMask));
8990 __ b(ne, &ascii_string);
8991
8992 // 2-byte string. We can add without shifting since the Smi tag size is the
8993 // log2 of the number of bytes in a two-byte character.
8994 ASSERT_EQ(1, kSmiTagSize);
8995 ASSERT_EQ(0, kSmiShiftSize);
8996 __ add(scratch, object, Operand(index));
8997 __ ldrh(result, FieldMemOperand(scratch, SeqTwoByteString::kHeaderSize));
8998 __ jmp(&got_char_code);
8999
9000 // Handle non-flat strings.
9001 __ bind(&not_a_flat_string);
9002 __ and_(result, result, Operand(kStringRepresentationMask));
9003 __ cmp(result, Operand(kConsStringTag));
9004 __ b(ne, slow_case);
9005
9006 // ConsString.
9007 // Check whether the right hand side is the empty string (i.e. if
9008 // this is really a flat string in a cons string). If that is not
9009 // the case we would rather go to the runtime system now to flatten
9010 // the string.
9011 __ ldr(result, FieldMemOperand(object, ConsString::kSecondOffset));
9012 __ LoadRoot(scratch, Heap::kEmptyStringRootIndex);
9013 __ cmp(result, Operand(scratch));
9014 __ b(ne, slow_case);
9015
9016 // Get the first of the two strings and load its instance type.
9017 __ ldr(object, FieldMemOperand(object, ConsString::kFirstOffset));
9018 __ ldr(result, FieldMemOperand(object, HeapObject::kMapOffset));
9019 __ ldrb(result, FieldMemOperand(result, Map::kInstanceTypeOffset));
9020 __ jmp(&try_again_with_new_string);
9021
9022 // ASCII string.
9023 __ bind(&ascii_string);
9024 __ add(scratch, object, Operand(index, LSR, kSmiTagSize));
9025 __ ldrb(result, FieldMemOperand(scratch, SeqAsciiString::kHeaderSize));
9026
9027 __ bind(&got_char_code);
9028 __ mov(result, Operand(result, LSL, kSmiTagSize));
9029}
9030
9031
9032void StringHelper::GenerateCharFromCode(MacroAssembler* masm,
9033 Register code,
9034 Register scratch,
9035 Register result,
9036 InvokeFlag flag) {
9037 ASSERT(!code.is(result));
9038
9039 Label slow_case;
9040 Label exit;
9041
9042 // Fast case of Heap::LookupSingleCharacterStringFromCode.
9043 ASSERT(kSmiTag == 0);
9044 ASSERT(kSmiShiftSize == 0);
9045 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
9046 __ tst(code, Operand(kSmiTagMask |
9047 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
9048 __ b(nz, &slow_case);
9049
9050 ASSERT(kSmiTag == 0);
9051 __ mov(result, Operand(Factory::single_character_string_cache()));
9052 __ add(result, result, Operand(code, LSL, kPointerSizeLog2 - kSmiTagSize));
9053 __ ldr(result, MemOperand(result, FixedArray::kHeaderSize - kHeapObjectTag));
9054 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
9055 __ cmp(result, scratch);
9056 __ b(eq, &slow_case);
9057 __ b(&exit);
9058
9059 __ bind(&slow_case);
9060 if (flag == CALL_FUNCTION) {
9061 __ push(code);
9062 __ CallRuntime(Runtime::kCharFromCode, 1);
9063 if (!result.is(r0)) {
9064 __ mov(result, r0);
9065 }
9066 } else {
9067 ASSERT(flag == JUMP_FUNCTION);
9068 ASSERT(result.is(r0));
9069 __ push(code);
9070 __ TailCallRuntime(Runtime::kCharFromCode, 1, 1);
9071 }
9072
9073 __ bind(&exit);
9074 if (flag == JUMP_FUNCTION) {
9075 ASSERT(result.is(r0));
9076 __ Ret();
9077 }
9078}
9079
9080
9081void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
9082 Register dest,
9083 Register src,
9084 Register count,
9085 Register scratch,
9086 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009087 Label loop;
9088 Label done;
9089 // This loop just copies one character at a time, as it is only used for very
9090 // short strings.
9091 if (!ascii) {
9092 __ add(count, count, Operand(count), SetCC);
9093 } else {
9094 __ cmp(count, Operand(0));
9095 }
9096 __ b(eq, &done);
9097
9098 __ bind(&loop);
9099 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
9100 // Perform sub between load and dependent store to get the load time to
9101 // complete.
9102 __ sub(count, count, Operand(1), SetCC);
9103 __ strb(scratch, MemOperand(dest, 1, PostIndex));
9104 // last iteration.
9105 __ b(gt, &loop);
9106
9107 __ bind(&done);
9108}
9109
9110
9111enum CopyCharactersFlags {
9112 COPY_ASCII = 1,
9113 DEST_ALWAYS_ALIGNED = 2
9114};
9115
9116
ager@chromium.orgac091b72010-05-05 07:34:42 +00009117void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
9118 Register dest,
9119 Register src,
9120 Register count,
9121 Register scratch1,
9122 Register scratch2,
9123 Register scratch3,
9124 Register scratch4,
9125 Register scratch5,
9126 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009127 bool ascii = (flags & COPY_ASCII) != 0;
9128 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
9129
9130 if (dest_always_aligned && FLAG_debug_code) {
9131 // Check that destination is actually word aligned if the flag says
9132 // that it is.
9133 __ tst(dest, Operand(kPointerAlignmentMask));
9134 __ Check(eq, "Destination of copy not aligned.");
9135 }
9136
9137 const int kReadAlignment = 4;
9138 const int kReadAlignmentMask = kReadAlignment - 1;
9139 // Ensure that reading an entire aligned word containing the last character
9140 // of a string will not read outside the allocated area (because we pad up
9141 // to kObjectAlignment).
9142 ASSERT(kObjectAlignment >= kReadAlignment);
9143 // Assumes word reads and writes are little endian.
9144 // Nothing to do for zero characters.
9145 Label done;
9146 if (!ascii) {
9147 __ add(count, count, Operand(count), SetCC);
9148 } else {
9149 __ cmp(count, Operand(0));
9150 }
9151 __ b(eq, &done);
9152
9153 // Assume that you cannot read (or write) unaligned.
9154 Label byte_loop;
9155 // Must copy at least eight bytes, otherwise just do it one byte at a time.
9156 __ cmp(count, Operand(8));
9157 __ add(count, dest, Operand(count));
9158 Register limit = count; // Read until src equals this.
9159 __ b(lt, &byte_loop);
9160
9161 if (!dest_always_aligned) {
9162 // Align dest by byte copying. Copies between zero and three bytes.
9163 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
9164 Label dest_aligned;
9165 __ b(eq, &dest_aligned);
9166 __ cmp(scratch4, Operand(2));
9167 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
9168 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
9169 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
9170 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9171 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
9172 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
9173 __ bind(&dest_aligned);
9174 }
9175
9176 Label simple_loop;
9177
9178 __ sub(scratch4, dest, Operand(src));
9179 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
9180 __ b(eq, &simple_loop);
9181 // Shift register is number of bits in a source word that
9182 // must be combined with bits in the next source word in order
9183 // to create a destination word.
9184
9185 // Complex loop for src/dst that are not aligned the same way.
9186 {
9187 Label loop;
9188 __ mov(scratch4, Operand(scratch4, LSL, 3));
9189 Register left_shift = scratch4;
9190 __ and_(src, src, Operand(~3)); // Round down to load previous word.
9191 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9192 // Store the "shift" most significant bits of scratch in the least
9193 // signficant bits (i.e., shift down by (32-shift)).
9194 __ rsb(scratch2, left_shift, Operand(32));
9195 Register right_shift = scratch2;
9196 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
9197
9198 __ bind(&loop);
9199 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
9200 __ sub(scratch5, limit, Operand(dest));
9201 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
9202 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9203 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
9204 // Loop if four or more bytes left to copy.
9205 // Compare to eight, because we did the subtract before increasing dst.
9206 __ sub(scratch5, scratch5, Operand(8), SetCC);
9207 __ b(ge, &loop);
9208 }
9209 // There is now between zero and three bytes left to copy (negative that
9210 // number is in scratch5), and between one and three bytes already read into
9211 // scratch1 (eight times that number in scratch4). We may have read past
9212 // the end of the string, but because objects are aligned, we have not read
9213 // past the end of the object.
9214 // Find the minimum of remaining characters to move and preloaded characters
9215 // and write those as bytes.
9216 __ add(scratch5, scratch5, Operand(4), SetCC);
9217 __ b(eq, &done);
9218 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
9219 // Move minimum of bytes read and bytes left to copy to scratch4.
9220 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
9221 // Between one and three (value in scratch5) characters already read into
9222 // scratch ready to write.
9223 __ cmp(scratch5, Operand(2));
9224 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9225 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
9226 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
9227 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
9228 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
9229 // Copy any remaining bytes.
9230 __ b(&byte_loop);
9231
9232 // Simple loop.
9233 // Copy words from src to dst, until less than four bytes left.
9234 // Both src and dest are word aligned.
9235 __ bind(&simple_loop);
9236 {
9237 Label loop;
9238 __ bind(&loop);
9239 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9240 __ sub(scratch3, limit, Operand(dest));
9241 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9242 // Compare to 8, not 4, because we do the substraction before increasing
9243 // dest.
9244 __ cmp(scratch3, Operand(8));
9245 __ b(ge, &loop);
9246 }
9247
9248 // Copy bytes from src to dst until dst hits limit.
9249 __ bind(&byte_loop);
9250 __ cmp(dest, Operand(limit));
9251 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
9252 __ b(ge, &done);
9253 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9254 __ b(&byte_loop);
9255
9256 __ bind(&done);
9257}
9258
9259
ager@chromium.orgac091b72010-05-05 07:34:42 +00009260void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
9261 Register c1,
9262 Register c2,
9263 Register scratch1,
9264 Register scratch2,
9265 Register scratch3,
9266 Register scratch4,
9267 Register scratch5,
9268 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009269 // Register scratch3 is the general scratch register in this function.
9270 Register scratch = scratch3;
9271
9272 // Make sure that both characters are not digits as such strings has a
9273 // different hash algorithm. Don't try to look for these in the symbol table.
9274 Label not_array_index;
9275 __ sub(scratch, c1, Operand(static_cast<int>('0')));
9276 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
9277 __ b(hi, &not_array_index);
9278 __ sub(scratch, c2, Operand(static_cast<int>('0')));
9279 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
9280
9281 // If check failed combine both characters into single halfword.
9282 // This is required by the contract of the method: code at the
9283 // not_found branch expects this combination in c1 register
9284 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
9285 __ b(ls, not_found);
9286
9287 __ bind(&not_array_index);
9288 // Calculate the two character string hash.
9289 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009290 StringHelper::GenerateHashInit(masm, hash, c1);
9291 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
9292 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009293
9294 // Collect the two characters in a register.
9295 Register chars = c1;
9296 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
9297
9298 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
9299 // hash: hash of two character string.
9300
9301 // Load symbol table
9302 // Load address of first element of the symbol table.
9303 Register symbol_table = c2;
9304 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
9305
9306 // Load undefined value
9307 Register undefined = scratch4;
9308 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
9309
9310 // Calculate capacity mask from the symbol table capacity.
9311 Register mask = scratch2;
9312 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
9313 __ mov(mask, Operand(mask, ASR, 1));
9314 __ sub(mask, mask, Operand(1));
9315
9316 // Calculate untagged address of the first element of the symbol table.
9317 Register first_symbol_table_element = symbol_table;
9318 __ add(first_symbol_table_element, symbol_table,
9319 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
9320
9321 // Registers
9322 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
9323 // hash: hash of two character string
9324 // mask: capacity mask
9325 // first_symbol_table_element: address of the first element of
9326 // the symbol table
9327 // scratch: -
9328
9329 // Perform a number of probes in the symbol table.
9330 static const int kProbes = 4;
9331 Label found_in_symbol_table;
9332 Label next_probe[kProbes];
9333 for (int i = 0; i < kProbes; i++) {
9334 Register candidate = scratch5; // Scratch register contains candidate.
9335
9336 // Calculate entry in symbol table.
9337 if (i > 0) {
9338 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
9339 } else {
9340 __ mov(candidate, hash);
9341 }
9342
9343 __ and_(candidate, candidate, Operand(mask));
9344
9345 // Load the entry from the symble table.
9346 ASSERT_EQ(1, SymbolTable::kEntrySize);
9347 __ ldr(candidate,
9348 MemOperand(first_symbol_table_element,
9349 candidate,
9350 LSL,
9351 kPointerSizeLog2));
9352
9353 // If entry is undefined no string with this hash can be found.
9354 __ cmp(candidate, undefined);
9355 __ b(eq, not_found);
9356
9357 // If length is not 2 the string is not a candidate.
9358 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009359 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009360 __ b(ne, &next_probe[i]);
9361
9362 // Check that the candidate is a non-external ascii string.
9363 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
9364 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
9365 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
9366 &next_probe[i]);
9367
9368 // Check if the two characters match.
9369 // Assumes that word load is little endian.
9370 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
9371 __ cmp(chars, scratch);
9372 __ b(eq, &found_in_symbol_table);
9373 __ bind(&next_probe[i]);
9374 }
9375
9376 // No matching 2 character string found by probing.
9377 __ jmp(not_found);
9378
9379 // Scratch register contains result when we fall through to here.
9380 Register result = scratch;
9381 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00009382 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009383}
9384
9385
ager@chromium.orgac091b72010-05-05 07:34:42 +00009386void StringHelper::GenerateHashInit(MacroAssembler* masm,
9387 Register hash,
9388 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009389 // hash = character + (character << 10);
9390 __ add(hash, character, Operand(character, LSL, 10));
9391 // hash ^= hash >> 6;
9392 __ eor(hash, hash, Operand(hash, ASR, 6));
9393}
9394
9395
ager@chromium.orgac091b72010-05-05 07:34:42 +00009396void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
9397 Register hash,
9398 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009399 // hash += character;
9400 __ add(hash, hash, Operand(character));
9401 // hash += hash << 10;
9402 __ add(hash, hash, Operand(hash, LSL, 10));
9403 // hash ^= hash >> 6;
9404 __ eor(hash, hash, Operand(hash, ASR, 6));
9405}
9406
9407
ager@chromium.orgac091b72010-05-05 07:34:42 +00009408void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
9409 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009410 // hash += hash << 3;
9411 __ add(hash, hash, Operand(hash, LSL, 3));
9412 // hash ^= hash >> 11;
9413 __ eor(hash, hash, Operand(hash, ASR, 11));
9414 // hash += hash << 15;
9415 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
9416
9417 // if (hash == 0) hash = 27;
9418 __ mov(hash, Operand(27), LeaveCC, nz);
9419}
9420
9421
ager@chromium.org5c838252010-02-19 08:53:10 +00009422void SubStringStub::Generate(MacroAssembler* masm) {
9423 Label runtime;
9424
9425 // Stack frame on entry.
9426 // lr: return address
9427 // sp[0]: to
9428 // sp[4]: from
9429 // sp[8]: string
9430
9431 // This stub is called from the native-call %_SubString(...), so
9432 // nothing can be assumed about the arguments. It is tested that:
9433 // "string" is a sequential string,
9434 // both "from" and "to" are smis, and
9435 // 0 <= from <= to <= string.length.
9436 // If any of these assumptions fail, we call the runtime system.
9437
9438 static const int kToOffset = 0 * kPointerSize;
9439 static const int kFromOffset = 1 * kPointerSize;
9440 static const int kStringOffset = 2 * kPointerSize;
9441
9442
9443 // Check bounds and smi-ness.
9444 __ ldr(r7, MemOperand(sp, kToOffset));
9445 __ ldr(r6, MemOperand(sp, kFromOffset));
9446 ASSERT_EQ(0, kSmiTag);
9447 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
9448 // I.e., arithmetic shift right by one un-smi-tags.
9449 __ mov(r2, Operand(r7, ASR, 1), SetCC);
9450 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
9451 // If either r2 or r6 had the smi tag bit set, then carry is set now.
9452 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
9453 __ b(mi, &runtime); // From is negative.
9454
9455 __ sub(r2, r2, Operand(r3), SetCC);
9456 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009457 // Special handling of sub-strings of length 1 and 2. One character strings
9458 // are handled in the runtime system (looked up in the single character
9459 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00009460 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009461 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00009462
9463 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009464 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009465 // r6: from (smi)
9466 // r7: to (smi)
9467
9468 // Make sure first argument is a sequential (or flat) string.
9469 __ ldr(r5, MemOperand(sp, kStringOffset));
9470 ASSERT_EQ(0, kSmiTag);
9471 __ tst(r5, Operand(kSmiTagMask));
9472 __ b(eq, &runtime);
9473 Condition is_string = masm->IsObjectStringType(r5, r1);
9474 __ b(NegateCondition(is_string), &runtime);
9475
9476 // r1: instance type
9477 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009478 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009479 // r5: string
9480 // r6: from (smi)
9481 // r7: to (smi)
9482 Label seq_string;
9483 __ and_(r4, r1, Operand(kStringRepresentationMask));
9484 ASSERT(kSeqStringTag < kConsStringTag);
9485 ASSERT(kExternalStringTag > kConsStringTag);
9486 __ cmp(r4, Operand(kConsStringTag));
9487 __ b(gt, &runtime); // External strings go to runtime.
9488 __ b(lt, &seq_string); // Sequential strings are handled directly.
9489
9490 // Cons string. Try to recurse (once) on the first substring.
9491 // (This adds a little more generality than necessary to handle flattened
9492 // cons strings, but not much).
9493 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
9494 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
9495 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9496 __ tst(r1, Operand(kStringRepresentationMask));
9497 ASSERT_EQ(0, kSeqStringTag);
9498 __ b(ne, &runtime); // Cons and External strings go to runtime.
9499
9500 // Definitly a sequential string.
9501 __ bind(&seq_string);
9502
9503 // r1: instance type.
9504 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009505 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009506 // r5: string
9507 // r6: from (smi)
9508 // r7: to (smi)
9509 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009510 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +00009511 __ b(lt, &runtime); // Fail if to > length.
9512
9513 // r1: instance type.
9514 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009515 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00009516 // r5: string.
9517 // r6: from offset (smi)
9518 // Check for flat ascii string.
9519 Label non_ascii_flat;
9520 __ tst(r1, Operand(kStringEncodingMask));
9521 ASSERT_EQ(0, kTwoByteStringTag);
9522 __ b(eq, &non_ascii_flat);
9523
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009524 Label result_longer_than_two;
9525 __ cmp(r2, Operand(2));
9526 __ b(gt, &result_longer_than_two);
9527
9528 // Sub string of length 2 requested.
9529 // Get the two characters forming the sub string.
9530 __ add(r5, r5, Operand(r3));
9531 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
9532 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
9533
9534 // Try to lookup two character string in symbol table.
9535 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009536 StringHelper::GenerateTwoCharacterSymbolTableProbe(
9537 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009538 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9539 __ add(sp, sp, Operand(3 * kPointerSize));
9540 __ Ret();
9541
9542 // r2: result string length.
9543 // r3: two characters combined into halfword in little endian byte order.
9544 __ bind(&make_two_character_string);
9545 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
9546 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9547 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9548 __ add(sp, sp, Operand(3 * kPointerSize));
9549 __ Ret();
9550
9551 __ bind(&result_longer_than_two);
9552
ager@chromium.org5c838252010-02-19 08:53:10 +00009553 // Allocate the result.
9554 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
9555
9556 // r0: result string.
9557 // r2: result string length.
9558 // r5: string.
9559 // r6: from offset (smi)
9560 // Locate first character of result.
9561 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9562 // Locate 'from' character of string.
9563 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9564 __ add(r5, r5, Operand(r6, ASR, 1));
9565
9566 // r0: result string.
9567 // r1: first character of result string.
9568 // r2: result string length.
9569 // r5: first character of sub string to copy.
9570 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009571 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9572 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +00009573 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9574 __ add(sp, sp, Operand(3 * kPointerSize));
9575 __ Ret();
9576
9577 __ bind(&non_ascii_flat);
9578 // r2: result string length.
9579 // r5: string.
9580 // r6: from offset (smi)
9581 // Check for flat two byte string.
9582
9583 // Allocate the result.
9584 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
9585
9586 // r0: result string.
9587 // r2: result string length.
9588 // r5: string.
9589 // Locate first character of result.
9590 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9591 // Locate 'from' character of string.
9592 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9593 // As "from" is a smi it is 2 times the value which matches the size of a two
9594 // byte character.
9595 __ add(r5, r5, Operand(r6));
9596
9597 // r0: result string.
9598 // r1: first character of result.
9599 // r2: result length.
9600 // r5: first character of string to copy.
9601 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009602 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
9603 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +00009604 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
9605 __ add(sp, sp, Operand(3 * kPointerSize));
9606 __ Ret();
9607
9608 // Just jump to runtime to create the sub string.
9609 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009610 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009611}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009612
9613
9614void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
9615 Register left,
9616 Register right,
9617 Register scratch1,
9618 Register scratch2,
9619 Register scratch3,
9620 Register scratch4) {
9621 Label compare_lengths;
9622 // Find minimum length and length difference.
9623 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
9624 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
9625 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
9626 Register length_delta = scratch3;
9627 __ mov(scratch1, scratch2, LeaveCC, gt);
9628 Register min_length = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009629 ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009630 __ tst(min_length, Operand(min_length));
9631 __ b(eq, &compare_lengths);
9632
ager@chromium.orgac091b72010-05-05 07:34:42 +00009633 // Untag smi.
9634 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
9635
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009636 // Setup registers so that we only need to increment one register
9637 // in the loop.
9638 __ add(scratch2, min_length,
9639 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9640 __ add(left, left, Operand(scratch2));
9641 __ add(right, right, Operand(scratch2));
9642 // Registers left and right points to the min_length character of strings.
9643 __ rsb(min_length, min_length, Operand(-1));
9644 Register index = min_length;
9645 // Index starts at -min_length.
9646
9647 {
9648 // Compare loop.
9649 Label loop;
9650 __ bind(&loop);
9651 // Compare characters.
9652 __ add(index, index, Operand(1), SetCC);
9653 __ ldrb(scratch2, MemOperand(left, index), ne);
9654 __ ldrb(scratch4, MemOperand(right, index), ne);
9655 // Skip to compare lengths with eq condition true.
9656 __ b(eq, &compare_lengths);
9657 __ cmp(scratch2, scratch4);
9658 __ b(eq, &loop);
9659 // Fallthrough with eq condition false.
9660 }
9661 // Compare lengths - strings up to min-length are equal.
9662 __ bind(&compare_lengths);
9663 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
9664 // Use zero length_delta as result.
9665 __ mov(r0, Operand(length_delta), SetCC, eq);
9666 // Fall through to here if characters compare not-equal.
9667 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
9668 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
9669 __ Ret();
9670}
9671
9672
9673void StringCompareStub::Generate(MacroAssembler* masm) {
9674 Label runtime;
9675
9676 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00009677 // sp[0]: right string
9678 // sp[4]: left string
9679 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
9680 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009681
9682 Label not_same;
9683 __ cmp(r0, r1);
9684 __ b(ne, &not_same);
9685 ASSERT_EQ(0, EQUAL);
9686 ASSERT_EQ(0, kSmiTag);
9687 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
9688 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
9689 __ add(sp, sp, Operand(2 * kPointerSize));
9690 __ Ret();
9691
9692 __ bind(&not_same);
9693
9694 // Check that both objects are sequential ascii strings.
9695 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
9696
9697 // Compare flat ascii strings natively. Remove arguments from stack first.
9698 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
9699 __ add(sp, sp, Operand(2 * kPointerSize));
9700 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
9701
9702 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
9703 // tagged as a small integer.
9704 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009705 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009706}
9707
9708
ager@chromium.org5c838252010-02-19 08:53:10 +00009709void StringAddStub::Generate(MacroAssembler* masm) {
9710 Label string_add_runtime;
9711 // Stack on entry:
9712 // sp[0]: second argument.
9713 // sp[4]: first argument.
9714
9715 // Load the two arguments.
9716 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
9717 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
9718
9719 // Make sure that both arguments are strings if not known in advance.
9720 if (string_check_) {
9721 ASSERT_EQ(0, kSmiTag);
9722 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
9723 // Load instance types.
9724 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9725 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9726 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9727 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9728 ASSERT_EQ(0, kStringTag);
9729 // If either is not a string, go to runtime.
9730 __ tst(r4, Operand(kIsNotStringMask));
9731 __ tst(r5, Operand(kIsNotStringMask), eq);
9732 __ b(ne, &string_add_runtime);
9733 }
9734
9735 // Both arguments are strings.
9736 // r0: first string
9737 // r1: second string
9738 // r4: first string instance type (if string_check_)
9739 // r5: second string instance type (if string_check_)
9740 {
9741 Label strings_not_empty;
9742 // Check if either of the strings are empty. In that case return the other.
9743 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
9744 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009745 ASSERT(kSmiTag == 0);
9746 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +00009747 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009748 ASSERT(kSmiTag == 0);
9749 // Else test if second string is empty.
9750 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +00009751 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
9752
9753 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9754 __ add(sp, sp, Operand(2 * kPointerSize));
9755 __ Ret();
9756
9757 __ bind(&strings_not_empty);
9758 }
9759
ager@chromium.orgac091b72010-05-05 07:34:42 +00009760 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
9761 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009762 // Both strings are non-empty.
9763 // r0: first string
9764 // r1: second string
9765 // r2: length of first string
9766 // r3: length of second string
9767 // r4: first string instance type (if string_check_)
9768 // r5: second string instance type (if string_check_)
9769 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009770 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00009771 // Adding two lengths can't overflow.
9772 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
9773 __ add(r6, r2, Operand(r3));
9774 // Use the runtime system when adding two one character strings, as it
9775 // contains optimizations for this specific case using the symbol table.
9776 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009777 __ b(ne, &longer_than_two);
9778
9779 // Check that both strings are non-external ascii strings.
9780 if (!string_check_) {
9781 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9782 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9783 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9784 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9785 }
9786 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
9787 &string_add_runtime);
9788
9789 // Get the two characters forming the sub string.
9790 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9791 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
9792
9793 // Try to lookup two character string in symbol table. If it is not found
9794 // just allocate a new one.
9795 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009796 StringHelper::GenerateTwoCharacterSymbolTableProbe(
9797 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009798 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9799 __ add(sp, sp, Operand(2 * kPointerSize));
9800 __ Ret();
9801
9802 __ bind(&make_two_character_string);
9803 // Resulting string has length 2 and first chars of two strings
9804 // are combined into single halfword in r2 register.
9805 // So we can fill resulting string without two loops by a single
9806 // halfword store instruction (which assumes that processor is
9807 // in a little endian mode)
9808 __ mov(r6, Operand(2));
9809 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
9810 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
9811 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9812 __ add(sp, sp, Operand(2 * kPointerSize));
9813 __ Ret();
9814
9815 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00009816 // Check if resulting string will be flat.
9817 __ cmp(r6, Operand(String::kMinNonFlatLength));
9818 __ b(lt, &string_add_flat_result);
9819 // Handle exceptionally long strings in the runtime system.
9820 ASSERT((String::kMaxLength & 0x80000000) == 0);
9821 ASSERT(IsPowerOf2(String::kMaxLength + 1));
9822 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
9823 __ cmp(r6, Operand(String::kMaxLength + 1));
9824 __ b(hs, &string_add_runtime);
9825
9826 // If result is not supposed to be flat, allocate a cons string object.
9827 // If both strings are ascii the result is an ascii cons string.
9828 if (!string_check_) {
9829 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9830 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9831 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9832 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9833 }
9834 Label non_ascii, allocated;
9835 ASSERT_EQ(0, kTwoByteStringTag);
9836 __ tst(r4, Operand(kStringEncodingMask));
9837 __ tst(r5, Operand(kStringEncodingMask), ne);
9838 __ b(eq, &non_ascii);
9839
9840 // Allocate an ASCII cons string.
9841 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
9842 __ bind(&allocated);
9843 // Fill the fields of the cons string.
9844 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
9845 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
9846 __ mov(r0, Operand(r7));
9847 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9848 __ add(sp, sp, Operand(2 * kPointerSize));
9849 __ Ret();
9850
9851 __ bind(&non_ascii);
9852 // Allocate a two byte cons string.
9853 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
9854 __ jmp(&allocated);
9855
9856 // Handle creating a flat result. First check that both strings are
9857 // sequential and that they have the same encoding.
9858 // r0: first string
9859 // r1: second string
9860 // r2: length of first string
9861 // r3: length of second string
9862 // r4: first string instance type (if string_check_)
9863 // r5: second string instance type (if string_check_)
9864 // r6: sum of lengths.
9865 __ bind(&string_add_flat_result);
9866 if (!string_check_) {
9867 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
9868 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
9869 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
9870 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
9871 }
9872 // Check that both strings are sequential.
9873 ASSERT_EQ(0, kSeqStringTag);
9874 __ tst(r4, Operand(kStringRepresentationMask));
9875 __ tst(r5, Operand(kStringRepresentationMask), eq);
9876 __ b(ne, &string_add_runtime);
9877 // Now check if both strings have the same encoding (ASCII/Two-byte).
9878 // r0: first string.
9879 // r1: second string.
9880 // r2: length of first string.
9881 // r3: length of second string.
9882 // r6: sum of lengths..
9883 Label non_ascii_string_add_flat_result;
9884 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
9885 __ eor(r7, r4, Operand(r5));
9886 __ tst(r7, Operand(kStringEncodingMask));
9887 __ b(ne, &string_add_runtime);
9888 // And see if it's ASCII or two-byte.
9889 __ tst(r4, Operand(kStringEncodingMask));
9890 __ b(eq, &non_ascii_string_add_flat_result);
9891
9892 // Both strings are sequential ASCII strings. We also know that they are
9893 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009894 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00009895 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
9896 // Locate first character of result.
9897 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9898 // Locate first character of first argument.
9899 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9900 // r0: first character of first string.
9901 // r1: second string.
9902 // r2: length of first string.
9903 // r3: length of second string.
9904 // r6: first character of result.
9905 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009906 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +00009907
9908 // Load second argument and locate first character.
9909 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9910 // r1: first character of second string.
9911 // r3: length of second string.
9912 // r6: next character of result.
9913 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009914 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +00009915 __ mov(r0, Operand(r7));
9916 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9917 __ add(sp, sp, Operand(2 * kPointerSize));
9918 __ Ret();
9919
9920 __ bind(&non_ascii_string_add_flat_result);
9921 // Both strings are sequential two byte strings.
9922 // r0: first string.
9923 // r1: second string.
9924 // r2: length of first string.
9925 // r3: length of second string.
9926 // r6: sum of length of strings.
9927 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
9928 // r0: first string.
9929 // r1: second string.
9930 // r2: length of first string.
9931 // r3: length of second string.
9932 // r7: result string.
9933
9934 // Locate first character of result.
9935 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9936 // Locate first character of first argument.
9937 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9938
9939 // r0: first character of first string.
9940 // r1: second string.
9941 // r2: length of first string.
9942 // r3: length of second string.
9943 // r6: first character of result.
9944 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009945 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +00009946
9947 // Locate first character of second argument.
9948 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
9949
9950 // r1: first character of second string.
9951 // r3: length of second string.
9952 // r6: next character of result (after copy of first string).
9953 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009954 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +00009955
9956 __ mov(r0, Operand(r7));
9957 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
9958 __ add(sp, sp, Operand(2 * kPointerSize));
9959 __ Ret();
9960
9961 // Just jump to runtime to add the two strings.
9962 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009963 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009964}
9965
9966
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009967#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009968
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00009969} } // namespace v8::internal