blob: 4bcf1a07df9c990c01d1ec413758011b0ce0d5de [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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000034#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "debug.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000036#include "ic-inl.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000037#include "jsregexp.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000038#include "jump-target-light-inl.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000039#include "parser.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000040#include "regexp-macro-assembler.h"
41#include "regexp-stack.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000042#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000044#include "scopes.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000045#include "virtual-frame-inl.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000046#include "virtual-frame-arm-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000051
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000052static void EmitIdenticalObjectComparison(MacroAssembler* masm,
53 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000054 Condition cc,
55 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000056static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +000057 Register lhs,
58 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000059 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000060 Label* slow,
61 bool strict);
62static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
ager@chromium.orgb5737492010-07-15 09:29:43 +000063static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
64 Register lhs,
65 Register rhs);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000066static void MultiplyByKnownInt(MacroAssembler* masm,
67 Register source,
68 Register destination,
69 int known_int);
70static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000071
72
ricow@chromium.org30ce4112010-05-31 10:38:25 +000073#define __ ACCESS_MASM(masm_)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000074
ager@chromium.orge2902be2009-06-08 12:21:35 +000075// -------------------------------------------------------------------------
76// Platform-specific DeferredCode functions.
77
78void DeferredCode::SaveRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000079 // On ARM you either have a completely spilled frame or you
80 // handle it yourself, but at the moment there's no automation
81 // of registers and deferred code.
ager@chromium.orge2902be2009-06-08 12:21:35 +000082}
83
84
85void DeferredCode::RestoreRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000086}
87
88
89// -------------------------------------------------------------------------
90// Platform-specific RuntimeCallHelper functions.
91
92void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
93 frame_state_->frame()->AssertIsSpilled();
94}
95
96
97void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
98}
99
100
101void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
102 masm->EnterInternalFrame();
103}
104
105
106void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
107 masm->LeaveInternalFrame();
ager@chromium.orge2902be2009-06-08 12:21:35 +0000108}
109
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000110
111// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000112// CodeGenState implementation.
113
ager@chromium.org7c537e22008-10-16 08:43:32 +0000114CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000115 : owner_(owner),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000116 previous_(owner->state()) {
117 owner->set_state(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000118}
119
120
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000121ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner,
122 JumpTarget* true_target,
123 JumpTarget* false_target)
124 : CodeGenState(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000125 true_target_(true_target),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000126 false_target_(false_target) {
127 owner->set_state(this);
128}
129
130
131TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner,
132 Slot* slot,
133 TypeInfo type_info)
134 : CodeGenState(owner),
135 slot_(slot) {
136 owner->set_state(this);
137 old_type_info_ = owner->set_type_info(slot, type_info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000138}
139
140
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000141CodeGenState::~CodeGenState() {
142 ASSERT(owner_->state() == this);
143 owner_->set_state(previous_);
144}
145
146
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000147TypeInfoCodeGenState::~TypeInfoCodeGenState() {
148 owner()->set_type_info(slot_, old_type_info_);
149}
150
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000151// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000152// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000154int CodeGenerator::inlined_write_barrier_size_ = -1;
155
ager@chromium.org5c838252010-02-19 08:53:10 +0000156CodeGenerator::CodeGenerator(MacroAssembler* masm)
157 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000158 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000159 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000160 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000161 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 cc_reg_(al),
163 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000164 loop_nesting_(0),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000165 type_info_(NULL),
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000166 function_return_(JumpTarget::BIDIRECTIONAL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000167 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168}
169
170
171// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000172// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175// cp: callee's context
176
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000177void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000178 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000180 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
182 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000183 info_ = info;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000184
185 int slots = scope()->num_parameters() + scope()->num_stack_slots();
186 ScopedVector<TypeInfo> type_info_array(slots);
187 type_info_ = &type_info_array;
188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000189 ASSERT(allocator_ == NULL);
190 RegisterAllocator register_allocator(this);
191 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000192 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000193 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000194 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000195
196 // Adjust for function-level loop nesting.
197 ASSERT_EQ(0, loop_nesting_);
198 loop_nesting_ = info->loop_nesting();
199
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000200 {
201 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000203 // Entry:
204 // Stack: receiver, arguments
205 // lr: return address
206 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000208 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000210 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212#ifdef DEBUG
213 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000214 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000215 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000216 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 }
218#endif
219
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000220 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000221 frame_->Enter();
222 // tos: code slot
223
224 // Allocate space for locals and initialize them. This also checks
225 // for stack overflow.
226 frame_->AllocateStackSlots();
227
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000228 frame_->AssertIsSpilled();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000229 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000230 if (heap_slots > 0) {
231 // Allocate local context.
232 // Get outer context and create a new context based on it.
233 __ ldr(r0, frame_->Function());
234 frame_->EmitPush(r0);
235 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
236 FastNewContextStub stub(heap_slots);
237 frame_->CallStub(&stub, 1);
238 } else {
239 frame_->CallRuntime(Runtime::kNewContext, 1);
240 }
241
242#ifdef DEBUG
243 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000244 __ cmp(r0, cp);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000245 verified_true.Branch(eq);
246 __ stop("NewContext: r0 is expected to be the same as cp");
247 verified_true.Bind();
248#endif
249 // Update context local.
250 __ str(cp, frame_->Context());
251 }
252
253 // TODO(1241774): Improve this code:
254 // 1) only needed if we have a context
255 // 2) no need to recompute context ptr every single time
256 // 3) don't copy parameter operand code from SlotOperand!
257 {
258 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000259 // Note that iteration order is relevant here! If we have the same
260 // parameter twice (e.g., function (x, y, x)), and that parameter
261 // needs to be copied into the context, it must be the last argument
262 // passed to the parameter that needs to be copied. This is a rare
263 // case so we don't check for it, instead we rely on the copying
264 // order: such a parameter is copied repeatedly into the same
265 // context location and thus the last value is what is seen inside
266 // the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000267 frame_->AssertIsSpilled();
ager@chromium.org5c838252010-02-19 08:53:10 +0000268 for (int i = 0; i < scope()->num_parameters(); i++) {
269 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000270 Slot* slot = par->slot();
271 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000272 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000273 __ ldr(r1, frame_->ParameterAt(i));
274 // Loads r2 with context; used below in RecordWrite.
275 __ str(r1, SlotOperand(slot, r2));
276 // Load the offset into r3.
277 int slot_offset =
278 FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000279 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 }
281 }
282 }
283
284 // Store the arguments object. This must happen after context
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000285 // initialization because the arguments object may be stored in
286 // the context.
287 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
288 StoreArgumentsObject(true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289 }
290
291 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000292 if (scope()->is_function_scope() && scope()->function() != NULL) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000293 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
ager@chromium.org5c838252010-02-19 08:53:10 +0000294 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000295 }
296 } else {
297 // When used as the secondary compiler for splitting, r1, cp,
298 // fp, and lr have been pushed on the stack. Adjust the virtual
299 // frame to match this state.
300 frame_->Adjust(4);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000301
302 // Bind all the bailout labels to the beginning of the function.
303 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
304 for (int i = 0; i < bailouts->length(); i++) {
305 __ bind(bailouts->at(i)->label());
306 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000307 }
308
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000309 // Initialize the function return target after the locals are set
310 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000311 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000312 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000314 // Generate code to 'execute' declarations and initialize functions
315 // (source elements). In case of an illegal redeclaration we need to
316 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000317 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000319 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 } else {
321 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000322 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000323 // Bail out if a stack-overflow exception occurred when processing
324 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000325 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 }
327
mads.s.ager31e71382008-08-13 09:32:07 +0000328 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000330 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332
333 // Compile the body of the function in a vanilla state. Don't
334 // bother compiling all the code if the scope has an illegal
335 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000336 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 Comment cmnt(masm_, "[ function body");
338#ifdef DEBUG
339 bool is_builtin = Bootstrapper::IsActive();
340 bool should_trace =
341 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000342 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000343 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000344 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000347 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 }
350
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000351 // Handle the return from the function.
352 if (has_valid_frame()) {
353 // If there is a valid frame, control flow can fall off the end of
354 // the body. In that case there is an implicit return statement.
355 ASSERT(!function_return_is_shadowed_);
356 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000357 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000358 if (function_return_.is_bound()) {
359 function_return_.Jump();
360 } else {
361 function_return_.Bind();
362 GenerateReturnSequence();
363 }
364 } else if (function_return_.is_linked()) {
365 // If the return target has dangling jumps to it, then we have not
366 // yet generated the return sequence. This can happen when (a)
367 // control does not flow off the end of the body so we did not
368 // compile an artificial return statement just above, and (b) there
369 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000370 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000371 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000372 }
373
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000374 // Adjust for function-level loop nesting.
375 ASSERT(loop_nesting_ == info->loop_nesting());
376 loop_nesting_ = 0;
377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 ASSERT(!has_cc());
380 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000381 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000382 ASSERT(!function_return_is_shadowed_);
383 function_return_.Unuse();
384 DeleteFrame();
385
386 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000387 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000388 ProcessDeferred();
389 }
390
391 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000392 type_info_ = NULL;
393}
394
395
396int CodeGenerator::NumberOfSlot(Slot* slot) {
397 if (slot == NULL) return kInvalidSlotNumber;
398 switch (slot->type()) {
399 case Slot::PARAMETER:
400 return slot->index();
401 case Slot::LOCAL:
402 return slot->index() + scope()->num_parameters();
403 default:
404 break;
405 }
406 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407}
408
409
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
411 // Currently, this assertion will fail if we try to assign to
412 // a constant variable that is constant because it is read-only
413 // (such as the variable referring to a named function expression).
414 // We need to implement assignments to read-only variables.
415 // Ideally, we should do this during AST generation (by converting
416 // such assignments into expression statements); however, in general
417 // we may not be able to make the decision until past AST generation,
418 // that is when the entire program is known.
419 ASSERT(slot != NULL);
420 int index = slot->index();
421 switch (slot->type()) {
422 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000423 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000424
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000425 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000426 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000427
428 case Slot::CONTEXT: {
429 // Follow the context chain if necessary.
430 ASSERT(!tmp.is(cp)); // do not overwrite context register
431 Register context = cp;
432 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000433 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000434 // Load the closure.
435 // (All contexts, even 'with' contexts, have a closure,
436 // and it is the same for all contexts inside a function.
437 // There is no need to go to the function context first.)
438 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
439 // Load the function context (which is the incoming, outer context).
440 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
441 context = tmp;
442 }
443 // We may have a 'with' context now. Get the function context.
444 // (In fact this mov may never be the needed, since the scope analysis
445 // may not permit a direct context access in this case and thus we are
446 // always at a function context. However it is safe to dereference be-
447 // cause the function context of a function context is itself. Before
448 // deleting this mov we should try to create a counter-example first,
449 // though...)
450 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
451 return ContextOperand(tmp, index);
452 }
453
454 default:
455 UNREACHABLE();
456 return MemOperand(r0, 0);
457 }
458}
459
460
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
462 Slot* slot,
463 Register tmp,
464 Register tmp2,
465 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000466 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000467 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468
ager@chromium.org381abbb2009-02-25 13:23:22 +0000469 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
470 if (s->num_heap_slots() > 0) {
471 if (s->calls_eval()) {
472 // Check that extension is NULL.
473 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
474 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000476 }
477 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
478 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
479 context = tmp;
480 }
481 }
482 // Check that last extension is NULL.
483 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
484 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000486 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000487 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000488}
489
490
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000491// Loads a value on TOS. If it is a boolean value, the result may have been
492// (partially) translated into branches, or it may have set the condition
493// code register. If force_cc is set, the value is forced to set the
494// condition code register and no value is pushed. If the condition code
495// register was set, has_cc() is true and cc_reg_ contains the condition to
496// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000497void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498 JumpTarget* true_target,
499 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000500 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000501 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000504 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000505 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506
507 // If we hit a stack overflow, we may not have actually visited
508 // the expression. In that case, we ensure that we have a
509 // valid-looking frame state because we will continue to generate
510 // code as we unwind the C++ stack.
511 //
512 // It's possible to have both a stack overflow and a valid frame
513 // state (eg, a subexpression overflowed, visiting it returned
514 // with a dummied frame state, and visiting this expression
515 // returned with a normal-looking state).
516 if (HasStackOverflow() &&
517 has_valid_frame() &&
518 !has_cc() &&
519 frame_->height() == original_height) {
520 true_target->Jump();
521 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000522 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000524 // Convert the TOS value to a boolean in the condition code register.
525 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527 ASSERT(!force_cc || !has_valid_frame() || has_cc());
528 ASSERT(!has_valid_frame() ||
529 (has_cc() && frame_->height() == original_height) ||
530 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531}
532
533
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000534void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535#ifdef DEBUG
536 int original_height = frame_->height();
537#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000538 JumpTarget true_target;
539 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000540 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541
542 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000544 JumpTarget loaded;
545 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 materialize_true.Branch(cc_reg_);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000547 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000548 loaded.Jump();
549 materialize_true.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000550 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000551 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 cc_reg_ = al;
553 }
554
555 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 // We have at least one condition value that has been "translated"
557 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000558 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000559 if (frame_ != NULL) {
560 loaded.Jump(); // Don't lose the current TOS.
561 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000565 true_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000566 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 // If both "true" and "false" need to be loaded jump across the code for
569 // "false".
570 if (both) {
571 loaded.Jump();
572 }
573 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000575 false_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000576 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000578 // A value is loaded on all paths reaching this point.
579 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000581 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 ASSERT(!has_cc());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000583 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584}
585
586
ager@chromium.org7c537e22008-10-16 08:43:32 +0000587void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000588 Register reg = frame_->GetTOSRegister();
589 __ ldr(reg, GlobalObject());
590 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591}
592
593
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000594void CodeGenerator::LoadGlobalReceiver(Register scratch) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000595 Register reg = frame_->GetTOSRegister();
596 __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX));
597 __ ldr(reg,
598 FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset));
599 frame_->EmitPush(reg);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000600}
601
602
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000603ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
604 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
605 ASSERT(scope()->arguments_shadow() != NULL);
606 // We don't want to do lazy arguments allocation for functions that
607 // have heap-allocated contexts, because it interfers with the
608 // uninitialized const tracking in the context objects.
609 return (scope()->num_heap_slots() > 0)
610 ? EAGER_ARGUMENTS_ALLOCATION
611 : LAZY_ARGUMENTS_ALLOCATION;
612}
613
614
615void CodeGenerator::StoreArgumentsObject(bool initial) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000616 ArgumentsAllocationMode mode = ArgumentsMode();
617 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
618
619 Comment cmnt(masm_, "[ store arguments object");
620 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
621 // When using lazy arguments allocation, we store the hole value
622 // as a sentinel indicating that the arguments object hasn't been
623 // allocated yet.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000624 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000625 } else {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000626 frame_->SpillAll();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000627 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
628 __ ldr(r2, frame_->Function());
629 // The receiver is below the arguments, the return address, and the
630 // frame pointer on the stack.
631 const int kReceiverDisplacement = 2 + scope()->num_parameters();
632 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
633 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
634 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000635 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000636 frame_->CallStub(&stub, 3);
637 frame_->EmitPush(r0);
638 }
639
640 Variable* arguments = scope()->arguments()->var();
641 Variable* shadow = scope()->arguments_shadow()->var();
642 ASSERT(arguments != NULL && arguments->slot() != NULL);
643 ASSERT(shadow != NULL && shadow->slot() != NULL);
644 JumpTarget done;
645 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
646 // We have to skip storing into the arguments slot if it has
647 // already been written to. This can happen if the a function
648 // has a local variable named 'arguments'.
649 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000650 Register arguments = frame_->PopToRegister();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000651 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000652 __ cmp(arguments, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000653 done.Branch(ne);
654 }
655 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
656 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
657 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
658}
659
660
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000661void CodeGenerator::LoadTypeofExpression(Expression* expr) {
662 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000663 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000665 // For a global variable we build the property reference
666 // <global>.<variable> and perform a (regular non-contextual) property
667 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
669 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000670 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000671 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000672 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000673 } else if (variable != NULL && variable->slot() != NULL) {
674 // For a variable that rewrites to a slot, we signal it is the immediate
675 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000676 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000678 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000679 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 }
681}
682
683
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000684Reference::Reference(CodeGenerator* cgen,
685 Expression* expression,
686 bool persist_after_get)
687 : cgen_(cgen),
688 expression_(expression),
689 type_(ILLEGAL),
690 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 cgen->LoadReference(this);
692}
693
694
695Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000696 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697}
698
699
ager@chromium.org7c537e22008-10-16 08:43:32 +0000700void CodeGenerator::LoadReference(Reference* ref) {
701 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 Expression* e = ref->expression();
703 Property* property = e->AsProperty();
704 Variable* var = e->AsVariableProxy()->AsVariable();
705
706 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000707 // The expression is either a property or a variable proxy that rewrites
708 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000709 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000710 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 ref->set_type(Reference::NAMED);
712 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000713 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 ref->set_type(Reference::KEYED);
715 }
716 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000717 // The expression is a variable proxy that does not rewrite to a
718 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 LoadGlobal();
721 ref->set_type(Reference::NAMED);
722 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000723 ASSERT(var->slot() != NULL);
724 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 }
726 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000727 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000728 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000729 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 }
731}
732
733
ager@chromium.org7c537e22008-10-16 08:43:32 +0000734void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000736 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000737 if (size == 0) return;
738
739 // Pop a reference from the stack while preserving TOS.
740 VirtualFrame::RegisterAllocationScope scope(this);
741 Comment cmnt(masm_, "[ UnloadReference");
742 if (size > 0) {
743 Register tos = frame_->PopToRegister();
744 frame_->Drop(size);
745 frame_->EmitPush(tos);
746 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747}
748
749
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
751// register to a boolean in the condition code register. The code
752// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753void CodeGenerator::ToBoolean(JumpTarget* true_target,
754 JumpTarget* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000755 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 // Only the condition code should be set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000757 bool known_smi = frame_->KnownSmiAt(0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000758 Register tos = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759
760 // Fast case checks
761
mads.s.ager31e71382008-08-13 09:32:07 +0000762 // Check if the value is 'false'.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000763 if (!known_smi) {
764 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
765 __ cmp(tos, ip);
766 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000768 // Check if the value is 'true'.
769 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
770 __ cmp(tos, ip);
771 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000773 // Check if the value is 'undefined'.
774 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
775 __ cmp(tos, ip);
776 false_target->Branch(eq);
777 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778
mads.s.ager31e71382008-08-13 09:32:07 +0000779 // Check if the value is a smi.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000780 __ cmp(tos, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000782 if (!known_smi) {
783 false_target->Branch(eq);
784 __ tst(tos, Operand(kSmiTagMask));
785 true_target->Branch(eq);
786
787 // Slow case: call the runtime.
788 frame_->EmitPush(tos);
789 frame_->CallRuntime(Runtime::kToBool, 1);
790 // Convert the result (r0) to a condition code.
791 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
792 __ cmp(r0, ip);
793 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
795 cc_reg_ = ne;
796}
797
798
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000799void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000800 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000801 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000802 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000803 // top of virtual frame: y
804 // 2nd elt. on virtual frame : x
805 // result : top of virtual frame
806
807 // Stub is entered with a call: 'return address' is in lr.
808 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000809 case Token::ADD:
810 case Token::SUB:
811 if (inline_smi) {
812 JumpTarget done;
813 Register rhs = frame_->PopToRegister();
814 Register lhs = frame_->PopToRegister(rhs);
815 Register scratch = VirtualFrame::scratch0();
816 __ orr(scratch, rhs, Operand(lhs));
817 // Check they are both small and positive.
818 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
819 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000820 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000821 if (op == Token::ADD) {
822 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
823 } else {
824 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
825 }
826 done.Branch(eq);
827 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
828 frame_->SpillAll();
829 frame_->CallStub(&stub, 0);
830 done.Bind();
831 frame_->EmitPush(r0);
832 break;
833 } else {
834 // Fall through!
835 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000836 case Token::BIT_OR:
837 case Token::BIT_AND:
838 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000839 if (inline_smi) {
840 bool rhs_is_smi = frame_->KnownSmiAt(0);
841 bool lhs_is_smi = frame_->KnownSmiAt(1);
842 Register rhs = frame_->PopToRegister();
843 Register lhs = frame_->PopToRegister(rhs);
844 Register smi_test_reg;
845 Condition cond;
846 if (!rhs_is_smi || !lhs_is_smi) {
847 if (rhs_is_smi) {
848 smi_test_reg = lhs;
849 } else if (lhs_is_smi) {
850 smi_test_reg = rhs;
851 } else {
852 smi_test_reg = VirtualFrame::scratch0();
853 __ orr(smi_test_reg, rhs, Operand(lhs));
854 }
855 // Check they are both Smis.
856 __ tst(smi_test_reg, Operand(kSmiTagMask));
857 cond = eq;
858 } else {
859 cond = al;
860 }
861 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
862 if (op == Token::BIT_OR) {
863 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
864 } else if (op == Token::BIT_AND) {
865 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
866 } else {
867 ASSERT(op == Token::BIT_XOR);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000868 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000869 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
870 }
871 if (cond != al) {
872 JumpTarget done;
873 done.Branch(cond);
874 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
875 frame_->SpillAll();
876 frame_->CallStub(&stub, 0);
877 done.Bind();
878 }
879 frame_->EmitPush(r0);
880 break;
881 } else {
882 // Fall through!
883 }
884 case Token::MUL:
885 case Token::DIV:
886 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000887 case Token::SHL:
888 case Token::SHR:
889 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000890 Register rhs = frame_->PopToRegister();
891 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000892 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
893 frame_->SpillAll();
894 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000895 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 break;
897 }
898
ager@chromium.org357bf652010-04-12 11:30:10 +0000899 case Token::COMMA: {
900 Register scratch = frame_->PopToRegister();
901 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000903 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000905 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
907 default:
908 // Other cases should have been handled before this point.
909 UNREACHABLE();
910 break;
911 }
912}
913
914
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000915class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000916 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000917 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000918 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000919 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000920 OverwriteMode overwrite_mode,
921 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000922 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000923 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000924 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000925 overwrite_mode_(overwrite_mode),
926 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000927 set_comment("[ DeferredInlinedSmiOperation");
928 }
929
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000930 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000931
932 private:
933 Token::Value op_;
934 int value_;
935 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000936 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000937 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000938};
939
940
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000941
942// On entry the non-constant side of the binary operation is in tos_register_
943// and the constant smi side is nowhere. The tos_register_ is not used by the
944// virtual frame. On exit the answer is in the tos_register_ and the virtual
945// frame is unchanged.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000946void DeferredInlineSmiOperation::Generate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000947 VirtualFrame copied_frame(*frame_state()->frame());
948 copied_frame.SpillAll();
949
ager@chromium.org357bf652010-04-12 11:30:10 +0000950 Register lhs = r1;
951 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000952 switch (op_) {
953 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000954 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000955 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000956 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000957 __ mov(r1, Operand(Smi::FromInt(value_)));
958 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000959 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000960 __ mov(r0, Operand(Smi::FromInt(value_)));
961 }
962 break;
963 }
964
965 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000966 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000967 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000968 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000969 __ mov(r1, Operand(Smi::FromInt(value_)));
970 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000971 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000972 __ mov(r0, Operand(Smi::FromInt(value_)));
973 }
974 break;
975 }
976
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000977 // For these operations there is no optimistic operation that needs to be
978 // reverted.
979 case Token::MUL:
980 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000981 case Token::BIT_OR:
982 case Token::BIT_XOR:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000983 case Token::BIT_AND:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000984 case Token::SHL:
985 case Token::SHR:
986 case Token::SAR: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000987 if (tos_register_.is(r1)) {
988 __ mov(r0, Operand(Smi::FromInt(value_)));
989 } else {
990 ASSERT(tos_register_.is(r0));
991 __ mov(r1, Operand(Smi::FromInt(value_)));
992 }
993 if (reversed_ == tos_register_.is(r1)) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000994 lhs = r0;
995 rhs = r1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000996 }
997 break;
998 }
999
1000 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +00001001 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001002 UNREACHABLE();
1003 break;
1004 }
1005
ager@chromium.org357bf652010-04-12 11:30:10 +00001006 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001007 __ CallStub(&stub);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001008
ager@chromium.org357bf652010-04-12 11:30:10 +00001009 // The generic stub returns its value in r0, but that's not
1010 // necessarily what we want. We want whatever the inlined code
1011 // expected, which is that the answer is in the same register as
1012 // the operand was.
1013 __ Move(tos_register_, r0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001014
1015 // The tos register was not in use for the virtual frame that we
1016 // came into this function with, so we can merge back to that frame
1017 // without trashing it.
1018 copied_frame.MergeTo(frame_state()->frame());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001019}
1020
1021
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001022static bool PopCountLessThanEqual2(unsigned int x) {
1023 x &= x - 1;
1024 return (x & (x - 1)) == 0;
1025}
1026
1027
1028// Returns the index of the lowest bit set.
1029static int BitPosition(unsigned x) {
1030 int bit_posn = 0;
1031 while ((x & 0xf) == 0) {
1032 bit_posn += 4;
1033 x >>= 4;
1034 }
1035 while ((x & 1) == 0) {
1036 bit_posn++;
1037 x >>= 1;
1038 }
1039 return bit_posn;
1040}
1041
1042
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001043void CodeGenerator::SmiOperation(Token::Value op,
1044 Handle<Object> value,
1045 bool reversed,
1046 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001047 int int_value = Smi::cast(*value)->value();
1048
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001049 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1050
ager@chromium.org357bf652010-04-12 11:30:10 +00001051 bool something_to_inline;
1052 switch (op) {
1053 case Token::ADD:
1054 case Token::SUB:
1055 case Token::BIT_AND:
1056 case Token::BIT_OR:
1057 case Token::BIT_XOR: {
1058 something_to_inline = true;
1059 break;
1060 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001061 case Token::SHL: {
1062 something_to_inline = (both_sides_are_smi || !reversed);
1063 break;
1064 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001065 case Token::SHR:
1066 case Token::SAR: {
1067 if (reversed) {
1068 something_to_inline = false;
1069 } else {
1070 something_to_inline = true;
1071 }
1072 break;
1073 }
1074 case Token::MOD: {
1075 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1076 something_to_inline = false;
1077 } else {
1078 something_to_inline = true;
1079 }
1080 break;
1081 }
1082 case Token::MUL: {
1083 if (!IsEasyToMultiplyBy(int_value)) {
1084 something_to_inline = false;
1085 } else {
1086 something_to_inline = true;
1087 }
1088 break;
1089 }
1090 default: {
1091 something_to_inline = false;
1092 break;
1093 }
1094 }
1095
1096 if (!something_to_inline) {
1097 if (!reversed) {
1098 // Push the rhs onto the virtual frame by putting it in a TOS register.
1099 Register rhs = frame_->GetTOSRegister();
1100 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001101 frame_->EmitPush(rhs, TypeInfo::Smi());
1102 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001103 } else {
1104 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1105 // at most one pop, the rest takes place in TOS registers.
1106 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1107 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1108 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001109 frame_->EmitPush(lhs, TypeInfo::Smi());
1110 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1111 frame_->EmitPush(rhs, t);
1112 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001113 }
1114 return;
1115 }
1116
1117 // We move the top of stack to a register (normally no move is invoved).
1118 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001119 switch (op) {
1120 case Token::ADD: {
1121 DeferredCode* deferred =
1122 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1123
1124 __ add(tos, tos, Operand(value), SetCC);
1125 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001126 if (!both_sides_are_smi) {
1127 __ tst(tos, Operand(kSmiTagMask));
1128 deferred->Branch(ne);
1129 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001130 deferred->BindExit();
1131 frame_->EmitPush(tos);
1132 break;
1133 }
1134
1135 case Token::SUB: {
1136 DeferredCode* deferred =
1137 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1138
1139 if (reversed) {
1140 __ rsb(tos, tos, Operand(value), SetCC);
1141 } else {
1142 __ sub(tos, tos, Operand(value), SetCC);
1143 }
1144 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001145 if (!both_sides_are_smi) {
1146 __ tst(tos, Operand(kSmiTagMask));
1147 deferred->Branch(ne);
1148 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001149 deferred->BindExit();
1150 frame_->EmitPush(tos);
1151 break;
1152 }
1153
1154
1155 case Token::BIT_OR:
1156 case Token::BIT_XOR:
1157 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001158 if (both_sides_are_smi) {
1159 switch (op) {
1160 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1161 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001162 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001163 default: UNREACHABLE();
1164 }
1165 frame_->EmitPush(tos, TypeInfo::Smi());
1166 } else {
1167 DeferredCode* deferred =
1168 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1169 __ tst(tos, Operand(kSmiTagMask));
1170 deferred->Branch(ne);
1171 switch (op) {
1172 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1173 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001174 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001175 default: UNREACHABLE();
1176 }
1177 deferred->BindExit();
1178 TypeInfo result_type =
1179 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1180 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001181 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001182 break;
1183 }
1184
1185 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001186 if (reversed) {
1187 ASSERT(both_sides_are_smi);
1188 int max_shift = 0;
1189 int max_result = int_value == 0 ? 1 : int_value;
1190 while (Smi::IsValid(max_result << 1)) {
1191 max_shift++;
1192 max_result <<= 1;
1193 }
1194 DeferredCode* deferred =
1195 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1196 // Mask off the last 5 bits of the shift operand (rhs). This is part
1197 // of the definition of shift in JS and we know we have a Smi so we
1198 // can safely do this. The masked version gets passed to the
1199 // deferred code, but that makes no difference.
1200 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1201 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1202 deferred->Branch(ge);
1203 Register scratch = VirtualFrame::scratch0();
1204 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1205 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1206 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1207 deferred->BindExit();
1208 TypeInfo result = TypeInfo::Integer32();
1209 frame_->EmitPush(tos, result);
1210 break;
1211 }
1212 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001213 case Token::SHR:
1214 case Token::SAR: {
1215 ASSERT(!reversed);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001216 TypeInfo result = TypeInfo::Integer32();
ager@chromium.org357bf652010-04-12 11:30:10 +00001217 Register scratch = VirtualFrame::scratch0();
1218 Register scratch2 = VirtualFrame::scratch1();
1219 int shift_value = int_value & 0x1f; // least significant 5 bits
1220 DeferredCode* deferred =
1221 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001222 uint32_t problematic_mask = kSmiTagMask;
1223 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001224 bool skip_smi_test = both_sides_are_smi;
1225 if (shift_value == 0 && op == Token::SHR) {
1226 problematic_mask |= 0x80000000;
1227 skip_smi_test = false;
1228 }
1229 if (!skip_smi_test) {
1230 __ tst(tos, Operand(problematic_mask));
1231 deferred->Branch(ne); // Go slow for problematic input.
1232 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001233 switch (op) {
1234 case Token::SHL: {
1235 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001236 int adjusted_shift = shift_value - kSmiTagSize;
1237 ASSERT(adjusted_shift >= 0);
1238 if (adjusted_shift != 0) {
1239 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1240 // Check that the *signed* result fits in a smi.
1241 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1242 deferred->Branch(mi);
1243 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1244 } else {
1245 // Check that the *signed* result fits in a smi.
1246 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1247 deferred->Branch(mi);
1248 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1249 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001250 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001251 break;
1252 }
1253 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001254 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001255 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1256 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001257 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001258 if (shift_value == 1) {
1259 // check that the *unsigned* result fits in a smi
1260 // neither of the two high-order bits can be set:
1261 // - 0x80000000: high bit would be lost when smi tagging
1262 // - 0x40000000: this number would convert to negative when
1263 // smi tagging these two cases can only happen with shifts
1264 // by 0 or 1 when handed a valid smi
1265 __ tst(scratch, Operand(0xc0000000));
1266 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001267 } else {
1268 ASSERT(shift_value >= 2);
1269 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001270 }
1271 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001272 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001273 break;
1274 }
1275 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001276 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1277 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001278 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001279 // Do the shift and the tag removal in one operation. If the shift
1280 // is 31 bits (the highest possible value) then we emit the
1281 // instruction as a shift by 0 which means shift arithmetically by
1282 // 32.
1283 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1284 // Put tag back.
1285 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001286 // SAR by at least 1 gives a Smi.
1287 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001288 }
1289 break;
1290 }
1291 default: UNREACHABLE();
1292 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001293 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001294 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001295 break;
1296 }
1297
1298 case Token::MOD: {
1299 ASSERT(!reversed);
1300 ASSERT(int_value >= 2);
1301 ASSERT(IsPowerOf2(int_value));
1302 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001303 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001304 unsigned mask = (0x80000000u | kSmiTagMask);
1305 __ tst(tos, Operand(mask));
1306 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1307 mask = (int_value << kSmiTagSize) - 1;
1308 __ and_(tos, tos, Operand(mask));
1309 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001310 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1311 frame_->EmitPush(
1312 tos,
1313 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001314 break;
1315 }
1316
1317 case Token::MUL: {
1318 ASSERT(IsEasyToMultiplyBy(int_value));
1319 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001320 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001321 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1322 max_smi_that_wont_overflow <<= kSmiTagSize;
1323 unsigned mask = 0x80000000u;
1324 while ((mask & max_smi_that_wont_overflow) == 0) {
1325 mask |= mask >> 1;
1326 }
1327 mask |= kSmiTagMask;
1328 // This does a single mask that checks for a too high value in a
1329 // conservative way and for a non-Smi. It also filters out negative
1330 // numbers, unfortunately, but since this code is inline we prefer
1331 // brevity to comprehensiveness.
1332 __ tst(tos, Operand(mask));
1333 deferred->Branch(ne);
1334 MultiplyByKnownInt(masm_, tos, tos, int_value);
1335 deferred->BindExit();
1336 frame_->EmitPush(tos);
1337 break;
1338 }
1339
1340 default:
1341 UNREACHABLE();
1342 break;
1343 }
1344}
1345
1346
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001347void CodeGenerator::Comparison(Condition cc,
1348 Expression* left,
1349 Expression* right,
1350 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001351 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001352
ager@chromium.org357bf652010-04-12 11:30:10 +00001353 if (left != NULL) Load(left);
1354 if (right != NULL) Load(right);
1355
mads.s.ager31e71382008-08-13 09:32:07 +00001356 // sp[0] : y
1357 // sp[1] : x
1358 // result : cc register
1359
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 // Strict only makes sense for equality comparisons.
1361 ASSERT(!strict || cc == eq);
1362
ager@chromium.org357bf652010-04-12 11:30:10 +00001363 Register lhs;
1364 Register rhs;
1365
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001366 bool lhs_is_smi;
1367 bool rhs_is_smi;
1368
ager@chromium.org357bf652010-04-12 11:30:10 +00001369 // We load the top two stack positions into registers chosen by the virtual
1370 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001371 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1372 if (cc == gt || cc == le) {
1373 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001374 lhs_is_smi = frame_->KnownSmiAt(0);
1375 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001376 lhs = frame_->PopToRegister();
1377 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001378 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001379 rhs_is_smi = frame_->KnownSmiAt(0);
1380 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001381 rhs = frame_->PopToRegister();
1382 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001383 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001384
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001385 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1386
ager@chromium.org357bf652010-04-12 11:30:10 +00001387 ASSERT(rhs.is(r0) || rhs.is(r1));
1388 ASSERT(lhs.is(r0) || lhs.is(r1));
1389
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001390 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001392 if (!both_sides_are_smi) {
1393 // Now we have the two sides in r0 and r1. We flush any other registers
1394 // because the stub doesn't know about register allocation.
1395 frame_->SpillAll();
1396 Register scratch = VirtualFrame::scratch0();
1397 Register smi_test_reg;
1398 if (lhs_is_smi) {
1399 smi_test_reg = rhs;
1400 } else if (rhs_is_smi) {
1401 smi_test_reg = lhs;
1402 } else {
1403 __ orr(scratch, lhs, Operand(rhs));
1404 smi_test_reg = scratch;
1405 }
1406 __ tst(smi_test_reg, Operand(kSmiTagMask));
1407 JumpTarget smi;
1408 smi.Branch(eq);
1409
1410 // Perform non-smi comparison by stub.
1411 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1412 // We call with 0 args because there are 0 on the stack.
ager@chromium.orgb5737492010-07-15 09:29:43 +00001413 CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001414 frame_->CallStub(&stub, 0);
1415 __ cmp(r0, Operand(0));
1416 exit.Jump();
1417
1418 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001419 }
1420
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001421 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001422 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 cc_reg_ = cc;
1426}
1427
1428
mads.s.ager31e71382008-08-13 09:32:07 +00001429// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001430void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001431 CallFunctionFlags flags,
1432 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 int arg_count = args->length();
1435 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001436 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001437 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438
kasper.lund7276f142008-07-30 08:49:36 +00001439 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001440 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
kasper.lund7276f142008-07-30 08:49:36 +00001442 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001443 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001444 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446
1447 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001448 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001449 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450}
1451
1452
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001453void CodeGenerator::CallApplyLazy(Expression* applicand,
1454 Expression* receiver,
1455 VariableProxy* arguments,
1456 int position) {
1457 // An optimized implementation of expressions of the form
1458 // x.apply(y, arguments).
1459 // If the arguments object of the scope has not been allocated,
1460 // and x.apply is Function.prototype.apply, this optimization
1461 // just copies y and the arguments of the current function on the
1462 // stack, as receiver and arguments, and calls x.
1463 // In the implementation comments, we call x the applicand
1464 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001465
1466 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1467 ASSERT(arguments->IsArguments());
1468
1469 // Load applicand.apply onto the stack. This will usually
1470 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001471 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001472 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001473 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001474 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001475 frame_->EmitPush(r0);
1476
1477 // Load the receiver and the existing arguments object onto the
1478 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001479 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001480 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1481
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001482 // At this point the top two stack elements are probably in registers
1483 // since they were just loaded. Ensure they are in regs and get the
1484 // regs.
1485 Register receiver_reg = frame_->Peek2();
1486 Register arguments_reg = frame_->Peek();
1487
1488 // From now on the frame is spilled.
1489 frame_->SpillAll();
1490
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001491 // Emit the source position information after having loaded the
1492 // receiver and the arguments.
1493 CodeForSourcePosition(position);
1494 // Contents of the stack at this point:
1495 // sp[0]: arguments object of the current function or the hole.
1496 // sp[1]: receiver
1497 // sp[2]: applicand.apply
1498 // sp[3]: applicand.
1499
1500 // Check if the arguments object has been lazily allocated
1501 // already. If so, just use that instead of copying the arguments
1502 // from the stack. This also deals with cases where a local variable
1503 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001504 JumpTarget slow;
1505 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001506 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001507 __ cmp(ip, arguments_reg);
1508 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001509
1510 Label build_args;
1511 // Get rid of the arguments object probe.
1512 frame_->Drop();
1513 // Stack now has 3 elements on it.
1514 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001515 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001516 // sp[1]: applicand.apply
1517 // sp[2]: applicand.
1518
1519 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001520 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001521 // We allow all JSObjects including JSFunctions. As long as
1522 // JS_FUNCTION_TYPE is the last instance type and it is right
1523 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1524 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001525 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1526 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001527 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001528 __ b(lt, &build_args);
1529
1530 // Check that applicand.apply is Function.prototype.apply.
1531 __ ldr(r0, MemOperand(sp, kPointerSize));
1532 __ BranchOnSmi(r0, &build_args);
1533 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1534 __ b(ne, &build_args);
1535 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
1536 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
1537 __ ldr(r1, FieldMemOperand(r0, SharedFunctionInfo::kCodeOffset));
1538 __ cmp(r1, Operand(apply_code));
1539 __ b(ne, &build_args);
1540
1541 // Check that applicand is a function.
1542 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1543 __ BranchOnSmi(r1, &build_args);
1544 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1545 __ b(ne, &build_args);
1546
1547 // Copy the arguments to this function possibly from the
1548 // adaptor frame below it.
1549 Label invoke, adapted;
1550 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1551 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1552 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1553 __ b(eq, &adapted);
1554
1555 // No arguments adaptor frame. Copy fixed number of arguments.
1556 __ mov(r0, Operand(scope()->num_parameters()));
1557 for (int i = 0; i < scope()->num_parameters(); i++) {
1558 __ ldr(r2, frame_->ParameterAt(i));
1559 __ push(r2);
1560 }
1561 __ jmp(&invoke);
1562
1563 // Arguments adaptor frame present. Copy arguments from there, but
1564 // avoid copying too many arguments to avoid stack overflows.
1565 __ bind(&adapted);
1566 static const uint32_t kArgumentsLimit = 1 * KB;
1567 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1568 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1569 __ mov(r3, r0);
1570 __ cmp(r0, Operand(kArgumentsLimit));
1571 __ b(gt, &build_args);
1572
1573 // Loop through the arguments pushing them onto the execution
1574 // stack. We don't inform the virtual frame of the push, so we don't
1575 // have to worry about getting rid of the elements from the virtual
1576 // frame.
1577 Label loop;
1578 // r3 is a small non-negative integer, due to the test above.
1579 __ cmp(r3, Operand(0));
1580 __ b(eq, &invoke);
1581 // Compute the address of the first argument.
1582 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1583 __ add(r2, r2, Operand(kPointerSize));
1584 __ bind(&loop);
1585 // Post-decrement argument address by kPointerSize on each iteration.
1586 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1587 __ push(r4);
1588 __ sub(r3, r3, Operand(1), SetCC);
1589 __ b(gt, &loop);
1590
1591 // Invoke the function.
1592 __ bind(&invoke);
1593 ParameterCount actual(r0);
1594 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1595 // Drop applicand.apply and applicand from the stack, and push
1596 // the result of the function call, but leave the spilled frame
1597 // unchanged, with 3 elements, so it is correct when we compile the
1598 // slow-case code.
1599 __ add(sp, sp, Operand(2 * kPointerSize));
1600 __ push(r0);
1601 // Stack now has 1 element:
1602 // sp[0]: result
1603 __ jmp(&done);
1604
1605 // Slow-case: Allocate the arguments object since we know it isn't
1606 // there, and fall-through to the slow-case where we call
1607 // applicand.apply.
1608 __ bind(&build_args);
1609 // Stack now has 3 elements, because we have jumped from where:
1610 // sp[0]: receiver
1611 // sp[1]: applicand.apply
1612 // sp[2]: applicand.
1613 StoreArgumentsObject(false);
1614
1615 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001616 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001617
1618 // Generic computation of x.apply(y, args) with no special optimization.
1619 // Flip applicand.apply and applicand on the stack, so
1620 // applicand looks like the receiver of the applicand.apply call.
1621 // Then process it as a normal function call.
1622 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1623 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001624 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001625
1626 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1627 frame_->CallStub(&call_function, 3);
1628 // The function and its two arguments have been dropped.
1629 frame_->Drop(); // Drop the receiver as well.
1630 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001631 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001632 // Stack now has 1 element:
1633 // sp[0]: result
1634 __ bind(&done);
1635
1636 // Restore the context register after a call.
1637 __ ldr(cp, frame_->Context());
1638}
1639
1640
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001641void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 ASSERT(has_cc());
1643 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001644 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 cc_reg_ = al;
1646}
1647
1648
ager@chromium.org7c537e22008-10-16 08:43:32 +00001649void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001650 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001651 Comment cmnt(masm_, "[ check stack");
1652 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1653 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1654 // the implicit 8 byte offset that always applies to operations with pc and
1655 // gives a return address 12 bytes down.
1656 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1657 masm_->cmp(sp, Operand(ip));
1658 StackCheckStub stub;
1659 // Call the stub if lower.
1660 masm_->mov(pc,
1661 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1662 RelocInfo::CODE_TARGET),
1663 LeaveCC,
1664 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665}
1666
1667
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001668void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1669#ifdef DEBUG
1670 int original_height = frame_->height();
1671#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001672 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001673 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001674 }
1675 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1676}
1677
1678
ager@chromium.org7c537e22008-10-16 08:43:32 +00001679void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001680#ifdef DEBUG
1681 int original_height = frame_->height();
1682#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001684 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001685 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001686 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 if (node->break_target()->is_linked()) {
1688 node->break_target()->Bind();
1689 }
1690 node->break_target()->Unuse();
1691 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692}
1693
1694
ager@chromium.org7c537e22008-10-16 08:43:32 +00001695void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001696 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001697 frame_->EmitPush(Operand(pairs));
1698 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1699
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001700 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001701 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702}
1703
1704
ager@chromium.org7c537e22008-10-16 08:43:32 +00001705void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001706#ifdef DEBUG
1707 int original_height = frame_->height();
1708#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 Comment cmnt(masm_, "[ Declaration");
1710 Variable* var = node->proxy()->var();
1711 ASSERT(var != NULL); // must have been resolved
1712 Slot* slot = var->slot();
1713
1714 // If it was not possible to allocate the variable at compile time,
1715 // we need to "declare" it at runtime to make sure it actually
1716 // exists in the local context.
1717 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1718 // Variables with a "LOOKUP" slot were introduced as non-locals
1719 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001720 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001722 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001723 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 // Declaration nodes are always declared in only two modes.
1725 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1726 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001727 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 // Push initial value, if any.
1729 // Note: For variables we must not push an initial value (such as
1730 // 'undefined') because we may have a (legal) redeclaration and we
1731 // must not destroy the current value.
1732 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001733 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001735 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001737 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001739
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001740 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001741 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001742
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001743 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 return;
1745 }
1746
1747 ASSERT(!var->is_global());
1748
1749 // If we have a function or a constant, we need to initialize the variable.
1750 Expression* val = NULL;
1751 if (node->mode() == Variable::CONST) {
1752 val = new Literal(Factory::the_hole_value());
1753 } else {
1754 val = node->fun(); // NULL if we don't have a function
1755 }
1756
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001759 WriteBarrierCharacter wb_info =
1760 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1761 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001762 // Set initial value.
1763 Reference target(this, node->proxy());
1764 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001765 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001766
iposva@chromium.org245aa852009-02-10 00:49:54 +00001767 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771}
1772
1773
ager@chromium.org7c537e22008-10-16 08:43:32 +00001774void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001775#ifdef DEBUG
1776 int original_height = frame_->height();
1777#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 Expression* expression = node->expression();
1781 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001782 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 frame_->Drop();
1784 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785}
1786
1787
ager@chromium.org7c537e22008-10-16 08:43:32 +00001788void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789#ifdef DEBUG
1790 int original_height = frame_->height();
1791#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001795 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796}
1797
1798
ager@chromium.org7c537e22008-10-16 08:43:32 +00001799void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001800#ifdef DEBUG
1801 int original_height = frame_->height();
1802#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001804 // Generate different code depending on which parts of the if statement
1805 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 bool has_then_stm = node->HasThenStatement();
1807 bool has_else_stm = node->HasElseStatement();
1808
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001811 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001813 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001814 JumpTarget then;
1815 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001817 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001818 if (frame_ != NULL) {
1819 Branch(false, &else_);
1820 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822 if (frame_ != NULL || then.is_linked()) {
1823 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001824 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 }
1826 if (frame_ != NULL) {
1827 exit.Jump();
1828 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830 if (else_.is_linked()) {
1831 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001832 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001833 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834
1835 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001836 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001838 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001840 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 if (frame_ != NULL) {
1842 Branch(false, &exit);
1843 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 if (frame_ != NULL || then.is_linked()) {
1846 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001847 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001848 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849
1850 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001851 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001853 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001855 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 if (frame_ != NULL) {
1857 Branch(true, &exit);
1858 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 if (frame_ != NULL || else_.is_linked()) {
1861 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001862 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864
1865 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001866 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 ASSERT(!has_then_stm && !has_else_stm);
1868 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001869 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 if (frame_ != NULL) {
1871 if (has_cc()) {
1872 cc_reg_ = al;
1873 } else {
1874 frame_->Drop();
1875 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 }
1877 }
1878
1879 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 if (exit.is_linked()) {
1881 exit.Bind();
1882 }
1883 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884}
1885
1886
ager@chromium.org7c537e22008-10-16 08:43:32 +00001887void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001889 CodeForStatementPosition(node);
1890 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891}
1892
1893
ager@chromium.org7c537e22008-10-16 08:43:32 +00001894void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001896 CodeForStatementPosition(node);
1897 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898}
1899
1900
ager@chromium.org7c537e22008-10-16 08:43:32 +00001901void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001902 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001904
ager@chromium.org4af710e2009-09-15 12:20:11 +00001905 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001906 Load(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 frame_->EmitPop(r0);
1909 function_return_.Jump();
1910 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 // Pop the result from the frame and prepare the frame for
1912 // returning thus making it easier to merge.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001913 frame_->PopToR0();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 frame_->PrepareForReturn();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001915 if (function_return_.is_bound()) {
1916 // If the function return label is already bound we reuse the
1917 // code by jumping to the return site.
1918 function_return_.Jump();
1919 } else {
1920 function_return_.Bind();
1921 GenerateReturnSequence();
1922 }
1923 }
1924}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001926
1927void CodeGenerator::GenerateReturnSequence() {
1928 if (FLAG_trace) {
1929 // Push the return value on the stack as the parameter.
1930 // Runtime::TraceExit returns the parameter as it is.
1931 frame_->EmitPush(r0);
1932 frame_->CallRuntime(Runtime::kTraceExit, 1);
1933 }
1934
1935#ifdef DEBUG
1936 // Add a label for checking the size of the code used for returning.
1937 Label check_exit_codesize;
1938 masm_->bind(&check_exit_codesize);
1939#endif
1940 // Make sure that the constant pool is not emitted inside of the return
1941 // sequence.
1942 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1943 // Tear down the frame which will restore the caller's frame pointer and
1944 // the link register.
1945 frame_->Exit();
1946
1947 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1948 // tool from instrumenting as we rely on the code size here.
1949 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1950 masm_->add(sp, sp, Operand(sp_delta));
1951 masm_->Jump(lr);
1952 DeleteFrame();
1953
1954#ifdef DEBUG
1955 // Check that the size of the code used for returning matches what is
1956 // expected by the debugger. If the sp_delts above cannot be encoded in
1957 // the add instruction the add will generate two instructions.
1958 int return_sequence_length =
1959 masm_->InstructionsGeneratedSince(&check_exit_codesize);
1960 CHECK(return_sequence_length ==
1961 Assembler::kJSReturnSequenceInstructions ||
1962 return_sequence_length ==
1963 Assembler::kJSReturnSequenceInstructions + 1);
1964#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001965 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966}
1967
1968
ager@chromium.org7c537e22008-10-16 08:43:32 +00001969void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001970#ifdef DEBUG
1971 int original_height = frame_->height();
1972#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001974 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001975 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001976 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001978 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001980 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001981#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001982 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001983 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001984 verified_true.Branch(eq);
1985 __ stop("PushContext: r0 is expected to be the same as cp");
1986 verified_true.Bind();
1987#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001989 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991}
1992
1993
ager@chromium.org7c537e22008-10-16 08:43:32 +00001994void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995#ifdef DEBUG
1996 int original_height = frame_->height();
1997#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 // Pop context.
2001 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2002 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002003 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002004 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005}
2006
2007
ager@chromium.org7c537e22008-10-16 08:43:32 +00002008void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009#ifdef DEBUG
2010 int original_height = frame_->height();
2011#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002014 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002016 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002017
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002018 JumpTarget next_test;
2019 JumpTarget fall_through;
2020 JumpTarget default_entry;
2021 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 ZoneList<CaseClause*>* cases = node->cases();
2023 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
2026 for (int i = 0; i < length; i++) {
2027 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002029 // Remember the default clause and compile it at the end.
2030 default_clause = clause;
2031 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 }
2033
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 Comment cmnt(masm_, "[ Case clause");
2035 // Compile the test.
2036 next_test.Bind();
2037 next_test.Unuse();
2038 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002039 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002040 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002041 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002042
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 // Before entering the body from the test, remove the switch value from
2044 // the stack.
2045 frame_->Drop();
2046
2047 // Label the body so that fall through is enabled.
2048 if (i > 0 && cases->at(i - 1)->is_default()) {
2049 default_exit.Bind();
2050 } else {
2051 fall_through.Bind();
2052 fall_through.Unuse();
2053 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002054 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002055
2056 // If control flow can fall through from the body, jump to the next body
2057 // or the end of the statement.
2058 if (frame_ != NULL) {
2059 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2060 default_entry.Jump();
2061 } else {
2062 fall_through.Jump();
2063 }
2064 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 }
2066
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002067 // The final "test" removes the switch value.
2068 next_test.Bind();
2069 frame_->Drop();
2070
2071 // If there is a default clause, compile it.
2072 if (default_clause != NULL) {
2073 Comment cmnt(masm_, "[ Default clause");
2074 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002075 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002076 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002077 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002078 if (frame_ != NULL && default_exit.is_bound()) {
2079 default_exit.Jump();
2080 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002081 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 if (fall_through.is_linked()) {
2084 fall_through.Bind();
2085 }
2086
2087 if (node->break_target()->is_linked()) {
2088 node->break_target()->Bind();
2089 }
2090 node->break_target()->Unuse();
2091 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092}
2093
2094
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002095void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096#ifdef DEBUG
2097 int original_height = frame_->height();
2098#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002099 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002100 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002101 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002102 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002103 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002105 // Label the top of the loop for the backward CFG edge. If the test
2106 // is always true we can use the continue target, and if the test is
2107 // always false there is no need.
2108 ConditionAnalysis info = AnalyzeCondition(node->cond());
2109 switch (info) {
2110 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002111 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002113 break;
2114 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002115 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002116 break;
2117 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002118 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002119 body.Bind();
2120 break;
2121 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002123 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002124 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002126 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002127 switch (info) {
2128 case ALWAYS_TRUE:
2129 // If control can fall off the end of the body, jump back to the
2130 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002132 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002135 case ALWAYS_FALSE:
2136 // If we have a continue in the body, we only have to bind its
2137 // jump target.
2138 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002140 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002141 break;
2142 case DONT_KNOW:
2143 // We have to compile the test expression if it can be reached by
2144 // control flow falling out of the body or via continue.
2145 if (node->continue_target()->is_linked()) {
2146 node->continue_target()->Bind();
2147 }
2148 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002149 Comment cmnt(masm_, "[ DoWhileCondition");
2150 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002151 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002152 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002153 // A invalid frame here indicates that control did not
2154 // fall out of the test expression.
2155 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 }
2157 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 break;
2159 }
2160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 if (node->break_target()->is_linked()) {
2162 node->break_target()->Bind();
2163 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002164 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002165 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2166}
2167
2168
2169void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2170#ifdef DEBUG
2171 int original_height = frame_->height();
2172#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002173 Comment cmnt(masm_, "[ WhileStatement");
2174 CodeForStatementPosition(node);
2175
2176 // If the test is never true and has no side effects there is no need
2177 // to compile the test or body.
2178 ConditionAnalysis info = AnalyzeCondition(node->cond());
2179 if (info == ALWAYS_FALSE) return;
2180
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002181 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002182 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002183
2184 // Label the top of the loop with the continue target for the backward
2185 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002186 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002187 node->continue_target()->Bind();
2188
2189 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002190 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002191 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002192 if (has_valid_frame()) {
2193 // A NULL frame indicates that control did not fall out of the
2194 // test expression.
2195 Branch(false, node->break_target());
2196 }
2197 if (has_valid_frame() || body.is_linked()) {
2198 body.Bind();
2199 }
2200 }
2201
2202 if (has_valid_frame()) {
2203 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002204 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002205
2206 // If control flow can fall out of the body, jump back to the top.
2207 if (has_valid_frame()) {
2208 node->continue_target()->Jump();
2209 }
2210 }
2211 if (node->break_target()->is_linked()) {
2212 node->break_target()->Bind();
2213 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002214 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002215 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2216}
2217
2218
2219void CodeGenerator::VisitForStatement(ForStatement* node) {
2220#ifdef DEBUG
2221 int original_height = frame_->height();
2222#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002223 Comment cmnt(masm_, "[ ForStatement");
2224 CodeForStatementPosition(node);
2225 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002226 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002227 }
2228
2229 // If the test is never true there is no need to compile the test or
2230 // body.
2231 ConditionAnalysis info = AnalyzeCondition(node->cond());
2232 if (info == ALWAYS_FALSE) return;
2233
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002234 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002235 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002236
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002237 // We know that the loop index is a smi if it is not modified in the
2238 // loop body and it is checked against a constant limit in the loop
2239 // condition. In this case, we reset the static type information of the
2240 // loop index to smi before compiling the body, the update expression, and
2241 // the bottom check of the loop condition.
2242 TypeInfoCodeGenState type_info_scope(this,
2243 node->is_fast_smi_loop() ?
2244 node->loop_variable()->slot() :
2245 NULL,
2246 TypeInfo::Smi());
2247
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002248 // If there is no update statement, label the top of the loop with the
2249 // continue target, otherwise with the loop target.
2250 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2251 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002252 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002253 node->continue_target()->Bind();
2254 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002255 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002256 loop.Bind();
2257 }
2258
2259 // If the test is always true, there is no need to compile it.
2260 if (info == DONT_KNOW) {
2261 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002262 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002263 if (has_valid_frame()) {
2264 Branch(false, node->break_target());
2265 }
2266 if (has_valid_frame() || body.is_linked()) {
2267 body.Bind();
2268 }
2269 }
2270
2271 if (has_valid_frame()) {
2272 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002273 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002274
2275 if (node->next() == NULL) {
2276 // If there is no update statement and control flow can fall out
2277 // of the loop, jump directly to the continue label.
2278 if (has_valid_frame()) {
2279 node->continue_target()->Jump();
2280 }
2281 } else {
2282 // If there is an update statement and control flow can reach it
2283 // via falling out of the body of the loop or continuing, we
2284 // compile the update statement.
2285 if (node->continue_target()->is_linked()) {
2286 node->continue_target()->Bind();
2287 }
2288 if (has_valid_frame()) {
2289 // Record source position of the statement as this code which is
2290 // after the code for the body actually belongs to the loop
2291 // statement and not the body.
2292 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002293 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002294 loop.Jump();
2295 }
2296 }
2297 }
2298 if (node->break_target()->is_linked()) {
2299 node->break_target()->Bind();
2300 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002301 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303}
2304
2305
ager@chromium.org7c537e22008-10-16 08:43:32 +00002306void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002307#ifdef DEBUG
2308 int original_height = frame_->height();
2309#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002310 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002312 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002314 JumpTarget primitive;
2315 JumpTarget jsobject;
2316 JumpTarget fixed_array;
2317 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2318 JumpTarget end_del_check;
2319 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
2321 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002322 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323
2324 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2325 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002326 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002327 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2328 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002329 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002330 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2331 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002332 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333
2334 // Stack layout in body:
2335 // [iteration counter (Smi)]
2336 // [length of array]
2337 // [FixedArray]
2338 // [Map or 0]
2339 // [Object]
2340
2341 // Check if enumerable is already a JSObject
2342 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002344 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002345 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 primitive.Bind();
2348 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002349 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002353 // r0: value to be iterated over
2354 frame_->EmitPush(r0); // Push the object being iterated over.
2355
2356 // Check cache validity in generated code. This is a fast case for
2357 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2358 // guarantee cache validity, call the runtime system to check cache
2359 // validity or get the property names in a fixed array.
2360 JumpTarget call_runtime;
2361 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2362 JumpTarget check_prototype;
2363 JumpTarget use_cache;
2364 __ mov(r1, Operand(r0));
2365 loop.Bind();
2366 // Check that there are no elements.
2367 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2368 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2369 __ cmp(r2, r4);
2370 call_runtime.Branch(ne);
2371 // Check that instance descriptors are not empty so that we can
2372 // check for an enum cache. Leave the map in r3 for the subsequent
2373 // prototype load.
2374 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2375 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2376 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2377 __ cmp(r2, ip);
2378 call_runtime.Branch(eq);
2379 // Check that there in an enum cache in the non-empty instance
2380 // descriptors. This is the case if the next enumeration index
2381 // field does not contain a smi.
2382 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2383 __ tst(r2, Operand(kSmiTagMask));
2384 call_runtime.Branch(eq);
2385 // For all objects but the receiver, check that the cache is empty.
2386 // r4: empty fixed array root.
2387 __ cmp(r1, r0);
2388 check_prototype.Branch(eq);
2389 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2390 __ cmp(r2, r4);
2391 call_runtime.Branch(ne);
2392 check_prototype.Bind();
2393 // Load the prototype from the map and loop if non-null.
2394 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2395 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2396 __ cmp(r1, ip);
2397 loop.Branch(ne);
2398 // The enum cache is valid. Load the map of the object being
2399 // iterated over and use the cache for the iteration.
2400 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2401 use_cache.Jump();
2402
2403 call_runtime.Bind();
2404 // Call the runtime to get the property names for the object.
2405 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002406 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002408 // If we got a map from the runtime call, we can do a fast
2409 // modification check. Otherwise, we got a fixed array, and we have
2410 // to do a slow check.
2411 // r0: map or fixed array (result from call to
2412 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 __ mov(r2, Operand(r0));
2414 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002415 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2416 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002417 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002419 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002421 // r0: map (either the result from a call to
2422 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2423 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 __ mov(r1, Operand(r0));
2425 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2426 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2427 __ ldr(r2,
2428 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2429
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002430 frame_->EmitPush(r0); // map
2431 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002432 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002433 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002434 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 frame_->EmitPush(r0);
2436 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002438 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 frame_->EmitPush(r1); // insert 0 in place of Map
2441 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442
2443 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002444 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002445 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002446 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002447 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448
2449 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002451 // sp[0] : index
2452 // sp[1] : array/enum cache length
2453 // sp[2] : array or enum cache
2454 // sp[3] : 0 or map
2455 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 // Grab the current frame's height for the break and continue
2457 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002458 node->break_target()->SetExpectedHeight();
2459 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002461 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002462 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002463 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 node->break_target()->Branch(hs);
2465
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2469 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2470
2471 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002472 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 // Check if this (still) matches the map of the enumerable.
2474 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2477 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002478 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479
2480 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002481 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2482 frame_->EmitPush(r0);
2483 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002484 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002485 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486
2487 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002488 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2489 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002492 end_del_check.Bind();
2493 // Store the entry in the 'each' expression and take another spin in the
2494 // loop. r3: i'th entry of the enum cache (or string there of)
2495 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 { Reference each(this, node->each());
2497 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002498 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 __ ldr(r0, frame_->ElementAt(each.size()));
2500 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002501 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002502 frame_->Drop(2);
2503 } else {
2504 // If the reference was to a slot we rely on the convenient property
2505 // that it doesn't matter whether a value (eg, r3 pushed above) is
2506 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002507 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002508 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002509 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 }
2511 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002512 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002514 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002516 // Next. Reestablish a spilled frame in case we are coming here via
2517 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002518 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002519 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002520 frame_->EmitPop(r0);
2521 __ add(r0, r0, Operand(Smi::FromInt(1)));
2522 frame_->EmitPush(r0);
2523 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002525 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2526 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002527 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002528 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529
2530 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002531 exit.Bind();
2532 node->continue_target()->Unuse();
2533 node->break_target()->Unuse();
2534 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535}
2536
2537
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002538void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539#ifdef DEBUG
2540 int original_height = frame_->height();
2541#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002542 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002543 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002546 JumpTarget try_block;
2547 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552
2553 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002554 Variable* catch_var = node->catch_var()->var();
2555 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2556 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557
2558 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002559 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002561 VisitStatements(node->catch_block()->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 if (frame_ != NULL) {
2563 exit.Jump();
2564 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565
2566
2567 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2571 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002573 // Shadow the labels for all escapes from the try block, including
2574 // returns. During shadowing, the original label is hidden as the
2575 // LabelShadow and operations on the original actually affect the
2576 // shadowing label.
2577 //
2578 // We should probably try to unify the escaping labels and the return
2579 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 int nof_escapes = node->escaping_targets()->length();
2581 List<ShadowTarget*> shadows(1 + nof_escapes);
2582
2583 // Add the shadow target for the function return.
2584 static const int kReturnShadowIndex = 0;
2585 shadows.Add(new ShadowTarget(&function_return_));
2586 bool function_return_was_shadowed = function_return_is_shadowed_;
2587 function_return_is_shadowed_ = true;
2588 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2589
2590 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 }
2594
2595 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002596 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597
2598 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002599 // After shadowing stops, the original labels are unshadowed and the
2600 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 bool has_unlinks = false;
2602 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604 has_unlinks = has_unlinks || shadows[i]->is_linked();
2605 }
2606 function_return_is_shadowed_ = function_return_was_shadowed;
2607
2608 // Get an external reference to the handler address.
2609 ExternalReference handler_address(Top::k_handler_address);
2610
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002611 // If we can fall off the end of the try block, unlink from try chain.
2612 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002613 // The next handler address is on top of the frame. Unlink from
2614 // the handler list and drop the rest of this handler from the
2615 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002616 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002617 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002618 __ mov(r3, Operand(handler_address));
2619 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002620 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002621 if (has_unlinks) {
2622 exit.Jump();
2623 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 }
2625
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002626 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002627 // jumped to. Deallocate each shadow target.
2628 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002630 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002631 shadows[i]->Bind();
2632 // Because we can be jumping here (to spilled code) from unspilled
2633 // code, we need to reestablish a spilled frame at this block.
2634 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 // Reload sp from the top handler, because some statements that we
2637 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002638 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002640 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002642 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002643 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002645 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002647 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2648 frame_->PrepareForReturn();
2649 }
2650 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 }
2652 }
2653
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 exit.Bind();
2655 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656}
2657
2658
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002659void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660#ifdef DEBUG
2661 int original_height = frame_->height();
2662#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002663 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002664 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
2667 // State: Used to keep track of reason for entering the finally
2668 // block. Should probably be extended to hold information for
2669 // break/continue from within the try block.
2670 enum { FALLING, THROWING, JUMPING };
2671
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002672 JumpTarget try_block;
2673 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 // In case of thrown exceptions, this is where we continue.
2679 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002680 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681
2682 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2686 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002688 // Shadow the labels for all escapes from the try block, including
2689 // returns. Shadowing hides the original label as the LabelShadow and
2690 // operations on the original actually affect the shadowing label.
2691 //
2692 // We should probably try to unify the escaping labels and the return
2693 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694 int nof_escapes = node->escaping_targets()->length();
2695 List<ShadowTarget*> shadows(1 + nof_escapes);
2696
2697 // Add the shadow target for the function return.
2698 static const int kReturnShadowIndex = 0;
2699 shadows.Add(new ShadowTarget(&function_return_));
2700 bool function_return_was_shadowed = function_return_is_shadowed_;
2701 function_return_is_shadowed_ = true;
2702 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2703
2704 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002706 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 }
2708
2709 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002710 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002712 // Stop the introduced shadowing and count the number of required unlinks.
2713 // After shadowing stops, the original labels are unshadowed and the
2714 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002716 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717 shadows[i]->StopShadowing();
2718 if (shadows[i]->is_linked()) nof_unlinks++;
2719 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002720 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002722 // Get an external reference to the handler address.
2723 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002725 // If we can fall off the end of the try block, unlink from the try
2726 // chain and set the state on the frame to FALLING.
2727 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002728 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002729 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002730 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002731 __ mov(r3, Operand(handler_address));
2732 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002733 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002734
2735 // Fake a top of stack value (unneeded when FALLING) and set the
2736 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002737 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002738 frame_->EmitPush(r0);
2739 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2740 if (nof_unlinks > 0) {
2741 finally_block.Jump();
2742 }
2743 }
2744
2745 // Generate code to unlink and set the state for the (formerly)
2746 // shadowing targets that have been jumped to.
2747 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002749 // If we have come from the shadowed return, the return value is
2750 // in (a non-refcounted reference to) r0. We must preserve it
2751 // until it is pushed.
2752 //
2753 // Because we can be jumping here (to spilled code) from
2754 // unspilled code, we need to reestablish a spilled frame at
2755 // this block.
2756 shadows[i]->Bind();
2757 frame_->SpillAll();
2758
2759 // Reload sp from the top handler, because some statements that
2760 // we break from (eg, for...in) may have left stuff on the
2761 // stack.
2762 __ mov(r3, Operand(handler_address));
2763 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002764 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002765
2766 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002767 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002768 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769 frame_->EmitPop(r1);
2770 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002771 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002772
2773 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002774 // If this label shadowed the function return, materialize the
2775 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002776 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002777 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002778 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002779 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002780 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781 }
2782 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783 if (--nof_unlinks > 0) {
2784 // If this is not the last unlink block, jump around the next.
2785 finally_block.Jump();
2786 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 }
2788 }
2789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002791 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002793 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002794 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002795
2796 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002797 // and the state - while evaluating the finally block.
2798 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799 // Generate code for the statements in the finally block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002800 VisitStatements(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002802 if (has_valid_frame()) {
2803 // Restore state and return value or faked TOS.
2804 frame_->EmitPop(r2);
2805 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 }
2807
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002808 // Generate code to jump to the right destination for all used
2809 // formerly shadowing targets. Deallocate each shadow target.
2810 for (int i = 0; i < shadows.length(); i++) {
2811 if (has_valid_frame() && shadows[i]->is_bound()) {
2812 JumpTarget* original = shadows[i]->other_target();
2813 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2814 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002815 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002816 skip.Branch(ne);
2817 frame_->PrepareForReturn();
2818 original->Jump();
2819 skip.Bind();
2820 } else {
2821 original->Branch(eq);
2822 }
2823 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002826 if (has_valid_frame()) {
2827 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002828 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002829 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2830 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002832 // Rethrow exception.
2833 frame_->EmitPush(r0);
2834 frame_->CallRuntime(Runtime::kReThrow, 1);
2835
2836 // Done.
2837 exit.Bind();
2838 }
2839 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840}
2841
2842
ager@chromium.org7c537e22008-10-16 08:43:32 +00002843void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844#ifdef DEBUG
2845 int original_height = frame_->height();
2846#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002848 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002849#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002850 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002851#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002852 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002853 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854}
2855
2856
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002857void CodeGenerator::InstantiateFunction(
2858 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002859 // Use the fast case closure allocation code that allocates in new
2860 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002861 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002862 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002863 frame_->EmitPush(Operand(function_info));
2864 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002865 frame_->CallStub(&stub, 1);
2866 frame_->EmitPush(r0);
2867 } else {
2868 // Create a new closure.
2869 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002870 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002871 frame_->CallRuntime(Runtime::kNewClosure, 2);
2872 frame_->EmitPush(r0);
2873 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874}
2875
2876
ager@chromium.org7c537e22008-10-16 08:43:32 +00002877void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002878#ifdef DEBUG
2879 int original_height = frame_->height();
2880#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881 Comment cmnt(masm_, "[ FunctionLiteral");
2882
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002883 // Build the function info and instantiate it.
2884 Handle<SharedFunctionInfo> function_info =
2885 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002886 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002887 if (HasStackOverflow()) {
2888 ASSERT(frame_->height() == original_height);
2889 return;
2890 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002891 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002892 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893}
2894
2895
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002896void CodeGenerator::VisitSharedFunctionInfoLiteral(
2897 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002898#ifdef DEBUG
2899 int original_height = frame_->height();
2900#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002901 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2902 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002903 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904}
2905
2906
ager@chromium.org7c537e22008-10-16 08:43:32 +00002907void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002908#ifdef DEBUG
2909 int original_height = frame_->height();
2910#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002912 JumpTarget then;
2913 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002914 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002915 if (has_valid_frame()) {
2916 Branch(false, &else_);
2917 }
2918 if (has_valid_frame() || then.is_linked()) {
2919 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002920 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002921 }
2922 if (else_.is_linked()) {
2923 JumpTarget exit;
2924 if (has_valid_frame()) exit.Jump();
2925 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002926 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002927 if (exit.is_linked()) exit.Bind();
2928 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002929 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930}
2931
2932
ager@chromium.org7c537e22008-10-16 08:43:32 +00002933void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2934 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002935 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002937 // JumpTargets do not yet support merging frames so the frame must be
2938 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002939 JumpTarget slow;
2940 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002941
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002942 // Generate fast case for loading from slots that correspond to
2943 // local/global variables or arguments unless they are shadowed by
2944 // eval-introduced bindings.
2945 EmitDynamicLoadFromSlotFastCase(slot,
2946 typeof_state,
2947 &slow,
2948 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002949
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002950 slow.Bind();
2951 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002952 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002953
ager@chromium.org7c537e22008-10-16 08:43:32 +00002954 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002956 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002957 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002959
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002960 done.Bind();
2961 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962
2963 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002964 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002965 TypeInfo info = type_info(slot);
2966 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002967
ager@chromium.org7c537e22008-10-16 08:43:32 +00002968 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002969 // Const slots may contain 'the hole' value (the constant hasn't been
2970 // initialized yet) which needs to be converted into the 'undefined'
2971 // value.
2972 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002973 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002974 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002975 __ cmp(tos, ip);
2976 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
2977 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002978 }
2979 }
2980}
2981
2982
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002983void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
2984 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002985 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002986 LoadFromSlot(slot, state);
2987
2988 // Bail out quickly if we're not using lazy arguments allocation.
2989 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
2990
2991 // ... or if the slot isn't a non-parameter arguments slot.
2992 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
2993
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002994 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002995 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002996 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002997
2998 // If the loaded value is the sentinel that indicates that we
2999 // haven't loaded the arguments object yet, we need to do it now.
3000 JumpTarget exit;
3001 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003002 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003003 exit.Branch(ne);
3004 frame_->Drop();
3005 StoreArgumentsObject(false);
3006 exit.Bind();
3007}
3008
3009
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003010void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3011 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003012 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003013 if (slot->type() == Slot::LOOKUP) {
3014 ASSERT(slot->var()->is_dynamic());
3015
3016 // For now, just do a runtime call.
3017 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003018 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003019
3020 if (init_state == CONST_INIT) {
3021 // Same as the case for a normal store, but ignores attribute
3022 // (e.g. READ_ONLY) of context slot so that we can initialize
3023 // const properties (introduced via eval("const foo = (some
3024 // expr);")). Also, uses the current function context instead of
3025 // the top context.
3026 //
3027 // Note that we must declare the foo upon entry of eval(), via a
3028 // context slot declaration, but we cannot initialize it at the
3029 // same time, because the const declaration may be at the end of
3030 // the eval code (sigh...) and the const variable may have been
3031 // used before (where its value is 'undefined'). Thus, we can only
3032 // do the initialization when we actually encounter the expression
3033 // and when the expression operands are defined and valid, and
3034 // thus we need the split into 2 operations: declaration of the
3035 // context slot followed by initialization.
3036 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3037 } else {
3038 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3039 }
3040 // Storing a variable must keep the (new) value on the expression
3041 // stack. This is necessary for compiling assignment expressions.
3042 frame_->EmitPush(r0);
3043
3044 } else {
3045 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003046 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003047 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003048
ager@chromium.org357bf652010-04-12 11:30:10 +00003049 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003050 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003051
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003052 if (init_state == CONST_INIT) {
3053 ASSERT(slot->var()->mode() == Variable::CONST);
3054 // Only the first const initialization must be executed (the slot
3055 // still contains 'the hole' value). When the assignment is
3056 // executed, the code is identical to a normal store (see below).
3057 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003058 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003059 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003060 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003061 exit.Branch(ne);
3062 }
3063
3064 // We must execute the store. Storing a variable must keep the
3065 // (new) value on the stack. This is necessary for compiling
3066 // assignment expressions.
3067 //
3068 // Note: We will reach here even with slot->var()->mode() ==
3069 // Variable::CONST because of const declarations which will
3070 // initialize consts to 'the hole' value and by doing so, end up
3071 // calling this code. r2 may be loaded with context; used below in
3072 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003073 Register tos = frame_->Peek();
3074 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003075 if (slot->type() == Slot::CONTEXT) {
3076 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003077 __ tst(tos, Operand(kSmiTagMask));
3078 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003079 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003080 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003081 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003082 // We need an extra register. Until we have a way to do that in the
3083 // virtual frame we will cheat and ask for a free TOS register.
3084 Register scratch3 = frame_->GetTOSRegister();
3085 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003086 }
3087 // If we definitely did not jump over the assignment, we do not need
3088 // to bind the exit label. Doing so can defeat peephole
3089 // optimization.
3090 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3091 exit.Bind();
3092 }
3093 }
3094}
3095
3096
ager@chromium.org381abbb2009-02-25 13:23:22 +00003097void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3098 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003099 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003100 // Check that no extension objects have been created by calls to
3101 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003102 Register tmp = frame_->scratch0();
3103 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003104 Register context = cp;
3105 Scope* s = scope();
3106 while (s != NULL) {
3107 if (s->num_heap_slots() > 0) {
3108 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003109 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003110 // Check that extension is NULL.
3111 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3112 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003114 }
3115 // Load next context in chain.
3116 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3117 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3118 context = tmp;
3119 }
3120 // If no outer scope calls eval, we do not need to check more
3121 // context extensions.
3122 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3123 s = s->outer_scope();
3124 }
3125
3126 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003127 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003128 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003129 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003130 __ bind(&next);
3131 // Terminate at global context.
3132 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003133 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3134 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003135 __ b(eq, &fast);
3136 // Check that extension is NULL.
3137 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3138 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003139 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003140 // Load next context in chain.
3141 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3142 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3143 __ b(&next);
3144 __ bind(&fast);
3145 }
3146
ager@chromium.org381abbb2009-02-25 13:23:22 +00003147 // Load the global object.
3148 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003149 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003150 frame_->CallLoadIC(slot->var()->name(),
3151 typeof_state == INSIDE_TYPEOF
3152 ? RelocInfo::CODE_TARGET
3153 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003154}
3155
3156
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003157void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3158 TypeofState typeof_state,
3159 JumpTarget* slow,
3160 JumpTarget* done) {
3161 // Generate fast-case code for variables that might be shadowed by
3162 // eval-introduced variables. Eval is used a lot without
3163 // introducing variables. In those cases, we do not want to
3164 // perform a runtime call for all variables in the scope
3165 // containing the eval.
3166 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3167 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3168 frame_->SpillAll();
3169 done->Jump();
3170
3171 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3172 frame_->SpillAll();
3173 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3174 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3175 if (potential_slot != NULL) {
3176 // Generate fast case for locals that rewrite to slots.
3177 __ ldr(r0,
3178 ContextSlotOperandCheckExtensions(potential_slot,
3179 r1,
3180 r2,
3181 slow));
3182 if (potential_slot->var()->mode() == Variable::CONST) {
3183 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3184 __ cmp(r0, ip);
3185 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3186 }
3187 done->Jump();
3188 } else if (rewrite != NULL) {
3189 // Generate fast case for argument loads.
3190 Property* property = rewrite->AsProperty();
3191 if (property != NULL) {
3192 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3193 Literal* key_literal = property->key()->AsLiteral();
3194 if (obj_proxy != NULL &&
3195 key_literal != NULL &&
3196 obj_proxy->IsArguments() &&
3197 key_literal->handle()->IsSmi()) {
3198 // Load arguments object if there are no eval-introduced
3199 // variables. Then load the argument from the arguments
3200 // object using keyed load.
3201 __ ldr(r0,
3202 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3203 r1,
3204 r2,
3205 slow));
3206 frame_->EmitPush(r0);
3207 __ mov(r1, Operand(key_literal->handle()));
3208 frame_->EmitPush(r1);
3209 EmitKeyedLoad();
3210 done->Jump();
3211 }
3212 }
3213 }
3214 }
3215}
3216
3217
ager@chromium.org7c537e22008-10-16 08:43:32 +00003218void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219#ifdef DEBUG
3220 int original_height = frame_->height();
3221#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003222 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003223 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003224 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003225}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226
ager@chromium.org7c537e22008-10-16 08:43:32 +00003227
3228void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003229#ifdef DEBUG
3230 int original_height = frame_->height();
3231#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003232 Comment cmnt(masm_, "[ VariableProxy");
3233
3234 Variable* var = node->var();
3235 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003236 if (expr != NULL) {
3237 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003239 ASSERT(var->is_global());
3240 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003241 ref.GetValue();
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.org7c537e22008-10-16 08:43:32 +00003247void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248#ifdef DEBUG
3249 int original_height = frame_->height();
3250#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003252 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003253 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003254 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003255 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003256 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257}
3258
3259
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261#ifdef DEBUG
3262 int original_height = frame_->height();
3263#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264 Comment cmnt(masm_, "[ RexExp Literal");
3265
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003266 Register tmp = VirtualFrame::scratch0();
3267 // Free up a TOS register that can be used to push the literal.
3268 Register literal = frame_->GetTOSRegister();
3269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 // Retrieve the literal array and check the allocated entry.
3271
3272 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003273 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274
3275 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003276 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277
3278 // Load the literal at the ast saved index.
3279 int literal_offset =
3280 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003281 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003283 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003284 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003285 __ cmp(literal, ip);
3286 // This branch locks the virtual frame at the done label to match the
3287 // one we have here, where the literal register is not on the stack and
3288 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003289 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003291 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003293 // literal array (0)
3294 frame_->EmitPush(tmp);
3295 // literal index (1)
3296 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3297 // RegExp pattern (2)
3298 frame_->EmitPush(Operand(node->pattern()));
3299 // RegExp flags (3)
3300 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003301 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003302 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003304 materialized.Bind();
3305
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003306 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003307 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3308 frame_->EmitPush(Operand(Smi::FromInt(size)));
3309 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3310 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3311 // r0 is newly allocated space.
3312
3313 // Reuse literal variable with (possibly) a new register, still holding
3314 // the materialized boilerplate.
3315 literal = frame_->PopToRegister(r0);
3316
3317 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3318
3319 // Push the clone.
3320 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003321 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322}
3323
3324
ager@chromium.org7c537e22008-10-16 08:43:32 +00003325void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003326#ifdef DEBUG
3327 int original_height = frame_->height();
3328#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 Comment cmnt(masm_, "[ ObjectLiteral");
3330
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003331 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003333 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003334 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003335 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3336 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003337 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003338 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003339 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003340 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003341 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003342 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003343 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003344 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003345 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003346 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003347 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003348 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003349 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003350 // At the start of each iteration, the top of stack contains
3351 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352 ObjectLiteral::Property* property = node->properties()->at(i);
3353 Literal* key = property->key();
3354 Expression* value = property->value();
3355 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003356 case ObjectLiteral::Property::CONSTANT:
3357 break;
3358 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3359 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3360 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003361 case ObjectLiteral::Property::COMPUTED:
3362 if (key->handle()->IsSymbol()) {
3363 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003364 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003365 frame_->PopToR0();
3366 // Fetch the object literal.
3367 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003368 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003369 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3370 break;
3371 }
3372 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003373 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003374 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003375 Load(key);
3376 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003377 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378 break;
3379 }
3380 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003381 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003382 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003383 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003384 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003385 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003386 break;
3387 }
3388 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003389 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003390 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003391 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003392 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003393 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 break;
3395 }
3396 }
3397 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003398 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003399}
3400
3401
ager@chromium.org7c537e22008-10-16 08:43:32 +00003402void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403#ifdef DEBUG
3404 int original_height = frame_->height();
3405#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003407
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003408 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003409 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003410 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003411 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003412 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3413 frame_->EmitPush(tos);
3414 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3415 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003416 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003417 if (node->depth() > 1) {
3418 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003419 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003420 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003421 } else {
3422 FastCloneShallowArrayStub stub(length);
3423 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003424 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003425 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003426 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003427
3428 // Generate code to set the elements in the array that are not
3429 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003430 for (int i = 0; i < node->values()->length(); i++) {
3431 Expression* value = node->values()->at(i);
3432
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003433 // If value is a literal the property value is already set in the
3434 // boilerplate object.
3435 if (value->AsLiteral() != NULL) continue;
3436 // If value is a materialized literal the property value is already set
3437 // in the boilerplate object if it is simple.
3438 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003439
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003440 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003441 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003442 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003443 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003444 frame_->SpillAllButCopyTOSToR1();
3445
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003446 // Get the elements array.
3447 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003449 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003450 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003451 __ str(r0, FieldMemOperand(r1, offset));
3452
3453 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003454 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003456 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003457}
3458
3459
ager@chromium.org32912102009-01-16 10:38:43 +00003460void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003461#ifdef DEBUG
3462 int original_height = frame_->height();
3463#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003464 // Call runtime routine to allocate the catch extension object and
3465 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003466 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003467 Load(node->key());
3468 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003469 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3470 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003471 ASSERT_EQ(original_height + 1, frame_->height());
3472}
3473
3474
3475void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3476#ifdef DEBUG
3477 int original_height = frame_->height();
3478#endif
3479 Comment cmnt(masm(), "[ Variable Assignment");
3480 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3481 ASSERT(var != NULL);
3482 Slot* slot = var->slot();
3483 ASSERT(slot != NULL);
3484
3485 // Evaluate the right-hand side.
3486 if (node->is_compound()) {
3487 // For a compound assignment the right-hand side is a binary operation
3488 // between the current property value and the actual right-hand side.
3489 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3490
3491 // Perform the binary operation.
3492 Literal* literal = node->value()->AsLiteral();
3493 bool overwrite_value =
3494 (node->value()->AsBinaryOperation() != NULL &&
3495 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3496 if (literal != NULL && literal->handle()->IsSmi()) {
3497 SmiOperation(node->binary_op(),
3498 literal->handle(),
3499 false,
3500 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3501 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003502 GenerateInlineSmi inline_smi =
3503 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3504 if (literal != NULL) {
3505 ASSERT(!literal->handle()->IsSmi());
3506 inline_smi = DONT_GENERATE_INLINE_SMI;
3507 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003508 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003509 GenericBinaryOperation(node->binary_op(),
3510 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3511 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003512 }
3513 } else {
3514 Load(node->value());
3515 }
3516
3517 // Perform the assignment.
3518 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3519 CodeForSourcePosition(node->position());
3520 StoreToSlot(slot,
3521 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3522 }
3523 ASSERT_EQ(original_height + 1, frame_->height());
3524}
3525
3526
3527void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3528#ifdef DEBUG
3529 int original_height = frame_->height();
3530#endif
3531 Comment cmnt(masm(), "[ Named Property Assignment");
3532 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3533 Property* prop = node->target()->AsProperty();
3534 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3535
3536 // Initialize name and evaluate the receiver sub-expression if necessary. If
3537 // the receiver is trivial it is not placed on the stack at this point, but
3538 // loaded whenever actually needed.
3539 Handle<String> name;
3540 bool is_trivial_receiver = false;
3541 if (var != NULL) {
3542 name = var->name();
3543 } else {
3544 Literal* lit = prop->key()->AsLiteral();
3545 ASSERT_NOT_NULL(lit);
3546 name = Handle<String>::cast(lit->handle());
3547 // Do not materialize the receiver on the frame if it is trivial.
3548 is_trivial_receiver = prop->obj()->IsTrivial();
3549 if (!is_trivial_receiver) Load(prop->obj());
3550 }
3551
3552 // Change to slow case in the beginning of an initialization block to
3553 // avoid the quadratic behavior of repeatedly adding fast properties.
3554 if (node->starts_initialization_block()) {
3555 // Initialization block consists of assignments of the form expr.x = ..., so
3556 // this will never be an assignment to a variable, so there must be a
3557 // receiver object.
3558 ASSERT_EQ(NULL, var);
3559 if (is_trivial_receiver) {
3560 Load(prop->obj());
3561 } else {
3562 frame_->Dup();
3563 }
3564 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3565 }
3566
3567 // Change to fast case at the end of an initialization block. To prepare for
3568 // that add an extra copy of the receiver to the frame, so that it can be
3569 // converted back to fast case after the assignment.
3570 if (node->ends_initialization_block() && !is_trivial_receiver) {
3571 frame_->Dup();
3572 }
3573
3574 // Stack layout:
3575 // [tos] : receiver (only materialized if non-trivial)
3576 // [tos+1] : receiver if at the end of an initialization block
3577
3578 // Evaluate the right-hand side.
3579 if (node->is_compound()) {
3580 // For a compound assignment the right-hand side is a binary operation
3581 // between the current property value and the actual right-hand side.
3582 if (is_trivial_receiver) {
3583 Load(prop->obj());
3584 } else if (var != NULL) {
3585 LoadGlobal();
3586 } else {
3587 frame_->Dup();
3588 }
3589 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003590
3591 // Perform the binary operation.
3592 Literal* literal = node->value()->AsLiteral();
3593 bool overwrite_value =
3594 (node->value()->AsBinaryOperation() != NULL &&
3595 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3596 if (literal != NULL && literal->handle()->IsSmi()) {
3597 SmiOperation(node->binary_op(),
3598 literal->handle(),
3599 false,
3600 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3601 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003602 GenerateInlineSmi inline_smi =
3603 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3604 if (literal != NULL) {
3605 ASSERT(!literal->handle()->IsSmi());
3606 inline_smi = DONT_GENERATE_INLINE_SMI;
3607 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003608 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003609 GenericBinaryOperation(node->binary_op(),
3610 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3611 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003612 }
3613 } else {
3614 // For non-compound assignment just load the right-hand side.
3615 Load(node->value());
3616 }
3617
3618 // Stack layout:
3619 // [tos] : value
3620 // [tos+1] : receiver (only materialized if non-trivial)
3621 // [tos+2] : receiver if at the end of an initialization block
3622
3623 // Perform the assignment. It is safe to ignore constants here.
3624 ASSERT(var == NULL || var->mode() != Variable::CONST);
3625 ASSERT_NE(Token::INIT_CONST, node->op());
3626 if (is_trivial_receiver) {
3627 // Load the receiver and swap with the value.
3628 Load(prop->obj());
3629 Register t0 = frame_->PopToRegister();
3630 Register t1 = frame_->PopToRegister(t0);
3631 frame_->EmitPush(t0);
3632 frame_->EmitPush(t1);
3633 }
3634 CodeForSourcePosition(node->position());
3635 bool is_contextual = (var != NULL);
3636 EmitNamedStore(name, is_contextual);
3637 frame_->EmitPush(r0);
3638
3639 // Change to fast case at the end of an initialization block.
3640 if (node->ends_initialization_block()) {
3641 ASSERT_EQ(NULL, var);
3642 // The argument to the runtime call is the receiver.
3643 if (is_trivial_receiver) {
3644 Load(prop->obj());
3645 } else {
3646 // A copy of the receiver is below the value of the assignment. Swap
3647 // the receiver and the value of the assignment expression.
3648 Register t0 = frame_->PopToRegister();
3649 Register t1 = frame_->PopToRegister(t0);
3650 frame_->EmitPush(t0);
3651 frame_->EmitPush(t1);
3652 }
3653 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3654 }
3655
3656 // Stack layout:
3657 // [tos] : result
3658
3659 ASSERT_EQ(original_height + 1, frame_->height());
3660}
3661
3662
3663void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3664#ifdef DEBUG
3665 int original_height = frame_->height();
3666#endif
3667 Comment cmnt(masm_, "[ Keyed Property Assignment");
3668 Property* prop = node->target()->AsProperty();
3669 ASSERT_NOT_NULL(prop);
3670
3671 // Evaluate the receiver subexpression.
3672 Load(prop->obj());
3673
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003674 WriteBarrierCharacter wb_info;
3675
ager@chromium.orgac091b72010-05-05 07:34:42 +00003676 // Change to slow case in the beginning of an initialization block to
3677 // avoid the quadratic behavior of repeatedly adding fast properties.
3678 if (node->starts_initialization_block()) {
3679 frame_->Dup();
3680 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3681 }
3682
3683 // Change to fast case at the end of an initialization block. To prepare for
3684 // that add an extra copy of the receiver to the frame, so that it can be
3685 // converted back to fast case after the assignment.
3686 if (node->ends_initialization_block()) {
3687 frame_->Dup();
3688 }
3689
3690 // Evaluate the key subexpression.
3691 Load(prop->key());
3692
3693 // Stack layout:
3694 // [tos] : key
3695 // [tos+1] : receiver
3696 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003697 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003698 // Evaluate the right-hand side.
3699 if (node->is_compound()) {
3700 // For a compound assignment the right-hand side is a binary operation
3701 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003702 // Duplicate receiver and key for loading the current property value.
3703 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003704 EmitKeyedLoad();
3705 frame_->EmitPush(r0);
3706
3707 // Perform the binary operation.
3708 Literal* literal = node->value()->AsLiteral();
3709 bool overwrite_value =
3710 (node->value()->AsBinaryOperation() != NULL &&
3711 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3712 if (literal != NULL && literal->handle()->IsSmi()) {
3713 SmiOperation(node->binary_op(),
3714 literal->handle(),
3715 false,
3716 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3717 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003718 GenerateInlineSmi inline_smi =
3719 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3720 if (literal != NULL) {
3721 ASSERT(!literal->handle()->IsSmi());
3722 inline_smi = DONT_GENERATE_INLINE_SMI;
3723 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003724 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003725 GenericBinaryOperation(node->binary_op(),
3726 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3727 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003728 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003729 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003730 } else {
3731 // For non-compound assignment just load the right-hand side.
3732 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003733 wb_info = node->value()->AsLiteral() != NULL ?
3734 NEVER_NEWSPACE :
3735 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003736 }
3737
3738 // Stack layout:
3739 // [tos] : value
3740 // [tos+1] : key
3741 // [tos+2] : receiver
3742 // [tos+3] : receiver if at the end of an initialization block
3743
3744 // Perform the assignment. It is safe to ignore constants here.
3745 ASSERT(node->op() != Token::INIT_CONST);
3746 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003747 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003748 frame_->EmitPush(r0);
3749
3750 // Stack layout:
3751 // [tos] : result
3752 // [tos+1] : receiver if at the end of an initialization block
3753
3754 // Change to fast case at the end of an initialization block.
3755 if (node->ends_initialization_block()) {
3756 // The argument to the runtime call is the extra copy of the receiver,
3757 // which is below the value of the assignment. Swap the receiver and
3758 // the value of the assignment expression.
3759 Register t0 = frame_->PopToRegister();
3760 Register t1 = frame_->PopToRegister(t0);
3761 frame_->EmitPush(t1);
3762 frame_->EmitPush(t0);
3763 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3764 }
3765
3766 // Stack layout:
3767 // [tos] : result
3768
3769 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003770}
3771
3772
ager@chromium.org7c537e22008-10-16 08:43:32 +00003773void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003774 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003775#ifdef DEBUG
3776 int original_height = frame_->height();
3777#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003779
ager@chromium.orgac091b72010-05-05 07:34:42 +00003780 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3781 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003782
ager@chromium.orgac091b72010-05-05 07:34:42 +00003783 if (var != NULL && !var->is_global()) {
3784 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003785
ager@chromium.orgac091b72010-05-05 07:34:42 +00003786 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3787 (var != NULL && var->is_global())) {
3788 // Properties whose keys are property names and global variables are
3789 // treated as named property references. We do not need to consider
3790 // global 'this' because it is not a valid left-hand side.
3791 EmitNamedPropertyAssignment(node);
3792
3793 } else if (prop != NULL) {
3794 // Other properties (including rewritten parameters for a function that
3795 // uses arguments) are keyed property assignments.
3796 EmitKeyedPropertyAssignment(node);
3797
3798 } else {
3799 // Invalid left-hand side.
3800 Load(node->target());
3801 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3802 // The runtime call doesn't actually return but the code generator will
3803 // still generate code and expects a certain frame height.
3804 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003806 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807}
3808
3809
ager@chromium.org7c537e22008-10-16 08:43:32 +00003810void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003811#ifdef DEBUG
3812 int original_height = frame_->height();
3813#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814 Comment cmnt(masm_, "[ Throw");
3815
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003816 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003817 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818 frame_->CallRuntime(Runtime::kThrow, 1);
3819 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003820 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003821}
3822
3823
ager@chromium.org7c537e22008-10-16 08:43:32 +00003824void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003825#ifdef DEBUG
3826 int original_height = frame_->height();
3827#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003828 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003829
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003830 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003831 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003832 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003833 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834}
3835
3836
ager@chromium.org7c537e22008-10-16 08:43:32 +00003837void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003838#ifdef DEBUG
3839 int original_height = frame_->height();
3840#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841 Comment cmnt(masm_, "[ Call");
3842
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003843 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844 ZoneList<Expression*>* args = node->arguments();
3845
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003848 Variable* var = function->AsVariableProxy()->AsVariable();
3849 Property* property = function->AsProperty();
3850
3851 // ------------------------------------------------------------------------
3852 // Fast-case: Use inline caching.
3853 // ---
3854 // According to ECMA-262, section 11.2.3, page 44, the function to call
3855 // must be resolved after the arguments have been evaluated. The IC code
3856 // automatically handles this by loading the arguments before the function
3857 // is resolved in cache misses (this also holds for megamorphic calls).
3858 // ------------------------------------------------------------------------
3859
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003860 if (var != NULL && var->is_possibly_eval()) {
3861 // ----------------------------------
3862 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3863 // ----------------------------------
3864
3865 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3866 // resolve the function we need to call and the receiver of the
3867 // call. Then we call the resolved function using the given
3868 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003869
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003870 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003871 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003872
3873 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003874 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003875
3876 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003877 int arg_count = args->length();
3878 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003879 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003880 }
3881
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003882 VirtualFrame::SpilledScope spilled_scope(frame_);
3883
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003884 // If we know that eval can only be shadowed by eval-introduced
3885 // variables we attempt to load the global eval function directly
3886 // in generated code. If we succeed, there is no need to perform a
3887 // context lookup in the runtime system.
3888 JumpTarget done;
3889 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3890 ASSERT(var->slot()->type() == Slot::LOOKUP);
3891 JumpTarget slow;
3892 // Prepare the stack for the call to
3893 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3894 // function, the first argument to the eval call and the
3895 // receiver.
3896 LoadFromGlobalSlotCheckExtensions(var->slot(),
3897 NOT_INSIDE_TYPEOF,
3898 &slow);
3899 frame_->EmitPush(r0);
3900 if (arg_count > 0) {
3901 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3902 frame_->EmitPush(r1);
3903 } else {
3904 frame_->EmitPush(r2);
3905 }
3906 __ ldr(r1, frame_->Receiver());
3907 frame_->EmitPush(r1);
3908
3909 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3910
3911 done.Jump();
3912 slow.Bind();
3913 }
3914
3915 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3916 // pushing the loaded function, the first argument to the eval
3917 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003918 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3919 frame_->EmitPush(r1);
3920 if (arg_count > 0) {
3921 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3922 frame_->EmitPush(r1);
3923 } else {
3924 frame_->EmitPush(r2);
3925 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003926 __ ldr(r1, frame_->Receiver());
3927 frame_->EmitPush(r1);
3928
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003929 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003930 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003931
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003932 // If we generated fast-case code bind the jump-target where fast
3933 // and slow case merge.
3934 if (done.is_linked()) done.Bind();
3935
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003936 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003937 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003938 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3939
3940 // Call the function.
3941 CodeForSourcePosition(node->position());
3942
3943 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003944 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003945 frame_->CallStub(&call_function, arg_count + 1);
3946
3947 __ ldr(cp, frame_->Context());
3948 // Remove the function from the stack.
3949 frame_->Drop();
3950 frame_->EmitPush(r0);
3951
3952 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 // ----------------------------------
3954 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3955 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003956 // Pass the global object as the receiver and let the IC stub
3957 // patch the stack to use the global proxy as 'this' in the
3958 // invoked function.
3959 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960
3961 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003962 int arg_count = args->length();
3963 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003964 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003965 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003967 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00003968 // Setup the name register and call the IC initialization code.
3969 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003970 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3971 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003972 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003973 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3974 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003975 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003976 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977
3978 } else if (var != NULL && var->slot() != NULL &&
3979 var->slot()->type() == Slot::LOOKUP) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003980 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003981 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003982 // JavaScript examples:
3983 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003984 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003985 //
3986 // function f() {};
3987 // function g() {
3988 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003989 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003990 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 // ----------------------------------
3992
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003993 // JumpTargets do not yet support merging frames so the frame must be
3994 // spilled when jumping to these targets.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003995 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003996
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003997 // Generate fast case for loading functions from slots that
3998 // correspond to local/global variables or arguments unless they
3999 // are shadowed by eval-introduced bindings.
4000 EmitDynamicLoadFromSlotFastCase(var->slot(),
4001 NOT_INSIDE_TYPEOF,
4002 &slow,
4003 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004004
4005 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004007 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004008 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004009 frame_->EmitPush(r0);
4010 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011 // r0: slot value; r1: receiver
4012
4013 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004014 frame_->EmitPush(r0); // function
4015 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004017 // If fast case code has been generated, emit code to push the
4018 // function and receiver and have the slow path jump around this
4019 // code.
4020 if (done.is_linked()) {
4021 JumpTarget call;
4022 call.Jump();
4023 done.Bind();
4024 frame_->EmitPush(r0); // function
4025 LoadGlobalReceiver(r1); // receiver
4026 call.Bind();
4027 }
4028
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004029 // Call the function. At this point, everything is spilled but the
4030 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004031 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033
4034 } else if (property != NULL) {
4035 // Check if the key is a literal string.
4036 Literal* literal = property->key()->AsLiteral();
4037
4038 if (literal != NULL && literal->handle()->IsSymbol()) {
4039 // ------------------------------------------------------------------
4040 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4041 // ------------------------------------------------------------------
4042
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004043 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004045 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4046 name->IsEqualTo(CStrVector("apply")) &&
4047 args->length() == 2 &&
4048 args->at(1)->AsVariableProxy() != NULL &&
4049 args->at(1)->AsVariableProxy()->IsArguments()) {
4050 // Use the optimized Function.prototype.apply that avoids
4051 // allocating lazily allocated arguments objects.
4052 CallApplyLazy(property->obj(),
4053 args->at(0),
4054 args->at(1)->AsVariableProxy(),
4055 node->position());
4056
4057 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004058 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004059 // Load the arguments.
4060 int arg_count = args->length();
4061 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004062 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004063 }
4064
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004065 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004066 // Set the name register and call the IC initialization code.
4067 __ mov(r2, Operand(name));
4068 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4069 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4070 CodeForSourcePosition(node->position());
4071 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4072 __ ldr(cp, frame_->Context());
4073 frame_->EmitPush(r0);
4074 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075
4076 } else {
4077 // -------------------------------------------
4078 // JavaScript example: 'array[index](1, 2, 3)'
4079 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004080 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004081
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004082 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004084 Load(property->key());
4085 EmitKeyedLoad();
4086 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004087 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004088 frame_->EmitPush(r0); // Function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089 LoadGlobalReceiver(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004090 // Call the function.
4091 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4092 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004093 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004094 // Load the arguments.
4095 int arg_count = args->length();
4096 for (int i = 0; i < arg_count; i++) {
4097 Load(args->at(i));
4098 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004099
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004100 // Set the name register and call the IC initialization code.
4101 Load(property->key());
4102 frame_->EmitPop(r2); // Function name.
4103
4104 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4105 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4106 CodeForSourcePosition(node->position());
4107 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4108 __ ldr(cp, frame_->Context());
4109 frame_->EmitPush(r0);
4110 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004111 }
4112
4113 } else {
4114 // ----------------------------------
4115 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4116 // ----------------------------------
4117
4118 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004119 Load(function);
4120
4121 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004122
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004123 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004124 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004127 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004128 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004129 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004130 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004131}
4132
4133
ager@chromium.org7c537e22008-10-16 08:43:32 +00004134void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004135#ifdef DEBUG
4136 int original_height = frame_->height();
4137#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138 Comment cmnt(masm_, "[ CallNew");
4139
4140 // According to ECMA-262, section 11.2.2, page 44, the function
4141 // expression in new calls must be evaluated before the
4142 // arguments. This is different from ordinary calls, where the
4143 // actual function to call is resolved after the arguments have been
4144 // evaluated.
4145
4146 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004147 // receiver. There is no need to use the global proxy here because
4148 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004149 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004150 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004151
4152 // Push the arguments ("left-to-right") on the stack.
4153 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004154 int arg_count = args->length();
4155 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004156 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004157 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004158
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004159 VirtualFrame::SpilledScope spilled_scope(frame_);
4160
mads.s.ager31e71382008-08-13 09:32:07 +00004161 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004162 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004163 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004164 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004166 // Call the construct call builtin that handles allocation and
4167 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004168 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004169 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004170 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004171
4172 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004173 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004174 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004175}
4176
4177
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004178void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004179 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004180 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004181 JumpTarget leave, null, function, non_function_constructor;
4182
4183 // Load the object into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004184 Load(args->at(0));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004185 frame_->EmitPop(r0);
4186
4187 // If the object is a smi, we return null.
4188 __ tst(r0, Operand(kSmiTagMask));
4189 null.Branch(eq);
4190
4191 // Check that the object is a JS object but take special care of JS
4192 // functions to make sure they have 'Function' as their class.
4193 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
4194 null.Branch(lt);
4195
4196 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4197 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4198 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004199 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4200 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004201 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4202 function.Branch(eq);
4203
4204 // Check if the constructor in the map is a function.
4205 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
4206 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
4207 non_function_constructor.Branch(ne);
4208
4209 // The r0 register now contains the constructor function. Grab the
4210 // instance class name from there.
4211 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
4212 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004213 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004214 leave.Jump();
4215
4216 // Functions have class 'Function'.
4217 function.Bind();
4218 __ mov(r0, Operand(Factory::function_class_symbol()));
4219 frame_->EmitPush(r0);
4220 leave.Jump();
4221
4222 // Objects with a non-function constructor have class 'Object'.
4223 non_function_constructor.Bind();
4224 __ mov(r0, Operand(Factory::Object_symbol()));
4225 frame_->EmitPush(r0);
4226 leave.Jump();
4227
4228 // Non-JS objects have class null.
4229 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004230 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004231 frame_->EmitPush(r0);
4232
4233 // All done.
4234 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004235}
4236
4237
ager@chromium.org7c537e22008-10-16 08:43:32 +00004238void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004239 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004240 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004241 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004242 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004244 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004245 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004246 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004247 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4248 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004249 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004250 // Load the value.
4251 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004252 leave.Bind();
4253 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004254}
4255
4256
ager@chromium.org7c537e22008-10-16 08:43:32 +00004257void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004258 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004260 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004261 Load(args->at(0)); // Load the object.
4262 Load(args->at(1)); // Load the value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004263 frame_->EmitPop(r0); // r0 contains value
4264 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004265 // if (object->IsSmi()) return object.
4266 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004267 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004268 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4269 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004270 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004271 // Store the value.
4272 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
4273 // Update the write barrier.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004274 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004275 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004276 leave.Bind();
4277 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004278}
4279
4280
ager@chromium.org7c537e22008-10-16 08:43:32 +00004281void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004282 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004283 Load(args->at(0));
4284 Register reg = frame_->PopToRegister();
4285 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004286 cc_reg_ = eq;
4287}
4288
4289
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004290void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4291 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4292 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004293#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004294 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004295 Load(args->at(1));
4296 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004297 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004298 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004299#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004300 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004301}
4302
4303
ager@chromium.org7c537e22008-10-16 08:43:32 +00004304void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004305 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004306 Load(args->at(0));
4307 Register reg = frame_->PopToRegister();
4308 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004309 cc_reg_ = eq;
4310}
4311
4312
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004313// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004314void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4315 ASSERT(args->length() == 2);
4316 Load(args->at(0));
4317 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004318
4319 if (!CpuFeatures::IsSupported(VFP3)) {
4320 frame_->CallRuntime(Runtime::kMath_pow, 2);
4321 frame_->EmitPush(r0);
4322 } else {
4323 CpuFeatures::Scope scope(VFP3);
4324 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004325 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004326
4327 Register scratch1 = VirtualFrame::scratch0();
4328 Register scratch2 = VirtualFrame::scratch1();
4329
4330 // Get base and exponent to registers.
4331 Register exponent = frame_->PopToRegister();
4332 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004333 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004334
4335 // Set the frame for the runtime jump target. The code below jumps to the
4336 // jump target label so the frame needs to be established before that.
4337 ASSERT(runtime.entry_frame() == NULL);
4338 runtime.set_entry_frame(frame_);
4339
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004340 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4341 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004342
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004343 heap_number_map = r6;
4344 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4345
4346 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4347 // d1.
4348 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4349 __ b(&powi);
4350
4351 __ bind(&base_nonsmi);
4352 // Exponent is smi and base is non smi. Get the double value from the base
4353 // into vfp register d1.
4354 __ ObjectToDoubleVFPRegister(base, d1,
4355 scratch1, scratch2, heap_number_map, s0,
4356 runtime.entry_label());
4357
4358 __ bind(&powi);
4359
4360 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004361 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004362
4363 // Get the absolute untagged value of the exponent and use that for the
4364 // calculation.
4365 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4366 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4367 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4368
4369 // Run through all the bits in the exponent. The result is calculated in d0
4370 // and d1 holds base^(bit^2).
4371 Label more_bits;
4372 __ bind(&more_bits);
4373 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4374 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4375 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4376 __ b(ne, &more_bits);
4377
4378 // If exponent is positive we are done.
4379 __ cmp(exponent, Operand(0));
4380 __ b(ge, &allocate_return);
4381
4382 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4383 // case). However if d0 has reached infinity this will not provide the
4384 // correct result, so call runtime if that is the case.
4385 __ mov(scratch2, Operand(0x7FF00000));
4386 __ mov(scratch1, Operand(0));
4387 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4388 __ vcmp(d0, d1);
4389 __ vmrs(pc);
4390 runtime.Branch(eq); // d0 reached infinity.
4391 __ vdiv(d0, d2, d0);
4392 __ b(&allocate_return);
4393
4394 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004395 // Special handling of raising to the power of -0.5 and 0.5. First check
4396 // that the value is a heap number and that the lower bits (which for both
4397 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004398 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004399 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4400 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4401 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4402 __ cmp(scratch1, heap_number_map);
4403 runtime.Branch(ne);
4404 __ tst(scratch2, scratch2);
4405 runtime.Branch(ne);
4406
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004407 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004408 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4409
4410 // Compare exponent with -0.5.
4411 __ cmp(scratch1, Operand(0xbfe00000));
4412 __ b(ne, &not_minus_half);
4413
4414 // Get the double value from the base into vfp register d0.
4415 __ ObjectToDoubleVFPRegister(base, d0,
4416 scratch1, scratch2, heap_number_map, s0,
4417 runtime.entry_label(),
4418 AVOID_NANS_AND_INFINITIES);
4419
4420 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004421 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004422
4423 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4424 __ vdiv(d0, d2, d0);
4425 __ vsqrt(d0, d0);
4426
4427 __ b(&allocate_return);
4428
4429 __ bind(&not_minus_half);
4430 // Compare exponent with 0.5.
4431 __ cmp(scratch1, Operand(0x3fe00000));
4432 runtime.Branch(ne);
4433
4434 // Get the double value from the base into vfp register d0.
4435 __ ObjectToDoubleVFPRegister(base, d0,
4436 scratch1, scratch2, heap_number_map, s0,
4437 runtime.entry_label(),
4438 AVOID_NANS_AND_INFINITIES);
4439 __ vsqrt(d0, d0);
4440
4441 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004442 Register scratch3 = r5;
4443 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4444 heap_number_map, runtime.entry_label());
4445 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004446 done.Jump();
4447
4448 runtime.Bind();
4449
4450 // Push back the arguments again for the runtime call.
4451 frame_->EmitPush(base);
4452 frame_->EmitPush(exponent);
4453 frame_->CallRuntime(Runtime::kMath_pow, 2);
4454 __ Move(base, r0);
4455
4456 done.Bind();
4457 frame_->EmitPush(base);
4458 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004459}
4460
4461
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004462// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004463void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4464 ASSERT(args->length() == 1);
4465 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004466
4467 if (!CpuFeatures::IsSupported(VFP3)) {
4468 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4469 frame_->EmitPush(r0);
4470 } else {
4471 CpuFeatures::Scope scope(VFP3);
4472 JumpTarget runtime, done;
4473
4474 Register scratch1 = VirtualFrame::scratch0();
4475 Register scratch2 = VirtualFrame::scratch1();
4476
4477 // Get the value from the frame.
4478 Register tos = frame_->PopToRegister();
4479
4480 // Set the frame for the runtime jump target. The code below jumps to the
4481 // jump target label so the frame needs to be established before that.
4482 ASSERT(runtime.entry_frame() == NULL);
4483 runtime.set_entry_frame(frame_);
4484
4485 Register heap_number_map = r6;
4486 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4487
4488 // Get the double value from the heap number into vfp register d0.
4489 __ ObjectToDoubleVFPRegister(tos, d0,
4490 scratch1, scratch2, heap_number_map, s0,
4491 runtime.entry_label());
4492
4493 // Calculate the square root of d0 and place result in a heap number object.
4494 __ vsqrt(d0, d0);
4495 __ AllocateHeapNumberWithValue(
4496 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4497 done.Jump();
4498
4499 runtime.Bind();
4500 // Push back the argument again for the runtime call.
4501 frame_->EmitPush(tos);
4502 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4503 __ Move(tos, r0);
4504
4505 done.Bind();
4506 frame_->EmitPush(tos);
4507 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004508}
4509
4510
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004511class DeferredStringCharCodeAt : public DeferredCode {
4512 public:
4513 DeferredStringCharCodeAt(Register object,
4514 Register index,
4515 Register scratch,
4516 Register result)
4517 : result_(result),
4518 char_code_at_generator_(object,
4519 index,
4520 scratch,
4521 result,
4522 &need_conversion_,
4523 &need_conversion_,
4524 &index_out_of_range_,
4525 STRING_INDEX_IS_NUMBER) {}
4526
4527 StringCharCodeAtGenerator* fast_case_generator() {
4528 return &char_code_at_generator_;
4529 }
4530
4531 virtual void Generate() {
4532 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4533 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4534
4535 __ bind(&need_conversion_);
4536 // Move the undefined value into the result register, which will
4537 // trigger conversion.
4538 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4539 __ jmp(exit_label());
4540
4541 __ bind(&index_out_of_range_);
4542 // When the index is out of range, the spec requires us to return
4543 // NaN.
4544 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4545 __ jmp(exit_label());
4546 }
4547
4548 private:
4549 Register result_;
4550
4551 Label need_conversion_;
4552 Label index_out_of_range_;
4553
4554 StringCharCodeAtGenerator char_code_at_generator_;
4555};
4556
4557
4558// This generates code that performs a String.prototype.charCodeAt() call
4559// or returns a smi in order to trigger conversion.
4560void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
4561 VirtualFrame::SpilledScope spilled_scope(frame_);
4562 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004563 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004564
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004565 Load(args->at(0));
4566 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004567
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004568 Register index = r1;
4569 Register object = r2;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004570
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004571 frame_->EmitPop(r1);
4572 frame_->EmitPop(r2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004573
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004574 // We need two extra registers.
4575 Register scratch = r3;
4576 Register result = r0;
4577
4578 DeferredStringCharCodeAt* deferred =
4579 new DeferredStringCharCodeAt(object,
4580 index,
4581 scratch,
4582 result);
4583 deferred->fast_case_generator()->GenerateFast(masm_);
4584 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004585 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004586}
4587
4588
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004589class DeferredStringCharFromCode : public DeferredCode {
4590 public:
4591 DeferredStringCharFromCode(Register code,
4592 Register result)
4593 : char_from_code_generator_(code, result) {}
4594
4595 StringCharFromCodeGenerator* fast_case_generator() {
4596 return &char_from_code_generator_;
4597 }
4598
4599 virtual void Generate() {
4600 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4601 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4602 }
4603
4604 private:
4605 StringCharFromCodeGenerator char_from_code_generator_;
4606};
4607
4608
4609// Generates code for creating a one-char string from a char code.
4610void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
4611 VirtualFrame::SpilledScope spilled_scope(frame_);
4612 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004613 ASSERT(args->length() == 1);
4614
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004615 Load(args->at(0));
4616
ager@chromium.orgac091b72010-05-05 07:34:42 +00004617 Register code = r1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00004618 Register result = r0;
4619
ager@chromium.orgac091b72010-05-05 07:34:42 +00004620 frame_->EmitPop(code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004621
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004622 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4623 code, result);
4624 deferred->fast_case_generator()->GenerateFast(masm_);
4625 deferred->BindExit();
4626 frame_->EmitPush(result);
4627}
4628
4629
4630class DeferredStringCharAt : public DeferredCode {
4631 public:
4632 DeferredStringCharAt(Register object,
4633 Register index,
4634 Register scratch1,
4635 Register scratch2,
4636 Register result)
4637 : result_(result),
4638 char_at_generator_(object,
4639 index,
4640 scratch1,
4641 scratch2,
4642 result,
4643 &need_conversion_,
4644 &need_conversion_,
4645 &index_out_of_range_,
4646 STRING_INDEX_IS_NUMBER) {}
4647
4648 StringCharAtGenerator* fast_case_generator() {
4649 return &char_at_generator_;
4650 }
4651
4652 virtual void Generate() {
4653 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4654 char_at_generator_.GenerateSlow(masm(), call_helper);
4655
4656 __ bind(&need_conversion_);
4657 // Move smi zero into the result register, which will trigger
4658 // conversion.
4659 __ mov(result_, Operand(Smi::FromInt(0)));
4660 __ jmp(exit_label());
4661
4662 __ bind(&index_out_of_range_);
4663 // When the index is out of range, the spec requires us to return
4664 // the empty string.
4665 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4666 __ jmp(exit_label());
4667 }
4668
4669 private:
4670 Register result_;
4671
4672 Label need_conversion_;
4673 Label index_out_of_range_;
4674
4675 StringCharAtGenerator char_at_generator_;
4676};
4677
4678
4679// This generates code that performs a String.prototype.charAt() call
4680// or returns a smi in order to trigger conversion.
4681void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
4682 VirtualFrame::SpilledScope spilled_scope(frame_);
4683 Comment(masm_, "[ GenerateStringCharAt");
4684 ASSERT(args->length() == 2);
4685
4686 Load(args->at(0));
4687 Load(args->at(1));
4688
4689 Register index = r1;
4690 Register object = r2;
4691
4692 frame_->EmitPop(r1);
4693 frame_->EmitPop(r2);
4694
4695 // We need three extra registers.
4696 Register scratch1 = r3;
4697 Register scratch2 = r4;
4698 Register result = r0;
4699
4700 DeferredStringCharAt* deferred =
4701 new DeferredStringCharAt(object,
4702 index,
4703 scratch1,
4704 scratch2,
4705 result);
4706 deferred->fast_case_generator()->GenerateFast(masm_);
4707 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004708 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004709}
4710
4711
ager@chromium.org7c537e22008-10-16 08:43:32 +00004712void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004713 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004714 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004715 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004716 // We need the CC bits to come out as not_equal in the case where the
4717 // object is a smi. This can't be done with the usual test opcode so
4718 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004719 Register possible_array = frame_->PopToRegister();
4720 Register scratch = VirtualFrame::scratch0();
4721 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4722 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004723 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004724 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004725 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004726 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004727 cc_reg_ = eq;
4728}
4729
4730
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004731void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004732 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004733 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004734 JumpTarget answer;
4735 // We need the CC bits to come out as not_equal in the case where the
4736 // object is a smi. This can't be done with the usual test opcode so
4737 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004738 Register possible_regexp = frame_->PopToRegister();
4739 Register scratch = VirtualFrame::scratch0();
4740 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4741 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004742 answer.Branch(ne);
4743 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004744 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004745 answer.Bind();
4746 cc_reg_ = eq;
4747}
4748
4749
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004750void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4751 // This generates a fast version of:
4752 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004753 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004754 Load(args->at(0));
4755 Register possible_object = frame_->PopToRegister();
4756 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004757 false_target()->Branch(eq);
4758
4759 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004760 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004761 true_target()->Branch(eq);
4762
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004763 Register map_reg = VirtualFrame::scratch0();
4764 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004765 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004766 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4767 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004768 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004769
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004770 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4771 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004772 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004773 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004774 cc_reg_ = le;
4775}
4776
4777
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004778void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4779 // This generates a fast version of:
4780 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4781 // typeof(arg) == function).
4782 // It includes undetectable objects (as opposed to IsObject).
4783 ASSERT(args->length() == 1);
4784 Load(args->at(0));
4785 Register value = frame_->PopToRegister();
4786 __ tst(value, Operand(kSmiTagMask));
4787 false_target()->Branch(eq);
4788 // Check that this is an object.
4789 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4790 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4791 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4792 cc_reg_ = ge;
4793}
4794
4795
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004796void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4797 // This generates a fast version of:
4798 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004799 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004800 Load(args->at(0));
4801 Register possible_function = frame_->PopToRegister();
4802 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004803 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004804 Register map_reg = VirtualFrame::scratch0();
4805 Register scratch = VirtualFrame::scratch1();
4806 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004807 cc_reg_ = eq;
4808}
4809
4810
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004811void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004812 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004813 Load(args->at(0));
4814 Register possible_undetectable = frame_->PopToRegister();
4815 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004816 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004817 Register scratch = VirtualFrame::scratch0();
4818 __ ldr(scratch,
4819 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4820 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4821 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004822 cc_reg_ = ne;
4823}
4824
4825
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004826void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004827 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004828
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004829 Register scratch0 = VirtualFrame::scratch0();
4830 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004831 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004832 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004833
4834 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004835 __ ldr(scratch1,
4836 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4837 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4838 __ ldr(scratch0,
4839 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004840
4841 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004842 __ ldr(scratch1,
4843 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
4844 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004845 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004846}
4847
4848
ager@chromium.org7c537e22008-10-16 08:43:32 +00004849void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004850 ASSERT(args->length() == 0);
4851
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004852 Register tos = frame_->GetTOSRegister();
4853 Register scratch0 = VirtualFrame::scratch0();
4854 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004855
lrn@chromium.org25156de2010-04-06 13:10:27 +00004856 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004857 __ ldr(scratch0,
4858 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4859 __ ldr(scratch1,
4860 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4861 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4862
4863 // Get the number of formal parameters.
4864 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004865
4866 // Arguments adaptor case: Read the arguments length from the
4867 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004868 __ ldr(tos,
4869 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
4870 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004871
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004872 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004873}
4874
4875
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004876void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004877 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004878 ASSERT(args->length() == 1);
4879
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004880 // Satisfy contract with ArgumentsAccessStub:
4881 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004882 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004883 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004884 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004885
4886 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004887 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004888 frame_->CallStub(&stub, 0);
4889 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004890}
4891
4892
ager@chromium.org357bf652010-04-12 11:30:10 +00004893void CodeGenerator::GenerateRandomHeapNumber(
4894 ZoneList<Expression*>* args) {
4895 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004896 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004897
4898 Label slow_allocate_heapnumber;
4899 Label heapnumber_allocated;
4900
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004901 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
4902 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004903 __ jmp(&heapnumber_allocated);
4904
4905 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004906 // Allocate a heap number.
4907 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004908 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004909
4910 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004911
4912 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4913 // by computing:
4914 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4915 if (CpuFeatures::IsSupported(VFP3)) {
4916 __ PrepareCallCFunction(0, r1);
4917 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4918
4919 CpuFeatures::Scope scope(VFP3);
4920 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4921 // Create this constant using mov/orr to avoid PC relative load.
4922 __ mov(r1, Operand(0x41000000));
4923 __ orr(r1, r1, Operand(0x300000));
4924 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4925 __ vmov(d7, r0, r1);
4926 // Move 0x4130000000000000 to VFP.
4927 __ mov(r0, Operand(0));
4928 __ vmov(d8, r0, r1);
4929 // Subtract and store the result in the heap number.
4930 __ vsub(d7, d7, d8);
4931 __ sub(r0, r4, Operand(kHeapObjectTag));
4932 __ vstr(d7, r0, HeapNumber::kValueOffset);
4933 frame_->EmitPush(r4);
4934 } else {
4935 __ mov(r0, Operand(r4));
4936 __ PrepareCallCFunction(1, r1);
4937 __ CallCFunction(
4938 ExternalReference::fill_heap_number_with_random_function(), 1);
4939 frame_->EmitPush(r0);
4940 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004941}
4942
4943
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004944void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4945 ASSERT_EQ(2, args->length());
4946
4947 Load(args->at(0));
4948 Load(args->at(1));
4949
ager@chromium.org5c838252010-02-19 08:53:10 +00004950 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004951 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004952 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004953 frame_->EmitPush(r0);
4954}
4955
4956
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004957void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4958 ASSERT_EQ(3, args->length());
4959
4960 Load(args->at(0));
4961 Load(args->at(1));
4962 Load(args->at(2));
4963
ager@chromium.org5c838252010-02-19 08:53:10 +00004964 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004965 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004966 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004967 frame_->EmitPush(r0);
4968}
4969
4970
4971void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4972 ASSERT_EQ(2, args->length());
4973
4974 Load(args->at(0));
4975 Load(args->at(1));
4976
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004977 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004978 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004979 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004980 frame_->EmitPush(r0);
4981}
4982
4983
4984void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4985 ASSERT_EQ(4, args->length());
4986
4987 Load(args->at(0));
4988 Load(args->at(1));
4989 Load(args->at(2));
4990 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004991 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004992 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004993 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004994 frame_->EmitPush(r0);
4995}
4996
4997
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004998void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4999 // No stub. This code only occurs a few times in regexp.js.
5000 const int kMaxInlineLength = 100;
5001 ASSERT_EQ(3, args->length());
5002 Load(args->at(0)); // Size of array, smi.
5003 Load(args->at(1)); // "index" property value.
5004 Load(args->at(2)); // "input" property value.
5005 {
5006 VirtualFrame::SpilledScope spilled_scope(frame_);
5007 Label slowcase;
5008 Label done;
5009 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5010 STATIC_ASSERT(kSmiTag == 0);
5011 STATIC_ASSERT(kSmiTagSize == 1);
5012 __ tst(r1, Operand(kSmiTagMask));
5013 __ b(ne, &slowcase);
5014 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5015 __ b(hi, &slowcase);
5016 // Smi-tagging is equivalent to multiplying by 2.
5017 // Allocate RegExpResult followed by FixedArray with size in ebx.
5018 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5019 // Elements: [Map][Length][..elements..]
5020 // Size of JSArray with two in-object properties and the header of a
5021 // FixedArray.
5022 int objects_size =
5023 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5024 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5025 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005026 __ AllocateInNewSpace(
5027 r2, // In: Size, in words.
5028 r0, // Out: Start of allocation (tagged).
5029 r3, // Scratch register.
5030 r4, // Scratch register.
5031 &slowcase,
5032 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005033 // r0: Start of allocated area, object-tagged.
5034 // r1: Number of elements in array, as smi.
5035 // r5: Number of elements, untagged.
5036
5037 // Set JSArray map to global.regexp_result_map().
5038 // Set empty properties FixedArray.
5039 // Set elements to point to FixedArray allocated right after the JSArray.
5040 // Interleave operations for better latency.
5041 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5042 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5043 __ mov(r4, Operand(Factory::empty_fixed_array()));
5044 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5045 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5046 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5047 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5048 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5049
5050 // Set input, index and length fields from arguments.
5051 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5052 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5053 __ add(sp, sp, Operand(kPointerSize));
5054 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5055 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5056
5057 // Fill out the elements FixedArray.
5058 // r0: JSArray, tagged.
5059 // r3: FixedArray, tagged.
5060 // r5: Number of elements in array, untagged.
5061
5062 // Set map.
5063 __ mov(r2, Operand(Factory::fixed_array_map()));
5064 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5065 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005066 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5067 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005068 // Fill contents of fixed-array with the-hole.
5069 __ mov(r2, Operand(Factory::the_hole_value()));
5070 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5071 // Fill fixed array elements with hole.
5072 // r0: JSArray, tagged.
5073 // r2: the hole.
5074 // r3: Start of elements in FixedArray.
5075 // r5: Number of elements to fill.
5076 Label loop;
5077 __ tst(r5, Operand(r5));
5078 __ bind(&loop);
5079 __ b(le, &done); // Jump if r1 is negative or zero.
5080 __ sub(r5, r5, Operand(1), SetCC);
5081 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5082 __ jmp(&loop);
5083
5084 __ bind(&slowcase);
5085 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5086
5087 __ bind(&done);
5088 }
5089 frame_->Forget(3);
5090 frame_->EmitPush(r0);
5091}
5092
5093
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005094class DeferredSearchCache: public DeferredCode {
5095 public:
5096 DeferredSearchCache(Register dst, Register cache, Register key)
5097 : dst_(dst), cache_(cache), key_(key) {
5098 set_comment("[ DeferredSearchCache");
5099 }
5100
5101 virtual void Generate();
5102
5103 private:
5104 Register dst_, cache_, key_;
5105};
5106
5107
5108void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005109 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005110 __ CallRuntime(Runtime::kGetFromCache, 2);
5111 if (!dst_.is(r0)) {
5112 __ mov(dst_, r0);
5113 }
5114}
5115
5116
5117void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5118 ASSERT_EQ(2, args->length());
5119
5120 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5121 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5122
5123 Handle<FixedArray> jsfunction_result_caches(
5124 Top::global_context()->jsfunction_result_caches());
5125 if (jsfunction_result_caches->length() <= cache_id) {
5126 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005127 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005128 return;
5129 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005130
5131 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005132
5133 VirtualFrame::SpilledScope spilled_scope(frame_);
5134
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005135 frame_->EmitPop(r2);
5136
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005137 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
5138 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
5139 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5140 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
5141
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005142 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
5143
5144 const int kFingerOffset =
5145 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005146 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005147 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
5148 // r0 now holds finger offset as a smi.
5149 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5150 // r3 now points to the start of fixed array elements.
5151 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5152 // Note side effect of PreIndex: r3 now points to the key of the pair.
5153 __ cmp(r2, r0);
5154 deferred->Branch(ne);
5155
5156 __ ldr(r0, MemOperand(r3, kPointerSize));
5157
5158 deferred->BindExit();
5159 frame_->EmitPush(r0);
5160}
5161
5162
ager@chromium.org5c838252010-02-19 08:53:10 +00005163void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5164 ASSERT_EQ(args->length(), 1);
5165
5166 // Load the argument on the stack and jump to the runtime.
5167 Load(args->at(0));
5168
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005169 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005170 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005171 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005172 frame_->EmitPush(r0);
5173}
5174
5175
ager@chromium.orgac091b72010-05-05 07:34:42 +00005176class DeferredSwapElements: public DeferredCode {
5177 public:
5178 DeferredSwapElements(Register object, Register index1, Register index2)
5179 : object_(object), index1_(index1), index2_(index2) {
5180 set_comment("[ DeferredSwapElements");
5181 }
5182
5183 virtual void Generate();
5184
5185 private:
5186 Register object_, index1_, index2_;
5187};
5188
5189
5190void DeferredSwapElements::Generate() {
5191 __ push(object_);
5192 __ push(index1_);
5193 __ push(index2_);
5194 __ CallRuntime(Runtime::kSwapElements, 3);
5195}
5196
5197
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005198void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5199 Comment cmnt(masm_, "[ GenerateSwapElements");
5200
5201 ASSERT_EQ(3, args->length());
5202
5203 Load(args->at(0));
5204 Load(args->at(1));
5205 Load(args->at(2));
5206
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005207 VirtualFrame::SpilledScope spilled_scope(frame_);
5208
ager@chromium.orgac091b72010-05-05 07:34:42 +00005209 Register index2 = r2;
5210 Register index1 = r1;
5211 Register object = r0;
5212 Register tmp1 = r3;
5213 Register tmp2 = r4;
5214
5215 frame_->EmitPop(index2);
5216 frame_->EmitPop(index1);
5217 frame_->EmitPop(object);
5218
5219 DeferredSwapElements* deferred =
5220 new DeferredSwapElements(object, index1, index2);
5221
5222 // Fetch the map and check if array is in fast case.
5223 // Check that object doesn't require security checks and
5224 // has no indexed interceptor.
5225 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5226 deferred->Branch(lt);
5227 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5228 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5229 deferred->Branch(nz);
5230
5231 // Check the object's elements are in fast case.
5232 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5233 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5234 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5235 __ cmp(tmp2, ip);
5236 deferred->Branch(ne);
5237
5238 // Smi-tagging is equivalent to multiplying by 2.
5239 STATIC_ASSERT(kSmiTag == 0);
5240 STATIC_ASSERT(kSmiTagSize == 1);
5241
5242 // Check that both indices are smis.
5243 __ mov(tmp2, index1);
5244 __ orr(tmp2, tmp2, index2);
5245 __ tst(tmp2, Operand(kSmiTagMask));
5246 deferred->Branch(nz);
5247
5248 // Bring the offsets into the fixed array in tmp1 into index1 and
5249 // index2.
5250 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5251 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5252 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5253
5254 // Swap elements.
5255 Register tmp3 = object;
5256 object = no_reg;
5257 __ ldr(tmp3, MemOperand(tmp1, index1));
5258 __ ldr(tmp2, MemOperand(tmp1, index2));
5259 __ str(tmp3, MemOperand(tmp1, index2));
5260 __ str(tmp2, MemOperand(tmp1, index1));
5261
5262 Label done;
5263 __ InNewSpace(tmp1, tmp2, eq, &done);
5264 // Possible optimization: do a check that both values are Smis
5265 // (or them and test against Smi mask.)
5266
5267 __ mov(tmp2, tmp1);
5268 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5269 __ CallStub(&recordWrite1);
5270
5271 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5272 __ CallStub(&recordWrite2);
5273
5274 __ bind(&done);
5275
5276 deferred->BindExit();
5277 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5278 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005279}
5280
5281
ager@chromium.org357bf652010-04-12 11:30:10 +00005282void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5283 Comment cmnt(masm_, "[ GenerateCallFunction");
5284
5285 ASSERT(args->length() >= 2);
5286
5287 int n_args = args->length() - 2; // for receiver and function.
5288 Load(args->at(0)); // receiver
5289 for (int i = 0; i < n_args; i++) {
5290 Load(args->at(i + 1));
5291 }
5292 Load(args->at(n_args + 1)); // function
5293 frame_->CallJSFunction(n_args);
5294 frame_->EmitPush(r0);
5295}
5296
5297
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005298void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5299 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005300 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005301 if (CpuFeatures::IsSupported(VFP3)) {
5302 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5303 frame_->SpillAllButCopyTOSToR0();
5304 frame_->CallStub(&stub, 1);
5305 } else {
5306 frame_->CallRuntime(Runtime::kMath_sin, 1);
5307 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005308 frame_->EmitPush(r0);
5309}
5310
5311
5312void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5313 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005314 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005315 if (CpuFeatures::IsSupported(VFP3)) {
5316 TranscendentalCacheStub stub(TranscendentalCache::COS);
5317 frame_->SpillAllButCopyTOSToR0();
5318 frame_->CallStub(&stub, 1);
5319 } else {
5320 frame_->CallRuntime(Runtime::kMath_cos, 1);
5321 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005322 frame_->EmitPush(r0);
5323}
5324
5325
ager@chromium.org7c537e22008-10-16 08:43:32 +00005326void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005327 ASSERT(args->length() == 2);
5328
5329 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005330 Load(args->at(0));
5331 Load(args->at(1));
5332 Register lhs = frame_->PopToRegister();
5333 Register rhs = frame_->PopToRegister(lhs);
5334 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005335 cc_reg_ = eq;
5336}
5337
5338
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005339void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5340 ASSERT(args->length() == 2);
5341
5342 // Load the two objects into registers and perform the comparison.
5343 Load(args->at(0));
5344 Load(args->at(1));
5345 Register right = frame_->PopToRegister();
5346 Register left = frame_->PopToRegister(right);
5347 Register tmp = frame_->scratch0();
5348 Register tmp2 = frame_->scratch1();
5349
5350 // Jumps to done must have the eq flag set if the test is successful
5351 // and clear if the test has failed.
5352 Label done;
5353
5354 // Fail if either is a non-HeapObject.
5355 __ cmp(left, Operand(right));
5356 __ b(eq, &done);
5357 __ and_(tmp, left, Operand(right));
5358 __ eor(tmp, tmp, Operand(kSmiTagMask));
5359 __ tst(tmp, Operand(kSmiTagMask));
5360 __ b(ne, &done);
5361 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5362 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5363 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5364 __ b(ne, &done);
5365 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5366 __ cmp(tmp, Operand(tmp2));
5367 __ b(ne, &done);
5368 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5369 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5370 __ cmp(tmp, tmp2);
5371 __ bind(&done);
5372 cc_reg_ = eq;
5373}
5374
5375
5376
ager@chromium.org7c537e22008-10-16 08:43:32 +00005377void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005378#ifdef DEBUG
5379 int original_height = frame_->height();
5380#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005381 if (CheckForInlineRuntimeCall(node)) {
5382 ASSERT((has_cc() && frame_->height() == original_height) ||
5383 (!has_cc() && frame_->height() == original_height + 1));
5384 return;
5385 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005386
5387 ZoneList<Expression*>* args = node->arguments();
5388 Comment cmnt(masm_, "[ CallRuntime");
5389 Runtime::Function* function = node->function();
5390
ager@chromium.org41826e72009-03-30 13:30:57 +00005391 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005392 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005393 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005394 Register scratch = VirtualFrame::scratch0();
5395 __ ldr(scratch, GlobalObject());
5396 Register builtins = frame_->GetTOSRegister();
5397 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5398 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005399 }
mads.s.ager31e71382008-08-13 09:32:07 +00005400
ager@chromium.org41826e72009-03-30 13:30:57 +00005401 // Push the arguments ("left-to-right").
5402 int arg_count = args->length();
5403 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005404 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005405 }
mads.s.ager31e71382008-08-13 09:32:07 +00005406
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005407 VirtualFrame::SpilledScope spilled_scope(frame_);
5408
ager@chromium.org41826e72009-03-30 13:30:57 +00005409 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005410 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005411 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005412 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5413 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005414 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005415 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005416 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005417 } else {
5418 // Call the C runtime function.
5419 frame_->CallRuntime(function, arg_count);
5420 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005421 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005422 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005423}
5424
5425
ager@chromium.org7c537e22008-10-16 08:43:32 +00005426void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005427#ifdef DEBUG
5428 int original_height = frame_->height();
5429#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005430 Comment cmnt(masm_, "[ UnaryOperation");
5431
5432 Token::Value op = node->op();
5433
5434 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005435 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005436 // LoadCondition may (and usually does) leave a test and branch to
5437 // be emitted by the caller. In that case, negate the condition.
5438 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005439
5440 } else if (op == Token::DELETE) {
5441 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005442 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005443 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005444 Load(property->obj());
5445 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005446 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005447 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005448
mads.s.ager31e71382008-08-13 09:32:07 +00005449 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005450 Slot* slot = variable->slot();
5451 if (variable->is_global()) {
5452 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005453 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005454 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005455 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005456
5457 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5458 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005459 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005460 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005461 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005462 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005463 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005464 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005465 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005466 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005467
mads.s.ager31e71382008-08-13 09:32:07 +00005468 } else {
5469 // Default: Result of deleting non-global, not dynamically
5470 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005471 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005472 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005473
5474 } else {
5475 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005476 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005477 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005478 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005479 }
5480
5481 } else if (op == Token::TYPEOF) {
5482 // Special case for loading the typeof expression; see comment on
5483 // LoadTypeofExpression().
5484 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005485 frame_->CallRuntime(Runtime::kTypeof, 1);
5486 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005487
5488 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005489 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005490 (node->expression()->AsBinaryOperation() != NULL &&
5491 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005492 UnaryOverwriteMode overwrite =
5493 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5494
5495 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005496 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005497 switch (op) {
5498 case Token::NOT:
5499 case Token::DELETE:
5500 case Token::TYPEOF:
5501 UNREACHABLE(); // handled above
5502 break;
5503
5504 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005505 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005506 GenericUnaryOpStub stub(
5507 Token::SUB,
5508 overwrite,
5509 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005510 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005511 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005512 break;
5513 }
5514
5515 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005516 Register tos = frame_->PopToRegister();
5517 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005518 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005519 // Smi check.
5520 __ tst(tos, Operand(kSmiTagMask));
5521 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005522
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005523 __ mvn(tos, Operand(tos));
5524 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5525 frame_->EmitPush(tos);
5526 // The fast case is the first to jump to the continue label, so it gets
5527 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005528 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005529
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005530 not_smi_label.Bind();
5531 frame_->SpillAll();
5532 __ Move(r0, tos);
5533 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5534 frame_->CallStub(&stub, 0);
5535 frame_->EmitPush(r0);
5536
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005537 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005538 break;
5539 }
5540
5541 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005542 frame_->Drop();
5543 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005544 break;
5545
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005546 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005547 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005548 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005549 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005550 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005551 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005552
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005553 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005554 frame_->EmitPush(r0);
5555
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005556 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005557 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005558 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005559 default:
5560 UNREACHABLE();
5561 }
5562 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005563 ASSERT(!has_valid_frame() ||
5564 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005565 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005566}
5567
5568
ager@chromium.org7c537e22008-10-16 08:43:32 +00005569void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005570#ifdef DEBUG
5571 int original_height = frame_->height();
5572#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005573 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005574 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005575
5576 bool is_postfix = node->is_postfix();
5577 bool is_increment = node->op() == Token::INC;
5578
5579 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5580 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005581 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005582
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005583 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5584 // The type info declares that this variable is always a Smi. That
5585 // means it is a Smi both before and after the increment/decrement.
5586 // Lets make use of that to make a very minimal count.
5587 Reference target(this, node->expression(), !is_const);
5588 ASSERT(!target.is_illegal());
5589 target.GetValue(); // Pushes the value.
5590 Register value = frame_->PopToRegister();
5591 if (is_postfix) frame_->EmitPush(value);
5592 if (is_increment) {
5593 __ add(value, value, Operand(Smi::FromInt(1)));
5594 } else {
5595 __ sub(value, value, Operand(Smi::FromInt(1)));
5596 }
5597 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005598 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005599 if (is_postfix) frame_->Pop();
5600 ASSERT_EQ(original_height + 1, frame_->height());
5601 return;
5602 }
5603
5604 // If it's a postfix expression and its result is not ignored and the
5605 // reference is non-trivial, then push a placeholder on the stack now
5606 // to hold the result of the expression.
5607 bool placeholder_pushed = false;
5608 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005609 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005610 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005611 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005612
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005613 // A constant reference is not saved to, so a constant reference is not a
5614 // compound assignment reference.
5615 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005616 if (target.is_illegal()) {
5617 // Spoof the virtual frame to have the expected height (one higher
5618 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005619 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005620 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005621 return;
5622 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005623
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005624 // This pushes 0, 1 or 2 words on the object to be used later when updating
5625 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005626 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005627
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005628 JumpTarget slow;
5629 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005630
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005631 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005632
5633 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005634 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005635 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005636 } else if (is_postfix) {
5637 frame_->EmitPush(value);
5638 __ mov(VirtualFrame::scratch0(), value);
5639 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005641
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005642 // Check for smi operand.
5643 __ tst(value, Operand(kSmiTagMask));
5644 slow.Branch(ne);
5645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005646 // Perform optimistic increment/decrement.
5647 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005648 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005649 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005650 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005651 }
5652
5653 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005654 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005655
5656 // Revert optimistic increment/decrement.
5657 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005658 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005659 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005660 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005661 }
5662
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005663 // Slow case: Convert to number. At this point the
5664 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005665 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005666
5667 // Convert the operand to a number.
5668 frame_->EmitPush(value);
5669
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005670 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005671 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005672 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005673
5674 if (is_postfix) {
5675 // Postfix: store to result (on the stack).
5676 __ str(r0, frame_->ElementAt(target.size()));
5677 }
5678
5679 // Compute the new value.
5680 frame_->EmitPush(r0);
5681 frame_->EmitPush(Operand(Smi::FromInt(1)));
5682 if (is_increment) {
5683 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5684 } else {
5685 frame_->CallRuntime(Runtime::kNumberSub, 2);
5686 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005687 }
5688
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005689 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005691 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005692 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005693 frame_->EmitPush(value);
5694 // Set the target with the result, leaving the result on
5695 // top of the stack. Removes the target from the stack if
5696 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005697 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005698 }
5699
5700 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005701 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005702 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005703}
5704
5705
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005706void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005707 // According to ECMA-262 section 11.11, page 58, the binary logical
5708 // operators must yield the result of one of the two expressions
5709 // before any ToBoolean() conversions. This means that the value
5710 // produced by a && or || operator is not necessarily a boolean.
5711
5712 // NOTE: If the left hand side produces a materialized value (not in
5713 // the CC register), we force the right hand side to do the
5714 // same. This is necessary because we may have to branch to the exit
5715 // after evaluating the left hand side (due to the shortcut
5716 // semantics), but the compiler must (statically) know if the result
5717 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005718 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005719 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005720 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005721 if (has_valid_frame() && !has_cc()) {
5722 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005723 JumpTarget pop_and_continue;
5724 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005725
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005726 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005727 // Avoid popping the result if it converts to 'false' using the
5728 // standard ToBoolean() conversion as described in ECMA-262,
5729 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005730 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005731 Branch(false, &exit);
5732
5733 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005734 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005735 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005736
5737 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005738 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005739 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005740
5741 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005742 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005743 } else if (has_cc() || is_true.is_linked()) {
5744 // The left-hand side is either (a) partially compiled to
5745 // control flow with a final branch left to emit or (b) fully
5746 // compiled to control flow and possibly true.
5747 if (has_cc()) {
5748 Branch(false, false_target());
5749 }
5750 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005751 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005752 } else {
5753 // Nothing to do.
5754 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005755 }
5756
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005757 } else {
5758 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005759 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005760 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005761 if (has_valid_frame() && !has_cc()) {
5762 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005763 JumpTarget pop_and_continue;
5764 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005765
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005766 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005767 // Avoid popping the result if it converts to 'true' using the
5768 // standard ToBoolean() conversion as described in ECMA-262,
5769 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005770 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005771 Branch(true, &exit);
5772
5773 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005774 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005775 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005776
5777 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005778 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005779 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005780
5781 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005782 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005783 } else if (has_cc() || is_false.is_linked()) {
5784 // The left-hand side is either (a) partially compiled to
5785 // control flow with a final branch left to emit or (b) fully
5786 // compiled to control flow and possibly false.
5787 if (has_cc()) {
5788 Branch(true, true_target());
5789 }
5790 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005791 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005792 } else {
5793 // Nothing to do.
5794 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005795 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005796 }
5797}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005798
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005799
5800void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5801#ifdef DEBUG
5802 int original_height = frame_->height();
5803#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005804 Comment cmnt(masm_, "[ BinaryOperation");
5805
5806 if (node->op() == Token::AND || node->op() == Token::OR) {
5807 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005808 } else {
5809 // Optimize for the case where (at least) one of the expressions
5810 // is a literal small integer.
5811 Literal* lliteral = node->left()->AsLiteral();
5812 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005813 // NOTE: The code below assumes that the slow cases (calls to runtime)
5814 // never return a constant/immutable object.
5815 bool overwrite_left =
5816 (node->left()->AsBinaryOperation() != NULL &&
5817 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5818 bool overwrite_right =
5819 (node->right()->AsBinaryOperation() != NULL &&
5820 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005821
5822 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005823 VirtualFrame::RegisterAllocationScope scope(this);
5824 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005825 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005826 SmiOperation(node->op(),
5827 rliteral->handle(),
5828 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005829 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005830 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005831 VirtualFrame::RegisterAllocationScope scope(this);
5832 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005833 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005834 SmiOperation(node->op(),
5835 lliteral->handle(),
5836 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005837 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005838 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005839 GenerateInlineSmi inline_smi =
5840 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
5841 if (lliteral != NULL) {
5842 ASSERT(!lliteral->handle()->IsSmi());
5843 inline_smi = DONT_GENERATE_INLINE_SMI;
5844 }
5845 if (rliteral != NULL) {
5846 ASSERT(!rliteral->handle()->IsSmi());
5847 inline_smi = DONT_GENERATE_INLINE_SMI;
5848 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005849 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005850 OverwriteMode overwrite_mode = NO_OVERWRITE;
5851 if (overwrite_left) {
5852 overwrite_mode = OVERWRITE_LEFT;
5853 } else if (overwrite_right) {
5854 overwrite_mode = OVERWRITE_RIGHT;
5855 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005856 Load(node->left());
5857 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005858 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005859 }
5860 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005861 ASSERT(!has_valid_frame() ||
5862 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005863 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005864}
5865
5866
ager@chromium.org7c537e22008-10-16 08:43:32 +00005867void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005868#ifdef DEBUG
5869 int original_height = frame_->height();
5870#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005871 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005872 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005873}
5874
5875
ager@chromium.org7c537e22008-10-16 08:43:32 +00005876void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005877#ifdef DEBUG
5878 int original_height = frame_->height();
5879#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005880 Comment cmnt(masm_, "[ CompareOperation");
5881
ager@chromium.org357bf652010-04-12 11:30:10 +00005882 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5883
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005884 // Get the expressions from the node.
5885 Expression* left = node->left();
5886 Expression* right = node->right();
5887 Token::Value op = node->op();
5888
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005889 // To make null checks efficient, we check if either left or right is the
5890 // literal 'null'. If so, we optimize the code by inlining a null check
5891 // instead of calling the (very) general runtime routine for checking
5892 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005893 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005894 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005895 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005896 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005897 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5898 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005899 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005900 Load(left_is_null ? right : left);
5901 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005902 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005903 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005904
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005905 // The 'null' value is only equal to 'undefined' if using non-strict
5906 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005907 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005908 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005909
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005910 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005911 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005912 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005913
ager@chromium.org357bf652010-04-12 11:30:10 +00005914 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005915 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005916
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005917 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005918 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5919 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5920 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5921 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005922 }
5923
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005924 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005925 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005926 return;
5927 }
5928 }
5929
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005930 // To make typeof testing for natives implemented in JavaScript really
5931 // efficient, we generate special code for expressions of the form:
5932 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005933 UnaryOperation* operation = left->AsUnaryOperation();
5934 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5935 (operation != NULL && operation->op() == Token::TYPEOF) &&
5936 (right->AsLiteral() != NULL &&
5937 right->AsLiteral()->handle()->IsString())) {
5938 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5939
ager@chromium.org357bf652010-04-12 11:30:10 +00005940 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005941 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005942 Register tos = frame_->PopToRegister();
5943
ager@chromium.org357bf652010-04-12 11:30:10 +00005944 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005945
5946 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005947 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005948 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005949 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005950 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005951 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005952 cc_reg_ = eq;
5953
5954 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005955 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005956 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005957
ager@chromium.org357bf652010-04-12 11:30:10 +00005958 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005959
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005960 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005961 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5962 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5963 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005964 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005965
ager@chromium.org357bf652010-04-12 11:30:10 +00005966 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5967 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005968 cc_reg_ = lt;
5969
5970 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005971 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005972 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005973 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005974 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005975 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005976 cc_reg_ = eq;
5977
5978 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005979 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005980 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005981 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005982
ager@chromium.org357bf652010-04-12 11:30:10 +00005983 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005984 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005985
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005986 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005987 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5988 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5989 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5990 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005991
5992 cc_reg_ = eq;
5993
5994 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005995 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005996 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005997 Register map_reg = scratch;
5998 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005999 true_target()->Branch(eq);
6000 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006001 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006002 cc_reg_ = eq;
6003
6004 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006005 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006006 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006007
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006008 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006009 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006010 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006011
ager@chromium.org357bf652010-04-12 11:30:10 +00006012 Register map_reg = scratch;
6013 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006014 false_target()->Branch(eq);
6015
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006016 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006017 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6018 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6019 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006020 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006021
ager@chromium.org357bf652010-04-12 11:30:10 +00006022 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6023 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006024 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006025 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006026 cc_reg_ = le;
6027
6028 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006029 // Uncommon case: typeof testing against a string literal that is
6030 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006031 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006032 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006033 ASSERT(!has_valid_frame() ||
6034 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006035 return;
6036 }
6037
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006038 switch (op) {
6039 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006040 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006041 break;
6042
6043 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006044 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006045 break;
6046
6047 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006048 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006049 break;
6050
6051 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006052 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006053 break;
6054
6055 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006056 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006057 break;
6058
6059 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006060 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006061 break;
6062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006063 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006064 Load(left);
6065 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006066 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006067 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006068 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006069 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006070
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006071 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006072 Load(left);
6073 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006074 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006075 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006076 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006077 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006078 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006079 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006080 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006081
6082 default:
6083 UNREACHABLE();
6084 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006085 ASSERT((has_cc() && frame_->height() == original_height) ||
6086 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087}
6088
6089
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006090class DeferredReferenceGetNamedValue: public DeferredCode {
6091 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006092 explicit DeferredReferenceGetNamedValue(Register receiver,
6093 Handle<String> name)
6094 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006095 set_comment("[ DeferredReferenceGetNamedValue");
6096 }
6097
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006098 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006099
6100 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006101 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006102 Handle<String> name_;
6103};
6104
6105
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006106// Convention for this is that on entry the receiver is in a register that
6107// is not used by the stack. On exit the answer is found in that same
6108// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006109void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006110#ifdef DEBUG
6111 int expected_height = frame_state()->frame()->height();
6112#endif
6113 VirtualFrame copied_frame(*frame_state()->frame());
6114 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006115
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006116 Register scratch1 = VirtualFrame::scratch0();
6117 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006118 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006119 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6120 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6121
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006122 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6123 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006124 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006125
6126 // The rest of the instructions in the deferred code must be together.
6127 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6128 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6129 __ Call(ic, RelocInfo::CODE_TARGET);
6130 // The call must be followed by a nop(1) instruction to indicate that the
6131 // in-object has been inlined.
6132 __ nop(PROPERTY_ACCESS_INLINED);
6133
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006134 // At this point the answer is in r0. We move it to the expected register
6135 // if necessary.
6136 __ Move(receiver_, r0);
6137
6138 // Now go back to the frame that we entered with. This will not overwrite
6139 // the receiver register since that register was not in use when we came
6140 // in. The instructions emitted by this merge are skipped over by the
6141 // inline load patching mechanism when looking for the branch instruction
6142 // that tells it where the code to patch is.
6143 copied_frame.MergeTo(frame_state()->frame());
6144
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006145 // Block the constant pool for one more instruction after leaving this
6146 // constant pool block scope to include the branch instruction ending the
6147 // deferred code.
6148 __ BlockConstPoolFor(1);
6149 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006150 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006151}
6152
6153
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006154class DeferredReferenceGetKeyedValue: public DeferredCode {
6155 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006156 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6157 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006158 set_comment("[ DeferredReferenceGetKeyedValue");
6159 }
6160
6161 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006162
6163 private:
6164 Register key_;
6165 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006166};
6167
6168
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006169// Takes key and register in r0 and r1 or vice versa. Returns result
6170// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006171void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006172 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6173 (key_.is(r1) && receiver_.is(r0)));
6174
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006175 VirtualFrame copied_frame(*frame_state()->frame());
6176 copied_frame.SpillAll();
6177
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006178 Register scratch1 = VirtualFrame::scratch0();
6179 Register scratch2 = VirtualFrame::scratch1();
6180 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6181 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6182
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006183 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6184 // convention.
6185 if (key_.is(r1)) {
6186 __ Swap(r0, r1, ip);
6187 }
6188
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006189 // The rest of the instructions in the deferred code must be together.
6190 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006191 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006192 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6193 __ Call(ic, RelocInfo::CODE_TARGET);
6194 // The call must be followed by a nop instruction to indicate that the
6195 // keyed load has been inlined.
6196 __ nop(PROPERTY_ACCESS_INLINED);
6197
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006198 // Now go back to the frame that we entered with. This will not overwrite
6199 // the receiver or key registers since they were not in use when we came
6200 // in. The instructions emitted by this merge are skipped over by the
6201 // inline load patching mechanism when looking for the branch instruction
6202 // that tells it where the code to patch is.
6203 copied_frame.MergeTo(frame_state()->frame());
6204
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006205 // Block the constant pool for one more instruction after leaving this
6206 // constant pool block scope to include the branch instruction ending the
6207 // deferred code.
6208 __ BlockConstPoolFor(1);
6209 }
6210}
6211
6212
6213class DeferredReferenceSetKeyedValue: public DeferredCode {
6214 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006215 DeferredReferenceSetKeyedValue(Register value,
6216 Register key,
6217 Register receiver)
6218 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006219 set_comment("[ DeferredReferenceSetKeyedValue");
6220 }
6221
6222 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006223
6224 private:
6225 Register value_;
6226 Register key_;
6227 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006228};
6229
6230
6231void DeferredReferenceSetKeyedValue::Generate() {
6232 Register scratch1 = VirtualFrame::scratch0();
6233 Register scratch2 = VirtualFrame::scratch1();
6234 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6235 __ IncrementCounter(
6236 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6237
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006238 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6239 // calling convention.
6240 if (value_.is(r1)) {
6241 __ Swap(r0, r1, ip);
6242 }
6243 ASSERT(receiver_.is(r2));
6244
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006245 // The rest of the instructions in the deferred code must be together.
6246 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006247 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6248 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006249 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6250 __ Call(ic, RelocInfo::CODE_TARGET);
6251 // The call must be followed by a nop instruction to indicate that the
6252 // keyed store has been inlined.
6253 __ nop(PROPERTY_ACCESS_INLINED);
6254
6255 // Block the constant pool for one more instruction after leaving this
6256 // constant pool block scope to include the branch instruction ending the
6257 // deferred code.
6258 __ BlockConstPoolFor(1);
6259 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006260}
6261
6262
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006263class DeferredReferenceSetNamedValue: public DeferredCode {
6264 public:
6265 DeferredReferenceSetNamedValue(Register value,
6266 Register receiver,
6267 Handle<String> name)
6268 : value_(value), receiver_(receiver), name_(name) {
6269 set_comment("[ DeferredReferenceSetNamedValue");
6270 }
6271
6272 virtual void Generate();
6273
6274 private:
6275 Register value_;
6276 Register receiver_;
6277 Handle<String> name_;
6278};
6279
6280
6281// Takes value in r0, receiver in r1 and returns the result (the
6282// value) in r0.
6283void DeferredReferenceSetNamedValue::Generate() {
6284 // Record the entry frame and spill.
6285 VirtualFrame copied_frame(*frame_state()->frame());
6286 copied_frame.SpillAll();
6287
6288 // Ensure value in r0, receiver in r1 to match store ic calling
6289 // convention.
6290 ASSERT(value_.is(r0) && receiver_.is(r1));
6291 __ mov(r2, Operand(name_));
6292
6293 // The rest of the instructions in the deferred code must be together.
6294 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6295 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6296 // r1 and r2.
6297 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6298 __ Call(ic, RelocInfo::CODE_TARGET);
6299 // The call must be followed by a nop instruction to indicate that the
6300 // named store has been inlined.
6301 __ nop(PROPERTY_ACCESS_INLINED);
6302
6303 // Go back to the frame we entered with. The instructions
6304 // generated by this merge are skipped over by the inline store
6305 // patching mechanism when looking for the branch instruction that
6306 // tells it where the code to patch is.
6307 copied_frame.MergeTo(frame_state()->frame());
6308
6309 // Block the constant pool for one more instruction after leaving this
6310 // constant pool block scope to include the branch instruction ending the
6311 // deferred code.
6312 __ BlockConstPoolFor(1);
6313 }
6314}
6315
6316
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006317// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006318void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6319 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6320 Comment cmnt(masm(), "[ Load from named Property");
6321 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006322 frame_->CallLoadIC(name,
6323 is_contextual
6324 ? RelocInfo::CODE_TARGET_CONTEXT
6325 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006326 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006327 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006328 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006329 Comment cmnt(masm(), "[ Inlined named property load");
6330
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006331 // Counter will be decremented in the deferred code. Placed here to avoid
6332 // having it in the instruction stream below where patching will occur.
6333 __ IncrementCounter(&Counters::named_load_inline, 1,
6334 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006335
6336 // The following instructions are the inlined load of an in-object property.
6337 // Parts of this code is patched, so the exact instructions generated needs
6338 // to be fixed. Therefore the instruction pool is blocked when generating
6339 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006340
6341 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006342 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006343
6344 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006345 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006346
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006347#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006348 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006349 Label check_inlined_codesize;
6350 masm_->bind(&check_inlined_codesize);
6351#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006352
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006353 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006354 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006355 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006356 deferred->Branch(eq);
6357
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006358 Register scratch = VirtualFrame::scratch0();
6359 Register scratch2 = VirtualFrame::scratch1();
6360
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006361 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006362 // code. Therefore we can't use a LoadRoot call.
6363 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6364 __ mov(scratch2, Operand(Factory::null_value()));
6365 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006366 deferred->Branch(ne);
6367
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006368 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006369 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006370 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006371
6372 // Make sure that the expected number of instructions are generated.
6373 ASSERT_EQ(kInlinedNamedLoadInstructions,
6374 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006375 }
6376
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006377 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006378 // At this point the receiver register has the result, either from the
6379 // deferred code or from the inlined code.
6380 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006381 }
6382}
6383
6384
ager@chromium.orgac091b72010-05-05 07:34:42 +00006385void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6386#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006387 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006388#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006389
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006390 Result result;
6391 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6392 frame()->CallStoreIC(name, is_contextual);
6393 } else {
6394 // Inline the in-object property case.
6395 JumpTarget slow, done;
6396
6397 // Get the value and receiver from the stack.
6398 frame()->PopToR0();
6399 Register value = r0;
6400 frame()->PopToR1();
6401 Register receiver = r1;
6402
6403 DeferredReferenceSetNamedValue* deferred =
6404 new DeferredReferenceSetNamedValue(value, receiver, name);
6405
6406 // Check that the receiver is a heap object.
6407 __ tst(receiver, Operand(kSmiTagMask));
6408 deferred->Branch(eq);
6409
6410 // The following instructions are the part of the inlined
6411 // in-object property store code which can be patched. Therefore
6412 // the exact number of instructions generated must be fixed, so
6413 // the constant pool is blocked while generating this code.
6414 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6415 Register scratch0 = VirtualFrame::scratch0();
6416 Register scratch1 = VirtualFrame::scratch1();
6417
6418 // Check the map. Initially use an invalid map to force a
6419 // failure. The map check will be patched in the runtime system.
6420 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6421
6422#ifdef DEBUG
6423 Label check_inlined_codesize;
6424 masm_->bind(&check_inlined_codesize);
6425#endif
6426 __ mov(scratch0, Operand(Factory::null_value()));
6427 __ cmp(scratch0, scratch1);
6428 deferred->Branch(ne);
6429
6430 int offset = 0;
6431 __ str(value, MemOperand(receiver, offset));
6432
6433 // Update the write barrier and record its size. We do not use
6434 // the RecordWrite macro here because we want the offset
6435 // addition instruction first to make it easy to patch.
6436 Label record_write_start, record_write_done;
6437 __ bind(&record_write_start);
6438 // Add offset into the object.
6439 __ add(scratch0, receiver, Operand(offset));
6440 // Test that the object is not in the new space. We cannot set
6441 // region marks for new space pages.
6442 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6443 // Record the actual write.
6444 __ RecordWriteHelper(receiver, scratch0, scratch1);
6445 __ bind(&record_write_done);
6446 // Clobber all input registers when running with the debug-code flag
6447 // turned on to provoke errors.
6448 if (FLAG_debug_code) {
6449 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6450 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6451 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6452 }
6453 // Check that this is the first inlined write barrier or that
6454 // this inlined write barrier has the same size as all the other
6455 // inlined write barriers.
6456 ASSERT((inlined_write_barrier_size_ == -1) ||
6457 (inlined_write_barrier_size_ ==
6458 masm()->InstructionsGeneratedSince(&record_write_start)));
6459 inlined_write_barrier_size_ =
6460 masm()->InstructionsGeneratedSince(&record_write_start);
6461
6462 // Make sure that the expected number of instructions are generated.
6463 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6464 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6465 }
6466 deferred->BindExit();
6467 }
6468 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006469}
6470
6471
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006472void CodeGenerator::EmitKeyedLoad() {
6473 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006474 Comment cmnt(masm_, "[ Load from keyed property");
6475 frame_->CallKeyedLoadIC();
6476 } else {
6477 // Inline the keyed load.
6478 Comment cmnt(masm_, "[ Inlined load from keyed property");
6479
6480 // Counter will be decremented in the deferred code. Placed here to avoid
6481 // having it in the instruction stream below where patching will occur.
6482 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6483 frame_->scratch0(), frame_->scratch1());
6484
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006485 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006486 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006487 Register key = frame_->PopToRegister();
6488 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006489
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006490 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006491 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006492 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006493
6494 // Check that the receiver is a heap object.
6495 __ tst(receiver, Operand(kSmiTagMask));
6496 deferred->Branch(eq);
6497
6498 // The following instructions are the part of the inlined load keyed
6499 // property code which can be patched. Therefore the exact number of
6500 // instructions generated need to be fixed, so the constant pool is blocked
6501 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006502 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6503 Register scratch1 = VirtualFrame::scratch0();
6504 Register scratch2 = VirtualFrame::scratch1();
6505 // Check the map. The null map used below is patched by the inline cache
6506 // code.
6507 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006508
6509 // Check that the key is a smi.
6510 if (!key_is_known_smi) {
6511 __ tst(key, Operand(kSmiTagMask));
6512 deferred->Branch(ne);
6513 }
6514
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006515#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006516 Label check_inlined_codesize;
6517 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006518#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006519 __ mov(scratch2, Operand(Factory::null_value()));
6520 __ cmp(scratch1, scratch2);
6521 deferred->Branch(ne);
6522
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006523 // Get the elements array from the receiver and check that it
6524 // is not a dictionary.
6525 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006526 if (FLAG_debug_code) {
6527 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
6528 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
6529 __ cmp(scratch2, ip);
6530 __ Assert(eq, "JSObject with fast elements map has slow elements");
6531 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006532
6533 // Check that key is within bounds. Use unsigned comparison to handle
6534 // negative keys.
6535 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006536 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006537 deferred->Branch(ls); // Unsigned less equal.
6538
6539 // Load and check that the result is not the hole (key is a smi).
6540 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6541 __ add(scratch1,
6542 scratch1,
6543 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006544 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006545 MemOperand(scratch1, key, LSL,
6546 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006547 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006548 deferred->Branch(eq);
6549
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006550 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006551 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006552 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006553 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6554 }
6555
6556 deferred->BindExit();
6557 }
6558}
6559
6560
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006561void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6562 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006563 // Generate inlined version of the keyed store if the code is in a loop
6564 // and the key is likely to be a smi.
6565 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6566 // Inline the keyed store.
6567 Comment cmnt(masm_, "[ Inlined store to keyed property");
6568
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006569 Register scratch1 = VirtualFrame::scratch0();
6570 Register scratch2 = VirtualFrame::scratch1();
6571 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006572
6573 // Counter will be decremented in the deferred code. Placed here to avoid
6574 // having it in the instruction stream below where patching will occur.
6575 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006576 scratch1, scratch2);
6577
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006578
6579
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006580 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006581 bool value_is_harmless = frame_->KnownSmiAt(0);
6582 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6583 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006584 Register value = frame_->PopToRegister();
6585 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006586 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006587 Register receiver = r2;
6588 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006589
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006590#ifdef DEBUG
6591 bool we_remembered_the_write_barrier = value_is_harmless;
6592#endif
6593
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006594 // The deferred code expects value, key and receiver in registers.
6595 DeferredReferenceSetKeyedValue* deferred =
6596 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006597
6598 // Check that the value is a smi. As this inlined code does not set the
6599 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006600 if (!value_is_harmless) {
6601 // If the value is not likely to be a Smi then let's test the fixed array
6602 // for new space instead. See below.
6603 if (wb_info == LIKELY_SMI) {
6604 __ tst(value, Operand(kSmiTagMask));
6605 deferred->Branch(ne);
6606#ifdef DEBUG
6607 we_remembered_the_write_barrier = true;
6608#endif
6609 }
6610 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006611
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006612 if (!key_is_smi) {
6613 // Check that the key is a smi.
6614 __ tst(key, Operand(kSmiTagMask));
6615 deferred->Branch(ne);
6616 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006617
6618 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006619 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006620 deferred->Branch(eq);
6621
6622 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006623 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006624 deferred->Branch(ne);
6625
6626 // Check that the key is within bounds. Both the key and the length of
6627 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006628 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6629 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006630 deferred->Branch(ls); // Unsigned less equal.
6631
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006632 // Get the elements array from the receiver.
6633 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6634 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6635 Label ok;
6636 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6637 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6638 __ tst(value, Operand(kSmiTagMask), ne);
6639 deferred->Branch(ne);
6640#ifdef DEBUG
6641 we_remembered_the_write_barrier = true;
6642#endif
6643 }
6644 // Check that the elements array is not a dictionary.
6645 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006646 // The following instructions are the part of the inlined store keyed
6647 // property code which can be patched. Therefore the exact number of
6648 // instructions generated need to be fixed, so the constant pool is blocked
6649 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006650 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006651#ifdef DEBUG
6652 Label check_inlined_codesize;
6653 masm_->bind(&check_inlined_codesize);
6654#endif
6655
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006656 // Read the fixed array map from the constant pool (not from the root
6657 // array) so that the value can be patched. When debugging, we patch this
6658 // comparison to always fail so that we will hit the IC call in the
6659 // deferred code which will allow the debugger to break for fast case
6660 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006661 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6662 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006663 deferred->Branch(ne);
6664
6665 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006666 __ add(scratch1, scratch1,
6667 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6668 __ str(value,
6669 MemOperand(scratch1, key, LSL,
6670 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006671
6672 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006673 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006674 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6675 }
6676
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006677 ASSERT(we_remembered_the_write_barrier);
6678
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006679 deferred->BindExit();
6680 } else {
6681 frame()->CallKeyedStoreIC();
6682 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006683}
6684
6685
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006686#ifdef DEBUG
6687bool CodeGenerator::HasValidEntryRegisters() { return true; }
6688#endif
6689
6690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006691#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006692#define __ ACCESS_MASM(masm)
6693
ager@chromium.org7c537e22008-10-16 08:43:32 +00006694Handle<String> Reference::GetName() {
6695 ASSERT(type_ == NAMED);
6696 Property* property = expression_->AsProperty();
6697 if (property == NULL) {
6698 // Global variable reference treated as a named property reference.
6699 VariableProxy* proxy = expression_->AsVariableProxy();
6700 ASSERT(proxy->AsVariable() != NULL);
6701 ASSERT(proxy->AsVariable()->is_global());
6702 return proxy->name();
6703 } else {
6704 Literal* raw_name = property->key()->AsLiteral();
6705 ASSERT(raw_name != NULL);
6706 return Handle<String>(String::cast(*raw_name->handle()));
6707 }
6708}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006709
ager@chromium.org7c537e22008-10-16 08:43:32 +00006710
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006711void Reference::DupIfPersist() {
6712 if (persist_after_get_) {
6713 switch (type_) {
6714 case KEYED:
6715 cgen_->frame()->Dup2();
6716 break;
6717 case NAMED:
6718 cgen_->frame()->Dup();
6719 // Fall through.
6720 case UNLOADED:
6721 case ILLEGAL:
6722 case SLOT:
6723 // Do nothing.
6724 ;
6725 }
6726 } else {
6727 set_unloaded();
6728 }
6729}
6730
6731
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006732void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006733 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006734 ASSERT(!is_illegal());
6735 ASSERT(!cgen_->has_cc());
6736 MacroAssembler* masm = cgen_->masm();
6737 Property* property = expression_->AsProperty();
6738 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006739 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006740 }
6741
6742 switch (type_) {
6743 case SLOT: {
6744 Comment cmnt(masm, "[ Load from Slot");
6745 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6746 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006747 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006748 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006749 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006750 }
6751
ager@chromium.org7c537e22008-10-16 08:43:32 +00006752 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006753 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006754 bool is_global = var != NULL;
6755 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006756 Handle<String> name = GetName();
6757 DupIfPersist();
6758 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006759 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006760 }
6761
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006762 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006763 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006764 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006765 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006766 cgen_->frame()->EmitPush(r0);
6767 break;
6768 }
6769
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006770 default:
6771 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006772 }
6773}
6774
6775
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006776void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006777 ASSERT(!is_illegal());
6778 ASSERT(!cgen_->has_cc());
6779 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006780 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006781 Property* property = expression_->AsProperty();
6782 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006783 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006784 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006785
ager@chromium.org7c537e22008-10-16 08:43:32 +00006786 switch (type_) {
6787 case SLOT: {
6788 Comment cmnt(masm, "[ Store to Slot");
6789 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006790 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006791 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006792 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006793 }
6794
ager@chromium.org7c537e22008-10-16 08:43:32 +00006795 case NAMED: {
6796 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00006797 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006798 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00006799 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006800 break;
6801 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006802
ager@chromium.org7c537e22008-10-16 08:43:32 +00006803 case KEYED: {
6804 Comment cmnt(masm, "[ Store to keyed Property");
6805 Property* property = expression_->AsProperty();
6806 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006807 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006808 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006809 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006810 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006811 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006812 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00006813
6814 default:
6815 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006816 }
6817}
6818
6819
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006820void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006821 // Create a new closure from the given function info in new
6822 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006823 Label gc;
6824
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006825 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006826 __ pop(r3);
6827
6828 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006829 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006830 r0,
6831 r1,
6832 r2,
6833 &gc,
6834 TAG_OBJECT);
6835
6836 // Compute the function map in the current global context and set that
6837 // as the map of the allocated object.
6838 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6839 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
6840 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
6841 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
6842
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006843 // Initialize the rest of the function. We don't have to update the
6844 // write barrier because the allocated object is in new space.
6845 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
6846 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
6847 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
6848 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
6849 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
6850 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
6851 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
6852 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006853
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006854 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006855 __ Ret();
6856
6857 // Create a new closure through the slower runtime call.
6858 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006859 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006860 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006861}
6862
6863
6864void FastNewContextStub::Generate(MacroAssembler* masm) {
6865 // Try to allocate the context in new space.
6866 Label gc;
6867 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
6868
6869 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006870 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006871 r0,
6872 r1,
6873 r2,
6874 &gc,
6875 TAG_OBJECT);
6876
6877 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00006878 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006879
6880 // Setup the object header.
6881 __ LoadRoot(r2, Heap::kContextMapRootIndex);
6882 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006883 __ mov(r2, Operand(Smi::FromInt(length)));
6884 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006885
6886 // Setup the fixed slots.
6887 __ mov(r1, Operand(Smi::FromInt(0)));
6888 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
6889 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
6890 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
6891 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
6892
6893 // Copy the global object from the surrounding context.
6894 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6895 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
6896
6897 // Initialize the rest of the slots to undefined.
6898 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
6899 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
6900 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
6901 }
6902
6903 // Remove the on-stack argument and return.
6904 __ mov(cp, r0);
6905 __ pop();
6906 __ Ret();
6907
6908 // Need to collect. Call into runtime system.
6909 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006910 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006911}
6912
6913
ager@chromium.org5c838252010-02-19 08:53:10 +00006914void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
6915 // Stack layout on entry:
6916 //
6917 // [sp]: constant elements.
6918 // [sp + kPointerSize]: literal index.
6919 // [sp + (2 * kPointerSize)]: literals array.
6920
6921 // All sizes here are multiples of kPointerSize.
6922 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
6923 int size = JSArray::kSize + elements_size;
6924
6925 // Load boilerplate object into r3 and check if we need to create a
6926 // boilerplate.
6927 Label slow_case;
6928 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6929 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6930 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6931 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6932 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6933 __ cmp(r3, ip);
6934 __ b(eq, &slow_case);
6935
6936 // Allocate both the JS array and the elements array in one big
6937 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006938 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00006939 r0,
6940 r1,
6941 r2,
6942 &slow_case,
6943 TAG_OBJECT);
6944
6945 // Copy the JS array part.
6946 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
6947 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
6948 __ ldr(r1, FieldMemOperand(r3, i));
6949 __ str(r1, FieldMemOperand(r0, i));
6950 }
6951 }
6952
6953 if (length_ > 0) {
6954 // Get hold of the elements array of the boilerplate and setup the
6955 // elements pointer in the resulting object.
6956 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
6957 __ add(r2, r0, Operand(JSArray::kSize));
6958 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
6959
6960 // Copy the elements array.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00006961 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00006962 }
6963
6964 // Return and remove the on-stack parameters.
6965 __ add(sp, sp, Operand(3 * kPointerSize));
6966 __ Ret();
6967
6968 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006969 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00006970}
6971
6972
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006973// Takes a Smi and converts to an IEEE 64 bit floating point value in two
6974// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
6975// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
6976// scratch register. Destroys the source register. No GC occurs during this
6977// stub so you don't have to set up the frame.
6978class ConvertToDoubleStub : public CodeStub {
6979 public:
6980 ConvertToDoubleStub(Register result_reg_1,
6981 Register result_reg_2,
6982 Register source_reg,
6983 Register scratch_reg)
6984 : result1_(result_reg_1),
6985 result2_(result_reg_2),
6986 source_(source_reg),
6987 zeros_(scratch_reg) { }
6988
6989 private:
6990 Register result1_;
6991 Register result2_;
6992 Register source_;
6993 Register zeros_;
6994
6995 // Minor key encoding in 16 bits.
6996 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
6997 class OpBits: public BitField<Token::Value, 2, 14> {};
6998
6999 Major MajorKey() { return ConvertToDouble; }
7000 int MinorKey() {
7001 // Encode the parameters in a unique 16 bit value.
7002 return result1_.code() +
7003 (result2_.code() << 4) +
7004 (source_.code() << 8) +
7005 (zeros_.code() << 12);
7006 }
7007
7008 void Generate(MacroAssembler* masm);
7009
7010 const char* GetName() { return "ConvertToDoubleStub"; }
7011
7012#ifdef DEBUG
7013 void Print() { PrintF("ConvertToDoubleStub\n"); }
7014#endif
7015};
7016
7017
7018void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
7019#ifndef BIG_ENDIAN_FLOATING_POINT
7020 Register exponent = result1_;
7021 Register mantissa = result2_;
7022#else
7023 Register exponent = result2_;
7024 Register mantissa = result1_;
7025#endif
7026 Label not_special;
7027 // Convert from Smi to integer.
7028 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
7029 // Move sign bit from source to destination. This works because the sign bit
7030 // in the exponent word of the double has the same position and polarity as
7031 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007032 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007033 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
7034 // Subtract from 0 if source was negative.
7035 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007036
7037 // We have -1, 0 or 1, which we treat specially. Register source_ contains
7038 // absolute value: it is either equal to 1 (special case of -1 and 1),
7039 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007040 __ cmp(source_, Operand(1));
7041 __ b(gt, &not_special);
7042
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007043 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
7044 static const uint32_t exponent_word_for_1 =
7045 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007046 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007047 // 1, 0 and -1 all have 0 for the second word.
7048 __ mov(mantissa, Operand(0));
7049 __ Ret();
7050
7051 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007052 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007053 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007054 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007055 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007056 // We use mantissa as a scratch register here. Use a fudge factor to
7057 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7058 // that fit in the ARM's constant field.
7059 int fudge = 0x400;
7060 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7061 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007062 __ orr(exponent,
7063 exponent,
7064 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7065 // Shift up the source chopping the top bit off.
7066 __ add(zeros_, zeros_, Operand(1));
7067 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7068 __ mov(source_, Operand(source_, LSL, zeros_));
7069 // Compute lower part of fraction (last 12 bits).
7070 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7071 // And the top (top 20 bits).
7072 __ orr(exponent,
7073 exponent,
7074 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7075 __ Ret();
7076}
7077
7078
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007079// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007080void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007081 Label max_negative_int;
7082 // the_int_ has the answer which is a signed int32 but not a Smi.
7083 // We test for the special value that has a different exponent. This test
7084 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007085 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007086 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007087 __ b(eq, &max_negative_int);
7088 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7089 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7090 uint32_t non_smi_exponent =
7091 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7092 __ mov(scratch_, Operand(non_smi_exponent));
7093 // Set the sign bit in scratch_ if the value was negative.
7094 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7095 // Subtract from 0 if the value was negative.
7096 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7097 // We should be masking the implict first digit of the mantissa away here,
7098 // but it just ends up combining harmlessly with the last digit of the
7099 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7100 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7101 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7102 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7103 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7104 __ str(scratch_, FieldMemOperand(the_heap_number_,
7105 HeapNumber::kExponentOffset));
7106 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7107 __ str(scratch_, FieldMemOperand(the_heap_number_,
7108 HeapNumber::kMantissaOffset));
7109 __ Ret();
7110
7111 __ bind(&max_negative_int);
7112 // The max negative int32 is stored as a positive number in the mantissa of
7113 // a double because it uses a sign bit instead of using two's complement.
7114 // The actual mantissa bits stored are all 0 because the implicit most
7115 // significant 1 bit is not stored.
7116 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7117 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7118 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7119 __ mov(ip, Operand(0));
7120 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7121 __ Ret();
7122}
7123
7124
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007125// Handle the case where the lhs and rhs are the same object.
7126// Equality is almost reflexive (everything but NaN), so this is a test
7127// for "identity and not NaN".
7128static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7129 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007130 Condition cc,
7131 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007132 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007133 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007134 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007135 __ b(ne, &not_identical);
7136
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007137 // The two objects are identical. If we know that one of them isn't NaN then
7138 // we now know they test equal.
7139 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007140 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7141 // so we do the second best thing - test it ourselves.
7142 // They are both equal and they are not both Smis so both of them are not
7143 // Smis. If it's not a heap number, then return equal.
7144 if (cc == lt || cc == gt) {
7145 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007146 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007147 } else {
7148 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7149 __ b(eq, &heap_number);
7150 // Comparing JS objects with <=, >= is complicated.
7151 if (cc != eq) {
7152 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7153 __ b(ge, slow);
7154 // Normally here we fall through to return_equal, but undefined is
7155 // special: (undefined == undefined) == true, but
7156 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7157 if (cc == le || cc == ge) {
7158 __ cmp(r4, Operand(ODDBALL_TYPE));
7159 __ b(ne, &return_equal);
7160 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007161 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007162 __ b(ne, &return_equal);
7163 if (cc == le) {
7164 // undefined <= undefined should fail.
7165 __ mov(r0, Operand(GREATER));
7166 } else {
7167 // undefined >= undefined should fail.
7168 __ mov(r0, Operand(LESS));
7169 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007170 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007171 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007172 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007173 }
7174 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007175
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007176 __ bind(&return_equal);
7177 if (cc == lt) {
7178 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7179 } else if (cc == gt) {
7180 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7181 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007182 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007183 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007184 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007185
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007186 if (cc != eq || !never_nan_nan) {
7187 // For less and greater we don't have to check for NaN since the result of
7188 // x < x is false regardless. For the others here is some code to check
7189 // for NaN.
7190 if (cc != lt && cc != gt) {
7191 __ bind(&heap_number);
7192 // It is a heap number, so return non-equal if it's NaN and equal if it's
7193 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007194
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007195 // The representation of NaN values has all exponent bits (52..62) set,
7196 // and not all mantissa bits (0..51) clear.
7197 // Read top bits of double representation (second word of value).
7198 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7199 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007200 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7201 // NaNs have all-one exponents so they sign extend to -1.
7202 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007203 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007204
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007205 // Shift out flag and all exponent bits, retaining only mantissa.
7206 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7207 // Or with all low-bits of mantissa.
7208 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7209 __ orr(r0, r3, Operand(r2), SetCC);
7210 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007211 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7212 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7213 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007214 if (cc != eq) {
7215 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007216 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007217 if (cc == le) {
7218 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7219 } else {
7220 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7221 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007222 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007223 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007224 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007225 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007226 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007227
7228 __ bind(&not_identical);
7229}
7230
7231
7232// See comment at call site.
7233static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007234 Register lhs,
7235 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007236 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007237 Label* slow,
7238 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007239 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7240 (lhs.is(r1) && rhs.is(r0)));
7241
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007242 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007243 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007244 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007245
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007246 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007247 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007248 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007249 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007250 // succeed. Return non-equal
7251 // If rhs is r0 then there is already a non zero value in it.
7252 if (!rhs.is(r0)) {
7253 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7254 }
7255 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007256 } else {
7257 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7258 // the runtime.
7259 __ b(ne, slow);
7260 }
7261
ager@chromium.orgb5737492010-07-15 09:29:43 +00007262 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007263 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007264 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007265 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007266 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007267 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007268 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007269 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007270 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007271 __ push(lr);
7272 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007273 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007274 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7275 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007276 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007277 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007278 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007279 }
7280
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007281 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007282 // since it's a smi.
7283 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007284
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007285 __ bind(&rhs_is_smi);
7286 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007287 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007288 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007289 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007290 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007291 // If lhs is r0 then there is already a non zero value in it.
7292 if (!lhs.is(r0)) {
7293 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7294 }
7295 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007296 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007297 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007298 // the runtime.
7299 __ b(ne, slow);
7300 }
7301
ager@chromium.orgb5737492010-07-15 09:29:43 +00007302 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007303 if (CpuFeatures::IsSupported(VFP3)) {
7304 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007305 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007306 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007307 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007308 // Convert rhs to a double in d6 .
7309 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007310 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007311 __ push(lr);
7312 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007313 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007314 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007315 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007316 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7317 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007318 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007319 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007320 // Fall through to both_loaded_as_doubles.
7321}
7322
7323
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007324void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007325 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007326 Register rhs_exponent = exp_first ? r0 : r1;
7327 Register lhs_exponent = exp_first ? r2 : r3;
7328 Register rhs_mantissa = exp_first ? r1 : r0;
7329 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007330 Label one_is_nan, neither_is_nan;
7331
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007332 __ Sbfx(r4,
7333 lhs_exponent,
7334 HeapNumber::kExponentShift,
7335 HeapNumber::kExponentBits);
7336 // NaNs have all-one exponents so they sign extend to -1.
7337 __ cmp(r4, Operand(-1));
7338 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007339 __ mov(r4,
7340 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7341 SetCC);
7342 __ b(ne, &one_is_nan);
7343 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007344 __ b(ne, &one_is_nan);
7345
7346 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007347 __ Sbfx(r4,
7348 rhs_exponent,
7349 HeapNumber::kExponentShift,
7350 HeapNumber::kExponentBits);
7351 // NaNs have all-one exponents so they sign extend to -1.
7352 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007353 __ b(ne, &neither_is_nan);
7354 __ mov(r4,
7355 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7356 SetCC);
7357 __ b(ne, &one_is_nan);
7358 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007359 __ b(eq, &neither_is_nan);
7360
7361 __ bind(&one_is_nan);
7362 // NaN comparisons always fail.
7363 // Load whatever we need in r0 to make the comparison fail.
7364 if (cc == lt || cc == le) {
7365 __ mov(r0, Operand(GREATER));
7366 } else {
7367 __ mov(r0, Operand(LESS));
7368 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007369 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007370
7371 __ bind(&neither_is_nan);
7372}
7373
7374
7375// See comment at call site.
7376static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7377 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007378 Register rhs_exponent = exp_first ? r0 : r1;
7379 Register lhs_exponent = exp_first ? r2 : r3;
7380 Register rhs_mantissa = exp_first ? r1 : r0;
7381 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007382
7383 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7384 if (cc == eq) {
7385 // Doubles are not equal unless they have the same bit pattern.
7386 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007387 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7388 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007389 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007390 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007391
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007392 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007393 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007394 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007395
7396 // Exponents are unequal. The only way we can return that the numbers
7397 // are equal is if one is -0 and the other is 0. We already dealt
7398 // with the case where both are -0 or both are 0.
7399 // We start by seeing if the mantissas (that are equal) or the bottom
7400 // 31 bits of the rhs exponent are non-zero. If so we return not
7401 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007402 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007403 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007404 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007405 // Now they are equal if and only if the lhs exponent is zero in its
7406 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007407 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007408 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007409 } else {
7410 // Call a native function to do a comparison between two non-NaNs.
7411 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007412 __ push(lr);
7413 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7414 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7415 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007416 }
7417}
7418
7419
7420// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007421static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7422 Register lhs,
7423 Register rhs) {
7424 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7425 (lhs.is(r1) && rhs.is(r0)));
7426
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007427 // If either operand is a JSObject or an oddball value, then they are
7428 // not equal since their pointers are different.
7429 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007430 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007431 Label first_non_object;
7432 // Get the type of the first operand into r2 and compare it with
7433 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007434 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007435 __ b(lt, &first_non_object);
7436
7437 // Return non-zero (r0 is not zero)
7438 Label return_not_equal;
7439 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007440 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007441
7442 __ bind(&first_non_object);
7443 // Check for oddballs: true, false, null, undefined.
7444 __ cmp(r2, Operand(ODDBALL_TYPE));
7445 __ b(eq, &return_not_equal);
7446
ager@chromium.orgb5737492010-07-15 09:29:43 +00007447 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007448 __ b(ge, &return_not_equal);
7449
7450 // Check for oddballs: true, false, null, undefined.
7451 __ cmp(r3, Operand(ODDBALL_TYPE));
7452 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007453
7454 // Now that we have the types we might as well check for symbol-symbol.
7455 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007456 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7457 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007458 __ and_(r2, r2, Operand(r3));
7459 __ tst(r2, Operand(kIsSymbolMask));
7460 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007461}
7462
7463
7464// See comment at call site.
7465static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007466 Register lhs,
7467 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007468 Label* both_loaded_as_doubles,
7469 Label* not_heap_numbers,
7470 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007471 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7472 (lhs.is(r1) && rhs.is(r0)));
7473
7474 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007475 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007476 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007477 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007478 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7479
7480 // Both are heap numbers. Load them up then jump to the code we have
7481 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007482 if (CpuFeatures::IsSupported(VFP3)) {
7483 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007484 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007485 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007486 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007487 __ vldr(d7, r7, HeapNumber::kValueOffset);
7488 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007489 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7490 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007491 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007492 __ jmp(both_loaded_as_doubles);
7493}
7494
7495
7496// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007497static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007498 Register lhs,
7499 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007500 Label* possible_strings,
7501 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007502 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7503 (lhs.is(r1) && rhs.is(r0)));
7504
7505 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007506 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007507 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007508 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007509 __ tst(r2, Operand(kIsNotStringMask));
7510 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007511 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007512 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007513 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007514 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007515 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007516 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007517
7518 // Both are symbols. We already checked they weren't the same pointer
7519 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007520 __ mov(r0, Operand(NOT_EQUAL));
7521 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007522
7523 __ bind(&object_test);
7524 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7525 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007526 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007527 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007528 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007529 // are not equal, since they are different objects and an object is not
7530 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007531 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007532 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7533 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7534 __ and_(r0, r2, Operand(r3));
7535 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7536 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007537 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007538}
7539
7540
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007541void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7542 Register object,
7543 Register result,
7544 Register scratch1,
7545 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007546 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007547 bool object_is_smi,
7548 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007549 // Use of registers. Register result is used as a temporary.
7550 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007551 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007552
7553 // Load the number string cache.
7554 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7555
7556 // Make the hash mask from the length of the number string cache. It
7557 // contains two elements (number and string) for each cache entry.
7558 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007559 // Divide length by two (length is a smi).
7560 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007561 __ sub(mask, mask, Operand(1)); // Make mask.
7562
7563 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007564 // number string cache for smis is just the smi value, and the hash for
7565 // doubles is the xor of the upper and lower words. See
7566 // Heap::GetNumberStringCache.
7567 Label is_smi;
7568 Label load_result_from_cache;
7569 if (!object_is_smi) {
7570 __ BranchOnSmi(object, &is_smi);
7571 if (CpuFeatures::IsSupported(VFP3)) {
7572 CpuFeatures::Scope scope(VFP3);
7573 __ CheckMap(object,
7574 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007575 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007576 not_found,
7577 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007578
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007579 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007580 __ add(scratch1,
7581 object,
7582 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7583 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7584 __ eor(scratch1, scratch1, Operand(scratch2));
7585 __ and_(scratch1, scratch1, Operand(mask));
7586
7587 // Calculate address of entry in string cache: each entry consists
7588 // of two pointer sized fields.
7589 __ add(scratch1,
7590 number_string_cache,
7591 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7592
7593 Register probe = mask;
7594 __ ldr(probe,
7595 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7596 __ BranchOnSmi(probe, not_found);
7597 __ sub(scratch2, object, Operand(kHeapObjectTag));
7598 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7599 __ sub(probe, probe, Operand(kHeapObjectTag));
7600 __ vldr(d1, probe, HeapNumber::kValueOffset);
7601 __ vcmp(d0, d1);
7602 __ vmrs(pc);
7603 __ b(ne, not_found); // The cache did not contain this value.
7604 __ b(&load_result_from_cache);
7605 } else {
7606 __ b(not_found);
7607 }
7608 }
7609
7610 __ bind(&is_smi);
7611 Register scratch = scratch1;
7612 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007613 // Calculate address of entry in string cache: each entry consists
7614 // of two pointer sized fields.
7615 __ add(scratch,
7616 number_string_cache,
7617 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7618
7619 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007620 Register probe = mask;
7621 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7622 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007623 __ b(ne, not_found);
7624
7625 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007626 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007627 __ ldr(result,
7628 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007629 __ IncrementCounter(&Counters::number_to_string_native,
7630 1,
7631 scratch1,
7632 scratch2);
7633}
7634
7635
7636void NumberToStringStub::Generate(MacroAssembler* masm) {
7637 Label runtime;
7638
7639 __ ldr(r1, MemOperand(sp, 0));
7640
7641 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007642 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007643 __ add(sp, sp, Operand(1 * kPointerSize));
7644 __ Ret();
7645
7646 __ bind(&runtime);
7647 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007648 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007649}
7650
7651
ager@chromium.orgac091b72010-05-05 07:34:42 +00007652void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007653 __ add(offset_, object_, Operand(offset_));
7654 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007655 __ Ret();
7656}
7657
7658
ager@chromium.orgb5737492010-07-15 09:29:43 +00007659// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007660// On exit r0 is 0, positive or negative to indicate the result of
7661// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007662void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007663 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7664 (lhs_.is(r1) && rhs_.is(r0)));
7665
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007666 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007667 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007668
7669 // NOTICE! This code is only reached after a smi-fast-case check, so
7670 // it is certain that at least one operand isn't a smi.
7671
7672 // Handle the case where the objects are identical. Either returns the answer
7673 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007674 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007675
7676 // If either is a Smi (we know that not both are), then they can only
7677 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007678 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007679 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007680 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007681 __ tst(r2, Operand(kSmiTagMask));
7682 __ b(ne, &not_smis);
7683 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7684 // 1) Return the answer.
7685 // 2) Go to slow.
7686 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007687 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007688 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007689 // comparison. If VFP3 is supported the double values of the numbers have
7690 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7691 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007692 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007693
7694 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007695 // The arguments have been converted to doubles and stored in d6 and d7, if
7696 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007697 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007698 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007699 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007700 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007701 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007702 __ vcmp(d7, d6);
7703 __ vmrs(pc); // Move vector status bits to normal status bits.
7704 Label nan;
7705 __ b(vs, &nan);
7706 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7707 __ mov(r0, Operand(LESS), LeaveCC, lt);
7708 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007709 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007710
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007711 __ bind(&nan);
7712 // If one of the sides was a NaN then the v flag is set. Load r0 with
7713 // whatever it takes to make the comparison fail, since comparisons with NaN
7714 // always fail.
7715 if (cc_ == lt || cc_ == le) {
7716 __ mov(r0, Operand(GREATER));
7717 } else {
7718 __ mov(r0, Operand(LESS));
7719 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007720 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007721 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007722 // Checks for NaN in the doubles we have loaded. Can return the answer or
7723 // fall through if neither is a NaN. Also binds lhs_not_nan.
7724 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007725 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7726 // answer. Never falls through.
7727 EmitTwoNonNanDoubleComparison(masm, cc_);
7728 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007729
7730 __ bind(&not_smis);
7731 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007732 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007733 if (strict_) {
7734 // This returns non-equal for some object types, or falls through if it
7735 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007736 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007737 }
7738
7739 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007740 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007741 // Check for heap-number-heap-number comparison. Can jump to slow case,
7742 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7743 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007744 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007745 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007746 lhs_,
7747 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007748 &both_loaded_as_doubles,
7749 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007750 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007751
7752 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007753 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7754 // symbols.
7755 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007756 // Returns an answer for two symbols or two detectable objects.
7757 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007758 // Assumes that r2 is the type of rhs_ on entry.
7759 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007760 }
7761
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007762 // Check for both being sequential ASCII strings, and inline if that is the
7763 // case.
7764 __ bind(&flat_string_check);
7765
ager@chromium.orgb5737492010-07-15 09:29:43 +00007766 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007767
7768 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7769 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007770 lhs_,
7771 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007772 r2,
7773 r3,
7774 r4,
7775 r5);
7776 // Never falls through to here.
7777
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007778 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007779
ager@chromium.orgb5737492010-07-15 09:29:43 +00007780 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007781 // Figure out which native to call and setup the arguments.
7782 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007783 if (cc_ == eq) {
7784 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7785 } else {
7786 native = Builtins::COMPARE;
7787 int ncr; // NaN compare result
7788 if (cc_ == lt || cc_ == le) {
7789 ncr = GREATER;
7790 } else {
7791 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7792 ncr = LESS;
7793 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007794 __ mov(r0, Operand(Smi::FromInt(ncr)));
7795 __ push(r0);
7796 }
7797
7798 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7799 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007800 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007801}
7802
7803
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00007804// We fall into this code if the operands were Smis, but the result was
7805// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007806// the operands were not both Smi. The operands are in r0 and r1. In order
7807// to call the C-implemented binary fp operation routines we need to end up
7808// with the double precision floating point operands in r0 and r1 (for the
7809// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00007810void GenericBinaryOpStub::HandleBinaryOpSlowCases(
7811 MacroAssembler* masm,
7812 Label* not_smi,
7813 Register lhs,
7814 Register rhs,
7815 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007816 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00007817 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007818
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007819 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007820 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00007821
7822 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007823 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7824
ager@chromium.org357bf652010-04-12 11:30:10 +00007825 // Smi-smi case (overflow).
7826 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007827 // The new heap number is in r5. r3 and r7 are scratch.
7828 __ AllocateHeapNumber(
7829 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007830
7831 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
7832 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007833 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007834 CpuFeatures::Scope scope(VFP3);
7835 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
7836 __ vmov(s15, r7);
7837 __ vcvt_f64_s32(d7, s15);
7838 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
7839 __ vmov(s13, r7);
7840 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007841 if (!use_fp_registers) {
7842 __ vmov(r2, r3, d7);
7843 __ vmov(r0, r1, d6);
7844 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007845 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007846 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007847 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007848 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007849 __ push(lr);
7850 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007851 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007852 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007853 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007854 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
7855 __ pop(lr);
7856 }
7857 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007858 }
7859
ager@chromium.org357bf652010-04-12 11:30:10 +00007860 // We branch here if at least one of r0 and r1 is not a Smi.
7861 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007862 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007863
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007864 // After this point we have the left hand side in r1 and the right hand side
7865 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007866 if (lhs.is(r0)) {
7867 __ Swap(r0, r1, ip);
7868 }
7869
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007870 // The type transition also calculates the answer.
7871 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007872
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007873 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007874 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
7875 switch (op_) {
7876 case Token::ADD:
7877 case Token::SUB:
7878 case Token::MUL:
7879 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007880 GenerateTypeTransition(masm); // Tail call.
7881 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00007882 break;
7883
7884 default:
7885 break;
7886 }
7887 }
7888
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007889 if (generate_code_to_calculate_answer) {
7890 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
7891 if (mode_ == NO_OVERWRITE) {
7892 // In the case where there is no chance of an overwritable float we may
7893 // as well do the allocation immediately while r0 and r1 are untouched.
7894 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007895 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007896
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007897 // Move r0 to a double in r2-r3.
7898 __ tst(r0, Operand(kSmiTagMask));
7899 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
7900 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7901 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7902 __ cmp(r4, heap_number_map);
7903 __ b(ne, &slow);
7904 if (mode_ == OVERWRITE_RIGHT) {
7905 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007906 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007907 if (use_fp_registers) {
7908 CpuFeatures::Scope scope(VFP3);
7909 // Load the double from tagged HeapNumber r0 to d7.
7910 __ sub(r7, r0, Operand(kHeapObjectTag));
7911 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00007912 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007913 // Calling convention says that second double is in r2 and r3.
7914 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007915 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007916 __ jmp(&finished_loading_r0);
7917 __ bind(&r0_is_smi);
7918 if (mode_ == OVERWRITE_RIGHT) {
7919 // We can't overwrite a Smi so get address of new heap number into r5.
7920 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7921 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007922
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007923 if (CpuFeatures::IsSupported(VFP3)) {
7924 CpuFeatures::Scope scope(VFP3);
7925 // Convert smi in r0 to double in d7.
7926 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
7927 __ vmov(s15, r7);
7928 __ vcvt_f64_s32(d7, s15);
7929 if (!use_fp_registers) {
7930 __ vmov(r2, r3, d7);
7931 }
7932 } else {
7933 // Write Smi from r0 to r3 and r2 in double format.
7934 __ mov(r7, Operand(r0));
7935 ConvertToDoubleStub stub3(r3, r2, r7, r4);
7936 __ push(lr);
7937 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
7938 __ pop(lr);
7939 }
7940
7941 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
7942 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
7943 Label r1_is_not_smi;
7944 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
7945 __ tst(r1, Operand(kSmiTagMask));
7946 __ b(ne, &r1_is_not_smi);
7947 GenerateTypeTransition(masm); // Tail call.
7948 }
7949
7950 __ bind(&finished_loading_r0);
7951
7952 // Move r1 to a double in r0-r1.
7953 __ tst(r1, Operand(kSmiTagMask));
7954 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
7955 __ bind(&r1_is_not_smi);
7956 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
7957 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7958 __ cmp(r4, heap_number_map);
7959 __ b(ne, &slow);
7960 if (mode_ == OVERWRITE_LEFT) {
7961 __ mov(r5, Operand(r1)); // Overwrite this heap number.
7962 }
7963 if (use_fp_registers) {
7964 CpuFeatures::Scope scope(VFP3);
7965 // Load the double from tagged HeapNumber r1 to d6.
7966 __ sub(r7, r1, Operand(kHeapObjectTag));
7967 __ vldr(d6, r7, HeapNumber::kValueOffset);
7968 } else {
7969 // Calling convention says that first double is in r0 and r1.
7970 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
7971 }
7972 __ jmp(&finished_loading_r1);
7973 __ bind(&r1_is_smi);
7974 if (mode_ == OVERWRITE_LEFT) {
7975 // We can't overwrite a Smi so get address of new heap number into r5.
7976 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7977 }
7978
7979 if (CpuFeatures::IsSupported(VFP3)) {
7980 CpuFeatures::Scope scope(VFP3);
7981 // Convert smi in r1 to double in d6.
7982 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
7983 __ vmov(s13, r7);
7984 __ vcvt_f64_s32(d6, s13);
7985 if (!use_fp_registers) {
7986 __ vmov(r0, r1, d6);
7987 }
7988 } else {
7989 // Write Smi from r1 to r1 and r0 in double format.
7990 __ mov(r7, Operand(r1));
7991 ConvertToDoubleStub stub4(r1, r0, r7, r9);
7992 __ push(lr);
7993 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
7994 __ pop(lr);
7995 }
7996
7997 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00007998 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007999
8000 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
8001 __ bind(&do_the_call);
8002 // If we are inlining the operation using VFP3 instructions for
8003 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
8004 if (use_fp_registers) {
8005 CpuFeatures::Scope scope(VFP3);
8006 // ARMv7 VFP3 instructions to implement
8007 // double precision, add, subtract, multiply, divide.
8008
8009 if (Token::MUL == op_) {
8010 __ vmul(d5, d6, d7);
8011 } else if (Token::DIV == op_) {
8012 __ vdiv(d5, d6, d7);
8013 } else if (Token::ADD == op_) {
8014 __ vadd(d5, d6, d7);
8015 } else if (Token::SUB == op_) {
8016 __ vsub(d5, d6, d7);
8017 } else {
8018 UNREACHABLE();
8019 }
8020 __ sub(r0, r5, Operand(kHeapObjectTag));
8021 __ vstr(d5, r0, HeapNumber::kValueOffset);
8022 __ add(r0, r0, Operand(kHeapObjectTag));
8023 __ mov(pc, lr);
8024 } else {
8025 // If we did not inline the operation, then the arguments are in:
8026 // r0: Left value (least significant part of mantissa).
8027 // r1: Left value (sign, exponent, top of mantissa).
8028 // r2: Right value (least significant part of mantissa).
8029 // r3: Right value (sign, exponent, top of mantissa).
8030 // r5: Address of heap number for result.
8031
8032 __ push(lr); // For later.
8033 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
8034 // Call C routine that may not cause GC or other trouble. r5 is callee
8035 // save.
8036 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
8037 // Store answer in the overwritable heap number.
8038 #if !defined(USE_ARM_EABI)
8039 // Double returned in fp coprocessor register 0 and 1, encoded as
8040 // register cr8. Offsets must be divisible by 4 for coprocessor so we
8041 // need to substract the tag from r5.
8042 __ sub(r4, r5, Operand(kHeapObjectTag));
8043 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
8044 #else
8045 // Double returned in registers 0 and 1.
8046 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8047 #endif
8048 __ mov(r0, Operand(r5));
8049 // And we are done.
8050 __ pop(pc);
8051 }
8052 }
8053 }
8054
8055 if (!generate_code_to_calculate_answer &&
8056 !slow_reverse.is_linked() &&
8057 !slow.is_linked()) {
8058 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008059 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008060
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008061 if (lhs.is(r0)) {
8062 __ b(&slow);
8063 __ bind(&slow_reverse);
8064 __ Swap(r0, r1, ip);
8065 }
8066
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008067 heap_number_map = no_reg; // Don't use this any more from here on.
8068
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008069 // We jump to here if something goes wrong (one param is not a number of any
8070 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008071 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008072
8073 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008074 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008075
ager@chromium.org357bf652010-04-12 11:30:10 +00008076 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008077 // Test for string arguments before calling runtime.
8078 // r1 : first argument
8079 // r0 : second argument
8080 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008081 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008082
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008083 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008084 __ tst(r1, Operand(kSmiTagMask));
8085 __ b(eq, &not_string1);
8086 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8087 __ b(ge, &not_string1);
8088
8089 // First argument is a a string, test second.
8090 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008091 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008092 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8093 __ b(ge, &string1);
8094
8095 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008096 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8097 __ TailCallStub(&string_add_stub);
8098
8099 __ bind(&string1_smi2);
8100 // First argument is a string, second is a smi. Try to lookup the number
8101 // string for the smi in the number string cache.
8102 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008103 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008104
8105 // Replace second argument on stack and tailcall string add stub to make
8106 // the result.
8107 __ str(r2, MemOperand(sp, 0));
8108 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008109
8110 // Only first argument is a string.
8111 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008112 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8113
8114 // First argument was not a string, test second.
8115 __ bind(&not_string1);
8116 __ tst(r0, Operand(kSmiTagMask));
8117 __ b(eq, &not_strings);
8118 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8119 __ b(ge, &not_strings);
8120
8121 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008122 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8123
8124 __ bind(&not_strings);
8125 }
8126
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008127 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008128}
8129
8130
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008131// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008132// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008133// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8134// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008135// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8136// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008137static void GetInt32(MacroAssembler* masm,
8138 Register source,
8139 Register dest,
8140 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008141 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008142 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008143 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008144 // Get exponent word.
8145 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8146 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008147 __ Ubfx(scratch2,
8148 scratch,
8149 HeapNumber::kExponentShift,
8150 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008151 // Load dest with zero. We use this either for the final shift or
8152 // for the answer.
8153 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008154 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008155 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8156 // the exponent that we are fastest at and also the highest exponent we can
8157 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008158 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8159 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8160 // split it up to avoid a constant pool entry. You can't do that in general
8161 // for cmp because of the overflow flag, but we know the exponent is in the
8162 // range 0-2047 so there is no overflow.
8163 int fudge_factor = 0x400;
8164 __ sub(scratch2, scratch2, Operand(fudge_factor));
8165 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008166 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8167 __ b(eq, &right_exponent);
8168 // If the exponent is higher than that then go to slow case. This catches
8169 // numbers that don't fit in a signed int32, infinities and NaNs.
8170 __ b(gt, slow);
8171
8172 // We know the exponent is smaller than 30 (biased). If it is less than
8173 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8174 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008175 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8176 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008177 // Dest already has a Smi zero.
8178 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008179 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008180 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8181 // get how much to shift down.
8182 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008183 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008184 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008185 if (CpuFeatures::IsSupported(VFP3)) {
8186 CpuFeatures::Scope scope(VFP3);
8187 // ARMv7 VFP3 instructions implementing double precision to integer
8188 // conversion using round to zero.
8189 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008190 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008191 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008192 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008193 } else {
8194 // Get the top bits of the mantissa.
8195 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8196 // Put back the implicit 1.
8197 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8198 // Shift up the mantissa bits to take up the space the exponent used to
8199 // take. We just orred in the implicit bit so that took care of one and
8200 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8201 // distance.
8202 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8203 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8204 // Put sign in zero flag.
8205 __ tst(scratch, Operand(HeapNumber::kSignMask));
8206 // Get the second half of the double. For some exponents we don't
8207 // actually need this because the bits get shifted out again, but
8208 // it's probably slower to test than just to do it.
8209 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8210 // Shift down 22 bits to get the last 10 bits.
8211 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8212 // Move down according to the exponent.
8213 __ mov(dest, Operand(scratch, LSR, dest));
8214 // Fix sign if sign bit was set.
8215 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8216 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008217 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008218}
8219
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008220// For bitwise ops where the inputs are not both Smis we here try to determine
8221// whether both inputs are either Smis or at least heap numbers that can be
8222// represented by a 32 bit signed value. We truncate towards zero as required
8223// by the ES spec. If this is the case we do the bitwise op and see if the
8224// result is a Smi. If so, great, otherwise we try to find a heap number to
8225// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008226// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8227void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8228 Register lhs,
8229 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008230 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008231 Label rhs_is_smi, lhs_is_smi;
8232 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008233
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008234 Register heap_number_map = r6;
8235 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8236
ager@chromium.org357bf652010-04-12 11:30:10 +00008237 __ tst(lhs, Operand(kSmiTagMask));
8238 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008239 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8240 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008241 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008242 GetInt32(masm, lhs, r3, r5, r4, &slow);
8243 __ jmp(&done_checking_lhs);
8244 __ bind(&lhs_is_smi);
8245 __ mov(r3, Operand(lhs, ASR, 1));
8246 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008247
ager@chromium.org357bf652010-04-12 11:30:10 +00008248 __ tst(rhs, Operand(kSmiTagMask));
8249 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008250 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8251 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008252 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008253 GetInt32(masm, rhs, r2, r5, r4, &slow);
8254 __ jmp(&done_checking_rhs);
8255 __ bind(&rhs_is_smi);
8256 __ mov(r2, Operand(rhs, ASR, 1));
8257 __ bind(&done_checking_rhs);
8258
8259 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008260
8261 // r0 and r1: Original operands (Smi or heap numbers).
8262 // r2 and r3: Signed int32 operands.
8263 switch (op_) {
8264 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8265 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8266 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8267 case Token::SAR:
8268 // Use only the 5 least significant bits of the shift count.
8269 __ and_(r2, r2, Operand(0x1f));
8270 __ mov(r2, Operand(r3, ASR, r2));
8271 break;
8272 case Token::SHR:
8273 // Use only the 5 least significant bits of the shift count.
8274 __ and_(r2, r2, Operand(0x1f));
8275 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8276 // SHR is special because it is required to produce a positive answer.
8277 // The code below for writing into heap numbers isn't capable of writing
8278 // the register as an unsigned int so we go to slow case if we hit this
8279 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008280 if (CpuFeatures::IsSupported(VFP3)) {
8281 __ b(mi, &result_not_a_smi);
8282 } else {
8283 __ b(mi, &slow);
8284 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008285 break;
8286 case Token::SHL:
8287 // Use only the 5 least significant bits of the shift count.
8288 __ and_(r2, r2, Operand(0x1f));
8289 __ mov(r2, Operand(r3, LSL, r2));
8290 break;
8291 default: UNREACHABLE();
8292 }
8293 // check that the *signed* result fits in a smi
8294 __ add(r3, r2, Operand(0x40000000), SetCC);
8295 __ b(mi, &result_not_a_smi);
8296 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8297 __ Ret();
8298
8299 Label have_to_allocate, got_a_heap_number;
8300 __ bind(&result_not_a_smi);
8301 switch (mode_) {
8302 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008303 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008304 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008305 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008306 break;
8307 }
8308 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008309 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008310 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008311 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008312 break;
8313 }
8314 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008315 // Get a new heap number in r5. r4 and r7 are scratch.
8316 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008317 }
8318 default: break;
8319 }
8320 __ bind(&got_a_heap_number);
8321 // r2: Answer as signed int32.
8322 // r5: Heap number to write answer into.
8323
8324 // Nothing can go wrong now, so move the heap number to r0, which is the
8325 // result.
8326 __ mov(r0, Operand(r5));
8327
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008328 if (CpuFeatures::IsSupported(VFP3)) {
8329 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8330 CpuFeatures::Scope scope(VFP3);
8331 __ vmov(s0, r2);
8332 if (op_ == Token::SHR) {
8333 __ vcvt_f64_u32(d0, s0);
8334 } else {
8335 __ vcvt_f64_s32(d0, s0);
8336 }
8337 __ sub(r3, r0, Operand(kHeapObjectTag));
8338 __ vstr(d0, r3, HeapNumber::kValueOffset);
8339 __ Ret();
8340 } else {
8341 // Tail call that writes the int32 in r2 to the heap number in r0, using
8342 // r3 as scratch. r0 is preserved and returned.
8343 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8344 __ TailCallStub(&stub);
8345 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008346
8347 if (mode_ != NO_OVERWRITE) {
8348 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008349 // Get a new heap number in r5. r4 and r7 are scratch.
8350 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008351 __ jmp(&got_a_heap_number);
8352 }
8353
8354 // If all else failed then we go to the runtime system.
8355 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008356 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008357 switch (op_) {
8358 case Token::BIT_OR:
8359 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8360 break;
8361 case Token::BIT_AND:
8362 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8363 break;
8364 case Token::BIT_XOR:
8365 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8366 break;
8367 case Token::SAR:
8368 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8369 break;
8370 case Token::SHR:
8371 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8372 break;
8373 case Token::SHL:
8374 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8375 break;
8376 default:
8377 UNREACHABLE();
8378 }
8379}
8380
8381
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008382// Can we multiply by x with max two shifts and an add.
8383// This answers yes to all integers from 2 to 10.
8384static bool IsEasyToMultiplyBy(int x) {
8385 if (x < 2) return false; // Avoid special cases.
8386 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8387 if (IsPowerOf2(x)) return true; // Simple shift.
8388 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8389 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8390 return false;
8391}
8392
8393
8394// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8395// Source and destination may be the same register. This routine does
8396// not set carry and overflow the way a mul instruction would.
8397static void MultiplyByKnownInt(MacroAssembler* masm,
8398 Register source,
8399 Register destination,
8400 int known_int) {
8401 if (IsPowerOf2(known_int)) {
8402 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8403 } else if (PopCountLessThanEqual2(known_int)) {
8404 int first_bit = BitPosition(known_int);
8405 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8406 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8407 if (first_bit != 0) {
8408 __ mov(destination, Operand(destination, LSL, first_bit));
8409 }
8410 } else {
8411 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8412 int the_bit = BitPosition(known_int + 1);
8413 __ rsb(destination, source, Operand(source, LSL, the_bit));
8414 }
8415}
8416
8417
8418// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8419// a register for the cases where it doesn't know a good trick, and may deliver
8420// a result that needs shifting.
8421static void MultiplyByKnownInt2(
8422 MacroAssembler* masm,
8423 Register result,
8424 Register source,
8425 Register known_int_register, // Smi tagged.
8426 int known_int,
8427 int* required_shift) { // Including Smi tag shift
8428 switch (known_int) {
8429 case 3:
8430 __ add(result, source, Operand(source, LSL, 1));
8431 *required_shift = 1;
8432 break;
8433 case 5:
8434 __ add(result, source, Operand(source, LSL, 2));
8435 *required_shift = 1;
8436 break;
8437 case 6:
8438 __ add(result, source, Operand(source, LSL, 1));
8439 *required_shift = 2;
8440 break;
8441 case 7:
8442 __ rsb(result, source, Operand(source, LSL, 3));
8443 *required_shift = 1;
8444 break;
8445 case 9:
8446 __ add(result, source, Operand(source, LSL, 3));
8447 *required_shift = 1;
8448 break;
8449 case 10:
8450 __ add(result, source, Operand(source, LSL, 2));
8451 *required_shift = 2;
8452 break;
8453 default:
8454 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8455 __ mul(result, source, known_int_register);
8456 *required_shift = 0;
8457 }
8458}
8459
8460
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008461// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8462// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8463// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8464// number from 0 to mask. On exit the 'eq' condition flags are set if the
8465// answer is exactly the mask.
8466void IntegerModStub::DigitSum(MacroAssembler* masm,
8467 Register lhs,
8468 int mask,
8469 int shift,
8470 Label* entry) {
8471 ASSERT(mask > 0);
8472 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8473 Label loop;
8474 __ bind(&loop);
8475 __ and_(ip, lhs, Operand(mask));
8476 __ add(lhs, ip, Operand(lhs, LSR, shift));
8477 __ bind(entry);
8478 __ cmp(lhs, Operand(mask));
8479 __ b(gt, &loop);
8480}
8481
8482
8483void IntegerModStub::DigitSum(MacroAssembler* masm,
8484 Register lhs,
8485 Register scratch,
8486 int mask,
8487 int shift1,
8488 int shift2,
8489 Label* entry) {
8490 ASSERT(mask > 0);
8491 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8492 Label loop;
8493 __ bind(&loop);
8494 __ bic(scratch, lhs, Operand(mask));
8495 __ and_(ip, lhs, Operand(mask));
8496 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8497 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8498 __ bind(entry);
8499 __ cmp(lhs, Operand(mask));
8500 __ b(gt, &loop);
8501}
8502
8503
8504// Splits the number into two halves (bottom half has shift bits). The top
8505// half is subtracted from the bottom half. If the result is negative then
8506// rhs is added.
8507void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8508 Register lhs,
8509 int shift,
8510 int rhs) {
8511 int mask = (1 << shift) - 1;
8512 __ and_(ip, lhs, Operand(mask));
8513 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8514 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8515}
8516
8517
8518void IntegerModStub::ModReduce(MacroAssembler* masm,
8519 Register lhs,
8520 int max,
8521 int denominator) {
8522 int limit = denominator;
8523 while (limit * 2 <= max) limit *= 2;
8524 while (limit >= denominator) {
8525 __ cmp(lhs, Operand(limit));
8526 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8527 limit >>= 1;
8528 }
8529}
8530
8531
8532void IntegerModStub::ModAnswer(MacroAssembler* masm,
8533 Register result,
8534 Register shift_distance,
8535 Register mask_bits,
8536 Register sum_of_digits) {
8537 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8538 __ Ret();
8539}
8540
8541
8542// See comment for class.
8543void IntegerModStub::Generate(MacroAssembler* masm) {
8544 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8545 __ bic(odd_number_, odd_number_, Operand(1));
8546 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8547 // We now have (odd_number_ - 1) * 2 in the register.
8548 // Build a switch out of branches instead of data because it avoids
8549 // having to teach the assembler about intra-code-object pointers
8550 // that are not in relative branch instructions.
8551 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8552 Label mod21, mod23, mod25;
8553 { Assembler::BlockConstPoolScope block_const_pool(masm);
8554 __ add(pc, pc, Operand(odd_number_));
8555 // When you read pc it is always 8 ahead, but when you write it you always
8556 // write the actual value. So we put in two nops to take up the slack.
8557 __ nop();
8558 __ nop();
8559 __ b(&mod3);
8560 __ b(&mod5);
8561 __ b(&mod7);
8562 __ b(&mod9);
8563 __ b(&mod11);
8564 __ b(&mod13);
8565 __ b(&mod15);
8566 __ b(&mod17);
8567 __ b(&mod19);
8568 __ b(&mod21);
8569 __ b(&mod23);
8570 __ b(&mod25);
8571 }
8572
8573 // For each denominator we find a multiple that is almost only ones
8574 // when expressed in binary. Then we do the sum-of-digits trick for
8575 // that number. If the multiple is not 1 then we have to do a little
8576 // more work afterwards to get the answer into the 0-denominator-1
8577 // range.
8578 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8579 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8580 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8581
8582 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8583 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8584 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8585
8586 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8587 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8588 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8589
8590 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8591 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8592 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8593
8594 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8595 ModReduce(masm, lhs_, 0x3f, 11);
8596 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8597
8598 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8599 ModReduce(masm, lhs_, 0xff, 13);
8600 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8601
8602 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8603 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8604 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8605
8606 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8607 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8608 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8609
8610 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8611 ModReduce(masm, lhs_, 0xff, 19);
8612 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8613
8614 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8615 ModReduce(masm, lhs_, 0x3f, 21);
8616 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8617
8618 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8619 ModReduce(masm, lhs_, 0xff, 23);
8620 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8621
8622 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8623 ModReduce(masm, lhs_, 0x7f, 25);
8624 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8625}
8626
8627
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008628const char* GenericBinaryOpStub::GetName() {
8629 if (name_ != NULL) return name_;
8630 const int len = 100;
8631 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8632 if (name_ == NULL) return "OOM";
8633 const char* op_name = Token::Name(op_);
8634 const char* overwrite_name;
8635 switch (mode_) {
8636 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8637 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8638 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8639 default: overwrite_name = "UnknownOverwrite"; break;
8640 }
8641
8642 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008643 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008644 op_name,
8645 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008646 specialized_on_rhs_ ? "_ConstantRhs" : "",
8647 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008648 return name_;
8649}
8650
8651
ager@chromium.org5c838252010-02-19 08:53:10 +00008652
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008653void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008654 // lhs_ : x
8655 // rhs_ : y
8656 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008657
ager@chromium.org357bf652010-04-12 11:30:10 +00008658 Register result = r0;
8659 Register lhs = lhs_;
8660 Register rhs = rhs_;
8661
8662 // This code can't cope with other register allocations yet.
8663 ASSERT(result.is(r0) &&
8664 ((lhs.is(r0) && rhs.is(r1)) ||
8665 (lhs.is(r1) && rhs.is(r0))));
8666
8667 Register smi_test_reg = VirtualFrame::scratch0();
8668 Register scratch = VirtualFrame::scratch1();
8669
8670 // All ops need to know whether we are dealing with two Smis. Set up
8671 // smi_test_reg to tell us that.
8672 if (ShouldGenerateSmiCode()) {
8673 __ orr(smi_test_reg, lhs, Operand(rhs));
8674 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008675
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008676 switch (op_) {
8677 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008678 Label not_smi;
8679 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008680 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008681 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008682 __ tst(smi_test_reg, Operand(kSmiTagMask));
8683 __ b(ne, &not_smi);
8684 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8685 // Return if no overflow.
8686 __ Ret(vc);
8687 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8688 }
8689 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008690 break;
8691 }
8692
8693 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008694 Label not_smi;
8695 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008696 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008697 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008698 __ tst(smi_test_reg, Operand(kSmiTagMask));
8699 __ b(ne, &not_smi);
8700 if (lhs.is(r1)) {
8701 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8702 // Return if no overflow.
8703 __ Ret(vc);
8704 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8705 } else {
8706 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8707 // Return if no overflow.
8708 __ Ret(vc);
8709 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8710 }
8711 }
8712 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008713 break;
8714 }
8715
8716 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008717 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008718 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008719 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008720 __ tst(smi_test_reg, Operand(kSmiTagMask));
8721 Register scratch2 = smi_test_reg;
8722 smi_test_reg = no_reg;
8723 __ b(ne, &not_smi);
8724 // Remove tag from one operand (but keep sign), so that result is Smi.
8725 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8726 // Do multiplication
8727 // scratch = lower 32 bits of ip * lhs.
8728 __ smull(scratch, scratch2, lhs, ip);
8729 // Go slow on overflows (overflow bit is not set).
8730 __ mov(ip, Operand(scratch, ASR, 31));
8731 // No overflow if higher 33 bits are identical.
8732 __ cmp(ip, Operand(scratch2));
8733 __ b(ne, &slow);
8734 // Go slow on zero result to handle -0.
8735 __ tst(scratch, Operand(scratch));
8736 __ mov(result, Operand(scratch), LeaveCC, ne);
8737 __ Ret(ne);
8738 // We need -0 if we were multiplying a negative number with 0 to get 0.
8739 // We know one of them was zero.
8740 __ add(scratch2, rhs, Operand(lhs), SetCC);
8741 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
8742 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
8743 // Slow case. We fall through here if we multiplied a negative number
8744 // with 0, because that would mean we should produce -0.
8745 __ bind(&slow);
8746 }
8747 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008748 break;
8749 }
8750
8751 case Token::DIV:
8752 case Token::MOD: {
8753 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008754 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008755 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00008756 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008757 if (IsPowerOf2(constant_rhs_)) {
8758 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008759 __ and_(rhs,
8760 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008761 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
8762 SetCC);
8763 // We now have the answer, but if the input was negative we also
8764 // have the sign bit. Our work is done if the result is
8765 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00008766 if (!rhs.is(r0)) {
8767 __ mov(r0, rhs, LeaveCC, pl);
8768 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008769 __ Ret(pl);
8770 // A mod of a negative left hand side must return a negative number.
8771 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00008772 // already optimistically trashed rhs so we may need to restore it.
8773 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008774 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008775 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008776 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008777 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00008778 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008779 } else {
8780 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00008781 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008782 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008783 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008784 int shift = 0;
8785 int d = constant_rhs_;
8786 while ((d & 1) == 0) {
8787 d >>= 1;
8788 shift++;
8789 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008790 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008791 __ bic(r0, r0, Operand(kSmiTagMask));
8792 }
8793 } else {
8794 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00008795 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008796 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008797 // Find a fixed point reciprocal of the divisor so we can divide by
8798 // multiplying.
8799 double divisor = 1.0 / constant_rhs_;
8800 int shift = 32;
8801 double scale = 4294967296.0; // 1 << 32.
8802 uint32_t mul;
8803 // Maximise the precision of the fixed point reciprocal.
8804 while (true) {
8805 mul = static_cast<uint32_t>(scale * divisor);
8806 if (mul >= 0x7fffffff) break;
8807 scale *= 2.0;
8808 shift++;
8809 }
8810 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00008811 Register scratch2 = smi_test_reg;
8812 smi_test_reg = no_reg;
8813 __ mov(scratch2, Operand(mul));
8814 __ umull(scratch, scratch2, scratch2, lhs);
8815 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
8816 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
8817 // rhs is still the known rhs. rhs is Smi tagged.
8818 // lhs is still the unkown lhs. lhs is Smi tagged.
8819 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
8820 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008821 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00008822 scratch,
8823 scratch2,
8824 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008825 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00008826 &required_scratch_shift);
8827 // scratch << required_scratch_shift is now the Smi tagged rhs *
8828 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008829 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008830 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008831 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00008832 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008833 } else {
8834 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00008835 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008836 }
8837 }
8838 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008839 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008840 } else if (op_ == Token::MOD &&
8841 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
8842 runtime_operands_type_ != BinaryOpIC::STRINGS) {
8843 // Do generate a bit of smi code for modulus even though the default for
8844 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008845 // support for modulus checking for smis makes sense. We can handle
8846 // 1 to 25 times any power of 2. This covers over half the numbers from
8847 // 1 to 100 including all of the first 25. (Actually the constants < 10
8848 // are handled above by reciprocal multiplication. We only get here for
8849 // those cases if the right hand side is not a constant or for cases
8850 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
8851 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008852 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008853 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008854 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008855 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008856 // Check for two positive smis.
8857 __ orr(smi_test_reg, lhs, Operand(rhs));
8858 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
8859 __ b(ne, &slow);
8860 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008861 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008862 __ sub(scratch, rhs, Operand(1), SetCC);
8863 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008864 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
8865 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008866 // Calculate power of two modulus.
8867 __ and_(result, lhs, Operand(scratch));
8868 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008869
8870 __ bind(&not_power_of_2);
8871 __ eor(scratch, scratch, Operand(mask_bits));
8872 // At least two bits are set in the modulus. The high one(s) are in
8873 // mask_bits and the low one is scratch + 1.
8874 __ and_(mask_bits, scratch, Operand(lhs));
8875 Register shift_distance = scratch;
8876 scratch = no_reg;
8877
8878 // The rhs consists of a power of 2 multiplied by some odd number.
8879 // The power-of-2 part we handle by putting the corresponding bits
8880 // from the lhs in the mask_bits register, and the power in the
8881 // shift_distance register. Shift distance is never 0 due to Smi
8882 // tagging.
8883 __ CountLeadingZeros(r4, shift_distance, shift_distance);
8884 __ rsb(shift_distance, r4, Operand(32));
8885
8886 // Now we need to find out what the odd number is. The last bit is
8887 // always 1.
8888 Register odd_number = r4;
8889 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
8890 __ cmp(odd_number, Operand(25));
8891 __ b(gt, &slow);
8892
8893 IntegerModStub stub(
8894 result, shift_distance, odd_number, mask_bits, lhs, r5);
8895 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
8896
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008897 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008898 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008899 HandleBinaryOpSlowCases(
8900 masm,
8901 &not_smi,
8902 lhs,
8903 rhs,
8904 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008905 break;
8906 }
8907
8908 case Token::BIT_OR:
8909 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008910 case Token::BIT_XOR:
8911 case Token::SAR:
8912 case Token::SHR:
8913 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008914 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008915 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008916 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008917 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008918 Register scratch2 = smi_test_reg;
8919 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008920 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008921 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
8922 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
8923 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008924 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008925 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00008926 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8927 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008928 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008929 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008930 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008931 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008932 // Remove tags from operands. We can't do this on a 31 bit number
8933 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00008934 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8935 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8936 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008937 // Unsigned shift is not allowed to produce a negative number, so
8938 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00008939 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008940 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008941 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008942 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008943 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008944 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008945 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00008946 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8947 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8948 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008949 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00008950 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008951 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008952 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008953 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008954 default: UNREACHABLE();
8955 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008956 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008957 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008958 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008959 break;
8960 }
8961
8962 default: UNREACHABLE();
8963 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008964 // This code should be unreachable.
8965 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00008966
8967 // Generate an unreachable reference to the DEFAULT stub so that it can be
8968 // found at the end of this stub when clearing ICs at GC.
8969 // TODO(kaznacheev): Check performance impact and get rid of this.
8970 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
8971 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
8972 __ CallStub(&uninit);
8973 }
8974}
8975
8976
8977void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
8978 Label get_result;
8979
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008980 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008981
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008982 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
8983 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00008984 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008985 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008986
8987 __ TailCallExternalReference(
8988 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008989 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00008990 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008991}
8992
8993
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008994Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008995 GenericBinaryOpStub stub(key, type_info);
8996 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008997}
8998
8999
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009000void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9001 // Argument is a number and is on stack and in r0.
9002 Label runtime_call;
9003 Label input_not_smi;
9004 Label loaded;
9005
9006 if (CpuFeatures::IsSupported(VFP3)) {
9007 // Load argument and check if it is a smi.
9008 __ BranchOnNotSmi(r0, &input_not_smi);
9009
9010 CpuFeatures::Scope scope(VFP3);
9011 // Input is a smi. Convert to double and load the low and high words
9012 // of the double into r2, r3.
9013 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
9014 __ b(&loaded);
9015
9016 __ bind(&input_not_smi);
9017 // Check if input is a HeapNumber.
9018 __ CheckMap(r0,
9019 r1,
9020 Heap::kHeapNumberMapRootIndex,
9021 &runtime_call,
9022 true);
9023 // Input is a HeapNumber. Load it to a double register and store the
9024 // low and high words into r2, r3.
9025 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
9026
9027 __ bind(&loaded);
9028 // r2 = low 32 bits of double value
9029 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009030 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009031 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
9032 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009033 __ eor(r1, r1, Operand(r1, ASR, 16));
9034 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009035 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009036 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009037
9038 // r2 = low 32 bits of double value.
9039 // r3 = high 32 bits of double value.
9040 // r1 = TranscendentalCache::hash(double value).
9041 __ mov(r0,
9042 Operand(ExternalReference::transcendental_cache_array_address()));
9043 // r0 points to cache array.
9044 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
9045 // r0 points to the cache for the type type_.
9046 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9047 __ cmp(r0, Operand(0));
9048 __ b(eq, &runtime_call);
9049
9050#ifdef DEBUG
9051 // Check that the layout of cache elements match expectations.
9052 { TranscendentalCache::Element test_elem[2];
9053 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9054 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9055 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9056 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9057 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9058 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9059 CHECK_EQ(0, elem_in0 - elem_start);
9060 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9061 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9062 }
9063#endif
9064
9065 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9066 __ add(r1, r1, Operand(r1, LSL, 1));
9067 __ add(r0, r0, Operand(r1, LSL, 2));
9068 // Check if cache matches: Double value is stored in uint32_t[2] array.
9069 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9070 __ cmp(r2, r4);
9071 __ b(ne, &runtime_call);
9072 __ cmp(r3, r5);
9073 __ b(ne, &runtime_call);
9074 // Cache hit. Load result, pop argument and return.
9075 __ mov(r0, Operand(r6));
9076 __ pop();
9077 __ Ret();
9078 }
9079
9080 __ bind(&runtime_call);
9081 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9082}
9083
9084
9085Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9086 switch (type_) {
9087 // Add more cases when necessary.
9088 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9089 case TranscendentalCache::COS: return Runtime::kMath_cos;
9090 default:
9091 UNIMPLEMENTED();
9092 return Runtime::kAbort;
9093 }
9094}
9095
9096
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009097void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009098 // Do tail-call to runtime routine. Runtime routines expect at least one
9099 // argument, so give it a Smi.
9100 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009101 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009102 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009103
9104 __ StubReturn(1);
9105}
9106
9107
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009108void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009109 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009110
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009111 Register heap_number_map = r6;
9112 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9113
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009114 if (op_ == Token::SUB) {
9115 // Check whether the value is a smi.
9116 Label try_float;
9117 __ tst(r0, Operand(kSmiTagMask));
9118 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009119
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009120 // Go slow case if the value of the expression is zero
9121 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009122 if (negative_zero_ == kStrictNegativeZero) {
9123 // If we have to check for zero, then we can check for the max negative
9124 // smi while we are at it.
9125 __ bic(ip, r0, Operand(0x80000000), SetCC);
9126 __ b(eq, &slow);
9127 __ rsb(r0, r0, Operand(0));
9128 __ StubReturn(1);
9129 } else {
9130 // The value of the expression is a smi and 0 is OK for -0. Try
9131 // optimistic subtraction '0 - value'.
9132 __ rsb(r0, r0, Operand(0), SetCC);
9133 __ StubReturn(1, vc);
9134 // We don't have to reverse the optimistic neg since the only case
9135 // where we fall through is the minimum negative Smi, which is the case
9136 // where the neg leaves the register unchanged.
9137 __ jmp(&slow); // Go slow on max negative Smi.
9138 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009139
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009140 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009141 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9142 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9143 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009144 __ b(ne, &slow);
9145 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009146 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009147 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9148 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9149 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9150 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009151 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009152 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9153 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9154 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9155 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9156 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9157 __ mov(r0, Operand(r1));
9158 }
9159 } else if (op_ == Token::BIT_NOT) {
9160 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009161 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9162 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9163 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009164 __ b(ne, &slow);
9165
9166 // Convert the heap number is r0 to an untagged integer in r1.
9167 GetInt32(masm, r0, r1, r2, r3, &slow);
9168
9169 // Do the bitwise operation (move negated) and check if the result
9170 // fits in a smi.
9171 Label try_float;
9172 __ mvn(r1, Operand(r1));
9173 __ add(r2, r1, Operand(0x40000000), SetCC);
9174 __ b(mi, &try_float);
9175 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9176 __ b(&done);
9177
9178 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009179 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009180 // Allocate a fresh heap number, but don't overwrite r0 until
9181 // we're sure we can do it without going through the slow case
9182 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009183 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009184 __ mov(r0, Operand(r2));
9185 }
9186
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009187 if (CpuFeatures::IsSupported(VFP3)) {
9188 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9189 CpuFeatures::Scope scope(VFP3);
9190 __ vmov(s0, r1);
9191 __ vcvt_f64_s32(d0, s0);
9192 __ sub(r2, r0, Operand(kHeapObjectTag));
9193 __ vstr(d0, r2, HeapNumber::kValueOffset);
9194 } else {
9195 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9196 // have to set up a frame.
9197 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9198 __ push(lr);
9199 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9200 __ pop(lr);
9201 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009202 } else {
9203 UNIMPLEMENTED();
9204 }
9205
9206 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009207 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009208
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009209 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009210 __ bind(&slow);
9211 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009212 switch (op_) {
9213 case Token::SUB:
9214 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9215 break;
9216 case Token::BIT_NOT:
9217 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9218 break;
9219 default:
9220 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009221 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009222}
9223
9224
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009225void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009226 // r0 holds the exception.
9227
9228 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009229 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009230
9231 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009232 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9233 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009234
9235 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009236 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009237 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009238 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009239 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009240 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9241
9242 // Before returning we restore the context from the frame pointer if
9243 // not NULL. The frame pointer is NULL in the exception handler of a
9244 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009245 __ cmp(fp, Operand(0));
9246 // Set cp to NULL if fp is NULL.
9247 __ mov(cp, Operand(0), LeaveCC, eq);
9248 // Restore cp otherwise.
9249 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009250#ifdef DEBUG
9251 if (FLAG_debug_code) {
9252 __ mov(lr, Operand(pc));
9253 }
9254#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009255 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009256 __ pop(pc);
9257}
9258
9259
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009260void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9261 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009262 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009263 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009264
9265 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009266 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009267 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009268
9269 // Unwind the handlers until the ENTRY handler is found.
9270 Label loop, done;
9271 __ bind(&loop);
9272 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009273 const int kStateOffset = StackHandlerConstants::kStateOffset;
9274 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009275 __ cmp(r2, Operand(StackHandler::ENTRY));
9276 __ b(eq, &done);
9277 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009278 const int kNextOffset = StackHandlerConstants::kNextOffset;
9279 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009280 __ jmp(&loop);
9281 __ bind(&done);
9282
9283 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009284 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009285 __ pop(r2);
9286 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009287
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009288 if (type == OUT_OF_MEMORY) {
9289 // Set external caught exception to false.
9290 ExternalReference external_caught(Top::k_external_caught_exception_address);
9291 __ mov(r0, Operand(false));
9292 __ mov(r2, Operand(external_caught));
9293 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009294
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009295 // Set pending exception and r0 to out of memory exception.
9296 Failure* out_of_memory = Failure::OutOfMemoryException();
9297 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9298 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9299 __ str(r0, MemOperand(r2));
9300 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009301
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009302 // Stack layout at this point. See also StackHandlerConstants.
9303 // sp -> state (ENTRY)
9304 // fp
9305 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009306
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009307 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009308 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009309 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
9310 // Before returning we restore the context from the frame pointer if
9311 // not NULL. The frame pointer is NULL in the exception handler of a
9312 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009313 __ cmp(fp, Operand(0));
9314 // Set cp to NULL if fp is NULL.
9315 __ mov(cp, Operand(0), LeaveCC, eq);
9316 // Restore cp otherwise.
9317 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009318#ifdef DEBUG
9319 if (FLAG_debug_code) {
9320 __ mov(lr, Operand(pc));
9321 }
9322#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009323 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009324 __ pop(pc);
9325}
9326
9327
9328void CEntryStub::GenerateCore(MacroAssembler* masm,
9329 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009330 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009331 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009332 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009333 bool always_allocate,
9334 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009335 // r0: result parameter for PerformGC, if any
9336 // r4: number of arguments including receiver (C callee-saved)
9337 // r5: pointer to builtin function (C callee-saved)
9338 // r6: pointer to the first argument (C callee-saved)
9339
9340 if (do_gc) {
9341 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009342 __ PrepareCallCFunction(1, r1);
9343 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009344 }
9345
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009346 ExternalReference scope_depth =
9347 ExternalReference::heap_always_allocate_scope_depth();
9348 if (always_allocate) {
9349 __ mov(r0, Operand(scope_depth));
9350 __ ldr(r1, MemOperand(r0));
9351 __ add(r1, r1, Operand(1));
9352 __ str(r1, MemOperand(r0));
9353 }
9354
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009355 // Call C built-in.
9356 // r0 = argc, r1 = argv
9357 __ mov(r0, Operand(r4));
9358 __ mov(r1, Operand(r6));
9359
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009360 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9361 int frame_alignment_mask = frame_alignment - 1;
9362#if defined(V8_HOST_ARCH_ARM)
9363 if (FLAG_debug_code) {
9364 if (frame_alignment > kPointerSize) {
9365 Label alignment_as_expected;
9366 ASSERT(IsPowerOf2(frame_alignment));
9367 __ sub(r2, sp, Operand(frame_alignment_skew));
9368 __ tst(r2, Operand(frame_alignment_mask));
9369 __ b(eq, &alignment_as_expected);
9370 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9371 __ stop("Unexpected alignment");
9372 __ bind(&alignment_as_expected);
9373 }
9374 }
9375#endif
9376
9377 // Just before the call (jump) below lr is pushed, so the actual alignment is
9378 // adding one to the current skew.
9379 int alignment_before_call =
9380 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9381 if (alignment_before_call > 0) {
9382 // Push until the alignment before the call is met.
9383 __ mov(r2, Operand(0));
9384 for (int i = alignment_before_call;
9385 (i & frame_alignment_mask) != 0;
9386 i += kPointerSize) {
9387 __ push(r2);
9388 }
9389 }
9390
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009391 // TODO(1242173): To let the GC traverse the return address of the exit
9392 // frames, we need to know where the return address is. Right now,
9393 // we push it on the stack to be able to find it again, but we never
9394 // restore from it in case of changes, which makes it impossible to
9395 // support moving the C entry code stub. This should be fixed, but currently
9396 // this is OK because the CEntryStub gets generated so early in the V8 boot
9397 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009398 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009399 masm->push(lr);
9400 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009401
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009402 // Restore sp back to before aligning the stack.
9403 if (alignment_before_call > 0) {
9404 __ add(sp, sp, Operand(alignment_before_call));
9405 }
9406
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009407 if (always_allocate) {
9408 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9409 // though (contain the result).
9410 __ mov(r2, Operand(scope_depth));
9411 __ ldr(r3, MemOperand(r2));
9412 __ sub(r3, r3, Operand(1));
9413 __ str(r3, MemOperand(r2));
9414 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009415
9416 // check for failure result
9417 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009418 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009419 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9420 __ add(r2, r0, Operand(1));
9421 __ tst(r2, Operand(kFailureTagMask));
9422 __ b(eq, &failure_returned);
9423
9424 // Exit C frame and return.
9425 // r0:r1: result
9426 // sp: stack pointer
9427 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009428 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009429
9430 // check if we should retry or throw exception
9431 Label retry;
9432 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009433 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009434 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9435 __ b(eq, &retry);
9436
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009437 // Special handling of out of memory exceptions.
9438 Failure* out_of_memory = Failure::OutOfMemoryException();
9439 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9440 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009441
9442 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009443 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009444 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009445 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009446 __ ldr(r0, MemOperand(ip));
9447 __ str(r3, MemOperand(ip));
9448
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009449 // Special handling of termination exceptions which are uncatchable
9450 // by javascript code.
9451 __ cmp(r0, Operand(Factory::termination_exception()));
9452 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009453
9454 // Handle normal exception.
9455 __ jmp(throw_normal_exception);
9456
9457 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9458}
9459
9460
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009461void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009462 // Called from JavaScript; parameters are on stack as if calling JS function
9463 // r0: number of arguments including receiver
9464 // r1: pointer to builtin function
9465 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009466 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009467 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009468
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009469 // Result returned in r0 or r0+r1 by default.
9470
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009471 // NOTE: Invocations of builtins may return failure objects
9472 // instead of a proper result. The builtin entry handles
9473 // this by performing a garbage collection and retrying the
9474 // builtin once.
9475
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009476 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009477 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009478
9479 // r4: number of arguments (C callee-saved)
9480 // r5: pointer to builtin function (C callee-saved)
9481 // r6: pointer to first argument (C callee-saved)
9482
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009483 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009484 Label throw_termination_exception;
9485 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009486
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009487 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009488 GenerateCore(masm,
9489 &throw_normal_exception,
9490 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009491 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009492 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009493 false,
9494 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009495
9496 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009497 GenerateCore(masm,
9498 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009499 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009500 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009501 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009502 false,
9503 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009504
9505 // Do full GC and retry runtime call one final time.
9506 Failure* failure = Failure::InternalError();
9507 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9508 GenerateCore(masm,
9509 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009510 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009511 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009512 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009513 true,
9514 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009515
9516 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009517 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9518
9519 __ bind(&throw_termination_exception);
9520 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009521
9522 __ bind(&throw_normal_exception);
9523 GenerateThrowTOS(masm);
9524}
9525
9526
9527void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9528 // r0: code entry
9529 // r1: function
9530 // r2: receiver
9531 // r3: argc
9532 // [sp+0]: argv
9533
9534 Label invoke, exit;
9535
9536 // Called from C, so do not pop argc and args on exit (preserve sp)
9537 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009538 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009539 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9540
9541 // Get address of argv, see stm above.
9542 // r0: code entry
9543 // r1: function
9544 // r2: receiver
9545 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009546 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009547
9548 // Push a frame with special values setup to mark it as an entry frame.
9549 // r0: code entry
9550 // r1: function
9551 // r2: receiver
9552 // r3: argc
9553 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009554 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009555 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9556 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009557 __ mov(r6, Operand(Smi::FromInt(marker)));
9558 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9559 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009560 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009561
9562 // Setup frame pointer for the frame to be pushed.
9563 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9564
9565 // Call a faked try-block that does the invoke.
9566 __ bl(&invoke);
9567
9568 // Caught exception: Store result (exception) in the pending
9569 // exception field in the JSEnv and return a failure sentinel.
9570 // Coming in here the fp will be invalid because the PushTryHandler below
9571 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009572 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009573 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009574 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009575 __ b(&exit);
9576
9577 // Invoke: Link this frame into the handler chain.
9578 __ bind(&invoke);
9579 // Must preserve r0-r4, r5-r7 are available.
9580 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009581 // If an exception not caught by another handler occurs, this handler
9582 // returns control to the code after the bl(&invoke) above, which
9583 // restores all kCalleeSaved registers (including cp and fp) to their
9584 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009585
9586 // Clear any pending exceptions.
9587 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9588 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009589 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009590 __ str(r5, MemOperand(ip));
9591
9592 // Invoke the function by calling through JS entry trampoline builtin.
9593 // Notice that we cannot store a reference to the trampoline code directly in
9594 // this stub, because runtime stubs are not traversed when doing GC.
9595
9596 // Expected registers by Builtins::JSEntryTrampoline
9597 // r0: code entry
9598 // r1: function
9599 // r2: receiver
9600 // r3: argc
9601 // r4: argv
9602 if (is_construct) {
9603 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9604 __ mov(ip, Operand(construct_entry));
9605 } else {
9606 ExternalReference entry(Builtins::JSEntryTrampoline);
9607 __ mov(ip, Operand(entry));
9608 }
9609 __ ldr(ip, MemOperand(ip)); // deref address
9610
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009611 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9612 // macro for the add instruction because we don't want the coverage tool
9613 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009614 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009615 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009616
9617 // Unlink this frame from the handler chain. When reading the
9618 // address of the next handler, there is no need to use the address
9619 // displacement since the current stack pointer (sp) points directly
9620 // to the stack handler.
9621 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9622 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9623 __ str(r3, MemOperand(ip));
9624 // No need to restore registers
9625 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9626
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009627
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009628 __ bind(&exit); // r0 holds result
9629 // Restore the top frame descriptors from the stack.
9630 __ pop(r3);
9631 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9632 __ str(r3, MemOperand(ip));
9633
9634 // Reset the stack to the callee saved registers.
9635 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9636
9637 // Restore callee-saved registers and return.
9638#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009639 if (FLAG_debug_code) {
9640 __ mov(lr, Operand(pc));
9641 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009642#endif
9643 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9644}
9645
9646
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009647// This stub performs an instanceof, calling the builtin function if
9648// necessary. Uses r1 for the object, r0 for the function that it may
9649// be an instance of (these are fetched from the stack).
9650void InstanceofStub::Generate(MacroAssembler* masm) {
9651 // Get the object - slow case for smis (we may need to throw an exception
9652 // depending on the rhs).
9653 Label slow, loop, is_instance, is_not_instance;
9654 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9655 __ BranchOnSmi(r0, &slow);
9656
9657 // Check that the left hand is a JS object and put map in r3.
9658 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9659 __ b(lt, &slow);
9660 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9661 __ b(gt, &slow);
9662
9663 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00009664 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009665 // r1 is function, r3 is map.
9666
9667 // Look up the function and the map in the instanceof cache.
9668 Label miss;
9669 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
9670 __ cmp(r1, ip);
9671 __ b(ne, &miss);
9672 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
9673 __ cmp(r3, ip);
9674 __ b(ne, &miss);
9675 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
9676 __ pop();
9677 __ pop();
9678 __ mov(pc, Operand(lr));
9679
9680 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009681 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
9682
9683 // Check that the function prototype is a JS object.
9684 __ BranchOnSmi(r4, &slow);
9685 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
9686 __ b(lt, &slow);
9687 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
9688 __ b(gt, &slow);
9689
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009690 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
9691 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
9692
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009693 // Register mapping: r3 is object map and r4 is function prototype.
9694 // Get prototype of object into r2.
9695 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9696
9697 // Loop through the prototype chain looking for the function prototype.
9698 __ bind(&loop);
9699 __ cmp(r2, Operand(r4));
9700 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009701 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9702 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009703 __ b(eq, &is_not_instance);
9704 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9705 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9706 __ jmp(&loop);
9707
9708 __ bind(&is_instance);
9709 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009710 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009711 __ pop();
9712 __ pop();
9713 __ mov(pc, Operand(lr)); // Return.
9714
9715 __ bind(&is_not_instance);
9716 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009717 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009718 __ pop();
9719 __ pop();
9720 __ mov(pc, Operand(lr)); // Return.
9721
9722 // Slow-case. Tail call builtin.
9723 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009724 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9725}
9726
9727
ager@chromium.org7c537e22008-10-16 08:43:32 +00009728void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9729 // The displacement is the offset of the last parameter (if any)
9730 // relative to the frame pointer.
9731 static const int kDisplacement =
9732 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009733
ager@chromium.org7c537e22008-10-16 08:43:32 +00009734 // Check that the key is a smi.
9735 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009736 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009737
ager@chromium.org7c537e22008-10-16 08:43:32 +00009738 // Check if the calling frame is an arguments adaptor frame.
9739 Label adaptor;
9740 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9741 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009742 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009743 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009744
ager@chromium.org7c537e22008-10-16 08:43:32 +00009745 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00009746 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00009747 // check for free.
9748 __ cmp(r1, r0);
9749 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009750
ager@chromium.org7c537e22008-10-16 08:43:32 +00009751 // Read the argument from the stack and return it.
9752 __ sub(r3, r0, r1);
9753 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9754 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009755 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009756
9757 // Arguments adaptor case: Check index against actual arguments
9758 // limit found in the arguments adaptor frame. Use unsigned
9759 // comparison to get negative check for free.
9760 __ bind(&adaptor);
9761 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9762 __ cmp(r1, r0);
9763 __ b(cs, &slow);
9764
9765 // Read the argument from the adaptor frame and return it.
9766 __ sub(r3, r0, r1);
9767 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9768 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009769 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009770
9771 // Slow-case: Handle non-smi or out-of-bounds access to arguments
9772 // by calling the runtime system.
9773 __ bind(&slow);
9774 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009775 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009776}
9777
9778
9779void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009780 // sp[0] : number of parameters
9781 // sp[4] : receiver displacement
9782 // sp[8] : function
9783
ager@chromium.org7c537e22008-10-16 08:43:32 +00009784 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00009785 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00009786 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9787 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009788 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00009789 __ b(eq, &adaptor_frame);
9790
9791 // Get the length from the frame.
9792 __ ldr(r1, MemOperand(sp, 0));
9793 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009794
9795 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00009796 __ bind(&adaptor_frame);
9797 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9798 __ str(r1, MemOperand(sp, 0));
9799 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009800 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
9801 __ str(r3, MemOperand(sp, 1 * kPointerSize));
9802
ager@chromium.org5c838252010-02-19 08:53:10 +00009803 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009804 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +00009805 Label add_arguments_object;
9806 __ bind(&try_allocate);
9807 __ cmp(r1, Operand(0));
9808 __ b(eq, &add_arguments_object);
9809 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
9810 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
9811 __ bind(&add_arguments_object);
9812 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
9813
9814 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009815 __ AllocateInNewSpace(
9816 r1,
9817 r0,
9818 r2,
9819 r3,
9820 &runtime,
9821 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +00009822
9823 // Get the arguments boilerplate from the current (global) context.
9824 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
9825 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
9826 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
9827 __ ldr(r4, MemOperand(r4, offset));
9828
9829 // Copy the JS object part.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00009830 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00009831
9832 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009833 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +00009834 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
9835 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
9836
9837 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009838 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009839 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
9840 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
9841
9842 // If there are no actual arguments, we're done.
9843 Label done;
9844 __ cmp(r1, Operand(0));
9845 __ b(eq, &done);
9846
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009847 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00009848 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009849
9850 // Setup the elements pointer in the allocated arguments object and
9851 // initialize the header in the elements fixed array.
9852 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
9853 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
9854 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
9855 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
9856 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009857 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +00009858
9859 // Copy the fixed array slots.
9860 Label loop;
9861 // Setup r4 to point to the first array slot.
9862 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
9863 __ bind(&loop);
9864 // Pre-decrement r2 with kPointerSize on each iteration.
9865 // Pre-decrement in order to skip receiver.
9866 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
9867 // Post-increment r4 with kPointerSize on each iteration.
9868 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
9869 __ sub(r1, r1, Operand(1));
9870 __ cmp(r1, Operand(0));
9871 __ b(ne, &loop);
9872
9873 // Return and remove the on-stack parameters.
9874 __ bind(&done);
9875 __ add(sp, sp, Operand(3 * kPointerSize));
9876 __ Ret();
9877
ager@chromium.org7c537e22008-10-16 08:43:32 +00009878 // Do the runtime call to allocate the arguments object.
9879 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009880 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009881}
9882
9883
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009884void RegExpExecStub::Generate(MacroAssembler* masm) {
9885 // Just jump directly to runtime if native RegExp is not selected at compile
9886 // time or if regexp entry in generated code is turned off runtime switch or
9887 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009888#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009889 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009890#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009891 if (!FLAG_regexp_entry_native) {
9892 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
9893 return;
9894 }
9895
9896 // Stack frame on entry.
9897 // sp[0]: last_match_info (expected JSArray)
9898 // sp[4]: previous index
9899 // sp[8]: subject string
9900 // sp[12]: JSRegExp object
9901
9902 static const int kLastMatchInfoOffset = 0 * kPointerSize;
9903 static const int kPreviousIndexOffset = 1 * kPointerSize;
9904 static const int kSubjectOffset = 2 * kPointerSize;
9905 static const int kJSRegExpOffset = 3 * kPointerSize;
9906
9907 Label runtime, invoke_regexp;
9908
9909 // Allocation of registers for this function. These are in callee save
9910 // registers and will be preserved by the call to the native RegExp code, as
9911 // this code is called using the normal C calling convention. When calling
9912 // directly from generated code the native RegExp code will not do a GC and
9913 // therefore the content of these registers are safe to use after the call.
9914 Register subject = r4;
9915 Register regexp_data = r5;
9916 Register last_match_info_elements = r6;
9917
9918 // Ensure that a RegExp stack is allocated.
9919 ExternalReference address_of_regexp_stack_memory_address =
9920 ExternalReference::address_of_regexp_stack_memory_address();
9921 ExternalReference address_of_regexp_stack_memory_size =
9922 ExternalReference::address_of_regexp_stack_memory_size();
9923 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
9924 __ ldr(r0, MemOperand(r0, 0));
9925 __ tst(r0, Operand(r0));
9926 __ b(eq, &runtime);
9927
9928 // Check that the first argument is a JSRegExp object.
9929 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009930 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009931 __ tst(r0, Operand(kSmiTagMask));
9932 __ b(eq, &runtime);
9933 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
9934 __ b(ne, &runtime);
9935
9936 // Check that the RegExp has been compiled (data contains a fixed array).
9937 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
9938 if (FLAG_debug_code) {
9939 __ tst(regexp_data, Operand(kSmiTagMask));
9940 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
9941 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
9942 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
9943 }
9944
9945 // regexp_data: RegExp data (FixedArray)
9946 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
9947 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
9948 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
9949 __ b(ne, &runtime);
9950
9951 // regexp_data: RegExp data (FixedArray)
9952 // Check that the number of captures fit in the static offsets vector buffer.
9953 __ ldr(r2,
9954 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
9955 // Calculate number of capture registers (number_of_captures + 1) * 2. This
9956 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009957 STATIC_ASSERT(kSmiTag == 0);
9958 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009959 __ add(r2, r2, Operand(2)); // r2 was a smi.
9960 // Check that the static offsets vector buffer is large enough.
9961 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
9962 __ b(hi, &runtime);
9963
9964 // r2: Number of capture registers
9965 // regexp_data: RegExp data (FixedArray)
9966 // Check that the second argument is a string.
9967 __ ldr(subject, MemOperand(sp, kSubjectOffset));
9968 __ tst(subject, Operand(kSmiTagMask));
9969 __ b(eq, &runtime);
9970 Condition is_string = masm->IsObjectStringType(subject, r0);
9971 __ b(NegateCondition(is_string), &runtime);
9972 // Get the length of the string to r3.
9973 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
9974
9975 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00009976 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009977 // subject: Subject string
9978 // regexp_data: RegExp data (FixedArray)
9979 // Check that the third argument is a positive smi less than the subject
9980 // string length. A negative value will be greater (unsigned comparison).
9981 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009982 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009983 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009984 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009985 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009986
9987 // r2: Number of capture registers
9988 // subject: Subject string
9989 // regexp_data: RegExp data (FixedArray)
9990 // Check that the fourth object is a JSArray object.
9991 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
9992 __ tst(r0, Operand(kSmiTagMask));
9993 __ b(eq, &runtime);
9994 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
9995 __ b(ne, &runtime);
9996 // Check that the JSArray is in fast case.
9997 __ ldr(last_match_info_elements,
9998 FieldMemOperand(r0, JSArray::kElementsOffset));
9999 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010000 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000010001 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010002 __ b(ne, &runtime);
10003 // Check that the last match info has space for the capture registers and the
10004 // additional information.
10005 __ ldr(r0,
10006 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
10007 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010008 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010009 __ b(gt, &runtime);
10010
10011 // subject: Subject string
10012 // regexp_data: RegExp data (FixedArray)
10013 // Check the representation and encoding of the subject string.
10014 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010015 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10016 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010017 // First check for flat string.
10018 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010019 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010020 __ b(eq, &seq_string);
10021
10022 // subject: Subject string
10023 // regexp_data: RegExp data (FixedArray)
10024 // Check for flat cons string.
10025 // A flat cons string is a cons string where the second part is the empty
10026 // string. In that case the subject string is just the first part of the cons
10027 // string. Also in this case the first part of the cons string is known to be
10028 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010029 STATIC_ASSERT(kExternalStringTag !=0);
10030 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010031 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010032 __ b(ne, &runtime);
10033 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
10034 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
10035 __ cmp(r0, r1);
10036 __ b(ne, &runtime);
10037 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
10038 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10039 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010040 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010041 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010042 __ tst(r0, Operand(kStringRepresentationMask));
10043 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010044
10045 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010046 // subject: Subject string
10047 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010048 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010049 STATIC_ASSERT(4 == kAsciiStringTag);
10050 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010051 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010052 __ and_(r0, r0, Operand(kStringEncodingMask));
10053 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010054 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10055 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10056
10057 // Check that the irregexp code has been generated for the actual string
10058 // encoding. If it has, the field contains a code object otherwise it contains
10059 // the hole.
10060 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10061 __ b(ne, &runtime);
10062
10063 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10064 // r7: code
10065 // subject: Subject string
10066 // regexp_data: RegExp data (FixedArray)
10067 // Load used arguments before starting to push arguments for call to native
10068 // RegExp code to avoid handling changing stack height.
10069 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10070 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10071
10072 // r1: previous index
10073 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10074 // r7: code
10075 // subject: Subject string
10076 // regexp_data: RegExp data (FixedArray)
10077 // All checks done. Now push arguments for native regexp code.
10078 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10079
10080 static const int kRegExpExecuteArguments = 7;
10081 __ push(lr);
10082 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10083
10084 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10085 __ mov(r0, Operand(1));
10086 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10087
10088 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10089 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10090 __ ldr(r0, MemOperand(r0, 0));
10091 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10092 __ ldr(r2, MemOperand(r2, 0));
10093 __ add(r0, r0, Operand(r2));
10094 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10095
10096 // Argument 5 (sp[0]): static offsets vector buffer.
10097 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10098 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10099
10100 // For arguments 4 and 3 get string length, calculate start of string data and
10101 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10102 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010103 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010104 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010105 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10106 __ eor(r3, r3, Operand(1));
10107 // Argument 4 (r3): End of string data
10108 // Argument 3 (r2): Start of string data
10109 __ add(r2, r9, Operand(r1, LSL, r3));
10110 __ add(r3, r9, Operand(r0, LSL, r3));
10111
10112 // Argument 2 (r1): Previous index.
10113 // Already there
10114
10115 // Argument 1 (r0): Subject string.
10116 __ mov(r0, subject);
10117
10118 // Locate the code entry and call it.
10119 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10120 __ CallCFunction(r7, kRegExpExecuteArguments);
10121 __ pop(lr);
10122
10123 // r0: result
10124 // subject: subject string (callee saved)
10125 // regexp_data: RegExp data (callee saved)
10126 // last_match_info_elements: Last match info elements (callee saved)
10127
10128 // Check the result.
10129 Label success;
10130 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10131 __ b(eq, &success);
10132 Label failure;
10133 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10134 __ b(eq, &failure);
10135 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10136 // If not exception it can only be retry. Handle that in the runtime system.
10137 __ b(ne, &runtime);
10138 // Result must now be exception. If there is no pending exception already a
10139 // stack overflow (on the backtrack stack) was detected in RegExp code but
10140 // haven't created the exception yet. Handle that in the runtime system.
10141 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10142 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10143 __ ldr(r0, MemOperand(r0, 0));
10144 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10145 __ ldr(r1, MemOperand(r1, 0));
10146 __ cmp(r0, r1);
10147 __ b(eq, &runtime);
10148 __ bind(&failure);
10149 // For failure and exception return null.
10150 __ mov(r0, Operand(Factory::null_value()));
10151 __ add(sp, sp, Operand(4 * kPointerSize));
10152 __ Ret();
10153
10154 // Process the result from the native regexp code.
10155 __ bind(&success);
10156 __ ldr(r1,
10157 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10158 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010159 STATIC_ASSERT(kSmiTag == 0);
10160 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010161 __ add(r1, r1, Operand(2)); // r1 was a smi.
10162
10163 // r1: number of capture registers
10164 // r4: subject string
10165 // Store the capture count.
10166 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10167 __ str(r2, FieldMemOperand(last_match_info_elements,
10168 RegExpImpl::kLastCaptureCountOffset));
10169 // Store last subject and last input.
10170 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010171 __ str(subject,
10172 FieldMemOperand(last_match_info_elements,
10173 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010174 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010175 __ str(subject,
10176 FieldMemOperand(last_match_info_elements,
10177 RegExpImpl::kLastInputOffset));
10178 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010179 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010180
10181 // Get the static offsets vector filled by the native regexp code.
10182 ExternalReference address_of_static_offsets_vector =
10183 ExternalReference::address_of_static_offsets_vector();
10184 __ mov(r2, Operand(address_of_static_offsets_vector));
10185
10186 // r1: number of capture registers
10187 // r2: offsets vector
10188 Label next_capture, done;
10189 // Capture register counter starts from number of capture registers and
10190 // counts down until wraping after zero.
10191 __ add(r0,
10192 last_match_info_elements,
10193 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10194 __ bind(&next_capture);
10195 __ sub(r1, r1, Operand(1), SetCC);
10196 __ b(mi, &done);
10197 // Read the value from the static offsets vector buffer.
10198 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10199 // Store the smi value in the last match info.
10200 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10201 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10202 __ jmp(&next_capture);
10203 __ bind(&done);
10204
10205 // Return last match info.
10206 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10207 __ add(sp, sp, Operand(4 * kPointerSize));
10208 __ Ret();
10209
10210 // Do the runtime call to execute the regexp.
10211 __ bind(&runtime);
10212 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010213#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010214}
10215
10216
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010217void CallFunctionStub::Generate(MacroAssembler* masm) {
10218 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010219
10220 // If the receiver might be a value (string, number or boolean) check for this
10221 // and box it if it is.
10222 if (ReceiverMightBeValue()) {
10223 // Get the receiver from the stack.
10224 // function, receiver [, arguments]
10225 Label receiver_is_value, receiver_is_js_object;
10226 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10227
10228 // Check if receiver is a smi (which is a number value).
10229 __ BranchOnSmi(r1, &receiver_is_value);
10230
10231 // Check if the receiver is a valid JS object.
10232 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10233 __ b(ge, &receiver_is_js_object);
10234
10235 // Call the runtime to box the value.
10236 __ bind(&receiver_is_value);
10237 __ EnterInternalFrame();
10238 __ push(r1);
10239 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10240 __ LeaveInternalFrame();
10241 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10242
10243 __ bind(&receiver_is_js_object);
10244 }
10245
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010246 // Get the function to call from the stack.
10247 // function, receiver [, arguments]
10248 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10249
10250 // Check that the function is really a JavaScript function.
10251 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010252 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010253 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010254 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010255 __ b(ne, &slow);
10256
10257 // Fast-case: Invoke the function now.
10258 // r1: pushed function
10259 ParameterCount actual(argc_);
10260 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10261
10262 // Slow-case: Non-function called.
10263 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010264 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10265 // of the original receiver from the call site).
10266 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010267 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010268 __ mov(r2, Operand(0));
10269 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10270 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10271 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010272}
10273
10274
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010275// Unfortunately you have to run without snapshots to see most of these
10276// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010277const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010278 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10279 (lhs_.is(r1) && rhs_.is(r0)));
10280
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010281 if (name_ != NULL) return name_;
10282 const int kMaxNameLength = 100;
10283 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10284 if (name_ == NULL) return "OOM";
10285
10286 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010287 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010288 case lt: cc_name = "LT"; break;
10289 case gt: cc_name = "GT"; break;
10290 case le: cc_name = "LE"; break;
10291 case ge: cc_name = "GE"; break;
10292 case eq: cc_name = "EQ"; break;
10293 case ne: cc_name = "NE"; break;
10294 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010295 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010296
ager@chromium.orgb5737492010-07-15 09:29:43 +000010297 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10298 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10299
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010300 const char* strict_name = "";
10301 if (strict_ && (cc_ == eq || cc_ == ne)) {
10302 strict_name = "_STRICT";
10303 }
10304
10305 const char* never_nan_nan_name = "";
10306 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10307 never_nan_nan_name = "_NO_NAN";
10308 }
10309
10310 const char* include_number_compare_name = "";
10311 if (!include_number_compare_) {
10312 include_number_compare_name = "_NO_NUMBER";
10313 }
10314
10315 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010316 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010317 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010318 lhs_name,
10319 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010320 strict_name,
10321 never_nan_nan_name,
10322 include_number_compare_name);
10323 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010324}
10325
10326
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010327int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010328 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10329 // stubs the never NaN NaN condition is only taken into account if the
10330 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010331 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10332 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10333 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010334 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010335 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010336 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010337 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10338 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010339}
10340
10341
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010342// StringCharCodeAtGenerator
10343
10344void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10345 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010346 Label ascii_string;
10347 Label got_char_code;
10348
10349 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010350 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010351
10352 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010353 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10354 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010355 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010356 __ tst(result_, Operand(kIsNotStringMask));
10357 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010358
10359 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010360 __ BranchOnNotSmi(index_, &index_not_smi_);
10361
10362 // Put smi-tagged index into scratch register.
10363 __ mov(scratch_, index_);
10364 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010365
10366 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010367 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10368 __ cmp(ip, Operand(scratch_));
10369 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010370
10371 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010372 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010373 __ tst(result_, Operand(kStringRepresentationMask));
10374 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010375
10376 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010377 __ tst(result_, Operand(kIsConsStringMask));
10378 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010379
10380 // ConsString.
10381 // Check whether the right hand side is the empty string (i.e. if
10382 // this is really a flat string in a cons string). If that is not
10383 // the case we would rather go to the runtime system now to flatten
10384 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010385 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10386 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10387 __ cmp(result_, Operand(ip));
10388 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010389 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010390 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10391 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10392 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10393 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010394 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010395 __ tst(result_, Operand(kStringRepresentationMask));
10396 __ b(nz, &call_runtime_);
10397
10398 // Check for 1-byte or 2-byte string.
10399 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010400 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010401 __ tst(result_, Operand(kStringEncodingMask));
10402 __ b(nz, &ascii_string);
10403
10404 // 2-byte string.
10405 // Load the 2-byte character code into the result register. We can
10406 // add without shifting since the smi tag size is the log2 of the
10407 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010408 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010409 __ add(scratch_, object_, Operand(scratch_));
10410 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10411 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010412
10413 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010414 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010415 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010416 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10417 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010418
10419 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010420 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10421 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010422}
10423
10424
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010425void StringCharCodeAtGenerator::GenerateSlow(
10426 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10427 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010428
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010429 // Index is not a smi.
10430 __ bind(&index_not_smi_);
10431 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010432 __ CheckMap(index_,
10433 scratch_,
10434 Heap::kHeapNumberMapRootIndex,
10435 index_not_number_,
10436 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010437 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010438 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010439 __ push(index_); // Consumed by runtime conversion function.
10440 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10441 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10442 } else {
10443 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10444 // NumberToSmi discards numbers that are not exact integers.
10445 __ CallRuntime(Runtime::kNumberToSmi, 1);
10446 }
10447 if (!scratch_.is(r0)) {
10448 // Save the conversion result before the pop instructions below
10449 // have a chance to overwrite it.
10450 __ mov(scratch_, r0);
10451 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010452 __ pop(index_);
10453 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010454 // Reload the instance type.
10455 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10456 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010457 call_helper.AfterCall(masm);
10458 // If index is still not a smi, it must be out of range.
10459 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10460 // Otherwise, return to the fast path.
10461 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010462
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010463 // Call runtime. We get here when the receiver is a string and the
10464 // index is a number, but the code of getting the actual character
10465 // is too complex (e.g., when the string needs to be flattened).
10466 __ bind(&call_runtime_);
10467 call_helper.BeforeCall(masm);
10468 __ Push(object_, index_);
10469 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
10470 if (!result_.is(r0)) {
10471 __ mov(result_, r0);
10472 }
10473 call_helper.AfterCall(masm);
10474 __ jmp(&exit_);
10475
10476 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10477}
10478
10479
10480// -------------------------------------------------------------------------
10481// StringCharFromCodeGenerator
10482
10483void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010484 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010485 STATIC_ASSERT(kSmiTag == 0);
10486 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010487 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010488 __ tst(code_,
10489 Operand(kSmiTagMask |
10490 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10491 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010492
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010493 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10494 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010495 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010496 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10497 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10498 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10499 __ cmp(result_, Operand(ip));
10500 __ b(eq, &slow_case_);
10501 __ bind(&exit_);
10502}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010503
ager@chromium.orgac091b72010-05-05 07:34:42 +000010504
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010505void StringCharFromCodeGenerator::GenerateSlow(
10506 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10507 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10508
10509 __ bind(&slow_case_);
10510 call_helper.BeforeCall(masm);
10511 __ push(code_);
10512 __ CallRuntime(Runtime::kCharFromCode, 1);
10513 if (!result_.is(r0)) {
10514 __ mov(result_, r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010515 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010516 call_helper.AfterCall(masm);
10517 __ jmp(&exit_);
10518
10519 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10520}
10521
10522
10523// -------------------------------------------------------------------------
10524// StringCharAtGenerator
10525
10526void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10527 char_code_at_generator_.GenerateFast(masm);
10528 char_from_code_generator_.GenerateFast(masm);
10529}
10530
10531
10532void StringCharAtGenerator::GenerateSlow(
10533 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10534 char_code_at_generator_.GenerateSlow(masm, call_helper);
10535 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010536}
10537
10538
10539void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10540 Register dest,
10541 Register src,
10542 Register count,
10543 Register scratch,
10544 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010545 Label loop;
10546 Label done;
10547 // This loop just copies one character at a time, as it is only used for very
10548 // short strings.
10549 if (!ascii) {
10550 __ add(count, count, Operand(count), SetCC);
10551 } else {
10552 __ cmp(count, Operand(0));
10553 }
10554 __ b(eq, &done);
10555
10556 __ bind(&loop);
10557 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10558 // Perform sub between load and dependent store to get the load time to
10559 // complete.
10560 __ sub(count, count, Operand(1), SetCC);
10561 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10562 // last iteration.
10563 __ b(gt, &loop);
10564
10565 __ bind(&done);
10566}
10567
10568
10569enum CopyCharactersFlags {
10570 COPY_ASCII = 1,
10571 DEST_ALWAYS_ALIGNED = 2
10572};
10573
10574
ager@chromium.orgac091b72010-05-05 07:34:42 +000010575void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10576 Register dest,
10577 Register src,
10578 Register count,
10579 Register scratch1,
10580 Register scratch2,
10581 Register scratch3,
10582 Register scratch4,
10583 Register scratch5,
10584 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010585 bool ascii = (flags & COPY_ASCII) != 0;
10586 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10587
10588 if (dest_always_aligned && FLAG_debug_code) {
10589 // Check that destination is actually word aligned if the flag says
10590 // that it is.
10591 __ tst(dest, Operand(kPointerAlignmentMask));
10592 __ Check(eq, "Destination of copy not aligned.");
10593 }
10594
10595 const int kReadAlignment = 4;
10596 const int kReadAlignmentMask = kReadAlignment - 1;
10597 // Ensure that reading an entire aligned word containing the last character
10598 // of a string will not read outside the allocated area (because we pad up
10599 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010600 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010601 // Assumes word reads and writes are little endian.
10602 // Nothing to do for zero characters.
10603 Label done;
10604 if (!ascii) {
10605 __ add(count, count, Operand(count), SetCC);
10606 } else {
10607 __ cmp(count, Operand(0));
10608 }
10609 __ b(eq, &done);
10610
10611 // Assume that you cannot read (or write) unaligned.
10612 Label byte_loop;
10613 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10614 __ cmp(count, Operand(8));
10615 __ add(count, dest, Operand(count));
10616 Register limit = count; // Read until src equals this.
10617 __ b(lt, &byte_loop);
10618
10619 if (!dest_always_aligned) {
10620 // Align dest by byte copying. Copies between zero and three bytes.
10621 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10622 Label dest_aligned;
10623 __ b(eq, &dest_aligned);
10624 __ cmp(scratch4, Operand(2));
10625 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10626 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10627 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10628 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10629 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10630 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10631 __ bind(&dest_aligned);
10632 }
10633
10634 Label simple_loop;
10635
10636 __ sub(scratch4, dest, Operand(src));
10637 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10638 __ b(eq, &simple_loop);
10639 // Shift register is number of bits in a source word that
10640 // must be combined with bits in the next source word in order
10641 // to create a destination word.
10642
10643 // Complex loop for src/dst that are not aligned the same way.
10644 {
10645 Label loop;
10646 __ mov(scratch4, Operand(scratch4, LSL, 3));
10647 Register left_shift = scratch4;
10648 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10649 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10650 // Store the "shift" most significant bits of scratch in the least
10651 // signficant bits (i.e., shift down by (32-shift)).
10652 __ rsb(scratch2, left_shift, Operand(32));
10653 Register right_shift = scratch2;
10654 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10655
10656 __ bind(&loop);
10657 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10658 __ sub(scratch5, limit, Operand(dest));
10659 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10660 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10661 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10662 // Loop if four or more bytes left to copy.
10663 // Compare to eight, because we did the subtract before increasing dst.
10664 __ sub(scratch5, scratch5, Operand(8), SetCC);
10665 __ b(ge, &loop);
10666 }
10667 // There is now between zero and three bytes left to copy (negative that
10668 // number is in scratch5), and between one and three bytes already read into
10669 // scratch1 (eight times that number in scratch4). We may have read past
10670 // the end of the string, but because objects are aligned, we have not read
10671 // past the end of the object.
10672 // Find the minimum of remaining characters to move and preloaded characters
10673 // and write those as bytes.
10674 __ add(scratch5, scratch5, Operand(4), SetCC);
10675 __ b(eq, &done);
10676 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
10677 // Move minimum of bytes read and bytes left to copy to scratch4.
10678 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
10679 // Between one and three (value in scratch5) characters already read into
10680 // scratch ready to write.
10681 __ cmp(scratch5, Operand(2));
10682 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10683 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
10684 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
10685 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
10686 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
10687 // Copy any remaining bytes.
10688 __ b(&byte_loop);
10689
10690 // Simple loop.
10691 // Copy words from src to dst, until less than four bytes left.
10692 // Both src and dest are word aligned.
10693 __ bind(&simple_loop);
10694 {
10695 Label loop;
10696 __ bind(&loop);
10697 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10698 __ sub(scratch3, limit, Operand(dest));
10699 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10700 // Compare to 8, not 4, because we do the substraction before increasing
10701 // dest.
10702 __ cmp(scratch3, Operand(8));
10703 __ b(ge, &loop);
10704 }
10705
10706 // Copy bytes from src to dst until dst hits limit.
10707 __ bind(&byte_loop);
10708 __ cmp(dest, Operand(limit));
10709 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10710 __ b(ge, &done);
10711 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10712 __ b(&byte_loop);
10713
10714 __ bind(&done);
10715}
10716
10717
ager@chromium.orgac091b72010-05-05 07:34:42 +000010718void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10719 Register c1,
10720 Register c2,
10721 Register scratch1,
10722 Register scratch2,
10723 Register scratch3,
10724 Register scratch4,
10725 Register scratch5,
10726 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010727 // Register scratch3 is the general scratch register in this function.
10728 Register scratch = scratch3;
10729
10730 // Make sure that both characters are not digits as such strings has a
10731 // different hash algorithm. Don't try to look for these in the symbol table.
10732 Label not_array_index;
10733 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10734 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10735 __ b(hi, &not_array_index);
10736 __ sub(scratch, c2, Operand(static_cast<int>('0')));
10737 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10738
10739 // If check failed combine both characters into single halfword.
10740 // This is required by the contract of the method: code at the
10741 // not_found branch expects this combination in c1 register
10742 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
10743 __ b(ls, not_found);
10744
10745 __ bind(&not_array_index);
10746 // Calculate the two character string hash.
10747 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010748 StringHelper::GenerateHashInit(masm, hash, c1);
10749 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
10750 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010751
10752 // Collect the two characters in a register.
10753 Register chars = c1;
10754 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
10755
10756 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10757 // hash: hash of two character string.
10758
10759 // Load symbol table
10760 // Load address of first element of the symbol table.
10761 Register symbol_table = c2;
10762 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
10763
10764 // Load undefined value
10765 Register undefined = scratch4;
10766 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
10767
10768 // Calculate capacity mask from the symbol table capacity.
10769 Register mask = scratch2;
10770 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
10771 __ mov(mask, Operand(mask, ASR, 1));
10772 __ sub(mask, mask, Operand(1));
10773
10774 // Calculate untagged address of the first element of the symbol table.
10775 Register first_symbol_table_element = symbol_table;
10776 __ add(first_symbol_table_element, symbol_table,
10777 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
10778
10779 // Registers
10780 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10781 // hash: hash of two character string
10782 // mask: capacity mask
10783 // first_symbol_table_element: address of the first element of
10784 // the symbol table
10785 // scratch: -
10786
10787 // Perform a number of probes in the symbol table.
10788 static const int kProbes = 4;
10789 Label found_in_symbol_table;
10790 Label next_probe[kProbes];
10791 for (int i = 0; i < kProbes; i++) {
10792 Register candidate = scratch5; // Scratch register contains candidate.
10793
10794 // Calculate entry in symbol table.
10795 if (i > 0) {
10796 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
10797 } else {
10798 __ mov(candidate, hash);
10799 }
10800
10801 __ and_(candidate, candidate, Operand(mask));
10802
10803 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010804 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010805 __ ldr(candidate,
10806 MemOperand(first_symbol_table_element,
10807 candidate,
10808 LSL,
10809 kPointerSizeLog2));
10810
10811 // If entry is undefined no string with this hash can be found.
10812 __ cmp(candidate, undefined);
10813 __ b(eq, not_found);
10814
10815 // If length is not 2 the string is not a candidate.
10816 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010817 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010818 __ b(ne, &next_probe[i]);
10819
10820 // Check that the candidate is a non-external ascii string.
10821 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
10822 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
10823 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
10824 &next_probe[i]);
10825
10826 // Check if the two characters match.
10827 // Assumes that word load is little endian.
10828 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
10829 __ cmp(chars, scratch);
10830 __ b(eq, &found_in_symbol_table);
10831 __ bind(&next_probe[i]);
10832 }
10833
10834 // No matching 2 character string found by probing.
10835 __ jmp(not_found);
10836
10837 // Scratch register contains result when we fall through to here.
10838 Register result = scratch;
10839 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000010840 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010841}
10842
10843
ager@chromium.orgac091b72010-05-05 07:34:42 +000010844void StringHelper::GenerateHashInit(MacroAssembler* masm,
10845 Register hash,
10846 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010847 // hash = character + (character << 10);
10848 __ add(hash, character, Operand(character, LSL, 10));
10849 // hash ^= hash >> 6;
10850 __ eor(hash, hash, Operand(hash, ASR, 6));
10851}
10852
10853
ager@chromium.orgac091b72010-05-05 07:34:42 +000010854void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
10855 Register hash,
10856 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010857 // hash += character;
10858 __ add(hash, hash, Operand(character));
10859 // hash += hash << 10;
10860 __ add(hash, hash, Operand(hash, LSL, 10));
10861 // hash ^= hash >> 6;
10862 __ eor(hash, hash, Operand(hash, ASR, 6));
10863}
10864
10865
ager@chromium.orgac091b72010-05-05 07:34:42 +000010866void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
10867 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010868 // hash += hash << 3;
10869 __ add(hash, hash, Operand(hash, LSL, 3));
10870 // hash ^= hash >> 11;
10871 __ eor(hash, hash, Operand(hash, ASR, 11));
10872 // hash += hash << 15;
10873 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
10874
10875 // if (hash == 0) hash = 27;
10876 __ mov(hash, Operand(27), LeaveCC, nz);
10877}
10878
10879
ager@chromium.org5c838252010-02-19 08:53:10 +000010880void SubStringStub::Generate(MacroAssembler* masm) {
10881 Label runtime;
10882
10883 // Stack frame on entry.
10884 // lr: return address
10885 // sp[0]: to
10886 // sp[4]: from
10887 // sp[8]: string
10888
10889 // This stub is called from the native-call %_SubString(...), so
10890 // nothing can be assumed about the arguments. It is tested that:
10891 // "string" is a sequential string,
10892 // both "from" and "to" are smis, and
10893 // 0 <= from <= to <= string.length.
10894 // If any of these assumptions fail, we call the runtime system.
10895
10896 static const int kToOffset = 0 * kPointerSize;
10897 static const int kFromOffset = 1 * kPointerSize;
10898 static const int kStringOffset = 2 * kPointerSize;
10899
10900
10901 // Check bounds and smi-ness.
10902 __ ldr(r7, MemOperand(sp, kToOffset));
10903 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010904 STATIC_ASSERT(kSmiTag == 0);
10905 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010906 // I.e., arithmetic shift right by one un-smi-tags.
10907 __ mov(r2, Operand(r7, ASR, 1), SetCC);
10908 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
10909 // If either r2 or r6 had the smi tag bit set, then carry is set now.
10910 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
10911 __ b(mi, &runtime); // From is negative.
10912
10913 __ sub(r2, r2, Operand(r3), SetCC);
10914 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010915 // Special handling of sub-strings of length 1 and 2. One character strings
10916 // are handled in the runtime system (looked up in the single character
10917 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000010918 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010919 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000010920
10921 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010922 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010923 // r6: from (smi)
10924 // r7: to (smi)
10925
10926 // Make sure first argument is a sequential (or flat) string.
10927 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010928 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010929 __ tst(r5, Operand(kSmiTagMask));
10930 __ b(eq, &runtime);
10931 Condition is_string = masm->IsObjectStringType(r5, r1);
10932 __ b(NegateCondition(is_string), &runtime);
10933
10934 // r1: instance type
10935 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010936 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010937 // r5: string
10938 // r6: from (smi)
10939 // r7: to (smi)
10940 Label seq_string;
10941 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010942 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
10943 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000010944 __ cmp(r4, Operand(kConsStringTag));
10945 __ b(gt, &runtime); // External strings go to runtime.
10946 __ b(lt, &seq_string); // Sequential strings are handled directly.
10947
10948 // Cons string. Try to recurse (once) on the first substring.
10949 // (This adds a little more generality than necessary to handle flattened
10950 // cons strings, but not much).
10951 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
10952 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
10953 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10954 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010955 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010956 __ b(ne, &runtime); // Cons and External strings go to runtime.
10957
10958 // Definitly a sequential string.
10959 __ bind(&seq_string);
10960
10961 // r1: instance type.
10962 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010963 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010964 // r5: string
10965 // r6: from (smi)
10966 // r7: to (smi)
10967 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010968 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000010969 __ b(lt, &runtime); // Fail if to > length.
10970
10971 // r1: instance type.
10972 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010973 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010974 // r5: string.
10975 // r6: from offset (smi)
10976 // Check for flat ascii string.
10977 Label non_ascii_flat;
10978 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010979 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010980 __ b(eq, &non_ascii_flat);
10981
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010982 Label result_longer_than_two;
10983 __ cmp(r2, Operand(2));
10984 __ b(gt, &result_longer_than_two);
10985
10986 // Sub string of length 2 requested.
10987 // Get the two characters forming the sub string.
10988 __ add(r5, r5, Operand(r3));
10989 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
10990 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
10991
10992 // Try to lookup two character string in symbol table.
10993 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010994 StringHelper::GenerateTwoCharacterSymbolTableProbe(
10995 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010996 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10997 __ add(sp, sp, Operand(3 * kPointerSize));
10998 __ Ret();
10999
11000 // r2: result string length.
11001 // r3: two characters combined into halfword in little endian byte order.
11002 __ bind(&make_two_character_string);
11003 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
11004 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11005 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11006 __ add(sp, sp, Operand(3 * kPointerSize));
11007 __ Ret();
11008
11009 __ bind(&result_longer_than_two);
11010
ager@chromium.org5c838252010-02-19 08:53:10 +000011011 // Allocate the result.
11012 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
11013
11014 // r0: result string.
11015 // r2: result string length.
11016 // r5: string.
11017 // r6: from offset (smi)
11018 // Locate first character of result.
11019 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11020 // Locate 'from' character of string.
11021 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11022 __ add(r5, r5, Operand(r6, ASR, 1));
11023
11024 // r0: result string.
11025 // r1: first character of result string.
11026 // r2: result string length.
11027 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011028 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011029 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11030 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011031 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11032 __ add(sp, sp, Operand(3 * kPointerSize));
11033 __ Ret();
11034
11035 __ bind(&non_ascii_flat);
11036 // r2: result string length.
11037 // r5: string.
11038 // r6: from offset (smi)
11039 // Check for flat two byte string.
11040
11041 // Allocate the result.
11042 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
11043
11044 // r0: result string.
11045 // r2: result string length.
11046 // r5: string.
11047 // Locate first character of result.
11048 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11049 // Locate 'from' character of string.
11050 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11051 // As "from" is a smi it is 2 times the value which matches the size of a two
11052 // byte character.
11053 __ add(r5, r5, Operand(r6));
11054
11055 // r0: result string.
11056 // r1: first character of result.
11057 // r2: result length.
11058 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011059 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011060 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11061 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011062 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11063 __ add(sp, sp, Operand(3 * kPointerSize));
11064 __ Ret();
11065
11066 // Just jump to runtime to create the sub string.
11067 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011068 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011069}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011070
11071
11072void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11073 Register left,
11074 Register right,
11075 Register scratch1,
11076 Register scratch2,
11077 Register scratch3,
11078 Register scratch4) {
11079 Label compare_lengths;
11080 // Find minimum length and length difference.
11081 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11082 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11083 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11084 Register length_delta = scratch3;
11085 __ mov(scratch1, scratch2, LeaveCC, gt);
11086 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011087 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011088 __ tst(min_length, Operand(min_length));
11089 __ b(eq, &compare_lengths);
11090
ager@chromium.orgac091b72010-05-05 07:34:42 +000011091 // Untag smi.
11092 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11093
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011094 // Setup registers so that we only need to increment one register
11095 // in the loop.
11096 __ add(scratch2, min_length,
11097 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11098 __ add(left, left, Operand(scratch2));
11099 __ add(right, right, Operand(scratch2));
11100 // Registers left and right points to the min_length character of strings.
11101 __ rsb(min_length, min_length, Operand(-1));
11102 Register index = min_length;
11103 // Index starts at -min_length.
11104
11105 {
11106 // Compare loop.
11107 Label loop;
11108 __ bind(&loop);
11109 // Compare characters.
11110 __ add(index, index, Operand(1), SetCC);
11111 __ ldrb(scratch2, MemOperand(left, index), ne);
11112 __ ldrb(scratch4, MemOperand(right, index), ne);
11113 // Skip to compare lengths with eq condition true.
11114 __ b(eq, &compare_lengths);
11115 __ cmp(scratch2, scratch4);
11116 __ b(eq, &loop);
11117 // Fallthrough with eq condition false.
11118 }
11119 // Compare lengths - strings up to min-length are equal.
11120 __ bind(&compare_lengths);
11121 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11122 // Use zero length_delta as result.
11123 __ mov(r0, Operand(length_delta), SetCC, eq);
11124 // Fall through to here if characters compare not-equal.
11125 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11126 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11127 __ Ret();
11128}
11129
11130
11131void StringCompareStub::Generate(MacroAssembler* masm) {
11132 Label runtime;
11133
11134 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011135 // sp[0]: right string
11136 // sp[4]: left string
11137 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11138 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011139
11140 Label not_same;
11141 __ cmp(r0, r1);
11142 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011143 STATIC_ASSERT(EQUAL == 0);
11144 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011145 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11146 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11147 __ add(sp, sp, Operand(2 * kPointerSize));
11148 __ Ret();
11149
11150 __ bind(&not_same);
11151
11152 // Check that both objects are sequential ascii strings.
11153 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11154
11155 // Compare flat ascii strings natively. Remove arguments from stack first.
11156 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11157 __ add(sp, sp, Operand(2 * kPointerSize));
11158 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11159
11160 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11161 // tagged as a small integer.
11162 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011163 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011164}
11165
11166
ager@chromium.org5c838252010-02-19 08:53:10 +000011167void StringAddStub::Generate(MacroAssembler* masm) {
11168 Label string_add_runtime;
11169 // Stack on entry:
11170 // sp[0]: second argument.
11171 // sp[4]: first argument.
11172
11173 // Load the two arguments.
11174 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11175 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11176
11177 // Make sure that both arguments are strings if not known in advance.
11178 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011179 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011180 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11181 // Load instance types.
11182 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11183 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11184 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11185 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011186 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011187 // If either is not a string, go to runtime.
11188 __ tst(r4, Operand(kIsNotStringMask));
11189 __ tst(r5, Operand(kIsNotStringMask), eq);
11190 __ b(ne, &string_add_runtime);
11191 }
11192
11193 // Both arguments are strings.
11194 // r0: first string
11195 // r1: second string
11196 // r4: first string instance type (if string_check_)
11197 // r5: second string instance type (if string_check_)
11198 {
11199 Label strings_not_empty;
11200 // Check if either of the strings are empty. In that case return the other.
11201 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11202 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011203 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011204 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011205 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011206 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011207 // Else test if second string is empty.
11208 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011209 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11210
11211 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11212 __ add(sp, sp, Operand(2 * kPointerSize));
11213 __ Ret();
11214
11215 __ bind(&strings_not_empty);
11216 }
11217
ager@chromium.orgac091b72010-05-05 07:34:42 +000011218 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11219 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011220 // Both strings are non-empty.
11221 // r0: first string
11222 // r1: second string
11223 // r2: length of first string
11224 // r3: length of second string
11225 // r4: first string instance type (if string_check_)
11226 // r5: second string instance type (if string_check_)
11227 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011228 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011229 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011230 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011231 __ add(r6, r2, Operand(r3));
11232 // Use the runtime system when adding two one character strings, as it
11233 // contains optimizations for this specific case using the symbol table.
11234 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011235 __ b(ne, &longer_than_two);
11236
11237 // Check that both strings are non-external ascii strings.
11238 if (!string_check_) {
11239 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11240 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11241 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11242 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11243 }
11244 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11245 &string_add_runtime);
11246
11247 // Get the two characters forming the sub string.
11248 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11249 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11250
11251 // Try to lookup two character string in symbol table. If it is not found
11252 // just allocate a new one.
11253 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011254 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11255 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011256 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11257 __ add(sp, sp, Operand(2 * kPointerSize));
11258 __ Ret();
11259
11260 __ bind(&make_two_character_string);
11261 // Resulting string has length 2 and first chars of two strings
11262 // are combined into single halfword in r2 register.
11263 // So we can fill resulting string without two loops by a single
11264 // halfword store instruction (which assumes that processor is
11265 // in a little endian mode)
11266 __ mov(r6, Operand(2));
11267 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11268 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11269 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11270 __ add(sp, sp, Operand(2 * kPointerSize));
11271 __ Ret();
11272
11273 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011274 // Check if resulting string will be flat.
11275 __ cmp(r6, Operand(String::kMinNonFlatLength));
11276 __ b(lt, &string_add_flat_result);
11277 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011278 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011279 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11280 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11281 __ cmp(r6, Operand(String::kMaxLength + 1));
11282 __ b(hs, &string_add_runtime);
11283
11284 // If result is not supposed to be flat, allocate a cons string object.
11285 // If both strings are ascii the result is an ascii cons string.
11286 if (!string_check_) {
11287 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11288 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11289 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11290 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11291 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011292 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011293 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011294 __ tst(r4, Operand(kStringEncodingMask));
11295 __ tst(r5, Operand(kStringEncodingMask), ne);
11296 __ b(eq, &non_ascii);
11297
11298 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011299 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011300 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11301 __ bind(&allocated);
11302 // Fill the fields of the cons string.
11303 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11304 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11305 __ mov(r0, Operand(r7));
11306 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11307 __ add(sp, sp, Operand(2 * kPointerSize));
11308 __ Ret();
11309
11310 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011311 // At least one of the strings is two-byte. Check whether it happens
11312 // to contain only ascii characters.
11313 // r4: first instance type.
11314 // r5: second instance type.
11315 __ tst(r4, Operand(kAsciiDataHintMask));
11316 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11317 __ b(ne, &ascii_data);
11318 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011319 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011320 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11321 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11322 __ b(eq, &ascii_data);
11323
ager@chromium.org5c838252010-02-19 08:53:10 +000011324 // Allocate a two byte cons string.
11325 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11326 __ jmp(&allocated);
11327
11328 // Handle creating a flat result. First check that both strings are
11329 // sequential and that they have the same encoding.
11330 // r0: first string
11331 // r1: second string
11332 // r2: length of first string
11333 // r3: length of second string
11334 // r4: first string instance type (if string_check_)
11335 // r5: second string instance type (if string_check_)
11336 // r6: sum of lengths.
11337 __ bind(&string_add_flat_result);
11338 if (!string_check_) {
11339 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11340 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11341 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11342 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11343 }
11344 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011345 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011346 __ tst(r4, Operand(kStringRepresentationMask));
11347 __ tst(r5, Operand(kStringRepresentationMask), eq);
11348 __ b(ne, &string_add_runtime);
11349 // Now check if both strings have the same encoding (ASCII/Two-byte).
11350 // r0: first string.
11351 // r1: second string.
11352 // r2: length of first string.
11353 // r3: length of second string.
11354 // r6: sum of lengths..
11355 Label non_ascii_string_add_flat_result;
11356 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11357 __ eor(r7, r4, Operand(r5));
11358 __ tst(r7, Operand(kStringEncodingMask));
11359 __ b(ne, &string_add_runtime);
11360 // And see if it's ASCII or two-byte.
11361 __ tst(r4, Operand(kStringEncodingMask));
11362 __ b(eq, &non_ascii_string_add_flat_result);
11363
11364 // Both strings are sequential ASCII strings. We also know that they are
11365 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011366 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011367 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11368 // Locate first character of result.
11369 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11370 // Locate first character of first argument.
11371 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11372 // r0: first character of first string.
11373 // r1: second string.
11374 // r2: length of first string.
11375 // r3: length of second string.
11376 // r6: first character of result.
11377 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011378 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011379
11380 // Load second argument and locate first character.
11381 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11382 // r1: first character of second string.
11383 // r3: length of second string.
11384 // r6: next character of result.
11385 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011386 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011387 __ mov(r0, Operand(r7));
11388 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11389 __ add(sp, sp, Operand(2 * kPointerSize));
11390 __ Ret();
11391
11392 __ bind(&non_ascii_string_add_flat_result);
11393 // Both strings are sequential two byte strings.
11394 // r0: first string.
11395 // r1: second string.
11396 // r2: length of first string.
11397 // r3: length of second string.
11398 // r6: sum of length of strings.
11399 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11400 // r0: first string.
11401 // r1: second string.
11402 // r2: length of first string.
11403 // r3: length of second string.
11404 // r7: result string.
11405
11406 // Locate first character of result.
11407 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11408 // Locate first character of first argument.
11409 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11410
11411 // r0: first character of first string.
11412 // r1: second string.
11413 // r2: length of first string.
11414 // r3: length of second string.
11415 // r6: first character of result.
11416 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011417 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011418
11419 // Locate first character of second argument.
11420 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11421
11422 // r1: first character of second string.
11423 // r3: length of second string.
11424 // r6: next character of result (after copy of first string).
11425 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011426 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011427
11428 __ mov(r0, Operand(r7));
11429 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11430 __ add(sp, sp, Operand(2 * kPointerSize));
11431 __ Ret();
11432
11433 // Just jump to runtime to add the two strings.
11434 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011435 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011436}
11437
11438
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011439#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011441} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011442
11443#endif // V8_TARGET_ARCH_ARM