blob: aec80d731590514a144ebc22ce75d0827407ff8f [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);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001535 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001536 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001537 __ cmp(r1, Operand(apply_code));
1538 __ b(ne, &build_args);
1539
1540 // Check that applicand is a function.
1541 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1542 __ BranchOnSmi(r1, &build_args);
1543 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1544 __ b(ne, &build_args);
1545
1546 // Copy the arguments to this function possibly from the
1547 // adaptor frame below it.
1548 Label invoke, adapted;
1549 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1550 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1551 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1552 __ b(eq, &adapted);
1553
1554 // No arguments adaptor frame. Copy fixed number of arguments.
1555 __ mov(r0, Operand(scope()->num_parameters()));
1556 for (int i = 0; i < scope()->num_parameters(); i++) {
1557 __ ldr(r2, frame_->ParameterAt(i));
1558 __ push(r2);
1559 }
1560 __ jmp(&invoke);
1561
1562 // Arguments adaptor frame present. Copy arguments from there, but
1563 // avoid copying too many arguments to avoid stack overflows.
1564 __ bind(&adapted);
1565 static const uint32_t kArgumentsLimit = 1 * KB;
1566 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1567 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1568 __ mov(r3, r0);
1569 __ cmp(r0, Operand(kArgumentsLimit));
1570 __ b(gt, &build_args);
1571
1572 // Loop through the arguments pushing them onto the execution
1573 // stack. We don't inform the virtual frame of the push, so we don't
1574 // have to worry about getting rid of the elements from the virtual
1575 // frame.
1576 Label loop;
1577 // r3 is a small non-negative integer, due to the test above.
1578 __ cmp(r3, Operand(0));
1579 __ b(eq, &invoke);
1580 // Compute the address of the first argument.
1581 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1582 __ add(r2, r2, Operand(kPointerSize));
1583 __ bind(&loop);
1584 // Post-decrement argument address by kPointerSize on each iteration.
1585 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1586 __ push(r4);
1587 __ sub(r3, r3, Operand(1), SetCC);
1588 __ b(gt, &loop);
1589
1590 // Invoke the function.
1591 __ bind(&invoke);
1592 ParameterCount actual(r0);
1593 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1594 // Drop applicand.apply and applicand from the stack, and push
1595 // the result of the function call, but leave the spilled frame
1596 // unchanged, with 3 elements, so it is correct when we compile the
1597 // slow-case code.
1598 __ add(sp, sp, Operand(2 * kPointerSize));
1599 __ push(r0);
1600 // Stack now has 1 element:
1601 // sp[0]: result
1602 __ jmp(&done);
1603
1604 // Slow-case: Allocate the arguments object since we know it isn't
1605 // there, and fall-through to the slow-case where we call
1606 // applicand.apply.
1607 __ bind(&build_args);
1608 // Stack now has 3 elements, because we have jumped from where:
1609 // sp[0]: receiver
1610 // sp[1]: applicand.apply
1611 // sp[2]: applicand.
1612 StoreArgumentsObject(false);
1613
1614 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001615 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001616
1617 // Generic computation of x.apply(y, args) with no special optimization.
1618 // Flip applicand.apply and applicand on the stack, so
1619 // applicand looks like the receiver of the applicand.apply call.
1620 // Then process it as a normal function call.
1621 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1622 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001623 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001624
1625 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1626 frame_->CallStub(&call_function, 3);
1627 // The function and its two arguments have been dropped.
1628 frame_->Drop(); // Drop the receiver as well.
1629 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001630 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001631 // Stack now has 1 element:
1632 // sp[0]: result
1633 __ bind(&done);
1634
1635 // Restore the context register after a call.
1636 __ ldr(cp, frame_->Context());
1637}
1638
1639
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 ASSERT(has_cc());
1642 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001643 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 cc_reg_ = al;
1645}
1646
1647
ager@chromium.org7c537e22008-10-16 08:43:32 +00001648void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001649 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001650 Comment cmnt(masm_, "[ check stack");
1651 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1652 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1653 // the implicit 8 byte offset that always applies to operations with pc and
1654 // gives a return address 12 bytes down.
1655 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1656 masm_->cmp(sp, Operand(ip));
1657 StackCheckStub stub;
1658 // Call the stub if lower.
1659 masm_->mov(pc,
1660 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1661 RelocInfo::CODE_TARGET),
1662 LeaveCC,
1663 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664}
1665
1666
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001667void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1668#ifdef DEBUG
1669 int original_height = frame_->height();
1670#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001671 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001672 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001673 }
1674 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1675}
1676
1677
ager@chromium.org7c537e22008-10-16 08:43:32 +00001678void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679#ifdef DEBUG
1680 int original_height = frame_->height();
1681#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001683 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001684 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001685 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001686 if (node->break_target()->is_linked()) {
1687 node->break_target()->Bind();
1688 }
1689 node->break_target()->Unuse();
1690 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691}
1692
1693
ager@chromium.org7c537e22008-10-16 08:43:32 +00001694void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001695 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001696 frame_->EmitPush(Operand(pairs));
1697 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1698
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001699 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001700 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701}
1702
1703
ager@chromium.org7c537e22008-10-16 08:43:32 +00001704void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001705#ifdef DEBUG
1706 int original_height = frame_->height();
1707#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 Comment cmnt(masm_, "[ Declaration");
1709 Variable* var = node->proxy()->var();
1710 ASSERT(var != NULL); // must have been resolved
1711 Slot* slot = var->slot();
1712
1713 // If it was not possible to allocate the variable at compile time,
1714 // we need to "declare" it at runtime to make sure it actually
1715 // exists in the local context.
1716 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1717 // Variables with a "LOOKUP" slot were introduced as non-locals
1718 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001719 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001721 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001722 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 // Declaration nodes are always declared in only two modes.
1724 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1725 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001726 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 // Push initial value, if any.
1728 // Note: For variables we must not push an initial value (such as
1729 // 'undefined') because we may have a (legal) redeclaration and we
1730 // must not destroy the current value.
1731 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001732 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001734 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001736 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001738
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001740 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001741
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001742 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 return;
1744 }
1745
1746 ASSERT(!var->is_global());
1747
1748 // If we have a function or a constant, we need to initialize the variable.
1749 Expression* val = NULL;
1750 if (node->mode() == Variable::CONST) {
1751 val = new Literal(Factory::the_hole_value());
1752 } else {
1753 val = node->fun(); // NULL if we don't have a function
1754 }
1755
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001756
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001758 WriteBarrierCharacter wb_info =
1759 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1760 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001761 // Set initial value.
1762 Reference target(this, node->proxy());
1763 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001764 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001765
iposva@chromium.org245aa852009-02-10 00:49:54 +00001766 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001769 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770}
1771
1772
ager@chromium.org7c537e22008-10-16 08:43:32 +00001773void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001774#ifdef DEBUG
1775 int original_height = frame_->height();
1776#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 Expression* expression = node->expression();
1780 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001781 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 frame_->Drop();
1783 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784}
1785
1786
ager@chromium.org7c537e22008-10-16 08:43:32 +00001787void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788#ifdef DEBUG
1789 int original_height = frame_->height();
1790#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795}
1796
1797
ager@chromium.org7c537e22008-10-16 08:43:32 +00001798void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001799#ifdef DEBUG
1800 int original_height = frame_->height();
1801#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803 // Generate different code depending on which parts of the if statement
1804 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 bool has_then_stm = node->HasThenStatement();
1806 bool has_else_stm = node->HasElseStatement();
1807
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001808 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001810 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001812 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001813 JumpTarget then;
1814 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001816 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 if (frame_ != NULL) {
1818 Branch(false, &else_);
1819 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821 if (frame_ != NULL || then.is_linked()) {
1822 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001823 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 }
1825 if (frame_ != NULL) {
1826 exit.Jump();
1827 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829 if (else_.is_linked()) {
1830 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001831 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001835 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001837 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001839 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001840 if (frame_ != NULL) {
1841 Branch(false, &exit);
1842 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 if (frame_ != NULL || then.is_linked()) {
1845 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001846 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848
1849 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001850 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001852 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001854 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 if (frame_ != NULL) {
1856 Branch(true, &exit);
1857 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 if (frame_ != NULL || else_.is_linked()) {
1860 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001861 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863
1864 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001865 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 ASSERT(!has_then_stm && !has_else_stm);
1867 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001868 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 if (frame_ != NULL) {
1870 if (has_cc()) {
1871 cc_reg_ = al;
1872 } else {
1873 frame_->Drop();
1874 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 }
1876 }
1877
1878 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001879 if (exit.is_linked()) {
1880 exit.Bind();
1881 }
1882 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883}
1884
1885
ager@chromium.org7c537e22008-10-16 08:43:32 +00001886void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 CodeForStatementPosition(node);
1889 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890}
1891
1892
ager@chromium.org7c537e22008-10-16 08:43:32 +00001893void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001895 CodeForStatementPosition(node);
1896 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897}
1898
1899
ager@chromium.org7c537e22008-10-16 08:43:32 +00001900void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001901 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001903
ager@chromium.org4af710e2009-09-15 12:20:11 +00001904 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001905 Load(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 frame_->EmitPop(r0);
1908 function_return_.Jump();
1909 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910 // Pop the result from the frame and prepare the frame for
1911 // returning thus making it easier to merge.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001912 frame_->PopToR0();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001913 frame_->PrepareForReturn();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001914 if (function_return_.is_bound()) {
1915 // If the function return label is already bound we reuse the
1916 // code by jumping to the return site.
1917 function_return_.Jump();
1918 } else {
1919 function_return_.Bind();
1920 GenerateReturnSequence();
1921 }
1922 }
1923}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001924
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001925
1926void CodeGenerator::GenerateReturnSequence() {
1927 if (FLAG_trace) {
1928 // Push the return value on the stack as the parameter.
1929 // Runtime::TraceExit returns the parameter as it is.
1930 frame_->EmitPush(r0);
1931 frame_->CallRuntime(Runtime::kTraceExit, 1);
1932 }
1933
1934#ifdef DEBUG
1935 // Add a label for checking the size of the code used for returning.
1936 Label check_exit_codesize;
1937 masm_->bind(&check_exit_codesize);
1938#endif
1939 // Make sure that the constant pool is not emitted inside of the return
1940 // sequence.
1941 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1942 // Tear down the frame which will restore the caller's frame pointer and
1943 // the link register.
1944 frame_->Exit();
1945
1946 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1947 // tool from instrumenting as we rely on the code size here.
1948 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1949 masm_->add(sp, sp, Operand(sp_delta));
1950 masm_->Jump(lr);
1951 DeleteFrame();
1952
1953#ifdef DEBUG
1954 // Check that the size of the code used for returning matches what is
1955 // expected by the debugger. If the sp_delts above cannot be encoded in
1956 // the add instruction the add will generate two instructions.
1957 int return_sequence_length =
1958 masm_->InstructionsGeneratedSince(&check_exit_codesize);
1959 CHECK(return_sequence_length ==
1960 Assembler::kJSReturnSequenceInstructions ||
1961 return_sequence_length ==
1962 Assembler::kJSReturnSequenceInstructions + 1);
1963#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965}
1966
1967
ager@chromium.org7c537e22008-10-16 08:43:32 +00001968void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969#ifdef DEBUG
1970 int original_height = frame_->height();
1971#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001973 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001974 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001975 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001976 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001977 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001978 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001979 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001980#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001981 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001982 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001983 verified_true.Branch(eq);
1984 __ stop("PushContext: r0 is expected to be the same as cp");
1985 verified_true.Bind();
1986#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001988 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990}
1991
1992
ager@chromium.org7c537e22008-10-16 08:43:32 +00001993void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994#ifdef DEBUG
1995 int original_height = frame_->height();
1996#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 // Pop context.
2000 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2001 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002002 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002003 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004}
2005
2006
ager@chromium.org7c537e22008-10-16 08:43:32 +00002007void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002008#ifdef DEBUG
2009 int original_height = frame_->height();
2010#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002013 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002015 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002016
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002017 JumpTarget next_test;
2018 JumpTarget fall_through;
2019 JumpTarget default_entry;
2020 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 ZoneList<CaseClause*>* cases = node->cases();
2022 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024
2025 for (int i = 0; i < length; i++) {
2026 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 // Remember the default clause and compile it at the end.
2029 default_clause = clause;
2030 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 }
2032
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002033 Comment cmnt(masm_, "[ Case clause");
2034 // Compile the test.
2035 next_test.Bind();
2036 next_test.Unuse();
2037 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002038 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002039 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002041
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 // Before entering the body from the test, remove the switch value from
2043 // the stack.
2044 frame_->Drop();
2045
2046 // Label the body so that fall through is enabled.
2047 if (i > 0 && cases->at(i - 1)->is_default()) {
2048 default_exit.Bind();
2049 } else {
2050 fall_through.Bind();
2051 fall_through.Unuse();
2052 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002053 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054
2055 // If control flow can fall through from the body, jump to the next body
2056 // or the end of the statement.
2057 if (frame_ != NULL) {
2058 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2059 default_entry.Jump();
2060 } else {
2061 fall_through.Jump();
2062 }
2063 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 }
2065
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002066 // The final "test" removes the switch value.
2067 next_test.Bind();
2068 frame_->Drop();
2069
2070 // If there is a default clause, compile it.
2071 if (default_clause != NULL) {
2072 Comment cmnt(masm_, "[ Default clause");
2073 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002074 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002076 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 if (frame_ != NULL && default_exit.is_bound()) {
2078 default_exit.Jump();
2079 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002080 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 if (fall_through.is_linked()) {
2083 fall_through.Bind();
2084 }
2085
2086 if (node->break_target()->is_linked()) {
2087 node->break_target()->Bind();
2088 }
2089 node->break_target()->Unuse();
2090 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091}
2092
2093
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002094void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002095#ifdef DEBUG
2096 int original_height = frame_->height();
2097#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002098 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002100 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002101 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002102 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002104 // Label the top of the loop for the backward CFG edge. If the test
2105 // is always true we can use the continue target, and if the test is
2106 // always false there is no need.
2107 ConditionAnalysis info = AnalyzeCondition(node->cond());
2108 switch (info) {
2109 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002110 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002111 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002112 break;
2113 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002114 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002115 break;
2116 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002117 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002118 body.Bind();
2119 break;
2120 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002122 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002123 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002124
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002125 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002126 switch (info) {
2127 case ALWAYS_TRUE:
2128 // If control can fall off the end of the body, jump back to the
2129 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002131 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002134 case ALWAYS_FALSE:
2135 // If we have a continue in the body, we only have to bind its
2136 // jump target.
2137 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002140 break;
2141 case DONT_KNOW:
2142 // We have to compile the test expression if it can be reached by
2143 // control flow falling out of the body or via continue.
2144 if (node->continue_target()->is_linked()) {
2145 node->continue_target()->Bind();
2146 }
2147 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002148 Comment cmnt(masm_, "[ DoWhileCondition");
2149 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002150 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002152 // A invalid frame here indicates that control did not
2153 // fall out of the test expression.
2154 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002155 }
2156 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 break;
2158 }
2159
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 if (node->break_target()->is_linked()) {
2161 node->break_target()->Bind();
2162 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002163 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002164 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2165}
2166
2167
2168void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2169#ifdef DEBUG
2170 int original_height = frame_->height();
2171#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002172 Comment cmnt(masm_, "[ WhileStatement");
2173 CodeForStatementPosition(node);
2174
2175 // If the test is never true and has no side effects there is no need
2176 // to compile the test or body.
2177 ConditionAnalysis info = AnalyzeCondition(node->cond());
2178 if (info == ALWAYS_FALSE) return;
2179
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002180 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002181 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002182
2183 // Label the top of the loop with the continue target for the backward
2184 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002185 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002186 node->continue_target()->Bind();
2187
2188 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002189 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002190 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002191 if (has_valid_frame()) {
2192 // A NULL frame indicates that control did not fall out of the
2193 // test expression.
2194 Branch(false, node->break_target());
2195 }
2196 if (has_valid_frame() || body.is_linked()) {
2197 body.Bind();
2198 }
2199 }
2200
2201 if (has_valid_frame()) {
2202 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002203 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002204
2205 // If control flow can fall out of the body, jump back to the top.
2206 if (has_valid_frame()) {
2207 node->continue_target()->Jump();
2208 }
2209 }
2210 if (node->break_target()->is_linked()) {
2211 node->break_target()->Bind();
2212 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002213 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002214 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2215}
2216
2217
2218void CodeGenerator::VisitForStatement(ForStatement* node) {
2219#ifdef DEBUG
2220 int original_height = frame_->height();
2221#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002222 Comment cmnt(masm_, "[ ForStatement");
2223 CodeForStatementPosition(node);
2224 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002225 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002226 }
2227
2228 // If the test is never true there is no need to compile the test or
2229 // body.
2230 ConditionAnalysis info = AnalyzeCondition(node->cond());
2231 if (info == ALWAYS_FALSE) return;
2232
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002233 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002234 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002235
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002236 // We know that the loop index is a smi if it is not modified in the
2237 // loop body and it is checked against a constant limit in the loop
2238 // condition. In this case, we reset the static type information of the
2239 // loop index to smi before compiling the body, the update expression, and
2240 // the bottom check of the loop condition.
2241 TypeInfoCodeGenState type_info_scope(this,
2242 node->is_fast_smi_loop() ?
2243 node->loop_variable()->slot() :
2244 NULL,
2245 TypeInfo::Smi());
2246
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002247 // If there is no update statement, label the top of the loop with the
2248 // continue target, otherwise with the loop target.
2249 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2250 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002251 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002252 node->continue_target()->Bind();
2253 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002254 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002255 loop.Bind();
2256 }
2257
2258 // If the test is always true, there is no need to compile it.
2259 if (info == DONT_KNOW) {
2260 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002261 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002262 if (has_valid_frame()) {
2263 Branch(false, node->break_target());
2264 }
2265 if (has_valid_frame() || body.is_linked()) {
2266 body.Bind();
2267 }
2268 }
2269
2270 if (has_valid_frame()) {
2271 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002272 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002273
2274 if (node->next() == NULL) {
2275 // If there is no update statement and control flow can fall out
2276 // of the loop, jump directly to the continue label.
2277 if (has_valid_frame()) {
2278 node->continue_target()->Jump();
2279 }
2280 } else {
2281 // If there is an update statement and control flow can reach it
2282 // via falling out of the body of the loop or continuing, we
2283 // compile the update statement.
2284 if (node->continue_target()->is_linked()) {
2285 node->continue_target()->Bind();
2286 }
2287 if (has_valid_frame()) {
2288 // Record source position of the statement as this code which is
2289 // after the code for the body actually belongs to the loop
2290 // statement and not the body.
2291 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002292 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002293 loop.Jump();
2294 }
2295 }
2296 }
2297 if (node->break_target()->is_linked()) {
2298 node->break_target()->Bind();
2299 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002300 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302}
2303
2304
ager@chromium.org7c537e22008-10-16 08:43:32 +00002305void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002306#ifdef DEBUG
2307 int original_height = frame_->height();
2308#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002309 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002311 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002313 JumpTarget primitive;
2314 JumpTarget jsobject;
2315 JumpTarget fixed_array;
2316 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2317 JumpTarget end_del_check;
2318 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
2320 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002321 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322
2323 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2324 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002325 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002326 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2327 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002329 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2330 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002331 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
2333 // Stack layout in body:
2334 // [iteration counter (Smi)]
2335 // [length of array]
2336 // [FixedArray]
2337 // [Map or 0]
2338 // [Object]
2339
2340 // Check if enumerable is already a JSObject
2341 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002342 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002343 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002344 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 primitive.Bind();
2347 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002348 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002350 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002352 // r0: value to be iterated over
2353 frame_->EmitPush(r0); // Push the object being iterated over.
2354
2355 // Check cache validity in generated code. This is a fast case for
2356 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2357 // guarantee cache validity, call the runtime system to check cache
2358 // validity or get the property names in a fixed array.
2359 JumpTarget call_runtime;
2360 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2361 JumpTarget check_prototype;
2362 JumpTarget use_cache;
2363 __ mov(r1, Operand(r0));
2364 loop.Bind();
2365 // Check that there are no elements.
2366 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2367 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2368 __ cmp(r2, r4);
2369 call_runtime.Branch(ne);
2370 // Check that instance descriptors are not empty so that we can
2371 // check for an enum cache. Leave the map in r3 for the subsequent
2372 // prototype load.
2373 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2374 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2375 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2376 __ cmp(r2, ip);
2377 call_runtime.Branch(eq);
2378 // Check that there in an enum cache in the non-empty instance
2379 // descriptors. This is the case if the next enumeration index
2380 // field does not contain a smi.
2381 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2382 __ tst(r2, Operand(kSmiTagMask));
2383 call_runtime.Branch(eq);
2384 // For all objects but the receiver, check that the cache is empty.
2385 // r4: empty fixed array root.
2386 __ cmp(r1, r0);
2387 check_prototype.Branch(eq);
2388 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2389 __ cmp(r2, r4);
2390 call_runtime.Branch(ne);
2391 check_prototype.Bind();
2392 // Load the prototype from the map and loop if non-null.
2393 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2394 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2395 __ cmp(r1, ip);
2396 loop.Branch(ne);
2397 // The enum cache is valid. Load the map of the object being
2398 // iterated over and use the cache for the iteration.
2399 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2400 use_cache.Jump();
2401
2402 call_runtime.Bind();
2403 // Call the runtime to get the property names for the object.
2404 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002405 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002407 // If we got a map from the runtime call, we can do a fast
2408 // modification check. Otherwise, we got a fixed array, and we have
2409 // to do a slow check.
2410 // r0: map or fixed array (result from call to
2411 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412 __ mov(r2, Operand(r0));
2413 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002414 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2415 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002416 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002418 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002420 // r0: map (either the result from a call to
2421 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2422 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002423 __ mov(r1, Operand(r0));
2424 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2425 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2426 __ ldr(r2,
2427 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2428
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002429 frame_->EmitPush(r0); // map
2430 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002431 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002433 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002434 frame_->EmitPush(r0);
2435 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002437 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002439 frame_->EmitPush(r1); // insert 0 in place of Map
2440 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441
2442 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002443 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002445 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002446 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447
2448 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002450 // sp[0] : index
2451 // sp[1] : array/enum cache length
2452 // sp[2] : array or enum cache
2453 // sp[3] : 0 or map
2454 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 // Grab the current frame's height for the break and continue
2456 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002457 node->break_target()->SetExpectedHeight();
2458 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002460 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002461 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002462 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002463 node->break_target()->Branch(hs);
2464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2468 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2469
2470 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 // Check if this (still) matches the map of the enumerable.
2473 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002474 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2476 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478
2479 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002480 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2481 frame_->EmitPush(r0);
2482 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002483 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002484 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485
2486 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002487 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2488 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002489 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002491 end_del_check.Bind();
2492 // Store the entry in the 'each' expression and take another spin in the
2493 // loop. r3: i'th entry of the enum cache (or string there of)
2494 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 { Reference each(this, node->each());
2496 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002497 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 __ ldr(r0, frame_->ElementAt(each.size()));
2499 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002500 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002501 frame_->Drop(2);
2502 } else {
2503 // If the reference was to a slot we rely on the convenient property
2504 // that it doesn't matter whether a value (eg, r3 pushed above) is
2505 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002506 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002507 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002508 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 }
2510 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002511 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002513 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002514
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002515 // Next. Reestablish a spilled frame in case we are coming here via
2516 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002517 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002518 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002519 frame_->EmitPop(r0);
2520 __ add(r0, r0, Operand(Smi::FromInt(1)));
2521 frame_->EmitPush(r0);
2522 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002524 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2525 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002526 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002527 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528
2529 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002530 exit.Bind();
2531 node->continue_target()->Unuse();
2532 node->break_target()->Unuse();
2533 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534}
2535
2536
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002537void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002538#ifdef DEBUG
2539 int original_height = frame_->height();
2540#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002541 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002542 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002545 JumpTarget try_block;
2546 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551
2552 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002553 Variable* catch_var = node->catch_var()->var();
2554 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2555 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556
2557 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002558 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002560 VisitStatements(node->catch_block()->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 if (frame_ != NULL) {
2562 exit.Jump();
2563 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564
2565
2566 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002569 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2570 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002572 // Shadow the labels for all escapes from the try block, including
2573 // returns. During shadowing, the original label is hidden as the
2574 // LabelShadow and operations on the original actually affect the
2575 // shadowing label.
2576 //
2577 // We should probably try to unify the escaping labels and the return
2578 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002579 int nof_escapes = node->escaping_targets()->length();
2580 List<ShadowTarget*> shadows(1 + nof_escapes);
2581
2582 // Add the shadow target for the function return.
2583 static const int kReturnShadowIndex = 0;
2584 shadows.Add(new ShadowTarget(&function_return_));
2585 bool function_return_was_shadowed = function_return_is_shadowed_;
2586 function_return_is_shadowed_ = true;
2587 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2588
2589 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 }
2593
2594 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002595 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596
2597 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002598 // After shadowing stops, the original labels are unshadowed and the
2599 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002600 bool has_unlinks = false;
2601 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 has_unlinks = has_unlinks || shadows[i]->is_linked();
2604 }
2605 function_return_is_shadowed_ = function_return_was_shadowed;
2606
2607 // Get an external reference to the handler address.
2608 ExternalReference handler_address(Top::k_handler_address);
2609
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002610 // If we can fall off the end of the try block, unlink from try chain.
2611 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002612 // The next handler address is on top of the frame. Unlink from
2613 // the handler list and drop the rest of this handler from the
2614 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002615 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002616 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617 __ mov(r3, Operand(handler_address));
2618 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002619 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 if (has_unlinks) {
2621 exit.Jump();
2622 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 }
2624
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002625 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626 // jumped to. Deallocate each shadow target.
2627 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002629 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630 shadows[i]->Bind();
2631 // Because we can be jumping here (to spilled code) from unspilled
2632 // code, we need to reestablish a spilled frame at this block.
2633 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 // Reload sp from the top handler, because some statements that we
2636 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002637 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002639 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002641 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002642 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002644 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002646 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2647 frame_->PrepareForReturn();
2648 }
2649 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 }
2651 }
2652
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653 exit.Bind();
2654 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655}
2656
2657
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002658void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659#ifdef DEBUG
2660 int original_height = frame_->height();
2661#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002662 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002663 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665
2666 // State: Used to keep track of reason for entering the finally
2667 // block. Should probably be extended to hold information for
2668 // break/continue from within the try block.
2669 enum { FALLING, THROWING, JUMPING };
2670
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002671 JumpTarget try_block;
2672 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002674 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002676 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 // In case of thrown exceptions, this is where we continue.
2678 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002679 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680
2681 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2685 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002687 // Shadow the labels for all escapes from the try block, including
2688 // returns. Shadowing hides the original label as the LabelShadow and
2689 // operations on the original actually affect the shadowing label.
2690 //
2691 // We should probably try to unify the escaping labels and the return
2692 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002693 int nof_escapes = node->escaping_targets()->length();
2694 List<ShadowTarget*> shadows(1 + nof_escapes);
2695
2696 // Add the shadow target for the function return.
2697 static const int kReturnShadowIndex = 0;
2698 shadows.Add(new ShadowTarget(&function_return_));
2699 bool function_return_was_shadowed = function_return_is_shadowed_;
2700 function_return_is_shadowed_ = true;
2701 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2702
2703 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002705 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 }
2707
2708 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002709 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002711 // Stop the introduced shadowing and count the number of required unlinks.
2712 // After shadowing stops, the original labels are unshadowed and the
2713 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002715 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 shadows[i]->StopShadowing();
2717 if (shadows[i]->is_linked()) nof_unlinks++;
2718 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002719 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002721 // Get an external reference to the handler address.
2722 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 // If we can fall off the end of the try block, unlink from the try
2725 // chain and set the state on the frame to FALLING.
2726 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002727 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002728 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002729 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730 __ mov(r3, Operand(handler_address));
2731 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002732 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002733
2734 // Fake a top of stack value (unneeded when FALLING) and set the
2735 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002736 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002737 frame_->EmitPush(r0);
2738 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2739 if (nof_unlinks > 0) {
2740 finally_block.Jump();
2741 }
2742 }
2743
2744 // Generate code to unlink and set the state for the (formerly)
2745 // shadowing targets that have been jumped to.
2746 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002748 // If we have come from the shadowed return, the return value is
2749 // in (a non-refcounted reference to) r0. We must preserve it
2750 // until it is pushed.
2751 //
2752 // Because we can be jumping here (to spilled code) from
2753 // unspilled code, we need to reestablish a spilled frame at
2754 // this block.
2755 shadows[i]->Bind();
2756 frame_->SpillAll();
2757
2758 // Reload sp from the top handler, because some statements that
2759 // we break from (eg, for...in) may have left stuff on the
2760 // stack.
2761 __ mov(r3, Operand(handler_address));
2762 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002763 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764
2765 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002766 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002767 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002768 frame_->EmitPop(r1);
2769 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002770 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002771
2772 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002773 // If this label shadowed the function return, materialize the
2774 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002775 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002776 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002778 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002779 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 }
2781 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002782 if (--nof_unlinks > 0) {
2783 // If this is not the last unlink block, jump around the next.
2784 finally_block.Jump();
2785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 }
2787 }
2788
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002790 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002792 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002793 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002794
2795 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002796 // and the state - while evaluating the finally block.
2797 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 // Generate code for the statements in the finally block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002799 VisitStatements(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002801 if (has_valid_frame()) {
2802 // Restore state and return value or faked TOS.
2803 frame_->EmitPop(r2);
2804 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 }
2806
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002807 // Generate code to jump to the right destination for all used
2808 // formerly shadowing targets. Deallocate each shadow target.
2809 for (int i = 0; i < shadows.length(); i++) {
2810 if (has_valid_frame() && shadows[i]->is_bound()) {
2811 JumpTarget* original = shadows[i]->other_target();
2812 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2813 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002814 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002815 skip.Branch(ne);
2816 frame_->PrepareForReturn();
2817 original->Jump();
2818 skip.Bind();
2819 } else {
2820 original->Branch(eq);
2821 }
2822 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002823 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002825 if (has_valid_frame()) {
2826 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002827 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002828 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2829 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002831 // Rethrow exception.
2832 frame_->EmitPush(r0);
2833 frame_->CallRuntime(Runtime::kReThrow, 1);
2834
2835 // Done.
2836 exit.Bind();
2837 }
2838 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839}
2840
2841
ager@chromium.org7c537e22008-10-16 08:43:32 +00002842void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002843#ifdef DEBUG
2844 int original_height = frame_->height();
2845#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002846 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002848#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002849 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002850#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002851 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002852 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853}
2854
2855
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002856void CodeGenerator::InstantiateFunction(
2857 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002858 // Use the fast case closure allocation code that allocates in new
2859 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002860 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002861 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002862 frame_->EmitPush(Operand(function_info));
2863 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002864 frame_->CallStub(&stub, 1);
2865 frame_->EmitPush(r0);
2866 } else {
2867 // Create a new closure.
2868 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002869 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002870 frame_->CallRuntime(Runtime::kNewClosure, 2);
2871 frame_->EmitPush(r0);
2872 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873}
2874
2875
ager@chromium.org7c537e22008-10-16 08:43:32 +00002876void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002877#ifdef DEBUG
2878 int original_height = frame_->height();
2879#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 Comment cmnt(masm_, "[ FunctionLiteral");
2881
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002882 // Build the function info and instantiate it.
2883 Handle<SharedFunctionInfo> function_info =
2884 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002885 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002886 if (HasStackOverflow()) {
2887 ASSERT(frame_->height() == original_height);
2888 return;
2889 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002890 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002891 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892}
2893
2894
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002895void CodeGenerator::VisitSharedFunctionInfoLiteral(
2896 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002897#ifdef DEBUG
2898 int original_height = frame_->height();
2899#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002900 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2901 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002902 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903}
2904
2905
ager@chromium.org7c537e22008-10-16 08:43:32 +00002906void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907#ifdef DEBUG
2908 int original_height = frame_->height();
2909#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002911 JumpTarget then;
2912 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002913 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002914 if (has_valid_frame()) {
2915 Branch(false, &else_);
2916 }
2917 if (has_valid_frame() || then.is_linked()) {
2918 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002919 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002920 }
2921 if (else_.is_linked()) {
2922 JumpTarget exit;
2923 if (has_valid_frame()) exit.Jump();
2924 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002925 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002926 if (exit.is_linked()) exit.Bind();
2927 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002928 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929}
2930
2931
ager@chromium.org7c537e22008-10-16 08:43:32 +00002932void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2933 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002934 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002936 // JumpTargets do not yet support merging frames so the frame must be
2937 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002938 JumpTarget slow;
2939 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002940
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002941 // Generate fast case for loading from slots that correspond to
2942 // local/global variables or arguments unless they are shadowed by
2943 // eval-introduced bindings.
2944 EmitDynamicLoadFromSlotFastCase(slot,
2945 typeof_state,
2946 &slow,
2947 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002948
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002949 slow.Bind();
2950 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002951 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952
ager@chromium.org7c537e22008-10-16 08:43:32 +00002953 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002955 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002956 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002958
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 done.Bind();
2960 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961
2962 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002963 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002964 TypeInfo info = type_info(slot);
2965 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002966
ager@chromium.org7c537e22008-10-16 08:43:32 +00002967 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002968 // Const slots may contain 'the hole' value (the constant hasn't been
2969 // initialized yet) which needs to be converted into the 'undefined'
2970 // value.
2971 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002972 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002973 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002974 __ cmp(tos, ip);
2975 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
2976 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 }
2978 }
2979}
2980
2981
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002982void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
2983 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002984 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002985 LoadFromSlot(slot, state);
2986
2987 // Bail out quickly if we're not using lazy arguments allocation.
2988 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
2989
2990 // ... or if the slot isn't a non-parameter arguments slot.
2991 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
2992
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002993 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002994 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002995 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002996
2997 // If the loaded value is the sentinel that indicates that we
2998 // haven't loaded the arguments object yet, we need to do it now.
2999 JumpTarget exit;
3000 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003001 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003002 exit.Branch(ne);
3003 frame_->Drop();
3004 StoreArgumentsObject(false);
3005 exit.Bind();
3006}
3007
3008
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003009void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3010 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003011 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003012 if (slot->type() == Slot::LOOKUP) {
3013 ASSERT(slot->var()->is_dynamic());
3014
3015 // For now, just do a runtime call.
3016 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003017 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003018
3019 if (init_state == CONST_INIT) {
3020 // Same as the case for a normal store, but ignores attribute
3021 // (e.g. READ_ONLY) of context slot so that we can initialize
3022 // const properties (introduced via eval("const foo = (some
3023 // expr);")). Also, uses the current function context instead of
3024 // the top context.
3025 //
3026 // Note that we must declare the foo upon entry of eval(), via a
3027 // context slot declaration, but we cannot initialize it at the
3028 // same time, because the const declaration may be at the end of
3029 // the eval code (sigh...) and the const variable may have been
3030 // used before (where its value is 'undefined'). Thus, we can only
3031 // do the initialization when we actually encounter the expression
3032 // and when the expression operands are defined and valid, and
3033 // thus we need the split into 2 operations: declaration of the
3034 // context slot followed by initialization.
3035 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3036 } else {
3037 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3038 }
3039 // Storing a variable must keep the (new) value on the expression
3040 // stack. This is necessary for compiling assignment expressions.
3041 frame_->EmitPush(r0);
3042
3043 } else {
3044 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003045 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003046 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003047
ager@chromium.org357bf652010-04-12 11:30:10 +00003048 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003049 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003050
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003051 if (init_state == CONST_INIT) {
3052 ASSERT(slot->var()->mode() == Variable::CONST);
3053 // Only the first const initialization must be executed (the slot
3054 // still contains 'the hole' value). When the assignment is
3055 // executed, the code is identical to a normal store (see below).
3056 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003057 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003058 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003059 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003060 exit.Branch(ne);
3061 }
3062
3063 // We must execute the store. Storing a variable must keep the
3064 // (new) value on the stack. This is necessary for compiling
3065 // assignment expressions.
3066 //
3067 // Note: We will reach here even with slot->var()->mode() ==
3068 // Variable::CONST because of const declarations which will
3069 // initialize consts to 'the hole' value and by doing so, end up
3070 // calling this code. r2 may be loaded with context; used below in
3071 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003072 Register tos = frame_->Peek();
3073 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003074 if (slot->type() == Slot::CONTEXT) {
3075 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003076 __ tst(tos, Operand(kSmiTagMask));
3077 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003078 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003079 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003080 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003081 // We need an extra register. Until we have a way to do that in the
3082 // virtual frame we will cheat and ask for a free TOS register.
3083 Register scratch3 = frame_->GetTOSRegister();
3084 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003085 }
3086 // If we definitely did not jump over the assignment, we do not need
3087 // to bind the exit label. Doing so can defeat peephole
3088 // optimization.
3089 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3090 exit.Bind();
3091 }
3092 }
3093}
3094
3095
ager@chromium.org381abbb2009-02-25 13:23:22 +00003096void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3097 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003098 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003099 // Check that no extension objects have been created by calls to
3100 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003101 Register tmp = frame_->scratch0();
3102 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003103 Register context = cp;
3104 Scope* s = scope();
3105 while (s != NULL) {
3106 if (s->num_heap_slots() > 0) {
3107 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003108 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003109 // Check that extension is NULL.
3110 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3111 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003112 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003113 }
3114 // Load next context in chain.
3115 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3116 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3117 context = tmp;
3118 }
3119 // If no outer scope calls eval, we do not need to check more
3120 // context extensions.
3121 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3122 s = s->outer_scope();
3123 }
3124
3125 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003126 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003127 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003128 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003129 __ bind(&next);
3130 // Terminate at global context.
3131 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003132 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3133 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003134 __ b(eq, &fast);
3135 // Check that extension is NULL.
3136 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3137 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003138 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003139 // Load next context in chain.
3140 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3141 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3142 __ b(&next);
3143 __ bind(&fast);
3144 }
3145
ager@chromium.org381abbb2009-02-25 13:23:22 +00003146 // Load the global object.
3147 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003148 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003149 frame_->CallLoadIC(slot->var()->name(),
3150 typeof_state == INSIDE_TYPEOF
3151 ? RelocInfo::CODE_TARGET
3152 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003153}
3154
3155
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003156void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3157 TypeofState typeof_state,
3158 JumpTarget* slow,
3159 JumpTarget* done) {
3160 // Generate fast-case code for variables that might be shadowed by
3161 // eval-introduced variables. Eval is used a lot without
3162 // introducing variables. In those cases, we do not want to
3163 // perform a runtime call for all variables in the scope
3164 // containing the eval.
3165 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3166 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3167 frame_->SpillAll();
3168 done->Jump();
3169
3170 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3171 frame_->SpillAll();
3172 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3173 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3174 if (potential_slot != NULL) {
3175 // Generate fast case for locals that rewrite to slots.
3176 __ ldr(r0,
3177 ContextSlotOperandCheckExtensions(potential_slot,
3178 r1,
3179 r2,
3180 slow));
3181 if (potential_slot->var()->mode() == Variable::CONST) {
3182 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3183 __ cmp(r0, ip);
3184 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3185 }
3186 done->Jump();
3187 } else if (rewrite != NULL) {
3188 // Generate fast case for argument loads.
3189 Property* property = rewrite->AsProperty();
3190 if (property != NULL) {
3191 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3192 Literal* key_literal = property->key()->AsLiteral();
3193 if (obj_proxy != NULL &&
3194 key_literal != NULL &&
3195 obj_proxy->IsArguments() &&
3196 key_literal->handle()->IsSmi()) {
3197 // Load arguments object if there are no eval-introduced
3198 // variables. Then load the argument from the arguments
3199 // object using keyed load.
3200 __ ldr(r0,
3201 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3202 r1,
3203 r2,
3204 slow));
3205 frame_->EmitPush(r0);
3206 __ mov(r1, Operand(key_literal->handle()));
3207 frame_->EmitPush(r1);
3208 EmitKeyedLoad();
3209 done->Jump();
3210 }
3211 }
3212 }
3213 }
3214}
3215
3216
ager@chromium.org7c537e22008-10-16 08:43:32 +00003217void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003218#ifdef DEBUG
3219 int original_height = frame_->height();
3220#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003221 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003222 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003223 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003224}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003225
ager@chromium.org7c537e22008-10-16 08:43:32 +00003226
3227void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003228#ifdef DEBUG
3229 int original_height = frame_->height();
3230#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003231 Comment cmnt(masm_, "[ VariableProxy");
3232
3233 Variable* var = node->var();
3234 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003235 if (expr != NULL) {
3236 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003238 ASSERT(var->is_global());
3239 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003240 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003242 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243}
3244
3245
ager@chromium.org7c537e22008-10-16 08:43:32 +00003246void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003247#ifdef DEBUG
3248 int original_height = frame_->height();
3249#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003251 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003252 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003253 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003254 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003255 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256}
3257
3258
ager@chromium.org7c537e22008-10-16 08:43:32 +00003259void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003260#ifdef DEBUG
3261 int original_height = frame_->height();
3262#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263 Comment cmnt(masm_, "[ RexExp Literal");
3264
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003265 Register tmp = VirtualFrame::scratch0();
3266 // Free up a TOS register that can be used to push the literal.
3267 Register literal = frame_->GetTOSRegister();
3268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003269 // Retrieve the literal array and check the allocated entry.
3270
3271 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003272 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273
3274 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003275 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003276
3277 // Load the literal at the ast saved index.
3278 int literal_offset =
3279 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003280 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003282 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003283 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003284 __ cmp(literal, ip);
3285 // This branch locks the virtual frame at the done label to match the
3286 // one we have here, where the literal register is not on the stack and
3287 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003288 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003290 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003292 // literal array (0)
3293 frame_->EmitPush(tmp);
3294 // literal index (1)
3295 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3296 // RegExp pattern (2)
3297 frame_->EmitPush(Operand(node->pattern()));
3298 // RegExp flags (3)
3299 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003300 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003301 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003303 materialized.Bind();
3304
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003305 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003306 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3307 frame_->EmitPush(Operand(Smi::FromInt(size)));
3308 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3309 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3310 // r0 is newly allocated space.
3311
3312 // Reuse literal variable with (possibly) a new register, still holding
3313 // the materialized boilerplate.
3314 literal = frame_->PopToRegister(r0);
3315
3316 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3317
3318 // Push the clone.
3319 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003320 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321}
3322
3323
ager@chromium.org7c537e22008-10-16 08:43:32 +00003324void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003325#ifdef DEBUG
3326 int original_height = frame_->height();
3327#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328 Comment cmnt(masm_, "[ ObjectLiteral");
3329
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003330 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003331 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003332 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003333 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003334 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3335 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003336 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003337 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003338 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003339 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003340 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003341 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003342 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003343 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003344 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003345 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003346 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003348 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003349 // At the start of each iteration, the top of stack contains
3350 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003351 ObjectLiteral::Property* property = node->properties()->at(i);
3352 Literal* key = property->key();
3353 Expression* value = property->value();
3354 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003355 case ObjectLiteral::Property::CONSTANT:
3356 break;
3357 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3358 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3359 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003360 case ObjectLiteral::Property::COMPUTED:
3361 if (key->handle()->IsSymbol()) {
3362 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003363 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003364 frame_->PopToR0();
3365 // Fetch the object literal.
3366 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003367 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003368 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3369 break;
3370 }
3371 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003373 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003374 Load(key);
3375 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003376 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377 break;
3378 }
3379 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003380 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003381 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003382 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003383 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003384 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003385 break;
3386 }
3387 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003388 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003389 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003390 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003391 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003392 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003393 break;
3394 }
3395 }
3396 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003397 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003398}
3399
3400
ager@chromium.org7c537e22008-10-16 08:43:32 +00003401void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003402#ifdef DEBUG
3403 int original_height = frame_->height();
3404#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003406
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003407 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003408 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003409 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003410 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003411 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3412 frame_->EmitPush(tos);
3413 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3414 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003415 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003416 if (node->depth() > 1) {
3417 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003418 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003419 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003420 } else {
3421 FastCloneShallowArrayStub stub(length);
3422 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003423 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003424 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003425 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003426
3427 // Generate code to set the elements in the array that are not
3428 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003429 for (int i = 0; i < node->values()->length(); i++) {
3430 Expression* value = node->values()->at(i);
3431
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003432 // If value is a literal the property value is already set in the
3433 // boilerplate object.
3434 if (value->AsLiteral() != NULL) continue;
3435 // If value is a materialized literal the property value is already set
3436 // in the boilerplate object if it is simple.
3437 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003438
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003439 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003440 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003441 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003442 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003443 frame_->SpillAllButCopyTOSToR1();
3444
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003445 // Get the elements array.
3446 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003448 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003449 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003450 __ str(r0, FieldMemOperand(r1, offset));
3451
3452 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003453 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003454 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003455 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003456}
3457
3458
ager@chromium.org32912102009-01-16 10:38:43 +00003459void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003460#ifdef DEBUG
3461 int original_height = frame_->height();
3462#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003463 // Call runtime routine to allocate the catch extension object and
3464 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003465 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003466 Load(node->key());
3467 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003468 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3469 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003470 ASSERT_EQ(original_height + 1, frame_->height());
3471}
3472
3473
3474void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3475#ifdef DEBUG
3476 int original_height = frame_->height();
3477#endif
3478 Comment cmnt(masm(), "[ Variable Assignment");
3479 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3480 ASSERT(var != NULL);
3481 Slot* slot = var->slot();
3482 ASSERT(slot != NULL);
3483
3484 // Evaluate the right-hand side.
3485 if (node->is_compound()) {
3486 // For a compound assignment the right-hand side is a binary operation
3487 // between the current property value and the actual right-hand side.
3488 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3489
3490 // Perform the binary operation.
3491 Literal* literal = node->value()->AsLiteral();
3492 bool overwrite_value =
3493 (node->value()->AsBinaryOperation() != NULL &&
3494 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3495 if (literal != NULL && literal->handle()->IsSmi()) {
3496 SmiOperation(node->binary_op(),
3497 literal->handle(),
3498 false,
3499 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3500 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003501 GenerateInlineSmi inline_smi =
3502 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3503 if (literal != NULL) {
3504 ASSERT(!literal->handle()->IsSmi());
3505 inline_smi = DONT_GENERATE_INLINE_SMI;
3506 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003507 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003508 GenericBinaryOperation(node->binary_op(),
3509 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3510 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003511 }
3512 } else {
3513 Load(node->value());
3514 }
3515
3516 // Perform the assignment.
3517 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3518 CodeForSourcePosition(node->position());
3519 StoreToSlot(slot,
3520 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3521 }
3522 ASSERT_EQ(original_height + 1, frame_->height());
3523}
3524
3525
3526void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3527#ifdef DEBUG
3528 int original_height = frame_->height();
3529#endif
3530 Comment cmnt(masm(), "[ Named Property Assignment");
3531 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3532 Property* prop = node->target()->AsProperty();
3533 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3534
3535 // Initialize name and evaluate the receiver sub-expression if necessary. If
3536 // the receiver is trivial it is not placed on the stack at this point, but
3537 // loaded whenever actually needed.
3538 Handle<String> name;
3539 bool is_trivial_receiver = false;
3540 if (var != NULL) {
3541 name = var->name();
3542 } else {
3543 Literal* lit = prop->key()->AsLiteral();
3544 ASSERT_NOT_NULL(lit);
3545 name = Handle<String>::cast(lit->handle());
3546 // Do not materialize the receiver on the frame if it is trivial.
3547 is_trivial_receiver = prop->obj()->IsTrivial();
3548 if (!is_trivial_receiver) Load(prop->obj());
3549 }
3550
3551 // Change to slow case in the beginning of an initialization block to
3552 // avoid the quadratic behavior of repeatedly adding fast properties.
3553 if (node->starts_initialization_block()) {
3554 // Initialization block consists of assignments of the form expr.x = ..., so
3555 // this will never be an assignment to a variable, so there must be a
3556 // receiver object.
3557 ASSERT_EQ(NULL, var);
3558 if (is_trivial_receiver) {
3559 Load(prop->obj());
3560 } else {
3561 frame_->Dup();
3562 }
3563 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3564 }
3565
3566 // Change to fast case at the end of an initialization block. To prepare for
3567 // that add an extra copy of the receiver to the frame, so that it can be
3568 // converted back to fast case after the assignment.
3569 if (node->ends_initialization_block() && !is_trivial_receiver) {
3570 frame_->Dup();
3571 }
3572
3573 // Stack layout:
3574 // [tos] : receiver (only materialized if non-trivial)
3575 // [tos+1] : receiver if at the end of an initialization block
3576
3577 // Evaluate the right-hand side.
3578 if (node->is_compound()) {
3579 // For a compound assignment the right-hand side is a binary operation
3580 // between the current property value and the actual right-hand side.
3581 if (is_trivial_receiver) {
3582 Load(prop->obj());
3583 } else if (var != NULL) {
3584 LoadGlobal();
3585 } else {
3586 frame_->Dup();
3587 }
3588 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003589
3590 // Perform the binary operation.
3591 Literal* literal = node->value()->AsLiteral();
3592 bool overwrite_value =
3593 (node->value()->AsBinaryOperation() != NULL &&
3594 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3595 if (literal != NULL && literal->handle()->IsSmi()) {
3596 SmiOperation(node->binary_op(),
3597 literal->handle(),
3598 false,
3599 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3600 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003601 GenerateInlineSmi inline_smi =
3602 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3603 if (literal != NULL) {
3604 ASSERT(!literal->handle()->IsSmi());
3605 inline_smi = DONT_GENERATE_INLINE_SMI;
3606 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003607 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003608 GenericBinaryOperation(node->binary_op(),
3609 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3610 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003611 }
3612 } else {
3613 // For non-compound assignment just load the right-hand side.
3614 Load(node->value());
3615 }
3616
3617 // Stack layout:
3618 // [tos] : value
3619 // [tos+1] : receiver (only materialized if non-trivial)
3620 // [tos+2] : receiver if at the end of an initialization block
3621
3622 // Perform the assignment. It is safe to ignore constants here.
3623 ASSERT(var == NULL || var->mode() != Variable::CONST);
3624 ASSERT_NE(Token::INIT_CONST, node->op());
3625 if (is_trivial_receiver) {
3626 // Load the receiver and swap with the value.
3627 Load(prop->obj());
3628 Register t0 = frame_->PopToRegister();
3629 Register t1 = frame_->PopToRegister(t0);
3630 frame_->EmitPush(t0);
3631 frame_->EmitPush(t1);
3632 }
3633 CodeForSourcePosition(node->position());
3634 bool is_contextual = (var != NULL);
3635 EmitNamedStore(name, is_contextual);
3636 frame_->EmitPush(r0);
3637
3638 // Change to fast case at the end of an initialization block.
3639 if (node->ends_initialization_block()) {
3640 ASSERT_EQ(NULL, var);
3641 // The argument to the runtime call is the receiver.
3642 if (is_trivial_receiver) {
3643 Load(prop->obj());
3644 } else {
3645 // A copy of the receiver is below the value of the assignment. Swap
3646 // the receiver and the value of the assignment expression.
3647 Register t0 = frame_->PopToRegister();
3648 Register t1 = frame_->PopToRegister(t0);
3649 frame_->EmitPush(t0);
3650 frame_->EmitPush(t1);
3651 }
3652 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3653 }
3654
3655 // Stack layout:
3656 // [tos] : result
3657
3658 ASSERT_EQ(original_height + 1, frame_->height());
3659}
3660
3661
3662void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3663#ifdef DEBUG
3664 int original_height = frame_->height();
3665#endif
3666 Comment cmnt(masm_, "[ Keyed Property Assignment");
3667 Property* prop = node->target()->AsProperty();
3668 ASSERT_NOT_NULL(prop);
3669
3670 // Evaluate the receiver subexpression.
3671 Load(prop->obj());
3672
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003673 WriteBarrierCharacter wb_info;
3674
ager@chromium.orgac091b72010-05-05 07:34:42 +00003675 // Change to slow case in the beginning of an initialization block to
3676 // avoid the quadratic behavior of repeatedly adding fast properties.
3677 if (node->starts_initialization_block()) {
3678 frame_->Dup();
3679 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3680 }
3681
3682 // Change to fast case at the end of an initialization block. To prepare for
3683 // that add an extra copy of the receiver to the frame, so that it can be
3684 // converted back to fast case after the assignment.
3685 if (node->ends_initialization_block()) {
3686 frame_->Dup();
3687 }
3688
3689 // Evaluate the key subexpression.
3690 Load(prop->key());
3691
3692 // Stack layout:
3693 // [tos] : key
3694 // [tos+1] : receiver
3695 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003696 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003697 // Evaluate the right-hand side.
3698 if (node->is_compound()) {
3699 // For a compound assignment the right-hand side is a binary operation
3700 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003701 // Duplicate receiver and key for loading the current property value.
3702 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003703 EmitKeyedLoad();
3704 frame_->EmitPush(r0);
3705
3706 // Perform the binary operation.
3707 Literal* literal = node->value()->AsLiteral();
3708 bool overwrite_value =
3709 (node->value()->AsBinaryOperation() != NULL &&
3710 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3711 if (literal != NULL && literal->handle()->IsSmi()) {
3712 SmiOperation(node->binary_op(),
3713 literal->handle(),
3714 false,
3715 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3716 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003717 GenerateInlineSmi inline_smi =
3718 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3719 if (literal != NULL) {
3720 ASSERT(!literal->handle()->IsSmi());
3721 inline_smi = DONT_GENERATE_INLINE_SMI;
3722 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003723 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003724 GenericBinaryOperation(node->binary_op(),
3725 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3726 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003727 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003728 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003729 } else {
3730 // For non-compound assignment just load the right-hand side.
3731 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003732 wb_info = node->value()->AsLiteral() != NULL ?
3733 NEVER_NEWSPACE :
3734 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003735 }
3736
3737 // Stack layout:
3738 // [tos] : value
3739 // [tos+1] : key
3740 // [tos+2] : receiver
3741 // [tos+3] : receiver if at the end of an initialization block
3742
3743 // Perform the assignment. It is safe to ignore constants here.
3744 ASSERT(node->op() != Token::INIT_CONST);
3745 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003746 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003747 frame_->EmitPush(r0);
3748
3749 // Stack layout:
3750 // [tos] : result
3751 // [tos+1] : receiver if at the end of an initialization block
3752
3753 // Change to fast case at the end of an initialization block.
3754 if (node->ends_initialization_block()) {
3755 // The argument to the runtime call is the extra copy of the receiver,
3756 // which is below the value of the assignment. Swap the receiver and
3757 // the value of the assignment expression.
3758 Register t0 = frame_->PopToRegister();
3759 Register t1 = frame_->PopToRegister(t0);
3760 frame_->EmitPush(t1);
3761 frame_->EmitPush(t0);
3762 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3763 }
3764
3765 // Stack layout:
3766 // [tos] : result
3767
3768 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003769}
3770
3771
ager@chromium.org7c537e22008-10-16 08:43:32 +00003772void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003773 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003774#ifdef DEBUG
3775 int original_height = frame_->height();
3776#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003778
ager@chromium.orgac091b72010-05-05 07:34:42 +00003779 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3780 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781
ager@chromium.orgac091b72010-05-05 07:34:42 +00003782 if (var != NULL && !var->is_global()) {
3783 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003784
ager@chromium.orgac091b72010-05-05 07:34:42 +00003785 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3786 (var != NULL && var->is_global())) {
3787 // Properties whose keys are property names and global variables are
3788 // treated as named property references. We do not need to consider
3789 // global 'this' because it is not a valid left-hand side.
3790 EmitNamedPropertyAssignment(node);
3791
3792 } else if (prop != NULL) {
3793 // Other properties (including rewritten parameters for a function that
3794 // uses arguments) are keyed property assignments.
3795 EmitKeyedPropertyAssignment(node);
3796
3797 } else {
3798 // Invalid left-hand side.
3799 Load(node->target());
3800 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3801 // The runtime call doesn't actually return but the code generator will
3802 // still generate code and expects a certain frame height.
3803 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003805 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003806}
3807
3808
ager@chromium.org7c537e22008-10-16 08:43:32 +00003809void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810#ifdef DEBUG
3811 int original_height = frame_->height();
3812#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813 Comment cmnt(masm_, "[ Throw");
3814
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003815 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003816 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003817 frame_->CallRuntime(Runtime::kThrow, 1);
3818 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003819 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820}
3821
3822
ager@chromium.org7c537e22008-10-16 08:43:32 +00003823void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003824#ifdef DEBUG
3825 int original_height = frame_->height();
3826#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003828
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003829 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003830 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003832 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833}
3834
3835
ager@chromium.org7c537e22008-10-16 08:43:32 +00003836void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003837#ifdef DEBUG
3838 int original_height = frame_->height();
3839#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 Comment cmnt(masm_, "[ Call");
3841
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003842 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003843 ZoneList<Expression*>* args = node->arguments();
3844
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003845 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847 Variable* var = function->AsVariableProxy()->AsVariable();
3848 Property* property = function->AsProperty();
3849
3850 // ------------------------------------------------------------------------
3851 // Fast-case: Use inline caching.
3852 // ---
3853 // According to ECMA-262, section 11.2.3, page 44, the function to call
3854 // must be resolved after the arguments have been evaluated. The IC code
3855 // automatically handles this by loading the arguments before the function
3856 // is resolved in cache misses (this also holds for megamorphic calls).
3857 // ------------------------------------------------------------------------
3858
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003859 if (var != NULL && var->is_possibly_eval()) {
3860 // ----------------------------------
3861 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3862 // ----------------------------------
3863
3864 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3865 // resolve the function we need to call and the receiver of the
3866 // call. Then we call the resolved function using the given
3867 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003868
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003869 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003870 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003871
3872 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003873 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003874
3875 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003876 int arg_count = args->length();
3877 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003878 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003879 }
3880
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003881 VirtualFrame::SpilledScope spilled_scope(frame_);
3882
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003883 // If we know that eval can only be shadowed by eval-introduced
3884 // variables we attempt to load the global eval function directly
3885 // in generated code. If we succeed, there is no need to perform a
3886 // context lookup in the runtime system.
3887 JumpTarget done;
3888 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3889 ASSERT(var->slot()->type() == Slot::LOOKUP);
3890 JumpTarget slow;
3891 // Prepare the stack for the call to
3892 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3893 // function, the first argument to the eval call and the
3894 // receiver.
3895 LoadFromGlobalSlotCheckExtensions(var->slot(),
3896 NOT_INSIDE_TYPEOF,
3897 &slow);
3898 frame_->EmitPush(r0);
3899 if (arg_count > 0) {
3900 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3901 frame_->EmitPush(r1);
3902 } else {
3903 frame_->EmitPush(r2);
3904 }
3905 __ ldr(r1, frame_->Receiver());
3906 frame_->EmitPush(r1);
3907
3908 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3909
3910 done.Jump();
3911 slow.Bind();
3912 }
3913
3914 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3915 // pushing the loaded function, the first argument to the eval
3916 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003917 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3918 frame_->EmitPush(r1);
3919 if (arg_count > 0) {
3920 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3921 frame_->EmitPush(r1);
3922 } else {
3923 frame_->EmitPush(r2);
3924 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003925 __ ldr(r1, frame_->Receiver());
3926 frame_->EmitPush(r1);
3927
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003928 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003929 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003930
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003931 // If we generated fast-case code bind the jump-target where fast
3932 // and slow case merge.
3933 if (done.is_linked()) done.Bind();
3934
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003935 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003936 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003937 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3938
3939 // Call the function.
3940 CodeForSourcePosition(node->position());
3941
3942 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003943 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003944 frame_->CallStub(&call_function, arg_count + 1);
3945
3946 __ ldr(cp, frame_->Context());
3947 // Remove the function from the stack.
3948 frame_->Drop();
3949 frame_->EmitPush(r0);
3950
3951 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003952 // ----------------------------------
3953 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3954 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003955 // Pass the global object as the receiver and let the IC stub
3956 // patch the stack to use the global proxy as 'this' in the
3957 // invoked function.
3958 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959
3960 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003961 int arg_count = args->length();
3962 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003963 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003966 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00003967 // Setup the name register and call the IC initialization code.
3968 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003969 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3970 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003971 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003972 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3973 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003974 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003975 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976
3977 } else if (var != NULL && var->slot() != NULL &&
3978 var->slot()->type() == Slot::LOOKUP) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003979 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003981 // JavaScript examples:
3982 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003983 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003984 //
3985 // function f() {};
3986 // function g() {
3987 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003988 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003989 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990 // ----------------------------------
3991
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003992 // JumpTargets do not yet support merging frames so the frame must be
3993 // spilled when jumping to these targets.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003994 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003995
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003996 // Generate fast case for loading functions from slots that
3997 // correspond to local/global variables or arguments unless they
3998 // are shadowed by eval-introduced bindings.
3999 EmitDynamicLoadFromSlotFastCase(var->slot(),
4000 NOT_INSIDE_TYPEOF,
4001 &slow,
4002 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004003
4004 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004005 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004006 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004007 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004008 frame_->EmitPush(r0);
4009 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004010 // r0: slot value; r1: receiver
4011
4012 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004013 frame_->EmitPush(r0); // function
4014 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004016 // If fast case code has been generated, emit code to push the
4017 // function and receiver and have the slow path jump around this
4018 // code.
4019 if (done.is_linked()) {
4020 JumpTarget call;
4021 call.Jump();
4022 done.Bind();
4023 frame_->EmitPush(r0); // function
4024 LoadGlobalReceiver(r1); // receiver
4025 call.Bind();
4026 }
4027
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004028 // Call the function. At this point, everything is spilled but the
4029 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004030 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
4033 } else if (property != NULL) {
4034 // Check if the key is a literal string.
4035 Literal* literal = property->key()->AsLiteral();
4036
4037 if (literal != NULL && literal->handle()->IsSymbol()) {
4038 // ------------------------------------------------------------------
4039 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4040 // ------------------------------------------------------------------
4041
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004042 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004044 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4045 name->IsEqualTo(CStrVector("apply")) &&
4046 args->length() == 2 &&
4047 args->at(1)->AsVariableProxy() != NULL &&
4048 args->at(1)->AsVariableProxy()->IsArguments()) {
4049 // Use the optimized Function.prototype.apply that avoids
4050 // allocating lazily allocated arguments objects.
4051 CallApplyLazy(property->obj(),
4052 args->at(0),
4053 args->at(1)->AsVariableProxy(),
4054 node->position());
4055
4056 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004057 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004058 // Load the arguments.
4059 int arg_count = args->length();
4060 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004061 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004062 }
4063
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004064 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004065 // Set the name register and call the IC initialization code.
4066 __ mov(r2, Operand(name));
4067 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4068 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4069 CodeForSourcePosition(node->position());
4070 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4071 __ ldr(cp, frame_->Context());
4072 frame_->EmitPush(r0);
4073 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074
4075 } else {
4076 // -------------------------------------------
4077 // JavaScript example: 'array[index](1, 2, 3)'
4078 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004079 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004081 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004083 Load(property->key());
4084 EmitKeyedLoad();
4085 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004086 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004087 frame_->EmitPush(r0); // Function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 LoadGlobalReceiver(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004089 // Call the function.
4090 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4091 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004093 // Load the arguments.
4094 int arg_count = args->length();
4095 for (int i = 0; i < arg_count; i++) {
4096 Load(args->at(i));
4097 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004098
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004099 // Set the name register and call the IC initialization code.
4100 Load(property->key());
4101 frame_->EmitPop(r2); // Function name.
4102
4103 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4104 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4105 CodeForSourcePosition(node->position());
4106 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4107 __ ldr(cp, frame_->Context());
4108 frame_->EmitPush(r0);
4109 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110 }
4111
4112 } else {
4113 // ----------------------------------
4114 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4115 // ----------------------------------
4116
4117 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004118 Load(function);
4119
4120 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004121
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004122 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004123 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004125 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004126 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004127 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004128 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004129 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004130}
4131
4132
ager@chromium.org7c537e22008-10-16 08:43:32 +00004133void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004134#ifdef DEBUG
4135 int original_height = frame_->height();
4136#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 Comment cmnt(masm_, "[ CallNew");
4138
4139 // According to ECMA-262, section 11.2.2, page 44, the function
4140 // expression in new calls must be evaluated before the
4141 // arguments. This is different from ordinary calls, where the
4142 // actual function to call is resolved after the arguments have been
4143 // evaluated.
4144
4145 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004146 // receiver. There is no need to use the global proxy here because
4147 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004148 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004149 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150
4151 // Push the arguments ("left-to-right") on the stack.
4152 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004153 int arg_count = args->length();
4154 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004155 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004156 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004157
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004158 VirtualFrame::SpilledScope spilled_scope(frame_);
4159
mads.s.ager31e71382008-08-13 09:32:07 +00004160 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004161 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004162 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004163 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004165 // Call the construct call builtin that handles allocation and
4166 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004167 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004168 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004169 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004170
4171 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004172 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004173 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004174}
4175
4176
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004177void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004178 JumpTarget leave, null, function, non_function_constructor;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004179 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004180
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004181 // Load the object into register.
4182 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004183 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004184 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004185
4186 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004187 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004188 null.Branch(eq);
4189
4190 // Check that the object is a JS object but take special care of JS
4191 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004192 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004193 null.Branch(lt);
4194
4195 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4196 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4197 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004198 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4199 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004200 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004201 function.Branch(eq);
4202
4203 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004204 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4205 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004206 non_function_constructor.Branch(ne);
4207
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004208 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004209 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004210 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4211 __ ldr(tos,
4212 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4213 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004214 leave.Jump();
4215
4216 // Functions have class 'Function'.
4217 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004218 __ mov(tos, Operand(Factory::function_class_symbol()));
4219 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004220 leave.Jump();
4221
4222 // Objects with a non-function constructor have class 'Object'.
4223 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004224 __ mov(tos, Operand(Factory::Object_symbol()));
4225 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004226 leave.Jump();
4227
4228 // Non-JS objects have class null.
4229 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004230 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4231 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004232
4233 // All done.
4234 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004235}
4236
4237
ager@chromium.org7c537e22008-10-16 08:43:32 +00004238void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004239 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004240 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004241
4242 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004243 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004244 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004245 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004246 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004247 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004248 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004249 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004250 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004252 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004253 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004254 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255}
4256
4257
ager@chromium.org7c537e22008-10-16 08:43:32 +00004258void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004259 Register scratch1 = VirtualFrame::scratch0();
4260 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004261 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004262
4263 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004264 Load(args->at(0)); // Load the object.
4265 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004266 Register value = frame_->PopToRegister();
4267 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004269 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004270 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004271 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004272 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004273 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004274 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004275 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004277 __ RecordWrite(object,
4278 Operand(JSValue::kValueOffset - kHeapObjectTag),
4279 scratch1,
4280 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004282 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004283 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284}
4285
4286
ager@chromium.org7c537e22008-10-16 08:43:32 +00004287void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004288 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004289 Load(args->at(0));
4290 Register reg = frame_->PopToRegister();
4291 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292 cc_reg_ = eq;
4293}
4294
4295
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004296void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4297 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4298 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004299#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004300 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004301 Load(args->at(1));
4302 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004303 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004304 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004305#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004306 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004307}
4308
4309
ager@chromium.org7c537e22008-10-16 08:43:32 +00004310void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004311 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004312 Load(args->at(0));
4313 Register reg = frame_->PopToRegister();
4314 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004315 cc_reg_ = eq;
4316}
4317
4318
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004319// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004320void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4321 ASSERT(args->length() == 2);
4322 Load(args->at(0));
4323 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004324
4325 if (!CpuFeatures::IsSupported(VFP3)) {
4326 frame_->CallRuntime(Runtime::kMath_pow, 2);
4327 frame_->EmitPush(r0);
4328 } else {
4329 CpuFeatures::Scope scope(VFP3);
4330 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004331 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004332
4333 Register scratch1 = VirtualFrame::scratch0();
4334 Register scratch2 = VirtualFrame::scratch1();
4335
4336 // Get base and exponent to registers.
4337 Register exponent = frame_->PopToRegister();
4338 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004339 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004340
4341 // Set the frame for the runtime jump target. The code below jumps to the
4342 // jump target label so the frame needs to be established before that.
4343 ASSERT(runtime.entry_frame() == NULL);
4344 runtime.set_entry_frame(frame_);
4345
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004346 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4347 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004348
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004349 heap_number_map = r6;
4350 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4351
4352 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4353 // d1.
4354 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4355 __ b(&powi);
4356
4357 __ bind(&base_nonsmi);
4358 // Exponent is smi and base is non smi. Get the double value from the base
4359 // into vfp register d1.
4360 __ ObjectToDoubleVFPRegister(base, d1,
4361 scratch1, scratch2, heap_number_map, s0,
4362 runtime.entry_label());
4363
4364 __ bind(&powi);
4365
4366 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004367 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004368
4369 // Get the absolute untagged value of the exponent and use that for the
4370 // calculation.
4371 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4372 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4373 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4374
4375 // Run through all the bits in the exponent. The result is calculated in d0
4376 // and d1 holds base^(bit^2).
4377 Label more_bits;
4378 __ bind(&more_bits);
4379 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4380 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4381 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4382 __ b(ne, &more_bits);
4383
4384 // If exponent is positive we are done.
4385 __ cmp(exponent, Operand(0));
4386 __ b(ge, &allocate_return);
4387
4388 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4389 // case). However if d0 has reached infinity this will not provide the
4390 // correct result, so call runtime if that is the case.
4391 __ mov(scratch2, Operand(0x7FF00000));
4392 __ mov(scratch1, Operand(0));
4393 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4394 __ vcmp(d0, d1);
4395 __ vmrs(pc);
4396 runtime.Branch(eq); // d0 reached infinity.
4397 __ vdiv(d0, d2, d0);
4398 __ b(&allocate_return);
4399
4400 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004401 // Special handling of raising to the power of -0.5 and 0.5. First check
4402 // that the value is a heap number and that the lower bits (which for both
4403 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004404 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004405 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4406 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4407 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4408 __ cmp(scratch1, heap_number_map);
4409 runtime.Branch(ne);
4410 __ tst(scratch2, scratch2);
4411 runtime.Branch(ne);
4412
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004413 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004414 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4415
4416 // Compare exponent with -0.5.
4417 __ cmp(scratch1, Operand(0xbfe00000));
4418 __ b(ne, &not_minus_half);
4419
4420 // Get the double value from the base into vfp register d0.
4421 __ ObjectToDoubleVFPRegister(base, d0,
4422 scratch1, scratch2, heap_number_map, s0,
4423 runtime.entry_label(),
4424 AVOID_NANS_AND_INFINITIES);
4425
4426 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004427 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004428
4429 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4430 __ vdiv(d0, d2, d0);
4431 __ vsqrt(d0, d0);
4432
4433 __ b(&allocate_return);
4434
4435 __ bind(&not_minus_half);
4436 // Compare exponent with 0.5.
4437 __ cmp(scratch1, Operand(0x3fe00000));
4438 runtime.Branch(ne);
4439
4440 // Get the double value from the base into vfp register d0.
4441 __ ObjectToDoubleVFPRegister(base, d0,
4442 scratch1, scratch2, heap_number_map, s0,
4443 runtime.entry_label(),
4444 AVOID_NANS_AND_INFINITIES);
4445 __ vsqrt(d0, d0);
4446
4447 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004448 Register scratch3 = r5;
4449 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4450 heap_number_map, runtime.entry_label());
4451 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004452 done.Jump();
4453
4454 runtime.Bind();
4455
4456 // Push back the arguments again for the runtime call.
4457 frame_->EmitPush(base);
4458 frame_->EmitPush(exponent);
4459 frame_->CallRuntime(Runtime::kMath_pow, 2);
4460 __ Move(base, r0);
4461
4462 done.Bind();
4463 frame_->EmitPush(base);
4464 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004465}
4466
4467
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004468// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004469void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4470 ASSERT(args->length() == 1);
4471 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004472
4473 if (!CpuFeatures::IsSupported(VFP3)) {
4474 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4475 frame_->EmitPush(r0);
4476 } else {
4477 CpuFeatures::Scope scope(VFP3);
4478 JumpTarget runtime, done;
4479
4480 Register scratch1 = VirtualFrame::scratch0();
4481 Register scratch2 = VirtualFrame::scratch1();
4482
4483 // Get the value from the frame.
4484 Register tos = frame_->PopToRegister();
4485
4486 // Set the frame for the runtime jump target. The code below jumps to the
4487 // jump target label so the frame needs to be established before that.
4488 ASSERT(runtime.entry_frame() == NULL);
4489 runtime.set_entry_frame(frame_);
4490
4491 Register heap_number_map = r6;
4492 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4493
4494 // Get the double value from the heap number into vfp register d0.
4495 __ ObjectToDoubleVFPRegister(tos, d0,
4496 scratch1, scratch2, heap_number_map, s0,
4497 runtime.entry_label());
4498
4499 // Calculate the square root of d0 and place result in a heap number object.
4500 __ vsqrt(d0, d0);
4501 __ AllocateHeapNumberWithValue(
4502 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4503 done.Jump();
4504
4505 runtime.Bind();
4506 // Push back the argument again for the runtime call.
4507 frame_->EmitPush(tos);
4508 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4509 __ Move(tos, r0);
4510
4511 done.Bind();
4512 frame_->EmitPush(tos);
4513 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004514}
4515
4516
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004517class DeferredStringCharCodeAt : public DeferredCode {
4518 public:
4519 DeferredStringCharCodeAt(Register object,
4520 Register index,
4521 Register scratch,
4522 Register result)
4523 : result_(result),
4524 char_code_at_generator_(object,
4525 index,
4526 scratch,
4527 result,
4528 &need_conversion_,
4529 &need_conversion_,
4530 &index_out_of_range_,
4531 STRING_INDEX_IS_NUMBER) {}
4532
4533 StringCharCodeAtGenerator* fast_case_generator() {
4534 return &char_code_at_generator_;
4535 }
4536
4537 virtual void Generate() {
4538 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4539 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4540
4541 __ bind(&need_conversion_);
4542 // Move the undefined value into the result register, which will
4543 // trigger conversion.
4544 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4545 __ jmp(exit_label());
4546
4547 __ bind(&index_out_of_range_);
4548 // When the index is out of range, the spec requires us to return
4549 // NaN.
4550 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4551 __ jmp(exit_label());
4552 }
4553
4554 private:
4555 Register result_;
4556
4557 Label need_conversion_;
4558 Label index_out_of_range_;
4559
4560 StringCharCodeAtGenerator char_code_at_generator_;
4561};
4562
4563
4564// This generates code that performs a String.prototype.charCodeAt() call
4565// or returns a smi in order to trigger conversion.
4566void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004567 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004568 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004569
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004570 Load(args->at(0));
4571 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004572
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004573 Register index = frame_->PopToRegister();
4574 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004575
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004576 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004577 Register scratch = VirtualFrame::scratch0();
4578 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004579
4580 DeferredStringCharCodeAt* deferred =
4581 new DeferredStringCharCodeAt(object,
4582 index,
4583 scratch,
4584 result);
4585 deferred->fast_case_generator()->GenerateFast(masm_);
4586 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004587 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004588}
4589
4590
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004591class DeferredStringCharFromCode : public DeferredCode {
4592 public:
4593 DeferredStringCharFromCode(Register code,
4594 Register result)
4595 : char_from_code_generator_(code, result) {}
4596
4597 StringCharFromCodeGenerator* fast_case_generator() {
4598 return &char_from_code_generator_;
4599 }
4600
4601 virtual void Generate() {
4602 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4603 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4604 }
4605
4606 private:
4607 StringCharFromCodeGenerator char_from_code_generator_;
4608};
4609
4610
4611// Generates code for creating a one-char string from a char code.
4612void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004613 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004614 ASSERT(args->length() == 1);
4615
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004616 Load(args->at(0));
4617
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004618 Register result = frame_->GetTOSRegister();
4619 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004620
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004621 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4622 code, result);
4623 deferred->fast_case_generator()->GenerateFast(masm_);
4624 deferred->BindExit();
4625 frame_->EmitPush(result);
4626}
4627
4628
4629class DeferredStringCharAt : public DeferredCode {
4630 public:
4631 DeferredStringCharAt(Register object,
4632 Register index,
4633 Register scratch1,
4634 Register scratch2,
4635 Register result)
4636 : result_(result),
4637 char_at_generator_(object,
4638 index,
4639 scratch1,
4640 scratch2,
4641 result,
4642 &need_conversion_,
4643 &need_conversion_,
4644 &index_out_of_range_,
4645 STRING_INDEX_IS_NUMBER) {}
4646
4647 StringCharAtGenerator* fast_case_generator() {
4648 return &char_at_generator_;
4649 }
4650
4651 virtual void Generate() {
4652 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4653 char_at_generator_.GenerateSlow(masm(), call_helper);
4654
4655 __ bind(&need_conversion_);
4656 // Move smi zero into the result register, which will trigger
4657 // conversion.
4658 __ mov(result_, Operand(Smi::FromInt(0)));
4659 __ jmp(exit_label());
4660
4661 __ bind(&index_out_of_range_);
4662 // When the index is out of range, the spec requires us to return
4663 // the empty string.
4664 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4665 __ jmp(exit_label());
4666 }
4667
4668 private:
4669 Register result_;
4670
4671 Label need_conversion_;
4672 Label index_out_of_range_;
4673
4674 StringCharAtGenerator char_at_generator_;
4675};
4676
4677
4678// This generates code that performs a String.prototype.charAt() call
4679// or returns a smi in order to trigger conversion.
4680void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004681 Comment(masm_, "[ GenerateStringCharAt");
4682 ASSERT(args->length() == 2);
4683
4684 Load(args->at(0));
4685 Load(args->at(1));
4686
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004687 Register index = frame_->PopToRegister();
4688 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004689
4690 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004691 Register scratch1 = VirtualFrame::scratch0();
4692 Register scratch2 = VirtualFrame::scratch1();
4693 // Use r6 without notifying the virtual frame.
4694 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004695
4696 DeferredStringCharAt* deferred =
4697 new DeferredStringCharAt(object,
4698 index,
4699 scratch1,
4700 scratch2,
4701 result);
4702 deferred->fast_case_generator()->GenerateFast(masm_);
4703 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004704 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004705}
4706
4707
ager@chromium.org7c537e22008-10-16 08:43:32 +00004708void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004709 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004710 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004711 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004712 // We need the CC bits to come out as not_equal in the case where the
4713 // object is a smi. This can't be done with the usual test opcode so
4714 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004715 Register possible_array = frame_->PopToRegister();
4716 Register scratch = VirtualFrame::scratch0();
4717 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4718 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004719 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004720 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004721 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004722 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004723 cc_reg_ = eq;
4724}
4725
4726
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004727void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004728 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004729 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004730 JumpTarget answer;
4731 // We need the CC bits to come out as not_equal in the case where the
4732 // object is a smi. This can't be done with the usual test opcode so
4733 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004734 Register possible_regexp = frame_->PopToRegister();
4735 Register scratch = VirtualFrame::scratch0();
4736 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4737 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004738 answer.Branch(ne);
4739 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004740 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004741 answer.Bind();
4742 cc_reg_ = eq;
4743}
4744
4745
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004746void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4747 // This generates a fast version of:
4748 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004749 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004750 Load(args->at(0));
4751 Register possible_object = frame_->PopToRegister();
4752 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004753 false_target()->Branch(eq);
4754
4755 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004756 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004757 true_target()->Branch(eq);
4758
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004759 Register map_reg = VirtualFrame::scratch0();
4760 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004761 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004762 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4763 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004764 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004765
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004766 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4767 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004768 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004769 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004770 cc_reg_ = le;
4771}
4772
4773
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004774void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4775 // This generates a fast version of:
4776 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4777 // typeof(arg) == function).
4778 // It includes undetectable objects (as opposed to IsObject).
4779 ASSERT(args->length() == 1);
4780 Load(args->at(0));
4781 Register value = frame_->PopToRegister();
4782 __ tst(value, Operand(kSmiTagMask));
4783 false_target()->Branch(eq);
4784 // Check that this is an object.
4785 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4786 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4787 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4788 cc_reg_ = ge;
4789}
4790
4791
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004792void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4793 // This generates a fast version of:
4794 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004795 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004796 Load(args->at(0));
4797 Register possible_function = frame_->PopToRegister();
4798 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004799 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004800 Register map_reg = VirtualFrame::scratch0();
4801 Register scratch = VirtualFrame::scratch1();
4802 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004803 cc_reg_ = eq;
4804}
4805
4806
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004807void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004808 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004809 Load(args->at(0));
4810 Register possible_undetectable = frame_->PopToRegister();
4811 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004812 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004813 Register scratch = VirtualFrame::scratch0();
4814 __ ldr(scratch,
4815 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4816 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4817 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004818 cc_reg_ = ne;
4819}
4820
4821
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004822void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004823 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004824
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004825 Register scratch0 = VirtualFrame::scratch0();
4826 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004827 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004828 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004829
4830 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004831 __ ldr(scratch1,
4832 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4833 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4834 __ ldr(scratch0,
4835 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004836
4837 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004838 __ ldr(scratch1,
4839 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
4840 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004841 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004842}
4843
4844
ager@chromium.org7c537e22008-10-16 08:43:32 +00004845void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004846 ASSERT(args->length() == 0);
4847
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004848 Register tos = frame_->GetTOSRegister();
4849 Register scratch0 = VirtualFrame::scratch0();
4850 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004851
lrn@chromium.org25156de2010-04-06 13:10:27 +00004852 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004853 __ ldr(scratch0,
4854 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4855 __ ldr(scratch1,
4856 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4857 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4858
4859 // Get the number of formal parameters.
4860 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004861
4862 // Arguments adaptor case: Read the arguments length from the
4863 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004864 __ ldr(tos,
4865 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
4866 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004867
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004868 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004869}
4870
4871
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004872void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004873 ASSERT(args->length() == 1);
4874
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004875 // Satisfy contract with ArgumentsAccessStub:
4876 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004877 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004878 frame_->PopToR1();
4879 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004880 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004881
4882 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004883 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004884 frame_->CallStub(&stub, 0);
4885 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004886}
4887
4888
ager@chromium.org357bf652010-04-12 11:30:10 +00004889void CodeGenerator::GenerateRandomHeapNumber(
4890 ZoneList<Expression*>* args) {
4891 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004892 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004893
4894 Label slow_allocate_heapnumber;
4895 Label heapnumber_allocated;
4896
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004897 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
4898 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004899 __ jmp(&heapnumber_allocated);
4900
4901 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004902 // Allocate a heap number.
4903 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004904 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004905
4906 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004907
4908 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4909 // by computing:
4910 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4911 if (CpuFeatures::IsSupported(VFP3)) {
4912 __ PrepareCallCFunction(0, r1);
4913 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4914
4915 CpuFeatures::Scope scope(VFP3);
4916 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4917 // Create this constant using mov/orr to avoid PC relative load.
4918 __ mov(r1, Operand(0x41000000));
4919 __ orr(r1, r1, Operand(0x300000));
4920 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4921 __ vmov(d7, r0, r1);
4922 // Move 0x4130000000000000 to VFP.
4923 __ mov(r0, Operand(0));
4924 __ vmov(d8, r0, r1);
4925 // Subtract and store the result in the heap number.
4926 __ vsub(d7, d7, d8);
4927 __ sub(r0, r4, Operand(kHeapObjectTag));
4928 __ vstr(d7, r0, HeapNumber::kValueOffset);
4929 frame_->EmitPush(r4);
4930 } else {
4931 __ mov(r0, Operand(r4));
4932 __ PrepareCallCFunction(1, r1);
4933 __ CallCFunction(
4934 ExternalReference::fill_heap_number_with_random_function(), 1);
4935 frame_->EmitPush(r0);
4936 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004937}
4938
4939
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004940void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4941 ASSERT_EQ(2, args->length());
4942
4943 Load(args->at(0));
4944 Load(args->at(1));
4945
ager@chromium.org5c838252010-02-19 08:53:10 +00004946 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004947 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004948 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004949 frame_->EmitPush(r0);
4950}
4951
4952
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004953void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4954 ASSERT_EQ(3, args->length());
4955
4956 Load(args->at(0));
4957 Load(args->at(1));
4958 Load(args->at(2));
4959
ager@chromium.org5c838252010-02-19 08:53:10 +00004960 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004961 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004962 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004963 frame_->EmitPush(r0);
4964}
4965
4966
4967void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4968 ASSERT_EQ(2, args->length());
4969
4970 Load(args->at(0));
4971 Load(args->at(1));
4972
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004973 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004974 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004975 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004976 frame_->EmitPush(r0);
4977}
4978
4979
4980void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4981 ASSERT_EQ(4, args->length());
4982
4983 Load(args->at(0));
4984 Load(args->at(1));
4985 Load(args->at(2));
4986 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004987 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004988 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004989 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004990 frame_->EmitPush(r0);
4991}
4992
4993
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004994void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4995 // No stub. This code only occurs a few times in regexp.js.
4996 const int kMaxInlineLength = 100;
4997 ASSERT_EQ(3, args->length());
4998 Load(args->at(0)); // Size of array, smi.
4999 Load(args->at(1)); // "index" property value.
5000 Load(args->at(2)); // "input" property value.
5001 {
5002 VirtualFrame::SpilledScope spilled_scope(frame_);
5003 Label slowcase;
5004 Label done;
5005 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5006 STATIC_ASSERT(kSmiTag == 0);
5007 STATIC_ASSERT(kSmiTagSize == 1);
5008 __ tst(r1, Operand(kSmiTagMask));
5009 __ b(ne, &slowcase);
5010 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5011 __ b(hi, &slowcase);
5012 // Smi-tagging is equivalent to multiplying by 2.
5013 // Allocate RegExpResult followed by FixedArray with size in ebx.
5014 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5015 // Elements: [Map][Length][..elements..]
5016 // Size of JSArray with two in-object properties and the header of a
5017 // FixedArray.
5018 int objects_size =
5019 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5020 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5021 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005022 __ AllocateInNewSpace(
5023 r2, // In: Size, in words.
5024 r0, // Out: Start of allocation (tagged).
5025 r3, // Scratch register.
5026 r4, // Scratch register.
5027 &slowcase,
5028 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005029 // r0: Start of allocated area, object-tagged.
5030 // r1: Number of elements in array, as smi.
5031 // r5: Number of elements, untagged.
5032
5033 // Set JSArray map to global.regexp_result_map().
5034 // Set empty properties FixedArray.
5035 // Set elements to point to FixedArray allocated right after the JSArray.
5036 // Interleave operations for better latency.
5037 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5038 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5039 __ mov(r4, Operand(Factory::empty_fixed_array()));
5040 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5041 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5042 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5043 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5044 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5045
5046 // Set input, index and length fields from arguments.
5047 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5048 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5049 __ add(sp, sp, Operand(kPointerSize));
5050 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5051 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5052
5053 // Fill out the elements FixedArray.
5054 // r0: JSArray, tagged.
5055 // r3: FixedArray, tagged.
5056 // r5: Number of elements in array, untagged.
5057
5058 // Set map.
5059 __ mov(r2, Operand(Factory::fixed_array_map()));
5060 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5061 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005062 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5063 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005064 // Fill contents of fixed-array with the-hole.
5065 __ mov(r2, Operand(Factory::the_hole_value()));
5066 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5067 // Fill fixed array elements with hole.
5068 // r0: JSArray, tagged.
5069 // r2: the hole.
5070 // r3: Start of elements in FixedArray.
5071 // r5: Number of elements to fill.
5072 Label loop;
5073 __ tst(r5, Operand(r5));
5074 __ bind(&loop);
5075 __ b(le, &done); // Jump if r1 is negative or zero.
5076 __ sub(r5, r5, Operand(1), SetCC);
5077 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5078 __ jmp(&loop);
5079
5080 __ bind(&slowcase);
5081 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5082
5083 __ bind(&done);
5084 }
5085 frame_->Forget(3);
5086 frame_->EmitPush(r0);
5087}
5088
5089
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005090class DeferredSearchCache: public DeferredCode {
5091 public:
5092 DeferredSearchCache(Register dst, Register cache, Register key)
5093 : dst_(dst), cache_(cache), key_(key) {
5094 set_comment("[ DeferredSearchCache");
5095 }
5096
5097 virtual void Generate();
5098
5099 private:
5100 Register dst_, cache_, key_;
5101};
5102
5103
5104void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005105 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005106 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005107 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005108}
5109
5110
5111void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5112 ASSERT_EQ(2, args->length());
5113
5114 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5115 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5116
5117 Handle<FixedArray> jsfunction_result_caches(
5118 Top::global_context()->jsfunction_result_caches());
5119 if (jsfunction_result_caches->length() <= cache_id) {
5120 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005121 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005122 return;
5123 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005124
5125 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005126
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005127 frame_->PopToR1();
5128 frame_->SpillAll();
5129 Register key = r1; // Just poped to r1
5130 Register result = r0; // Free, as frame has just been spilled.
5131 Register scratch1 = VirtualFrame::scratch0();
5132 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005133
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005134 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5135 __ ldr(scratch1,
5136 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5137 __ ldr(scratch1,
5138 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5139 __ ldr(scratch1,
5140 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005141
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005142 DeferredSearchCache* deferred =
5143 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005144
5145 const int kFingerOffset =
5146 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005147 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005148 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5149 // result now holds finger offset as a smi.
5150 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5151 // scratch2 now points to the start of fixed array elements.
5152 __ ldr(result,
5153 MemOperand(
5154 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5155 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5156 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005157 deferred->Branch(ne);
5158
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005159 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005160
5161 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005162 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005163}
5164
5165
ager@chromium.org5c838252010-02-19 08:53:10 +00005166void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5167 ASSERT_EQ(args->length(), 1);
5168
5169 // Load the argument on the stack and jump to the runtime.
5170 Load(args->at(0));
5171
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005172 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005173 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005174 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005175 frame_->EmitPush(r0);
5176}
5177
5178
ager@chromium.orgac091b72010-05-05 07:34:42 +00005179class DeferredSwapElements: public DeferredCode {
5180 public:
5181 DeferredSwapElements(Register object, Register index1, Register index2)
5182 : object_(object), index1_(index1), index2_(index2) {
5183 set_comment("[ DeferredSwapElements");
5184 }
5185
5186 virtual void Generate();
5187
5188 private:
5189 Register object_, index1_, index2_;
5190};
5191
5192
5193void DeferredSwapElements::Generate() {
5194 __ push(object_);
5195 __ push(index1_);
5196 __ push(index2_);
5197 __ CallRuntime(Runtime::kSwapElements, 3);
5198}
5199
5200
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005201void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5202 Comment cmnt(masm_, "[ GenerateSwapElements");
5203
5204 ASSERT_EQ(3, args->length());
5205
5206 Load(args->at(0));
5207 Load(args->at(1));
5208 Load(args->at(2));
5209
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005210 VirtualFrame::SpilledScope spilled_scope(frame_);
5211
ager@chromium.orgac091b72010-05-05 07:34:42 +00005212 Register index2 = r2;
5213 Register index1 = r1;
5214 Register object = r0;
5215 Register tmp1 = r3;
5216 Register tmp2 = r4;
5217
5218 frame_->EmitPop(index2);
5219 frame_->EmitPop(index1);
5220 frame_->EmitPop(object);
5221
5222 DeferredSwapElements* deferred =
5223 new DeferredSwapElements(object, index1, index2);
5224
5225 // Fetch the map and check if array is in fast case.
5226 // Check that object doesn't require security checks and
5227 // has no indexed interceptor.
5228 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5229 deferred->Branch(lt);
5230 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5231 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5232 deferred->Branch(nz);
5233
5234 // Check the object's elements are in fast case.
5235 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5236 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5237 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5238 __ cmp(tmp2, ip);
5239 deferred->Branch(ne);
5240
5241 // Smi-tagging is equivalent to multiplying by 2.
5242 STATIC_ASSERT(kSmiTag == 0);
5243 STATIC_ASSERT(kSmiTagSize == 1);
5244
5245 // Check that both indices are smis.
5246 __ mov(tmp2, index1);
5247 __ orr(tmp2, tmp2, index2);
5248 __ tst(tmp2, Operand(kSmiTagMask));
5249 deferred->Branch(nz);
5250
5251 // Bring the offsets into the fixed array in tmp1 into index1 and
5252 // index2.
5253 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5254 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5255 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5256
5257 // Swap elements.
5258 Register tmp3 = object;
5259 object = no_reg;
5260 __ ldr(tmp3, MemOperand(tmp1, index1));
5261 __ ldr(tmp2, MemOperand(tmp1, index2));
5262 __ str(tmp3, MemOperand(tmp1, index2));
5263 __ str(tmp2, MemOperand(tmp1, index1));
5264
5265 Label done;
5266 __ InNewSpace(tmp1, tmp2, eq, &done);
5267 // Possible optimization: do a check that both values are Smis
5268 // (or them and test against Smi mask.)
5269
5270 __ mov(tmp2, tmp1);
5271 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5272 __ CallStub(&recordWrite1);
5273
5274 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5275 __ CallStub(&recordWrite2);
5276
5277 __ bind(&done);
5278
5279 deferred->BindExit();
5280 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5281 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005282}
5283
5284
ager@chromium.org357bf652010-04-12 11:30:10 +00005285void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5286 Comment cmnt(masm_, "[ GenerateCallFunction");
5287
5288 ASSERT(args->length() >= 2);
5289
5290 int n_args = args->length() - 2; // for receiver and function.
5291 Load(args->at(0)); // receiver
5292 for (int i = 0; i < n_args; i++) {
5293 Load(args->at(i + 1));
5294 }
5295 Load(args->at(n_args + 1)); // function
5296 frame_->CallJSFunction(n_args);
5297 frame_->EmitPush(r0);
5298}
5299
5300
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005301void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5302 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005303 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005304 if (CpuFeatures::IsSupported(VFP3)) {
5305 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5306 frame_->SpillAllButCopyTOSToR0();
5307 frame_->CallStub(&stub, 1);
5308 } else {
5309 frame_->CallRuntime(Runtime::kMath_sin, 1);
5310 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005311 frame_->EmitPush(r0);
5312}
5313
5314
5315void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5316 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005317 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005318 if (CpuFeatures::IsSupported(VFP3)) {
5319 TranscendentalCacheStub stub(TranscendentalCache::COS);
5320 frame_->SpillAllButCopyTOSToR0();
5321 frame_->CallStub(&stub, 1);
5322 } else {
5323 frame_->CallRuntime(Runtime::kMath_cos, 1);
5324 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005325 frame_->EmitPush(r0);
5326}
5327
5328
ager@chromium.org7c537e22008-10-16 08:43:32 +00005329void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005330 ASSERT(args->length() == 2);
5331
5332 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005333 Load(args->at(0));
5334 Load(args->at(1));
5335 Register lhs = frame_->PopToRegister();
5336 Register rhs = frame_->PopToRegister(lhs);
5337 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005338 cc_reg_ = eq;
5339}
5340
5341
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005342void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5343 ASSERT(args->length() == 2);
5344
5345 // Load the two objects into registers and perform the comparison.
5346 Load(args->at(0));
5347 Load(args->at(1));
5348 Register right = frame_->PopToRegister();
5349 Register left = frame_->PopToRegister(right);
5350 Register tmp = frame_->scratch0();
5351 Register tmp2 = frame_->scratch1();
5352
5353 // Jumps to done must have the eq flag set if the test is successful
5354 // and clear if the test has failed.
5355 Label done;
5356
5357 // Fail if either is a non-HeapObject.
5358 __ cmp(left, Operand(right));
5359 __ b(eq, &done);
5360 __ and_(tmp, left, Operand(right));
5361 __ eor(tmp, tmp, Operand(kSmiTagMask));
5362 __ tst(tmp, Operand(kSmiTagMask));
5363 __ b(ne, &done);
5364 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5365 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5366 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5367 __ b(ne, &done);
5368 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5369 __ cmp(tmp, Operand(tmp2));
5370 __ b(ne, &done);
5371 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5372 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5373 __ cmp(tmp, tmp2);
5374 __ bind(&done);
5375 cc_reg_ = eq;
5376}
5377
5378
5379
ager@chromium.org7c537e22008-10-16 08:43:32 +00005380void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005381#ifdef DEBUG
5382 int original_height = frame_->height();
5383#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005384 if (CheckForInlineRuntimeCall(node)) {
5385 ASSERT((has_cc() && frame_->height() == original_height) ||
5386 (!has_cc() && frame_->height() == original_height + 1));
5387 return;
5388 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005389
5390 ZoneList<Expression*>* args = node->arguments();
5391 Comment cmnt(masm_, "[ CallRuntime");
5392 Runtime::Function* function = node->function();
5393
ager@chromium.org41826e72009-03-30 13:30:57 +00005394 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005395 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005396 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005397 Register scratch = VirtualFrame::scratch0();
5398 __ ldr(scratch, GlobalObject());
5399 Register builtins = frame_->GetTOSRegister();
5400 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5401 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005402 }
mads.s.ager31e71382008-08-13 09:32:07 +00005403
ager@chromium.org41826e72009-03-30 13:30:57 +00005404 // Push the arguments ("left-to-right").
5405 int arg_count = args->length();
5406 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005407 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005408 }
mads.s.ager31e71382008-08-13 09:32:07 +00005409
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005410 VirtualFrame::SpilledScope spilled_scope(frame_);
5411
ager@chromium.org41826e72009-03-30 13:30:57 +00005412 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005413 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005414 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005415 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5416 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005417 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005418 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005419 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005420 } else {
5421 // Call the C runtime function.
5422 frame_->CallRuntime(function, arg_count);
5423 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005424 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005425 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005426}
5427
5428
ager@chromium.org7c537e22008-10-16 08:43:32 +00005429void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005430#ifdef DEBUG
5431 int original_height = frame_->height();
5432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005433 Comment cmnt(masm_, "[ UnaryOperation");
5434
5435 Token::Value op = node->op();
5436
5437 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005438 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005439 // LoadCondition may (and usually does) leave a test and branch to
5440 // be emitted by the caller. In that case, negate the condition.
5441 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005442
5443 } else if (op == Token::DELETE) {
5444 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005445 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005446 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005447 Load(property->obj());
5448 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005449 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005450 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005451
mads.s.ager31e71382008-08-13 09:32:07 +00005452 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005453 Slot* slot = variable->slot();
5454 if (variable->is_global()) {
5455 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005456 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005457 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005458 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005459
5460 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5461 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005462 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005463 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005464 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005465 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005466 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005467 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005468 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005469 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005470
mads.s.ager31e71382008-08-13 09:32:07 +00005471 } else {
5472 // Default: Result of deleting non-global, not dynamically
5473 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005474 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005475 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005476
5477 } else {
5478 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005479 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005480 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005481 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005482 }
5483
5484 } else if (op == Token::TYPEOF) {
5485 // Special case for loading the typeof expression; see comment on
5486 // LoadTypeofExpression().
5487 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005488 frame_->CallRuntime(Runtime::kTypeof, 1);
5489 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005490
5491 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005492 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005493 (node->expression()->AsBinaryOperation() != NULL &&
5494 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005495 UnaryOverwriteMode overwrite =
5496 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5497
5498 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005499 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005500 switch (op) {
5501 case Token::NOT:
5502 case Token::DELETE:
5503 case Token::TYPEOF:
5504 UNREACHABLE(); // handled above
5505 break;
5506
5507 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005508 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005509 GenericUnaryOpStub stub(
5510 Token::SUB,
5511 overwrite,
5512 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005513 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005514 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005515 break;
5516 }
5517
5518 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005519 Register tos = frame_->PopToRegister();
5520 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005521 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005522 // Smi check.
5523 __ tst(tos, Operand(kSmiTagMask));
5524 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005525
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005526 __ mvn(tos, Operand(tos));
5527 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5528 frame_->EmitPush(tos);
5529 // The fast case is the first to jump to the continue label, so it gets
5530 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005531 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005532
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005533 not_smi_label.Bind();
5534 frame_->SpillAll();
5535 __ Move(r0, tos);
5536 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5537 frame_->CallStub(&stub, 0);
5538 frame_->EmitPush(r0);
5539
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005540 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005541 break;
5542 }
5543
5544 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005545 frame_->Drop();
5546 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005547 break;
5548
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005549 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005550 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005551 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005552 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005553 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005554 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005555
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005556 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005557 frame_->EmitPush(r0);
5558
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005559 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005560 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005561 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005562 default:
5563 UNREACHABLE();
5564 }
5565 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005566 ASSERT(!has_valid_frame() ||
5567 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005568 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005569}
5570
5571
ager@chromium.org7c537e22008-10-16 08:43:32 +00005572void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005573#ifdef DEBUG
5574 int original_height = frame_->height();
5575#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005576 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005577 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005578
5579 bool is_postfix = node->is_postfix();
5580 bool is_increment = node->op() == Token::INC;
5581
5582 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5583 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005584 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005585
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005586 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5587 // The type info declares that this variable is always a Smi. That
5588 // means it is a Smi both before and after the increment/decrement.
5589 // Lets make use of that to make a very minimal count.
5590 Reference target(this, node->expression(), !is_const);
5591 ASSERT(!target.is_illegal());
5592 target.GetValue(); // Pushes the value.
5593 Register value = frame_->PopToRegister();
5594 if (is_postfix) frame_->EmitPush(value);
5595 if (is_increment) {
5596 __ add(value, value, Operand(Smi::FromInt(1)));
5597 } else {
5598 __ sub(value, value, Operand(Smi::FromInt(1)));
5599 }
5600 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005601 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005602 if (is_postfix) frame_->Pop();
5603 ASSERT_EQ(original_height + 1, frame_->height());
5604 return;
5605 }
5606
5607 // If it's a postfix expression and its result is not ignored and the
5608 // reference is non-trivial, then push a placeholder on the stack now
5609 // to hold the result of the expression.
5610 bool placeholder_pushed = false;
5611 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005612 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005613 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005615
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005616 // A constant reference is not saved to, so a constant reference is not a
5617 // compound assignment reference.
5618 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005619 if (target.is_illegal()) {
5620 // Spoof the virtual frame to have the expected height (one higher
5621 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005622 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005623 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005624 return;
5625 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005626
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005627 // This pushes 0, 1 or 2 words on the object to be used later when updating
5628 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005629 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005630
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005631 JumpTarget slow;
5632 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005633
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005634 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005635
5636 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005637 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005638 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005639 } else if (is_postfix) {
5640 frame_->EmitPush(value);
5641 __ mov(VirtualFrame::scratch0(), value);
5642 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005643 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005644
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005645 // Check for smi operand.
5646 __ tst(value, Operand(kSmiTagMask));
5647 slow.Branch(ne);
5648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005649 // Perform optimistic increment/decrement.
5650 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005651 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005652 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005653 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005654 }
5655
5656 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005657 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005658
5659 // Revert optimistic increment/decrement.
5660 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005661 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005662 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005663 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005664 }
5665
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005666 // Slow case: Convert to number. At this point the
5667 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005668 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005669
5670 // Convert the operand to a number.
5671 frame_->EmitPush(value);
5672
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005673 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005674 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005675 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005676
5677 if (is_postfix) {
5678 // Postfix: store to result (on the stack).
5679 __ str(r0, frame_->ElementAt(target.size()));
5680 }
5681
5682 // Compute the new value.
5683 frame_->EmitPush(r0);
5684 frame_->EmitPush(Operand(Smi::FromInt(1)));
5685 if (is_increment) {
5686 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5687 } else {
5688 frame_->CallRuntime(Runtime::kNumberSub, 2);
5689 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690 }
5691
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005692 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005693 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005694 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005695 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005696 frame_->EmitPush(value);
5697 // Set the target with the result, leaving the result on
5698 // top of the stack. Removes the target from the stack if
5699 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005700 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005701 }
5702
5703 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005704 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005705 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005706}
5707
5708
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005709void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005710 // According to ECMA-262 section 11.11, page 58, the binary logical
5711 // operators must yield the result of one of the two expressions
5712 // before any ToBoolean() conversions. This means that the value
5713 // produced by a && or || operator is not necessarily a boolean.
5714
5715 // NOTE: If the left hand side produces a materialized value (not in
5716 // the CC register), we force the right hand side to do the
5717 // same. This is necessary because we may have to branch to the exit
5718 // after evaluating the left hand side (due to the shortcut
5719 // semantics), but the compiler must (statically) know if the result
5720 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005721 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005722 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005723 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005724 if (has_valid_frame() && !has_cc()) {
5725 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005726 JumpTarget pop_and_continue;
5727 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005728
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005729 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005730 // Avoid popping the result if it converts to 'false' using the
5731 // standard ToBoolean() conversion as described in ECMA-262,
5732 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005733 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005734 Branch(false, &exit);
5735
5736 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005737 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005738 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005739
5740 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005741 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005742 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005743
5744 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005745 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005746 } else if (has_cc() || is_true.is_linked()) {
5747 // The left-hand side is either (a) partially compiled to
5748 // control flow with a final branch left to emit or (b) fully
5749 // compiled to control flow and possibly true.
5750 if (has_cc()) {
5751 Branch(false, false_target());
5752 }
5753 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005754 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005755 } else {
5756 // Nothing to do.
5757 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005758 }
5759
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005760 } else {
5761 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005762 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005763 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005764 if (has_valid_frame() && !has_cc()) {
5765 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005766 JumpTarget pop_and_continue;
5767 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005768
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005769 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005770 // Avoid popping the result if it converts to 'true' using the
5771 // standard ToBoolean() conversion as described in ECMA-262,
5772 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005773 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005774 Branch(true, &exit);
5775
5776 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005777 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005778 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005779
5780 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005781 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005782 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005783
5784 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005785 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005786 } else if (has_cc() || is_false.is_linked()) {
5787 // The left-hand side is either (a) partially compiled to
5788 // control flow with a final branch left to emit or (b) fully
5789 // compiled to control flow and possibly false.
5790 if (has_cc()) {
5791 Branch(true, true_target());
5792 }
5793 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005794 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005795 } else {
5796 // Nothing to do.
5797 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005798 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005799 }
5800}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005801
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005802
5803void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5804#ifdef DEBUG
5805 int original_height = frame_->height();
5806#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005807 Comment cmnt(masm_, "[ BinaryOperation");
5808
5809 if (node->op() == Token::AND || node->op() == Token::OR) {
5810 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005811 } else {
5812 // Optimize for the case where (at least) one of the expressions
5813 // is a literal small integer.
5814 Literal* lliteral = node->left()->AsLiteral();
5815 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005816 // NOTE: The code below assumes that the slow cases (calls to runtime)
5817 // never return a constant/immutable object.
5818 bool overwrite_left =
5819 (node->left()->AsBinaryOperation() != NULL &&
5820 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5821 bool overwrite_right =
5822 (node->right()->AsBinaryOperation() != NULL &&
5823 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005824
5825 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005826 VirtualFrame::RegisterAllocationScope scope(this);
5827 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005828 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005829 SmiOperation(node->op(),
5830 rliteral->handle(),
5831 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005832 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005833 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005834 VirtualFrame::RegisterAllocationScope scope(this);
5835 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005836 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005837 SmiOperation(node->op(),
5838 lliteral->handle(),
5839 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005840 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005841 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005842 GenerateInlineSmi inline_smi =
5843 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
5844 if (lliteral != NULL) {
5845 ASSERT(!lliteral->handle()->IsSmi());
5846 inline_smi = DONT_GENERATE_INLINE_SMI;
5847 }
5848 if (rliteral != NULL) {
5849 ASSERT(!rliteral->handle()->IsSmi());
5850 inline_smi = DONT_GENERATE_INLINE_SMI;
5851 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005852 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005853 OverwriteMode overwrite_mode = NO_OVERWRITE;
5854 if (overwrite_left) {
5855 overwrite_mode = OVERWRITE_LEFT;
5856 } else if (overwrite_right) {
5857 overwrite_mode = OVERWRITE_RIGHT;
5858 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005859 Load(node->left());
5860 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005861 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005862 }
5863 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005864 ASSERT(!has_valid_frame() ||
5865 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005866 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005867}
5868
5869
ager@chromium.org7c537e22008-10-16 08:43:32 +00005870void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005871#ifdef DEBUG
5872 int original_height = frame_->height();
5873#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005874 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005875 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005876}
5877
5878
ager@chromium.org7c537e22008-10-16 08:43:32 +00005879void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005880#ifdef DEBUG
5881 int original_height = frame_->height();
5882#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005883 Comment cmnt(masm_, "[ CompareOperation");
5884
ager@chromium.org357bf652010-04-12 11:30:10 +00005885 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5886
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005887 // Get the expressions from the node.
5888 Expression* left = node->left();
5889 Expression* right = node->right();
5890 Token::Value op = node->op();
5891
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005892 // To make null checks efficient, we check if either left or right is the
5893 // literal 'null'. If so, we optimize the code by inlining a null check
5894 // instead of calling the (very) general runtime routine for checking
5895 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005896 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005897 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005898 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005899 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005900 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5901 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005902 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005903 Load(left_is_null ? right : left);
5904 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005905 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005906 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005907
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005908 // The 'null' value is only equal to 'undefined' if using non-strict
5909 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005910 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005911 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005912
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005913 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005914 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005915 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005916
ager@chromium.org357bf652010-04-12 11:30:10 +00005917 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005918 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005919
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005920 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005921 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5922 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5923 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5924 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005925 }
5926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005927 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005928 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005929 return;
5930 }
5931 }
5932
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005933 // To make typeof testing for natives implemented in JavaScript really
5934 // efficient, we generate special code for expressions of the form:
5935 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005936 UnaryOperation* operation = left->AsUnaryOperation();
5937 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5938 (operation != NULL && operation->op() == Token::TYPEOF) &&
5939 (right->AsLiteral() != NULL &&
5940 right->AsLiteral()->handle()->IsString())) {
5941 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5942
ager@chromium.org357bf652010-04-12 11:30:10 +00005943 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005944 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005945 Register tos = frame_->PopToRegister();
5946
ager@chromium.org357bf652010-04-12 11:30:10 +00005947 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005948
5949 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005950 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005951 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005952 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005953 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005954 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005955 cc_reg_ = eq;
5956
5957 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005958 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005959 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005960
ager@chromium.org357bf652010-04-12 11:30:10 +00005961 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005962
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005963 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005964 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5965 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5966 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005967 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005968
ager@chromium.org357bf652010-04-12 11:30:10 +00005969 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5970 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005971 cc_reg_ = lt;
5972
5973 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005974 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005975 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005976 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005977 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005978 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005979 cc_reg_ = eq;
5980
5981 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005982 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005983 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005984 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005985
ager@chromium.org357bf652010-04-12 11:30:10 +00005986 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005987 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005988
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005989 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005990 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5991 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5992 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5993 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005994
5995 cc_reg_ = eq;
5996
5997 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005998 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005999 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006000 Register map_reg = scratch;
6001 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006002 true_target()->Branch(eq);
6003 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006004 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006005 cc_reg_ = eq;
6006
6007 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006008 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006009 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006010
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006011 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006012 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006013 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006014
ager@chromium.org357bf652010-04-12 11:30:10 +00006015 Register map_reg = scratch;
6016 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006017 false_target()->Branch(eq);
6018
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006019 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006020 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6021 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6022 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006023 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006024
ager@chromium.org357bf652010-04-12 11:30:10 +00006025 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6026 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006027 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006028 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006029 cc_reg_ = le;
6030
6031 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006032 // Uncommon case: typeof testing against a string literal that is
6033 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006034 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006035 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006036 ASSERT(!has_valid_frame() ||
6037 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006038 return;
6039 }
6040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006041 switch (op) {
6042 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006043 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006044 break;
6045
6046 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006047 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006048 break;
6049
6050 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006051 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006052 break;
6053
6054 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006055 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006056 break;
6057
6058 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006059 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006060 break;
6061
6062 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006063 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006064 break;
6065
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006066 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006067 Load(left);
6068 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006069 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006070 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006071 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006073
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006074 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006075 Load(left);
6076 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006077 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006078 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006079 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006080 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006081 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006082 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006084
6085 default:
6086 UNREACHABLE();
6087 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006088 ASSERT((has_cc() && frame_->height() == original_height) ||
6089 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006090}
6091
6092
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006093class DeferredReferenceGetNamedValue: public DeferredCode {
6094 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006095 explicit DeferredReferenceGetNamedValue(Register receiver,
6096 Handle<String> name)
6097 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006098 set_comment("[ DeferredReferenceGetNamedValue");
6099 }
6100
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006101 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006102
6103 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006104 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006105 Handle<String> name_;
6106};
6107
6108
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006109// Convention for this is that on entry the receiver is in a register that
6110// is not used by the stack. On exit the answer is found in that same
6111// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006112void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006113#ifdef DEBUG
6114 int expected_height = frame_state()->frame()->height();
6115#endif
6116 VirtualFrame copied_frame(*frame_state()->frame());
6117 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006118
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006119 Register scratch1 = VirtualFrame::scratch0();
6120 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006121 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006122 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6123 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6124
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006125 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6126 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006127 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006128
6129 // The rest of the instructions in the deferred code must be together.
6130 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6131 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6132 __ Call(ic, RelocInfo::CODE_TARGET);
6133 // The call must be followed by a nop(1) instruction to indicate that the
6134 // in-object has been inlined.
6135 __ nop(PROPERTY_ACCESS_INLINED);
6136
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006137 // At this point the answer is in r0. We move it to the expected register
6138 // if necessary.
6139 __ Move(receiver_, r0);
6140
6141 // Now go back to the frame that we entered with. This will not overwrite
6142 // the receiver register since that register was not in use when we came
6143 // in. The instructions emitted by this merge are skipped over by the
6144 // inline load patching mechanism when looking for the branch instruction
6145 // that tells it where the code to patch is.
6146 copied_frame.MergeTo(frame_state()->frame());
6147
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006148 // Block the constant pool for one more instruction after leaving this
6149 // constant pool block scope to include the branch instruction ending the
6150 // deferred code.
6151 __ BlockConstPoolFor(1);
6152 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006153 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006154}
6155
6156
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006157class DeferredReferenceGetKeyedValue: public DeferredCode {
6158 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006159 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6160 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006161 set_comment("[ DeferredReferenceGetKeyedValue");
6162 }
6163
6164 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006165
6166 private:
6167 Register key_;
6168 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006169};
6170
6171
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006172// Takes key and register in r0 and r1 or vice versa. Returns result
6173// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006174void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006175 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6176 (key_.is(r1) && receiver_.is(r0)));
6177
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006178 VirtualFrame copied_frame(*frame_state()->frame());
6179 copied_frame.SpillAll();
6180
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006181 Register scratch1 = VirtualFrame::scratch0();
6182 Register scratch2 = VirtualFrame::scratch1();
6183 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6184 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6185
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006186 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6187 // convention.
6188 if (key_.is(r1)) {
6189 __ Swap(r0, r1, ip);
6190 }
6191
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006192 // The rest of the instructions in the deferred code must be together.
6193 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006194 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006195 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6196 __ Call(ic, RelocInfo::CODE_TARGET);
6197 // The call must be followed by a nop instruction to indicate that the
6198 // keyed load has been inlined.
6199 __ nop(PROPERTY_ACCESS_INLINED);
6200
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006201 // Now go back to the frame that we entered with. This will not overwrite
6202 // the receiver or key registers since they were not in use when we came
6203 // in. The instructions emitted by this merge are skipped over by the
6204 // inline load patching mechanism when looking for the branch instruction
6205 // that tells it where the code to patch is.
6206 copied_frame.MergeTo(frame_state()->frame());
6207
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006208 // Block the constant pool for one more instruction after leaving this
6209 // constant pool block scope to include the branch instruction ending the
6210 // deferred code.
6211 __ BlockConstPoolFor(1);
6212 }
6213}
6214
6215
6216class DeferredReferenceSetKeyedValue: public DeferredCode {
6217 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006218 DeferredReferenceSetKeyedValue(Register value,
6219 Register key,
6220 Register receiver)
6221 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006222 set_comment("[ DeferredReferenceSetKeyedValue");
6223 }
6224
6225 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006226
6227 private:
6228 Register value_;
6229 Register key_;
6230 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006231};
6232
6233
6234void DeferredReferenceSetKeyedValue::Generate() {
6235 Register scratch1 = VirtualFrame::scratch0();
6236 Register scratch2 = VirtualFrame::scratch1();
6237 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6238 __ IncrementCounter(
6239 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6240
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006241 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6242 // calling convention.
6243 if (value_.is(r1)) {
6244 __ Swap(r0, r1, ip);
6245 }
6246 ASSERT(receiver_.is(r2));
6247
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006248 // The rest of the instructions in the deferred code must be together.
6249 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006250 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6251 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006252 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6253 __ Call(ic, RelocInfo::CODE_TARGET);
6254 // The call must be followed by a nop instruction to indicate that the
6255 // keyed store has been inlined.
6256 __ nop(PROPERTY_ACCESS_INLINED);
6257
6258 // Block the constant pool for one more instruction after leaving this
6259 // constant pool block scope to include the branch instruction ending the
6260 // deferred code.
6261 __ BlockConstPoolFor(1);
6262 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006263}
6264
6265
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006266class DeferredReferenceSetNamedValue: public DeferredCode {
6267 public:
6268 DeferredReferenceSetNamedValue(Register value,
6269 Register receiver,
6270 Handle<String> name)
6271 : value_(value), receiver_(receiver), name_(name) {
6272 set_comment("[ DeferredReferenceSetNamedValue");
6273 }
6274
6275 virtual void Generate();
6276
6277 private:
6278 Register value_;
6279 Register receiver_;
6280 Handle<String> name_;
6281};
6282
6283
6284// Takes value in r0, receiver in r1 and returns the result (the
6285// value) in r0.
6286void DeferredReferenceSetNamedValue::Generate() {
6287 // Record the entry frame and spill.
6288 VirtualFrame copied_frame(*frame_state()->frame());
6289 copied_frame.SpillAll();
6290
6291 // Ensure value in r0, receiver in r1 to match store ic calling
6292 // convention.
6293 ASSERT(value_.is(r0) && receiver_.is(r1));
6294 __ mov(r2, Operand(name_));
6295
6296 // The rest of the instructions in the deferred code must be together.
6297 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6298 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6299 // r1 and r2.
6300 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6301 __ Call(ic, RelocInfo::CODE_TARGET);
6302 // The call must be followed by a nop instruction to indicate that the
6303 // named store has been inlined.
6304 __ nop(PROPERTY_ACCESS_INLINED);
6305
6306 // Go back to the frame we entered with. The instructions
6307 // generated by this merge are skipped over by the inline store
6308 // patching mechanism when looking for the branch instruction that
6309 // tells it where the code to patch is.
6310 copied_frame.MergeTo(frame_state()->frame());
6311
6312 // Block the constant pool for one more instruction after leaving this
6313 // constant pool block scope to include the branch instruction ending the
6314 // deferred code.
6315 __ BlockConstPoolFor(1);
6316 }
6317}
6318
6319
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006320// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006321void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6322 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6323 Comment cmnt(masm(), "[ Load from named Property");
6324 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006325 frame_->CallLoadIC(name,
6326 is_contextual
6327 ? RelocInfo::CODE_TARGET_CONTEXT
6328 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006329 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006330 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006331 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006332 Comment cmnt(masm(), "[ Inlined named property load");
6333
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006334 // Counter will be decremented in the deferred code. Placed here to avoid
6335 // having it in the instruction stream below where patching will occur.
6336 __ IncrementCounter(&Counters::named_load_inline, 1,
6337 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006338
6339 // The following instructions are the inlined load of an in-object property.
6340 // Parts of this code is patched, so the exact instructions generated needs
6341 // to be fixed. Therefore the instruction pool is blocked when generating
6342 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006343
6344 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006345 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006346
6347 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006348 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006349
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006350#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006351 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006352 Label check_inlined_codesize;
6353 masm_->bind(&check_inlined_codesize);
6354#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006355
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006356 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006357 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006358 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006359 deferred->Branch(eq);
6360
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006361 Register scratch = VirtualFrame::scratch0();
6362 Register scratch2 = VirtualFrame::scratch1();
6363
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006364 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006365 // code. Therefore we can't use a LoadRoot call.
6366 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6367 __ mov(scratch2, Operand(Factory::null_value()));
6368 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006369 deferred->Branch(ne);
6370
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006371 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006372 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006373 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006374
6375 // Make sure that the expected number of instructions are generated.
6376 ASSERT_EQ(kInlinedNamedLoadInstructions,
6377 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006378 }
6379
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006380 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006381 // At this point the receiver register has the result, either from the
6382 // deferred code or from the inlined code.
6383 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006384 }
6385}
6386
6387
ager@chromium.orgac091b72010-05-05 07:34:42 +00006388void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6389#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006390 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006391#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006392
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006393 Result result;
6394 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6395 frame()->CallStoreIC(name, is_contextual);
6396 } else {
6397 // Inline the in-object property case.
6398 JumpTarget slow, done;
6399
6400 // Get the value and receiver from the stack.
6401 frame()->PopToR0();
6402 Register value = r0;
6403 frame()->PopToR1();
6404 Register receiver = r1;
6405
6406 DeferredReferenceSetNamedValue* deferred =
6407 new DeferredReferenceSetNamedValue(value, receiver, name);
6408
6409 // Check that the receiver is a heap object.
6410 __ tst(receiver, Operand(kSmiTagMask));
6411 deferred->Branch(eq);
6412
6413 // The following instructions are the part of the inlined
6414 // in-object property store code which can be patched. Therefore
6415 // the exact number of instructions generated must be fixed, so
6416 // the constant pool is blocked while generating this code.
6417 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6418 Register scratch0 = VirtualFrame::scratch0();
6419 Register scratch1 = VirtualFrame::scratch1();
6420
6421 // Check the map. Initially use an invalid map to force a
6422 // failure. The map check will be patched in the runtime system.
6423 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6424
6425#ifdef DEBUG
6426 Label check_inlined_codesize;
6427 masm_->bind(&check_inlined_codesize);
6428#endif
6429 __ mov(scratch0, Operand(Factory::null_value()));
6430 __ cmp(scratch0, scratch1);
6431 deferred->Branch(ne);
6432
6433 int offset = 0;
6434 __ str(value, MemOperand(receiver, offset));
6435
6436 // Update the write barrier and record its size. We do not use
6437 // the RecordWrite macro here because we want the offset
6438 // addition instruction first to make it easy to patch.
6439 Label record_write_start, record_write_done;
6440 __ bind(&record_write_start);
6441 // Add offset into the object.
6442 __ add(scratch0, receiver, Operand(offset));
6443 // Test that the object is not in the new space. We cannot set
6444 // region marks for new space pages.
6445 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6446 // Record the actual write.
6447 __ RecordWriteHelper(receiver, scratch0, scratch1);
6448 __ bind(&record_write_done);
6449 // Clobber all input registers when running with the debug-code flag
6450 // turned on to provoke errors.
6451 if (FLAG_debug_code) {
6452 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6453 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6454 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6455 }
6456 // Check that this is the first inlined write barrier or that
6457 // this inlined write barrier has the same size as all the other
6458 // inlined write barriers.
6459 ASSERT((inlined_write_barrier_size_ == -1) ||
6460 (inlined_write_barrier_size_ ==
6461 masm()->InstructionsGeneratedSince(&record_write_start)));
6462 inlined_write_barrier_size_ =
6463 masm()->InstructionsGeneratedSince(&record_write_start);
6464
6465 // Make sure that the expected number of instructions are generated.
6466 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6467 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6468 }
6469 deferred->BindExit();
6470 }
6471 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006472}
6473
6474
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006475void CodeGenerator::EmitKeyedLoad() {
6476 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006477 Comment cmnt(masm_, "[ Load from keyed property");
6478 frame_->CallKeyedLoadIC();
6479 } else {
6480 // Inline the keyed load.
6481 Comment cmnt(masm_, "[ Inlined load from keyed property");
6482
6483 // Counter will be decremented in the deferred code. Placed here to avoid
6484 // having it in the instruction stream below where patching will occur.
6485 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6486 frame_->scratch0(), frame_->scratch1());
6487
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006488 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006489 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006490 Register key = frame_->PopToRegister();
6491 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006492
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006493 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006494 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006495 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006496
6497 // Check that the receiver is a heap object.
6498 __ tst(receiver, Operand(kSmiTagMask));
6499 deferred->Branch(eq);
6500
6501 // The following instructions are the part of the inlined load keyed
6502 // property code which can be patched. Therefore the exact number of
6503 // instructions generated need to be fixed, so the constant pool is blocked
6504 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006505 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6506 Register scratch1 = VirtualFrame::scratch0();
6507 Register scratch2 = VirtualFrame::scratch1();
6508 // Check the map. The null map used below is patched by the inline cache
6509 // code.
6510 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006511
6512 // Check that the key is a smi.
6513 if (!key_is_known_smi) {
6514 __ tst(key, Operand(kSmiTagMask));
6515 deferred->Branch(ne);
6516 }
6517
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006518#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006519 Label check_inlined_codesize;
6520 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006521#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006522 __ mov(scratch2, Operand(Factory::null_value()));
6523 __ cmp(scratch1, scratch2);
6524 deferred->Branch(ne);
6525
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006526 // Get the elements array from the receiver and check that it
6527 // is not a dictionary.
6528 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006529 if (FLAG_debug_code) {
6530 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
6531 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
6532 __ cmp(scratch2, ip);
6533 __ Assert(eq, "JSObject with fast elements map has slow elements");
6534 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006535
6536 // Check that key is within bounds. Use unsigned comparison to handle
6537 // negative keys.
6538 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006539 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006540 deferred->Branch(ls); // Unsigned less equal.
6541
6542 // Load and check that the result is not the hole (key is a smi).
6543 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6544 __ add(scratch1,
6545 scratch1,
6546 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006547 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006548 MemOperand(scratch1, key, LSL,
6549 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006550 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006551 deferred->Branch(eq);
6552
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006553 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006554 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006555 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006556 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6557 }
6558
6559 deferred->BindExit();
6560 }
6561}
6562
6563
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006564void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6565 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006566 // Generate inlined version of the keyed store if the code is in a loop
6567 // and the key is likely to be a smi.
6568 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6569 // Inline the keyed store.
6570 Comment cmnt(masm_, "[ Inlined store to keyed property");
6571
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006572 Register scratch1 = VirtualFrame::scratch0();
6573 Register scratch2 = VirtualFrame::scratch1();
6574 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006575
6576 // Counter will be decremented in the deferred code. Placed here to avoid
6577 // having it in the instruction stream below where patching will occur.
6578 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006579 scratch1, scratch2);
6580
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006581
6582
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006583 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006584 bool value_is_harmless = frame_->KnownSmiAt(0);
6585 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6586 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006587 Register value = frame_->PopToRegister();
6588 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006589 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006590 Register receiver = r2;
6591 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006592
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006593#ifdef DEBUG
6594 bool we_remembered_the_write_barrier = value_is_harmless;
6595#endif
6596
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006597 // The deferred code expects value, key and receiver in registers.
6598 DeferredReferenceSetKeyedValue* deferred =
6599 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006600
6601 // Check that the value is a smi. As this inlined code does not set the
6602 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006603 if (!value_is_harmless) {
6604 // If the value is not likely to be a Smi then let's test the fixed array
6605 // for new space instead. See below.
6606 if (wb_info == LIKELY_SMI) {
6607 __ tst(value, Operand(kSmiTagMask));
6608 deferred->Branch(ne);
6609#ifdef DEBUG
6610 we_remembered_the_write_barrier = true;
6611#endif
6612 }
6613 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006614
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006615 if (!key_is_smi) {
6616 // Check that the key is a smi.
6617 __ tst(key, Operand(kSmiTagMask));
6618 deferred->Branch(ne);
6619 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006620
6621 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006622 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006623 deferred->Branch(eq);
6624
6625 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006626 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006627 deferred->Branch(ne);
6628
6629 // Check that the key is within bounds. Both the key and the length of
6630 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006631 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6632 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006633 deferred->Branch(ls); // Unsigned less equal.
6634
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006635 // Get the elements array from the receiver.
6636 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6637 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6638 Label ok;
6639 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6640 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6641 __ tst(value, Operand(kSmiTagMask), ne);
6642 deferred->Branch(ne);
6643#ifdef DEBUG
6644 we_remembered_the_write_barrier = true;
6645#endif
6646 }
6647 // Check that the elements array is not a dictionary.
6648 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006649 // The following instructions are the part of the inlined store keyed
6650 // property code which can be patched. Therefore the exact number of
6651 // instructions generated need to be fixed, so the constant pool is blocked
6652 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006653 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006654#ifdef DEBUG
6655 Label check_inlined_codesize;
6656 masm_->bind(&check_inlined_codesize);
6657#endif
6658
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006659 // Read the fixed array map from the constant pool (not from the root
6660 // array) so that the value can be patched. When debugging, we patch this
6661 // comparison to always fail so that we will hit the IC call in the
6662 // deferred code which will allow the debugger to break for fast case
6663 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006664 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6665 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006666 deferred->Branch(ne);
6667
6668 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006669 __ add(scratch1, scratch1,
6670 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6671 __ str(value,
6672 MemOperand(scratch1, key, LSL,
6673 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006674
6675 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006676 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006677 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6678 }
6679
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006680 ASSERT(we_remembered_the_write_barrier);
6681
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006682 deferred->BindExit();
6683 } else {
6684 frame()->CallKeyedStoreIC();
6685 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006686}
6687
6688
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006689#ifdef DEBUG
6690bool CodeGenerator::HasValidEntryRegisters() { return true; }
6691#endif
6692
6693
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006694#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006695#define __ ACCESS_MASM(masm)
6696
ager@chromium.org7c537e22008-10-16 08:43:32 +00006697Handle<String> Reference::GetName() {
6698 ASSERT(type_ == NAMED);
6699 Property* property = expression_->AsProperty();
6700 if (property == NULL) {
6701 // Global variable reference treated as a named property reference.
6702 VariableProxy* proxy = expression_->AsVariableProxy();
6703 ASSERT(proxy->AsVariable() != NULL);
6704 ASSERT(proxy->AsVariable()->is_global());
6705 return proxy->name();
6706 } else {
6707 Literal* raw_name = property->key()->AsLiteral();
6708 ASSERT(raw_name != NULL);
6709 return Handle<String>(String::cast(*raw_name->handle()));
6710 }
6711}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006712
ager@chromium.org7c537e22008-10-16 08:43:32 +00006713
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006714void Reference::DupIfPersist() {
6715 if (persist_after_get_) {
6716 switch (type_) {
6717 case KEYED:
6718 cgen_->frame()->Dup2();
6719 break;
6720 case NAMED:
6721 cgen_->frame()->Dup();
6722 // Fall through.
6723 case UNLOADED:
6724 case ILLEGAL:
6725 case SLOT:
6726 // Do nothing.
6727 ;
6728 }
6729 } else {
6730 set_unloaded();
6731 }
6732}
6733
6734
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006735void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006736 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006737 ASSERT(!is_illegal());
6738 ASSERT(!cgen_->has_cc());
6739 MacroAssembler* masm = cgen_->masm();
6740 Property* property = expression_->AsProperty();
6741 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006742 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006743 }
6744
6745 switch (type_) {
6746 case SLOT: {
6747 Comment cmnt(masm, "[ Load from Slot");
6748 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6749 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006750 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006751 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006752 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006753 }
6754
ager@chromium.org7c537e22008-10-16 08:43:32 +00006755 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006756 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006757 bool is_global = var != NULL;
6758 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006759 Handle<String> name = GetName();
6760 DupIfPersist();
6761 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006762 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006763 }
6764
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006765 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006766 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006767 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006768 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006769 cgen_->frame()->EmitPush(r0);
6770 break;
6771 }
6772
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006773 default:
6774 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006775 }
6776}
6777
6778
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006779void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006780 ASSERT(!is_illegal());
6781 ASSERT(!cgen_->has_cc());
6782 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006783 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006784 Property* property = expression_->AsProperty();
6785 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006786 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006787 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006788
ager@chromium.org7c537e22008-10-16 08:43:32 +00006789 switch (type_) {
6790 case SLOT: {
6791 Comment cmnt(masm, "[ Store to Slot");
6792 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006793 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006794 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006795 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006796 }
6797
ager@chromium.org7c537e22008-10-16 08:43:32 +00006798 case NAMED: {
6799 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00006800 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006801 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00006802 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006803 break;
6804 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006805
ager@chromium.org7c537e22008-10-16 08:43:32 +00006806 case KEYED: {
6807 Comment cmnt(masm, "[ Store to keyed Property");
6808 Property* property = expression_->AsProperty();
6809 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006810 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006811 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006812 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006813 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006814 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006815 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00006816
6817 default:
6818 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006819 }
6820}
6821
6822
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006823void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006824 // Create a new closure from the given function info in new
6825 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006826 Label gc;
6827
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006828 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006829 __ pop(r3);
6830
6831 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006832 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006833 r0,
6834 r1,
6835 r2,
6836 &gc,
6837 TAG_OBJECT);
6838
6839 // Compute the function map in the current global context and set that
6840 // as the map of the allocated object.
6841 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6842 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
6843 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
6844 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
6845
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006846 // Initialize the rest of the function. We don't have to update the
6847 // write barrier because the allocated object is in new space.
6848 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
6849 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
6850 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
6851 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
6852 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
6853 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
6854 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
6855 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006856
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00006857 // Initialize the code pointer in the function to be the one
6858 // found in the shared function info object.
6859 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
6860 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeOffset));
6861
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006862 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006863 __ Ret();
6864
6865 // Create a new closure through the slower runtime call.
6866 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006867 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006868 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006869}
6870
6871
6872void FastNewContextStub::Generate(MacroAssembler* masm) {
6873 // Try to allocate the context in new space.
6874 Label gc;
6875 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
6876
6877 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006878 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006879 r0,
6880 r1,
6881 r2,
6882 &gc,
6883 TAG_OBJECT);
6884
6885 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00006886 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006887
6888 // Setup the object header.
6889 __ LoadRoot(r2, Heap::kContextMapRootIndex);
6890 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006891 __ mov(r2, Operand(Smi::FromInt(length)));
6892 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006893
6894 // Setup the fixed slots.
6895 __ mov(r1, Operand(Smi::FromInt(0)));
6896 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
6897 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
6898 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
6899 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
6900
6901 // Copy the global object from the surrounding context.
6902 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6903 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
6904
6905 // Initialize the rest of the slots to undefined.
6906 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
6907 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
6908 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
6909 }
6910
6911 // Remove the on-stack argument and return.
6912 __ mov(cp, r0);
6913 __ pop();
6914 __ Ret();
6915
6916 // Need to collect. Call into runtime system.
6917 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006918 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006919}
6920
6921
ager@chromium.org5c838252010-02-19 08:53:10 +00006922void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
6923 // Stack layout on entry:
6924 //
6925 // [sp]: constant elements.
6926 // [sp + kPointerSize]: literal index.
6927 // [sp + (2 * kPointerSize)]: literals array.
6928
6929 // All sizes here are multiples of kPointerSize.
6930 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
6931 int size = JSArray::kSize + elements_size;
6932
6933 // Load boilerplate object into r3 and check if we need to create a
6934 // boilerplate.
6935 Label slow_case;
6936 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6937 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6938 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6939 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6940 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6941 __ cmp(r3, ip);
6942 __ b(eq, &slow_case);
6943
6944 // Allocate both the JS array and the elements array in one big
6945 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006946 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00006947 r0,
6948 r1,
6949 r2,
6950 &slow_case,
6951 TAG_OBJECT);
6952
6953 // Copy the JS array part.
6954 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
6955 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
6956 __ ldr(r1, FieldMemOperand(r3, i));
6957 __ str(r1, FieldMemOperand(r0, i));
6958 }
6959 }
6960
6961 if (length_ > 0) {
6962 // Get hold of the elements array of the boilerplate and setup the
6963 // elements pointer in the resulting object.
6964 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
6965 __ add(r2, r0, Operand(JSArray::kSize));
6966 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
6967
6968 // Copy the elements array.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00006969 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00006970 }
6971
6972 // Return and remove the on-stack parameters.
6973 __ add(sp, sp, Operand(3 * kPointerSize));
6974 __ Ret();
6975
6976 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006977 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00006978}
6979
6980
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006981// Takes a Smi and converts to an IEEE 64 bit floating point value in two
6982// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
6983// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
6984// scratch register. Destroys the source register. No GC occurs during this
6985// stub so you don't have to set up the frame.
6986class ConvertToDoubleStub : public CodeStub {
6987 public:
6988 ConvertToDoubleStub(Register result_reg_1,
6989 Register result_reg_2,
6990 Register source_reg,
6991 Register scratch_reg)
6992 : result1_(result_reg_1),
6993 result2_(result_reg_2),
6994 source_(source_reg),
6995 zeros_(scratch_reg) { }
6996
6997 private:
6998 Register result1_;
6999 Register result2_;
7000 Register source_;
7001 Register zeros_;
7002
7003 // Minor key encoding in 16 bits.
7004 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
7005 class OpBits: public BitField<Token::Value, 2, 14> {};
7006
7007 Major MajorKey() { return ConvertToDouble; }
7008 int MinorKey() {
7009 // Encode the parameters in a unique 16 bit value.
7010 return result1_.code() +
7011 (result2_.code() << 4) +
7012 (source_.code() << 8) +
7013 (zeros_.code() << 12);
7014 }
7015
7016 void Generate(MacroAssembler* masm);
7017
7018 const char* GetName() { return "ConvertToDoubleStub"; }
7019
7020#ifdef DEBUG
7021 void Print() { PrintF("ConvertToDoubleStub\n"); }
7022#endif
7023};
7024
7025
7026void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
7027#ifndef BIG_ENDIAN_FLOATING_POINT
7028 Register exponent = result1_;
7029 Register mantissa = result2_;
7030#else
7031 Register exponent = result2_;
7032 Register mantissa = result1_;
7033#endif
7034 Label not_special;
7035 // Convert from Smi to integer.
7036 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
7037 // Move sign bit from source to destination. This works because the sign bit
7038 // in the exponent word of the double has the same position and polarity as
7039 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007040 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007041 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
7042 // Subtract from 0 if source was negative.
7043 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007044
7045 // We have -1, 0 or 1, which we treat specially. Register source_ contains
7046 // absolute value: it is either equal to 1 (special case of -1 and 1),
7047 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007048 __ cmp(source_, Operand(1));
7049 __ b(gt, &not_special);
7050
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007051 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
7052 static const uint32_t exponent_word_for_1 =
7053 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007054 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007055 // 1, 0 and -1 all have 0 for the second word.
7056 __ mov(mantissa, Operand(0));
7057 __ Ret();
7058
7059 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007060 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007061 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007062 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007063 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007064 // We use mantissa as a scratch register here. Use a fudge factor to
7065 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7066 // that fit in the ARM's constant field.
7067 int fudge = 0x400;
7068 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7069 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007070 __ orr(exponent,
7071 exponent,
7072 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7073 // Shift up the source chopping the top bit off.
7074 __ add(zeros_, zeros_, Operand(1));
7075 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7076 __ mov(source_, Operand(source_, LSL, zeros_));
7077 // Compute lower part of fraction (last 12 bits).
7078 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7079 // And the top (top 20 bits).
7080 __ orr(exponent,
7081 exponent,
7082 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7083 __ Ret();
7084}
7085
7086
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007087// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007088void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007089 Label max_negative_int;
7090 // the_int_ has the answer which is a signed int32 but not a Smi.
7091 // We test for the special value that has a different exponent. This test
7092 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007093 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007094 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007095 __ b(eq, &max_negative_int);
7096 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7097 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7098 uint32_t non_smi_exponent =
7099 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7100 __ mov(scratch_, Operand(non_smi_exponent));
7101 // Set the sign bit in scratch_ if the value was negative.
7102 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7103 // Subtract from 0 if the value was negative.
7104 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7105 // We should be masking the implict first digit of the mantissa away here,
7106 // but it just ends up combining harmlessly with the last digit of the
7107 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7108 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7109 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7110 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7111 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7112 __ str(scratch_, FieldMemOperand(the_heap_number_,
7113 HeapNumber::kExponentOffset));
7114 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7115 __ str(scratch_, FieldMemOperand(the_heap_number_,
7116 HeapNumber::kMantissaOffset));
7117 __ Ret();
7118
7119 __ bind(&max_negative_int);
7120 // The max negative int32 is stored as a positive number in the mantissa of
7121 // a double because it uses a sign bit instead of using two's complement.
7122 // The actual mantissa bits stored are all 0 because the implicit most
7123 // significant 1 bit is not stored.
7124 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7125 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7126 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7127 __ mov(ip, Operand(0));
7128 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7129 __ Ret();
7130}
7131
7132
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007133// Handle the case where the lhs and rhs are the same object.
7134// Equality is almost reflexive (everything but NaN), so this is a test
7135// for "identity and not NaN".
7136static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7137 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007138 Condition cc,
7139 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007140 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007141 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007142 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007143 __ b(ne, &not_identical);
7144
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007145 // The two objects are identical. If we know that one of them isn't NaN then
7146 // we now know they test equal.
7147 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007148 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7149 // so we do the second best thing - test it ourselves.
7150 // They are both equal and they are not both Smis so both of them are not
7151 // Smis. If it's not a heap number, then return equal.
7152 if (cc == lt || cc == gt) {
7153 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007154 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007155 } else {
7156 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7157 __ b(eq, &heap_number);
7158 // Comparing JS objects with <=, >= is complicated.
7159 if (cc != eq) {
7160 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7161 __ b(ge, slow);
7162 // Normally here we fall through to return_equal, but undefined is
7163 // special: (undefined == undefined) == true, but
7164 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7165 if (cc == le || cc == ge) {
7166 __ cmp(r4, Operand(ODDBALL_TYPE));
7167 __ b(ne, &return_equal);
7168 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007169 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007170 __ b(ne, &return_equal);
7171 if (cc == le) {
7172 // undefined <= undefined should fail.
7173 __ mov(r0, Operand(GREATER));
7174 } else {
7175 // undefined >= undefined should fail.
7176 __ mov(r0, Operand(LESS));
7177 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007178 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007179 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007180 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007181 }
7182 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007183
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007184 __ bind(&return_equal);
7185 if (cc == lt) {
7186 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7187 } else if (cc == gt) {
7188 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7189 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007190 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007191 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007192 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007193
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007194 if (cc != eq || !never_nan_nan) {
7195 // For less and greater we don't have to check for NaN since the result of
7196 // x < x is false regardless. For the others here is some code to check
7197 // for NaN.
7198 if (cc != lt && cc != gt) {
7199 __ bind(&heap_number);
7200 // It is a heap number, so return non-equal if it's NaN and equal if it's
7201 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007202
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007203 // The representation of NaN values has all exponent bits (52..62) set,
7204 // and not all mantissa bits (0..51) clear.
7205 // Read top bits of double representation (second word of value).
7206 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7207 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007208 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7209 // NaNs have all-one exponents so they sign extend to -1.
7210 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007211 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007212
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007213 // Shift out flag and all exponent bits, retaining only mantissa.
7214 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7215 // Or with all low-bits of mantissa.
7216 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7217 __ orr(r0, r3, Operand(r2), SetCC);
7218 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007219 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7220 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7221 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007222 if (cc != eq) {
7223 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007224 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007225 if (cc == le) {
7226 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7227 } else {
7228 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7229 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007230 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007231 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007232 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007233 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007234 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007235
7236 __ bind(&not_identical);
7237}
7238
7239
7240// See comment at call site.
7241static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007242 Register lhs,
7243 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007244 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007245 Label* slow,
7246 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007247 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7248 (lhs.is(r1) && rhs.is(r0)));
7249
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007250 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007251 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007252 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007253
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007254 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007255 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007256 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007257 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007258 // succeed. Return non-equal
7259 // If rhs is r0 then there is already a non zero value in it.
7260 if (!rhs.is(r0)) {
7261 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7262 }
7263 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007264 } else {
7265 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7266 // the runtime.
7267 __ b(ne, slow);
7268 }
7269
ager@chromium.orgb5737492010-07-15 09:29:43 +00007270 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007271 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007272 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007273 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007274 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007275 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007276 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007277 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007278 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007279 __ push(lr);
7280 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007281 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007282 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7283 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007284 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007285 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007286 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007287 }
7288
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007289 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007290 // since it's a smi.
7291 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007293 __ bind(&rhs_is_smi);
7294 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007295 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007296 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007297 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007298 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007299 // If lhs is r0 then there is already a non zero value in it.
7300 if (!lhs.is(r0)) {
7301 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7302 }
7303 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007304 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007305 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007306 // the runtime.
7307 __ b(ne, slow);
7308 }
7309
ager@chromium.orgb5737492010-07-15 09:29:43 +00007310 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007311 if (CpuFeatures::IsSupported(VFP3)) {
7312 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007313 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007314 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007315 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007316 // Convert rhs to a double in d6 .
7317 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007318 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007319 __ push(lr);
7320 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007321 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007322 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007323 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007324 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7325 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007326 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007327 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007328 // Fall through to both_loaded_as_doubles.
7329}
7330
7331
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007332void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007333 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007334 Register rhs_exponent = exp_first ? r0 : r1;
7335 Register lhs_exponent = exp_first ? r2 : r3;
7336 Register rhs_mantissa = exp_first ? r1 : r0;
7337 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007338 Label one_is_nan, neither_is_nan;
7339
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007340 __ Sbfx(r4,
7341 lhs_exponent,
7342 HeapNumber::kExponentShift,
7343 HeapNumber::kExponentBits);
7344 // NaNs have all-one exponents so they sign extend to -1.
7345 __ cmp(r4, Operand(-1));
7346 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007347 __ mov(r4,
7348 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7349 SetCC);
7350 __ b(ne, &one_is_nan);
7351 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007352 __ b(ne, &one_is_nan);
7353
7354 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007355 __ Sbfx(r4,
7356 rhs_exponent,
7357 HeapNumber::kExponentShift,
7358 HeapNumber::kExponentBits);
7359 // NaNs have all-one exponents so they sign extend to -1.
7360 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007361 __ b(ne, &neither_is_nan);
7362 __ mov(r4,
7363 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7364 SetCC);
7365 __ b(ne, &one_is_nan);
7366 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007367 __ b(eq, &neither_is_nan);
7368
7369 __ bind(&one_is_nan);
7370 // NaN comparisons always fail.
7371 // Load whatever we need in r0 to make the comparison fail.
7372 if (cc == lt || cc == le) {
7373 __ mov(r0, Operand(GREATER));
7374 } else {
7375 __ mov(r0, Operand(LESS));
7376 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007377 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007378
7379 __ bind(&neither_is_nan);
7380}
7381
7382
7383// See comment at call site.
7384static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7385 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007386 Register rhs_exponent = exp_first ? r0 : r1;
7387 Register lhs_exponent = exp_first ? r2 : r3;
7388 Register rhs_mantissa = exp_first ? r1 : r0;
7389 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007390
7391 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7392 if (cc == eq) {
7393 // Doubles are not equal unless they have the same bit pattern.
7394 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007395 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7396 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007397 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007398 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007399
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007400 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007401 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007402 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007403
7404 // Exponents are unequal. The only way we can return that the numbers
7405 // are equal is if one is -0 and the other is 0. We already dealt
7406 // with the case where both are -0 or both are 0.
7407 // We start by seeing if the mantissas (that are equal) or the bottom
7408 // 31 bits of the rhs exponent are non-zero. If so we return not
7409 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007410 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007411 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007412 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007413 // Now they are equal if and only if the lhs exponent is zero in its
7414 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007415 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007416 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007417 } else {
7418 // Call a native function to do a comparison between two non-NaNs.
7419 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007420 __ push(lr);
7421 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7422 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7423 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007424 }
7425}
7426
7427
7428// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007429static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7430 Register lhs,
7431 Register rhs) {
7432 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7433 (lhs.is(r1) && rhs.is(r0)));
7434
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007435 // If either operand is a JSObject or an oddball value, then they are
7436 // not equal since their pointers are different.
7437 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007438 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007439 Label first_non_object;
7440 // Get the type of the first operand into r2 and compare it with
7441 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007442 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007443 __ b(lt, &first_non_object);
7444
7445 // Return non-zero (r0 is not zero)
7446 Label return_not_equal;
7447 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007448 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007449
7450 __ bind(&first_non_object);
7451 // Check for oddballs: true, false, null, undefined.
7452 __ cmp(r2, Operand(ODDBALL_TYPE));
7453 __ b(eq, &return_not_equal);
7454
ager@chromium.orgb5737492010-07-15 09:29:43 +00007455 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007456 __ b(ge, &return_not_equal);
7457
7458 // Check for oddballs: true, false, null, undefined.
7459 __ cmp(r3, Operand(ODDBALL_TYPE));
7460 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007461
7462 // Now that we have the types we might as well check for symbol-symbol.
7463 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007464 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7465 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007466 __ and_(r2, r2, Operand(r3));
7467 __ tst(r2, Operand(kIsSymbolMask));
7468 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007469}
7470
7471
7472// See comment at call site.
7473static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007474 Register lhs,
7475 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007476 Label* both_loaded_as_doubles,
7477 Label* not_heap_numbers,
7478 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007479 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7480 (lhs.is(r1) && rhs.is(r0)));
7481
7482 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007483 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007484 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007485 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007486 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7487
7488 // Both are heap numbers. Load them up then jump to the code we have
7489 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007490 if (CpuFeatures::IsSupported(VFP3)) {
7491 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007492 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007493 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007494 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007495 __ vldr(d7, r7, HeapNumber::kValueOffset);
7496 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007497 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7498 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007499 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007500 __ jmp(both_loaded_as_doubles);
7501}
7502
7503
7504// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007505static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007506 Register lhs,
7507 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007508 Label* possible_strings,
7509 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007510 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7511 (lhs.is(r1) && rhs.is(r0)));
7512
7513 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007514 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007515 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007516 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007517 __ tst(r2, Operand(kIsNotStringMask));
7518 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007519 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007520 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007521 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007522 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007523 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007524 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007525
7526 // Both are symbols. We already checked they weren't the same pointer
7527 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007528 __ mov(r0, Operand(NOT_EQUAL));
7529 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007530
7531 __ bind(&object_test);
7532 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7533 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007534 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007535 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007536 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007537 // are not equal, since they are different objects and an object is not
7538 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007539 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007540 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7541 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7542 __ and_(r0, r2, Operand(r3));
7543 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7544 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007545 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007546}
7547
7548
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007549void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7550 Register object,
7551 Register result,
7552 Register scratch1,
7553 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007554 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007555 bool object_is_smi,
7556 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007557 // Use of registers. Register result is used as a temporary.
7558 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007559 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007560
7561 // Load the number string cache.
7562 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7563
7564 // Make the hash mask from the length of the number string cache. It
7565 // contains two elements (number and string) for each cache entry.
7566 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007567 // Divide length by two (length is a smi).
7568 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007569 __ sub(mask, mask, Operand(1)); // Make mask.
7570
7571 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007572 // number string cache for smis is just the smi value, and the hash for
7573 // doubles is the xor of the upper and lower words. See
7574 // Heap::GetNumberStringCache.
7575 Label is_smi;
7576 Label load_result_from_cache;
7577 if (!object_is_smi) {
7578 __ BranchOnSmi(object, &is_smi);
7579 if (CpuFeatures::IsSupported(VFP3)) {
7580 CpuFeatures::Scope scope(VFP3);
7581 __ CheckMap(object,
7582 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007583 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007584 not_found,
7585 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007586
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007587 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007588 __ add(scratch1,
7589 object,
7590 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7591 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7592 __ eor(scratch1, scratch1, Operand(scratch2));
7593 __ and_(scratch1, scratch1, Operand(mask));
7594
7595 // Calculate address of entry in string cache: each entry consists
7596 // of two pointer sized fields.
7597 __ add(scratch1,
7598 number_string_cache,
7599 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7600
7601 Register probe = mask;
7602 __ ldr(probe,
7603 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7604 __ BranchOnSmi(probe, not_found);
7605 __ sub(scratch2, object, Operand(kHeapObjectTag));
7606 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7607 __ sub(probe, probe, Operand(kHeapObjectTag));
7608 __ vldr(d1, probe, HeapNumber::kValueOffset);
7609 __ vcmp(d0, d1);
7610 __ vmrs(pc);
7611 __ b(ne, not_found); // The cache did not contain this value.
7612 __ b(&load_result_from_cache);
7613 } else {
7614 __ b(not_found);
7615 }
7616 }
7617
7618 __ bind(&is_smi);
7619 Register scratch = scratch1;
7620 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007621 // Calculate address of entry in string cache: each entry consists
7622 // of two pointer sized fields.
7623 __ add(scratch,
7624 number_string_cache,
7625 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7626
7627 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007628 Register probe = mask;
7629 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7630 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007631 __ b(ne, not_found);
7632
7633 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007634 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007635 __ ldr(result,
7636 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007637 __ IncrementCounter(&Counters::number_to_string_native,
7638 1,
7639 scratch1,
7640 scratch2);
7641}
7642
7643
7644void NumberToStringStub::Generate(MacroAssembler* masm) {
7645 Label runtime;
7646
7647 __ ldr(r1, MemOperand(sp, 0));
7648
7649 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007650 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007651 __ add(sp, sp, Operand(1 * kPointerSize));
7652 __ Ret();
7653
7654 __ bind(&runtime);
7655 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007656 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007657}
7658
7659
ager@chromium.orgac091b72010-05-05 07:34:42 +00007660void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007661 __ add(offset_, object_, Operand(offset_));
7662 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007663 __ Ret();
7664}
7665
7666
ager@chromium.orgb5737492010-07-15 09:29:43 +00007667// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007668// On exit r0 is 0, positive or negative to indicate the result of
7669// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007670void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007671 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7672 (lhs_.is(r1) && rhs_.is(r0)));
7673
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007674 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007675 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007676
7677 // NOTICE! This code is only reached after a smi-fast-case check, so
7678 // it is certain that at least one operand isn't a smi.
7679
7680 // Handle the case where the objects are identical. Either returns the answer
7681 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007682 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007683
7684 // If either is a Smi (we know that not both are), then they can only
7685 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007686 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007687 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007688 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007689 __ tst(r2, Operand(kSmiTagMask));
7690 __ b(ne, &not_smis);
7691 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7692 // 1) Return the answer.
7693 // 2) Go to slow.
7694 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007695 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007696 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007697 // comparison. If VFP3 is supported the double values of the numbers have
7698 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7699 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007700 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007701
7702 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007703 // The arguments have been converted to doubles and stored in d6 and d7, if
7704 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007705 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007706 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007707 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007708 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007709 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007710 __ vcmp(d7, d6);
7711 __ vmrs(pc); // Move vector status bits to normal status bits.
7712 Label nan;
7713 __ b(vs, &nan);
7714 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7715 __ mov(r0, Operand(LESS), LeaveCC, lt);
7716 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007717 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007718
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007719 __ bind(&nan);
7720 // If one of the sides was a NaN then the v flag is set. Load r0 with
7721 // whatever it takes to make the comparison fail, since comparisons with NaN
7722 // always fail.
7723 if (cc_ == lt || cc_ == le) {
7724 __ mov(r0, Operand(GREATER));
7725 } else {
7726 __ mov(r0, Operand(LESS));
7727 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007728 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007729 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007730 // Checks for NaN in the doubles we have loaded. Can return the answer or
7731 // fall through if neither is a NaN. Also binds lhs_not_nan.
7732 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007733 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7734 // answer. Never falls through.
7735 EmitTwoNonNanDoubleComparison(masm, cc_);
7736 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007737
7738 __ bind(&not_smis);
7739 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007740 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007741 if (strict_) {
7742 // This returns non-equal for some object types, or falls through if it
7743 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007744 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007745 }
7746
7747 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007748 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007749 // Check for heap-number-heap-number comparison. Can jump to slow case,
7750 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7751 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007752 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007753 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007754 lhs_,
7755 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007756 &both_loaded_as_doubles,
7757 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007758 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007759
7760 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007761 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7762 // symbols.
7763 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007764 // Returns an answer for two symbols or two detectable objects.
7765 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007766 // Assumes that r2 is the type of rhs_ on entry.
7767 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007768 }
7769
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007770 // Check for both being sequential ASCII strings, and inline if that is the
7771 // case.
7772 __ bind(&flat_string_check);
7773
ager@chromium.orgb5737492010-07-15 09:29:43 +00007774 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007775
7776 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7777 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007778 lhs_,
7779 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007780 r2,
7781 r3,
7782 r4,
7783 r5);
7784 // Never falls through to here.
7785
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007786 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007787
ager@chromium.orgb5737492010-07-15 09:29:43 +00007788 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007789 // Figure out which native to call and setup the arguments.
7790 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007791 if (cc_ == eq) {
7792 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7793 } else {
7794 native = Builtins::COMPARE;
7795 int ncr; // NaN compare result
7796 if (cc_ == lt || cc_ == le) {
7797 ncr = GREATER;
7798 } else {
7799 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7800 ncr = LESS;
7801 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007802 __ mov(r0, Operand(Smi::FromInt(ncr)));
7803 __ push(r0);
7804 }
7805
7806 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7807 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007808 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007809}
7810
7811
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00007812// We fall into this code if the operands were Smis, but the result was
7813// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007814// the operands were not both Smi. The operands are in r0 and r1. In order
7815// to call the C-implemented binary fp operation routines we need to end up
7816// with the double precision floating point operands in r0 and r1 (for the
7817// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00007818void GenericBinaryOpStub::HandleBinaryOpSlowCases(
7819 MacroAssembler* masm,
7820 Label* not_smi,
7821 Register lhs,
7822 Register rhs,
7823 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007824 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00007825 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007826
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007827 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007828 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00007829
7830 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007831 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7832
ager@chromium.org357bf652010-04-12 11:30:10 +00007833 // Smi-smi case (overflow).
7834 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007835 // The new heap number is in r5. r3 and r7 are scratch.
7836 __ AllocateHeapNumber(
7837 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007838
7839 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
7840 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007841 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007842 CpuFeatures::Scope scope(VFP3);
7843 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
7844 __ vmov(s15, r7);
7845 __ vcvt_f64_s32(d7, s15);
7846 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
7847 __ vmov(s13, r7);
7848 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007849 if (!use_fp_registers) {
7850 __ vmov(r2, r3, d7);
7851 __ vmov(r0, r1, d6);
7852 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007853 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007854 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007855 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007856 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007857 __ push(lr);
7858 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007859 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007860 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007861 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007862 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
7863 __ pop(lr);
7864 }
7865 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007866 }
7867
ager@chromium.org357bf652010-04-12 11:30:10 +00007868 // We branch here if at least one of r0 and r1 is not a Smi.
7869 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007870 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007871
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007872 // After this point we have the left hand side in r1 and the right hand side
7873 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007874 if (lhs.is(r0)) {
7875 __ Swap(r0, r1, ip);
7876 }
7877
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007878 // The type transition also calculates the answer.
7879 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007880
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007881 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007882 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
7883 switch (op_) {
7884 case Token::ADD:
7885 case Token::SUB:
7886 case Token::MUL:
7887 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007888 GenerateTypeTransition(masm); // Tail call.
7889 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00007890 break;
7891
7892 default:
7893 break;
7894 }
7895 }
7896
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007897 if (generate_code_to_calculate_answer) {
7898 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
7899 if (mode_ == NO_OVERWRITE) {
7900 // In the case where there is no chance of an overwritable float we may
7901 // as well do the allocation immediately while r0 and r1 are untouched.
7902 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007903 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007904
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007905 // Move r0 to a double in r2-r3.
7906 __ tst(r0, Operand(kSmiTagMask));
7907 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
7908 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7909 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7910 __ cmp(r4, heap_number_map);
7911 __ b(ne, &slow);
7912 if (mode_ == OVERWRITE_RIGHT) {
7913 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007914 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007915 if (use_fp_registers) {
7916 CpuFeatures::Scope scope(VFP3);
7917 // Load the double from tagged HeapNumber r0 to d7.
7918 __ sub(r7, r0, Operand(kHeapObjectTag));
7919 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00007920 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007921 // Calling convention says that second double is in r2 and r3.
7922 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007923 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007924 __ jmp(&finished_loading_r0);
7925 __ bind(&r0_is_smi);
7926 if (mode_ == OVERWRITE_RIGHT) {
7927 // We can't overwrite a Smi so get address of new heap number into r5.
7928 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7929 }
ager@chromium.org357bf652010-04-12 11:30:10 +00007930
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00007931 if (CpuFeatures::IsSupported(VFP3)) {
7932 CpuFeatures::Scope scope(VFP3);
7933 // Convert smi in r0 to double in d7.
7934 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
7935 __ vmov(s15, r7);
7936 __ vcvt_f64_s32(d7, s15);
7937 if (!use_fp_registers) {
7938 __ vmov(r2, r3, d7);
7939 }
7940 } else {
7941 // Write Smi from r0 to r3 and r2 in double format.
7942 __ mov(r7, Operand(r0));
7943 ConvertToDoubleStub stub3(r3, r2, r7, r4);
7944 __ push(lr);
7945 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
7946 __ pop(lr);
7947 }
7948
7949 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
7950 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
7951 Label r1_is_not_smi;
7952 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
7953 __ tst(r1, Operand(kSmiTagMask));
7954 __ b(ne, &r1_is_not_smi);
7955 GenerateTypeTransition(masm); // Tail call.
7956 }
7957
7958 __ bind(&finished_loading_r0);
7959
7960 // Move r1 to a double in r0-r1.
7961 __ tst(r1, Operand(kSmiTagMask));
7962 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
7963 __ bind(&r1_is_not_smi);
7964 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
7965 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7966 __ cmp(r4, heap_number_map);
7967 __ b(ne, &slow);
7968 if (mode_ == OVERWRITE_LEFT) {
7969 __ mov(r5, Operand(r1)); // Overwrite this heap number.
7970 }
7971 if (use_fp_registers) {
7972 CpuFeatures::Scope scope(VFP3);
7973 // Load the double from tagged HeapNumber r1 to d6.
7974 __ sub(r7, r1, Operand(kHeapObjectTag));
7975 __ vldr(d6, r7, HeapNumber::kValueOffset);
7976 } else {
7977 // Calling convention says that first double is in r0 and r1.
7978 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
7979 }
7980 __ jmp(&finished_loading_r1);
7981 __ bind(&r1_is_smi);
7982 if (mode_ == OVERWRITE_LEFT) {
7983 // We can't overwrite a Smi so get address of new heap number into r5.
7984 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
7985 }
7986
7987 if (CpuFeatures::IsSupported(VFP3)) {
7988 CpuFeatures::Scope scope(VFP3);
7989 // Convert smi in r1 to double in d6.
7990 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
7991 __ vmov(s13, r7);
7992 __ vcvt_f64_s32(d6, s13);
7993 if (!use_fp_registers) {
7994 __ vmov(r0, r1, d6);
7995 }
7996 } else {
7997 // Write Smi from r1 to r1 and r0 in double format.
7998 __ mov(r7, Operand(r1));
7999 ConvertToDoubleStub stub4(r1, r0, r7, r9);
8000 __ push(lr);
8001 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
8002 __ pop(lr);
8003 }
8004
8005 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00008006 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008007
8008 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
8009 __ bind(&do_the_call);
8010 // If we are inlining the operation using VFP3 instructions for
8011 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
8012 if (use_fp_registers) {
8013 CpuFeatures::Scope scope(VFP3);
8014 // ARMv7 VFP3 instructions to implement
8015 // double precision, add, subtract, multiply, divide.
8016
8017 if (Token::MUL == op_) {
8018 __ vmul(d5, d6, d7);
8019 } else if (Token::DIV == op_) {
8020 __ vdiv(d5, d6, d7);
8021 } else if (Token::ADD == op_) {
8022 __ vadd(d5, d6, d7);
8023 } else if (Token::SUB == op_) {
8024 __ vsub(d5, d6, d7);
8025 } else {
8026 UNREACHABLE();
8027 }
8028 __ sub(r0, r5, Operand(kHeapObjectTag));
8029 __ vstr(d5, r0, HeapNumber::kValueOffset);
8030 __ add(r0, r0, Operand(kHeapObjectTag));
8031 __ mov(pc, lr);
8032 } else {
8033 // If we did not inline the operation, then the arguments are in:
8034 // r0: Left value (least significant part of mantissa).
8035 // r1: Left value (sign, exponent, top of mantissa).
8036 // r2: Right value (least significant part of mantissa).
8037 // r3: Right value (sign, exponent, top of mantissa).
8038 // r5: Address of heap number for result.
8039
8040 __ push(lr); // For later.
8041 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
8042 // Call C routine that may not cause GC or other trouble. r5 is callee
8043 // save.
8044 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
8045 // Store answer in the overwritable heap number.
8046 #if !defined(USE_ARM_EABI)
8047 // Double returned in fp coprocessor register 0 and 1, encoded as
8048 // register cr8. Offsets must be divisible by 4 for coprocessor so we
8049 // need to substract the tag from r5.
8050 __ sub(r4, r5, Operand(kHeapObjectTag));
8051 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
8052 #else
8053 // Double returned in registers 0 and 1.
8054 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8055 #endif
8056 __ mov(r0, Operand(r5));
8057 // And we are done.
8058 __ pop(pc);
8059 }
8060 }
8061 }
8062
8063 if (!generate_code_to_calculate_answer &&
8064 !slow_reverse.is_linked() &&
8065 !slow.is_linked()) {
8066 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008067 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008068
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008069 if (lhs.is(r0)) {
8070 __ b(&slow);
8071 __ bind(&slow_reverse);
8072 __ Swap(r0, r1, ip);
8073 }
8074
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008075 heap_number_map = no_reg; // Don't use this any more from here on.
8076
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008077 // We jump to here if something goes wrong (one param is not a number of any
8078 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008079 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008080
8081 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008082 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008083
ager@chromium.org357bf652010-04-12 11:30:10 +00008084 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008085 // Test for string arguments before calling runtime.
8086 // r1 : first argument
8087 // r0 : second argument
8088 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008089 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008090
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008091 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008092 __ tst(r1, Operand(kSmiTagMask));
8093 __ b(eq, &not_string1);
8094 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8095 __ b(ge, &not_string1);
8096
8097 // First argument is a a string, test second.
8098 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008099 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008100 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8101 __ b(ge, &string1);
8102
8103 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008104 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8105 __ TailCallStub(&string_add_stub);
8106
8107 __ bind(&string1_smi2);
8108 // First argument is a string, second is a smi. Try to lookup the number
8109 // string for the smi in the number string cache.
8110 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008111 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008112
8113 // Replace second argument on stack and tailcall string add stub to make
8114 // the result.
8115 __ str(r2, MemOperand(sp, 0));
8116 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008117
8118 // Only first argument is a string.
8119 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008120 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8121
8122 // First argument was not a string, test second.
8123 __ bind(&not_string1);
8124 __ tst(r0, Operand(kSmiTagMask));
8125 __ b(eq, &not_strings);
8126 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8127 __ b(ge, &not_strings);
8128
8129 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008130 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8131
8132 __ bind(&not_strings);
8133 }
8134
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008135 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008136}
8137
8138
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008139// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008140// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008141// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8142// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008143// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8144// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008145static void GetInt32(MacroAssembler* masm,
8146 Register source,
8147 Register dest,
8148 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008149 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008150 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008151 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008152 // Get exponent word.
8153 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8154 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008155 __ Ubfx(scratch2,
8156 scratch,
8157 HeapNumber::kExponentShift,
8158 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008159 // Load dest with zero. We use this either for the final shift or
8160 // for the answer.
8161 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008162 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008163 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8164 // the exponent that we are fastest at and also the highest exponent we can
8165 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008166 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8167 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8168 // split it up to avoid a constant pool entry. You can't do that in general
8169 // for cmp because of the overflow flag, but we know the exponent is in the
8170 // range 0-2047 so there is no overflow.
8171 int fudge_factor = 0x400;
8172 __ sub(scratch2, scratch2, Operand(fudge_factor));
8173 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008174 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8175 __ b(eq, &right_exponent);
8176 // If the exponent is higher than that then go to slow case. This catches
8177 // numbers that don't fit in a signed int32, infinities and NaNs.
8178 __ b(gt, slow);
8179
8180 // We know the exponent is smaller than 30 (biased). If it is less than
8181 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8182 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008183 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8184 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008185 // Dest already has a Smi zero.
8186 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008187 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008188 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8189 // get how much to shift down.
8190 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008191 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008192 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008193 if (CpuFeatures::IsSupported(VFP3)) {
8194 CpuFeatures::Scope scope(VFP3);
8195 // ARMv7 VFP3 instructions implementing double precision to integer
8196 // conversion using round to zero.
8197 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008198 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008199 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008200 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008201 } else {
8202 // Get the top bits of the mantissa.
8203 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8204 // Put back the implicit 1.
8205 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8206 // Shift up the mantissa bits to take up the space the exponent used to
8207 // take. We just orred in the implicit bit so that took care of one and
8208 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8209 // distance.
8210 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8211 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8212 // Put sign in zero flag.
8213 __ tst(scratch, Operand(HeapNumber::kSignMask));
8214 // Get the second half of the double. For some exponents we don't
8215 // actually need this because the bits get shifted out again, but
8216 // it's probably slower to test than just to do it.
8217 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8218 // Shift down 22 bits to get the last 10 bits.
8219 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8220 // Move down according to the exponent.
8221 __ mov(dest, Operand(scratch, LSR, dest));
8222 // Fix sign if sign bit was set.
8223 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8224 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008225 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008226}
8227
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008228// For bitwise ops where the inputs are not both Smis we here try to determine
8229// whether both inputs are either Smis or at least heap numbers that can be
8230// represented by a 32 bit signed value. We truncate towards zero as required
8231// by the ES spec. If this is the case we do the bitwise op and see if the
8232// result is a Smi. If so, great, otherwise we try to find a heap number to
8233// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008234// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8235void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8236 Register lhs,
8237 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008238 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008239 Label rhs_is_smi, lhs_is_smi;
8240 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008241
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008242 Register heap_number_map = r6;
8243 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8244
ager@chromium.org357bf652010-04-12 11:30:10 +00008245 __ tst(lhs, Operand(kSmiTagMask));
8246 __ 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 +00008247 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8248 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008249 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008250 GetInt32(masm, lhs, r3, r5, r4, &slow);
8251 __ jmp(&done_checking_lhs);
8252 __ bind(&lhs_is_smi);
8253 __ mov(r3, Operand(lhs, ASR, 1));
8254 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008255
ager@chromium.org357bf652010-04-12 11:30:10 +00008256 __ tst(rhs, Operand(kSmiTagMask));
8257 __ 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 +00008258 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8259 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008260 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008261 GetInt32(masm, rhs, r2, r5, r4, &slow);
8262 __ jmp(&done_checking_rhs);
8263 __ bind(&rhs_is_smi);
8264 __ mov(r2, Operand(rhs, ASR, 1));
8265 __ bind(&done_checking_rhs);
8266
8267 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008268
8269 // r0 and r1: Original operands (Smi or heap numbers).
8270 // r2 and r3: Signed int32 operands.
8271 switch (op_) {
8272 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8273 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8274 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8275 case Token::SAR:
8276 // Use only the 5 least significant bits of the shift count.
8277 __ and_(r2, r2, Operand(0x1f));
8278 __ mov(r2, Operand(r3, ASR, r2));
8279 break;
8280 case Token::SHR:
8281 // Use only the 5 least significant bits of the shift count.
8282 __ and_(r2, r2, Operand(0x1f));
8283 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8284 // SHR is special because it is required to produce a positive answer.
8285 // The code below for writing into heap numbers isn't capable of writing
8286 // the register as an unsigned int so we go to slow case if we hit this
8287 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008288 if (CpuFeatures::IsSupported(VFP3)) {
8289 __ b(mi, &result_not_a_smi);
8290 } else {
8291 __ b(mi, &slow);
8292 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008293 break;
8294 case Token::SHL:
8295 // Use only the 5 least significant bits of the shift count.
8296 __ and_(r2, r2, Operand(0x1f));
8297 __ mov(r2, Operand(r3, LSL, r2));
8298 break;
8299 default: UNREACHABLE();
8300 }
8301 // check that the *signed* result fits in a smi
8302 __ add(r3, r2, Operand(0x40000000), SetCC);
8303 __ b(mi, &result_not_a_smi);
8304 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8305 __ Ret();
8306
8307 Label have_to_allocate, got_a_heap_number;
8308 __ bind(&result_not_a_smi);
8309 switch (mode_) {
8310 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008311 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008312 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008313 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008314 break;
8315 }
8316 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008317 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008318 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008319 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008320 break;
8321 }
8322 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008323 // Get a new heap number in r5. r4 and r7 are scratch.
8324 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008325 }
8326 default: break;
8327 }
8328 __ bind(&got_a_heap_number);
8329 // r2: Answer as signed int32.
8330 // r5: Heap number to write answer into.
8331
8332 // Nothing can go wrong now, so move the heap number to r0, which is the
8333 // result.
8334 __ mov(r0, Operand(r5));
8335
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008336 if (CpuFeatures::IsSupported(VFP3)) {
8337 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8338 CpuFeatures::Scope scope(VFP3);
8339 __ vmov(s0, r2);
8340 if (op_ == Token::SHR) {
8341 __ vcvt_f64_u32(d0, s0);
8342 } else {
8343 __ vcvt_f64_s32(d0, s0);
8344 }
8345 __ sub(r3, r0, Operand(kHeapObjectTag));
8346 __ vstr(d0, r3, HeapNumber::kValueOffset);
8347 __ Ret();
8348 } else {
8349 // Tail call that writes the int32 in r2 to the heap number in r0, using
8350 // r3 as scratch. r0 is preserved and returned.
8351 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8352 __ TailCallStub(&stub);
8353 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008354
8355 if (mode_ != NO_OVERWRITE) {
8356 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008357 // Get a new heap number in r5. r4 and r7 are scratch.
8358 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008359 __ jmp(&got_a_heap_number);
8360 }
8361
8362 // If all else failed then we go to the runtime system.
8363 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008364 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008365 switch (op_) {
8366 case Token::BIT_OR:
8367 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8368 break;
8369 case Token::BIT_AND:
8370 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8371 break;
8372 case Token::BIT_XOR:
8373 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8374 break;
8375 case Token::SAR:
8376 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8377 break;
8378 case Token::SHR:
8379 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8380 break;
8381 case Token::SHL:
8382 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8383 break;
8384 default:
8385 UNREACHABLE();
8386 }
8387}
8388
8389
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008390// Can we multiply by x with max two shifts and an add.
8391// This answers yes to all integers from 2 to 10.
8392static bool IsEasyToMultiplyBy(int x) {
8393 if (x < 2) return false; // Avoid special cases.
8394 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8395 if (IsPowerOf2(x)) return true; // Simple shift.
8396 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8397 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8398 return false;
8399}
8400
8401
8402// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8403// Source and destination may be the same register. This routine does
8404// not set carry and overflow the way a mul instruction would.
8405static void MultiplyByKnownInt(MacroAssembler* masm,
8406 Register source,
8407 Register destination,
8408 int known_int) {
8409 if (IsPowerOf2(known_int)) {
8410 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8411 } else if (PopCountLessThanEqual2(known_int)) {
8412 int first_bit = BitPosition(known_int);
8413 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8414 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8415 if (first_bit != 0) {
8416 __ mov(destination, Operand(destination, LSL, first_bit));
8417 }
8418 } else {
8419 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8420 int the_bit = BitPosition(known_int + 1);
8421 __ rsb(destination, source, Operand(source, LSL, the_bit));
8422 }
8423}
8424
8425
8426// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8427// a register for the cases where it doesn't know a good trick, and may deliver
8428// a result that needs shifting.
8429static void MultiplyByKnownInt2(
8430 MacroAssembler* masm,
8431 Register result,
8432 Register source,
8433 Register known_int_register, // Smi tagged.
8434 int known_int,
8435 int* required_shift) { // Including Smi tag shift
8436 switch (known_int) {
8437 case 3:
8438 __ add(result, source, Operand(source, LSL, 1));
8439 *required_shift = 1;
8440 break;
8441 case 5:
8442 __ add(result, source, Operand(source, LSL, 2));
8443 *required_shift = 1;
8444 break;
8445 case 6:
8446 __ add(result, source, Operand(source, LSL, 1));
8447 *required_shift = 2;
8448 break;
8449 case 7:
8450 __ rsb(result, source, Operand(source, LSL, 3));
8451 *required_shift = 1;
8452 break;
8453 case 9:
8454 __ add(result, source, Operand(source, LSL, 3));
8455 *required_shift = 1;
8456 break;
8457 case 10:
8458 __ add(result, source, Operand(source, LSL, 2));
8459 *required_shift = 2;
8460 break;
8461 default:
8462 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8463 __ mul(result, source, known_int_register);
8464 *required_shift = 0;
8465 }
8466}
8467
8468
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008469// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8470// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8471// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8472// number from 0 to mask. On exit the 'eq' condition flags are set if the
8473// answer is exactly the mask.
8474void IntegerModStub::DigitSum(MacroAssembler* masm,
8475 Register lhs,
8476 int mask,
8477 int shift,
8478 Label* entry) {
8479 ASSERT(mask > 0);
8480 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8481 Label loop;
8482 __ bind(&loop);
8483 __ and_(ip, lhs, Operand(mask));
8484 __ add(lhs, ip, Operand(lhs, LSR, shift));
8485 __ bind(entry);
8486 __ cmp(lhs, Operand(mask));
8487 __ b(gt, &loop);
8488}
8489
8490
8491void IntegerModStub::DigitSum(MacroAssembler* masm,
8492 Register lhs,
8493 Register scratch,
8494 int mask,
8495 int shift1,
8496 int shift2,
8497 Label* entry) {
8498 ASSERT(mask > 0);
8499 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8500 Label loop;
8501 __ bind(&loop);
8502 __ bic(scratch, lhs, Operand(mask));
8503 __ and_(ip, lhs, Operand(mask));
8504 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8505 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8506 __ bind(entry);
8507 __ cmp(lhs, Operand(mask));
8508 __ b(gt, &loop);
8509}
8510
8511
8512// Splits the number into two halves (bottom half has shift bits). The top
8513// half is subtracted from the bottom half. If the result is negative then
8514// rhs is added.
8515void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8516 Register lhs,
8517 int shift,
8518 int rhs) {
8519 int mask = (1 << shift) - 1;
8520 __ and_(ip, lhs, Operand(mask));
8521 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8522 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8523}
8524
8525
8526void IntegerModStub::ModReduce(MacroAssembler* masm,
8527 Register lhs,
8528 int max,
8529 int denominator) {
8530 int limit = denominator;
8531 while (limit * 2 <= max) limit *= 2;
8532 while (limit >= denominator) {
8533 __ cmp(lhs, Operand(limit));
8534 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8535 limit >>= 1;
8536 }
8537}
8538
8539
8540void IntegerModStub::ModAnswer(MacroAssembler* masm,
8541 Register result,
8542 Register shift_distance,
8543 Register mask_bits,
8544 Register sum_of_digits) {
8545 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8546 __ Ret();
8547}
8548
8549
8550// See comment for class.
8551void IntegerModStub::Generate(MacroAssembler* masm) {
8552 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8553 __ bic(odd_number_, odd_number_, Operand(1));
8554 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8555 // We now have (odd_number_ - 1) * 2 in the register.
8556 // Build a switch out of branches instead of data because it avoids
8557 // having to teach the assembler about intra-code-object pointers
8558 // that are not in relative branch instructions.
8559 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8560 Label mod21, mod23, mod25;
8561 { Assembler::BlockConstPoolScope block_const_pool(masm);
8562 __ add(pc, pc, Operand(odd_number_));
8563 // When you read pc it is always 8 ahead, but when you write it you always
8564 // write the actual value. So we put in two nops to take up the slack.
8565 __ nop();
8566 __ nop();
8567 __ b(&mod3);
8568 __ b(&mod5);
8569 __ b(&mod7);
8570 __ b(&mod9);
8571 __ b(&mod11);
8572 __ b(&mod13);
8573 __ b(&mod15);
8574 __ b(&mod17);
8575 __ b(&mod19);
8576 __ b(&mod21);
8577 __ b(&mod23);
8578 __ b(&mod25);
8579 }
8580
8581 // For each denominator we find a multiple that is almost only ones
8582 // when expressed in binary. Then we do the sum-of-digits trick for
8583 // that number. If the multiple is not 1 then we have to do a little
8584 // more work afterwards to get the answer into the 0-denominator-1
8585 // range.
8586 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8587 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8588 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8589
8590 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8591 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8592 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8593
8594 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8595 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8596 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8597
8598 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8599 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8600 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8601
8602 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8603 ModReduce(masm, lhs_, 0x3f, 11);
8604 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8605
8606 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8607 ModReduce(masm, lhs_, 0xff, 13);
8608 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8609
8610 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8611 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8612 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8613
8614 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8615 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8616 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8617
8618 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8619 ModReduce(masm, lhs_, 0xff, 19);
8620 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8621
8622 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8623 ModReduce(masm, lhs_, 0x3f, 21);
8624 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8625
8626 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8627 ModReduce(masm, lhs_, 0xff, 23);
8628 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8629
8630 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8631 ModReduce(masm, lhs_, 0x7f, 25);
8632 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8633}
8634
8635
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008636const char* GenericBinaryOpStub::GetName() {
8637 if (name_ != NULL) return name_;
8638 const int len = 100;
8639 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8640 if (name_ == NULL) return "OOM";
8641 const char* op_name = Token::Name(op_);
8642 const char* overwrite_name;
8643 switch (mode_) {
8644 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8645 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8646 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8647 default: overwrite_name = "UnknownOverwrite"; break;
8648 }
8649
8650 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008651 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008652 op_name,
8653 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008654 specialized_on_rhs_ ? "_ConstantRhs" : "",
8655 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008656 return name_;
8657}
8658
8659
ager@chromium.org5c838252010-02-19 08:53:10 +00008660
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008661void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008662 // lhs_ : x
8663 // rhs_ : y
8664 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008665
ager@chromium.org357bf652010-04-12 11:30:10 +00008666 Register result = r0;
8667 Register lhs = lhs_;
8668 Register rhs = rhs_;
8669
8670 // This code can't cope with other register allocations yet.
8671 ASSERT(result.is(r0) &&
8672 ((lhs.is(r0) && rhs.is(r1)) ||
8673 (lhs.is(r1) && rhs.is(r0))));
8674
8675 Register smi_test_reg = VirtualFrame::scratch0();
8676 Register scratch = VirtualFrame::scratch1();
8677
8678 // All ops need to know whether we are dealing with two Smis. Set up
8679 // smi_test_reg to tell us that.
8680 if (ShouldGenerateSmiCode()) {
8681 __ orr(smi_test_reg, lhs, Operand(rhs));
8682 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008683
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008684 switch (op_) {
8685 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008686 Label not_smi;
8687 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008688 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008689 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008690 __ tst(smi_test_reg, Operand(kSmiTagMask));
8691 __ b(ne, &not_smi);
8692 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8693 // Return if no overflow.
8694 __ Ret(vc);
8695 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8696 }
8697 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008698 break;
8699 }
8700
8701 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008702 Label not_smi;
8703 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008704 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008705 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008706 __ tst(smi_test_reg, Operand(kSmiTagMask));
8707 __ b(ne, &not_smi);
8708 if (lhs.is(r1)) {
8709 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8710 // Return if no overflow.
8711 __ Ret(vc);
8712 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8713 } else {
8714 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8715 // Return if no overflow.
8716 __ Ret(vc);
8717 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8718 }
8719 }
8720 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008721 break;
8722 }
8723
8724 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008725 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008726 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008727 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008728 __ tst(smi_test_reg, Operand(kSmiTagMask));
8729 Register scratch2 = smi_test_reg;
8730 smi_test_reg = no_reg;
8731 __ b(ne, &not_smi);
8732 // Remove tag from one operand (but keep sign), so that result is Smi.
8733 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8734 // Do multiplication
8735 // scratch = lower 32 bits of ip * lhs.
8736 __ smull(scratch, scratch2, lhs, ip);
8737 // Go slow on overflows (overflow bit is not set).
8738 __ mov(ip, Operand(scratch, ASR, 31));
8739 // No overflow if higher 33 bits are identical.
8740 __ cmp(ip, Operand(scratch2));
8741 __ b(ne, &slow);
8742 // Go slow on zero result to handle -0.
8743 __ tst(scratch, Operand(scratch));
8744 __ mov(result, Operand(scratch), LeaveCC, ne);
8745 __ Ret(ne);
8746 // We need -0 if we were multiplying a negative number with 0 to get 0.
8747 // We know one of them was zero.
8748 __ add(scratch2, rhs, Operand(lhs), SetCC);
8749 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
8750 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
8751 // Slow case. We fall through here if we multiplied a negative number
8752 // with 0, because that would mean we should produce -0.
8753 __ bind(&slow);
8754 }
8755 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008756 break;
8757 }
8758
8759 case Token::DIV:
8760 case Token::MOD: {
8761 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008762 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008763 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00008764 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008765 if (IsPowerOf2(constant_rhs_)) {
8766 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008767 __ and_(rhs,
8768 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008769 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
8770 SetCC);
8771 // We now have the answer, but if the input was negative we also
8772 // have the sign bit. Our work is done if the result is
8773 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00008774 if (!rhs.is(r0)) {
8775 __ mov(r0, rhs, LeaveCC, pl);
8776 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008777 __ Ret(pl);
8778 // A mod of a negative left hand side must return a negative number.
8779 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00008780 // already optimistically trashed rhs so we may need to restore it.
8781 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008782 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008783 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008784 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008785 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00008786 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008787 } else {
8788 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00008789 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008790 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008791 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008792 int shift = 0;
8793 int d = constant_rhs_;
8794 while ((d & 1) == 0) {
8795 d >>= 1;
8796 shift++;
8797 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008798 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008799 __ bic(r0, r0, Operand(kSmiTagMask));
8800 }
8801 } else {
8802 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00008803 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008804 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008805 // Find a fixed point reciprocal of the divisor so we can divide by
8806 // multiplying.
8807 double divisor = 1.0 / constant_rhs_;
8808 int shift = 32;
8809 double scale = 4294967296.0; // 1 << 32.
8810 uint32_t mul;
8811 // Maximise the precision of the fixed point reciprocal.
8812 while (true) {
8813 mul = static_cast<uint32_t>(scale * divisor);
8814 if (mul >= 0x7fffffff) break;
8815 scale *= 2.0;
8816 shift++;
8817 }
8818 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00008819 Register scratch2 = smi_test_reg;
8820 smi_test_reg = no_reg;
8821 __ mov(scratch2, Operand(mul));
8822 __ umull(scratch, scratch2, scratch2, lhs);
8823 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
8824 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
8825 // rhs is still the known rhs. rhs is Smi tagged.
8826 // lhs is still the unkown lhs. lhs is Smi tagged.
8827 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
8828 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008829 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00008830 scratch,
8831 scratch2,
8832 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008833 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00008834 &required_scratch_shift);
8835 // scratch << required_scratch_shift is now the Smi tagged rhs *
8836 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008837 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008838 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008839 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00008840 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008841 } else {
8842 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00008843 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008844 }
8845 }
8846 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008847 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008848 } else if (op_ == Token::MOD &&
8849 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
8850 runtime_operands_type_ != BinaryOpIC::STRINGS) {
8851 // Do generate a bit of smi code for modulus even though the default for
8852 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008853 // support for modulus checking for smis makes sense. We can handle
8854 // 1 to 25 times any power of 2. This covers over half the numbers from
8855 // 1 to 100 including all of the first 25. (Actually the constants < 10
8856 // are handled above by reciprocal multiplication. We only get here for
8857 // those cases if the right hand side is not a constant or for cases
8858 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
8859 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008860 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008861 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008862 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008863 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008864 // Check for two positive smis.
8865 __ orr(smi_test_reg, lhs, Operand(rhs));
8866 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
8867 __ b(ne, &slow);
8868 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008869 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008870 __ sub(scratch, rhs, Operand(1), SetCC);
8871 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008872 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
8873 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008874 // Calculate power of two modulus.
8875 __ and_(result, lhs, Operand(scratch));
8876 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008877
8878 __ bind(&not_power_of_2);
8879 __ eor(scratch, scratch, Operand(mask_bits));
8880 // At least two bits are set in the modulus. The high one(s) are in
8881 // mask_bits and the low one is scratch + 1.
8882 __ and_(mask_bits, scratch, Operand(lhs));
8883 Register shift_distance = scratch;
8884 scratch = no_reg;
8885
8886 // The rhs consists of a power of 2 multiplied by some odd number.
8887 // The power-of-2 part we handle by putting the corresponding bits
8888 // from the lhs in the mask_bits register, and the power in the
8889 // shift_distance register. Shift distance is never 0 due to Smi
8890 // tagging.
8891 __ CountLeadingZeros(r4, shift_distance, shift_distance);
8892 __ rsb(shift_distance, r4, Operand(32));
8893
8894 // Now we need to find out what the odd number is. The last bit is
8895 // always 1.
8896 Register odd_number = r4;
8897 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
8898 __ cmp(odd_number, Operand(25));
8899 __ b(gt, &slow);
8900
8901 IntegerModStub stub(
8902 result, shift_distance, odd_number, mask_bits, lhs, r5);
8903 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
8904
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008905 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008906 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008907 HandleBinaryOpSlowCases(
8908 masm,
8909 &not_smi,
8910 lhs,
8911 rhs,
8912 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008913 break;
8914 }
8915
8916 case Token::BIT_OR:
8917 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008918 case Token::BIT_XOR:
8919 case Token::SAR:
8920 case Token::SHR:
8921 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008922 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008923 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008924 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008925 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008926 Register scratch2 = smi_test_reg;
8927 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008928 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008929 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
8930 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
8931 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008932 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008933 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00008934 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8935 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008936 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008937 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008938 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008939 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008940 // Remove tags from operands. We can't do this on a 31 bit number
8941 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00008942 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8943 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8944 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008945 // Unsigned shift is not allowed to produce a negative number, so
8946 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00008947 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008948 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008949 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008950 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008951 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008952 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008953 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00008954 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8955 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8956 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008957 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00008958 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008959 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008960 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008961 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008962 default: UNREACHABLE();
8963 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008964 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008965 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008966 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008967 break;
8968 }
8969
8970 default: UNREACHABLE();
8971 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008972 // This code should be unreachable.
8973 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00008974
8975 // Generate an unreachable reference to the DEFAULT stub so that it can be
8976 // found at the end of this stub when clearing ICs at GC.
8977 // TODO(kaznacheev): Check performance impact and get rid of this.
8978 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
8979 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
8980 __ CallStub(&uninit);
8981 }
8982}
8983
8984
8985void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
8986 Label get_result;
8987
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008988 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008989
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008990 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
8991 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00008992 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008993 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008994
8995 __ TailCallExternalReference(
8996 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008997 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00008998 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008999}
9000
9001
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009002Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009003 GenericBinaryOpStub stub(key, type_info);
9004 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009005}
9006
9007
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009008void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9009 // Argument is a number and is on stack and in r0.
9010 Label runtime_call;
9011 Label input_not_smi;
9012 Label loaded;
9013
9014 if (CpuFeatures::IsSupported(VFP3)) {
9015 // Load argument and check if it is a smi.
9016 __ BranchOnNotSmi(r0, &input_not_smi);
9017
9018 CpuFeatures::Scope scope(VFP3);
9019 // Input is a smi. Convert to double and load the low and high words
9020 // of the double into r2, r3.
9021 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
9022 __ b(&loaded);
9023
9024 __ bind(&input_not_smi);
9025 // Check if input is a HeapNumber.
9026 __ CheckMap(r0,
9027 r1,
9028 Heap::kHeapNumberMapRootIndex,
9029 &runtime_call,
9030 true);
9031 // Input is a HeapNumber. Load it to a double register and store the
9032 // low and high words into r2, r3.
9033 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
9034
9035 __ bind(&loaded);
9036 // r2 = low 32 bits of double value
9037 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009038 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009039 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
9040 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009041 __ eor(r1, r1, Operand(r1, ASR, 16));
9042 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009043 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009044 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009045
9046 // r2 = low 32 bits of double value.
9047 // r3 = high 32 bits of double value.
9048 // r1 = TranscendentalCache::hash(double value).
9049 __ mov(r0,
9050 Operand(ExternalReference::transcendental_cache_array_address()));
9051 // r0 points to cache array.
9052 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
9053 // r0 points to the cache for the type type_.
9054 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9055 __ cmp(r0, Operand(0));
9056 __ b(eq, &runtime_call);
9057
9058#ifdef DEBUG
9059 // Check that the layout of cache elements match expectations.
9060 { TranscendentalCache::Element test_elem[2];
9061 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9062 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9063 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9064 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9065 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9066 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9067 CHECK_EQ(0, elem_in0 - elem_start);
9068 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9069 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9070 }
9071#endif
9072
9073 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9074 __ add(r1, r1, Operand(r1, LSL, 1));
9075 __ add(r0, r0, Operand(r1, LSL, 2));
9076 // Check if cache matches: Double value is stored in uint32_t[2] array.
9077 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9078 __ cmp(r2, r4);
9079 __ b(ne, &runtime_call);
9080 __ cmp(r3, r5);
9081 __ b(ne, &runtime_call);
9082 // Cache hit. Load result, pop argument and return.
9083 __ mov(r0, Operand(r6));
9084 __ pop();
9085 __ Ret();
9086 }
9087
9088 __ bind(&runtime_call);
9089 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9090}
9091
9092
9093Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9094 switch (type_) {
9095 // Add more cases when necessary.
9096 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9097 case TranscendentalCache::COS: return Runtime::kMath_cos;
9098 default:
9099 UNIMPLEMENTED();
9100 return Runtime::kAbort;
9101 }
9102}
9103
9104
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009105void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009106 // Do tail-call to runtime routine. Runtime routines expect at least one
9107 // argument, so give it a Smi.
9108 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009109 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009110 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009111
9112 __ StubReturn(1);
9113}
9114
9115
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009116void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009117 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009118
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009119 Register heap_number_map = r6;
9120 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9121
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009122 if (op_ == Token::SUB) {
9123 // Check whether the value is a smi.
9124 Label try_float;
9125 __ tst(r0, Operand(kSmiTagMask));
9126 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009127
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009128 // Go slow case if the value of the expression is zero
9129 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009130 if (negative_zero_ == kStrictNegativeZero) {
9131 // If we have to check for zero, then we can check for the max negative
9132 // smi while we are at it.
9133 __ bic(ip, r0, Operand(0x80000000), SetCC);
9134 __ b(eq, &slow);
9135 __ rsb(r0, r0, Operand(0));
9136 __ StubReturn(1);
9137 } else {
9138 // The value of the expression is a smi and 0 is OK for -0. Try
9139 // optimistic subtraction '0 - value'.
9140 __ rsb(r0, r0, Operand(0), SetCC);
9141 __ StubReturn(1, vc);
9142 // We don't have to reverse the optimistic neg since the only case
9143 // where we fall through is the minimum negative Smi, which is the case
9144 // where the neg leaves the register unchanged.
9145 __ jmp(&slow); // Go slow on max negative Smi.
9146 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009147
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009148 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009149 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9150 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9151 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009152 __ b(ne, &slow);
9153 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009154 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009155 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9156 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9157 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9158 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009159 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009160 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9161 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9162 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9163 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9164 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9165 __ mov(r0, Operand(r1));
9166 }
9167 } else if (op_ == Token::BIT_NOT) {
9168 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009169 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9170 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9171 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009172 __ b(ne, &slow);
9173
9174 // Convert the heap number is r0 to an untagged integer in r1.
9175 GetInt32(masm, r0, r1, r2, r3, &slow);
9176
9177 // Do the bitwise operation (move negated) and check if the result
9178 // fits in a smi.
9179 Label try_float;
9180 __ mvn(r1, Operand(r1));
9181 __ add(r2, r1, Operand(0x40000000), SetCC);
9182 __ b(mi, &try_float);
9183 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9184 __ b(&done);
9185
9186 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009187 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009188 // Allocate a fresh heap number, but don't overwrite r0 until
9189 // we're sure we can do it without going through the slow case
9190 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009191 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009192 __ mov(r0, Operand(r2));
9193 }
9194
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009195 if (CpuFeatures::IsSupported(VFP3)) {
9196 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9197 CpuFeatures::Scope scope(VFP3);
9198 __ vmov(s0, r1);
9199 __ vcvt_f64_s32(d0, s0);
9200 __ sub(r2, r0, Operand(kHeapObjectTag));
9201 __ vstr(d0, r2, HeapNumber::kValueOffset);
9202 } else {
9203 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9204 // have to set up a frame.
9205 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9206 __ push(lr);
9207 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9208 __ pop(lr);
9209 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009210 } else {
9211 UNIMPLEMENTED();
9212 }
9213
9214 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009215 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009216
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009217 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009218 __ bind(&slow);
9219 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009220 switch (op_) {
9221 case Token::SUB:
9222 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9223 break;
9224 case Token::BIT_NOT:
9225 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9226 break;
9227 default:
9228 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009229 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009230}
9231
9232
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009233void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009234 // r0 holds the exception.
9235
9236 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009237 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009238
9239 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009240 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9241 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009242
9243 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009244 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009245 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009246 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009247 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009248 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9249
9250 // Before returning we restore the context from the frame pointer if
9251 // not NULL. The frame pointer is NULL in the exception handler of a
9252 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009253 __ cmp(fp, Operand(0));
9254 // Set cp to NULL if fp is NULL.
9255 __ mov(cp, Operand(0), LeaveCC, eq);
9256 // Restore cp otherwise.
9257 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009258#ifdef DEBUG
9259 if (FLAG_debug_code) {
9260 __ mov(lr, Operand(pc));
9261 }
9262#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009263 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009264 __ pop(pc);
9265}
9266
9267
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009268void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9269 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009270 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009271 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009272
9273 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009274 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009275 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009276
9277 // Unwind the handlers until the ENTRY handler is found.
9278 Label loop, done;
9279 __ bind(&loop);
9280 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009281 const int kStateOffset = StackHandlerConstants::kStateOffset;
9282 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009283 __ cmp(r2, Operand(StackHandler::ENTRY));
9284 __ b(eq, &done);
9285 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009286 const int kNextOffset = StackHandlerConstants::kNextOffset;
9287 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009288 __ jmp(&loop);
9289 __ bind(&done);
9290
9291 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009292 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009293 __ pop(r2);
9294 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009295
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009296 if (type == OUT_OF_MEMORY) {
9297 // Set external caught exception to false.
9298 ExternalReference external_caught(Top::k_external_caught_exception_address);
9299 __ mov(r0, Operand(false));
9300 __ mov(r2, Operand(external_caught));
9301 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009302
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009303 // Set pending exception and r0 to out of memory exception.
9304 Failure* out_of_memory = Failure::OutOfMemoryException();
9305 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9306 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9307 __ str(r0, MemOperand(r2));
9308 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009309
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009310 // Stack layout at this point. See also StackHandlerConstants.
9311 // sp -> state (ENTRY)
9312 // fp
9313 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009314
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009315 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009316 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009317 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
9318 // Before returning we restore the context from the frame pointer if
9319 // not NULL. The frame pointer is NULL in the exception handler of a
9320 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009321 __ cmp(fp, Operand(0));
9322 // Set cp to NULL if fp is NULL.
9323 __ mov(cp, Operand(0), LeaveCC, eq);
9324 // Restore cp otherwise.
9325 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009326#ifdef DEBUG
9327 if (FLAG_debug_code) {
9328 __ mov(lr, Operand(pc));
9329 }
9330#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009331 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009332 __ pop(pc);
9333}
9334
9335
9336void CEntryStub::GenerateCore(MacroAssembler* masm,
9337 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009338 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009339 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009340 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009341 bool always_allocate,
9342 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009343 // r0: result parameter for PerformGC, if any
9344 // r4: number of arguments including receiver (C callee-saved)
9345 // r5: pointer to builtin function (C callee-saved)
9346 // r6: pointer to the first argument (C callee-saved)
9347
9348 if (do_gc) {
9349 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009350 __ PrepareCallCFunction(1, r1);
9351 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009352 }
9353
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009354 ExternalReference scope_depth =
9355 ExternalReference::heap_always_allocate_scope_depth();
9356 if (always_allocate) {
9357 __ mov(r0, Operand(scope_depth));
9358 __ ldr(r1, MemOperand(r0));
9359 __ add(r1, r1, Operand(1));
9360 __ str(r1, MemOperand(r0));
9361 }
9362
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009363 // Call C built-in.
9364 // r0 = argc, r1 = argv
9365 __ mov(r0, Operand(r4));
9366 __ mov(r1, Operand(r6));
9367
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009368 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9369 int frame_alignment_mask = frame_alignment - 1;
9370#if defined(V8_HOST_ARCH_ARM)
9371 if (FLAG_debug_code) {
9372 if (frame_alignment > kPointerSize) {
9373 Label alignment_as_expected;
9374 ASSERT(IsPowerOf2(frame_alignment));
9375 __ sub(r2, sp, Operand(frame_alignment_skew));
9376 __ tst(r2, Operand(frame_alignment_mask));
9377 __ b(eq, &alignment_as_expected);
9378 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9379 __ stop("Unexpected alignment");
9380 __ bind(&alignment_as_expected);
9381 }
9382 }
9383#endif
9384
9385 // Just before the call (jump) below lr is pushed, so the actual alignment is
9386 // adding one to the current skew.
9387 int alignment_before_call =
9388 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9389 if (alignment_before_call > 0) {
9390 // Push until the alignment before the call is met.
9391 __ mov(r2, Operand(0));
9392 for (int i = alignment_before_call;
9393 (i & frame_alignment_mask) != 0;
9394 i += kPointerSize) {
9395 __ push(r2);
9396 }
9397 }
9398
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009399 // TODO(1242173): To let the GC traverse the return address of the exit
9400 // frames, we need to know where the return address is. Right now,
9401 // we push it on the stack to be able to find it again, but we never
9402 // restore from it in case of changes, which makes it impossible to
9403 // support moving the C entry code stub. This should be fixed, but currently
9404 // this is OK because the CEntryStub gets generated so early in the V8 boot
9405 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009406 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009407 masm->push(lr);
9408 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009409
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009410 // Restore sp back to before aligning the stack.
9411 if (alignment_before_call > 0) {
9412 __ add(sp, sp, Operand(alignment_before_call));
9413 }
9414
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009415 if (always_allocate) {
9416 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9417 // though (contain the result).
9418 __ mov(r2, Operand(scope_depth));
9419 __ ldr(r3, MemOperand(r2));
9420 __ sub(r3, r3, Operand(1));
9421 __ str(r3, MemOperand(r2));
9422 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009423
9424 // check for failure result
9425 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009426 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009427 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9428 __ add(r2, r0, Operand(1));
9429 __ tst(r2, Operand(kFailureTagMask));
9430 __ b(eq, &failure_returned);
9431
9432 // Exit C frame and return.
9433 // r0:r1: result
9434 // sp: stack pointer
9435 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009436 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009437
9438 // check if we should retry or throw exception
9439 Label retry;
9440 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009441 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009442 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9443 __ b(eq, &retry);
9444
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009445 // Special handling of out of memory exceptions.
9446 Failure* out_of_memory = Failure::OutOfMemoryException();
9447 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9448 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009449
9450 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009451 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009452 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009453 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009454 __ ldr(r0, MemOperand(ip));
9455 __ str(r3, MemOperand(ip));
9456
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009457 // Special handling of termination exceptions which are uncatchable
9458 // by javascript code.
9459 __ cmp(r0, Operand(Factory::termination_exception()));
9460 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009461
9462 // Handle normal exception.
9463 __ jmp(throw_normal_exception);
9464
9465 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9466}
9467
9468
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009469void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009470 // Called from JavaScript; parameters are on stack as if calling JS function
9471 // r0: number of arguments including receiver
9472 // r1: pointer to builtin function
9473 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009474 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009475 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009476
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009477 // Result returned in r0 or r0+r1 by default.
9478
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009479 // NOTE: Invocations of builtins may return failure objects
9480 // instead of a proper result. The builtin entry handles
9481 // this by performing a garbage collection and retrying the
9482 // builtin once.
9483
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009484 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009485 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009486
9487 // r4: number of arguments (C callee-saved)
9488 // r5: pointer to builtin function (C callee-saved)
9489 // r6: pointer to first argument (C callee-saved)
9490
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009491 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009492 Label throw_termination_exception;
9493 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009494
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009495 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009496 GenerateCore(masm,
9497 &throw_normal_exception,
9498 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009499 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009500 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009501 false,
9502 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009503
9504 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009505 GenerateCore(masm,
9506 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009507 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009508 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009509 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009510 false,
9511 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009512
9513 // Do full GC and retry runtime call one final time.
9514 Failure* failure = Failure::InternalError();
9515 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9516 GenerateCore(masm,
9517 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009518 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009519 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009520 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009521 true,
9522 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009523
9524 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009525 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9526
9527 __ bind(&throw_termination_exception);
9528 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009529
9530 __ bind(&throw_normal_exception);
9531 GenerateThrowTOS(masm);
9532}
9533
9534
9535void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9536 // r0: code entry
9537 // r1: function
9538 // r2: receiver
9539 // r3: argc
9540 // [sp+0]: argv
9541
9542 Label invoke, exit;
9543
9544 // Called from C, so do not pop argc and args on exit (preserve sp)
9545 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009546 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009547 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9548
9549 // Get address of argv, see stm above.
9550 // r0: code entry
9551 // r1: function
9552 // r2: receiver
9553 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009554 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009555
9556 // Push a frame with special values setup to mark it as an entry frame.
9557 // r0: code entry
9558 // r1: function
9559 // r2: receiver
9560 // r3: argc
9561 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009562 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009563 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9564 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009565 __ mov(r6, Operand(Smi::FromInt(marker)));
9566 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9567 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009568 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009569
9570 // Setup frame pointer for the frame to be pushed.
9571 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9572
9573 // Call a faked try-block that does the invoke.
9574 __ bl(&invoke);
9575
9576 // Caught exception: Store result (exception) in the pending
9577 // exception field in the JSEnv and return a failure sentinel.
9578 // Coming in here the fp will be invalid because the PushTryHandler below
9579 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009580 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009581 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009582 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009583 __ b(&exit);
9584
9585 // Invoke: Link this frame into the handler chain.
9586 __ bind(&invoke);
9587 // Must preserve r0-r4, r5-r7 are available.
9588 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009589 // If an exception not caught by another handler occurs, this handler
9590 // returns control to the code after the bl(&invoke) above, which
9591 // restores all kCalleeSaved registers (including cp and fp) to their
9592 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009593
9594 // Clear any pending exceptions.
9595 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9596 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009597 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009598 __ str(r5, MemOperand(ip));
9599
9600 // Invoke the function by calling through JS entry trampoline builtin.
9601 // Notice that we cannot store a reference to the trampoline code directly in
9602 // this stub, because runtime stubs are not traversed when doing GC.
9603
9604 // Expected registers by Builtins::JSEntryTrampoline
9605 // r0: code entry
9606 // r1: function
9607 // r2: receiver
9608 // r3: argc
9609 // r4: argv
9610 if (is_construct) {
9611 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9612 __ mov(ip, Operand(construct_entry));
9613 } else {
9614 ExternalReference entry(Builtins::JSEntryTrampoline);
9615 __ mov(ip, Operand(entry));
9616 }
9617 __ ldr(ip, MemOperand(ip)); // deref address
9618
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009619 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9620 // macro for the add instruction because we don't want the coverage tool
9621 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009622 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009623 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009624
9625 // Unlink this frame from the handler chain. When reading the
9626 // address of the next handler, there is no need to use the address
9627 // displacement since the current stack pointer (sp) points directly
9628 // to the stack handler.
9629 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9630 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9631 __ str(r3, MemOperand(ip));
9632 // No need to restore registers
9633 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9634
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009635
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009636 __ bind(&exit); // r0 holds result
9637 // Restore the top frame descriptors from the stack.
9638 __ pop(r3);
9639 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9640 __ str(r3, MemOperand(ip));
9641
9642 // Reset the stack to the callee saved registers.
9643 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9644
9645 // Restore callee-saved registers and return.
9646#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009647 if (FLAG_debug_code) {
9648 __ mov(lr, Operand(pc));
9649 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009650#endif
9651 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9652}
9653
9654
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009655// This stub performs an instanceof, calling the builtin function if
9656// necessary. Uses r1 for the object, r0 for the function that it may
9657// be an instance of (these are fetched from the stack).
9658void InstanceofStub::Generate(MacroAssembler* masm) {
9659 // Get the object - slow case for smis (we may need to throw an exception
9660 // depending on the rhs).
9661 Label slow, loop, is_instance, is_not_instance;
9662 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9663 __ BranchOnSmi(r0, &slow);
9664
9665 // Check that the left hand is a JS object and put map in r3.
9666 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9667 __ b(lt, &slow);
9668 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9669 __ b(gt, &slow);
9670
9671 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00009672 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009673 // r1 is function, r3 is map.
9674
9675 // Look up the function and the map in the instanceof cache.
9676 Label miss;
9677 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
9678 __ cmp(r1, ip);
9679 __ b(ne, &miss);
9680 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
9681 __ cmp(r3, ip);
9682 __ b(ne, &miss);
9683 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
9684 __ pop();
9685 __ pop();
9686 __ mov(pc, Operand(lr));
9687
9688 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009689 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
9690
9691 // Check that the function prototype is a JS object.
9692 __ BranchOnSmi(r4, &slow);
9693 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
9694 __ b(lt, &slow);
9695 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
9696 __ b(gt, &slow);
9697
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009698 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
9699 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
9700
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009701 // Register mapping: r3 is object map and r4 is function prototype.
9702 // Get prototype of object into r2.
9703 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9704
9705 // Loop through the prototype chain looking for the function prototype.
9706 __ bind(&loop);
9707 __ cmp(r2, Operand(r4));
9708 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009709 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9710 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009711 __ b(eq, &is_not_instance);
9712 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9713 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9714 __ jmp(&loop);
9715
9716 __ bind(&is_instance);
9717 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009718 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009719 __ pop();
9720 __ pop();
9721 __ mov(pc, Operand(lr)); // Return.
9722
9723 __ bind(&is_not_instance);
9724 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009725 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009726 __ pop();
9727 __ pop();
9728 __ mov(pc, Operand(lr)); // Return.
9729
9730 // Slow-case. Tail call builtin.
9731 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009732 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9733}
9734
9735
ager@chromium.org7c537e22008-10-16 08:43:32 +00009736void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9737 // The displacement is the offset of the last parameter (if any)
9738 // relative to the frame pointer.
9739 static const int kDisplacement =
9740 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009741
ager@chromium.org7c537e22008-10-16 08:43:32 +00009742 // Check that the key is a smi.
9743 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009744 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009745
ager@chromium.org7c537e22008-10-16 08:43:32 +00009746 // Check if the calling frame is an arguments adaptor frame.
9747 Label adaptor;
9748 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9749 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009750 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009751 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009752
ager@chromium.org7c537e22008-10-16 08:43:32 +00009753 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00009754 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00009755 // check for free.
9756 __ cmp(r1, r0);
9757 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009758
ager@chromium.org7c537e22008-10-16 08:43:32 +00009759 // Read the argument from the stack and return it.
9760 __ sub(r3, r0, r1);
9761 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9762 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009763 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009764
9765 // Arguments adaptor case: Check index against actual arguments
9766 // limit found in the arguments adaptor frame. Use unsigned
9767 // comparison to get negative check for free.
9768 __ bind(&adaptor);
9769 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9770 __ cmp(r1, r0);
9771 __ b(cs, &slow);
9772
9773 // Read the argument from the adaptor frame and return it.
9774 __ sub(r3, r0, r1);
9775 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9776 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009777 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009778
9779 // Slow-case: Handle non-smi or out-of-bounds access to arguments
9780 // by calling the runtime system.
9781 __ bind(&slow);
9782 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009783 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009784}
9785
9786
9787void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009788 // sp[0] : number of parameters
9789 // sp[4] : receiver displacement
9790 // sp[8] : function
9791
ager@chromium.org7c537e22008-10-16 08:43:32 +00009792 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00009793 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00009794 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9795 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009796 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00009797 __ b(eq, &adaptor_frame);
9798
9799 // Get the length from the frame.
9800 __ ldr(r1, MemOperand(sp, 0));
9801 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009802
9803 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00009804 __ bind(&adaptor_frame);
9805 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9806 __ str(r1, MemOperand(sp, 0));
9807 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009808 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
9809 __ str(r3, MemOperand(sp, 1 * kPointerSize));
9810
ager@chromium.org5c838252010-02-19 08:53:10 +00009811 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009812 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +00009813 Label add_arguments_object;
9814 __ bind(&try_allocate);
9815 __ cmp(r1, Operand(0));
9816 __ b(eq, &add_arguments_object);
9817 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
9818 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
9819 __ bind(&add_arguments_object);
9820 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
9821
9822 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009823 __ AllocateInNewSpace(
9824 r1,
9825 r0,
9826 r2,
9827 r3,
9828 &runtime,
9829 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +00009830
9831 // Get the arguments boilerplate from the current (global) context.
9832 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
9833 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
9834 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
9835 __ ldr(r4, MemOperand(r4, offset));
9836
9837 // Copy the JS object part.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00009838 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00009839
9840 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009841 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +00009842 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
9843 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
9844
9845 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009846 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00009847 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
9848 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
9849
9850 // If there are no actual arguments, we're done.
9851 Label done;
9852 __ cmp(r1, Operand(0));
9853 __ b(eq, &done);
9854
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009855 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00009856 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009857
9858 // Setup the elements pointer in the allocated arguments object and
9859 // initialize the header in the elements fixed array.
9860 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
9861 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
9862 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
9863 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
9864 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009865 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +00009866
9867 // Copy the fixed array slots.
9868 Label loop;
9869 // Setup r4 to point to the first array slot.
9870 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
9871 __ bind(&loop);
9872 // Pre-decrement r2 with kPointerSize on each iteration.
9873 // Pre-decrement in order to skip receiver.
9874 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
9875 // Post-increment r4 with kPointerSize on each iteration.
9876 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
9877 __ sub(r1, r1, Operand(1));
9878 __ cmp(r1, Operand(0));
9879 __ b(ne, &loop);
9880
9881 // Return and remove the on-stack parameters.
9882 __ bind(&done);
9883 __ add(sp, sp, Operand(3 * kPointerSize));
9884 __ Ret();
9885
ager@chromium.org7c537e22008-10-16 08:43:32 +00009886 // Do the runtime call to allocate the arguments object.
9887 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009888 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009889}
9890
9891
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009892void RegExpExecStub::Generate(MacroAssembler* masm) {
9893 // Just jump directly to runtime if native RegExp is not selected at compile
9894 // time or if regexp entry in generated code is turned off runtime switch or
9895 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009896#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009897 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009898#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009899 if (!FLAG_regexp_entry_native) {
9900 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
9901 return;
9902 }
9903
9904 // Stack frame on entry.
9905 // sp[0]: last_match_info (expected JSArray)
9906 // sp[4]: previous index
9907 // sp[8]: subject string
9908 // sp[12]: JSRegExp object
9909
9910 static const int kLastMatchInfoOffset = 0 * kPointerSize;
9911 static const int kPreviousIndexOffset = 1 * kPointerSize;
9912 static const int kSubjectOffset = 2 * kPointerSize;
9913 static const int kJSRegExpOffset = 3 * kPointerSize;
9914
9915 Label runtime, invoke_regexp;
9916
9917 // Allocation of registers for this function. These are in callee save
9918 // registers and will be preserved by the call to the native RegExp code, as
9919 // this code is called using the normal C calling convention. When calling
9920 // directly from generated code the native RegExp code will not do a GC and
9921 // therefore the content of these registers are safe to use after the call.
9922 Register subject = r4;
9923 Register regexp_data = r5;
9924 Register last_match_info_elements = r6;
9925
9926 // Ensure that a RegExp stack is allocated.
9927 ExternalReference address_of_regexp_stack_memory_address =
9928 ExternalReference::address_of_regexp_stack_memory_address();
9929 ExternalReference address_of_regexp_stack_memory_size =
9930 ExternalReference::address_of_regexp_stack_memory_size();
9931 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
9932 __ ldr(r0, MemOperand(r0, 0));
9933 __ tst(r0, Operand(r0));
9934 __ b(eq, &runtime);
9935
9936 // Check that the first argument is a JSRegExp object.
9937 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009938 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009939 __ tst(r0, Operand(kSmiTagMask));
9940 __ b(eq, &runtime);
9941 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
9942 __ b(ne, &runtime);
9943
9944 // Check that the RegExp has been compiled (data contains a fixed array).
9945 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
9946 if (FLAG_debug_code) {
9947 __ tst(regexp_data, Operand(kSmiTagMask));
9948 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
9949 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
9950 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
9951 }
9952
9953 // regexp_data: RegExp data (FixedArray)
9954 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
9955 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
9956 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
9957 __ b(ne, &runtime);
9958
9959 // regexp_data: RegExp data (FixedArray)
9960 // Check that the number of captures fit in the static offsets vector buffer.
9961 __ ldr(r2,
9962 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
9963 // Calculate number of capture registers (number_of_captures + 1) * 2. This
9964 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009965 STATIC_ASSERT(kSmiTag == 0);
9966 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009967 __ add(r2, r2, Operand(2)); // r2 was a smi.
9968 // Check that the static offsets vector buffer is large enough.
9969 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
9970 __ b(hi, &runtime);
9971
9972 // r2: Number of capture registers
9973 // regexp_data: RegExp data (FixedArray)
9974 // Check that the second argument is a string.
9975 __ ldr(subject, MemOperand(sp, kSubjectOffset));
9976 __ tst(subject, Operand(kSmiTagMask));
9977 __ b(eq, &runtime);
9978 Condition is_string = masm->IsObjectStringType(subject, r0);
9979 __ b(NegateCondition(is_string), &runtime);
9980 // Get the length of the string to r3.
9981 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
9982
9983 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00009984 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009985 // subject: Subject string
9986 // regexp_data: RegExp data (FixedArray)
9987 // Check that the third argument is a positive smi less than the subject
9988 // string length. A negative value will be greater (unsigned comparison).
9989 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009990 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009991 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009992 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009993 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009994
9995 // r2: Number of capture registers
9996 // subject: Subject string
9997 // regexp_data: RegExp data (FixedArray)
9998 // Check that the fourth object is a JSArray object.
9999 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10000 __ tst(r0, Operand(kSmiTagMask));
10001 __ b(eq, &runtime);
10002 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
10003 __ b(ne, &runtime);
10004 // Check that the JSArray is in fast case.
10005 __ ldr(last_match_info_elements,
10006 FieldMemOperand(r0, JSArray::kElementsOffset));
10007 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010008 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000010009 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010010 __ b(ne, &runtime);
10011 // Check that the last match info has space for the capture registers and the
10012 // additional information.
10013 __ ldr(r0,
10014 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
10015 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010016 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010017 __ b(gt, &runtime);
10018
10019 // subject: Subject string
10020 // regexp_data: RegExp data (FixedArray)
10021 // Check the representation and encoding of the subject string.
10022 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010023 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10024 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010025 // First check for flat string.
10026 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010027 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010028 __ b(eq, &seq_string);
10029
10030 // subject: Subject string
10031 // regexp_data: RegExp data (FixedArray)
10032 // Check for flat cons string.
10033 // A flat cons string is a cons string where the second part is the empty
10034 // string. In that case the subject string is just the first part of the cons
10035 // string. Also in this case the first part of the cons string is known to be
10036 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010037 STATIC_ASSERT(kExternalStringTag !=0);
10038 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010039 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010040 __ b(ne, &runtime);
10041 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
10042 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
10043 __ cmp(r0, r1);
10044 __ b(ne, &runtime);
10045 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
10046 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10047 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010048 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010049 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010050 __ tst(r0, Operand(kStringRepresentationMask));
10051 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010052
10053 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010054 // subject: Subject string
10055 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010056 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010057 STATIC_ASSERT(4 == kAsciiStringTag);
10058 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010059 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010060 __ and_(r0, r0, Operand(kStringEncodingMask));
10061 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010062 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10063 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10064
10065 // Check that the irregexp code has been generated for the actual string
10066 // encoding. If it has, the field contains a code object otherwise it contains
10067 // the hole.
10068 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10069 __ b(ne, &runtime);
10070
10071 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10072 // r7: code
10073 // subject: Subject string
10074 // regexp_data: RegExp data (FixedArray)
10075 // Load used arguments before starting to push arguments for call to native
10076 // RegExp code to avoid handling changing stack height.
10077 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10078 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10079
10080 // r1: previous index
10081 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10082 // r7: code
10083 // subject: Subject string
10084 // regexp_data: RegExp data (FixedArray)
10085 // All checks done. Now push arguments for native regexp code.
10086 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10087
10088 static const int kRegExpExecuteArguments = 7;
10089 __ push(lr);
10090 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10091
10092 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10093 __ mov(r0, Operand(1));
10094 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10095
10096 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10097 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10098 __ ldr(r0, MemOperand(r0, 0));
10099 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10100 __ ldr(r2, MemOperand(r2, 0));
10101 __ add(r0, r0, Operand(r2));
10102 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10103
10104 // Argument 5 (sp[0]): static offsets vector buffer.
10105 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10106 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10107
10108 // For arguments 4 and 3 get string length, calculate start of string data and
10109 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10110 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010111 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010112 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010113 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10114 __ eor(r3, r3, Operand(1));
10115 // Argument 4 (r3): End of string data
10116 // Argument 3 (r2): Start of string data
10117 __ add(r2, r9, Operand(r1, LSL, r3));
10118 __ add(r3, r9, Operand(r0, LSL, r3));
10119
10120 // Argument 2 (r1): Previous index.
10121 // Already there
10122
10123 // Argument 1 (r0): Subject string.
10124 __ mov(r0, subject);
10125
10126 // Locate the code entry and call it.
10127 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10128 __ CallCFunction(r7, kRegExpExecuteArguments);
10129 __ pop(lr);
10130
10131 // r0: result
10132 // subject: subject string (callee saved)
10133 // regexp_data: RegExp data (callee saved)
10134 // last_match_info_elements: Last match info elements (callee saved)
10135
10136 // Check the result.
10137 Label success;
10138 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10139 __ b(eq, &success);
10140 Label failure;
10141 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10142 __ b(eq, &failure);
10143 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10144 // If not exception it can only be retry. Handle that in the runtime system.
10145 __ b(ne, &runtime);
10146 // Result must now be exception. If there is no pending exception already a
10147 // stack overflow (on the backtrack stack) was detected in RegExp code but
10148 // haven't created the exception yet. Handle that in the runtime system.
10149 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10150 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10151 __ ldr(r0, MemOperand(r0, 0));
10152 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10153 __ ldr(r1, MemOperand(r1, 0));
10154 __ cmp(r0, r1);
10155 __ b(eq, &runtime);
10156 __ bind(&failure);
10157 // For failure and exception return null.
10158 __ mov(r0, Operand(Factory::null_value()));
10159 __ add(sp, sp, Operand(4 * kPointerSize));
10160 __ Ret();
10161
10162 // Process the result from the native regexp code.
10163 __ bind(&success);
10164 __ ldr(r1,
10165 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10166 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010167 STATIC_ASSERT(kSmiTag == 0);
10168 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010169 __ add(r1, r1, Operand(2)); // r1 was a smi.
10170
10171 // r1: number of capture registers
10172 // r4: subject string
10173 // Store the capture count.
10174 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10175 __ str(r2, FieldMemOperand(last_match_info_elements,
10176 RegExpImpl::kLastCaptureCountOffset));
10177 // Store last subject and last input.
10178 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010179 __ str(subject,
10180 FieldMemOperand(last_match_info_elements,
10181 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010182 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010183 __ str(subject,
10184 FieldMemOperand(last_match_info_elements,
10185 RegExpImpl::kLastInputOffset));
10186 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010187 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010188
10189 // Get the static offsets vector filled by the native regexp code.
10190 ExternalReference address_of_static_offsets_vector =
10191 ExternalReference::address_of_static_offsets_vector();
10192 __ mov(r2, Operand(address_of_static_offsets_vector));
10193
10194 // r1: number of capture registers
10195 // r2: offsets vector
10196 Label next_capture, done;
10197 // Capture register counter starts from number of capture registers and
10198 // counts down until wraping after zero.
10199 __ add(r0,
10200 last_match_info_elements,
10201 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10202 __ bind(&next_capture);
10203 __ sub(r1, r1, Operand(1), SetCC);
10204 __ b(mi, &done);
10205 // Read the value from the static offsets vector buffer.
10206 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10207 // Store the smi value in the last match info.
10208 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10209 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10210 __ jmp(&next_capture);
10211 __ bind(&done);
10212
10213 // Return last match info.
10214 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10215 __ add(sp, sp, Operand(4 * kPointerSize));
10216 __ Ret();
10217
10218 // Do the runtime call to execute the regexp.
10219 __ bind(&runtime);
10220 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010221#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010222}
10223
10224
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010225void CallFunctionStub::Generate(MacroAssembler* masm) {
10226 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010227
10228 // If the receiver might be a value (string, number or boolean) check for this
10229 // and box it if it is.
10230 if (ReceiverMightBeValue()) {
10231 // Get the receiver from the stack.
10232 // function, receiver [, arguments]
10233 Label receiver_is_value, receiver_is_js_object;
10234 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10235
10236 // Check if receiver is a smi (which is a number value).
10237 __ BranchOnSmi(r1, &receiver_is_value);
10238
10239 // Check if the receiver is a valid JS object.
10240 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10241 __ b(ge, &receiver_is_js_object);
10242
10243 // Call the runtime to box the value.
10244 __ bind(&receiver_is_value);
10245 __ EnterInternalFrame();
10246 __ push(r1);
10247 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10248 __ LeaveInternalFrame();
10249 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10250
10251 __ bind(&receiver_is_js_object);
10252 }
10253
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010254 // Get the function to call from the stack.
10255 // function, receiver [, arguments]
10256 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10257
10258 // Check that the function is really a JavaScript function.
10259 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010260 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010261 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010262 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010263 __ b(ne, &slow);
10264
10265 // Fast-case: Invoke the function now.
10266 // r1: pushed function
10267 ParameterCount actual(argc_);
10268 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10269
10270 // Slow-case: Non-function called.
10271 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010272 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10273 // of the original receiver from the call site).
10274 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010275 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010276 __ mov(r2, Operand(0));
10277 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10278 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10279 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010280}
10281
10282
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010283// Unfortunately you have to run without snapshots to see most of these
10284// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010285const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010286 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10287 (lhs_.is(r1) && rhs_.is(r0)));
10288
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010289 if (name_ != NULL) return name_;
10290 const int kMaxNameLength = 100;
10291 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10292 if (name_ == NULL) return "OOM";
10293
10294 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010295 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010296 case lt: cc_name = "LT"; break;
10297 case gt: cc_name = "GT"; break;
10298 case le: cc_name = "LE"; break;
10299 case ge: cc_name = "GE"; break;
10300 case eq: cc_name = "EQ"; break;
10301 case ne: cc_name = "NE"; break;
10302 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010303 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010304
ager@chromium.orgb5737492010-07-15 09:29:43 +000010305 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10306 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10307
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010308 const char* strict_name = "";
10309 if (strict_ && (cc_ == eq || cc_ == ne)) {
10310 strict_name = "_STRICT";
10311 }
10312
10313 const char* never_nan_nan_name = "";
10314 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10315 never_nan_nan_name = "_NO_NAN";
10316 }
10317
10318 const char* include_number_compare_name = "";
10319 if (!include_number_compare_) {
10320 include_number_compare_name = "_NO_NUMBER";
10321 }
10322
10323 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010324 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010325 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010326 lhs_name,
10327 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010328 strict_name,
10329 never_nan_nan_name,
10330 include_number_compare_name);
10331 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010332}
10333
10334
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010335int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010336 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10337 // stubs the never NaN NaN condition is only taken into account if the
10338 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010339 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10340 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10341 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010342 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010343 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010344 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010345 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10346 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010347}
10348
10349
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010350// StringCharCodeAtGenerator
10351
10352void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10353 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010354 Label ascii_string;
10355 Label got_char_code;
10356
10357 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010358 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010359
10360 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010361 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10362 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010363 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010364 __ tst(result_, Operand(kIsNotStringMask));
10365 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010366
10367 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010368 __ BranchOnNotSmi(index_, &index_not_smi_);
10369
10370 // Put smi-tagged index into scratch register.
10371 __ mov(scratch_, index_);
10372 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010373
10374 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010375 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10376 __ cmp(ip, Operand(scratch_));
10377 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010378
10379 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010380 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010381 __ tst(result_, Operand(kStringRepresentationMask));
10382 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010383
10384 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010385 __ tst(result_, Operand(kIsConsStringMask));
10386 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010387
10388 // ConsString.
10389 // Check whether the right hand side is the empty string (i.e. if
10390 // this is really a flat string in a cons string). If that is not
10391 // the case we would rather go to the runtime system now to flatten
10392 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010393 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10394 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10395 __ cmp(result_, Operand(ip));
10396 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010397 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010398 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10399 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10400 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10401 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010402 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010403 __ tst(result_, Operand(kStringRepresentationMask));
10404 __ b(nz, &call_runtime_);
10405
10406 // Check for 1-byte or 2-byte string.
10407 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010408 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010409 __ tst(result_, Operand(kStringEncodingMask));
10410 __ b(nz, &ascii_string);
10411
10412 // 2-byte string.
10413 // Load the 2-byte character code into the result register. We can
10414 // add without shifting since the smi tag size is the log2 of the
10415 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010416 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010417 __ add(scratch_, object_, Operand(scratch_));
10418 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10419 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010420
10421 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010422 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010423 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010424 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10425 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010426
10427 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010428 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10429 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010430}
10431
10432
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010433void StringCharCodeAtGenerator::GenerateSlow(
10434 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10435 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010436
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010437 // Index is not a smi.
10438 __ bind(&index_not_smi_);
10439 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010440 __ CheckMap(index_,
10441 scratch_,
10442 Heap::kHeapNumberMapRootIndex,
10443 index_not_number_,
10444 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010445 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010446 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010447 __ push(index_); // Consumed by runtime conversion function.
10448 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10449 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10450 } else {
10451 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10452 // NumberToSmi discards numbers that are not exact integers.
10453 __ CallRuntime(Runtime::kNumberToSmi, 1);
10454 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010455 // Save the conversion result before the pop instructions below
10456 // have a chance to overwrite it.
10457 __ Move(scratch_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010458 __ pop(index_);
10459 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010460 // Reload the instance type.
10461 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10462 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010463 call_helper.AfterCall(masm);
10464 // If index is still not a smi, it must be out of range.
10465 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10466 // Otherwise, return to the fast path.
10467 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010468
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010469 // Call runtime. We get here when the receiver is a string and the
10470 // index is a number, but the code of getting the actual character
10471 // is too complex (e.g., when the string needs to be flattened).
10472 __ bind(&call_runtime_);
10473 call_helper.BeforeCall(masm);
10474 __ Push(object_, index_);
10475 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010476 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010477 call_helper.AfterCall(masm);
10478 __ jmp(&exit_);
10479
10480 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10481}
10482
10483
10484// -------------------------------------------------------------------------
10485// StringCharFromCodeGenerator
10486
10487void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010488 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010489 STATIC_ASSERT(kSmiTag == 0);
10490 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010491 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010492 __ tst(code_,
10493 Operand(kSmiTagMask |
10494 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10495 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010496
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010497 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10498 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010499 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010500 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10501 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10502 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10503 __ cmp(result_, Operand(ip));
10504 __ b(eq, &slow_case_);
10505 __ bind(&exit_);
10506}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010507
ager@chromium.orgac091b72010-05-05 07:34:42 +000010508
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010509void StringCharFromCodeGenerator::GenerateSlow(
10510 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10511 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10512
10513 __ bind(&slow_case_);
10514 call_helper.BeforeCall(masm);
10515 __ push(code_);
10516 __ CallRuntime(Runtime::kCharFromCode, 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010517 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010518 call_helper.AfterCall(masm);
10519 __ jmp(&exit_);
10520
10521 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10522}
10523
10524
10525// -------------------------------------------------------------------------
10526// StringCharAtGenerator
10527
10528void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10529 char_code_at_generator_.GenerateFast(masm);
10530 char_from_code_generator_.GenerateFast(masm);
10531}
10532
10533
10534void StringCharAtGenerator::GenerateSlow(
10535 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10536 char_code_at_generator_.GenerateSlow(masm, call_helper);
10537 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010538}
10539
10540
10541void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10542 Register dest,
10543 Register src,
10544 Register count,
10545 Register scratch,
10546 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010547 Label loop;
10548 Label done;
10549 // This loop just copies one character at a time, as it is only used for very
10550 // short strings.
10551 if (!ascii) {
10552 __ add(count, count, Operand(count), SetCC);
10553 } else {
10554 __ cmp(count, Operand(0));
10555 }
10556 __ b(eq, &done);
10557
10558 __ bind(&loop);
10559 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10560 // Perform sub between load and dependent store to get the load time to
10561 // complete.
10562 __ sub(count, count, Operand(1), SetCC);
10563 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10564 // last iteration.
10565 __ b(gt, &loop);
10566
10567 __ bind(&done);
10568}
10569
10570
10571enum CopyCharactersFlags {
10572 COPY_ASCII = 1,
10573 DEST_ALWAYS_ALIGNED = 2
10574};
10575
10576
ager@chromium.orgac091b72010-05-05 07:34:42 +000010577void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10578 Register dest,
10579 Register src,
10580 Register count,
10581 Register scratch1,
10582 Register scratch2,
10583 Register scratch3,
10584 Register scratch4,
10585 Register scratch5,
10586 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010587 bool ascii = (flags & COPY_ASCII) != 0;
10588 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10589
10590 if (dest_always_aligned && FLAG_debug_code) {
10591 // Check that destination is actually word aligned if the flag says
10592 // that it is.
10593 __ tst(dest, Operand(kPointerAlignmentMask));
10594 __ Check(eq, "Destination of copy not aligned.");
10595 }
10596
10597 const int kReadAlignment = 4;
10598 const int kReadAlignmentMask = kReadAlignment - 1;
10599 // Ensure that reading an entire aligned word containing the last character
10600 // of a string will not read outside the allocated area (because we pad up
10601 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010602 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010603 // Assumes word reads and writes are little endian.
10604 // Nothing to do for zero characters.
10605 Label done;
10606 if (!ascii) {
10607 __ add(count, count, Operand(count), SetCC);
10608 } else {
10609 __ cmp(count, Operand(0));
10610 }
10611 __ b(eq, &done);
10612
10613 // Assume that you cannot read (or write) unaligned.
10614 Label byte_loop;
10615 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10616 __ cmp(count, Operand(8));
10617 __ add(count, dest, Operand(count));
10618 Register limit = count; // Read until src equals this.
10619 __ b(lt, &byte_loop);
10620
10621 if (!dest_always_aligned) {
10622 // Align dest by byte copying. Copies between zero and three bytes.
10623 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10624 Label dest_aligned;
10625 __ b(eq, &dest_aligned);
10626 __ cmp(scratch4, Operand(2));
10627 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10628 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10629 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10630 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10631 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10632 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10633 __ bind(&dest_aligned);
10634 }
10635
10636 Label simple_loop;
10637
10638 __ sub(scratch4, dest, Operand(src));
10639 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10640 __ b(eq, &simple_loop);
10641 // Shift register is number of bits in a source word that
10642 // must be combined with bits in the next source word in order
10643 // to create a destination word.
10644
10645 // Complex loop for src/dst that are not aligned the same way.
10646 {
10647 Label loop;
10648 __ mov(scratch4, Operand(scratch4, LSL, 3));
10649 Register left_shift = scratch4;
10650 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10651 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10652 // Store the "shift" most significant bits of scratch in the least
10653 // signficant bits (i.e., shift down by (32-shift)).
10654 __ rsb(scratch2, left_shift, Operand(32));
10655 Register right_shift = scratch2;
10656 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10657
10658 __ bind(&loop);
10659 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10660 __ sub(scratch5, limit, Operand(dest));
10661 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10662 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10663 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10664 // Loop if four or more bytes left to copy.
10665 // Compare to eight, because we did the subtract before increasing dst.
10666 __ sub(scratch5, scratch5, Operand(8), SetCC);
10667 __ b(ge, &loop);
10668 }
10669 // There is now between zero and three bytes left to copy (negative that
10670 // number is in scratch5), and between one and three bytes already read into
10671 // scratch1 (eight times that number in scratch4). We may have read past
10672 // the end of the string, but because objects are aligned, we have not read
10673 // past the end of the object.
10674 // Find the minimum of remaining characters to move and preloaded characters
10675 // and write those as bytes.
10676 __ add(scratch5, scratch5, Operand(4), SetCC);
10677 __ b(eq, &done);
10678 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
10679 // Move minimum of bytes read and bytes left to copy to scratch4.
10680 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
10681 // Between one and three (value in scratch5) characters already read into
10682 // scratch ready to write.
10683 __ cmp(scratch5, Operand(2));
10684 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10685 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
10686 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
10687 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
10688 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
10689 // Copy any remaining bytes.
10690 __ b(&byte_loop);
10691
10692 // Simple loop.
10693 // Copy words from src to dst, until less than four bytes left.
10694 // Both src and dest are word aligned.
10695 __ bind(&simple_loop);
10696 {
10697 Label loop;
10698 __ bind(&loop);
10699 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10700 __ sub(scratch3, limit, Operand(dest));
10701 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10702 // Compare to 8, not 4, because we do the substraction before increasing
10703 // dest.
10704 __ cmp(scratch3, Operand(8));
10705 __ b(ge, &loop);
10706 }
10707
10708 // Copy bytes from src to dst until dst hits limit.
10709 __ bind(&byte_loop);
10710 __ cmp(dest, Operand(limit));
10711 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10712 __ b(ge, &done);
10713 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10714 __ b(&byte_loop);
10715
10716 __ bind(&done);
10717}
10718
10719
ager@chromium.orgac091b72010-05-05 07:34:42 +000010720void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10721 Register c1,
10722 Register c2,
10723 Register scratch1,
10724 Register scratch2,
10725 Register scratch3,
10726 Register scratch4,
10727 Register scratch5,
10728 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010729 // Register scratch3 is the general scratch register in this function.
10730 Register scratch = scratch3;
10731
10732 // Make sure that both characters are not digits as such strings has a
10733 // different hash algorithm. Don't try to look for these in the symbol table.
10734 Label not_array_index;
10735 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10736 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10737 __ b(hi, &not_array_index);
10738 __ sub(scratch, c2, Operand(static_cast<int>('0')));
10739 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10740
10741 // If check failed combine both characters into single halfword.
10742 // This is required by the contract of the method: code at the
10743 // not_found branch expects this combination in c1 register
10744 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
10745 __ b(ls, not_found);
10746
10747 __ bind(&not_array_index);
10748 // Calculate the two character string hash.
10749 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010750 StringHelper::GenerateHashInit(masm, hash, c1);
10751 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
10752 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010753
10754 // Collect the two characters in a register.
10755 Register chars = c1;
10756 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
10757
10758 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10759 // hash: hash of two character string.
10760
10761 // Load symbol table
10762 // Load address of first element of the symbol table.
10763 Register symbol_table = c2;
10764 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
10765
10766 // Load undefined value
10767 Register undefined = scratch4;
10768 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
10769
10770 // Calculate capacity mask from the symbol table capacity.
10771 Register mask = scratch2;
10772 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
10773 __ mov(mask, Operand(mask, ASR, 1));
10774 __ sub(mask, mask, Operand(1));
10775
10776 // Calculate untagged address of the first element of the symbol table.
10777 Register first_symbol_table_element = symbol_table;
10778 __ add(first_symbol_table_element, symbol_table,
10779 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
10780
10781 // Registers
10782 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10783 // hash: hash of two character string
10784 // mask: capacity mask
10785 // first_symbol_table_element: address of the first element of
10786 // the symbol table
10787 // scratch: -
10788
10789 // Perform a number of probes in the symbol table.
10790 static const int kProbes = 4;
10791 Label found_in_symbol_table;
10792 Label next_probe[kProbes];
10793 for (int i = 0; i < kProbes; i++) {
10794 Register candidate = scratch5; // Scratch register contains candidate.
10795
10796 // Calculate entry in symbol table.
10797 if (i > 0) {
10798 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
10799 } else {
10800 __ mov(candidate, hash);
10801 }
10802
10803 __ and_(candidate, candidate, Operand(mask));
10804
10805 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010806 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010807 __ ldr(candidate,
10808 MemOperand(first_symbol_table_element,
10809 candidate,
10810 LSL,
10811 kPointerSizeLog2));
10812
10813 // If entry is undefined no string with this hash can be found.
10814 __ cmp(candidate, undefined);
10815 __ b(eq, not_found);
10816
10817 // If length is not 2 the string is not a candidate.
10818 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010819 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010820 __ b(ne, &next_probe[i]);
10821
10822 // Check that the candidate is a non-external ascii string.
10823 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
10824 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
10825 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
10826 &next_probe[i]);
10827
10828 // Check if the two characters match.
10829 // Assumes that word load is little endian.
10830 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
10831 __ cmp(chars, scratch);
10832 __ b(eq, &found_in_symbol_table);
10833 __ bind(&next_probe[i]);
10834 }
10835
10836 // No matching 2 character string found by probing.
10837 __ jmp(not_found);
10838
10839 // Scratch register contains result when we fall through to here.
10840 Register result = scratch;
10841 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000010842 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010843}
10844
10845
ager@chromium.orgac091b72010-05-05 07:34:42 +000010846void StringHelper::GenerateHashInit(MacroAssembler* masm,
10847 Register hash,
10848 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010849 // hash = character + (character << 10);
10850 __ add(hash, character, Operand(character, LSL, 10));
10851 // hash ^= hash >> 6;
10852 __ eor(hash, hash, Operand(hash, ASR, 6));
10853}
10854
10855
ager@chromium.orgac091b72010-05-05 07:34:42 +000010856void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
10857 Register hash,
10858 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010859 // hash += character;
10860 __ add(hash, hash, Operand(character));
10861 // hash += hash << 10;
10862 __ add(hash, hash, Operand(hash, LSL, 10));
10863 // hash ^= hash >> 6;
10864 __ eor(hash, hash, Operand(hash, ASR, 6));
10865}
10866
10867
ager@chromium.orgac091b72010-05-05 07:34:42 +000010868void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
10869 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010870 // hash += hash << 3;
10871 __ add(hash, hash, Operand(hash, LSL, 3));
10872 // hash ^= hash >> 11;
10873 __ eor(hash, hash, Operand(hash, ASR, 11));
10874 // hash += hash << 15;
10875 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
10876
10877 // if (hash == 0) hash = 27;
10878 __ mov(hash, Operand(27), LeaveCC, nz);
10879}
10880
10881
ager@chromium.org5c838252010-02-19 08:53:10 +000010882void SubStringStub::Generate(MacroAssembler* masm) {
10883 Label runtime;
10884
10885 // Stack frame on entry.
10886 // lr: return address
10887 // sp[0]: to
10888 // sp[4]: from
10889 // sp[8]: string
10890
10891 // This stub is called from the native-call %_SubString(...), so
10892 // nothing can be assumed about the arguments. It is tested that:
10893 // "string" is a sequential string,
10894 // both "from" and "to" are smis, and
10895 // 0 <= from <= to <= string.length.
10896 // If any of these assumptions fail, we call the runtime system.
10897
10898 static const int kToOffset = 0 * kPointerSize;
10899 static const int kFromOffset = 1 * kPointerSize;
10900 static const int kStringOffset = 2 * kPointerSize;
10901
10902
10903 // Check bounds and smi-ness.
10904 __ ldr(r7, MemOperand(sp, kToOffset));
10905 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010906 STATIC_ASSERT(kSmiTag == 0);
10907 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010908 // I.e., arithmetic shift right by one un-smi-tags.
10909 __ mov(r2, Operand(r7, ASR, 1), SetCC);
10910 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
10911 // If either r2 or r6 had the smi tag bit set, then carry is set now.
10912 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
10913 __ b(mi, &runtime); // From is negative.
10914
10915 __ sub(r2, r2, Operand(r3), SetCC);
10916 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010917 // Special handling of sub-strings of length 1 and 2. One character strings
10918 // are handled in the runtime system (looked up in the single character
10919 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000010920 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010921 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000010922
10923 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010924 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010925 // r6: from (smi)
10926 // r7: to (smi)
10927
10928 // Make sure first argument is a sequential (or flat) string.
10929 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010930 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010931 __ tst(r5, Operand(kSmiTagMask));
10932 __ b(eq, &runtime);
10933 Condition is_string = masm->IsObjectStringType(r5, r1);
10934 __ b(NegateCondition(is_string), &runtime);
10935
10936 // r1: instance type
10937 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010938 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010939 // r5: string
10940 // r6: from (smi)
10941 // r7: to (smi)
10942 Label seq_string;
10943 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010944 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
10945 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000010946 __ cmp(r4, Operand(kConsStringTag));
10947 __ b(gt, &runtime); // External strings go to runtime.
10948 __ b(lt, &seq_string); // Sequential strings are handled directly.
10949
10950 // Cons string. Try to recurse (once) on the first substring.
10951 // (This adds a little more generality than necessary to handle flattened
10952 // cons strings, but not much).
10953 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
10954 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
10955 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10956 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010957 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010958 __ b(ne, &runtime); // Cons and External strings go to runtime.
10959
10960 // Definitly a sequential string.
10961 __ bind(&seq_string);
10962
10963 // r1: instance type.
10964 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010965 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010966 // r5: string
10967 // r6: from (smi)
10968 // r7: to (smi)
10969 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010970 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000010971 __ b(lt, &runtime); // Fail if to > length.
10972
10973 // r1: instance type.
10974 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010975 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010976 // r5: string.
10977 // r6: from offset (smi)
10978 // Check for flat ascii string.
10979 Label non_ascii_flat;
10980 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010981 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010982 __ b(eq, &non_ascii_flat);
10983
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010984 Label result_longer_than_two;
10985 __ cmp(r2, Operand(2));
10986 __ b(gt, &result_longer_than_two);
10987
10988 // Sub string of length 2 requested.
10989 // Get the two characters forming the sub string.
10990 __ add(r5, r5, Operand(r3));
10991 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
10992 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
10993
10994 // Try to lookup two character string in symbol table.
10995 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010996 StringHelper::GenerateTwoCharacterSymbolTableProbe(
10997 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010998 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10999 __ add(sp, sp, Operand(3 * kPointerSize));
11000 __ Ret();
11001
11002 // r2: result string length.
11003 // r3: two characters combined into halfword in little endian byte order.
11004 __ bind(&make_two_character_string);
11005 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
11006 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11007 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11008 __ add(sp, sp, Operand(3 * kPointerSize));
11009 __ Ret();
11010
11011 __ bind(&result_longer_than_two);
11012
ager@chromium.org5c838252010-02-19 08:53:10 +000011013 // Allocate the result.
11014 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
11015
11016 // r0: result string.
11017 // r2: result string length.
11018 // r5: string.
11019 // r6: from offset (smi)
11020 // Locate first character of result.
11021 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11022 // Locate 'from' character of string.
11023 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11024 __ add(r5, r5, Operand(r6, ASR, 1));
11025
11026 // r0: result string.
11027 // r1: first character of result string.
11028 // r2: result string length.
11029 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011030 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011031 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11032 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011033 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11034 __ add(sp, sp, Operand(3 * kPointerSize));
11035 __ Ret();
11036
11037 __ bind(&non_ascii_flat);
11038 // r2: result string length.
11039 // r5: string.
11040 // r6: from offset (smi)
11041 // Check for flat two byte string.
11042
11043 // Allocate the result.
11044 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
11045
11046 // r0: result string.
11047 // r2: result string length.
11048 // r5: string.
11049 // Locate first character of result.
11050 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11051 // Locate 'from' character of string.
11052 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11053 // As "from" is a smi it is 2 times the value which matches the size of a two
11054 // byte character.
11055 __ add(r5, r5, Operand(r6));
11056
11057 // r0: result string.
11058 // r1: first character of result.
11059 // r2: result length.
11060 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011061 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011062 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11063 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011064 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11065 __ add(sp, sp, Operand(3 * kPointerSize));
11066 __ Ret();
11067
11068 // Just jump to runtime to create the sub string.
11069 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011070 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011071}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011072
11073
11074void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11075 Register left,
11076 Register right,
11077 Register scratch1,
11078 Register scratch2,
11079 Register scratch3,
11080 Register scratch4) {
11081 Label compare_lengths;
11082 // Find minimum length and length difference.
11083 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11084 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11085 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11086 Register length_delta = scratch3;
11087 __ mov(scratch1, scratch2, LeaveCC, gt);
11088 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011089 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011090 __ tst(min_length, Operand(min_length));
11091 __ b(eq, &compare_lengths);
11092
ager@chromium.orgac091b72010-05-05 07:34:42 +000011093 // Untag smi.
11094 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11095
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011096 // Setup registers so that we only need to increment one register
11097 // in the loop.
11098 __ add(scratch2, min_length,
11099 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11100 __ add(left, left, Operand(scratch2));
11101 __ add(right, right, Operand(scratch2));
11102 // Registers left and right points to the min_length character of strings.
11103 __ rsb(min_length, min_length, Operand(-1));
11104 Register index = min_length;
11105 // Index starts at -min_length.
11106
11107 {
11108 // Compare loop.
11109 Label loop;
11110 __ bind(&loop);
11111 // Compare characters.
11112 __ add(index, index, Operand(1), SetCC);
11113 __ ldrb(scratch2, MemOperand(left, index), ne);
11114 __ ldrb(scratch4, MemOperand(right, index), ne);
11115 // Skip to compare lengths with eq condition true.
11116 __ b(eq, &compare_lengths);
11117 __ cmp(scratch2, scratch4);
11118 __ b(eq, &loop);
11119 // Fallthrough with eq condition false.
11120 }
11121 // Compare lengths - strings up to min-length are equal.
11122 __ bind(&compare_lengths);
11123 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11124 // Use zero length_delta as result.
11125 __ mov(r0, Operand(length_delta), SetCC, eq);
11126 // Fall through to here if characters compare not-equal.
11127 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11128 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11129 __ Ret();
11130}
11131
11132
11133void StringCompareStub::Generate(MacroAssembler* masm) {
11134 Label runtime;
11135
11136 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011137 // sp[0]: right string
11138 // sp[4]: left string
11139 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11140 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011141
11142 Label not_same;
11143 __ cmp(r0, r1);
11144 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011145 STATIC_ASSERT(EQUAL == 0);
11146 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011147 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11148 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11149 __ add(sp, sp, Operand(2 * kPointerSize));
11150 __ Ret();
11151
11152 __ bind(&not_same);
11153
11154 // Check that both objects are sequential ascii strings.
11155 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11156
11157 // Compare flat ascii strings natively. Remove arguments from stack first.
11158 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11159 __ add(sp, sp, Operand(2 * kPointerSize));
11160 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11161
11162 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11163 // tagged as a small integer.
11164 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011165 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011166}
11167
11168
ager@chromium.org5c838252010-02-19 08:53:10 +000011169void StringAddStub::Generate(MacroAssembler* masm) {
11170 Label string_add_runtime;
11171 // Stack on entry:
11172 // sp[0]: second argument.
11173 // sp[4]: first argument.
11174
11175 // Load the two arguments.
11176 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11177 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11178
11179 // Make sure that both arguments are strings if not known in advance.
11180 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011181 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011182 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11183 // Load instance types.
11184 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11185 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11186 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11187 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011188 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011189 // If either is not a string, go to runtime.
11190 __ tst(r4, Operand(kIsNotStringMask));
11191 __ tst(r5, Operand(kIsNotStringMask), eq);
11192 __ b(ne, &string_add_runtime);
11193 }
11194
11195 // Both arguments are strings.
11196 // r0: first string
11197 // r1: second string
11198 // r4: first string instance type (if string_check_)
11199 // r5: second string instance type (if string_check_)
11200 {
11201 Label strings_not_empty;
11202 // Check if either of the strings are empty. In that case return the other.
11203 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11204 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011205 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011206 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011207 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011208 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011209 // Else test if second string is empty.
11210 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011211 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11212
11213 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11214 __ add(sp, sp, Operand(2 * kPointerSize));
11215 __ Ret();
11216
11217 __ bind(&strings_not_empty);
11218 }
11219
ager@chromium.orgac091b72010-05-05 07:34:42 +000011220 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11221 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011222 // Both strings are non-empty.
11223 // r0: first string
11224 // r1: second string
11225 // r2: length of first string
11226 // r3: length of second string
11227 // r4: first string instance type (if string_check_)
11228 // r5: second string instance type (if string_check_)
11229 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011230 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011231 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011232 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011233 __ add(r6, r2, Operand(r3));
11234 // Use the runtime system when adding two one character strings, as it
11235 // contains optimizations for this specific case using the symbol table.
11236 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011237 __ b(ne, &longer_than_two);
11238
11239 // Check that both strings are non-external ascii strings.
11240 if (!string_check_) {
11241 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11242 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11243 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11244 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11245 }
11246 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11247 &string_add_runtime);
11248
11249 // Get the two characters forming the sub string.
11250 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11251 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11252
11253 // Try to lookup two character string in symbol table. If it is not found
11254 // just allocate a new one.
11255 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011256 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11257 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011258 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11259 __ add(sp, sp, Operand(2 * kPointerSize));
11260 __ Ret();
11261
11262 __ bind(&make_two_character_string);
11263 // Resulting string has length 2 and first chars of two strings
11264 // are combined into single halfword in r2 register.
11265 // So we can fill resulting string without two loops by a single
11266 // halfword store instruction (which assumes that processor is
11267 // in a little endian mode)
11268 __ mov(r6, Operand(2));
11269 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11270 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11271 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11272 __ add(sp, sp, Operand(2 * kPointerSize));
11273 __ Ret();
11274
11275 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011276 // Check if resulting string will be flat.
11277 __ cmp(r6, Operand(String::kMinNonFlatLength));
11278 __ b(lt, &string_add_flat_result);
11279 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011280 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011281 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11282 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11283 __ cmp(r6, Operand(String::kMaxLength + 1));
11284 __ b(hs, &string_add_runtime);
11285
11286 // If result is not supposed to be flat, allocate a cons string object.
11287 // If both strings are ascii the result is an ascii cons string.
11288 if (!string_check_) {
11289 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11290 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11291 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11292 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11293 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011294 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011295 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011296 __ tst(r4, Operand(kStringEncodingMask));
11297 __ tst(r5, Operand(kStringEncodingMask), ne);
11298 __ b(eq, &non_ascii);
11299
11300 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011301 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011302 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11303 __ bind(&allocated);
11304 // Fill the fields of the cons string.
11305 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11306 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11307 __ mov(r0, Operand(r7));
11308 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11309 __ add(sp, sp, Operand(2 * kPointerSize));
11310 __ Ret();
11311
11312 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011313 // At least one of the strings is two-byte. Check whether it happens
11314 // to contain only ascii characters.
11315 // r4: first instance type.
11316 // r5: second instance type.
11317 __ tst(r4, Operand(kAsciiDataHintMask));
11318 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11319 __ b(ne, &ascii_data);
11320 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011321 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011322 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11323 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11324 __ b(eq, &ascii_data);
11325
ager@chromium.org5c838252010-02-19 08:53:10 +000011326 // Allocate a two byte cons string.
11327 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11328 __ jmp(&allocated);
11329
11330 // Handle creating a flat result. First check that both strings are
11331 // sequential and that they have the same encoding.
11332 // r0: first string
11333 // r1: second string
11334 // r2: length of first string
11335 // r3: length of second string
11336 // r4: first string instance type (if string_check_)
11337 // r5: second string instance type (if string_check_)
11338 // r6: sum of lengths.
11339 __ bind(&string_add_flat_result);
11340 if (!string_check_) {
11341 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11342 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11343 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11344 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11345 }
11346 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011347 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011348 __ tst(r4, Operand(kStringRepresentationMask));
11349 __ tst(r5, Operand(kStringRepresentationMask), eq);
11350 __ b(ne, &string_add_runtime);
11351 // Now check if both strings have the same encoding (ASCII/Two-byte).
11352 // r0: first string.
11353 // r1: second string.
11354 // r2: length of first string.
11355 // r3: length of second string.
11356 // r6: sum of lengths..
11357 Label non_ascii_string_add_flat_result;
11358 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11359 __ eor(r7, r4, Operand(r5));
11360 __ tst(r7, Operand(kStringEncodingMask));
11361 __ b(ne, &string_add_runtime);
11362 // And see if it's ASCII or two-byte.
11363 __ tst(r4, Operand(kStringEncodingMask));
11364 __ b(eq, &non_ascii_string_add_flat_result);
11365
11366 // Both strings are sequential ASCII strings. We also know that they are
11367 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011368 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011369 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11370 // Locate first character of result.
11371 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11372 // Locate first character of first argument.
11373 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11374 // r0: first character of first string.
11375 // r1: second string.
11376 // r2: length of first string.
11377 // r3: length of second string.
11378 // r6: first character of result.
11379 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011380 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011381
11382 // Load second argument and locate first character.
11383 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11384 // r1: first character of second string.
11385 // r3: length of second string.
11386 // r6: next character of result.
11387 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011388 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011389 __ mov(r0, Operand(r7));
11390 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11391 __ add(sp, sp, Operand(2 * kPointerSize));
11392 __ Ret();
11393
11394 __ bind(&non_ascii_string_add_flat_result);
11395 // Both strings are sequential two byte strings.
11396 // r0: first string.
11397 // r1: second string.
11398 // r2: length of first string.
11399 // r3: length of second string.
11400 // r6: sum of length of strings.
11401 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11402 // r0: first string.
11403 // r1: second string.
11404 // r2: length of first string.
11405 // r3: length of second string.
11406 // r7: result string.
11407
11408 // Locate first character of result.
11409 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11410 // Locate first character of first argument.
11411 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11412
11413 // r0: first character of first string.
11414 // r1: second string.
11415 // r2: length of first string.
11416 // r3: length of second string.
11417 // r6: first character of result.
11418 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011419 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011420
11421 // Locate first character of second argument.
11422 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11423
11424 // r1: first character of second string.
11425 // r3: length of second string.
11426 // r6: next character of result (after copy of first string).
11427 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011428 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011429
11430 __ mov(r0, Operand(r7));
11431 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11432 __ add(sp, sp, Operand(2 * kPointerSize));
11433 __ Ret();
11434
11435 // Just jump to runtime to add the two strings.
11436 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011437 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011438}
11439
11440
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011441#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011443} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011444
11445#endif // V8_TARGET_ARCH_ARM