blob: f06b79bd0ccd6b05f53ca66e241d3e2f7294dae5 [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
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003283 JumpTarget done;
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.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003289 done.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
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003304 // This call to bind will get us back to the virtual frame we had before
3305 // where things are not spilled and the literal register is not on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003306 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 // Push the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003308 frame_->EmitPush(literal);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003309 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003310}
3311
3312
ager@chromium.org7c537e22008-10-16 08:43:32 +00003313void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003314#ifdef DEBUG
3315 int original_height = frame_->height();
3316#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003317 Comment cmnt(masm_, "[ ObjectLiteral");
3318
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003319 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003321 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003322 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003323 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3324 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003325 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003326 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003327 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003328 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003329 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003330 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003331 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003332 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003333 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003334 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003335 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003336 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003337 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003338 // At the start of each iteration, the top of stack contains
3339 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340 ObjectLiteral::Property* property = node->properties()->at(i);
3341 Literal* key = property->key();
3342 Expression* value = property->value();
3343 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003344 case ObjectLiteral::Property::CONSTANT:
3345 break;
3346 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3347 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3348 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003349 case ObjectLiteral::Property::COMPUTED:
3350 if (key->handle()->IsSymbol()) {
3351 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003352 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003353 frame_->PopToR0();
3354 // Fetch the object literal.
3355 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003356 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003357 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3358 break;
3359 }
3360 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003362 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003363 Load(key);
3364 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003365 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 break;
3367 }
3368 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003369 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003370 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003371 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003372 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003373 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003374 break;
3375 }
3376 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003377 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003378 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003379 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003380 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003381 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382 break;
3383 }
3384 }
3385 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003386 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003387}
3388
3389
ager@chromium.org7c537e22008-10-16 08:43:32 +00003390void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003391#ifdef DEBUG
3392 int original_height = frame_->height();
3393#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003395
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003396 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003397 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003398 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003399 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003400 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3401 frame_->EmitPush(tos);
3402 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3403 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003404 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003405 if (node->depth() > 1) {
3406 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003407 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003408 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003409 } else {
3410 FastCloneShallowArrayStub stub(length);
3411 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003412 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003413 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003414 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003415
3416 // Generate code to set the elements in the array that are not
3417 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418 for (int i = 0; i < node->values()->length(); i++) {
3419 Expression* value = node->values()->at(i);
3420
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003421 // If value is a literal the property value is already set in the
3422 // boilerplate object.
3423 if (value->AsLiteral() != NULL) continue;
3424 // If value is a materialized literal the property value is already set
3425 // in the boilerplate object if it is simple.
3426 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003428 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003429 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003430 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003431 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003432 frame_->SpillAllButCopyTOSToR1();
3433
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003434 // Get the elements array.
3435 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003437 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003438 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003439 __ str(r0, FieldMemOperand(r1, offset));
3440
3441 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003442 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003444 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445}
3446
3447
ager@chromium.org32912102009-01-16 10:38:43 +00003448void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003449#ifdef DEBUG
3450 int original_height = frame_->height();
3451#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003452 // Call runtime routine to allocate the catch extension object and
3453 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003454 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003455 Load(node->key());
3456 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003457 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3458 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003459 ASSERT_EQ(original_height + 1, frame_->height());
3460}
3461
3462
3463void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3464#ifdef DEBUG
3465 int original_height = frame_->height();
3466#endif
3467 Comment cmnt(masm(), "[ Variable Assignment");
3468 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3469 ASSERT(var != NULL);
3470 Slot* slot = var->slot();
3471 ASSERT(slot != NULL);
3472
3473 // Evaluate the right-hand side.
3474 if (node->is_compound()) {
3475 // For a compound assignment the right-hand side is a binary operation
3476 // between the current property value and the actual right-hand side.
3477 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3478
3479 // Perform the binary operation.
3480 Literal* literal = node->value()->AsLiteral();
3481 bool overwrite_value =
3482 (node->value()->AsBinaryOperation() != NULL &&
3483 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3484 if (literal != NULL && literal->handle()->IsSmi()) {
3485 SmiOperation(node->binary_op(),
3486 literal->handle(),
3487 false,
3488 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3489 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003490 GenerateInlineSmi inline_smi =
3491 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3492 if (literal != NULL) {
3493 ASSERT(!literal->handle()->IsSmi());
3494 inline_smi = DONT_GENERATE_INLINE_SMI;
3495 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003496 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003497 GenericBinaryOperation(node->binary_op(),
3498 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3499 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003500 }
3501 } else {
3502 Load(node->value());
3503 }
3504
3505 // Perform the assignment.
3506 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3507 CodeForSourcePosition(node->position());
3508 StoreToSlot(slot,
3509 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3510 }
3511 ASSERT_EQ(original_height + 1, frame_->height());
3512}
3513
3514
3515void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3516#ifdef DEBUG
3517 int original_height = frame_->height();
3518#endif
3519 Comment cmnt(masm(), "[ Named Property Assignment");
3520 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3521 Property* prop = node->target()->AsProperty();
3522 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3523
3524 // Initialize name and evaluate the receiver sub-expression if necessary. If
3525 // the receiver is trivial it is not placed on the stack at this point, but
3526 // loaded whenever actually needed.
3527 Handle<String> name;
3528 bool is_trivial_receiver = false;
3529 if (var != NULL) {
3530 name = var->name();
3531 } else {
3532 Literal* lit = prop->key()->AsLiteral();
3533 ASSERT_NOT_NULL(lit);
3534 name = Handle<String>::cast(lit->handle());
3535 // Do not materialize the receiver on the frame if it is trivial.
3536 is_trivial_receiver = prop->obj()->IsTrivial();
3537 if (!is_trivial_receiver) Load(prop->obj());
3538 }
3539
3540 // Change to slow case in the beginning of an initialization block to
3541 // avoid the quadratic behavior of repeatedly adding fast properties.
3542 if (node->starts_initialization_block()) {
3543 // Initialization block consists of assignments of the form expr.x = ..., so
3544 // this will never be an assignment to a variable, so there must be a
3545 // receiver object.
3546 ASSERT_EQ(NULL, var);
3547 if (is_trivial_receiver) {
3548 Load(prop->obj());
3549 } else {
3550 frame_->Dup();
3551 }
3552 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3553 }
3554
3555 // Change to fast case at the end of an initialization block. To prepare for
3556 // that add an extra copy of the receiver to the frame, so that it can be
3557 // converted back to fast case after the assignment.
3558 if (node->ends_initialization_block() && !is_trivial_receiver) {
3559 frame_->Dup();
3560 }
3561
3562 // Stack layout:
3563 // [tos] : receiver (only materialized if non-trivial)
3564 // [tos+1] : receiver if at the end of an initialization block
3565
3566 // Evaluate the right-hand side.
3567 if (node->is_compound()) {
3568 // For a compound assignment the right-hand side is a binary operation
3569 // between the current property value and the actual right-hand side.
3570 if (is_trivial_receiver) {
3571 Load(prop->obj());
3572 } else if (var != NULL) {
3573 LoadGlobal();
3574 } else {
3575 frame_->Dup();
3576 }
3577 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003578
3579 // Perform the binary operation.
3580 Literal* literal = node->value()->AsLiteral();
3581 bool overwrite_value =
3582 (node->value()->AsBinaryOperation() != NULL &&
3583 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3584 if (literal != NULL && literal->handle()->IsSmi()) {
3585 SmiOperation(node->binary_op(),
3586 literal->handle(),
3587 false,
3588 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3589 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003590 GenerateInlineSmi inline_smi =
3591 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3592 if (literal != NULL) {
3593 ASSERT(!literal->handle()->IsSmi());
3594 inline_smi = DONT_GENERATE_INLINE_SMI;
3595 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003596 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003597 GenericBinaryOperation(node->binary_op(),
3598 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3599 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003600 }
3601 } else {
3602 // For non-compound assignment just load the right-hand side.
3603 Load(node->value());
3604 }
3605
3606 // Stack layout:
3607 // [tos] : value
3608 // [tos+1] : receiver (only materialized if non-trivial)
3609 // [tos+2] : receiver if at the end of an initialization block
3610
3611 // Perform the assignment. It is safe to ignore constants here.
3612 ASSERT(var == NULL || var->mode() != Variable::CONST);
3613 ASSERT_NE(Token::INIT_CONST, node->op());
3614 if (is_trivial_receiver) {
3615 // Load the receiver and swap with the value.
3616 Load(prop->obj());
3617 Register t0 = frame_->PopToRegister();
3618 Register t1 = frame_->PopToRegister(t0);
3619 frame_->EmitPush(t0);
3620 frame_->EmitPush(t1);
3621 }
3622 CodeForSourcePosition(node->position());
3623 bool is_contextual = (var != NULL);
3624 EmitNamedStore(name, is_contextual);
3625 frame_->EmitPush(r0);
3626
3627 // Change to fast case at the end of an initialization block.
3628 if (node->ends_initialization_block()) {
3629 ASSERT_EQ(NULL, var);
3630 // The argument to the runtime call is the receiver.
3631 if (is_trivial_receiver) {
3632 Load(prop->obj());
3633 } else {
3634 // A copy of the receiver is below the value of the assignment. Swap
3635 // the receiver and the value of the assignment expression.
3636 Register t0 = frame_->PopToRegister();
3637 Register t1 = frame_->PopToRegister(t0);
3638 frame_->EmitPush(t0);
3639 frame_->EmitPush(t1);
3640 }
3641 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3642 }
3643
3644 // Stack layout:
3645 // [tos] : result
3646
3647 ASSERT_EQ(original_height + 1, frame_->height());
3648}
3649
3650
3651void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3652#ifdef DEBUG
3653 int original_height = frame_->height();
3654#endif
3655 Comment cmnt(masm_, "[ Keyed Property Assignment");
3656 Property* prop = node->target()->AsProperty();
3657 ASSERT_NOT_NULL(prop);
3658
3659 // Evaluate the receiver subexpression.
3660 Load(prop->obj());
3661
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003662 WriteBarrierCharacter wb_info;
3663
ager@chromium.orgac091b72010-05-05 07:34:42 +00003664 // Change to slow case in the beginning of an initialization block to
3665 // avoid the quadratic behavior of repeatedly adding fast properties.
3666 if (node->starts_initialization_block()) {
3667 frame_->Dup();
3668 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3669 }
3670
3671 // Change to fast case at the end of an initialization block. To prepare for
3672 // that add an extra copy of the receiver to the frame, so that it can be
3673 // converted back to fast case after the assignment.
3674 if (node->ends_initialization_block()) {
3675 frame_->Dup();
3676 }
3677
3678 // Evaluate the key subexpression.
3679 Load(prop->key());
3680
3681 // Stack layout:
3682 // [tos] : key
3683 // [tos+1] : receiver
3684 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003685 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003686 // Evaluate the right-hand side.
3687 if (node->is_compound()) {
3688 // For a compound assignment the right-hand side is a binary operation
3689 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003690 // Duplicate receiver and key for loading the current property value.
3691 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003692 EmitKeyedLoad();
3693 frame_->EmitPush(r0);
3694
3695 // Perform the binary operation.
3696 Literal* literal = node->value()->AsLiteral();
3697 bool overwrite_value =
3698 (node->value()->AsBinaryOperation() != NULL &&
3699 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3700 if (literal != NULL && literal->handle()->IsSmi()) {
3701 SmiOperation(node->binary_op(),
3702 literal->handle(),
3703 false,
3704 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3705 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003706 GenerateInlineSmi inline_smi =
3707 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3708 if (literal != NULL) {
3709 ASSERT(!literal->handle()->IsSmi());
3710 inline_smi = DONT_GENERATE_INLINE_SMI;
3711 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003712 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003713 GenericBinaryOperation(node->binary_op(),
3714 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3715 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003716 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003717 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003718 } else {
3719 // For non-compound assignment just load the right-hand side.
3720 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003721 wb_info = node->value()->AsLiteral() != NULL ?
3722 NEVER_NEWSPACE :
3723 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003724 }
3725
3726 // Stack layout:
3727 // [tos] : value
3728 // [tos+1] : key
3729 // [tos+2] : receiver
3730 // [tos+3] : receiver if at the end of an initialization block
3731
3732 // Perform the assignment. It is safe to ignore constants here.
3733 ASSERT(node->op() != Token::INIT_CONST);
3734 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003735 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003736 frame_->EmitPush(r0);
3737
3738 // Stack layout:
3739 // [tos] : result
3740 // [tos+1] : receiver if at the end of an initialization block
3741
3742 // Change to fast case at the end of an initialization block.
3743 if (node->ends_initialization_block()) {
3744 // The argument to the runtime call is the extra copy of the receiver,
3745 // which is below the value of the assignment. Swap the receiver and
3746 // the value of the assignment expression.
3747 Register t0 = frame_->PopToRegister();
3748 Register t1 = frame_->PopToRegister(t0);
3749 frame_->EmitPush(t1);
3750 frame_->EmitPush(t0);
3751 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3752 }
3753
3754 // Stack layout:
3755 // [tos] : result
3756
3757 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003758}
3759
3760
ager@chromium.org7c537e22008-10-16 08:43:32 +00003761void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003762 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003763#ifdef DEBUG
3764 int original_height = frame_->height();
3765#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003767
ager@chromium.orgac091b72010-05-05 07:34:42 +00003768 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3769 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770
ager@chromium.orgac091b72010-05-05 07:34:42 +00003771 if (var != NULL && !var->is_global()) {
3772 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003773
ager@chromium.orgac091b72010-05-05 07:34:42 +00003774 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3775 (var != NULL && var->is_global())) {
3776 // Properties whose keys are property names and global variables are
3777 // treated as named property references. We do not need to consider
3778 // global 'this' because it is not a valid left-hand side.
3779 EmitNamedPropertyAssignment(node);
3780
3781 } else if (prop != NULL) {
3782 // Other properties (including rewritten parameters for a function that
3783 // uses arguments) are keyed property assignments.
3784 EmitKeyedPropertyAssignment(node);
3785
3786 } else {
3787 // Invalid left-hand side.
3788 Load(node->target());
3789 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3790 // The runtime call doesn't actually return but the code generator will
3791 // still generate code and expects a certain frame height.
3792 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003794 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795}
3796
3797
ager@chromium.org7c537e22008-10-16 08:43:32 +00003798void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003799#ifdef DEBUG
3800 int original_height = frame_->height();
3801#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 Comment cmnt(masm_, "[ Throw");
3803
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003804 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003805 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 frame_->CallRuntime(Runtime::kThrow, 1);
3807 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003808 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809}
3810
3811
ager@chromium.org7c537e22008-10-16 08:43:32 +00003812void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003813#ifdef DEBUG
3814 int original_height = frame_->height();
3815#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003817
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003819 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003821 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003822}
3823
3824
ager@chromium.org7c537e22008-10-16 08:43:32 +00003825void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003826#ifdef DEBUG
3827 int original_height = frame_->height();
3828#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003829 Comment cmnt(masm_, "[ Call");
3830
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003831 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 ZoneList<Expression*>* args = node->arguments();
3833
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003835 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003836 Variable* var = function->AsVariableProxy()->AsVariable();
3837 Property* property = function->AsProperty();
3838
3839 // ------------------------------------------------------------------------
3840 // Fast-case: Use inline caching.
3841 // ---
3842 // According to ECMA-262, section 11.2.3, page 44, the function to call
3843 // must be resolved after the arguments have been evaluated. The IC code
3844 // automatically handles this by loading the arguments before the function
3845 // is resolved in cache misses (this also holds for megamorphic calls).
3846 // ------------------------------------------------------------------------
3847
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003848 if (var != NULL && var->is_possibly_eval()) {
3849 // ----------------------------------
3850 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3851 // ----------------------------------
3852
3853 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3854 // resolve the function we need to call and the receiver of the
3855 // call. Then we call the resolved function using the given
3856 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003857
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003858 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003859 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003860
3861 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003862 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003863
3864 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003865 int arg_count = args->length();
3866 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003867 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003868 }
3869
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003870 VirtualFrame::SpilledScope spilled_scope(frame_);
3871
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003872 // If we know that eval can only be shadowed by eval-introduced
3873 // variables we attempt to load the global eval function directly
3874 // in generated code. If we succeed, there is no need to perform a
3875 // context lookup in the runtime system.
3876 JumpTarget done;
3877 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3878 ASSERT(var->slot()->type() == Slot::LOOKUP);
3879 JumpTarget slow;
3880 // Prepare the stack for the call to
3881 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3882 // function, the first argument to the eval call and the
3883 // receiver.
3884 LoadFromGlobalSlotCheckExtensions(var->slot(),
3885 NOT_INSIDE_TYPEOF,
3886 &slow);
3887 frame_->EmitPush(r0);
3888 if (arg_count > 0) {
3889 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3890 frame_->EmitPush(r1);
3891 } else {
3892 frame_->EmitPush(r2);
3893 }
3894 __ ldr(r1, frame_->Receiver());
3895 frame_->EmitPush(r1);
3896
3897 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3898
3899 done.Jump();
3900 slow.Bind();
3901 }
3902
3903 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3904 // pushing the loaded function, the first argument to the eval
3905 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003906 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3907 frame_->EmitPush(r1);
3908 if (arg_count > 0) {
3909 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3910 frame_->EmitPush(r1);
3911 } else {
3912 frame_->EmitPush(r2);
3913 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003914 __ ldr(r1, frame_->Receiver());
3915 frame_->EmitPush(r1);
3916
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003917 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003918 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003919
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003920 // If we generated fast-case code bind the jump-target where fast
3921 // and slow case merge.
3922 if (done.is_linked()) done.Bind();
3923
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003924 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003925 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003926 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3927
3928 // Call the function.
3929 CodeForSourcePosition(node->position());
3930
3931 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003932 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003933 frame_->CallStub(&call_function, arg_count + 1);
3934
3935 __ ldr(cp, frame_->Context());
3936 // Remove the function from the stack.
3937 frame_->Drop();
3938 frame_->EmitPush(r0);
3939
3940 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941 // ----------------------------------
3942 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3943 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003944 // Pass the global object as the receiver and let the IC stub
3945 // patch the stack to use the global proxy as 'this' in the
3946 // invoked function.
3947 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948
3949 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003950 int arg_count = args->length();
3951 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003952 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003953 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003955 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00003956 // Setup the name register and call the IC initialization code.
3957 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003958 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3959 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003960 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003961 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3962 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003963 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
3966 } else if (var != NULL && var->slot() != NULL &&
3967 var->slot()->type() == Slot::LOOKUP) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003968 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003970 // JavaScript examples:
3971 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003972 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003973 //
3974 // function f() {};
3975 // function g() {
3976 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003977 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003978 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979 // ----------------------------------
3980
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003981 // JumpTargets do not yet support merging frames so the frame must be
3982 // spilled when jumping to these targets.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003983 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003984
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003985 // Generate fast case for loading functions from slots that
3986 // correspond to local/global variables or arguments unless they
3987 // are shadowed by eval-introduced bindings.
3988 EmitDynamicLoadFromSlotFastCase(var->slot(),
3989 NOT_INSIDE_TYPEOF,
3990 &slow,
3991 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003992
3993 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003994 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003995 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003996 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003997 frame_->EmitPush(r0);
3998 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999 // r0: slot value; r1: receiver
4000
4001 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004002 frame_->EmitPush(r0); // function
4003 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004005 // If fast case code has been generated, emit code to push the
4006 // function and receiver and have the slow path jump around this
4007 // code.
4008 if (done.is_linked()) {
4009 JumpTarget call;
4010 call.Jump();
4011 done.Bind();
4012 frame_->EmitPush(r0); // function
4013 LoadGlobalReceiver(r1); // receiver
4014 call.Bind();
4015 }
4016
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004017 // Call the function. At this point, everything is spilled but the
4018 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004019 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004020 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021
4022 } else if (property != NULL) {
4023 // Check if the key is a literal string.
4024 Literal* literal = property->key()->AsLiteral();
4025
4026 if (literal != NULL && literal->handle()->IsSymbol()) {
4027 // ------------------------------------------------------------------
4028 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4029 // ------------------------------------------------------------------
4030
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004031 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004033 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4034 name->IsEqualTo(CStrVector("apply")) &&
4035 args->length() == 2 &&
4036 args->at(1)->AsVariableProxy() != NULL &&
4037 args->at(1)->AsVariableProxy()->IsArguments()) {
4038 // Use the optimized Function.prototype.apply that avoids
4039 // allocating lazily allocated arguments objects.
4040 CallApplyLazy(property->obj(),
4041 args->at(0),
4042 args->at(1)->AsVariableProxy(),
4043 node->position());
4044
4045 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004046 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004047 // Load the arguments.
4048 int arg_count = args->length();
4049 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004050 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004051 }
4052
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004053 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004054 // Set the name register and call the IC initialization code.
4055 __ mov(r2, Operand(name));
4056 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4057 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4058 CodeForSourcePosition(node->position());
4059 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4060 __ ldr(cp, frame_->Context());
4061 frame_->EmitPush(r0);
4062 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004063
4064 } else {
4065 // -------------------------------------------
4066 // JavaScript example: 'array[index](1, 2, 3)'
4067 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004068 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004070 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004071 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004072 Load(property->key());
4073 EmitKeyedLoad();
4074 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004075 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004076 frame_->EmitPush(r0); // Function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004077 LoadGlobalReceiver(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004078 // Call the function.
4079 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4080 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004081 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004082 // Load the arguments.
4083 int arg_count = args->length();
4084 for (int i = 0; i < arg_count; i++) {
4085 Load(args->at(i));
4086 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004087
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004088 // Set the name register and call the IC initialization code.
4089 Load(property->key());
4090 frame_->EmitPop(r2); // Function name.
4091
4092 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4093 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4094 CodeForSourcePosition(node->position());
4095 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4096 __ ldr(cp, frame_->Context());
4097 frame_->EmitPush(r0);
4098 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099 }
4100
4101 } else {
4102 // ----------------------------------
4103 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4104 // ----------------------------------
4105
4106 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004107 Load(function);
4108
4109 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004110
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004111 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004112 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004115 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004118 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004119}
4120
4121
ager@chromium.org7c537e22008-10-16 08:43:32 +00004122void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004123#ifdef DEBUG
4124 int original_height = frame_->height();
4125#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126 Comment cmnt(masm_, "[ CallNew");
4127
4128 // According to ECMA-262, section 11.2.2, page 44, the function
4129 // expression in new calls must be evaluated before the
4130 // arguments. This is different from ordinary calls, where the
4131 // actual function to call is resolved after the arguments have been
4132 // evaluated.
4133
4134 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004135 // receiver. There is no need to use the global proxy here because
4136 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004137 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004138 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139
4140 // Push the arguments ("left-to-right") on the stack.
4141 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004142 int arg_count = args->length();
4143 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004144 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004147 VirtualFrame::SpilledScope spilled_scope(frame_);
4148
mads.s.ager31e71382008-08-13 09:32:07 +00004149 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004150 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004151 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004152 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154 // Call the construct call builtin that handles allocation and
4155 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004156 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004157 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004158 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004159
4160 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004161 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004162 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163}
4164
4165
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004166void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004167 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004168 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004169 JumpTarget leave, null, function, non_function_constructor;
4170
4171 // Load the object into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004172 Load(args->at(0));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004173 frame_->EmitPop(r0);
4174
4175 // If the object is a smi, we return null.
4176 __ tst(r0, Operand(kSmiTagMask));
4177 null.Branch(eq);
4178
4179 // Check that the object is a JS object but take special care of JS
4180 // functions to make sure they have 'Function' as their class.
4181 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
4182 null.Branch(lt);
4183
4184 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4185 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4186 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004187 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4188 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004189 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4190 function.Branch(eq);
4191
4192 // Check if the constructor in the map is a function.
4193 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
4194 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
4195 non_function_constructor.Branch(ne);
4196
4197 // The r0 register now contains the constructor function. Grab the
4198 // instance class name from there.
4199 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
4200 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004201 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004202 leave.Jump();
4203
4204 // Functions have class 'Function'.
4205 function.Bind();
4206 __ mov(r0, Operand(Factory::function_class_symbol()));
4207 frame_->EmitPush(r0);
4208 leave.Jump();
4209
4210 // Objects with a non-function constructor have class 'Object'.
4211 non_function_constructor.Bind();
4212 __ mov(r0, Operand(Factory::Object_symbol()));
4213 frame_->EmitPush(r0);
4214 leave.Jump();
4215
4216 // Non-JS objects have class null.
4217 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004218 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004219 frame_->EmitPush(r0);
4220
4221 // All done.
4222 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004223}
4224
4225
ager@chromium.org7c537e22008-10-16 08:43:32 +00004226void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004227 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004228 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004229 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004230 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004231 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004232 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004233 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004235 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4236 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004237 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238 // Load the value.
4239 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004240 leave.Bind();
4241 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004242}
4243
4244
ager@chromium.org7c537e22008-10-16 08:43:32 +00004245void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004246 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004248 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004249 Load(args->at(0)); // Load the object.
4250 Load(args->at(1)); // Load the value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004251 frame_->EmitPop(r0); // r0 contains value
4252 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004253 // if (object->IsSmi()) return object.
4254 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004255 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004256 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4257 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004258 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259 // Store the value.
4260 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
4261 // Update the write barrier.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004262 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004263 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004264 leave.Bind();
4265 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004266}
4267
4268
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004270 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004271 Load(args->at(0));
4272 Register reg = frame_->PopToRegister();
4273 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004274 cc_reg_ = eq;
4275}
4276
4277
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004278void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4279 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4280 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004281#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004282 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004283 Load(args->at(1));
4284 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004285 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004286 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004287#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004288 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004289}
4290
4291
ager@chromium.org7c537e22008-10-16 08:43:32 +00004292void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004293 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004294 Load(args->at(0));
4295 Register reg = frame_->PopToRegister();
4296 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004297 cc_reg_ = eq;
4298}
4299
4300
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004301// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004302void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4303 ASSERT(args->length() == 2);
4304 Load(args->at(0));
4305 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004306
4307 if (!CpuFeatures::IsSupported(VFP3)) {
4308 frame_->CallRuntime(Runtime::kMath_pow, 2);
4309 frame_->EmitPush(r0);
4310 } else {
4311 CpuFeatures::Scope scope(VFP3);
4312 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004313 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004314
4315 Register scratch1 = VirtualFrame::scratch0();
4316 Register scratch2 = VirtualFrame::scratch1();
4317
4318 // Get base and exponent to registers.
4319 Register exponent = frame_->PopToRegister();
4320 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004321 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004322
4323 // Set the frame for the runtime jump target. The code below jumps to the
4324 // jump target label so the frame needs to be established before that.
4325 ASSERT(runtime.entry_frame() == NULL);
4326 runtime.set_entry_frame(frame_);
4327
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004328 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4329 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004330
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004331 heap_number_map = r6;
4332 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4333
4334 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4335 // d1.
4336 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4337 __ b(&powi);
4338
4339 __ bind(&base_nonsmi);
4340 // Exponent is smi and base is non smi. Get the double value from the base
4341 // into vfp register d1.
4342 __ ObjectToDoubleVFPRegister(base, d1,
4343 scratch1, scratch2, heap_number_map, s0,
4344 runtime.entry_label());
4345
4346 __ bind(&powi);
4347
4348 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004349 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004350
4351 // Get the absolute untagged value of the exponent and use that for the
4352 // calculation.
4353 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4354 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4355 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4356
4357 // Run through all the bits in the exponent. The result is calculated in d0
4358 // and d1 holds base^(bit^2).
4359 Label more_bits;
4360 __ bind(&more_bits);
4361 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4362 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4363 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4364 __ b(ne, &more_bits);
4365
4366 // If exponent is positive we are done.
4367 __ cmp(exponent, Operand(0));
4368 __ b(ge, &allocate_return);
4369
4370 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4371 // case). However if d0 has reached infinity this will not provide the
4372 // correct result, so call runtime if that is the case.
4373 __ mov(scratch2, Operand(0x7FF00000));
4374 __ mov(scratch1, Operand(0));
4375 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4376 __ vcmp(d0, d1);
4377 __ vmrs(pc);
4378 runtime.Branch(eq); // d0 reached infinity.
4379 __ vdiv(d0, d2, d0);
4380 __ b(&allocate_return);
4381
4382 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004383 // Special handling of raising to the power of -0.5 and 0.5. First check
4384 // that the value is a heap number and that the lower bits (which for both
4385 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004386 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004387 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4388 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4389 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4390 __ cmp(scratch1, heap_number_map);
4391 runtime.Branch(ne);
4392 __ tst(scratch2, scratch2);
4393 runtime.Branch(ne);
4394
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004395 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004396 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4397
4398 // Compare exponent with -0.5.
4399 __ cmp(scratch1, Operand(0xbfe00000));
4400 __ b(ne, &not_minus_half);
4401
4402 // Get the double value from the base into vfp register d0.
4403 __ ObjectToDoubleVFPRegister(base, d0,
4404 scratch1, scratch2, heap_number_map, s0,
4405 runtime.entry_label(),
4406 AVOID_NANS_AND_INFINITIES);
4407
4408 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004409 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004410
4411 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4412 __ vdiv(d0, d2, d0);
4413 __ vsqrt(d0, d0);
4414
4415 __ b(&allocate_return);
4416
4417 __ bind(&not_minus_half);
4418 // Compare exponent with 0.5.
4419 __ cmp(scratch1, Operand(0x3fe00000));
4420 runtime.Branch(ne);
4421
4422 // Get the double value from the base into vfp register d0.
4423 __ ObjectToDoubleVFPRegister(base, d0,
4424 scratch1, scratch2, heap_number_map, s0,
4425 runtime.entry_label(),
4426 AVOID_NANS_AND_INFINITIES);
4427 __ vsqrt(d0, d0);
4428
4429 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004430 Register scratch3 = r5;
4431 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4432 heap_number_map, runtime.entry_label());
4433 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004434 done.Jump();
4435
4436 runtime.Bind();
4437
4438 // Push back the arguments again for the runtime call.
4439 frame_->EmitPush(base);
4440 frame_->EmitPush(exponent);
4441 frame_->CallRuntime(Runtime::kMath_pow, 2);
4442 __ Move(base, r0);
4443
4444 done.Bind();
4445 frame_->EmitPush(base);
4446 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004447}
4448
4449
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004450// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004451void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4452 ASSERT(args->length() == 1);
4453 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004454
4455 if (!CpuFeatures::IsSupported(VFP3)) {
4456 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4457 frame_->EmitPush(r0);
4458 } else {
4459 CpuFeatures::Scope scope(VFP3);
4460 JumpTarget runtime, done;
4461
4462 Register scratch1 = VirtualFrame::scratch0();
4463 Register scratch2 = VirtualFrame::scratch1();
4464
4465 // Get the value from the frame.
4466 Register tos = frame_->PopToRegister();
4467
4468 // Set the frame for the runtime jump target. The code below jumps to the
4469 // jump target label so the frame needs to be established before that.
4470 ASSERT(runtime.entry_frame() == NULL);
4471 runtime.set_entry_frame(frame_);
4472
4473 Register heap_number_map = r6;
4474 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4475
4476 // Get the double value from the heap number into vfp register d0.
4477 __ ObjectToDoubleVFPRegister(tos, d0,
4478 scratch1, scratch2, heap_number_map, s0,
4479 runtime.entry_label());
4480
4481 // Calculate the square root of d0 and place result in a heap number object.
4482 __ vsqrt(d0, d0);
4483 __ AllocateHeapNumberWithValue(
4484 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4485 done.Jump();
4486
4487 runtime.Bind();
4488 // Push back the argument again for the runtime call.
4489 frame_->EmitPush(tos);
4490 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4491 __ Move(tos, r0);
4492
4493 done.Bind();
4494 frame_->EmitPush(tos);
4495 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004496}
4497
4498
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004499class DeferredStringCharCodeAt : public DeferredCode {
4500 public:
4501 DeferredStringCharCodeAt(Register object,
4502 Register index,
4503 Register scratch,
4504 Register result)
4505 : result_(result),
4506 char_code_at_generator_(object,
4507 index,
4508 scratch,
4509 result,
4510 &need_conversion_,
4511 &need_conversion_,
4512 &index_out_of_range_,
4513 STRING_INDEX_IS_NUMBER) {}
4514
4515 StringCharCodeAtGenerator* fast_case_generator() {
4516 return &char_code_at_generator_;
4517 }
4518
4519 virtual void Generate() {
4520 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4521 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4522
4523 __ bind(&need_conversion_);
4524 // Move the undefined value into the result register, which will
4525 // trigger conversion.
4526 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4527 __ jmp(exit_label());
4528
4529 __ bind(&index_out_of_range_);
4530 // When the index is out of range, the spec requires us to return
4531 // NaN.
4532 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4533 __ jmp(exit_label());
4534 }
4535
4536 private:
4537 Register result_;
4538
4539 Label need_conversion_;
4540 Label index_out_of_range_;
4541
4542 StringCharCodeAtGenerator char_code_at_generator_;
4543};
4544
4545
4546// This generates code that performs a String.prototype.charCodeAt() call
4547// or returns a smi in order to trigger conversion.
4548void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
4549 VirtualFrame::SpilledScope spilled_scope(frame_);
4550 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004551 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004552
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004553 Load(args->at(0));
4554 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004555
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004556 Register index = r1;
4557 Register object = r2;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004558
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004559 frame_->EmitPop(r1);
4560 frame_->EmitPop(r2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004561
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004562 // We need two extra registers.
4563 Register scratch = r3;
4564 Register result = r0;
4565
4566 DeferredStringCharCodeAt* deferred =
4567 new DeferredStringCharCodeAt(object,
4568 index,
4569 scratch,
4570 result);
4571 deferred->fast_case_generator()->GenerateFast(masm_);
4572 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004573 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004574}
4575
4576
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004577class DeferredStringCharFromCode : public DeferredCode {
4578 public:
4579 DeferredStringCharFromCode(Register code,
4580 Register result)
4581 : char_from_code_generator_(code, result) {}
4582
4583 StringCharFromCodeGenerator* fast_case_generator() {
4584 return &char_from_code_generator_;
4585 }
4586
4587 virtual void Generate() {
4588 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4589 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4590 }
4591
4592 private:
4593 StringCharFromCodeGenerator char_from_code_generator_;
4594};
4595
4596
4597// Generates code for creating a one-char string from a char code.
4598void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
4599 VirtualFrame::SpilledScope spilled_scope(frame_);
4600 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004601 ASSERT(args->length() == 1);
4602
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004603 Load(args->at(0));
4604
ager@chromium.orgac091b72010-05-05 07:34:42 +00004605 Register code = r1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00004606 Register result = r0;
4607
ager@chromium.orgac091b72010-05-05 07:34:42 +00004608 frame_->EmitPop(code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004609
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004610 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4611 code, result);
4612 deferred->fast_case_generator()->GenerateFast(masm_);
4613 deferred->BindExit();
4614 frame_->EmitPush(result);
4615}
4616
4617
4618class DeferredStringCharAt : public DeferredCode {
4619 public:
4620 DeferredStringCharAt(Register object,
4621 Register index,
4622 Register scratch1,
4623 Register scratch2,
4624 Register result)
4625 : result_(result),
4626 char_at_generator_(object,
4627 index,
4628 scratch1,
4629 scratch2,
4630 result,
4631 &need_conversion_,
4632 &need_conversion_,
4633 &index_out_of_range_,
4634 STRING_INDEX_IS_NUMBER) {}
4635
4636 StringCharAtGenerator* fast_case_generator() {
4637 return &char_at_generator_;
4638 }
4639
4640 virtual void Generate() {
4641 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4642 char_at_generator_.GenerateSlow(masm(), call_helper);
4643
4644 __ bind(&need_conversion_);
4645 // Move smi zero into the result register, which will trigger
4646 // conversion.
4647 __ mov(result_, Operand(Smi::FromInt(0)));
4648 __ jmp(exit_label());
4649
4650 __ bind(&index_out_of_range_);
4651 // When the index is out of range, the spec requires us to return
4652 // the empty string.
4653 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4654 __ jmp(exit_label());
4655 }
4656
4657 private:
4658 Register result_;
4659
4660 Label need_conversion_;
4661 Label index_out_of_range_;
4662
4663 StringCharAtGenerator char_at_generator_;
4664};
4665
4666
4667// This generates code that performs a String.prototype.charAt() call
4668// or returns a smi in order to trigger conversion.
4669void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
4670 VirtualFrame::SpilledScope spilled_scope(frame_);
4671 Comment(masm_, "[ GenerateStringCharAt");
4672 ASSERT(args->length() == 2);
4673
4674 Load(args->at(0));
4675 Load(args->at(1));
4676
4677 Register index = r1;
4678 Register object = r2;
4679
4680 frame_->EmitPop(r1);
4681 frame_->EmitPop(r2);
4682
4683 // We need three extra registers.
4684 Register scratch1 = r3;
4685 Register scratch2 = r4;
4686 Register result = r0;
4687
4688 DeferredStringCharAt* deferred =
4689 new DeferredStringCharAt(object,
4690 index,
4691 scratch1,
4692 scratch2,
4693 result);
4694 deferred->fast_case_generator()->GenerateFast(masm_);
4695 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004696 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004697}
4698
4699
ager@chromium.org7c537e22008-10-16 08:43:32 +00004700void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004701 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004702 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004703 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004704 // We need the CC bits to come out as not_equal in the case where the
4705 // object is a smi. This can't be done with the usual test opcode so
4706 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004707 Register possible_array = frame_->PopToRegister();
4708 Register scratch = VirtualFrame::scratch0();
4709 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4710 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004711 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004712 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004713 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004714 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004715 cc_reg_ = eq;
4716}
4717
4718
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004719void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004720 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004721 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004722 JumpTarget answer;
4723 // We need the CC bits to come out as not_equal in the case where the
4724 // object is a smi. This can't be done with the usual test opcode so
4725 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004726 Register possible_regexp = frame_->PopToRegister();
4727 Register scratch = VirtualFrame::scratch0();
4728 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4729 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004730 answer.Branch(ne);
4731 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004732 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004733 answer.Bind();
4734 cc_reg_ = eq;
4735}
4736
4737
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004738void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4739 // This generates a fast version of:
4740 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004741 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004742 Load(args->at(0));
4743 Register possible_object = frame_->PopToRegister();
4744 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004745 false_target()->Branch(eq);
4746
4747 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004748 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004749 true_target()->Branch(eq);
4750
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004751 Register map_reg = VirtualFrame::scratch0();
4752 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004753 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004754 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4755 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004756 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004757
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004758 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4759 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004760 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004761 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004762 cc_reg_ = le;
4763}
4764
4765
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004766void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4767 // This generates a fast version of:
4768 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4769 // typeof(arg) == function).
4770 // It includes undetectable objects (as opposed to IsObject).
4771 ASSERT(args->length() == 1);
4772 Load(args->at(0));
4773 Register value = frame_->PopToRegister();
4774 __ tst(value, Operand(kSmiTagMask));
4775 false_target()->Branch(eq);
4776 // Check that this is an object.
4777 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4778 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4779 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4780 cc_reg_ = ge;
4781}
4782
4783
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004784void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4785 // This generates a fast version of:
4786 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004787 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004788 Load(args->at(0));
4789 Register possible_function = frame_->PopToRegister();
4790 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004791 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004792 Register map_reg = VirtualFrame::scratch0();
4793 Register scratch = VirtualFrame::scratch1();
4794 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004795 cc_reg_ = eq;
4796}
4797
4798
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004799void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004800 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004801 Load(args->at(0));
4802 Register possible_undetectable = frame_->PopToRegister();
4803 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004804 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004805 Register scratch = VirtualFrame::scratch0();
4806 __ ldr(scratch,
4807 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4808 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4809 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004810 cc_reg_ = ne;
4811}
4812
4813
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004814void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004815 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004816
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004817 Register scratch0 = VirtualFrame::scratch0();
4818 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004819 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004820 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004821
4822 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004823 __ ldr(scratch1,
4824 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4825 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4826 __ ldr(scratch0,
4827 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004828
4829 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004830 __ ldr(scratch1,
4831 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
4832 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004833 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004834}
4835
4836
ager@chromium.org7c537e22008-10-16 08:43:32 +00004837void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004838 ASSERT(args->length() == 0);
4839
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004840 Register tos = frame_->GetTOSRegister();
4841 Register scratch0 = VirtualFrame::scratch0();
4842 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004843
lrn@chromium.org25156de2010-04-06 13:10:27 +00004844 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004845 __ ldr(scratch0,
4846 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4847 __ ldr(scratch1,
4848 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4849 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4850
4851 // Get the number of formal parameters.
4852 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004853
4854 // Arguments adaptor case: Read the arguments length from the
4855 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004856 __ ldr(tos,
4857 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
4858 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004859
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004860 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004861}
4862
4863
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004864void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004865 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004866 ASSERT(args->length() == 1);
4867
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004868 // Satisfy contract with ArgumentsAccessStub:
4869 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004870 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004871 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004872 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004873
4874 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004875 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004876 frame_->CallStub(&stub, 0);
4877 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004878}
4879
4880
ager@chromium.org357bf652010-04-12 11:30:10 +00004881void CodeGenerator::GenerateRandomHeapNumber(
4882 ZoneList<Expression*>* args) {
4883 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004884 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004885
4886 Label slow_allocate_heapnumber;
4887 Label heapnumber_allocated;
4888
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004889 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
4890 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004891 __ jmp(&heapnumber_allocated);
4892
4893 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004894 // Allocate a heap number.
4895 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004896 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004897
4898 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004899
4900 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4901 // by computing:
4902 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4903 if (CpuFeatures::IsSupported(VFP3)) {
4904 __ PrepareCallCFunction(0, r1);
4905 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4906
4907 CpuFeatures::Scope scope(VFP3);
4908 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4909 // Create this constant using mov/orr to avoid PC relative load.
4910 __ mov(r1, Operand(0x41000000));
4911 __ orr(r1, r1, Operand(0x300000));
4912 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4913 __ vmov(d7, r0, r1);
4914 // Move 0x4130000000000000 to VFP.
4915 __ mov(r0, Operand(0));
4916 __ vmov(d8, r0, r1);
4917 // Subtract and store the result in the heap number.
4918 __ vsub(d7, d7, d8);
4919 __ sub(r0, r4, Operand(kHeapObjectTag));
4920 __ vstr(d7, r0, HeapNumber::kValueOffset);
4921 frame_->EmitPush(r4);
4922 } else {
4923 __ mov(r0, Operand(r4));
4924 __ PrepareCallCFunction(1, r1);
4925 __ CallCFunction(
4926 ExternalReference::fill_heap_number_with_random_function(), 1);
4927 frame_->EmitPush(r0);
4928 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004929}
4930
4931
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004932void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4933 ASSERT_EQ(2, args->length());
4934
4935 Load(args->at(0));
4936 Load(args->at(1));
4937
ager@chromium.org5c838252010-02-19 08:53:10 +00004938 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004939 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004940 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004941 frame_->EmitPush(r0);
4942}
4943
4944
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004945void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4946 ASSERT_EQ(3, args->length());
4947
4948 Load(args->at(0));
4949 Load(args->at(1));
4950 Load(args->at(2));
4951
ager@chromium.org5c838252010-02-19 08:53:10 +00004952 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004953 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004954 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004955 frame_->EmitPush(r0);
4956}
4957
4958
4959void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4960 ASSERT_EQ(2, args->length());
4961
4962 Load(args->at(0));
4963 Load(args->at(1));
4964
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004965 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004966 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004967 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004968 frame_->EmitPush(r0);
4969}
4970
4971
4972void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4973 ASSERT_EQ(4, args->length());
4974
4975 Load(args->at(0));
4976 Load(args->at(1));
4977 Load(args->at(2));
4978 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004979 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004980 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004981 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004982 frame_->EmitPush(r0);
4983}
4984
4985
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004986void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4987 // No stub. This code only occurs a few times in regexp.js.
4988 const int kMaxInlineLength = 100;
4989 ASSERT_EQ(3, args->length());
4990 Load(args->at(0)); // Size of array, smi.
4991 Load(args->at(1)); // "index" property value.
4992 Load(args->at(2)); // "input" property value.
4993 {
4994 VirtualFrame::SpilledScope spilled_scope(frame_);
4995 Label slowcase;
4996 Label done;
4997 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4998 STATIC_ASSERT(kSmiTag == 0);
4999 STATIC_ASSERT(kSmiTagSize == 1);
5000 __ tst(r1, Operand(kSmiTagMask));
5001 __ b(ne, &slowcase);
5002 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5003 __ b(hi, &slowcase);
5004 // Smi-tagging is equivalent to multiplying by 2.
5005 // Allocate RegExpResult followed by FixedArray with size in ebx.
5006 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5007 // Elements: [Map][Length][..elements..]
5008 // Size of JSArray with two in-object properties and the header of a
5009 // FixedArray.
5010 int objects_size =
5011 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5012 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5013 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005014 __ AllocateInNewSpace(
5015 r2, // In: Size, in words.
5016 r0, // Out: Start of allocation (tagged).
5017 r3, // Scratch register.
5018 r4, // Scratch register.
5019 &slowcase,
5020 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005021 // r0: Start of allocated area, object-tagged.
5022 // r1: Number of elements in array, as smi.
5023 // r5: Number of elements, untagged.
5024
5025 // Set JSArray map to global.regexp_result_map().
5026 // Set empty properties FixedArray.
5027 // Set elements to point to FixedArray allocated right after the JSArray.
5028 // Interleave operations for better latency.
5029 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5030 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5031 __ mov(r4, Operand(Factory::empty_fixed_array()));
5032 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5033 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5034 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5035 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5036 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5037
5038 // Set input, index and length fields from arguments.
5039 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5040 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5041 __ add(sp, sp, Operand(kPointerSize));
5042 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5043 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5044
5045 // Fill out the elements FixedArray.
5046 // r0: JSArray, tagged.
5047 // r3: FixedArray, tagged.
5048 // r5: Number of elements in array, untagged.
5049
5050 // Set map.
5051 __ mov(r2, Operand(Factory::fixed_array_map()));
5052 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5053 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005054 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5055 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005056 // Fill contents of fixed-array with the-hole.
5057 __ mov(r2, Operand(Factory::the_hole_value()));
5058 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5059 // Fill fixed array elements with hole.
5060 // r0: JSArray, tagged.
5061 // r2: the hole.
5062 // r3: Start of elements in FixedArray.
5063 // r5: Number of elements to fill.
5064 Label loop;
5065 __ tst(r5, Operand(r5));
5066 __ bind(&loop);
5067 __ b(le, &done); // Jump if r1 is negative or zero.
5068 __ sub(r5, r5, Operand(1), SetCC);
5069 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5070 __ jmp(&loop);
5071
5072 __ bind(&slowcase);
5073 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5074
5075 __ bind(&done);
5076 }
5077 frame_->Forget(3);
5078 frame_->EmitPush(r0);
5079}
5080
5081
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005082class DeferredSearchCache: public DeferredCode {
5083 public:
5084 DeferredSearchCache(Register dst, Register cache, Register key)
5085 : dst_(dst), cache_(cache), key_(key) {
5086 set_comment("[ DeferredSearchCache");
5087 }
5088
5089 virtual void Generate();
5090
5091 private:
5092 Register dst_, cache_, key_;
5093};
5094
5095
5096void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005097 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005098 __ CallRuntime(Runtime::kGetFromCache, 2);
5099 if (!dst_.is(r0)) {
5100 __ mov(dst_, r0);
5101 }
5102}
5103
5104
5105void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5106 ASSERT_EQ(2, args->length());
5107
5108 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5109 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5110
5111 Handle<FixedArray> jsfunction_result_caches(
5112 Top::global_context()->jsfunction_result_caches());
5113 if (jsfunction_result_caches->length() <= cache_id) {
5114 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005115 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005116 return;
5117 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005118
5119 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005120
5121 VirtualFrame::SpilledScope spilled_scope(frame_);
5122
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005123 frame_->EmitPop(r2);
5124
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005125 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
5126 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
5127 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5128 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
5129
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005130 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
5131
5132 const int kFingerOffset =
5133 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005134 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005135 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
5136 // r0 now holds finger offset as a smi.
5137 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5138 // r3 now points to the start of fixed array elements.
5139 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5140 // Note side effect of PreIndex: r3 now points to the key of the pair.
5141 __ cmp(r2, r0);
5142 deferred->Branch(ne);
5143
5144 __ ldr(r0, MemOperand(r3, kPointerSize));
5145
5146 deferred->BindExit();
5147 frame_->EmitPush(r0);
5148}
5149
5150
ager@chromium.org5c838252010-02-19 08:53:10 +00005151void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5152 ASSERT_EQ(args->length(), 1);
5153
5154 // Load the argument on the stack and jump to the runtime.
5155 Load(args->at(0));
5156
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005157 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005158 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005159 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005160 frame_->EmitPush(r0);
5161}
5162
5163
ager@chromium.orgac091b72010-05-05 07:34:42 +00005164class DeferredSwapElements: public DeferredCode {
5165 public:
5166 DeferredSwapElements(Register object, Register index1, Register index2)
5167 : object_(object), index1_(index1), index2_(index2) {
5168 set_comment("[ DeferredSwapElements");
5169 }
5170
5171 virtual void Generate();
5172
5173 private:
5174 Register object_, index1_, index2_;
5175};
5176
5177
5178void DeferredSwapElements::Generate() {
5179 __ push(object_);
5180 __ push(index1_);
5181 __ push(index2_);
5182 __ CallRuntime(Runtime::kSwapElements, 3);
5183}
5184
5185
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005186void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5187 Comment cmnt(masm_, "[ GenerateSwapElements");
5188
5189 ASSERT_EQ(3, args->length());
5190
5191 Load(args->at(0));
5192 Load(args->at(1));
5193 Load(args->at(2));
5194
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005195 VirtualFrame::SpilledScope spilled_scope(frame_);
5196
ager@chromium.orgac091b72010-05-05 07:34:42 +00005197 Register index2 = r2;
5198 Register index1 = r1;
5199 Register object = r0;
5200 Register tmp1 = r3;
5201 Register tmp2 = r4;
5202
5203 frame_->EmitPop(index2);
5204 frame_->EmitPop(index1);
5205 frame_->EmitPop(object);
5206
5207 DeferredSwapElements* deferred =
5208 new DeferredSwapElements(object, index1, index2);
5209
5210 // Fetch the map and check if array is in fast case.
5211 // Check that object doesn't require security checks and
5212 // has no indexed interceptor.
5213 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5214 deferred->Branch(lt);
5215 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5216 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5217 deferred->Branch(nz);
5218
5219 // Check the object's elements are in fast case.
5220 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5221 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5222 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5223 __ cmp(tmp2, ip);
5224 deferred->Branch(ne);
5225
5226 // Smi-tagging is equivalent to multiplying by 2.
5227 STATIC_ASSERT(kSmiTag == 0);
5228 STATIC_ASSERT(kSmiTagSize == 1);
5229
5230 // Check that both indices are smis.
5231 __ mov(tmp2, index1);
5232 __ orr(tmp2, tmp2, index2);
5233 __ tst(tmp2, Operand(kSmiTagMask));
5234 deferred->Branch(nz);
5235
5236 // Bring the offsets into the fixed array in tmp1 into index1 and
5237 // index2.
5238 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5239 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5240 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5241
5242 // Swap elements.
5243 Register tmp3 = object;
5244 object = no_reg;
5245 __ ldr(tmp3, MemOperand(tmp1, index1));
5246 __ ldr(tmp2, MemOperand(tmp1, index2));
5247 __ str(tmp3, MemOperand(tmp1, index2));
5248 __ str(tmp2, MemOperand(tmp1, index1));
5249
5250 Label done;
5251 __ InNewSpace(tmp1, tmp2, eq, &done);
5252 // Possible optimization: do a check that both values are Smis
5253 // (or them and test against Smi mask.)
5254
5255 __ mov(tmp2, tmp1);
5256 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5257 __ CallStub(&recordWrite1);
5258
5259 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5260 __ CallStub(&recordWrite2);
5261
5262 __ bind(&done);
5263
5264 deferred->BindExit();
5265 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5266 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005267}
5268
5269
ager@chromium.org357bf652010-04-12 11:30:10 +00005270void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5271 Comment cmnt(masm_, "[ GenerateCallFunction");
5272
5273 ASSERT(args->length() >= 2);
5274
5275 int n_args = args->length() - 2; // for receiver and function.
5276 Load(args->at(0)); // receiver
5277 for (int i = 0; i < n_args; i++) {
5278 Load(args->at(i + 1));
5279 }
5280 Load(args->at(n_args + 1)); // function
5281 frame_->CallJSFunction(n_args);
5282 frame_->EmitPush(r0);
5283}
5284
5285
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005286void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5287 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005288 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005289 if (CpuFeatures::IsSupported(VFP3)) {
5290 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5291 frame_->SpillAllButCopyTOSToR0();
5292 frame_->CallStub(&stub, 1);
5293 } else {
5294 frame_->CallRuntime(Runtime::kMath_sin, 1);
5295 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005296 frame_->EmitPush(r0);
5297}
5298
5299
5300void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5301 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005302 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005303 if (CpuFeatures::IsSupported(VFP3)) {
5304 TranscendentalCacheStub stub(TranscendentalCache::COS);
5305 frame_->SpillAllButCopyTOSToR0();
5306 frame_->CallStub(&stub, 1);
5307 } else {
5308 frame_->CallRuntime(Runtime::kMath_cos, 1);
5309 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005310 frame_->EmitPush(r0);
5311}
5312
5313
ager@chromium.org7c537e22008-10-16 08:43:32 +00005314void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005315 ASSERT(args->length() == 2);
5316
5317 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005318 Load(args->at(0));
5319 Load(args->at(1));
5320 Register lhs = frame_->PopToRegister();
5321 Register rhs = frame_->PopToRegister(lhs);
5322 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005323 cc_reg_ = eq;
5324}
5325
5326
ager@chromium.org7c537e22008-10-16 08:43:32 +00005327void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005328#ifdef DEBUG
5329 int original_height = frame_->height();
5330#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005331 if (CheckForInlineRuntimeCall(node)) {
5332 ASSERT((has_cc() && frame_->height() == original_height) ||
5333 (!has_cc() && frame_->height() == original_height + 1));
5334 return;
5335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005336
5337 ZoneList<Expression*>* args = node->arguments();
5338 Comment cmnt(masm_, "[ CallRuntime");
5339 Runtime::Function* function = node->function();
5340
ager@chromium.org41826e72009-03-30 13:30:57 +00005341 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005342 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005343 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005344 Register scratch = VirtualFrame::scratch0();
5345 __ ldr(scratch, GlobalObject());
5346 Register builtins = frame_->GetTOSRegister();
5347 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5348 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005349 }
mads.s.ager31e71382008-08-13 09:32:07 +00005350
ager@chromium.org41826e72009-03-30 13:30:57 +00005351 // Push the arguments ("left-to-right").
5352 int arg_count = args->length();
5353 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005354 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005355 }
mads.s.ager31e71382008-08-13 09:32:07 +00005356
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005357 VirtualFrame::SpilledScope spilled_scope(frame_);
5358
ager@chromium.org41826e72009-03-30 13:30:57 +00005359 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005360 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005361 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005362 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5363 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005364 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005365 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005366 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005367 } else {
5368 // Call the C runtime function.
5369 frame_->CallRuntime(function, arg_count);
5370 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005371 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005372 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005373}
5374
5375
ager@chromium.org7c537e22008-10-16 08:43:32 +00005376void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005377#ifdef DEBUG
5378 int original_height = frame_->height();
5379#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005380 Comment cmnt(masm_, "[ UnaryOperation");
5381
5382 Token::Value op = node->op();
5383
5384 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005385 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005386 // LoadCondition may (and usually does) leave a test and branch to
5387 // be emitted by the caller. In that case, negate the condition.
5388 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005389
5390 } else if (op == Token::DELETE) {
5391 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005392 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005393 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005394 Load(property->obj());
5395 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005396 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005397 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005398
mads.s.ager31e71382008-08-13 09:32:07 +00005399 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005400 Slot* slot = variable->slot();
5401 if (variable->is_global()) {
5402 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005403 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005404 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005405 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005406
5407 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5408 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005409 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005410 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005411 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005412 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005413 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005414 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005415 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005416 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005417
mads.s.ager31e71382008-08-13 09:32:07 +00005418 } else {
5419 // Default: Result of deleting non-global, not dynamically
5420 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005421 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005422 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005423
5424 } else {
5425 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005426 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005427 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005428 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005429 }
5430
5431 } else if (op == Token::TYPEOF) {
5432 // Special case for loading the typeof expression; see comment on
5433 // LoadTypeofExpression().
5434 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005435 frame_->CallRuntime(Runtime::kTypeof, 1);
5436 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005437
5438 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005439 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005440 (node->expression()->AsBinaryOperation() != NULL &&
5441 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005442 UnaryOverwriteMode overwrite =
5443 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5444
5445 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005446 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005447 switch (op) {
5448 case Token::NOT:
5449 case Token::DELETE:
5450 case Token::TYPEOF:
5451 UNREACHABLE(); // handled above
5452 break;
5453
5454 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005455 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005456 GenericUnaryOpStub stub(
5457 Token::SUB,
5458 overwrite,
5459 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005460 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005461 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005462 break;
5463 }
5464
5465 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005466 Register tos = frame_->PopToRegister();
5467 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005468 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005469 // Smi check.
5470 __ tst(tos, Operand(kSmiTagMask));
5471 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005472
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005473 __ mvn(tos, Operand(tos));
5474 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5475 frame_->EmitPush(tos);
5476 // The fast case is the first to jump to the continue label, so it gets
5477 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005478 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005479
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005480 not_smi_label.Bind();
5481 frame_->SpillAll();
5482 __ Move(r0, tos);
5483 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5484 frame_->CallStub(&stub, 0);
5485 frame_->EmitPush(r0);
5486
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005487 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005488 break;
5489 }
5490
5491 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005492 frame_->Drop();
5493 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005494 break;
5495
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005496 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005497 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005498 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005499 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005500 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005501 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005502
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005503 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005504 frame_->EmitPush(r0);
5505
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005506 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005507 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005508 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005509 default:
5510 UNREACHABLE();
5511 }
5512 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005513 ASSERT(!has_valid_frame() ||
5514 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005515 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005516}
5517
5518
ager@chromium.org7c537e22008-10-16 08:43:32 +00005519void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005520#ifdef DEBUG
5521 int original_height = frame_->height();
5522#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005523 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005524 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005525
5526 bool is_postfix = node->is_postfix();
5527 bool is_increment = node->op() == Token::INC;
5528
5529 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5530 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005531 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005532
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005533 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5534 // The type info declares that this variable is always a Smi. That
5535 // means it is a Smi both before and after the increment/decrement.
5536 // Lets make use of that to make a very minimal count.
5537 Reference target(this, node->expression(), !is_const);
5538 ASSERT(!target.is_illegal());
5539 target.GetValue(); // Pushes the value.
5540 Register value = frame_->PopToRegister();
5541 if (is_postfix) frame_->EmitPush(value);
5542 if (is_increment) {
5543 __ add(value, value, Operand(Smi::FromInt(1)));
5544 } else {
5545 __ sub(value, value, Operand(Smi::FromInt(1)));
5546 }
5547 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005548 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005549 if (is_postfix) frame_->Pop();
5550 ASSERT_EQ(original_height + 1, frame_->height());
5551 return;
5552 }
5553
5554 // If it's a postfix expression and its result is not ignored and the
5555 // reference is non-trivial, then push a placeholder on the stack now
5556 // to hold the result of the expression.
5557 bool placeholder_pushed = false;
5558 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005559 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005560 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005561 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005562
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005563 // A constant reference is not saved to, so a constant reference is not a
5564 // compound assignment reference.
5565 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005566 if (target.is_illegal()) {
5567 // Spoof the virtual frame to have the expected height (one higher
5568 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005569 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005570 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005571 return;
5572 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005573
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005574 // This pushes 0, 1 or 2 words on the object to be used later when updating
5575 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005576 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005577
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005578 JumpTarget slow;
5579 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005580
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005581 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005582
5583 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005584 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005585 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005586 } else if (is_postfix) {
5587 frame_->EmitPush(value);
5588 __ mov(VirtualFrame::scratch0(), value);
5589 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005590 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005591
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005592 // Check for smi operand.
5593 __ tst(value, Operand(kSmiTagMask));
5594 slow.Branch(ne);
5595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005596 // Perform optimistic increment/decrement.
5597 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005598 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005599 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005600 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005601 }
5602
5603 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005604 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005605
5606 // Revert optimistic increment/decrement.
5607 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005608 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005609 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005610 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005611 }
5612
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005613 // Slow case: Convert to number. At this point the
5614 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005615 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005616
5617 // Convert the operand to a number.
5618 frame_->EmitPush(value);
5619
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005620 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005621 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005622 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005623
5624 if (is_postfix) {
5625 // Postfix: store to result (on the stack).
5626 __ str(r0, frame_->ElementAt(target.size()));
5627 }
5628
5629 // Compute the new value.
5630 frame_->EmitPush(r0);
5631 frame_->EmitPush(Operand(Smi::FromInt(1)));
5632 if (is_increment) {
5633 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5634 } else {
5635 frame_->CallRuntime(Runtime::kNumberSub, 2);
5636 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005637 }
5638
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005639 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005640 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005641 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005642 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005643 frame_->EmitPush(value);
5644 // Set the target with the result, leaving the result on
5645 // top of the stack. Removes the target from the stack if
5646 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005647 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005648 }
5649
5650 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005651 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005652 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005653}
5654
5655
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005656void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005657 // According to ECMA-262 section 11.11, page 58, the binary logical
5658 // operators must yield the result of one of the two expressions
5659 // before any ToBoolean() conversions. This means that the value
5660 // produced by a && or || operator is not necessarily a boolean.
5661
5662 // NOTE: If the left hand side produces a materialized value (not in
5663 // the CC register), we force the right hand side to do the
5664 // same. This is necessary because we may have to branch to the exit
5665 // after evaluating the left hand side (due to the shortcut
5666 // semantics), but the compiler must (statically) know if the result
5667 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005668 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005669 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005670 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005671 if (has_valid_frame() && !has_cc()) {
5672 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005673 JumpTarget pop_and_continue;
5674 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005675
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005676 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005677 // Avoid popping the result if it converts to 'false' using the
5678 // standard ToBoolean() conversion as described in ECMA-262,
5679 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005680 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005681 Branch(false, &exit);
5682
5683 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005684 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005685 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686
5687 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005688 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005689 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690
5691 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005692 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005693 } else if (has_cc() || is_true.is_linked()) {
5694 // The left-hand side is either (a) partially compiled to
5695 // control flow with a final branch left to emit or (b) fully
5696 // compiled to control flow and possibly true.
5697 if (has_cc()) {
5698 Branch(false, false_target());
5699 }
5700 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005701 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005702 } else {
5703 // Nothing to do.
5704 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005705 }
5706
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005707 } else {
5708 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005709 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005710 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005711 if (has_valid_frame() && !has_cc()) {
5712 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005713 JumpTarget pop_and_continue;
5714 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005715
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005716 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005717 // Avoid popping the result if it converts to 'true' using the
5718 // standard ToBoolean() conversion as described in ECMA-262,
5719 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005720 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005721 Branch(true, &exit);
5722
5723 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005724 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005725 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005726
5727 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005728 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005729 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005730
5731 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005732 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005733 } else if (has_cc() || is_false.is_linked()) {
5734 // The left-hand side is either (a) partially compiled to
5735 // control flow with a final branch left to emit or (b) fully
5736 // compiled to control flow and possibly false.
5737 if (has_cc()) {
5738 Branch(true, true_target());
5739 }
5740 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005741 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005742 } else {
5743 // Nothing to do.
5744 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005745 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005746 }
5747}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005748
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005749
5750void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5751#ifdef DEBUG
5752 int original_height = frame_->height();
5753#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005754 Comment cmnt(masm_, "[ BinaryOperation");
5755
5756 if (node->op() == Token::AND || node->op() == Token::OR) {
5757 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005758 } else {
5759 // Optimize for the case where (at least) one of the expressions
5760 // is a literal small integer.
5761 Literal* lliteral = node->left()->AsLiteral();
5762 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005763 // NOTE: The code below assumes that the slow cases (calls to runtime)
5764 // never return a constant/immutable object.
5765 bool overwrite_left =
5766 (node->left()->AsBinaryOperation() != NULL &&
5767 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5768 bool overwrite_right =
5769 (node->right()->AsBinaryOperation() != NULL &&
5770 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005771
5772 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005773 VirtualFrame::RegisterAllocationScope scope(this);
5774 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005775 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005776 SmiOperation(node->op(),
5777 rliteral->handle(),
5778 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005779 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005780 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005781 VirtualFrame::RegisterAllocationScope scope(this);
5782 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005783 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005784 SmiOperation(node->op(),
5785 lliteral->handle(),
5786 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005787 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005788 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005789 GenerateInlineSmi inline_smi =
5790 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
5791 if (lliteral != NULL) {
5792 ASSERT(!lliteral->handle()->IsSmi());
5793 inline_smi = DONT_GENERATE_INLINE_SMI;
5794 }
5795 if (rliteral != NULL) {
5796 ASSERT(!rliteral->handle()->IsSmi());
5797 inline_smi = DONT_GENERATE_INLINE_SMI;
5798 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005799 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005800 OverwriteMode overwrite_mode = NO_OVERWRITE;
5801 if (overwrite_left) {
5802 overwrite_mode = OVERWRITE_LEFT;
5803 } else if (overwrite_right) {
5804 overwrite_mode = OVERWRITE_RIGHT;
5805 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005806 Load(node->left());
5807 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005808 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005809 }
5810 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005811 ASSERT(!has_valid_frame() ||
5812 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005813 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005814}
5815
5816
ager@chromium.org7c537e22008-10-16 08:43:32 +00005817void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005818#ifdef DEBUG
5819 int original_height = frame_->height();
5820#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005821 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005822 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005823}
5824
5825
ager@chromium.org7c537e22008-10-16 08:43:32 +00005826void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005827#ifdef DEBUG
5828 int original_height = frame_->height();
5829#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005830 Comment cmnt(masm_, "[ CompareOperation");
5831
ager@chromium.org357bf652010-04-12 11:30:10 +00005832 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5833
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005834 // Get the expressions from the node.
5835 Expression* left = node->left();
5836 Expression* right = node->right();
5837 Token::Value op = node->op();
5838
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005839 // To make null checks efficient, we check if either left or right is the
5840 // literal 'null'. If so, we optimize the code by inlining a null check
5841 // instead of calling the (very) general runtime routine for checking
5842 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005843 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005844 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005845 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005846 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005847 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5848 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005849 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005850 Load(left_is_null ? right : left);
5851 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005852 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005853 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005854
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005855 // The 'null' value is only equal to 'undefined' if using non-strict
5856 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005857 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005858 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005859
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005860 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005861 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005862 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005863
ager@chromium.org357bf652010-04-12 11:30:10 +00005864 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005865 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005866
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005867 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005868 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5869 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5870 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5871 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005872 }
5873
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005874 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005875 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005876 return;
5877 }
5878 }
5879
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005880 // To make typeof testing for natives implemented in JavaScript really
5881 // efficient, we generate special code for expressions of the form:
5882 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005883 UnaryOperation* operation = left->AsUnaryOperation();
5884 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5885 (operation != NULL && operation->op() == Token::TYPEOF) &&
5886 (right->AsLiteral() != NULL &&
5887 right->AsLiteral()->handle()->IsString())) {
5888 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5889
ager@chromium.org357bf652010-04-12 11:30:10 +00005890 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005891 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005892 Register tos = frame_->PopToRegister();
5893
ager@chromium.org357bf652010-04-12 11:30:10 +00005894 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005895
5896 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005897 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005898 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005899 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005900 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005901 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005902 cc_reg_ = eq;
5903
5904 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005905 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005906 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005907
ager@chromium.org357bf652010-04-12 11:30:10 +00005908 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005909
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005910 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005911 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5912 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5913 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005914 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005915
ager@chromium.org357bf652010-04-12 11:30:10 +00005916 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5917 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005918 cc_reg_ = lt;
5919
5920 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005921 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005922 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005923 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005924 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005925 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005926 cc_reg_ = eq;
5927
5928 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005929 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005930 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005931 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005932
ager@chromium.org357bf652010-04-12 11:30:10 +00005933 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005934 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005935
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005936 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005937 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5938 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5939 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5940 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005941
5942 cc_reg_ = eq;
5943
5944 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005945 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005946 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005947 Register map_reg = scratch;
5948 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005949 true_target()->Branch(eq);
5950 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00005951 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005952 cc_reg_ = eq;
5953
5954 } else if (check->Equals(Heap::object_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.orgab99eea2009-08-25 07:05:41 +00005958 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005959 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005960 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005961
ager@chromium.org357bf652010-04-12 11:30:10 +00005962 Register map_reg = scratch;
5963 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005964 false_target()->Branch(eq);
5965
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005966 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005967 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5968 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5969 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005970 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005971
ager@chromium.org357bf652010-04-12 11:30:10 +00005972 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5973 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005974 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00005975 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005976 cc_reg_ = le;
5977
5978 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005979 // Uncommon case: typeof testing against a string literal that is
5980 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005981 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005982 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005983 ASSERT(!has_valid_frame() ||
5984 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005985 return;
5986 }
5987
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005988 switch (op) {
5989 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005990 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005991 break;
5992
5993 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005994 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005995 break;
5996
5997 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005998 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005999 break;
6000
6001 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006002 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006003 break;
6004
6005 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006006 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006007 break;
6008
6009 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006010 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006011 break;
6012
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006013 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006014 Load(left);
6015 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006016 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006017 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006018 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006019 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006021 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006022 Load(left);
6023 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006024 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006025 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006026 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006027 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006028 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006029 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006030 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006031
6032 default:
6033 UNREACHABLE();
6034 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006035 ASSERT((has_cc() && frame_->height() == original_height) ||
6036 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006037}
6038
6039
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006040class DeferredReferenceGetNamedValue: public DeferredCode {
6041 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006042 explicit DeferredReferenceGetNamedValue(Register receiver,
6043 Handle<String> name)
6044 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006045 set_comment("[ DeferredReferenceGetNamedValue");
6046 }
6047
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006048 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006049
6050 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006051 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006052 Handle<String> name_;
6053};
6054
6055
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006056// Convention for this is that on entry the receiver is in a register that
6057// is not used by the stack. On exit the answer is found in that same
6058// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006059void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006060#ifdef DEBUG
6061 int expected_height = frame_state()->frame()->height();
6062#endif
6063 VirtualFrame copied_frame(*frame_state()->frame());
6064 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006065
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006066 Register scratch1 = VirtualFrame::scratch0();
6067 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006068 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006069 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6070 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6071
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006072 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6073 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006074 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006075
6076 // The rest of the instructions in the deferred code must be together.
6077 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6078 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6079 __ Call(ic, RelocInfo::CODE_TARGET);
6080 // The call must be followed by a nop(1) instruction to indicate that the
6081 // in-object has been inlined.
6082 __ nop(PROPERTY_ACCESS_INLINED);
6083
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006084 // At this point the answer is in r0. We move it to the expected register
6085 // if necessary.
6086 __ Move(receiver_, r0);
6087
6088 // Now go back to the frame that we entered with. This will not overwrite
6089 // the receiver register since that register was not in use when we came
6090 // in. The instructions emitted by this merge are skipped over by the
6091 // inline load patching mechanism when looking for the branch instruction
6092 // that tells it where the code to patch is.
6093 copied_frame.MergeTo(frame_state()->frame());
6094
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006095 // Block the constant pool for one more instruction after leaving this
6096 // constant pool block scope to include the branch instruction ending the
6097 // deferred code.
6098 __ BlockConstPoolFor(1);
6099 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006100 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006101}
6102
6103
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006104class DeferredReferenceGetKeyedValue: public DeferredCode {
6105 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006106 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6107 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006108 set_comment("[ DeferredReferenceGetKeyedValue");
6109 }
6110
6111 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006112
6113 private:
6114 Register key_;
6115 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006116};
6117
6118
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006119// Takes key and register in r0 and r1 or vice versa. Returns result
6120// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006121void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006122 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6123 (key_.is(r1) && receiver_.is(r0)));
6124
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006125 VirtualFrame copied_frame(*frame_state()->frame());
6126 copied_frame.SpillAll();
6127
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006128 Register scratch1 = VirtualFrame::scratch0();
6129 Register scratch2 = VirtualFrame::scratch1();
6130 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6131 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6132
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006133 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6134 // convention.
6135 if (key_.is(r1)) {
6136 __ Swap(r0, r1, ip);
6137 }
6138
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006139 // The rest of the instructions in the deferred code must be together.
6140 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006141 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006142 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6143 __ Call(ic, RelocInfo::CODE_TARGET);
6144 // The call must be followed by a nop instruction to indicate that the
6145 // keyed load has been inlined.
6146 __ nop(PROPERTY_ACCESS_INLINED);
6147
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006148 // Now go back to the frame that we entered with. This will not overwrite
6149 // the receiver or key registers since they were not in use when we came
6150 // in. The instructions emitted by this merge are skipped over by the
6151 // inline load patching mechanism when looking for the branch instruction
6152 // that tells it where the code to patch is.
6153 copied_frame.MergeTo(frame_state()->frame());
6154
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006155 // Block the constant pool for one more instruction after leaving this
6156 // constant pool block scope to include the branch instruction ending the
6157 // deferred code.
6158 __ BlockConstPoolFor(1);
6159 }
6160}
6161
6162
6163class DeferredReferenceSetKeyedValue: public DeferredCode {
6164 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006165 DeferredReferenceSetKeyedValue(Register value,
6166 Register key,
6167 Register receiver)
6168 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006169 set_comment("[ DeferredReferenceSetKeyedValue");
6170 }
6171
6172 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006173
6174 private:
6175 Register value_;
6176 Register key_;
6177 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006178};
6179
6180
6181void DeferredReferenceSetKeyedValue::Generate() {
6182 Register scratch1 = VirtualFrame::scratch0();
6183 Register scratch2 = VirtualFrame::scratch1();
6184 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6185 __ IncrementCounter(
6186 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6187
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006188 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6189 // calling convention.
6190 if (value_.is(r1)) {
6191 __ Swap(r0, r1, ip);
6192 }
6193 ASSERT(receiver_.is(r2));
6194
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006195 // The rest of the instructions in the deferred code must be together.
6196 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006197 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6198 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006199 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6200 __ Call(ic, RelocInfo::CODE_TARGET);
6201 // The call must be followed by a nop instruction to indicate that the
6202 // keyed store has been inlined.
6203 __ nop(PROPERTY_ACCESS_INLINED);
6204
6205 // 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 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006210}
6211
6212
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006213class DeferredReferenceSetNamedValue: public DeferredCode {
6214 public:
6215 DeferredReferenceSetNamedValue(Register value,
6216 Register receiver,
6217 Handle<String> name)
6218 : value_(value), receiver_(receiver), name_(name) {
6219 set_comment("[ DeferredReferenceSetNamedValue");
6220 }
6221
6222 virtual void Generate();
6223
6224 private:
6225 Register value_;
6226 Register receiver_;
6227 Handle<String> name_;
6228};
6229
6230
6231// Takes value in r0, receiver in r1 and returns the result (the
6232// value) in r0.
6233void DeferredReferenceSetNamedValue::Generate() {
6234 // Record the entry frame and spill.
6235 VirtualFrame copied_frame(*frame_state()->frame());
6236 copied_frame.SpillAll();
6237
6238 // Ensure value in r0, receiver in r1 to match store ic calling
6239 // convention.
6240 ASSERT(value_.is(r0) && receiver_.is(r1));
6241 __ mov(r2, Operand(name_));
6242
6243 // The rest of the instructions in the deferred code must be together.
6244 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6245 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6246 // r1 and r2.
6247 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6248 __ Call(ic, RelocInfo::CODE_TARGET);
6249 // The call must be followed by a nop instruction to indicate that the
6250 // named store has been inlined.
6251 __ nop(PROPERTY_ACCESS_INLINED);
6252
6253 // Go back to the frame we entered with. The instructions
6254 // generated by this merge are skipped over by the inline store
6255 // patching mechanism when looking for the branch instruction that
6256 // tells it where the code to patch is.
6257 copied_frame.MergeTo(frame_state()->frame());
6258
6259 // Block the constant pool for one more instruction after leaving this
6260 // constant pool block scope to include the branch instruction ending the
6261 // deferred code.
6262 __ BlockConstPoolFor(1);
6263 }
6264}
6265
6266
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006267// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006268void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6269 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6270 Comment cmnt(masm(), "[ Load from named Property");
6271 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006272 frame_->CallLoadIC(name,
6273 is_contextual
6274 ? RelocInfo::CODE_TARGET_CONTEXT
6275 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006276 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006277 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006278 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006279 Comment cmnt(masm(), "[ Inlined named property load");
6280
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006281 // Counter will be decremented in the deferred code. Placed here to avoid
6282 // having it in the instruction stream below where patching will occur.
6283 __ IncrementCounter(&Counters::named_load_inline, 1,
6284 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006285
6286 // The following instructions are the inlined load of an in-object property.
6287 // Parts of this code is patched, so the exact instructions generated needs
6288 // to be fixed. Therefore the instruction pool is blocked when generating
6289 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006290
6291 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006292 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006293
6294 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006295 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006296
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006297#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006298 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006299 Label check_inlined_codesize;
6300 masm_->bind(&check_inlined_codesize);
6301#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006302
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006303 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006304 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006305 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006306 deferred->Branch(eq);
6307
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006308 Register scratch = VirtualFrame::scratch0();
6309 Register scratch2 = VirtualFrame::scratch1();
6310
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006311 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006312 // code. Therefore we can't use a LoadRoot call.
6313 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6314 __ mov(scratch2, Operand(Factory::null_value()));
6315 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006316 deferred->Branch(ne);
6317
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006318 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006319 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006320 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006321
6322 // Make sure that the expected number of instructions are generated.
6323 ASSERT_EQ(kInlinedNamedLoadInstructions,
6324 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006325 }
6326
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006327 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006328 // At this point the receiver register has the result, either from the
6329 // deferred code or from the inlined code.
6330 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006331 }
6332}
6333
6334
ager@chromium.orgac091b72010-05-05 07:34:42 +00006335void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6336#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006337 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006338#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006339
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006340 Result result;
6341 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6342 frame()->CallStoreIC(name, is_contextual);
6343 } else {
6344 // Inline the in-object property case.
6345 JumpTarget slow, done;
6346
6347 // Get the value and receiver from the stack.
6348 frame()->PopToR0();
6349 Register value = r0;
6350 frame()->PopToR1();
6351 Register receiver = r1;
6352
6353 DeferredReferenceSetNamedValue* deferred =
6354 new DeferredReferenceSetNamedValue(value, receiver, name);
6355
6356 // Check that the receiver is a heap object.
6357 __ tst(receiver, Operand(kSmiTagMask));
6358 deferred->Branch(eq);
6359
6360 // The following instructions are the part of the inlined
6361 // in-object property store code which can be patched. Therefore
6362 // the exact number of instructions generated must be fixed, so
6363 // the constant pool is blocked while generating this code.
6364 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6365 Register scratch0 = VirtualFrame::scratch0();
6366 Register scratch1 = VirtualFrame::scratch1();
6367
6368 // Check the map. Initially use an invalid map to force a
6369 // failure. The map check will be patched in the runtime system.
6370 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6371
6372#ifdef DEBUG
6373 Label check_inlined_codesize;
6374 masm_->bind(&check_inlined_codesize);
6375#endif
6376 __ mov(scratch0, Operand(Factory::null_value()));
6377 __ cmp(scratch0, scratch1);
6378 deferred->Branch(ne);
6379
6380 int offset = 0;
6381 __ str(value, MemOperand(receiver, offset));
6382
6383 // Update the write barrier and record its size. We do not use
6384 // the RecordWrite macro here because we want the offset
6385 // addition instruction first to make it easy to patch.
6386 Label record_write_start, record_write_done;
6387 __ bind(&record_write_start);
6388 // Add offset into the object.
6389 __ add(scratch0, receiver, Operand(offset));
6390 // Test that the object is not in the new space. We cannot set
6391 // region marks for new space pages.
6392 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6393 // Record the actual write.
6394 __ RecordWriteHelper(receiver, scratch0, scratch1);
6395 __ bind(&record_write_done);
6396 // Clobber all input registers when running with the debug-code flag
6397 // turned on to provoke errors.
6398 if (FLAG_debug_code) {
6399 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6400 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6401 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6402 }
6403 // Check that this is the first inlined write barrier or that
6404 // this inlined write barrier has the same size as all the other
6405 // inlined write barriers.
6406 ASSERT((inlined_write_barrier_size_ == -1) ||
6407 (inlined_write_barrier_size_ ==
6408 masm()->InstructionsGeneratedSince(&record_write_start)));
6409 inlined_write_barrier_size_ =
6410 masm()->InstructionsGeneratedSince(&record_write_start);
6411
6412 // Make sure that the expected number of instructions are generated.
6413 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6414 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6415 }
6416 deferred->BindExit();
6417 }
6418 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006419}
6420
6421
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006422void CodeGenerator::EmitKeyedLoad() {
6423 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006424 Comment cmnt(masm_, "[ Load from keyed property");
6425 frame_->CallKeyedLoadIC();
6426 } else {
6427 // Inline the keyed load.
6428 Comment cmnt(masm_, "[ Inlined load from keyed property");
6429
6430 // Counter will be decremented in the deferred code. Placed here to avoid
6431 // having it in the instruction stream below where patching will occur.
6432 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6433 frame_->scratch0(), frame_->scratch1());
6434
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006435 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006436 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006437 Register key = frame_->PopToRegister();
6438 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006439
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006440 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006441 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006442 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006443
6444 // Check that the receiver is a heap object.
6445 __ tst(receiver, Operand(kSmiTagMask));
6446 deferred->Branch(eq);
6447
6448 // The following instructions are the part of the inlined load keyed
6449 // property code which can be patched. Therefore the exact number of
6450 // instructions generated need to be fixed, so the constant pool is blocked
6451 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006452 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6453 Register scratch1 = VirtualFrame::scratch0();
6454 Register scratch2 = VirtualFrame::scratch1();
6455 // Check the map. The null map used below is patched by the inline cache
6456 // code.
6457 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006458
6459 // Check that the key is a smi.
6460 if (!key_is_known_smi) {
6461 __ tst(key, Operand(kSmiTagMask));
6462 deferred->Branch(ne);
6463 }
6464
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006465#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006466 Label check_inlined_codesize;
6467 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006468#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006469 __ mov(scratch2, Operand(Factory::null_value()));
6470 __ cmp(scratch1, scratch2);
6471 deferred->Branch(ne);
6472
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006473 // Get the elements array from the receiver and check that it
6474 // is not a dictionary.
6475 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006476 if (FLAG_debug_code) {
6477 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
6478 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
6479 __ cmp(scratch2, ip);
6480 __ Assert(eq, "JSObject with fast elements map has slow elements");
6481 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006482
6483 // Check that key is within bounds. Use unsigned comparison to handle
6484 // negative keys.
6485 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006486 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006487 deferred->Branch(ls); // Unsigned less equal.
6488
6489 // Load and check that the result is not the hole (key is a smi).
6490 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6491 __ add(scratch1,
6492 scratch1,
6493 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006494 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006495 MemOperand(scratch1, key, LSL,
6496 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006497 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006498 deferred->Branch(eq);
6499
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006500 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006501 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006502 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006503 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6504 }
6505
6506 deferred->BindExit();
6507 }
6508}
6509
6510
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006511void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6512 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006513 // Generate inlined version of the keyed store if the code is in a loop
6514 // and the key is likely to be a smi.
6515 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6516 // Inline the keyed store.
6517 Comment cmnt(masm_, "[ Inlined store to keyed property");
6518
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006519 Register scratch1 = VirtualFrame::scratch0();
6520 Register scratch2 = VirtualFrame::scratch1();
6521 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006522
6523 // Counter will be decremented in the deferred code. Placed here to avoid
6524 // having it in the instruction stream below where patching will occur.
6525 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006526 scratch1, scratch2);
6527
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006528
6529
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006530 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006531 bool value_is_harmless = frame_->KnownSmiAt(0);
6532 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6533 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006534 Register value = frame_->PopToRegister();
6535 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006536 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006537 Register receiver = r2;
6538 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006539
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006540#ifdef DEBUG
6541 bool we_remembered_the_write_barrier = value_is_harmless;
6542#endif
6543
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006544 // The deferred code expects value, key and receiver in registers.
6545 DeferredReferenceSetKeyedValue* deferred =
6546 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006547
6548 // Check that the value is a smi. As this inlined code does not set the
6549 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006550 if (!value_is_harmless) {
6551 // If the value is not likely to be a Smi then let's test the fixed array
6552 // for new space instead. See below.
6553 if (wb_info == LIKELY_SMI) {
6554 __ tst(value, Operand(kSmiTagMask));
6555 deferred->Branch(ne);
6556#ifdef DEBUG
6557 we_remembered_the_write_barrier = true;
6558#endif
6559 }
6560 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006561
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006562 if (!key_is_smi) {
6563 // Check that the key is a smi.
6564 __ tst(key, Operand(kSmiTagMask));
6565 deferred->Branch(ne);
6566 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006567
6568 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006569 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006570 deferred->Branch(eq);
6571
6572 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006573 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006574 deferred->Branch(ne);
6575
6576 // Check that the key is within bounds. Both the key and the length of
6577 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006578 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6579 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006580 deferred->Branch(ls); // Unsigned less equal.
6581
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006582 // Get the elements array from the receiver.
6583 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6584 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6585 Label ok;
6586 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6587 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6588 __ tst(value, Operand(kSmiTagMask), ne);
6589 deferred->Branch(ne);
6590#ifdef DEBUG
6591 we_remembered_the_write_barrier = true;
6592#endif
6593 }
6594 // Check that the elements array is not a dictionary.
6595 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006596 // The following instructions are the part of the inlined store keyed
6597 // property code which can be patched. Therefore the exact number of
6598 // instructions generated need to be fixed, so the constant pool is blocked
6599 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006600 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006601#ifdef DEBUG
6602 Label check_inlined_codesize;
6603 masm_->bind(&check_inlined_codesize);
6604#endif
6605
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006606 // Read the fixed array map from the constant pool (not from the root
6607 // array) so that the value can be patched. When debugging, we patch this
6608 // comparison to always fail so that we will hit the IC call in the
6609 // deferred code which will allow the debugger to break for fast case
6610 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006611 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6612 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006613 deferred->Branch(ne);
6614
6615 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006616 __ add(scratch1, scratch1,
6617 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6618 __ str(value,
6619 MemOperand(scratch1, key, LSL,
6620 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006621
6622 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006623 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006624 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6625 }
6626
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006627 ASSERT(we_remembered_the_write_barrier);
6628
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006629 deferred->BindExit();
6630 } else {
6631 frame()->CallKeyedStoreIC();
6632 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006633}
6634
6635
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006636#ifdef DEBUG
6637bool CodeGenerator::HasValidEntryRegisters() { return true; }
6638#endif
6639
6640
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006641#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006642#define __ ACCESS_MASM(masm)
6643
ager@chromium.org7c537e22008-10-16 08:43:32 +00006644Handle<String> Reference::GetName() {
6645 ASSERT(type_ == NAMED);
6646 Property* property = expression_->AsProperty();
6647 if (property == NULL) {
6648 // Global variable reference treated as a named property reference.
6649 VariableProxy* proxy = expression_->AsVariableProxy();
6650 ASSERT(proxy->AsVariable() != NULL);
6651 ASSERT(proxy->AsVariable()->is_global());
6652 return proxy->name();
6653 } else {
6654 Literal* raw_name = property->key()->AsLiteral();
6655 ASSERT(raw_name != NULL);
6656 return Handle<String>(String::cast(*raw_name->handle()));
6657 }
6658}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006659
ager@chromium.org7c537e22008-10-16 08:43:32 +00006660
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006661void Reference::DupIfPersist() {
6662 if (persist_after_get_) {
6663 switch (type_) {
6664 case KEYED:
6665 cgen_->frame()->Dup2();
6666 break;
6667 case NAMED:
6668 cgen_->frame()->Dup();
6669 // Fall through.
6670 case UNLOADED:
6671 case ILLEGAL:
6672 case SLOT:
6673 // Do nothing.
6674 ;
6675 }
6676 } else {
6677 set_unloaded();
6678 }
6679}
6680
6681
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006682void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006683 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006684 ASSERT(!is_illegal());
6685 ASSERT(!cgen_->has_cc());
6686 MacroAssembler* masm = cgen_->masm();
6687 Property* property = expression_->AsProperty();
6688 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006689 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006690 }
6691
6692 switch (type_) {
6693 case SLOT: {
6694 Comment cmnt(masm, "[ Load from Slot");
6695 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6696 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006697 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006698 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006699 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006700 }
6701
ager@chromium.org7c537e22008-10-16 08:43:32 +00006702 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006703 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006704 bool is_global = var != NULL;
6705 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006706 Handle<String> name = GetName();
6707 DupIfPersist();
6708 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006709 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006710 }
6711
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006712 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006713 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006714 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006715 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006716 cgen_->frame()->EmitPush(r0);
6717 break;
6718 }
6719
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006720 default:
6721 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006722 }
6723}
6724
6725
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006726void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006727 ASSERT(!is_illegal());
6728 ASSERT(!cgen_->has_cc());
6729 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006730 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006731 Property* property = expression_->AsProperty();
6732 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006733 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006734 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006735
ager@chromium.org7c537e22008-10-16 08:43:32 +00006736 switch (type_) {
6737 case SLOT: {
6738 Comment cmnt(masm, "[ Store to Slot");
6739 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006740 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006741 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006742 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006743 }
6744
ager@chromium.org7c537e22008-10-16 08:43:32 +00006745 case NAMED: {
6746 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00006747 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006748 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00006749 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006750 break;
6751 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006752
ager@chromium.org7c537e22008-10-16 08:43:32 +00006753 case KEYED: {
6754 Comment cmnt(masm, "[ Store to keyed Property");
6755 Property* property = expression_->AsProperty();
6756 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006757 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006758 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006759 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006760 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006761 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006762 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00006763
6764 default:
6765 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006766 }
6767}
6768
6769
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006770void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006771 // Create a new closure from the given function info in new
6772 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006773 Label gc;
6774
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006775 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006776 __ pop(r3);
6777
6778 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006779 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006780 r0,
6781 r1,
6782 r2,
6783 &gc,
6784 TAG_OBJECT);
6785
6786 // Compute the function map in the current global context and set that
6787 // as the map of the allocated object.
6788 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6789 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
6790 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
6791 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
6792
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006793 // Initialize the rest of the function. We don't have to update the
6794 // write barrier because the allocated object is in new space.
6795 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
6796 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
6797 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
6798 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
6799 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
6800 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
6801 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
6802 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006803
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006804 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006805 __ Ret();
6806
6807 // Create a new closure through the slower runtime call.
6808 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006809 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006810 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006811}
6812
6813
6814void FastNewContextStub::Generate(MacroAssembler* masm) {
6815 // Try to allocate the context in new space.
6816 Label gc;
6817 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
6818
6819 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006820 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006821 r0,
6822 r1,
6823 r2,
6824 &gc,
6825 TAG_OBJECT);
6826
6827 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00006828 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006829
6830 // Setup the object header.
6831 __ LoadRoot(r2, Heap::kContextMapRootIndex);
6832 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006833 __ mov(r2, Operand(Smi::FromInt(length)));
6834 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006835
6836 // Setup the fixed slots.
6837 __ mov(r1, Operand(Smi::FromInt(0)));
6838 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
6839 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
6840 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
6841 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
6842
6843 // Copy the global object from the surrounding context.
6844 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6845 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
6846
6847 // Initialize the rest of the slots to undefined.
6848 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
6849 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
6850 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
6851 }
6852
6853 // Remove the on-stack argument and return.
6854 __ mov(cp, r0);
6855 __ pop();
6856 __ Ret();
6857
6858 // Need to collect. Call into runtime system.
6859 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006860 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006861}
6862
6863
ager@chromium.org5c838252010-02-19 08:53:10 +00006864void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
6865 // Stack layout on entry:
6866 //
6867 // [sp]: constant elements.
6868 // [sp + kPointerSize]: literal index.
6869 // [sp + (2 * kPointerSize)]: literals array.
6870
6871 // All sizes here are multiples of kPointerSize.
6872 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
6873 int size = JSArray::kSize + elements_size;
6874
6875 // Load boilerplate object into r3 and check if we need to create a
6876 // boilerplate.
6877 Label slow_case;
6878 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6879 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6880 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6881 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6882 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6883 __ cmp(r3, ip);
6884 __ b(eq, &slow_case);
6885
6886 // Allocate both the JS array and the elements array in one big
6887 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006888 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00006889 r0,
6890 r1,
6891 r2,
6892 &slow_case,
6893 TAG_OBJECT);
6894
6895 // Copy the JS array part.
6896 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
6897 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
6898 __ ldr(r1, FieldMemOperand(r3, i));
6899 __ str(r1, FieldMemOperand(r0, i));
6900 }
6901 }
6902
6903 if (length_ > 0) {
6904 // Get hold of the elements array of the boilerplate and setup the
6905 // elements pointer in the resulting object.
6906 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
6907 __ add(r2, r0, Operand(JSArray::kSize));
6908 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
6909
6910 // Copy the elements array.
6911 for (int i = 0; i < elements_size; i += kPointerSize) {
6912 __ ldr(r1, FieldMemOperand(r3, i));
6913 __ str(r1, FieldMemOperand(r2, i));
6914 }
6915 }
6916
6917 // Return and remove the on-stack parameters.
6918 __ add(sp, sp, Operand(3 * kPointerSize));
6919 __ Ret();
6920
6921 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006922 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00006923}
6924
6925
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006926// Takes a Smi and converts to an IEEE 64 bit floating point value in two
6927// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
6928// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
6929// scratch register. Destroys the source register. No GC occurs during this
6930// stub so you don't have to set up the frame.
6931class ConvertToDoubleStub : public CodeStub {
6932 public:
6933 ConvertToDoubleStub(Register result_reg_1,
6934 Register result_reg_2,
6935 Register source_reg,
6936 Register scratch_reg)
6937 : result1_(result_reg_1),
6938 result2_(result_reg_2),
6939 source_(source_reg),
6940 zeros_(scratch_reg) { }
6941
6942 private:
6943 Register result1_;
6944 Register result2_;
6945 Register source_;
6946 Register zeros_;
6947
6948 // Minor key encoding in 16 bits.
6949 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
6950 class OpBits: public BitField<Token::Value, 2, 14> {};
6951
6952 Major MajorKey() { return ConvertToDouble; }
6953 int MinorKey() {
6954 // Encode the parameters in a unique 16 bit value.
6955 return result1_.code() +
6956 (result2_.code() << 4) +
6957 (source_.code() << 8) +
6958 (zeros_.code() << 12);
6959 }
6960
6961 void Generate(MacroAssembler* masm);
6962
6963 const char* GetName() { return "ConvertToDoubleStub"; }
6964
6965#ifdef DEBUG
6966 void Print() { PrintF("ConvertToDoubleStub\n"); }
6967#endif
6968};
6969
6970
6971void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
6972#ifndef BIG_ENDIAN_FLOATING_POINT
6973 Register exponent = result1_;
6974 Register mantissa = result2_;
6975#else
6976 Register exponent = result2_;
6977 Register mantissa = result1_;
6978#endif
6979 Label not_special;
6980 // Convert from Smi to integer.
6981 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
6982 // Move sign bit from source to destination. This works because the sign bit
6983 // in the exponent word of the double has the same position and polarity as
6984 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006985 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006986 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
6987 // Subtract from 0 if source was negative.
6988 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006989
6990 // We have -1, 0 or 1, which we treat specially. Register source_ contains
6991 // absolute value: it is either equal to 1 (special case of -1 and 1),
6992 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006993 __ cmp(source_, Operand(1));
6994 __ b(gt, &not_special);
6995
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006996 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
6997 static const uint32_t exponent_word_for_1 =
6998 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006999 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007000 // 1, 0 and -1 all have 0 for the second word.
7001 __ mov(mantissa, Operand(0));
7002 __ Ret();
7003
7004 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007005 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007006 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007007 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007008 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007009 // We use mantissa as a scratch register here. Use a fudge factor to
7010 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7011 // that fit in the ARM's constant field.
7012 int fudge = 0x400;
7013 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7014 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007015 __ orr(exponent,
7016 exponent,
7017 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7018 // Shift up the source chopping the top bit off.
7019 __ add(zeros_, zeros_, Operand(1));
7020 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7021 __ mov(source_, Operand(source_, LSL, zeros_));
7022 // Compute lower part of fraction (last 12 bits).
7023 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7024 // And the top (top 20 bits).
7025 __ orr(exponent,
7026 exponent,
7027 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7028 __ Ret();
7029}
7030
7031
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007032// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007033void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007034 Label max_negative_int;
7035 // the_int_ has the answer which is a signed int32 but not a Smi.
7036 // We test for the special value that has a different exponent. This test
7037 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007038 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007039 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007040 __ b(eq, &max_negative_int);
7041 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7042 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7043 uint32_t non_smi_exponent =
7044 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7045 __ mov(scratch_, Operand(non_smi_exponent));
7046 // Set the sign bit in scratch_ if the value was negative.
7047 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7048 // Subtract from 0 if the value was negative.
7049 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7050 // We should be masking the implict first digit of the mantissa away here,
7051 // but it just ends up combining harmlessly with the last digit of the
7052 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7053 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7054 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7055 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7056 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7057 __ str(scratch_, FieldMemOperand(the_heap_number_,
7058 HeapNumber::kExponentOffset));
7059 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7060 __ str(scratch_, FieldMemOperand(the_heap_number_,
7061 HeapNumber::kMantissaOffset));
7062 __ Ret();
7063
7064 __ bind(&max_negative_int);
7065 // The max negative int32 is stored as a positive number in the mantissa of
7066 // a double because it uses a sign bit instead of using two's complement.
7067 // The actual mantissa bits stored are all 0 because the implicit most
7068 // significant 1 bit is not stored.
7069 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7070 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7071 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7072 __ mov(ip, Operand(0));
7073 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7074 __ Ret();
7075}
7076
7077
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007078// Handle the case where the lhs and rhs are the same object.
7079// Equality is almost reflexive (everything but NaN), so this is a test
7080// for "identity and not NaN".
7081static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7082 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007083 Condition cc,
7084 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007085 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007086 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007087 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007088 __ b(ne, &not_identical);
7089
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007090 // The two objects are identical. If we know that one of them isn't NaN then
7091 // we now know they test equal.
7092 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007093 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7094 // so we do the second best thing - test it ourselves.
7095 // They are both equal and they are not both Smis so both of them are not
7096 // Smis. If it's not a heap number, then return equal.
7097 if (cc == lt || cc == gt) {
7098 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007099 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007100 } else {
7101 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7102 __ b(eq, &heap_number);
7103 // Comparing JS objects with <=, >= is complicated.
7104 if (cc != eq) {
7105 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7106 __ b(ge, slow);
7107 // Normally here we fall through to return_equal, but undefined is
7108 // special: (undefined == undefined) == true, but
7109 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7110 if (cc == le || cc == ge) {
7111 __ cmp(r4, Operand(ODDBALL_TYPE));
7112 __ b(ne, &return_equal);
7113 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007114 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007115 __ b(ne, &return_equal);
7116 if (cc == le) {
7117 // undefined <= undefined should fail.
7118 __ mov(r0, Operand(GREATER));
7119 } else {
7120 // undefined >= undefined should fail.
7121 __ mov(r0, Operand(LESS));
7122 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007123 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007124 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007125 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007126 }
7127 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007128
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007129 __ bind(&return_equal);
7130 if (cc == lt) {
7131 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7132 } else if (cc == gt) {
7133 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7134 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007135 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007136 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007137 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007138
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007139 if (cc != eq || !never_nan_nan) {
7140 // For less and greater we don't have to check for NaN since the result of
7141 // x < x is false regardless. For the others here is some code to check
7142 // for NaN.
7143 if (cc != lt && cc != gt) {
7144 __ bind(&heap_number);
7145 // It is a heap number, so return non-equal if it's NaN and equal if it's
7146 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007147
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007148 // The representation of NaN values has all exponent bits (52..62) set,
7149 // and not all mantissa bits (0..51) clear.
7150 // Read top bits of double representation (second word of value).
7151 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7152 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007153 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7154 // NaNs have all-one exponents so they sign extend to -1.
7155 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007156 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007157
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007158 // Shift out flag and all exponent bits, retaining only mantissa.
7159 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7160 // Or with all low-bits of mantissa.
7161 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7162 __ orr(r0, r3, Operand(r2), SetCC);
7163 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007164 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7165 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7166 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007167 if (cc != eq) {
7168 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007169 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007170 if (cc == le) {
7171 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7172 } else {
7173 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7174 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007175 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007176 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007177 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007178 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007179 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007180
7181 __ bind(&not_identical);
7182}
7183
7184
7185// See comment at call site.
7186static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007187 Register lhs,
7188 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007189 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007190 Label* slow,
7191 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007192 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7193 (lhs.is(r1) && rhs.is(r0)));
7194
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007195 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007196 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007197 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007198
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007199 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007200 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007201 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007202 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007203 // succeed. Return non-equal
7204 // If rhs is r0 then there is already a non zero value in it.
7205 if (!rhs.is(r0)) {
7206 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7207 }
7208 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007209 } else {
7210 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7211 // the runtime.
7212 __ b(ne, slow);
7213 }
7214
ager@chromium.orgb5737492010-07-15 09:29:43 +00007215 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007216 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007217 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007218 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007219 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007220 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007221 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007222 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007223 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007224 __ push(lr);
7225 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007226 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007227 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7228 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007229 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007230 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007231 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007232 }
7233
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007234 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007235 // since it's a smi.
7236 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007237
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007238 __ bind(&rhs_is_smi);
7239 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007240 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007241 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007242 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007243 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007244 // If lhs is r0 then there is already a non zero value in it.
7245 if (!lhs.is(r0)) {
7246 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7247 }
7248 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007249 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007250 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007251 // the runtime.
7252 __ b(ne, slow);
7253 }
7254
ager@chromium.orgb5737492010-07-15 09:29:43 +00007255 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007256 if (CpuFeatures::IsSupported(VFP3)) {
7257 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007258 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007259 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007260 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007261 // Convert rhs to a double in d6 .
7262 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007263 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007264 __ push(lr);
7265 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007266 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007267 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007268 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007269 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7270 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007271 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007272 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007273 // Fall through to both_loaded_as_doubles.
7274}
7275
7276
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007277void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007278 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007279 Register rhs_exponent = exp_first ? r0 : r1;
7280 Register lhs_exponent = exp_first ? r2 : r3;
7281 Register rhs_mantissa = exp_first ? r1 : r0;
7282 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007283 Label one_is_nan, neither_is_nan;
7284
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007285 __ Sbfx(r4,
7286 lhs_exponent,
7287 HeapNumber::kExponentShift,
7288 HeapNumber::kExponentBits);
7289 // NaNs have all-one exponents so they sign extend to -1.
7290 __ cmp(r4, Operand(-1));
7291 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007292 __ mov(r4,
7293 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7294 SetCC);
7295 __ b(ne, &one_is_nan);
7296 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007297 __ b(ne, &one_is_nan);
7298
7299 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007300 __ Sbfx(r4,
7301 rhs_exponent,
7302 HeapNumber::kExponentShift,
7303 HeapNumber::kExponentBits);
7304 // NaNs have all-one exponents so they sign extend to -1.
7305 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007306 __ b(ne, &neither_is_nan);
7307 __ mov(r4,
7308 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7309 SetCC);
7310 __ b(ne, &one_is_nan);
7311 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007312 __ b(eq, &neither_is_nan);
7313
7314 __ bind(&one_is_nan);
7315 // NaN comparisons always fail.
7316 // Load whatever we need in r0 to make the comparison fail.
7317 if (cc == lt || cc == le) {
7318 __ mov(r0, Operand(GREATER));
7319 } else {
7320 __ mov(r0, Operand(LESS));
7321 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007322 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007323
7324 __ bind(&neither_is_nan);
7325}
7326
7327
7328// See comment at call site.
7329static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7330 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007331 Register rhs_exponent = exp_first ? r0 : r1;
7332 Register lhs_exponent = exp_first ? r2 : r3;
7333 Register rhs_mantissa = exp_first ? r1 : r0;
7334 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007335
7336 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7337 if (cc == eq) {
7338 // Doubles are not equal unless they have the same bit pattern.
7339 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007340 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7341 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007342 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007343 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007345 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007346 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007347 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007348
7349 // Exponents are unequal. The only way we can return that the numbers
7350 // are equal is if one is -0 and the other is 0. We already dealt
7351 // with the case where both are -0 or both are 0.
7352 // We start by seeing if the mantissas (that are equal) or the bottom
7353 // 31 bits of the rhs exponent are non-zero. If so we return not
7354 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007355 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007356 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007357 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007358 // Now they are equal if and only if the lhs exponent is zero in its
7359 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007360 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007361 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007362 } else {
7363 // Call a native function to do a comparison between two non-NaNs.
7364 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007365 __ push(lr);
7366 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7367 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7368 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007369 }
7370}
7371
7372
7373// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007374static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7375 Register lhs,
7376 Register rhs) {
7377 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7378 (lhs.is(r1) && rhs.is(r0)));
7379
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007380 // If either operand is a JSObject or an oddball value, then they are
7381 // not equal since their pointers are different.
7382 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007383 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007384 Label first_non_object;
7385 // Get the type of the first operand into r2 and compare it with
7386 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007387 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007388 __ b(lt, &first_non_object);
7389
7390 // Return non-zero (r0 is not zero)
7391 Label return_not_equal;
7392 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007393 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007394
7395 __ bind(&first_non_object);
7396 // Check for oddballs: true, false, null, undefined.
7397 __ cmp(r2, Operand(ODDBALL_TYPE));
7398 __ b(eq, &return_not_equal);
7399
ager@chromium.orgb5737492010-07-15 09:29:43 +00007400 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007401 __ b(ge, &return_not_equal);
7402
7403 // Check for oddballs: true, false, null, undefined.
7404 __ cmp(r3, Operand(ODDBALL_TYPE));
7405 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007406
7407 // Now that we have the types we might as well check for symbol-symbol.
7408 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007409 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7410 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007411 __ and_(r2, r2, Operand(r3));
7412 __ tst(r2, Operand(kIsSymbolMask));
7413 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007414}
7415
7416
7417// See comment at call site.
7418static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007419 Register lhs,
7420 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007421 Label* both_loaded_as_doubles,
7422 Label* not_heap_numbers,
7423 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007424 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7425 (lhs.is(r1) && rhs.is(r0)));
7426
7427 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007428 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007429 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007430 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007431 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7432
7433 // Both are heap numbers. Load them up then jump to the code we have
7434 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007435 if (CpuFeatures::IsSupported(VFP3)) {
7436 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007437 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007438 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007439 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007440 __ vldr(d7, r7, HeapNumber::kValueOffset);
7441 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007442 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7443 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007444 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007445 __ jmp(both_loaded_as_doubles);
7446}
7447
7448
7449// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007450static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007451 Register lhs,
7452 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007453 Label* possible_strings,
7454 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007455 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7456 (lhs.is(r1) && rhs.is(r0)));
7457
7458 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007459 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007460 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007461 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007462 __ tst(r2, Operand(kIsNotStringMask));
7463 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007464 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007465 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007466 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007467 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007468 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007469 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007470
7471 // Both are symbols. We already checked they weren't the same pointer
7472 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007473 __ mov(r0, Operand(NOT_EQUAL));
7474 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007475
7476 __ bind(&object_test);
7477 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7478 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007479 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007480 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007481 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007482 // are not equal, since they are different objects and an object is not
7483 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007484 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007485 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7486 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7487 __ and_(r0, r2, Operand(r3));
7488 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7489 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007490 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007491}
7492
7493
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007494void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7495 Register object,
7496 Register result,
7497 Register scratch1,
7498 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007499 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007500 bool object_is_smi,
7501 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007502 // Use of registers. Register result is used as a temporary.
7503 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007504 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007505
7506 // Load the number string cache.
7507 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7508
7509 // Make the hash mask from the length of the number string cache. It
7510 // contains two elements (number and string) for each cache entry.
7511 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007512 // Divide length by two (length is a smi).
7513 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007514 __ sub(mask, mask, Operand(1)); // Make mask.
7515
7516 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007517 // number string cache for smis is just the smi value, and the hash for
7518 // doubles is the xor of the upper and lower words. See
7519 // Heap::GetNumberStringCache.
7520 Label is_smi;
7521 Label load_result_from_cache;
7522 if (!object_is_smi) {
7523 __ BranchOnSmi(object, &is_smi);
7524 if (CpuFeatures::IsSupported(VFP3)) {
7525 CpuFeatures::Scope scope(VFP3);
7526 __ CheckMap(object,
7527 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007528 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007529 not_found,
7530 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007531
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007532 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007533 __ add(scratch1,
7534 object,
7535 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7536 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7537 __ eor(scratch1, scratch1, Operand(scratch2));
7538 __ and_(scratch1, scratch1, Operand(mask));
7539
7540 // Calculate address of entry in string cache: each entry consists
7541 // of two pointer sized fields.
7542 __ add(scratch1,
7543 number_string_cache,
7544 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7545
7546 Register probe = mask;
7547 __ ldr(probe,
7548 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7549 __ BranchOnSmi(probe, not_found);
7550 __ sub(scratch2, object, Operand(kHeapObjectTag));
7551 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7552 __ sub(probe, probe, Operand(kHeapObjectTag));
7553 __ vldr(d1, probe, HeapNumber::kValueOffset);
7554 __ vcmp(d0, d1);
7555 __ vmrs(pc);
7556 __ b(ne, not_found); // The cache did not contain this value.
7557 __ b(&load_result_from_cache);
7558 } else {
7559 __ b(not_found);
7560 }
7561 }
7562
7563 __ bind(&is_smi);
7564 Register scratch = scratch1;
7565 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007566 // Calculate address of entry in string cache: each entry consists
7567 // of two pointer sized fields.
7568 __ add(scratch,
7569 number_string_cache,
7570 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7571
7572 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007573 Register probe = mask;
7574 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7575 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007576 __ b(ne, not_found);
7577
7578 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007579 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007580 __ ldr(result,
7581 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007582 __ IncrementCounter(&Counters::number_to_string_native,
7583 1,
7584 scratch1,
7585 scratch2);
7586}
7587
7588
7589void NumberToStringStub::Generate(MacroAssembler* masm) {
7590 Label runtime;
7591
7592 __ ldr(r1, MemOperand(sp, 0));
7593
7594 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007595 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007596 __ add(sp, sp, Operand(1 * kPointerSize));
7597 __ Ret();
7598
7599 __ bind(&runtime);
7600 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007601 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007602}
7603
7604
ager@chromium.orgac091b72010-05-05 07:34:42 +00007605void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007606 __ add(offset_, object_, Operand(offset_));
7607 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007608 __ Ret();
7609}
7610
7611
ager@chromium.orgb5737492010-07-15 09:29:43 +00007612// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007613// On exit r0 is 0, positive or negative to indicate the result of
7614// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007615void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007616 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7617 (lhs_.is(r1) && rhs_.is(r0)));
7618
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007619 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007620 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007621
7622 // NOTICE! This code is only reached after a smi-fast-case check, so
7623 // it is certain that at least one operand isn't a smi.
7624
7625 // Handle the case where the objects are identical. Either returns the answer
7626 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007627 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007628
7629 // If either is a Smi (we know that not both are), then they can only
7630 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007631 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007632 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007633 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007634 __ tst(r2, Operand(kSmiTagMask));
7635 __ b(ne, &not_smis);
7636 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7637 // 1) Return the answer.
7638 // 2) Go to slow.
7639 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007640 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007641 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007642 // comparison. If VFP3 is supported the double values of the numbers have
7643 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7644 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007645 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007646
7647 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007648 // The arguments have been converted to doubles and stored in d6 and d7, if
7649 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007650 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007651 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007652 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007653 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007654 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007655 __ vcmp(d7, d6);
7656 __ vmrs(pc); // Move vector status bits to normal status bits.
7657 Label nan;
7658 __ b(vs, &nan);
7659 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7660 __ mov(r0, Operand(LESS), LeaveCC, lt);
7661 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007662 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007663
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007664 __ bind(&nan);
7665 // If one of the sides was a NaN then the v flag is set. Load r0 with
7666 // whatever it takes to make the comparison fail, since comparisons with NaN
7667 // always fail.
7668 if (cc_ == lt || cc_ == le) {
7669 __ mov(r0, Operand(GREATER));
7670 } else {
7671 __ mov(r0, Operand(LESS));
7672 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007673 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007674 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007675 // Checks for NaN in the doubles we have loaded. Can return the answer or
7676 // fall through if neither is a NaN. Also binds lhs_not_nan.
7677 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007678 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7679 // answer. Never falls through.
7680 EmitTwoNonNanDoubleComparison(masm, cc_);
7681 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007682
7683 __ bind(&not_smis);
7684 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007685 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007686 if (strict_) {
7687 // This returns non-equal for some object types, or falls through if it
7688 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007689 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007690 }
7691
7692 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007693 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007694 // Check for heap-number-heap-number comparison. Can jump to slow case,
7695 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7696 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007697 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007698 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007699 lhs_,
7700 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007701 &both_loaded_as_doubles,
7702 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007703 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007704
7705 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007706 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7707 // symbols.
7708 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007709 // Returns an answer for two symbols or two detectable objects.
7710 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007711 // Assumes that r2 is the type of rhs_ on entry.
7712 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007713 }
7714
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007715 // Check for both being sequential ASCII strings, and inline if that is the
7716 // case.
7717 __ bind(&flat_string_check);
7718
ager@chromium.orgb5737492010-07-15 09:29:43 +00007719 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007720
7721 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7722 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007723 lhs_,
7724 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007725 r2,
7726 r3,
7727 r4,
7728 r5);
7729 // Never falls through to here.
7730
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007731 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007732
ager@chromium.orgb5737492010-07-15 09:29:43 +00007733 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007734 // Figure out which native to call and setup the arguments.
7735 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007736 if (cc_ == eq) {
7737 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7738 } else {
7739 native = Builtins::COMPARE;
7740 int ncr; // NaN compare result
7741 if (cc_ == lt || cc_ == le) {
7742 ncr = GREATER;
7743 } else {
7744 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7745 ncr = LESS;
7746 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007747 __ mov(r0, Operand(Smi::FromInt(ncr)));
7748 __ push(r0);
7749 }
7750
7751 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7752 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007753 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007754}
7755
7756
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00007757// We fall into this code if the operands were Smis, but the result was
7758// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007759// the operands were not both Smi. The operands are in r0 and r1. In order
7760// to call the C-implemented binary fp operation routines we need to end up
7761// with the double precision floating point operands in r0 and r1 (for the
7762// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00007763void GenericBinaryOpStub::HandleBinaryOpSlowCases(
7764 MacroAssembler* masm,
7765 Label* not_smi,
7766 Register lhs,
7767 Register rhs,
7768 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007769 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00007770 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007771
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007772 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007773 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00007774
7775 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007776 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7777
ager@chromium.org357bf652010-04-12 11:30:10 +00007778 // Smi-smi case (overflow).
7779 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007780 // The new heap number is in r5. r3 and r7 are scratch.
7781 __ AllocateHeapNumber(
7782 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007783
7784 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
7785 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007786 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007787 CpuFeatures::Scope scope(VFP3);
7788 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
7789 __ vmov(s15, r7);
7790 __ vcvt_f64_s32(d7, s15);
7791 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
7792 __ vmov(s13, r7);
7793 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007794 if (!use_fp_registers) {
7795 __ vmov(r2, r3, d7);
7796 __ vmov(r0, r1, d6);
7797 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007798 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007799 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007800 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007801 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007802 __ push(lr);
7803 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007804 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007805 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007806 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007807 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
7808 __ pop(lr);
7809 }
7810 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007811 }
7812
ager@chromium.org357bf652010-04-12 11:30:10 +00007813 // We branch here if at least one of r0 and r1 is not a Smi.
7814 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007815 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007816
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007817 // After this point we have the left hand side in r1 and the right hand side
7818 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007819 if (lhs.is(r0)) {
7820 __ Swap(r0, r1, ip);
7821 }
7822
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007823 // The type transition also calculates the answer.
7824 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007825
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007826 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007827 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
7828 switch (op_) {
7829 case Token::ADD:
7830 case Token::SUB:
7831 case Token::MUL:
7832 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007833 GenerateTypeTransition(masm); // Tail call.
7834 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00007835 break;
7836
7837 default:
7838 break;
7839 }
7840 }
7841
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007842 if (generate_code_to_calculate_answer) {
7843 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
7844 if (mode_ == NO_OVERWRITE) {
7845 // In the case where there is no chance of an overwritable float we may
7846 // as well do the allocation immediately while r0 and r1 are untouched.
7847 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007848 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007849
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007850 // Move r0 to a double in r2-r3.
7851 __ tst(r0, Operand(kSmiTagMask));
7852 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
7853 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7854 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7855 __ cmp(r4, heap_number_map);
7856 __ b(ne, &slow);
7857 if (mode_ == OVERWRITE_RIGHT) {
7858 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007859 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007860 if (use_fp_registers) {
7861 CpuFeatures::Scope scope(VFP3);
7862 // Load the double from tagged HeapNumber r0 to d7.
7863 __ sub(r7, r0, Operand(kHeapObjectTag));
7864 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00007865 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007866 // Calling convention says that second double is in r2 and r3.
7867 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007868 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007869 __ jmp(&finished_loading_r0);
7870 __ bind(&r0_is_smi);
7871 if (mode_ == OVERWRITE_RIGHT) {
7872 // We can't overwrite a Smi so get address of new heap number into r5.
7873 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7874 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007875
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007876 if (CpuFeatures::IsSupported(VFP3)) {
7877 CpuFeatures::Scope scope(VFP3);
7878 // Convert smi in r0 to double in d7.
7879 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
7880 __ vmov(s15, r7);
7881 __ vcvt_f64_s32(d7, s15);
7882 if (!use_fp_registers) {
7883 __ vmov(r2, r3, d7);
7884 }
7885 } else {
7886 // Write Smi from r0 to r3 and r2 in double format.
7887 __ mov(r7, Operand(r0));
7888 ConvertToDoubleStub stub3(r3, r2, r7, r4);
7889 __ push(lr);
7890 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
7891 __ pop(lr);
7892 }
7893
7894 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
7895 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
7896 Label r1_is_not_smi;
7897 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
7898 __ tst(r1, Operand(kSmiTagMask));
7899 __ b(ne, &r1_is_not_smi);
7900 GenerateTypeTransition(masm); // Tail call.
7901 }
7902
7903 __ bind(&finished_loading_r0);
7904
7905 // Move r1 to a double in r0-r1.
7906 __ tst(r1, Operand(kSmiTagMask));
7907 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
7908 __ bind(&r1_is_not_smi);
7909 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
7910 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7911 __ cmp(r4, heap_number_map);
7912 __ b(ne, &slow);
7913 if (mode_ == OVERWRITE_LEFT) {
7914 __ mov(r5, Operand(r1)); // Overwrite this heap number.
7915 }
7916 if (use_fp_registers) {
7917 CpuFeatures::Scope scope(VFP3);
7918 // Load the double from tagged HeapNumber r1 to d6.
7919 __ sub(r7, r1, Operand(kHeapObjectTag));
7920 __ vldr(d6, r7, HeapNumber::kValueOffset);
7921 } else {
7922 // Calling convention says that first double is in r0 and r1.
7923 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
7924 }
7925 __ jmp(&finished_loading_r1);
7926 __ bind(&r1_is_smi);
7927 if (mode_ == OVERWRITE_LEFT) {
7928 // We can't overwrite a Smi so get address of new heap number into r5.
7929 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7930 }
7931
7932 if (CpuFeatures::IsSupported(VFP3)) {
7933 CpuFeatures::Scope scope(VFP3);
7934 // Convert smi in r1 to double in d6.
7935 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
7936 __ vmov(s13, r7);
7937 __ vcvt_f64_s32(d6, s13);
7938 if (!use_fp_registers) {
7939 __ vmov(r0, r1, d6);
7940 }
7941 } else {
7942 // Write Smi from r1 to r1 and r0 in double format.
7943 __ mov(r7, Operand(r1));
7944 ConvertToDoubleStub stub4(r1, r0, r7, r9);
7945 __ push(lr);
7946 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
7947 __ pop(lr);
7948 }
7949
7950 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00007951 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007952
7953 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
7954 __ bind(&do_the_call);
7955 // If we are inlining the operation using VFP3 instructions for
7956 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
7957 if (use_fp_registers) {
7958 CpuFeatures::Scope scope(VFP3);
7959 // ARMv7 VFP3 instructions to implement
7960 // double precision, add, subtract, multiply, divide.
7961
7962 if (Token::MUL == op_) {
7963 __ vmul(d5, d6, d7);
7964 } else if (Token::DIV == op_) {
7965 __ vdiv(d5, d6, d7);
7966 } else if (Token::ADD == op_) {
7967 __ vadd(d5, d6, d7);
7968 } else if (Token::SUB == op_) {
7969 __ vsub(d5, d6, d7);
7970 } else {
7971 UNREACHABLE();
7972 }
7973 __ sub(r0, r5, Operand(kHeapObjectTag));
7974 __ vstr(d5, r0, HeapNumber::kValueOffset);
7975 __ add(r0, r0, Operand(kHeapObjectTag));
7976 __ mov(pc, lr);
7977 } else {
7978 // If we did not inline the operation, then the arguments are in:
7979 // r0: Left value (least significant part of mantissa).
7980 // r1: Left value (sign, exponent, top of mantissa).
7981 // r2: Right value (least significant part of mantissa).
7982 // r3: Right value (sign, exponent, top of mantissa).
7983 // r5: Address of heap number for result.
7984
7985 __ push(lr); // For later.
7986 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
7987 // Call C routine that may not cause GC or other trouble. r5 is callee
7988 // save.
7989 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
7990 // Store answer in the overwritable heap number.
7991 #if !defined(USE_ARM_EABI)
7992 // Double returned in fp coprocessor register 0 and 1, encoded as
7993 // register cr8. Offsets must be divisible by 4 for coprocessor so we
7994 // need to substract the tag from r5.
7995 __ sub(r4, r5, Operand(kHeapObjectTag));
7996 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
7997 #else
7998 // Double returned in registers 0 and 1.
7999 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8000 #endif
8001 __ mov(r0, Operand(r5));
8002 // And we are done.
8003 __ pop(pc);
8004 }
8005 }
8006 }
8007
8008 if (!generate_code_to_calculate_answer &&
8009 !slow_reverse.is_linked() &&
8010 !slow.is_linked()) {
8011 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008012 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008013
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008014 if (lhs.is(r0)) {
8015 __ b(&slow);
8016 __ bind(&slow_reverse);
8017 __ Swap(r0, r1, ip);
8018 }
8019
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008020 heap_number_map = no_reg; // Don't use this any more from here on.
8021
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008022 // We jump to here if something goes wrong (one param is not a number of any
8023 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008024 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008025
8026 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008027 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008028
ager@chromium.org357bf652010-04-12 11:30:10 +00008029 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008030 // Test for string arguments before calling runtime.
8031 // r1 : first argument
8032 // r0 : second argument
8033 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008034 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008035
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008036 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008037 __ tst(r1, Operand(kSmiTagMask));
8038 __ b(eq, &not_string1);
8039 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8040 __ b(ge, &not_string1);
8041
8042 // First argument is a a string, test second.
8043 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008044 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008045 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8046 __ b(ge, &string1);
8047
8048 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008049 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8050 __ TailCallStub(&string_add_stub);
8051
8052 __ bind(&string1_smi2);
8053 // First argument is a string, second is a smi. Try to lookup the number
8054 // string for the smi in the number string cache.
8055 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008056 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008057
8058 // Replace second argument on stack and tailcall string add stub to make
8059 // the result.
8060 __ str(r2, MemOperand(sp, 0));
8061 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008062
8063 // Only first argument is a string.
8064 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008065 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8066
8067 // First argument was not a string, test second.
8068 __ bind(&not_string1);
8069 __ tst(r0, Operand(kSmiTagMask));
8070 __ b(eq, &not_strings);
8071 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8072 __ b(ge, &not_strings);
8073
8074 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008075 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8076
8077 __ bind(&not_strings);
8078 }
8079
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008080 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008081}
8082
8083
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008084// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008085// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008086// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8087// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008088// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8089// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008090static void GetInt32(MacroAssembler* masm,
8091 Register source,
8092 Register dest,
8093 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008094 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008095 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008096 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008097 // Get exponent word.
8098 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8099 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008100 __ Ubfx(scratch2,
8101 scratch,
8102 HeapNumber::kExponentShift,
8103 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008104 // Load dest with zero. We use this either for the final shift or
8105 // for the answer.
8106 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008107 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008108 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8109 // the exponent that we are fastest at and also the highest exponent we can
8110 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008111 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8112 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8113 // split it up to avoid a constant pool entry. You can't do that in general
8114 // for cmp because of the overflow flag, but we know the exponent is in the
8115 // range 0-2047 so there is no overflow.
8116 int fudge_factor = 0x400;
8117 __ sub(scratch2, scratch2, Operand(fudge_factor));
8118 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008119 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8120 __ b(eq, &right_exponent);
8121 // If the exponent is higher than that then go to slow case. This catches
8122 // numbers that don't fit in a signed int32, infinities and NaNs.
8123 __ b(gt, slow);
8124
8125 // We know the exponent is smaller than 30 (biased). If it is less than
8126 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8127 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008128 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8129 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008130 // Dest already has a Smi zero.
8131 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008132 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008133 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8134 // get how much to shift down.
8135 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008136 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008137 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008138 if (CpuFeatures::IsSupported(VFP3)) {
8139 CpuFeatures::Scope scope(VFP3);
8140 // ARMv7 VFP3 instructions implementing double precision to integer
8141 // conversion using round to zero.
8142 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008143 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008144 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008145 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008146 } else {
8147 // Get the top bits of the mantissa.
8148 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8149 // Put back the implicit 1.
8150 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8151 // Shift up the mantissa bits to take up the space the exponent used to
8152 // take. We just orred in the implicit bit so that took care of one and
8153 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8154 // distance.
8155 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8156 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8157 // Put sign in zero flag.
8158 __ tst(scratch, Operand(HeapNumber::kSignMask));
8159 // Get the second half of the double. For some exponents we don't
8160 // actually need this because the bits get shifted out again, but
8161 // it's probably slower to test than just to do it.
8162 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8163 // Shift down 22 bits to get the last 10 bits.
8164 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8165 // Move down according to the exponent.
8166 __ mov(dest, Operand(scratch, LSR, dest));
8167 // Fix sign if sign bit was set.
8168 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8169 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008170 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008171}
8172
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008173// For bitwise ops where the inputs are not both Smis we here try to determine
8174// whether both inputs are either Smis or at least heap numbers that can be
8175// represented by a 32 bit signed value. We truncate towards zero as required
8176// by the ES spec. If this is the case we do the bitwise op and see if the
8177// result is a Smi. If so, great, otherwise we try to find a heap number to
8178// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008179// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8180void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8181 Register lhs,
8182 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008183 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008184 Label rhs_is_smi, lhs_is_smi;
8185 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008186
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008187 Register heap_number_map = r6;
8188 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8189
ager@chromium.org357bf652010-04-12 11:30:10 +00008190 __ tst(lhs, Operand(kSmiTagMask));
8191 __ 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 +00008192 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8193 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008194 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008195 GetInt32(masm, lhs, r3, r5, r4, &slow);
8196 __ jmp(&done_checking_lhs);
8197 __ bind(&lhs_is_smi);
8198 __ mov(r3, Operand(lhs, ASR, 1));
8199 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008200
ager@chromium.org357bf652010-04-12 11:30:10 +00008201 __ tst(rhs, Operand(kSmiTagMask));
8202 __ 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 +00008203 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8204 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008205 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008206 GetInt32(masm, rhs, r2, r5, r4, &slow);
8207 __ jmp(&done_checking_rhs);
8208 __ bind(&rhs_is_smi);
8209 __ mov(r2, Operand(rhs, ASR, 1));
8210 __ bind(&done_checking_rhs);
8211
8212 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008213
8214 // r0 and r1: Original operands (Smi or heap numbers).
8215 // r2 and r3: Signed int32 operands.
8216 switch (op_) {
8217 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8218 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8219 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8220 case Token::SAR:
8221 // Use only the 5 least significant bits of the shift count.
8222 __ and_(r2, r2, Operand(0x1f));
8223 __ mov(r2, Operand(r3, ASR, r2));
8224 break;
8225 case Token::SHR:
8226 // Use only the 5 least significant bits of the shift count.
8227 __ and_(r2, r2, Operand(0x1f));
8228 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8229 // SHR is special because it is required to produce a positive answer.
8230 // The code below for writing into heap numbers isn't capable of writing
8231 // the register as an unsigned int so we go to slow case if we hit this
8232 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008233 if (CpuFeatures::IsSupported(VFP3)) {
8234 __ b(mi, &result_not_a_smi);
8235 } else {
8236 __ b(mi, &slow);
8237 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008238 break;
8239 case Token::SHL:
8240 // Use only the 5 least significant bits of the shift count.
8241 __ and_(r2, r2, Operand(0x1f));
8242 __ mov(r2, Operand(r3, LSL, r2));
8243 break;
8244 default: UNREACHABLE();
8245 }
8246 // check that the *signed* result fits in a smi
8247 __ add(r3, r2, Operand(0x40000000), SetCC);
8248 __ b(mi, &result_not_a_smi);
8249 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8250 __ Ret();
8251
8252 Label have_to_allocate, got_a_heap_number;
8253 __ bind(&result_not_a_smi);
8254 switch (mode_) {
8255 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008256 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008257 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008258 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008259 break;
8260 }
8261 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008262 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008263 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008264 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008265 break;
8266 }
8267 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008268 // Get a new heap number in r5. r4 and r7 are scratch.
8269 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008270 }
8271 default: break;
8272 }
8273 __ bind(&got_a_heap_number);
8274 // r2: Answer as signed int32.
8275 // r5: Heap number to write answer into.
8276
8277 // Nothing can go wrong now, so move the heap number to r0, which is the
8278 // result.
8279 __ mov(r0, Operand(r5));
8280
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008281 if (CpuFeatures::IsSupported(VFP3)) {
8282 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8283 CpuFeatures::Scope scope(VFP3);
8284 __ vmov(s0, r2);
8285 if (op_ == Token::SHR) {
8286 __ vcvt_f64_u32(d0, s0);
8287 } else {
8288 __ vcvt_f64_s32(d0, s0);
8289 }
8290 __ sub(r3, r0, Operand(kHeapObjectTag));
8291 __ vstr(d0, r3, HeapNumber::kValueOffset);
8292 __ Ret();
8293 } else {
8294 // Tail call that writes the int32 in r2 to the heap number in r0, using
8295 // r3 as scratch. r0 is preserved and returned.
8296 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8297 __ TailCallStub(&stub);
8298 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008299
8300 if (mode_ != NO_OVERWRITE) {
8301 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008302 // Get a new heap number in r5. r4 and r7 are scratch.
8303 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008304 __ jmp(&got_a_heap_number);
8305 }
8306
8307 // If all else failed then we go to the runtime system.
8308 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008309 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008310 switch (op_) {
8311 case Token::BIT_OR:
8312 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8313 break;
8314 case Token::BIT_AND:
8315 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8316 break;
8317 case Token::BIT_XOR:
8318 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8319 break;
8320 case Token::SAR:
8321 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8322 break;
8323 case Token::SHR:
8324 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8325 break;
8326 case Token::SHL:
8327 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8328 break;
8329 default:
8330 UNREACHABLE();
8331 }
8332}
8333
8334
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008335// Can we multiply by x with max two shifts and an add.
8336// This answers yes to all integers from 2 to 10.
8337static bool IsEasyToMultiplyBy(int x) {
8338 if (x < 2) return false; // Avoid special cases.
8339 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8340 if (IsPowerOf2(x)) return true; // Simple shift.
8341 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8342 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8343 return false;
8344}
8345
8346
8347// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8348// Source and destination may be the same register. This routine does
8349// not set carry and overflow the way a mul instruction would.
8350static void MultiplyByKnownInt(MacroAssembler* masm,
8351 Register source,
8352 Register destination,
8353 int known_int) {
8354 if (IsPowerOf2(known_int)) {
8355 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8356 } else if (PopCountLessThanEqual2(known_int)) {
8357 int first_bit = BitPosition(known_int);
8358 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8359 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8360 if (first_bit != 0) {
8361 __ mov(destination, Operand(destination, LSL, first_bit));
8362 }
8363 } else {
8364 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8365 int the_bit = BitPosition(known_int + 1);
8366 __ rsb(destination, source, Operand(source, LSL, the_bit));
8367 }
8368}
8369
8370
8371// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8372// a register for the cases where it doesn't know a good trick, and may deliver
8373// a result that needs shifting.
8374static void MultiplyByKnownInt2(
8375 MacroAssembler* masm,
8376 Register result,
8377 Register source,
8378 Register known_int_register, // Smi tagged.
8379 int known_int,
8380 int* required_shift) { // Including Smi tag shift
8381 switch (known_int) {
8382 case 3:
8383 __ add(result, source, Operand(source, LSL, 1));
8384 *required_shift = 1;
8385 break;
8386 case 5:
8387 __ add(result, source, Operand(source, LSL, 2));
8388 *required_shift = 1;
8389 break;
8390 case 6:
8391 __ add(result, source, Operand(source, LSL, 1));
8392 *required_shift = 2;
8393 break;
8394 case 7:
8395 __ rsb(result, source, Operand(source, LSL, 3));
8396 *required_shift = 1;
8397 break;
8398 case 9:
8399 __ add(result, source, Operand(source, LSL, 3));
8400 *required_shift = 1;
8401 break;
8402 case 10:
8403 __ add(result, source, Operand(source, LSL, 2));
8404 *required_shift = 2;
8405 break;
8406 default:
8407 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8408 __ mul(result, source, known_int_register);
8409 *required_shift = 0;
8410 }
8411}
8412
8413
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008414// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8415// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8416// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8417// number from 0 to mask. On exit the 'eq' condition flags are set if the
8418// answer is exactly the mask.
8419void IntegerModStub::DigitSum(MacroAssembler* masm,
8420 Register lhs,
8421 int mask,
8422 int shift,
8423 Label* entry) {
8424 ASSERT(mask > 0);
8425 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8426 Label loop;
8427 __ bind(&loop);
8428 __ and_(ip, lhs, Operand(mask));
8429 __ add(lhs, ip, Operand(lhs, LSR, shift));
8430 __ bind(entry);
8431 __ cmp(lhs, Operand(mask));
8432 __ b(gt, &loop);
8433}
8434
8435
8436void IntegerModStub::DigitSum(MacroAssembler* masm,
8437 Register lhs,
8438 Register scratch,
8439 int mask,
8440 int shift1,
8441 int shift2,
8442 Label* entry) {
8443 ASSERT(mask > 0);
8444 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8445 Label loop;
8446 __ bind(&loop);
8447 __ bic(scratch, lhs, Operand(mask));
8448 __ and_(ip, lhs, Operand(mask));
8449 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8450 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8451 __ bind(entry);
8452 __ cmp(lhs, Operand(mask));
8453 __ b(gt, &loop);
8454}
8455
8456
8457// Splits the number into two halves (bottom half has shift bits). The top
8458// half is subtracted from the bottom half. If the result is negative then
8459// rhs is added.
8460void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8461 Register lhs,
8462 int shift,
8463 int rhs) {
8464 int mask = (1 << shift) - 1;
8465 __ and_(ip, lhs, Operand(mask));
8466 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8467 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8468}
8469
8470
8471void IntegerModStub::ModReduce(MacroAssembler* masm,
8472 Register lhs,
8473 int max,
8474 int denominator) {
8475 int limit = denominator;
8476 while (limit * 2 <= max) limit *= 2;
8477 while (limit >= denominator) {
8478 __ cmp(lhs, Operand(limit));
8479 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8480 limit >>= 1;
8481 }
8482}
8483
8484
8485void IntegerModStub::ModAnswer(MacroAssembler* masm,
8486 Register result,
8487 Register shift_distance,
8488 Register mask_bits,
8489 Register sum_of_digits) {
8490 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8491 __ Ret();
8492}
8493
8494
8495// See comment for class.
8496void IntegerModStub::Generate(MacroAssembler* masm) {
8497 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8498 __ bic(odd_number_, odd_number_, Operand(1));
8499 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8500 // We now have (odd_number_ - 1) * 2 in the register.
8501 // Build a switch out of branches instead of data because it avoids
8502 // having to teach the assembler about intra-code-object pointers
8503 // that are not in relative branch instructions.
8504 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8505 Label mod21, mod23, mod25;
8506 { Assembler::BlockConstPoolScope block_const_pool(masm);
8507 __ add(pc, pc, Operand(odd_number_));
8508 // When you read pc it is always 8 ahead, but when you write it you always
8509 // write the actual value. So we put in two nops to take up the slack.
8510 __ nop();
8511 __ nop();
8512 __ b(&mod3);
8513 __ b(&mod5);
8514 __ b(&mod7);
8515 __ b(&mod9);
8516 __ b(&mod11);
8517 __ b(&mod13);
8518 __ b(&mod15);
8519 __ b(&mod17);
8520 __ b(&mod19);
8521 __ b(&mod21);
8522 __ b(&mod23);
8523 __ b(&mod25);
8524 }
8525
8526 // For each denominator we find a multiple that is almost only ones
8527 // when expressed in binary. Then we do the sum-of-digits trick for
8528 // that number. If the multiple is not 1 then we have to do a little
8529 // more work afterwards to get the answer into the 0-denominator-1
8530 // range.
8531 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8532 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8533 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8534
8535 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8536 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8537 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8538
8539 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8540 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8541 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8542
8543 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8544 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8545 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8546
8547 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8548 ModReduce(masm, lhs_, 0x3f, 11);
8549 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8550
8551 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8552 ModReduce(masm, lhs_, 0xff, 13);
8553 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8554
8555 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8556 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8557 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8558
8559 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8560 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8561 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8562
8563 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8564 ModReduce(masm, lhs_, 0xff, 19);
8565 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8566
8567 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8568 ModReduce(masm, lhs_, 0x3f, 21);
8569 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8570
8571 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8572 ModReduce(masm, lhs_, 0xff, 23);
8573 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8574
8575 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8576 ModReduce(masm, lhs_, 0x7f, 25);
8577 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8578}
8579
8580
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008581const char* GenericBinaryOpStub::GetName() {
8582 if (name_ != NULL) return name_;
8583 const int len = 100;
8584 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8585 if (name_ == NULL) return "OOM";
8586 const char* op_name = Token::Name(op_);
8587 const char* overwrite_name;
8588 switch (mode_) {
8589 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8590 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8591 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8592 default: overwrite_name = "UnknownOverwrite"; break;
8593 }
8594
8595 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008596 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008597 op_name,
8598 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008599 specialized_on_rhs_ ? "_ConstantRhs" : "",
8600 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008601 return name_;
8602}
8603
8604
ager@chromium.org5c838252010-02-19 08:53:10 +00008605
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008606void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008607 // lhs_ : x
8608 // rhs_ : y
8609 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008610
ager@chromium.org357bf652010-04-12 11:30:10 +00008611 Register result = r0;
8612 Register lhs = lhs_;
8613 Register rhs = rhs_;
8614
8615 // This code can't cope with other register allocations yet.
8616 ASSERT(result.is(r0) &&
8617 ((lhs.is(r0) && rhs.is(r1)) ||
8618 (lhs.is(r1) && rhs.is(r0))));
8619
8620 Register smi_test_reg = VirtualFrame::scratch0();
8621 Register scratch = VirtualFrame::scratch1();
8622
8623 // All ops need to know whether we are dealing with two Smis. Set up
8624 // smi_test_reg to tell us that.
8625 if (ShouldGenerateSmiCode()) {
8626 __ orr(smi_test_reg, lhs, Operand(rhs));
8627 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008628
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008629 switch (op_) {
8630 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008631 Label not_smi;
8632 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008633 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008634 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008635 __ tst(smi_test_reg, Operand(kSmiTagMask));
8636 __ b(ne, &not_smi);
8637 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8638 // Return if no overflow.
8639 __ Ret(vc);
8640 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8641 }
8642 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008643 break;
8644 }
8645
8646 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008647 Label not_smi;
8648 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008649 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008650 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008651 __ tst(smi_test_reg, Operand(kSmiTagMask));
8652 __ b(ne, &not_smi);
8653 if (lhs.is(r1)) {
8654 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8655 // Return if no overflow.
8656 __ Ret(vc);
8657 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8658 } else {
8659 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8660 // Return if no overflow.
8661 __ Ret(vc);
8662 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8663 }
8664 }
8665 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008666 break;
8667 }
8668
8669 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008670 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008671 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008672 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008673 __ tst(smi_test_reg, Operand(kSmiTagMask));
8674 Register scratch2 = smi_test_reg;
8675 smi_test_reg = no_reg;
8676 __ b(ne, &not_smi);
8677 // Remove tag from one operand (but keep sign), so that result is Smi.
8678 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8679 // Do multiplication
8680 // scratch = lower 32 bits of ip * lhs.
8681 __ smull(scratch, scratch2, lhs, ip);
8682 // Go slow on overflows (overflow bit is not set).
8683 __ mov(ip, Operand(scratch, ASR, 31));
8684 // No overflow if higher 33 bits are identical.
8685 __ cmp(ip, Operand(scratch2));
8686 __ b(ne, &slow);
8687 // Go slow on zero result to handle -0.
8688 __ tst(scratch, Operand(scratch));
8689 __ mov(result, Operand(scratch), LeaveCC, ne);
8690 __ Ret(ne);
8691 // We need -0 if we were multiplying a negative number with 0 to get 0.
8692 // We know one of them was zero.
8693 __ add(scratch2, rhs, Operand(lhs), SetCC);
8694 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
8695 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
8696 // Slow case. We fall through here if we multiplied a negative number
8697 // with 0, because that would mean we should produce -0.
8698 __ bind(&slow);
8699 }
8700 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008701 break;
8702 }
8703
8704 case Token::DIV:
8705 case Token::MOD: {
8706 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008707 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008708 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00008709 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008710 if (IsPowerOf2(constant_rhs_)) {
8711 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008712 __ and_(rhs,
8713 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008714 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
8715 SetCC);
8716 // We now have the answer, but if the input was negative we also
8717 // have the sign bit. Our work is done if the result is
8718 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00008719 if (!rhs.is(r0)) {
8720 __ mov(r0, rhs, LeaveCC, pl);
8721 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008722 __ Ret(pl);
8723 // A mod of a negative left hand side must return a negative number.
8724 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00008725 // already optimistically trashed rhs so we may need to restore it.
8726 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008727 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008728 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008729 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008730 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00008731 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008732 } else {
8733 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00008734 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008735 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008736 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008737 int shift = 0;
8738 int d = constant_rhs_;
8739 while ((d & 1) == 0) {
8740 d >>= 1;
8741 shift++;
8742 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008743 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008744 __ bic(r0, r0, Operand(kSmiTagMask));
8745 }
8746 } else {
8747 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00008748 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008749 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008750 // Find a fixed point reciprocal of the divisor so we can divide by
8751 // multiplying.
8752 double divisor = 1.0 / constant_rhs_;
8753 int shift = 32;
8754 double scale = 4294967296.0; // 1 << 32.
8755 uint32_t mul;
8756 // Maximise the precision of the fixed point reciprocal.
8757 while (true) {
8758 mul = static_cast<uint32_t>(scale * divisor);
8759 if (mul >= 0x7fffffff) break;
8760 scale *= 2.0;
8761 shift++;
8762 }
8763 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00008764 Register scratch2 = smi_test_reg;
8765 smi_test_reg = no_reg;
8766 __ mov(scratch2, Operand(mul));
8767 __ umull(scratch, scratch2, scratch2, lhs);
8768 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
8769 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
8770 // rhs is still the known rhs. rhs is Smi tagged.
8771 // lhs is still the unkown lhs. lhs is Smi tagged.
8772 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
8773 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008774 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00008775 scratch,
8776 scratch2,
8777 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008778 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00008779 &required_scratch_shift);
8780 // scratch << required_scratch_shift is now the Smi tagged rhs *
8781 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008782 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008783 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008784 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00008785 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008786 } else {
8787 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00008788 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008789 }
8790 }
8791 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008792 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008793 } else if (op_ == Token::MOD &&
8794 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
8795 runtime_operands_type_ != BinaryOpIC::STRINGS) {
8796 // Do generate a bit of smi code for modulus even though the default for
8797 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008798 // support for modulus checking for smis makes sense. We can handle
8799 // 1 to 25 times any power of 2. This covers over half the numbers from
8800 // 1 to 100 including all of the first 25. (Actually the constants < 10
8801 // are handled above by reciprocal multiplication. We only get here for
8802 // those cases if the right hand side is not a constant or for cases
8803 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
8804 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008805 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008806 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008807 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008808 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008809 // Check for two positive smis.
8810 __ orr(smi_test_reg, lhs, Operand(rhs));
8811 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
8812 __ b(ne, &slow);
8813 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008814 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008815 __ sub(scratch, rhs, Operand(1), SetCC);
8816 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008817 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
8818 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008819 // Calculate power of two modulus.
8820 __ and_(result, lhs, Operand(scratch));
8821 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008822
8823 __ bind(&not_power_of_2);
8824 __ eor(scratch, scratch, Operand(mask_bits));
8825 // At least two bits are set in the modulus. The high one(s) are in
8826 // mask_bits and the low one is scratch + 1.
8827 __ and_(mask_bits, scratch, Operand(lhs));
8828 Register shift_distance = scratch;
8829 scratch = no_reg;
8830
8831 // The rhs consists of a power of 2 multiplied by some odd number.
8832 // The power-of-2 part we handle by putting the corresponding bits
8833 // from the lhs in the mask_bits register, and the power in the
8834 // shift_distance register. Shift distance is never 0 due to Smi
8835 // tagging.
8836 __ CountLeadingZeros(r4, shift_distance, shift_distance);
8837 __ rsb(shift_distance, r4, Operand(32));
8838
8839 // Now we need to find out what the odd number is. The last bit is
8840 // always 1.
8841 Register odd_number = r4;
8842 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
8843 __ cmp(odd_number, Operand(25));
8844 __ b(gt, &slow);
8845
8846 IntegerModStub stub(
8847 result, shift_distance, odd_number, mask_bits, lhs, r5);
8848 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
8849
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008850 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008851 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008852 HandleBinaryOpSlowCases(
8853 masm,
8854 &not_smi,
8855 lhs,
8856 rhs,
8857 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008858 break;
8859 }
8860
8861 case Token::BIT_OR:
8862 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008863 case Token::BIT_XOR:
8864 case Token::SAR:
8865 case Token::SHR:
8866 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008867 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008868 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008869 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008870 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008871 Register scratch2 = smi_test_reg;
8872 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008873 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008874 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
8875 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
8876 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008877 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008878 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00008879 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8880 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008881 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008882 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008883 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008884 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008885 // Remove tags from operands. We can't do this on a 31 bit number
8886 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00008887 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8888 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8889 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008890 // Unsigned shift is not allowed to produce a negative number, so
8891 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00008892 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008893 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008894 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008895 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008896 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008897 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008898 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00008899 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8900 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8901 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008902 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00008903 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008904 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008905 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008906 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008907 default: UNREACHABLE();
8908 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008909 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008910 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008911 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008912 break;
8913 }
8914
8915 default: UNREACHABLE();
8916 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008917 // This code should be unreachable.
8918 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00008919
8920 // Generate an unreachable reference to the DEFAULT stub so that it can be
8921 // found at the end of this stub when clearing ICs at GC.
8922 // TODO(kaznacheev): Check performance impact and get rid of this.
8923 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
8924 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
8925 __ CallStub(&uninit);
8926 }
8927}
8928
8929
8930void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
8931 Label get_result;
8932
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008933 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008934
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008935 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
8936 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00008937 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008938 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008939
8940 __ TailCallExternalReference(
8941 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008942 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00008943 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008944}
8945
8946
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008947Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008948 GenericBinaryOpStub stub(key, type_info);
8949 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008950}
8951
8952
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008953void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
8954 // Argument is a number and is on stack and in r0.
8955 Label runtime_call;
8956 Label input_not_smi;
8957 Label loaded;
8958
8959 if (CpuFeatures::IsSupported(VFP3)) {
8960 // Load argument and check if it is a smi.
8961 __ BranchOnNotSmi(r0, &input_not_smi);
8962
8963 CpuFeatures::Scope scope(VFP3);
8964 // Input is a smi. Convert to double and load the low and high words
8965 // of the double into r2, r3.
8966 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
8967 __ b(&loaded);
8968
8969 __ bind(&input_not_smi);
8970 // Check if input is a HeapNumber.
8971 __ CheckMap(r0,
8972 r1,
8973 Heap::kHeapNumberMapRootIndex,
8974 &runtime_call,
8975 true);
8976 // Input is a HeapNumber. Load it to a double register and store the
8977 // low and high words into r2, r3.
8978 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
8979
8980 __ bind(&loaded);
8981 // r2 = low 32 bits of double value
8982 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008983 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008984 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
8985 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008986 __ eor(r1, r1, Operand(r1, ASR, 16));
8987 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008988 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008989 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008990
8991 // r2 = low 32 bits of double value.
8992 // r3 = high 32 bits of double value.
8993 // r1 = TranscendentalCache::hash(double value).
8994 __ mov(r0,
8995 Operand(ExternalReference::transcendental_cache_array_address()));
8996 // r0 points to cache array.
8997 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
8998 // r0 points to the cache for the type type_.
8999 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9000 __ cmp(r0, Operand(0));
9001 __ b(eq, &runtime_call);
9002
9003#ifdef DEBUG
9004 // Check that the layout of cache elements match expectations.
9005 { TranscendentalCache::Element test_elem[2];
9006 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9007 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9008 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9009 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9010 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9011 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9012 CHECK_EQ(0, elem_in0 - elem_start);
9013 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9014 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9015 }
9016#endif
9017
9018 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9019 __ add(r1, r1, Operand(r1, LSL, 1));
9020 __ add(r0, r0, Operand(r1, LSL, 2));
9021 // Check if cache matches: Double value is stored in uint32_t[2] array.
9022 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9023 __ cmp(r2, r4);
9024 __ b(ne, &runtime_call);
9025 __ cmp(r3, r5);
9026 __ b(ne, &runtime_call);
9027 // Cache hit. Load result, pop argument and return.
9028 __ mov(r0, Operand(r6));
9029 __ pop();
9030 __ Ret();
9031 }
9032
9033 __ bind(&runtime_call);
9034 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9035}
9036
9037
9038Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9039 switch (type_) {
9040 // Add more cases when necessary.
9041 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9042 case TranscendentalCache::COS: return Runtime::kMath_cos;
9043 default:
9044 UNIMPLEMENTED();
9045 return Runtime::kAbort;
9046 }
9047}
9048
9049
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009050void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009051 // Do tail-call to runtime routine. Runtime routines expect at least one
9052 // argument, so give it a Smi.
9053 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009054 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009055 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009056
9057 __ StubReturn(1);
9058}
9059
9060
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009061void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009062 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009063
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009064 Register heap_number_map = r6;
9065 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9066
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009067 if (op_ == Token::SUB) {
9068 // Check whether the value is a smi.
9069 Label try_float;
9070 __ tst(r0, Operand(kSmiTagMask));
9071 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009072
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009073 // Go slow case if the value of the expression is zero
9074 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009075 if (negative_zero_ == kStrictNegativeZero) {
9076 // If we have to check for zero, then we can check for the max negative
9077 // smi while we are at it.
9078 __ bic(ip, r0, Operand(0x80000000), SetCC);
9079 __ b(eq, &slow);
9080 __ rsb(r0, r0, Operand(0));
9081 __ StubReturn(1);
9082 } else {
9083 // The value of the expression is a smi and 0 is OK for -0. Try
9084 // optimistic subtraction '0 - value'.
9085 __ rsb(r0, r0, Operand(0), SetCC);
9086 __ StubReturn(1, vc);
9087 // We don't have to reverse the optimistic neg since the only case
9088 // where we fall through is the minimum negative Smi, which is the case
9089 // where the neg leaves the register unchanged.
9090 __ jmp(&slow); // Go slow on max negative Smi.
9091 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009092
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009093 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009094 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9095 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9096 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009097 __ b(ne, &slow);
9098 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009099 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009100 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9101 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9102 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9103 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009104 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009105 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9106 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9107 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9108 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9109 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9110 __ mov(r0, Operand(r1));
9111 }
9112 } else if (op_ == Token::BIT_NOT) {
9113 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009114 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9115 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9116 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009117 __ b(ne, &slow);
9118
9119 // Convert the heap number is r0 to an untagged integer in r1.
9120 GetInt32(masm, r0, r1, r2, r3, &slow);
9121
9122 // Do the bitwise operation (move negated) and check if the result
9123 // fits in a smi.
9124 Label try_float;
9125 __ mvn(r1, Operand(r1));
9126 __ add(r2, r1, Operand(0x40000000), SetCC);
9127 __ b(mi, &try_float);
9128 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9129 __ b(&done);
9130
9131 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009132 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009133 // Allocate a fresh heap number, but don't overwrite r0 until
9134 // we're sure we can do it without going through the slow case
9135 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009136 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009137 __ mov(r0, Operand(r2));
9138 }
9139
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009140 if (CpuFeatures::IsSupported(VFP3)) {
9141 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9142 CpuFeatures::Scope scope(VFP3);
9143 __ vmov(s0, r1);
9144 __ vcvt_f64_s32(d0, s0);
9145 __ sub(r2, r0, Operand(kHeapObjectTag));
9146 __ vstr(d0, r2, HeapNumber::kValueOffset);
9147 } else {
9148 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9149 // have to set up a frame.
9150 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9151 __ push(lr);
9152 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9153 __ pop(lr);
9154 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009155 } else {
9156 UNIMPLEMENTED();
9157 }
9158
9159 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009160 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009161
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009162 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009163 __ bind(&slow);
9164 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009165 switch (op_) {
9166 case Token::SUB:
9167 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9168 break;
9169 case Token::BIT_NOT:
9170 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9171 break;
9172 default:
9173 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009174 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009175}
9176
9177
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009178void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009179 // r0 holds the exception.
9180
9181 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009182 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009183
9184 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009185 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9186 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009187
9188 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009189 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009190 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009191 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009192 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009193 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9194
9195 // Before returning we restore the context from the frame pointer if
9196 // not NULL. The frame pointer is NULL in the exception handler of a
9197 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009198 __ cmp(fp, Operand(0));
9199 // Set cp to NULL if fp is NULL.
9200 __ mov(cp, Operand(0), LeaveCC, eq);
9201 // Restore cp otherwise.
9202 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009203#ifdef DEBUG
9204 if (FLAG_debug_code) {
9205 __ mov(lr, Operand(pc));
9206 }
9207#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009208 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009209 __ pop(pc);
9210}
9211
9212
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009213void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9214 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009215 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009216 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009217
9218 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009219 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009220 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009221
9222 // Unwind the handlers until the ENTRY handler is found.
9223 Label loop, done;
9224 __ bind(&loop);
9225 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009226 const int kStateOffset = StackHandlerConstants::kStateOffset;
9227 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009228 __ cmp(r2, Operand(StackHandler::ENTRY));
9229 __ b(eq, &done);
9230 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009231 const int kNextOffset = StackHandlerConstants::kNextOffset;
9232 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009233 __ jmp(&loop);
9234 __ bind(&done);
9235
9236 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009237 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009238 __ pop(r2);
9239 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009240
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009241 if (type == OUT_OF_MEMORY) {
9242 // Set external caught exception to false.
9243 ExternalReference external_caught(Top::k_external_caught_exception_address);
9244 __ mov(r0, Operand(false));
9245 __ mov(r2, Operand(external_caught));
9246 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009247
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009248 // Set pending exception and r0 to out of memory exception.
9249 Failure* out_of_memory = Failure::OutOfMemoryException();
9250 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9251 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9252 __ str(r0, MemOperand(r2));
9253 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009254
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009255 // Stack layout at this point. See also StackHandlerConstants.
9256 // sp -> state (ENTRY)
9257 // fp
9258 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009259
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009260 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009261 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009262 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
9263 // Before returning we restore the context from the frame pointer if
9264 // not NULL. The frame pointer is NULL in the exception handler of a
9265 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009266 __ cmp(fp, Operand(0));
9267 // Set cp to NULL if fp is NULL.
9268 __ mov(cp, Operand(0), LeaveCC, eq);
9269 // Restore cp otherwise.
9270 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009271#ifdef DEBUG
9272 if (FLAG_debug_code) {
9273 __ mov(lr, Operand(pc));
9274 }
9275#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009276 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009277 __ pop(pc);
9278}
9279
9280
9281void CEntryStub::GenerateCore(MacroAssembler* masm,
9282 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009283 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009284 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009285 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009286 bool always_allocate,
9287 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009288 // r0: result parameter for PerformGC, if any
9289 // r4: number of arguments including receiver (C callee-saved)
9290 // r5: pointer to builtin function (C callee-saved)
9291 // r6: pointer to the first argument (C callee-saved)
9292
9293 if (do_gc) {
9294 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009295 __ PrepareCallCFunction(1, r1);
9296 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009297 }
9298
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009299 ExternalReference scope_depth =
9300 ExternalReference::heap_always_allocate_scope_depth();
9301 if (always_allocate) {
9302 __ mov(r0, Operand(scope_depth));
9303 __ ldr(r1, MemOperand(r0));
9304 __ add(r1, r1, Operand(1));
9305 __ str(r1, MemOperand(r0));
9306 }
9307
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009308 // Call C built-in.
9309 // r0 = argc, r1 = argv
9310 __ mov(r0, Operand(r4));
9311 __ mov(r1, Operand(r6));
9312
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009313 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9314 int frame_alignment_mask = frame_alignment - 1;
9315#if defined(V8_HOST_ARCH_ARM)
9316 if (FLAG_debug_code) {
9317 if (frame_alignment > kPointerSize) {
9318 Label alignment_as_expected;
9319 ASSERT(IsPowerOf2(frame_alignment));
9320 __ sub(r2, sp, Operand(frame_alignment_skew));
9321 __ tst(r2, Operand(frame_alignment_mask));
9322 __ b(eq, &alignment_as_expected);
9323 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9324 __ stop("Unexpected alignment");
9325 __ bind(&alignment_as_expected);
9326 }
9327 }
9328#endif
9329
9330 // Just before the call (jump) below lr is pushed, so the actual alignment is
9331 // adding one to the current skew.
9332 int alignment_before_call =
9333 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9334 if (alignment_before_call > 0) {
9335 // Push until the alignment before the call is met.
9336 __ mov(r2, Operand(0));
9337 for (int i = alignment_before_call;
9338 (i & frame_alignment_mask) != 0;
9339 i += kPointerSize) {
9340 __ push(r2);
9341 }
9342 }
9343
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009344 // TODO(1242173): To let the GC traverse the return address of the exit
9345 // frames, we need to know where the return address is. Right now,
9346 // we push it on the stack to be able to find it again, but we never
9347 // restore from it in case of changes, which makes it impossible to
9348 // support moving the C entry code stub. This should be fixed, but currently
9349 // this is OK because the CEntryStub gets generated so early in the V8 boot
9350 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009351 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009352 masm->push(lr);
9353 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009354
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009355 // Restore sp back to before aligning the stack.
9356 if (alignment_before_call > 0) {
9357 __ add(sp, sp, Operand(alignment_before_call));
9358 }
9359
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009360 if (always_allocate) {
9361 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9362 // though (contain the result).
9363 __ mov(r2, Operand(scope_depth));
9364 __ ldr(r3, MemOperand(r2));
9365 __ sub(r3, r3, Operand(1));
9366 __ str(r3, MemOperand(r2));
9367 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009368
9369 // check for failure result
9370 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009371 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009372 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9373 __ add(r2, r0, Operand(1));
9374 __ tst(r2, Operand(kFailureTagMask));
9375 __ b(eq, &failure_returned);
9376
9377 // Exit C frame and return.
9378 // r0:r1: result
9379 // sp: stack pointer
9380 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009381 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009382
9383 // check if we should retry or throw exception
9384 Label retry;
9385 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009386 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009387 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9388 __ b(eq, &retry);
9389
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009390 // Special handling of out of memory exceptions.
9391 Failure* out_of_memory = Failure::OutOfMemoryException();
9392 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9393 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009394
9395 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009396 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009397 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009398 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009399 __ ldr(r0, MemOperand(ip));
9400 __ str(r3, MemOperand(ip));
9401
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009402 // Special handling of termination exceptions which are uncatchable
9403 // by javascript code.
9404 __ cmp(r0, Operand(Factory::termination_exception()));
9405 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009406
9407 // Handle normal exception.
9408 __ jmp(throw_normal_exception);
9409
9410 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9411}
9412
9413
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009414void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009415 // Called from JavaScript; parameters are on stack as if calling JS function
9416 // r0: number of arguments including receiver
9417 // r1: pointer to builtin function
9418 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009419 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009420 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009421
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009422 // Result returned in r0 or r0+r1 by default.
9423
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009424 // NOTE: Invocations of builtins may return failure objects
9425 // instead of a proper result. The builtin entry handles
9426 // this by performing a garbage collection and retrying the
9427 // builtin once.
9428
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009429 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009430 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009431
9432 // r4: number of arguments (C callee-saved)
9433 // r5: pointer to builtin function (C callee-saved)
9434 // r6: pointer to first argument (C callee-saved)
9435
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009436 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009437 Label throw_termination_exception;
9438 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009439
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009440 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009441 GenerateCore(masm,
9442 &throw_normal_exception,
9443 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009444 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009445 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009446 false,
9447 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009448
9449 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009450 GenerateCore(masm,
9451 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009452 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009453 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009454 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009455 false,
9456 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009457
9458 // Do full GC and retry runtime call one final time.
9459 Failure* failure = Failure::InternalError();
9460 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9461 GenerateCore(masm,
9462 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009463 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009464 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009465 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009466 true,
9467 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009468
9469 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009470 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9471
9472 __ bind(&throw_termination_exception);
9473 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009474
9475 __ bind(&throw_normal_exception);
9476 GenerateThrowTOS(masm);
9477}
9478
9479
9480void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9481 // r0: code entry
9482 // r1: function
9483 // r2: receiver
9484 // r3: argc
9485 // [sp+0]: argv
9486
9487 Label invoke, exit;
9488
9489 // Called from C, so do not pop argc and args on exit (preserve sp)
9490 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009491 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009492 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9493
9494 // Get address of argv, see stm above.
9495 // r0: code entry
9496 // r1: function
9497 // r2: receiver
9498 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009499 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009500
9501 // Push a frame with special values setup to mark it as an entry frame.
9502 // r0: code entry
9503 // r1: function
9504 // r2: receiver
9505 // r3: argc
9506 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009507 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009508 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9509 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009510 __ mov(r6, Operand(Smi::FromInt(marker)));
9511 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9512 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009513 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009514
9515 // Setup frame pointer for the frame to be pushed.
9516 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9517
9518 // Call a faked try-block that does the invoke.
9519 __ bl(&invoke);
9520
9521 // Caught exception: Store result (exception) in the pending
9522 // exception field in the JSEnv and return a failure sentinel.
9523 // Coming in here the fp will be invalid because the PushTryHandler below
9524 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009525 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009526 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009527 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009528 __ b(&exit);
9529
9530 // Invoke: Link this frame into the handler chain.
9531 __ bind(&invoke);
9532 // Must preserve r0-r4, r5-r7 are available.
9533 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009534 // If an exception not caught by another handler occurs, this handler
9535 // returns control to the code after the bl(&invoke) above, which
9536 // restores all kCalleeSaved registers (including cp and fp) to their
9537 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009538
9539 // Clear any pending exceptions.
9540 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9541 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009542 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009543 __ str(r5, MemOperand(ip));
9544
9545 // Invoke the function by calling through JS entry trampoline builtin.
9546 // Notice that we cannot store a reference to the trampoline code directly in
9547 // this stub, because runtime stubs are not traversed when doing GC.
9548
9549 // Expected registers by Builtins::JSEntryTrampoline
9550 // r0: code entry
9551 // r1: function
9552 // r2: receiver
9553 // r3: argc
9554 // r4: argv
9555 if (is_construct) {
9556 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9557 __ mov(ip, Operand(construct_entry));
9558 } else {
9559 ExternalReference entry(Builtins::JSEntryTrampoline);
9560 __ mov(ip, Operand(entry));
9561 }
9562 __ ldr(ip, MemOperand(ip)); // deref address
9563
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009564 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9565 // macro for the add instruction because we don't want the coverage tool
9566 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009567 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009568 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009569
9570 // Unlink this frame from the handler chain. When reading the
9571 // address of the next handler, there is no need to use the address
9572 // displacement since the current stack pointer (sp) points directly
9573 // to the stack handler.
9574 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9575 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9576 __ str(r3, MemOperand(ip));
9577 // No need to restore registers
9578 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9579
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009580
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009581 __ bind(&exit); // r0 holds result
9582 // Restore the top frame descriptors from the stack.
9583 __ pop(r3);
9584 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9585 __ str(r3, MemOperand(ip));
9586
9587 // Reset the stack to the callee saved registers.
9588 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9589
9590 // Restore callee-saved registers and return.
9591#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009592 if (FLAG_debug_code) {
9593 __ mov(lr, Operand(pc));
9594 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009595#endif
9596 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9597}
9598
9599
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009600// This stub performs an instanceof, calling the builtin function if
9601// necessary. Uses r1 for the object, r0 for the function that it may
9602// be an instance of (these are fetched from the stack).
9603void InstanceofStub::Generate(MacroAssembler* masm) {
9604 // Get the object - slow case for smis (we may need to throw an exception
9605 // depending on the rhs).
9606 Label slow, loop, is_instance, is_not_instance;
9607 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9608 __ BranchOnSmi(r0, &slow);
9609
9610 // Check that the left hand is a JS object and put map in r3.
9611 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9612 __ b(lt, &slow);
9613 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9614 __ b(gt, &slow);
9615
9616 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00009617 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009618 // r1 is function, r3 is map.
9619
9620 // Look up the function and the map in the instanceof cache.
9621 Label miss;
9622 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
9623 __ cmp(r1, ip);
9624 __ b(ne, &miss);
9625 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
9626 __ cmp(r3, ip);
9627 __ b(ne, &miss);
9628 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
9629 __ pop();
9630 __ pop();
9631 __ mov(pc, Operand(lr));
9632
9633 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009634 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
9635
9636 // Check that the function prototype is a JS object.
9637 __ BranchOnSmi(r4, &slow);
9638 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
9639 __ b(lt, &slow);
9640 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
9641 __ b(gt, &slow);
9642
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009643 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
9644 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
9645
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009646 // Register mapping: r3 is object map and r4 is function prototype.
9647 // Get prototype of object into r2.
9648 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9649
9650 // Loop through the prototype chain looking for the function prototype.
9651 __ bind(&loop);
9652 __ cmp(r2, Operand(r4));
9653 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009654 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9655 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009656 __ b(eq, &is_not_instance);
9657 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9658 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9659 __ jmp(&loop);
9660
9661 __ bind(&is_instance);
9662 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009663 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009664 __ pop();
9665 __ pop();
9666 __ mov(pc, Operand(lr)); // Return.
9667
9668 __ bind(&is_not_instance);
9669 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009670 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009671 __ pop();
9672 __ pop();
9673 __ mov(pc, Operand(lr)); // Return.
9674
9675 // Slow-case. Tail call builtin.
9676 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009677 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9678}
9679
9680
ager@chromium.org7c537e22008-10-16 08:43:32 +00009681void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9682 // The displacement is the offset of the last parameter (if any)
9683 // relative to the frame pointer.
9684 static const int kDisplacement =
9685 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009686
ager@chromium.org7c537e22008-10-16 08:43:32 +00009687 // Check that the key is a smi.
9688 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009689 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009690
ager@chromium.org7c537e22008-10-16 08:43:32 +00009691 // Check if the calling frame is an arguments adaptor frame.
9692 Label adaptor;
9693 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9694 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009695 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009696 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009697
ager@chromium.org7c537e22008-10-16 08:43:32 +00009698 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00009699 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00009700 // check for free.
9701 __ cmp(r1, r0);
9702 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009703
ager@chromium.org7c537e22008-10-16 08:43:32 +00009704 // Read the argument from the stack and return it.
9705 __ sub(r3, r0, r1);
9706 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9707 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009708 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009709
9710 // Arguments adaptor case: Check index against actual arguments
9711 // limit found in the arguments adaptor frame. Use unsigned
9712 // comparison to get negative check for free.
9713 __ bind(&adaptor);
9714 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9715 __ cmp(r1, r0);
9716 __ b(cs, &slow);
9717
9718 // Read the argument from the adaptor frame and return it.
9719 __ sub(r3, r0, r1);
9720 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9721 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009722 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009723
9724 // Slow-case: Handle non-smi or out-of-bounds access to arguments
9725 // by calling the runtime system.
9726 __ bind(&slow);
9727 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009728 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009729}
9730
9731
9732void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009733 // sp[0] : number of parameters
9734 // sp[4] : receiver displacement
9735 // sp[8] : function
9736
ager@chromium.org7c537e22008-10-16 08:43:32 +00009737 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00009738 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00009739 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9740 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009741 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00009742 __ b(eq, &adaptor_frame);
9743
9744 // Get the length from the frame.
9745 __ ldr(r1, MemOperand(sp, 0));
9746 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009747
9748 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00009749 __ bind(&adaptor_frame);
9750 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9751 __ str(r1, MemOperand(sp, 0));
9752 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009753 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
9754 __ str(r3, MemOperand(sp, 1 * kPointerSize));
9755
ager@chromium.org5c838252010-02-19 08:53:10 +00009756 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009757 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +00009758 Label add_arguments_object;
9759 __ bind(&try_allocate);
9760 __ cmp(r1, Operand(0));
9761 __ b(eq, &add_arguments_object);
9762 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
9763 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
9764 __ bind(&add_arguments_object);
9765 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
9766
9767 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009768 __ AllocateInNewSpace(
9769 r1,
9770 r0,
9771 r2,
9772 r3,
9773 &runtime,
9774 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +00009775
9776 // Get the arguments boilerplate from the current (global) context.
9777 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
9778 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
9779 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
9780 __ ldr(r4, MemOperand(r4, offset));
9781
9782 // Copy the JS object part.
9783 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
9784 __ ldr(r3, FieldMemOperand(r4, i));
9785 __ str(r3, FieldMemOperand(r0, i));
9786 }
9787
9788 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009789 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +00009790 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
9791 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
9792
9793 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009794 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009795 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
9796 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
9797
9798 // If there are no actual arguments, we're done.
9799 Label done;
9800 __ cmp(r1, Operand(0));
9801 __ b(eq, &done);
9802
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009803 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00009804 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009805
9806 // Setup the elements pointer in the allocated arguments object and
9807 // initialize the header in the elements fixed array.
9808 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
9809 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
9810 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
9811 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
9812 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009813 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +00009814
9815 // Copy the fixed array slots.
9816 Label loop;
9817 // Setup r4 to point to the first array slot.
9818 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
9819 __ bind(&loop);
9820 // Pre-decrement r2 with kPointerSize on each iteration.
9821 // Pre-decrement in order to skip receiver.
9822 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
9823 // Post-increment r4 with kPointerSize on each iteration.
9824 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
9825 __ sub(r1, r1, Operand(1));
9826 __ cmp(r1, Operand(0));
9827 __ b(ne, &loop);
9828
9829 // Return and remove the on-stack parameters.
9830 __ bind(&done);
9831 __ add(sp, sp, Operand(3 * kPointerSize));
9832 __ Ret();
9833
ager@chromium.org7c537e22008-10-16 08:43:32 +00009834 // Do the runtime call to allocate the arguments object.
9835 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009836 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009837}
9838
9839
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009840void RegExpExecStub::Generate(MacroAssembler* masm) {
9841 // Just jump directly to runtime if native RegExp is not selected at compile
9842 // time or if regexp entry in generated code is turned off runtime switch or
9843 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009844#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009845 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009846#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009847 if (!FLAG_regexp_entry_native) {
9848 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
9849 return;
9850 }
9851
9852 // Stack frame on entry.
9853 // sp[0]: last_match_info (expected JSArray)
9854 // sp[4]: previous index
9855 // sp[8]: subject string
9856 // sp[12]: JSRegExp object
9857
9858 static const int kLastMatchInfoOffset = 0 * kPointerSize;
9859 static const int kPreviousIndexOffset = 1 * kPointerSize;
9860 static const int kSubjectOffset = 2 * kPointerSize;
9861 static const int kJSRegExpOffset = 3 * kPointerSize;
9862
9863 Label runtime, invoke_regexp;
9864
9865 // Allocation of registers for this function. These are in callee save
9866 // registers and will be preserved by the call to the native RegExp code, as
9867 // this code is called using the normal C calling convention. When calling
9868 // directly from generated code the native RegExp code will not do a GC and
9869 // therefore the content of these registers are safe to use after the call.
9870 Register subject = r4;
9871 Register regexp_data = r5;
9872 Register last_match_info_elements = r6;
9873
9874 // Ensure that a RegExp stack is allocated.
9875 ExternalReference address_of_regexp_stack_memory_address =
9876 ExternalReference::address_of_regexp_stack_memory_address();
9877 ExternalReference address_of_regexp_stack_memory_size =
9878 ExternalReference::address_of_regexp_stack_memory_size();
9879 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
9880 __ ldr(r0, MemOperand(r0, 0));
9881 __ tst(r0, Operand(r0));
9882 __ b(eq, &runtime);
9883
9884 // Check that the first argument is a JSRegExp object.
9885 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009886 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009887 __ tst(r0, Operand(kSmiTagMask));
9888 __ b(eq, &runtime);
9889 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
9890 __ b(ne, &runtime);
9891
9892 // Check that the RegExp has been compiled (data contains a fixed array).
9893 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
9894 if (FLAG_debug_code) {
9895 __ tst(regexp_data, Operand(kSmiTagMask));
9896 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
9897 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
9898 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
9899 }
9900
9901 // regexp_data: RegExp data (FixedArray)
9902 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
9903 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
9904 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
9905 __ b(ne, &runtime);
9906
9907 // regexp_data: RegExp data (FixedArray)
9908 // Check that the number of captures fit in the static offsets vector buffer.
9909 __ ldr(r2,
9910 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
9911 // Calculate number of capture registers (number_of_captures + 1) * 2. This
9912 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009913 STATIC_ASSERT(kSmiTag == 0);
9914 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009915 __ add(r2, r2, Operand(2)); // r2 was a smi.
9916 // Check that the static offsets vector buffer is large enough.
9917 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
9918 __ b(hi, &runtime);
9919
9920 // r2: Number of capture registers
9921 // regexp_data: RegExp data (FixedArray)
9922 // Check that the second argument is a string.
9923 __ ldr(subject, MemOperand(sp, kSubjectOffset));
9924 __ tst(subject, Operand(kSmiTagMask));
9925 __ b(eq, &runtime);
9926 Condition is_string = masm->IsObjectStringType(subject, r0);
9927 __ b(NegateCondition(is_string), &runtime);
9928 // Get the length of the string to r3.
9929 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
9930
9931 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00009932 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009933 // subject: Subject string
9934 // regexp_data: RegExp data (FixedArray)
9935 // Check that the third argument is a positive smi less than the subject
9936 // string length. A negative value will be greater (unsigned comparison).
9937 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009938 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009939 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009940 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009941 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009942
9943 // r2: Number of capture registers
9944 // subject: Subject string
9945 // regexp_data: RegExp data (FixedArray)
9946 // Check that the fourth object is a JSArray object.
9947 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
9948 __ tst(r0, Operand(kSmiTagMask));
9949 __ b(eq, &runtime);
9950 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
9951 __ b(ne, &runtime);
9952 // Check that the JSArray is in fast case.
9953 __ ldr(last_match_info_elements,
9954 FieldMemOperand(r0, JSArray::kElementsOffset));
9955 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009956 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00009957 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009958 __ b(ne, &runtime);
9959 // Check that the last match info has space for the capture registers and the
9960 // additional information.
9961 __ ldr(r0,
9962 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
9963 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009964 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009965 __ b(gt, &runtime);
9966
9967 // subject: Subject string
9968 // regexp_data: RegExp data (FixedArray)
9969 // Check the representation and encoding of the subject string.
9970 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009971 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
9972 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009973 // First check for flat string.
9974 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009975 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009976 __ b(eq, &seq_string);
9977
9978 // subject: Subject string
9979 // regexp_data: RegExp data (FixedArray)
9980 // Check for flat cons string.
9981 // A flat cons string is a cons string where the second part is the empty
9982 // string. In that case the subject string is just the first part of the cons
9983 // string. Also in this case the first part of the cons string is known to be
9984 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009985 STATIC_ASSERT(kExternalStringTag !=0);
9986 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009987 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009988 __ b(ne, &runtime);
9989 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
9990 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
9991 __ cmp(r0, r1);
9992 __ b(ne, &runtime);
9993 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
9994 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
9995 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009996 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009997 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009998 __ tst(r0, Operand(kStringRepresentationMask));
9999 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010000
10001 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010002 // subject: Subject string
10003 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010004 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010005 STATIC_ASSERT(4 == kAsciiStringTag);
10006 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010007 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010008 __ and_(r0, r0, Operand(kStringEncodingMask));
10009 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010010 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10011 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10012
10013 // Check that the irregexp code has been generated for the actual string
10014 // encoding. If it has, the field contains a code object otherwise it contains
10015 // the hole.
10016 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10017 __ b(ne, &runtime);
10018
10019 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10020 // r7: code
10021 // subject: Subject string
10022 // regexp_data: RegExp data (FixedArray)
10023 // Load used arguments before starting to push arguments for call to native
10024 // RegExp code to avoid handling changing stack height.
10025 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10026 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10027
10028 // r1: previous index
10029 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10030 // r7: code
10031 // subject: Subject string
10032 // regexp_data: RegExp data (FixedArray)
10033 // All checks done. Now push arguments for native regexp code.
10034 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10035
10036 static const int kRegExpExecuteArguments = 7;
10037 __ push(lr);
10038 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10039
10040 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10041 __ mov(r0, Operand(1));
10042 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10043
10044 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10045 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10046 __ ldr(r0, MemOperand(r0, 0));
10047 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10048 __ ldr(r2, MemOperand(r2, 0));
10049 __ add(r0, r0, Operand(r2));
10050 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10051
10052 // Argument 5 (sp[0]): static offsets vector buffer.
10053 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10054 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10055
10056 // For arguments 4 and 3 get string length, calculate start of string data and
10057 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10058 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010059 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010060 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010061 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10062 __ eor(r3, r3, Operand(1));
10063 // Argument 4 (r3): End of string data
10064 // Argument 3 (r2): Start of string data
10065 __ add(r2, r9, Operand(r1, LSL, r3));
10066 __ add(r3, r9, Operand(r0, LSL, r3));
10067
10068 // Argument 2 (r1): Previous index.
10069 // Already there
10070
10071 // Argument 1 (r0): Subject string.
10072 __ mov(r0, subject);
10073
10074 // Locate the code entry and call it.
10075 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10076 __ CallCFunction(r7, kRegExpExecuteArguments);
10077 __ pop(lr);
10078
10079 // r0: result
10080 // subject: subject string (callee saved)
10081 // regexp_data: RegExp data (callee saved)
10082 // last_match_info_elements: Last match info elements (callee saved)
10083
10084 // Check the result.
10085 Label success;
10086 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10087 __ b(eq, &success);
10088 Label failure;
10089 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10090 __ b(eq, &failure);
10091 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10092 // If not exception it can only be retry. Handle that in the runtime system.
10093 __ b(ne, &runtime);
10094 // Result must now be exception. If there is no pending exception already a
10095 // stack overflow (on the backtrack stack) was detected in RegExp code but
10096 // haven't created the exception yet. Handle that in the runtime system.
10097 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10098 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10099 __ ldr(r0, MemOperand(r0, 0));
10100 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10101 __ ldr(r1, MemOperand(r1, 0));
10102 __ cmp(r0, r1);
10103 __ b(eq, &runtime);
10104 __ bind(&failure);
10105 // For failure and exception return null.
10106 __ mov(r0, Operand(Factory::null_value()));
10107 __ add(sp, sp, Operand(4 * kPointerSize));
10108 __ Ret();
10109
10110 // Process the result from the native regexp code.
10111 __ bind(&success);
10112 __ ldr(r1,
10113 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10114 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010115 STATIC_ASSERT(kSmiTag == 0);
10116 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010117 __ add(r1, r1, Operand(2)); // r1 was a smi.
10118
10119 // r1: number of capture registers
10120 // r4: subject string
10121 // Store the capture count.
10122 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10123 __ str(r2, FieldMemOperand(last_match_info_elements,
10124 RegExpImpl::kLastCaptureCountOffset));
10125 // Store last subject and last input.
10126 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010127 __ str(subject,
10128 FieldMemOperand(last_match_info_elements,
10129 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010130 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010131 __ str(subject,
10132 FieldMemOperand(last_match_info_elements,
10133 RegExpImpl::kLastInputOffset));
10134 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010135 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010136
10137 // Get the static offsets vector filled by the native regexp code.
10138 ExternalReference address_of_static_offsets_vector =
10139 ExternalReference::address_of_static_offsets_vector();
10140 __ mov(r2, Operand(address_of_static_offsets_vector));
10141
10142 // r1: number of capture registers
10143 // r2: offsets vector
10144 Label next_capture, done;
10145 // Capture register counter starts from number of capture registers and
10146 // counts down until wraping after zero.
10147 __ add(r0,
10148 last_match_info_elements,
10149 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10150 __ bind(&next_capture);
10151 __ sub(r1, r1, Operand(1), SetCC);
10152 __ b(mi, &done);
10153 // Read the value from the static offsets vector buffer.
10154 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10155 // Store the smi value in the last match info.
10156 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10157 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10158 __ jmp(&next_capture);
10159 __ bind(&done);
10160
10161 // Return last match info.
10162 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10163 __ add(sp, sp, Operand(4 * kPointerSize));
10164 __ Ret();
10165
10166 // Do the runtime call to execute the regexp.
10167 __ bind(&runtime);
10168 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010169#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010170}
10171
10172
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010173void CallFunctionStub::Generate(MacroAssembler* masm) {
10174 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010175
10176 // If the receiver might be a value (string, number or boolean) check for this
10177 // and box it if it is.
10178 if (ReceiverMightBeValue()) {
10179 // Get the receiver from the stack.
10180 // function, receiver [, arguments]
10181 Label receiver_is_value, receiver_is_js_object;
10182 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10183
10184 // Check if receiver is a smi (which is a number value).
10185 __ BranchOnSmi(r1, &receiver_is_value);
10186
10187 // Check if the receiver is a valid JS object.
10188 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10189 __ b(ge, &receiver_is_js_object);
10190
10191 // Call the runtime to box the value.
10192 __ bind(&receiver_is_value);
10193 __ EnterInternalFrame();
10194 __ push(r1);
10195 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10196 __ LeaveInternalFrame();
10197 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10198
10199 __ bind(&receiver_is_js_object);
10200 }
10201
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010202 // Get the function to call from the stack.
10203 // function, receiver [, arguments]
10204 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10205
10206 // Check that the function is really a JavaScript function.
10207 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010208 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010209 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010210 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010211 __ b(ne, &slow);
10212
10213 // Fast-case: Invoke the function now.
10214 // r1: pushed function
10215 ParameterCount actual(argc_);
10216 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10217
10218 // Slow-case: Non-function called.
10219 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010220 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10221 // of the original receiver from the call site).
10222 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010223 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010224 __ mov(r2, Operand(0));
10225 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10226 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10227 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010228}
10229
10230
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010231// Unfortunately you have to run without snapshots to see most of these
10232// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010233const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010234 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10235 (lhs_.is(r1) && rhs_.is(r0)));
10236
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010237 if (name_ != NULL) return name_;
10238 const int kMaxNameLength = 100;
10239 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10240 if (name_ == NULL) return "OOM";
10241
10242 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010243 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010244 case lt: cc_name = "LT"; break;
10245 case gt: cc_name = "GT"; break;
10246 case le: cc_name = "LE"; break;
10247 case ge: cc_name = "GE"; break;
10248 case eq: cc_name = "EQ"; break;
10249 case ne: cc_name = "NE"; break;
10250 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010251 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010252
ager@chromium.orgb5737492010-07-15 09:29:43 +000010253 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10254 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10255
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010256 const char* strict_name = "";
10257 if (strict_ && (cc_ == eq || cc_ == ne)) {
10258 strict_name = "_STRICT";
10259 }
10260
10261 const char* never_nan_nan_name = "";
10262 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10263 never_nan_nan_name = "_NO_NAN";
10264 }
10265
10266 const char* include_number_compare_name = "";
10267 if (!include_number_compare_) {
10268 include_number_compare_name = "_NO_NUMBER";
10269 }
10270
10271 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010272 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010273 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010274 lhs_name,
10275 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010276 strict_name,
10277 never_nan_nan_name,
10278 include_number_compare_name);
10279 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010280}
10281
10282
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010283int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010284 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10285 // stubs the never NaN NaN condition is only taken into account if the
10286 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010287 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10288 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10289 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010290 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010291 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010292 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010293 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10294 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010295}
10296
10297
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010298// StringCharCodeAtGenerator
10299
10300void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10301 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010302 Label ascii_string;
10303 Label got_char_code;
10304
10305 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010306 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010307
10308 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010309 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10310 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010311 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010312 __ tst(result_, Operand(kIsNotStringMask));
10313 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010314
10315 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010316 __ BranchOnNotSmi(index_, &index_not_smi_);
10317
10318 // Put smi-tagged index into scratch register.
10319 __ mov(scratch_, index_);
10320 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010321
10322 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010323 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10324 __ cmp(ip, Operand(scratch_));
10325 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010326
10327 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010328 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010329 __ tst(result_, Operand(kStringRepresentationMask));
10330 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010331
10332 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010333 __ tst(result_, Operand(kIsConsStringMask));
10334 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010335
10336 // ConsString.
10337 // Check whether the right hand side is the empty string (i.e. if
10338 // this is really a flat string in a cons string). If that is not
10339 // the case we would rather go to the runtime system now to flatten
10340 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010341 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10342 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10343 __ cmp(result_, Operand(ip));
10344 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010345 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010346 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10347 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10348 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10349 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010350 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010351 __ tst(result_, Operand(kStringRepresentationMask));
10352 __ b(nz, &call_runtime_);
10353
10354 // Check for 1-byte or 2-byte string.
10355 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010356 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010357 __ tst(result_, Operand(kStringEncodingMask));
10358 __ b(nz, &ascii_string);
10359
10360 // 2-byte string.
10361 // Load the 2-byte character code into the result register. We can
10362 // add without shifting since the smi tag size is the log2 of the
10363 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010364 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010365 __ add(scratch_, object_, Operand(scratch_));
10366 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10367 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010368
10369 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010370 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010371 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010372 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10373 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010374
10375 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010376 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10377 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010378}
10379
10380
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010381void StringCharCodeAtGenerator::GenerateSlow(
10382 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10383 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010384
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010385 // Index is not a smi.
10386 __ bind(&index_not_smi_);
10387 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010388 __ CheckMap(index_,
10389 scratch_,
10390 Heap::kHeapNumberMapRootIndex,
10391 index_not_number_,
10392 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010393 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010394 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010395 __ push(index_); // Consumed by runtime conversion function.
10396 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10397 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10398 } else {
10399 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10400 // NumberToSmi discards numbers that are not exact integers.
10401 __ CallRuntime(Runtime::kNumberToSmi, 1);
10402 }
10403 if (!scratch_.is(r0)) {
10404 // Save the conversion result before the pop instructions below
10405 // have a chance to overwrite it.
10406 __ mov(scratch_, r0);
10407 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010408 __ pop(index_);
10409 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010410 // Reload the instance type.
10411 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10412 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010413 call_helper.AfterCall(masm);
10414 // If index is still not a smi, it must be out of range.
10415 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10416 // Otherwise, return to the fast path.
10417 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010418
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010419 // Call runtime. We get here when the receiver is a string and the
10420 // index is a number, but the code of getting the actual character
10421 // is too complex (e.g., when the string needs to be flattened).
10422 __ bind(&call_runtime_);
10423 call_helper.BeforeCall(masm);
10424 __ Push(object_, index_);
10425 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
10426 if (!result_.is(r0)) {
10427 __ mov(result_, r0);
10428 }
10429 call_helper.AfterCall(masm);
10430 __ jmp(&exit_);
10431
10432 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10433}
10434
10435
10436// -------------------------------------------------------------------------
10437// StringCharFromCodeGenerator
10438
10439void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010440 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010441 STATIC_ASSERT(kSmiTag == 0);
10442 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010443 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010444 __ tst(code_,
10445 Operand(kSmiTagMask |
10446 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10447 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010448
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010449 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10450 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010451 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010452 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10453 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10454 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10455 __ cmp(result_, Operand(ip));
10456 __ b(eq, &slow_case_);
10457 __ bind(&exit_);
10458}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010459
ager@chromium.orgac091b72010-05-05 07:34:42 +000010460
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010461void StringCharFromCodeGenerator::GenerateSlow(
10462 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10463 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10464
10465 __ bind(&slow_case_);
10466 call_helper.BeforeCall(masm);
10467 __ push(code_);
10468 __ CallRuntime(Runtime::kCharFromCode, 1);
10469 if (!result_.is(r0)) {
10470 __ mov(result_, r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010471 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010472 call_helper.AfterCall(masm);
10473 __ jmp(&exit_);
10474
10475 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10476}
10477
10478
10479// -------------------------------------------------------------------------
10480// StringCharAtGenerator
10481
10482void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10483 char_code_at_generator_.GenerateFast(masm);
10484 char_from_code_generator_.GenerateFast(masm);
10485}
10486
10487
10488void StringCharAtGenerator::GenerateSlow(
10489 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10490 char_code_at_generator_.GenerateSlow(masm, call_helper);
10491 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010492}
10493
10494
10495void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10496 Register dest,
10497 Register src,
10498 Register count,
10499 Register scratch,
10500 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010501 Label loop;
10502 Label done;
10503 // This loop just copies one character at a time, as it is only used for very
10504 // short strings.
10505 if (!ascii) {
10506 __ add(count, count, Operand(count), SetCC);
10507 } else {
10508 __ cmp(count, Operand(0));
10509 }
10510 __ b(eq, &done);
10511
10512 __ bind(&loop);
10513 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10514 // Perform sub between load and dependent store to get the load time to
10515 // complete.
10516 __ sub(count, count, Operand(1), SetCC);
10517 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10518 // last iteration.
10519 __ b(gt, &loop);
10520
10521 __ bind(&done);
10522}
10523
10524
10525enum CopyCharactersFlags {
10526 COPY_ASCII = 1,
10527 DEST_ALWAYS_ALIGNED = 2
10528};
10529
10530
ager@chromium.orgac091b72010-05-05 07:34:42 +000010531void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10532 Register dest,
10533 Register src,
10534 Register count,
10535 Register scratch1,
10536 Register scratch2,
10537 Register scratch3,
10538 Register scratch4,
10539 Register scratch5,
10540 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010541 bool ascii = (flags & COPY_ASCII) != 0;
10542 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10543
10544 if (dest_always_aligned && FLAG_debug_code) {
10545 // Check that destination is actually word aligned if the flag says
10546 // that it is.
10547 __ tst(dest, Operand(kPointerAlignmentMask));
10548 __ Check(eq, "Destination of copy not aligned.");
10549 }
10550
10551 const int kReadAlignment = 4;
10552 const int kReadAlignmentMask = kReadAlignment - 1;
10553 // Ensure that reading an entire aligned word containing the last character
10554 // of a string will not read outside the allocated area (because we pad up
10555 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010556 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010557 // Assumes word reads and writes are little endian.
10558 // Nothing to do for zero characters.
10559 Label done;
10560 if (!ascii) {
10561 __ add(count, count, Operand(count), SetCC);
10562 } else {
10563 __ cmp(count, Operand(0));
10564 }
10565 __ b(eq, &done);
10566
10567 // Assume that you cannot read (or write) unaligned.
10568 Label byte_loop;
10569 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10570 __ cmp(count, Operand(8));
10571 __ add(count, dest, Operand(count));
10572 Register limit = count; // Read until src equals this.
10573 __ b(lt, &byte_loop);
10574
10575 if (!dest_always_aligned) {
10576 // Align dest by byte copying. Copies between zero and three bytes.
10577 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10578 Label dest_aligned;
10579 __ b(eq, &dest_aligned);
10580 __ cmp(scratch4, Operand(2));
10581 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10582 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10583 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10584 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10585 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10586 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10587 __ bind(&dest_aligned);
10588 }
10589
10590 Label simple_loop;
10591
10592 __ sub(scratch4, dest, Operand(src));
10593 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10594 __ b(eq, &simple_loop);
10595 // Shift register is number of bits in a source word that
10596 // must be combined with bits in the next source word in order
10597 // to create a destination word.
10598
10599 // Complex loop for src/dst that are not aligned the same way.
10600 {
10601 Label loop;
10602 __ mov(scratch4, Operand(scratch4, LSL, 3));
10603 Register left_shift = scratch4;
10604 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10605 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10606 // Store the "shift" most significant bits of scratch in the least
10607 // signficant bits (i.e., shift down by (32-shift)).
10608 __ rsb(scratch2, left_shift, Operand(32));
10609 Register right_shift = scratch2;
10610 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10611
10612 __ bind(&loop);
10613 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10614 __ sub(scratch5, limit, Operand(dest));
10615 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10616 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10617 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10618 // Loop if four or more bytes left to copy.
10619 // Compare to eight, because we did the subtract before increasing dst.
10620 __ sub(scratch5, scratch5, Operand(8), SetCC);
10621 __ b(ge, &loop);
10622 }
10623 // There is now between zero and three bytes left to copy (negative that
10624 // number is in scratch5), and between one and three bytes already read into
10625 // scratch1 (eight times that number in scratch4). We may have read past
10626 // the end of the string, but because objects are aligned, we have not read
10627 // past the end of the object.
10628 // Find the minimum of remaining characters to move and preloaded characters
10629 // and write those as bytes.
10630 __ add(scratch5, scratch5, Operand(4), SetCC);
10631 __ b(eq, &done);
10632 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
10633 // Move minimum of bytes read and bytes left to copy to scratch4.
10634 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
10635 // Between one and three (value in scratch5) characters already read into
10636 // scratch ready to write.
10637 __ cmp(scratch5, Operand(2));
10638 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10639 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
10640 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
10641 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
10642 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
10643 // Copy any remaining bytes.
10644 __ b(&byte_loop);
10645
10646 // Simple loop.
10647 // Copy words from src to dst, until less than four bytes left.
10648 // Both src and dest are word aligned.
10649 __ bind(&simple_loop);
10650 {
10651 Label loop;
10652 __ bind(&loop);
10653 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10654 __ sub(scratch3, limit, Operand(dest));
10655 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10656 // Compare to 8, not 4, because we do the substraction before increasing
10657 // dest.
10658 __ cmp(scratch3, Operand(8));
10659 __ b(ge, &loop);
10660 }
10661
10662 // Copy bytes from src to dst until dst hits limit.
10663 __ bind(&byte_loop);
10664 __ cmp(dest, Operand(limit));
10665 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10666 __ b(ge, &done);
10667 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10668 __ b(&byte_loop);
10669
10670 __ bind(&done);
10671}
10672
10673
ager@chromium.orgac091b72010-05-05 07:34:42 +000010674void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10675 Register c1,
10676 Register c2,
10677 Register scratch1,
10678 Register scratch2,
10679 Register scratch3,
10680 Register scratch4,
10681 Register scratch5,
10682 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010683 // Register scratch3 is the general scratch register in this function.
10684 Register scratch = scratch3;
10685
10686 // Make sure that both characters are not digits as such strings has a
10687 // different hash algorithm. Don't try to look for these in the symbol table.
10688 Label not_array_index;
10689 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10690 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10691 __ b(hi, &not_array_index);
10692 __ sub(scratch, c2, Operand(static_cast<int>('0')));
10693 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10694
10695 // If check failed combine both characters into single halfword.
10696 // This is required by the contract of the method: code at the
10697 // not_found branch expects this combination in c1 register
10698 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
10699 __ b(ls, not_found);
10700
10701 __ bind(&not_array_index);
10702 // Calculate the two character string hash.
10703 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010704 StringHelper::GenerateHashInit(masm, hash, c1);
10705 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
10706 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010707
10708 // Collect the two characters in a register.
10709 Register chars = c1;
10710 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
10711
10712 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10713 // hash: hash of two character string.
10714
10715 // Load symbol table
10716 // Load address of first element of the symbol table.
10717 Register symbol_table = c2;
10718 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
10719
10720 // Load undefined value
10721 Register undefined = scratch4;
10722 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
10723
10724 // Calculate capacity mask from the symbol table capacity.
10725 Register mask = scratch2;
10726 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
10727 __ mov(mask, Operand(mask, ASR, 1));
10728 __ sub(mask, mask, Operand(1));
10729
10730 // Calculate untagged address of the first element of the symbol table.
10731 Register first_symbol_table_element = symbol_table;
10732 __ add(first_symbol_table_element, symbol_table,
10733 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
10734
10735 // Registers
10736 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10737 // hash: hash of two character string
10738 // mask: capacity mask
10739 // first_symbol_table_element: address of the first element of
10740 // the symbol table
10741 // scratch: -
10742
10743 // Perform a number of probes in the symbol table.
10744 static const int kProbes = 4;
10745 Label found_in_symbol_table;
10746 Label next_probe[kProbes];
10747 for (int i = 0; i < kProbes; i++) {
10748 Register candidate = scratch5; // Scratch register contains candidate.
10749
10750 // Calculate entry in symbol table.
10751 if (i > 0) {
10752 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
10753 } else {
10754 __ mov(candidate, hash);
10755 }
10756
10757 __ and_(candidate, candidate, Operand(mask));
10758
10759 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010760 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010761 __ ldr(candidate,
10762 MemOperand(first_symbol_table_element,
10763 candidate,
10764 LSL,
10765 kPointerSizeLog2));
10766
10767 // If entry is undefined no string with this hash can be found.
10768 __ cmp(candidate, undefined);
10769 __ b(eq, not_found);
10770
10771 // If length is not 2 the string is not a candidate.
10772 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010773 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010774 __ b(ne, &next_probe[i]);
10775
10776 // Check that the candidate is a non-external ascii string.
10777 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
10778 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
10779 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
10780 &next_probe[i]);
10781
10782 // Check if the two characters match.
10783 // Assumes that word load is little endian.
10784 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
10785 __ cmp(chars, scratch);
10786 __ b(eq, &found_in_symbol_table);
10787 __ bind(&next_probe[i]);
10788 }
10789
10790 // No matching 2 character string found by probing.
10791 __ jmp(not_found);
10792
10793 // Scratch register contains result when we fall through to here.
10794 Register result = scratch;
10795 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000010796 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010797}
10798
10799
ager@chromium.orgac091b72010-05-05 07:34:42 +000010800void StringHelper::GenerateHashInit(MacroAssembler* masm,
10801 Register hash,
10802 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010803 // hash = character + (character << 10);
10804 __ add(hash, character, Operand(character, LSL, 10));
10805 // hash ^= hash >> 6;
10806 __ eor(hash, hash, Operand(hash, ASR, 6));
10807}
10808
10809
ager@chromium.orgac091b72010-05-05 07:34:42 +000010810void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
10811 Register hash,
10812 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010813 // hash += character;
10814 __ add(hash, hash, Operand(character));
10815 // hash += hash << 10;
10816 __ add(hash, hash, Operand(hash, LSL, 10));
10817 // hash ^= hash >> 6;
10818 __ eor(hash, hash, Operand(hash, ASR, 6));
10819}
10820
10821
ager@chromium.orgac091b72010-05-05 07:34:42 +000010822void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
10823 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010824 // hash += hash << 3;
10825 __ add(hash, hash, Operand(hash, LSL, 3));
10826 // hash ^= hash >> 11;
10827 __ eor(hash, hash, Operand(hash, ASR, 11));
10828 // hash += hash << 15;
10829 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
10830
10831 // if (hash == 0) hash = 27;
10832 __ mov(hash, Operand(27), LeaveCC, nz);
10833}
10834
10835
ager@chromium.org5c838252010-02-19 08:53:10 +000010836void SubStringStub::Generate(MacroAssembler* masm) {
10837 Label runtime;
10838
10839 // Stack frame on entry.
10840 // lr: return address
10841 // sp[0]: to
10842 // sp[4]: from
10843 // sp[8]: string
10844
10845 // This stub is called from the native-call %_SubString(...), so
10846 // nothing can be assumed about the arguments. It is tested that:
10847 // "string" is a sequential string,
10848 // both "from" and "to" are smis, and
10849 // 0 <= from <= to <= string.length.
10850 // If any of these assumptions fail, we call the runtime system.
10851
10852 static const int kToOffset = 0 * kPointerSize;
10853 static const int kFromOffset = 1 * kPointerSize;
10854 static const int kStringOffset = 2 * kPointerSize;
10855
10856
10857 // Check bounds and smi-ness.
10858 __ ldr(r7, MemOperand(sp, kToOffset));
10859 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010860 STATIC_ASSERT(kSmiTag == 0);
10861 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010862 // I.e., arithmetic shift right by one un-smi-tags.
10863 __ mov(r2, Operand(r7, ASR, 1), SetCC);
10864 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
10865 // If either r2 or r6 had the smi tag bit set, then carry is set now.
10866 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
10867 __ b(mi, &runtime); // From is negative.
10868
10869 __ sub(r2, r2, Operand(r3), SetCC);
10870 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010871 // Special handling of sub-strings of length 1 and 2. One character strings
10872 // are handled in the runtime system (looked up in the single character
10873 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000010874 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010875 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000010876
10877 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010878 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010879 // r6: from (smi)
10880 // r7: to (smi)
10881
10882 // Make sure first argument is a sequential (or flat) string.
10883 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010884 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010885 __ tst(r5, Operand(kSmiTagMask));
10886 __ b(eq, &runtime);
10887 Condition is_string = masm->IsObjectStringType(r5, r1);
10888 __ b(NegateCondition(is_string), &runtime);
10889
10890 // r1: instance type
10891 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010892 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010893 // r5: string
10894 // r6: from (smi)
10895 // r7: to (smi)
10896 Label seq_string;
10897 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010898 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
10899 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000010900 __ cmp(r4, Operand(kConsStringTag));
10901 __ b(gt, &runtime); // External strings go to runtime.
10902 __ b(lt, &seq_string); // Sequential strings are handled directly.
10903
10904 // Cons string. Try to recurse (once) on the first substring.
10905 // (This adds a little more generality than necessary to handle flattened
10906 // cons strings, but not much).
10907 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
10908 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
10909 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10910 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010911 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010912 __ b(ne, &runtime); // Cons and External strings go to runtime.
10913
10914 // Definitly a sequential string.
10915 __ bind(&seq_string);
10916
10917 // r1: instance type.
10918 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010919 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010920 // r5: string
10921 // r6: from (smi)
10922 // r7: to (smi)
10923 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010924 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000010925 __ b(lt, &runtime); // Fail if to > length.
10926
10927 // r1: instance type.
10928 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010929 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010930 // r5: string.
10931 // r6: from offset (smi)
10932 // Check for flat ascii string.
10933 Label non_ascii_flat;
10934 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010935 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010936 __ b(eq, &non_ascii_flat);
10937
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010938 Label result_longer_than_two;
10939 __ cmp(r2, Operand(2));
10940 __ b(gt, &result_longer_than_two);
10941
10942 // Sub string of length 2 requested.
10943 // Get the two characters forming the sub string.
10944 __ add(r5, r5, Operand(r3));
10945 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
10946 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
10947
10948 // Try to lookup two character string in symbol table.
10949 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010950 StringHelper::GenerateTwoCharacterSymbolTableProbe(
10951 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010952 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10953 __ add(sp, sp, Operand(3 * kPointerSize));
10954 __ Ret();
10955
10956 // r2: result string length.
10957 // r3: two characters combined into halfword in little endian byte order.
10958 __ bind(&make_two_character_string);
10959 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
10960 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
10961 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10962 __ add(sp, sp, Operand(3 * kPointerSize));
10963 __ Ret();
10964
10965 __ bind(&result_longer_than_two);
10966
ager@chromium.org5c838252010-02-19 08:53:10 +000010967 // Allocate the result.
10968 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
10969
10970 // r0: result string.
10971 // r2: result string length.
10972 // r5: string.
10973 // r6: from offset (smi)
10974 // Locate first character of result.
10975 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10976 // Locate 'from' character of string.
10977 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10978 __ add(r5, r5, Operand(r6, ASR, 1));
10979
10980 // r0: result string.
10981 // r1: first character of result string.
10982 // r2: result string length.
10983 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010984 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010985 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
10986 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000010987 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10988 __ add(sp, sp, Operand(3 * kPointerSize));
10989 __ Ret();
10990
10991 __ bind(&non_ascii_flat);
10992 // r2: result string length.
10993 // r5: string.
10994 // r6: from offset (smi)
10995 // Check for flat two byte string.
10996
10997 // Allocate the result.
10998 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
10999
11000 // r0: result string.
11001 // r2: result string length.
11002 // r5: string.
11003 // Locate first character of result.
11004 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11005 // Locate 'from' character of string.
11006 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11007 // As "from" is a smi it is 2 times the value which matches the size of a two
11008 // byte character.
11009 __ add(r5, r5, Operand(r6));
11010
11011 // r0: result string.
11012 // r1: first character of result.
11013 // r2: result length.
11014 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011015 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011016 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11017 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011018 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11019 __ add(sp, sp, Operand(3 * kPointerSize));
11020 __ Ret();
11021
11022 // Just jump to runtime to create the sub string.
11023 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011024 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011025}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011026
11027
11028void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11029 Register left,
11030 Register right,
11031 Register scratch1,
11032 Register scratch2,
11033 Register scratch3,
11034 Register scratch4) {
11035 Label compare_lengths;
11036 // Find minimum length and length difference.
11037 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11038 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11039 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11040 Register length_delta = scratch3;
11041 __ mov(scratch1, scratch2, LeaveCC, gt);
11042 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011043 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011044 __ tst(min_length, Operand(min_length));
11045 __ b(eq, &compare_lengths);
11046
ager@chromium.orgac091b72010-05-05 07:34:42 +000011047 // Untag smi.
11048 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11049
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011050 // Setup registers so that we only need to increment one register
11051 // in the loop.
11052 __ add(scratch2, min_length,
11053 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11054 __ add(left, left, Operand(scratch2));
11055 __ add(right, right, Operand(scratch2));
11056 // Registers left and right points to the min_length character of strings.
11057 __ rsb(min_length, min_length, Operand(-1));
11058 Register index = min_length;
11059 // Index starts at -min_length.
11060
11061 {
11062 // Compare loop.
11063 Label loop;
11064 __ bind(&loop);
11065 // Compare characters.
11066 __ add(index, index, Operand(1), SetCC);
11067 __ ldrb(scratch2, MemOperand(left, index), ne);
11068 __ ldrb(scratch4, MemOperand(right, index), ne);
11069 // Skip to compare lengths with eq condition true.
11070 __ b(eq, &compare_lengths);
11071 __ cmp(scratch2, scratch4);
11072 __ b(eq, &loop);
11073 // Fallthrough with eq condition false.
11074 }
11075 // Compare lengths - strings up to min-length are equal.
11076 __ bind(&compare_lengths);
11077 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11078 // Use zero length_delta as result.
11079 __ mov(r0, Operand(length_delta), SetCC, eq);
11080 // Fall through to here if characters compare not-equal.
11081 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11082 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11083 __ Ret();
11084}
11085
11086
11087void StringCompareStub::Generate(MacroAssembler* masm) {
11088 Label runtime;
11089
11090 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011091 // sp[0]: right string
11092 // sp[4]: left string
11093 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11094 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011095
11096 Label not_same;
11097 __ cmp(r0, r1);
11098 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011099 STATIC_ASSERT(EQUAL == 0);
11100 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011101 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11102 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11103 __ add(sp, sp, Operand(2 * kPointerSize));
11104 __ Ret();
11105
11106 __ bind(&not_same);
11107
11108 // Check that both objects are sequential ascii strings.
11109 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11110
11111 // Compare flat ascii strings natively. Remove arguments from stack first.
11112 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11113 __ add(sp, sp, Operand(2 * kPointerSize));
11114 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11115
11116 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11117 // tagged as a small integer.
11118 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011119 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011120}
11121
11122
ager@chromium.org5c838252010-02-19 08:53:10 +000011123void StringAddStub::Generate(MacroAssembler* masm) {
11124 Label string_add_runtime;
11125 // Stack on entry:
11126 // sp[0]: second argument.
11127 // sp[4]: first argument.
11128
11129 // Load the two arguments.
11130 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11131 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11132
11133 // Make sure that both arguments are strings if not known in advance.
11134 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011135 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011136 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11137 // Load instance types.
11138 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11139 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11140 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11141 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011142 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011143 // If either is not a string, go to runtime.
11144 __ tst(r4, Operand(kIsNotStringMask));
11145 __ tst(r5, Operand(kIsNotStringMask), eq);
11146 __ b(ne, &string_add_runtime);
11147 }
11148
11149 // Both arguments are strings.
11150 // r0: first string
11151 // r1: second string
11152 // r4: first string instance type (if string_check_)
11153 // r5: second string instance type (if string_check_)
11154 {
11155 Label strings_not_empty;
11156 // Check if either of the strings are empty. In that case return the other.
11157 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11158 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011159 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011160 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011161 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011162 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011163 // Else test if second string is empty.
11164 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011165 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11166
11167 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11168 __ add(sp, sp, Operand(2 * kPointerSize));
11169 __ Ret();
11170
11171 __ bind(&strings_not_empty);
11172 }
11173
ager@chromium.orgac091b72010-05-05 07:34:42 +000011174 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11175 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011176 // Both strings are non-empty.
11177 // r0: first string
11178 // r1: second string
11179 // r2: length of first string
11180 // r3: length of second string
11181 // r4: first string instance type (if string_check_)
11182 // r5: second string instance type (if string_check_)
11183 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011184 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011185 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011186 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011187 __ add(r6, r2, Operand(r3));
11188 // Use the runtime system when adding two one character strings, as it
11189 // contains optimizations for this specific case using the symbol table.
11190 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011191 __ b(ne, &longer_than_two);
11192
11193 // Check that both strings are non-external ascii strings.
11194 if (!string_check_) {
11195 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11196 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11197 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11198 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11199 }
11200 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11201 &string_add_runtime);
11202
11203 // Get the two characters forming the sub string.
11204 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11205 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11206
11207 // Try to lookup two character string in symbol table. If it is not found
11208 // just allocate a new one.
11209 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011210 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11211 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011212 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11213 __ add(sp, sp, Operand(2 * kPointerSize));
11214 __ Ret();
11215
11216 __ bind(&make_two_character_string);
11217 // Resulting string has length 2 and first chars of two strings
11218 // are combined into single halfword in r2 register.
11219 // So we can fill resulting string without two loops by a single
11220 // halfword store instruction (which assumes that processor is
11221 // in a little endian mode)
11222 __ mov(r6, Operand(2));
11223 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11224 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11225 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11226 __ add(sp, sp, Operand(2 * kPointerSize));
11227 __ Ret();
11228
11229 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011230 // Check if resulting string will be flat.
11231 __ cmp(r6, Operand(String::kMinNonFlatLength));
11232 __ b(lt, &string_add_flat_result);
11233 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011234 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011235 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11236 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11237 __ cmp(r6, Operand(String::kMaxLength + 1));
11238 __ b(hs, &string_add_runtime);
11239
11240 // If result is not supposed to be flat, allocate a cons string object.
11241 // If both strings are ascii the result is an ascii cons string.
11242 if (!string_check_) {
11243 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11244 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11245 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11246 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11247 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011248 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011249 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011250 __ tst(r4, Operand(kStringEncodingMask));
11251 __ tst(r5, Operand(kStringEncodingMask), ne);
11252 __ b(eq, &non_ascii);
11253
11254 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011255 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011256 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11257 __ bind(&allocated);
11258 // Fill the fields of the cons string.
11259 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11260 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11261 __ mov(r0, Operand(r7));
11262 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11263 __ add(sp, sp, Operand(2 * kPointerSize));
11264 __ Ret();
11265
11266 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011267 // At least one of the strings is two-byte. Check whether it happens
11268 // to contain only ascii characters.
11269 // r4: first instance type.
11270 // r5: second instance type.
11271 __ tst(r4, Operand(kAsciiDataHintMask));
11272 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11273 __ b(ne, &ascii_data);
11274 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011275 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011276 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11277 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11278 __ b(eq, &ascii_data);
11279
ager@chromium.org5c838252010-02-19 08:53:10 +000011280 // Allocate a two byte cons string.
11281 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11282 __ jmp(&allocated);
11283
11284 // Handle creating a flat result. First check that both strings are
11285 // sequential and that they have the same encoding.
11286 // r0: first string
11287 // r1: second string
11288 // r2: length of first string
11289 // r3: length of second string
11290 // r4: first string instance type (if string_check_)
11291 // r5: second string instance type (if string_check_)
11292 // r6: sum of lengths.
11293 __ bind(&string_add_flat_result);
11294 if (!string_check_) {
11295 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11296 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11297 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11298 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11299 }
11300 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011301 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011302 __ tst(r4, Operand(kStringRepresentationMask));
11303 __ tst(r5, Operand(kStringRepresentationMask), eq);
11304 __ b(ne, &string_add_runtime);
11305 // Now check if both strings have the same encoding (ASCII/Two-byte).
11306 // r0: first string.
11307 // r1: second string.
11308 // r2: length of first string.
11309 // r3: length of second string.
11310 // r6: sum of lengths..
11311 Label non_ascii_string_add_flat_result;
11312 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11313 __ eor(r7, r4, Operand(r5));
11314 __ tst(r7, Operand(kStringEncodingMask));
11315 __ b(ne, &string_add_runtime);
11316 // And see if it's ASCII or two-byte.
11317 __ tst(r4, Operand(kStringEncodingMask));
11318 __ b(eq, &non_ascii_string_add_flat_result);
11319
11320 // Both strings are sequential ASCII strings. We also know that they are
11321 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011322 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011323 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11324 // Locate first character of result.
11325 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11326 // Locate first character of first argument.
11327 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11328 // r0: first character of first string.
11329 // r1: second string.
11330 // r2: length of first string.
11331 // r3: length of second string.
11332 // r6: first character of result.
11333 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011334 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011335
11336 // Load second argument and locate first character.
11337 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11338 // r1: first character of second string.
11339 // r3: length of second string.
11340 // r6: next character of result.
11341 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011342 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011343 __ mov(r0, Operand(r7));
11344 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11345 __ add(sp, sp, Operand(2 * kPointerSize));
11346 __ Ret();
11347
11348 __ bind(&non_ascii_string_add_flat_result);
11349 // Both strings are sequential two byte strings.
11350 // r0: first string.
11351 // r1: second string.
11352 // r2: length of first string.
11353 // r3: length of second string.
11354 // r6: sum of length of strings.
11355 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11356 // r0: first string.
11357 // r1: second string.
11358 // r2: length of first string.
11359 // r3: length of second string.
11360 // r7: result string.
11361
11362 // Locate first character of result.
11363 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11364 // Locate first character of first argument.
11365 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11366
11367 // r0: first character of first string.
11368 // r1: second string.
11369 // r2: length of first string.
11370 // r3: length of second string.
11371 // r6: first character of result.
11372 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011373 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011374
11375 // Locate first character of second argument.
11376 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11377
11378 // r1: first character of second string.
11379 // r3: length of second string.
11380 // r6: next character of result (after copy of first string).
11381 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011382 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011383
11384 __ mov(r0, Operand(r7));
11385 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11386 __ add(sp, sp, Operand(2 * kPointerSize));
11387 __ Ret();
11388
11389 // Just jump to runtime to add the two strings.
11390 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011391 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011392}
11393
11394
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011395#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011396
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011397} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011398
11399#endif // V8_TARGET_ARCH_ARM