blob: 8e87614c966813caf9081148ae35685ad13d3fb8 [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,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000057 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000058 Label* slow,
59 bool strict);
60static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
61static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000062static void MultiplyByKnownInt(MacroAssembler* masm,
63 Register source,
64 Register destination,
65 int known_int);
66static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000067
68
ricow@chromium.org30ce4112010-05-31 10:38:25 +000069#define __ ACCESS_MASM(masm_)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000070
ager@chromium.orge2902be2009-06-08 12:21:35 +000071// -------------------------------------------------------------------------
72// Platform-specific DeferredCode functions.
73
74void DeferredCode::SaveRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000075 // On ARM you either have a completely spilled frame or you
76 // handle it yourself, but at the moment there's no automation
77 // of registers and deferred code.
ager@chromium.orge2902be2009-06-08 12:21:35 +000078}
79
80
81void DeferredCode::RestoreRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000082}
83
84
85// -------------------------------------------------------------------------
86// Platform-specific RuntimeCallHelper functions.
87
88void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
89 frame_state_->frame()->AssertIsSpilled();
90}
91
92
93void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
94}
95
96
97void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
98 masm->EnterInternalFrame();
99}
100
101
102void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
103 masm->LeaveInternalFrame();
ager@chromium.orge2902be2009-06-08 12:21:35 +0000104}
105
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000106
107// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108// CodeGenState implementation.
109
ager@chromium.org7c537e22008-10-16 08:43:32 +0000110CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000111 : owner_(owner),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000112 previous_(owner->state()) {
113 owner->set_state(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000114}
115
116
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000117ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner,
118 JumpTarget* true_target,
119 JumpTarget* false_target)
120 : CodeGenState(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000121 true_target_(true_target),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000122 false_target_(false_target) {
123 owner->set_state(this);
124}
125
126
127TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner,
128 Slot* slot,
129 TypeInfo type_info)
130 : CodeGenState(owner),
131 slot_(slot) {
132 owner->set_state(this);
133 old_type_info_ = owner->set_type_info(slot, type_info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000134}
135
136
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000137CodeGenState::~CodeGenState() {
138 ASSERT(owner_->state() == this);
139 owner_->set_state(previous_);
140}
141
142
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000143TypeInfoCodeGenState::~TypeInfoCodeGenState() {
144 owner()->set_type_info(slot_, old_type_info_);
145}
146
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000147// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000148// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149
ager@chromium.org5c838252010-02-19 08:53:10 +0000150CodeGenerator::CodeGenerator(MacroAssembler* masm)
151 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000152 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000153 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000154 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000155 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 cc_reg_(al),
157 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000158 loop_nesting_(0),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000159 type_info_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000160 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161}
162
163
164// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000165// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000167// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168// cp: callee's context
169
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000170void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000171 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000172 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000173 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174
175 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000176 info_ = info;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000177
178 int slots = scope()->num_parameters() + scope()->num_stack_slots();
179 ScopedVector<TypeInfo> type_info_array(slots);
180 type_info_ = &type_info_array;
181
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 ASSERT(allocator_ == NULL);
183 RegisterAllocator register_allocator(this);
184 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000185 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000186 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000187 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000188
189 // Adjust for function-level loop nesting.
190 ASSERT_EQ(0, loop_nesting_);
191 loop_nesting_ = info->loop_nesting();
192
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000193 {
194 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000196 // Entry:
197 // Stack: receiver, arguments
198 // lr: return address
199 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000201 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000203 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205#ifdef DEBUG
206 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000207 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000208 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000209 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 }
211#endif
212
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000213 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000214 frame_->Enter();
215 // tos: code slot
216
217 // Allocate space for locals and initialize them. This also checks
218 // for stack overflow.
219 frame_->AllocateStackSlots();
220
ager@chromium.org357bf652010-04-12 11:30:10 +0000221 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000222 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000223 if (heap_slots > 0) {
224 // Allocate local context.
225 // Get outer context and create a new context based on it.
226 __ ldr(r0, frame_->Function());
227 frame_->EmitPush(r0);
228 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
229 FastNewContextStub stub(heap_slots);
230 frame_->CallStub(&stub, 1);
231 } else {
232 frame_->CallRuntime(Runtime::kNewContext, 1);
233 }
234
235#ifdef DEBUG
236 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000237 __ cmp(r0, cp);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000238 verified_true.Branch(eq);
239 __ stop("NewContext: r0 is expected to be the same as cp");
240 verified_true.Bind();
241#endif
242 // Update context local.
243 __ str(cp, frame_->Context());
244 }
245
246 // TODO(1241774): Improve this code:
247 // 1) only needed if we have a context
248 // 2) no need to recompute context ptr every single time
249 // 3) don't copy parameter operand code from SlotOperand!
250 {
251 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000252 // Note that iteration order is relevant here! If we have the same
253 // parameter twice (e.g., function (x, y, x)), and that parameter
254 // needs to be copied into the context, it must be the last argument
255 // passed to the parameter that needs to be copied. This is a rare
256 // case so we don't check for it, instead we rely on the copying
257 // order: such a parameter is copied repeatedly into the same
258 // context location and thus the last value is what is seen inside
259 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000260 for (int i = 0; i < scope()->num_parameters(); i++) {
261 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000262 Slot* slot = par->slot();
263 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000264 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000265 __ ldr(r1, frame_->ParameterAt(i));
266 // Loads r2 with context; used below in RecordWrite.
267 __ str(r1, SlotOperand(slot, r2));
268 // Load the offset into r3.
269 int slot_offset =
270 FixedArray::kHeaderSize + slot->index() * kPointerSize;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000271 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 }
273 }
274 }
275
276 // Store the arguments object. This must happen after context
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000277 // initialization because the arguments object may be stored in
278 // the context.
279 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
280 StoreArgumentsObject(true);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000281 }
282
283 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000284 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000285 __ mov(ip, Operand(Factory::the_hole_value()));
286 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000287 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000288 }
289 } else {
290 // When used as the secondary compiler for splitting, r1, cp,
291 // fp, and lr have been pushed on the stack. Adjust the virtual
292 // frame to match this state.
293 frame_->Adjust(4);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000294
295 // Bind all the bailout labels to the beginning of the function.
296 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
297 for (int i = 0; i < bailouts->length(); i++) {
298 __ bind(bailouts->at(i)->label());
299 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000300 }
301
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000302 // Initialize the function return target after the locals are set
303 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000304 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000305 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000307 // Generate code to 'execute' declarations and initialize functions
308 // (source elements). In case of an illegal redeclaration we need to
309 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000310 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000312 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 } else {
314 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000315 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000316 // Bail out if a stack-overflow exception occurred when processing
317 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000318 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 }
320
mads.s.ager31e71382008-08-13 09:32:07 +0000321 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000322 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000323 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325
326 // Compile the body of the function in a vanilla state. Don't
327 // bother compiling all the code if the scope has an illegal
328 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000329 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 Comment cmnt(masm_, "[ function body");
331#ifdef DEBUG
332 bool is_builtin = Bootstrapper::IsActive();
333 bool should_trace =
334 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000335 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000337 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000338 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000340 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 }
343
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000344 // Handle the return from the function.
345 if (has_valid_frame()) {
346 // If there is a valid frame, control flow can fall off the end of
347 // the body. In that case there is an implicit return statement.
348 ASSERT(!function_return_is_shadowed_);
349 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000350 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000351 if (function_return_.is_bound()) {
352 function_return_.Jump();
353 } else {
354 function_return_.Bind();
355 GenerateReturnSequence();
356 }
357 } else if (function_return_.is_linked()) {
358 // If the return target has dangling jumps to it, then we have not
359 // yet generated the return sequence. This can happen when (a)
360 // control does not flow off the end of the body so we did not
361 // compile an artificial return statement just above, and (b) there
362 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000363 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000364 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000365 }
366
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000367 // Adjust for function-level loop nesting.
368 ASSERT(loop_nesting_ == info->loop_nesting());
369 loop_nesting_ = 0;
370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 ASSERT(!has_cc());
373 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000374 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 ASSERT(!function_return_is_shadowed_);
376 function_return_.Unuse();
377 DeleteFrame();
378
379 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000380 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000381 ProcessDeferred();
382 }
383
384 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000385 type_info_ = NULL;
386}
387
388
389int CodeGenerator::NumberOfSlot(Slot* slot) {
390 if (slot == NULL) return kInvalidSlotNumber;
391 switch (slot->type()) {
392 case Slot::PARAMETER:
393 return slot->index();
394 case Slot::LOCAL:
395 return slot->index() + scope()->num_parameters();
396 default:
397 break;
398 }
399 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400}
401
402
ager@chromium.org7c537e22008-10-16 08:43:32 +0000403MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
404 // Currently, this assertion will fail if we try to assign to
405 // a constant variable that is constant because it is read-only
406 // (such as the variable referring to a named function expression).
407 // We need to implement assignments to read-only variables.
408 // Ideally, we should do this during AST generation (by converting
409 // such assignments into expression statements); however, in general
410 // we may not be able to make the decision until past AST generation,
411 // that is when the entire program is known.
412 ASSERT(slot != NULL);
413 int index = slot->index();
414 switch (slot->type()) {
415 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000416 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000417
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000418 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000419 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000420
421 case Slot::CONTEXT: {
422 // Follow the context chain if necessary.
423 ASSERT(!tmp.is(cp)); // do not overwrite context register
424 Register context = cp;
425 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000426 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000427 // Load the closure.
428 // (All contexts, even 'with' contexts, have a closure,
429 // and it is the same for all contexts inside a function.
430 // There is no need to go to the function context first.)
431 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
432 // Load the function context (which is the incoming, outer context).
433 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
434 context = tmp;
435 }
436 // We may have a 'with' context now. Get the function context.
437 // (In fact this mov may never be the needed, since the scope analysis
438 // may not permit a direct context access in this case and thus we are
439 // always at a function context. However it is safe to dereference be-
440 // cause the function context of a function context is itself. Before
441 // deleting this mov we should try to create a counter-example first,
442 // though...)
443 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
444 return ContextOperand(tmp, index);
445 }
446
447 default:
448 UNREACHABLE();
449 return MemOperand(r0, 0);
450 }
451}
452
453
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
455 Slot* slot,
456 Register tmp,
457 Register tmp2,
458 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000460 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461
ager@chromium.org381abbb2009-02-25 13:23:22 +0000462 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
463 if (s->num_heap_slots() > 0) {
464 if (s->calls_eval()) {
465 // Check that extension is NULL.
466 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
467 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000469 }
470 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
471 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
472 context = tmp;
473 }
474 }
475 // Check that last extension is NULL.
476 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
477 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000478 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000479 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000481}
482
483
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000484// Loads a value on TOS. If it is a boolean value, the result may have been
485// (partially) translated into branches, or it may have set the condition
486// code register. If force_cc is set, the value is forced to set the
487// condition code register and no value is pushed. If the condition code
488// register was set, has_cc() is true and cc_reg_ contains the condition to
489// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000490void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 JumpTarget* true_target,
492 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000493 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000494 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000497 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000498 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499
500 // If we hit a stack overflow, we may not have actually visited
501 // the expression. In that case, we ensure that we have a
502 // valid-looking frame state because we will continue to generate
503 // code as we unwind the C++ stack.
504 //
505 // It's possible to have both a stack overflow and a valid frame
506 // state (eg, a subexpression overflowed, visiting it returned
507 // with a dummied frame state, and visiting this expression
508 // returned with a normal-looking state).
509 if (HasStackOverflow() &&
510 has_valid_frame() &&
511 !has_cc() &&
512 frame_->height() == original_height) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000513 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 true_target->Jump();
515 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000516 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000517 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000518 // Convert the TOS value to a boolean in the condition code register.
519 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521 ASSERT(!force_cc || !has_valid_frame() || has_cc());
522 ASSERT(!has_valid_frame() ||
523 (has_cc() && frame_->height() == original_height) ||
524 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525}
526
527
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000528void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000529#ifdef DEBUG
530 int original_height = frame_->height();
531#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000532 JumpTarget true_target;
533 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000534 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535
536 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 // Convert cc_reg_ into a boolean value.
ager@chromium.org357bf652010-04-12 11:30:10 +0000538 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000539 JumpTarget loaded;
540 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000542 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 frame_->EmitPush(r0);
544 loaded.Jump();
545 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000546 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 frame_->EmitPush(r0);
548 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 cc_reg_ = al;
550 }
551
552 if (true_target.is_linked() || false_target.is_linked()) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000553 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 // We have at least one condition value that has been "translated"
555 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000556 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000557 if (frame_ != NULL) {
558 loaded.Jump(); // Don't lose the current TOS.
559 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000564 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000565 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 // If both "true" and "false" need to be loaded jump across the code for
568 // "false".
569 if (both) {
570 loaded.Jump();
571 }
572 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000574 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000575 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000576 frame_->EmitPush(r0);
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) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000595 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000596 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
597 __ ldr(scratch,
598 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000599 frame_->EmitPush(scratch);
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) {
616 VirtualFrame::SpilledScope spilled_scope(frame_);
617
618 ArgumentsAllocationMode mode = ArgumentsMode();
619 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
620
621 Comment cmnt(masm_, "[ store arguments object");
622 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
623 // When using lazy arguments allocation, we store the hole value
624 // as a sentinel indicating that the arguments object hasn't been
625 // allocated yet.
626 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
627 frame_->EmitPush(ip);
628 } else {
629 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
630 __ ldr(r2, frame_->Function());
631 // The receiver is below the arguments, the return address, and the
632 // frame pointer on the stack.
633 const int kReceiverDisplacement = 2 + scope()->num_parameters();
634 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
635 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
636 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000637 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000638 frame_->CallStub(&stub, 3);
639 frame_->EmitPush(r0);
640 }
641
642 Variable* arguments = scope()->arguments()->var();
643 Variable* shadow = scope()->arguments_shadow()->var();
644 ASSERT(arguments != NULL && arguments->slot() != NULL);
645 ASSERT(shadow != NULL && shadow->slot() != NULL);
646 JumpTarget done;
647 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
648 // We have to skip storing into the arguments slot if it has
649 // already been written to. This can happen if the a function
650 // has a local variable named 'arguments'.
651 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
652 frame_->EmitPop(r0);
653 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
654 __ cmp(r0, ip);
655 done.Branch(ne);
656 }
657 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
658 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
659 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
660}
661
662
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000663void CodeGenerator::LoadTypeofExpression(Expression* expr) {
664 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000665 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000667 // For a global variable we build the property reference
668 // <global>.<variable> and perform a (regular non-contextual) property
669 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
671 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000672 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000673 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000674 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000675 } else if (variable != NULL && variable->slot() != NULL) {
676 // For a variable that rewrites to a slot, we signal it is the immediate
677 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000678 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000680 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000681 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 }
683}
684
685
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000686Reference::Reference(CodeGenerator* cgen,
687 Expression* expression,
688 bool persist_after_get)
689 : cgen_(cgen),
690 expression_(expression),
691 type_(ILLEGAL),
692 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 cgen->LoadReference(this);
694}
695
696
697Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000698 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699}
700
701
ager@chromium.org7c537e22008-10-16 08:43:32 +0000702void CodeGenerator::LoadReference(Reference* ref) {
703 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 Expression* e = ref->expression();
705 Property* property = e->AsProperty();
706 Variable* var = e->AsVariableProxy()->AsVariable();
707
708 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000709 // The expression is either a property or a variable proxy that rewrites
710 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000711 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000712 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 ref->set_type(Reference::NAMED);
714 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000715 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 ref->set_type(Reference::KEYED);
717 }
718 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000719 // The expression is a variable proxy that does not rewrite to a
720 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 LoadGlobal();
723 ref->set_type(Reference::NAMED);
724 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000725 ASSERT(var->slot() != NULL);
726 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 }
728 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000729 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000730 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000731 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 }
733}
734
735
ager@chromium.org7c537e22008-10-16 08:43:32 +0000736void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000738 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000739 if (size == 0) return;
740
741 // Pop a reference from the stack while preserving TOS.
742 VirtualFrame::RegisterAllocationScope scope(this);
743 Comment cmnt(masm_, "[ UnloadReference");
744 if (size > 0) {
745 Register tos = frame_->PopToRegister();
746 frame_->Drop(size);
747 frame_->EmitPush(tos);
748 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749}
750
751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
753// register to a boolean in the condition code register. The code
754// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755void CodeGenerator::ToBoolean(JumpTarget* true_target,
756 JumpTarget* false_target) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000757 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000758 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
762 // Fast case checks
763
mads.s.ager31e71382008-08-13 09:32:07 +0000764 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000765 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
766 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000767 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768
mads.s.ager31e71382008-08-13 09:32:07 +0000769 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000770 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
771 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000772 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
mads.s.ager31e71382008-08-13 09:32:07 +0000774 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000775 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
776 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000777 false_target->Branch(eq);
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.
780 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000781 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000782 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
785 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000786 frame_->EmitPush(r0);
787 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000788 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000789 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
790 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791
792 cc_reg_ = ne;
793}
794
795
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000796void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000797 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000798 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000799 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000800 // top of virtual frame: y
801 // 2nd elt. on virtual frame : x
802 // result : top of virtual frame
803
804 // Stub is entered with a call: 'return address' is in lr.
805 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000806 case Token::ADD:
807 case Token::SUB:
808 if (inline_smi) {
809 JumpTarget done;
810 Register rhs = frame_->PopToRegister();
811 Register lhs = frame_->PopToRegister(rhs);
812 Register scratch = VirtualFrame::scratch0();
813 __ orr(scratch, rhs, Operand(lhs));
814 // Check they are both small and positive.
815 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
816 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
817 ASSERT_EQ(0, kSmiTag);
818 if (op == Token::ADD) {
819 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
820 } else {
821 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
822 }
823 done.Branch(eq);
824 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
825 frame_->SpillAll();
826 frame_->CallStub(&stub, 0);
827 done.Bind();
828 frame_->EmitPush(r0);
829 break;
830 } else {
831 // Fall through!
832 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000833 case Token::BIT_OR:
834 case Token::BIT_AND:
835 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000836 if (inline_smi) {
837 bool rhs_is_smi = frame_->KnownSmiAt(0);
838 bool lhs_is_smi = frame_->KnownSmiAt(1);
839 Register rhs = frame_->PopToRegister();
840 Register lhs = frame_->PopToRegister(rhs);
841 Register smi_test_reg;
842 Condition cond;
843 if (!rhs_is_smi || !lhs_is_smi) {
844 if (rhs_is_smi) {
845 smi_test_reg = lhs;
846 } else if (lhs_is_smi) {
847 smi_test_reg = rhs;
848 } else {
849 smi_test_reg = VirtualFrame::scratch0();
850 __ orr(smi_test_reg, rhs, Operand(lhs));
851 }
852 // Check they are both Smis.
853 __ tst(smi_test_reg, Operand(kSmiTagMask));
854 cond = eq;
855 } else {
856 cond = al;
857 }
858 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
859 if (op == Token::BIT_OR) {
860 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
861 } else if (op == Token::BIT_AND) {
862 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
863 } else {
864 ASSERT(op == Token::BIT_XOR);
865 ASSERT_EQ(0, kSmiTag);
866 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
867 }
868 if (cond != al) {
869 JumpTarget done;
870 done.Branch(cond);
871 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
872 frame_->SpillAll();
873 frame_->CallStub(&stub, 0);
874 done.Bind();
875 }
876 frame_->EmitPush(r0);
877 break;
878 } else {
879 // Fall through!
880 }
881 case Token::MUL:
882 case Token::DIV:
883 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000884 case Token::SHL:
885 case Token::SHR:
886 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000887 Register rhs = frame_->PopToRegister();
888 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000889 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
890 frame_->SpillAll();
891 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000892 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 break;
894 }
895
ager@chromium.org357bf652010-04-12 11:30:10 +0000896 case Token::COMMA: {
897 Register scratch = frame_->PopToRegister();
898 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000899 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000900 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000902 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
904 default:
905 // Other cases should have been handled before this point.
906 UNREACHABLE();
907 break;
908 }
909}
910
911
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000912class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000913 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000914 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000915 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000916 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000917 OverwriteMode overwrite_mode,
918 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000919 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000920 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000921 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000922 overwrite_mode_(overwrite_mode),
923 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000924 set_comment("[ DeferredInlinedSmiOperation");
925 }
926
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000927 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000928
929 private:
930 Token::Value op_;
931 int value_;
932 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000933 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000934 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935};
936
937
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000938void DeferredInlineSmiOperation::Generate() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000939 Register lhs = r1;
940 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000941 switch (op_) {
942 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000943 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000945 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000946 __ mov(r1, Operand(Smi::FromInt(value_)));
947 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000948 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000949 __ mov(r0, Operand(Smi::FromInt(value_)));
950 }
951 break;
952 }
953
954 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000955 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000956 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000957 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000958 __ mov(r1, Operand(Smi::FromInt(value_)));
959 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000960 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000961 __ mov(r0, Operand(Smi::FromInt(value_)));
962 }
963 break;
964 }
965
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000966 // For these operations there is no optimistic operation that needs to be
967 // reverted.
968 case Token::MUL:
969 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000970 case Token::BIT_OR:
971 case Token::BIT_XOR:
972 case Token::BIT_AND: {
973 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000974 if (tos_register_.is(r0)) {
975 __ mov(r1, Operand(Smi::FromInt(value_)));
976 } else {
977 ASSERT(tos_register_.is(r1));
978 __ mov(r0, Operand(Smi::FromInt(value_)));
979 lhs = r0;
980 rhs = r1;
981 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000982 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000983 if (tos_register_.is(r1)) {
984 __ mov(r0, Operand(Smi::FromInt(value_)));
985 } else {
986 ASSERT(tos_register_.is(r0));
987 __ mov(r1, Operand(Smi::FromInt(value_)));
988 lhs = r0;
989 rhs = r1;
990 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000991 }
992 break;
993 }
994
995 case Token::SHL:
996 case Token::SHR:
997 case Token::SAR: {
998 if (!reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000999 if (tos_register_.is(r1)) {
1000 __ mov(r0, Operand(Smi::FromInt(value_)));
1001 } else {
1002 ASSERT(tos_register_.is(r0));
1003 __ mov(r1, Operand(Smi::FromInt(value_)));
1004 lhs = r0;
1005 rhs = r1;
1006 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001007 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001008 ASSERT(op_ == Token::SHL);
1009 __ mov(r1, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001010 }
1011 break;
1012 }
1013
1014 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +00001015 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001016 UNREACHABLE();
1017 break;
1018 }
1019
ager@chromium.org357bf652010-04-12 11:30:10 +00001020 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001021 __ CallStub(&stub);
ager@chromium.org357bf652010-04-12 11:30:10 +00001022 // The generic stub returns its value in r0, but that's not
1023 // necessarily what we want. We want whatever the inlined code
1024 // expected, which is that the answer is in the same register as
1025 // the operand was.
1026 __ Move(tos_register_, r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001027}
1028
1029
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001030static bool PopCountLessThanEqual2(unsigned int x) {
1031 x &= x - 1;
1032 return (x & (x - 1)) == 0;
1033}
1034
1035
1036// Returns the index of the lowest bit set.
1037static int BitPosition(unsigned x) {
1038 int bit_posn = 0;
1039 while ((x & 0xf) == 0) {
1040 bit_posn += 4;
1041 x >>= 4;
1042 }
1043 while ((x & 1) == 0) {
1044 bit_posn++;
1045 x >>= 1;
1046 }
1047 return bit_posn;
1048}
1049
1050
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001051void CodeGenerator::SmiOperation(Token::Value op,
1052 Handle<Object> value,
1053 bool reversed,
1054 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001055 int int_value = Smi::cast(*value)->value();
1056
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001057 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1058
ager@chromium.org357bf652010-04-12 11:30:10 +00001059 bool something_to_inline;
1060 switch (op) {
1061 case Token::ADD:
1062 case Token::SUB:
1063 case Token::BIT_AND:
1064 case Token::BIT_OR:
1065 case Token::BIT_XOR: {
1066 something_to_inline = true;
1067 break;
1068 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001069 case Token::SHL: {
1070 something_to_inline = (both_sides_are_smi || !reversed);
1071 break;
1072 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001073 case Token::SHR:
1074 case Token::SAR: {
1075 if (reversed) {
1076 something_to_inline = false;
1077 } else {
1078 something_to_inline = true;
1079 }
1080 break;
1081 }
1082 case Token::MOD: {
1083 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1084 something_to_inline = false;
1085 } else {
1086 something_to_inline = true;
1087 }
1088 break;
1089 }
1090 case Token::MUL: {
1091 if (!IsEasyToMultiplyBy(int_value)) {
1092 something_to_inline = false;
1093 } else {
1094 something_to_inline = true;
1095 }
1096 break;
1097 }
1098 default: {
1099 something_to_inline = false;
1100 break;
1101 }
1102 }
1103
1104 if (!something_to_inline) {
1105 if (!reversed) {
1106 // Push the rhs onto the virtual frame by putting it in a TOS register.
1107 Register rhs = frame_->GetTOSRegister();
1108 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001109 frame_->EmitPush(rhs, TypeInfo::Smi());
1110 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001111 } else {
1112 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1113 // at most one pop, the rest takes place in TOS registers.
1114 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1115 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1116 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001117 frame_->EmitPush(lhs, TypeInfo::Smi());
1118 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1119 frame_->EmitPush(rhs, t);
1120 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001121 }
1122 return;
1123 }
1124
1125 // We move the top of stack to a register (normally no move is invoved).
1126 Register tos = frame_->PopToRegister();
1127 // All other registers are spilled. The deferred code expects one argument
1128 // in a register and all other values are flushed to the stack. The
1129 // answer is returned in the same register that the top of stack argument was
1130 // in.
1131 frame_->SpillAll();
1132
1133 switch (op) {
1134 case Token::ADD: {
1135 DeferredCode* deferred =
1136 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1137
1138 __ add(tos, tos, Operand(value), SetCC);
1139 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001140 if (!both_sides_are_smi) {
1141 __ tst(tos, Operand(kSmiTagMask));
1142 deferred->Branch(ne);
1143 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001144 deferred->BindExit();
1145 frame_->EmitPush(tos);
1146 break;
1147 }
1148
1149 case Token::SUB: {
1150 DeferredCode* deferred =
1151 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1152
1153 if (reversed) {
1154 __ rsb(tos, tos, Operand(value), SetCC);
1155 } else {
1156 __ sub(tos, tos, Operand(value), SetCC);
1157 }
1158 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001159 if (!both_sides_are_smi) {
1160 __ tst(tos, Operand(kSmiTagMask));
1161 deferred->Branch(ne);
1162 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001163 deferred->BindExit();
1164 frame_->EmitPush(tos);
1165 break;
1166 }
1167
1168
1169 case Token::BIT_OR:
1170 case Token::BIT_XOR:
1171 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001172 if (both_sides_are_smi) {
1173 switch (op) {
1174 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1175 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001176 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001177 default: UNREACHABLE();
1178 }
1179 frame_->EmitPush(tos, TypeInfo::Smi());
1180 } else {
1181 DeferredCode* deferred =
1182 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1183 __ tst(tos, Operand(kSmiTagMask));
1184 deferred->Branch(ne);
1185 switch (op) {
1186 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1187 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001188 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001189 default: UNREACHABLE();
1190 }
1191 deferred->BindExit();
1192 TypeInfo result_type =
1193 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1194 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001195 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001196 break;
1197 }
1198
1199 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001200 if (reversed) {
1201 ASSERT(both_sides_are_smi);
1202 int max_shift = 0;
1203 int max_result = int_value == 0 ? 1 : int_value;
1204 while (Smi::IsValid(max_result << 1)) {
1205 max_shift++;
1206 max_result <<= 1;
1207 }
1208 DeferredCode* deferred =
1209 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1210 // Mask off the last 5 bits of the shift operand (rhs). This is part
1211 // of the definition of shift in JS and we know we have a Smi so we
1212 // can safely do this. The masked version gets passed to the
1213 // deferred code, but that makes no difference.
1214 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1215 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1216 deferred->Branch(ge);
1217 Register scratch = VirtualFrame::scratch0();
1218 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1219 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1220 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1221 deferred->BindExit();
1222 TypeInfo result = TypeInfo::Integer32();
1223 frame_->EmitPush(tos, result);
1224 break;
1225 }
1226 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001227 case Token::SHR:
1228 case Token::SAR: {
1229 ASSERT(!reversed);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001230 TypeInfo result = TypeInfo::Integer32();
ager@chromium.org357bf652010-04-12 11:30:10 +00001231 Register scratch = VirtualFrame::scratch0();
1232 Register scratch2 = VirtualFrame::scratch1();
1233 int shift_value = int_value & 0x1f; // least significant 5 bits
1234 DeferredCode* deferred =
1235 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001236 uint32_t problematic_mask = kSmiTagMask;
1237 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001238 bool skip_smi_test = both_sides_are_smi;
1239 if (shift_value == 0 && op == Token::SHR) {
1240 problematic_mask |= 0x80000000;
1241 skip_smi_test = false;
1242 }
1243 if (!skip_smi_test) {
1244 __ tst(tos, Operand(problematic_mask));
1245 deferred->Branch(ne); // Go slow for problematic input.
1246 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001247 switch (op) {
1248 case Token::SHL: {
1249 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001250 int adjusted_shift = shift_value - kSmiTagSize;
1251 ASSERT(adjusted_shift >= 0);
1252 if (adjusted_shift != 0) {
1253 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1254 // Check that the *signed* result fits in a smi.
1255 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1256 deferred->Branch(mi);
1257 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1258 } else {
1259 // Check that the *signed* result fits in a smi.
1260 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1261 deferred->Branch(mi);
1262 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1263 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001264 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001265 break;
1266 }
1267 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001268 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001269 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1270 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001271 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001272 if (shift_value == 1) {
1273 // check that the *unsigned* result fits in a smi
1274 // neither of the two high-order bits can be set:
1275 // - 0x80000000: high bit would be lost when smi tagging
1276 // - 0x40000000: this number would convert to negative when
1277 // smi tagging these two cases can only happen with shifts
1278 // by 0 or 1 when handed a valid smi
1279 __ tst(scratch, Operand(0xc0000000));
1280 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001281 } else {
1282 ASSERT(shift_value >= 2);
1283 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001284 }
1285 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001286 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001287 break;
1288 }
1289 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001290 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1291 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001292 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001293 // Do the shift and the tag removal in one operation. If the shift
1294 // is 31 bits (the highest possible value) then we emit the
1295 // instruction as a shift by 0 which means shift arithmetically by
1296 // 32.
1297 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1298 // Put tag back.
1299 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001300 // SAR by at least 1 gives a Smi.
1301 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001302 }
1303 break;
1304 }
1305 default: UNREACHABLE();
1306 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001307 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001308 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001309 break;
1310 }
1311
1312 case Token::MOD: {
1313 ASSERT(!reversed);
1314 ASSERT(int_value >= 2);
1315 ASSERT(IsPowerOf2(int_value));
1316 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001317 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001318 unsigned mask = (0x80000000u | kSmiTagMask);
1319 __ tst(tos, Operand(mask));
1320 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1321 mask = (int_value << kSmiTagSize) - 1;
1322 __ and_(tos, tos, Operand(mask));
1323 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001324 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1325 frame_->EmitPush(
1326 tos,
1327 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001328 break;
1329 }
1330
1331 case Token::MUL: {
1332 ASSERT(IsEasyToMultiplyBy(int_value));
1333 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001334 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001335 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1336 max_smi_that_wont_overflow <<= kSmiTagSize;
1337 unsigned mask = 0x80000000u;
1338 while ((mask & max_smi_that_wont_overflow) == 0) {
1339 mask |= mask >> 1;
1340 }
1341 mask |= kSmiTagMask;
1342 // This does a single mask that checks for a too high value in a
1343 // conservative way and for a non-Smi. It also filters out negative
1344 // numbers, unfortunately, but since this code is inline we prefer
1345 // brevity to comprehensiveness.
1346 __ tst(tos, Operand(mask));
1347 deferred->Branch(ne);
1348 MultiplyByKnownInt(masm_, tos, tos, int_value);
1349 deferred->BindExit();
1350 frame_->EmitPush(tos);
1351 break;
1352 }
1353
1354 default:
1355 UNREACHABLE();
1356 break;
1357 }
1358}
1359
1360
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001361void CodeGenerator::Comparison(Condition cc,
1362 Expression* left,
1363 Expression* right,
1364 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001365 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001366
ager@chromium.org357bf652010-04-12 11:30:10 +00001367 if (left != NULL) Load(left);
1368 if (right != NULL) Load(right);
1369
mads.s.ager31e71382008-08-13 09:32:07 +00001370 // sp[0] : y
1371 // sp[1] : x
1372 // result : cc register
1373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 // Strict only makes sense for equality comparisons.
1375 ASSERT(!strict || cc == eq);
1376
ager@chromium.org357bf652010-04-12 11:30:10 +00001377 Register lhs;
1378 Register rhs;
1379
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001380 bool lhs_is_smi;
1381 bool rhs_is_smi;
1382
ager@chromium.org357bf652010-04-12 11:30:10 +00001383 // We load the top two stack positions into registers chosen by the virtual
1384 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001385 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1386 if (cc == gt || cc == le) {
1387 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001388 lhs_is_smi = frame_->KnownSmiAt(0);
1389 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001390 lhs = frame_->PopToRegister();
1391 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001392 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001393 rhs_is_smi = frame_->KnownSmiAt(0);
1394 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001395 rhs = frame_->PopToRegister();
1396 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001397 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001398
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001399 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1400
ager@chromium.org357bf652010-04-12 11:30:10 +00001401 ASSERT(rhs.is(r0) || rhs.is(r1));
1402 ASSERT(lhs.is(r0) || lhs.is(r1));
1403
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001404 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001406 if (!both_sides_are_smi) {
1407 // Now we have the two sides in r0 and r1. We flush any other registers
1408 // because the stub doesn't know about register allocation.
1409 frame_->SpillAll();
1410 Register scratch = VirtualFrame::scratch0();
1411 Register smi_test_reg;
1412 if (lhs_is_smi) {
1413 smi_test_reg = rhs;
1414 } else if (rhs_is_smi) {
1415 smi_test_reg = lhs;
1416 } else {
1417 __ orr(scratch, lhs, Operand(rhs));
1418 smi_test_reg = scratch;
1419 }
1420 __ tst(smi_test_reg, Operand(kSmiTagMask));
1421 JumpTarget smi;
1422 smi.Branch(eq);
1423
1424 // Perform non-smi comparison by stub.
1425 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1426 // We call with 0 args because there are 0 on the stack.
1427 if (!rhs.is(r0)) {
1428 __ Swap(rhs, lhs, ip);
1429 }
1430
1431 CompareStub stub(cc, strict);
1432 frame_->CallStub(&stub, 0);
1433 __ cmp(r0, Operand(0));
1434 exit.Jump();
1435
1436 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001437 }
1438
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001439 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001440 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001442 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 cc_reg_ = cc;
1444}
1445
1446
mads.s.ager31e71382008-08-13 09:32:07 +00001447// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001448void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001449 CallFunctionFlags flags,
1450 int position) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001451 frame_->AssertIsSpilled();
1452
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001454 int arg_count = args->length();
1455 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001456 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001457 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458
kasper.lund7276f142008-07-30 08:49:36 +00001459 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001460 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461
kasper.lund7276f142008-07-30 08:49:36 +00001462 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001463 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466
1467 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001468 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001469 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470}
1471
1472
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001473void CodeGenerator::CallApplyLazy(Expression* applicand,
1474 Expression* receiver,
1475 VariableProxy* arguments,
1476 int position) {
1477 // An optimized implementation of expressions of the form
1478 // x.apply(y, arguments).
1479 // If the arguments object of the scope has not been allocated,
1480 // and x.apply is Function.prototype.apply, this optimization
1481 // just copies y and the arguments of the current function on the
1482 // stack, as receiver and arguments, and calls x.
1483 // In the implementation comments, we call x the applicand
1484 // and y the receiver.
1485 VirtualFrame::SpilledScope spilled_scope(frame_);
1486
1487 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1488 ASSERT(arguments->IsArguments());
1489
1490 // Load applicand.apply onto the stack. This will usually
1491 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001492 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001493 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001494 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001495 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001496 frame_->EmitPush(r0);
1497
1498 // Load the receiver and the existing arguments object onto the
1499 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001500 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001501 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1502
1503 // Emit the source position information after having loaded the
1504 // receiver and the arguments.
1505 CodeForSourcePosition(position);
1506 // Contents of the stack at this point:
1507 // sp[0]: arguments object of the current function or the hole.
1508 // sp[1]: receiver
1509 // sp[2]: applicand.apply
1510 // sp[3]: applicand.
1511
1512 // Check if the arguments object has been lazily allocated
1513 // already. If so, just use that instead of copying the arguments
1514 // from the stack. This also deals with cases where a local variable
1515 // named 'arguments' has been introduced.
1516 __ ldr(r0, MemOperand(sp, 0));
1517
1518 Label slow, done;
1519 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
1520 __ cmp(ip, r0);
1521 __ b(ne, &slow);
1522
1523 Label build_args;
1524 // Get rid of the arguments object probe.
1525 frame_->Drop();
1526 // Stack now has 3 elements on it.
1527 // Contents of stack at this point:
1528 // sp[0]: receiver
1529 // sp[1]: applicand.apply
1530 // sp[2]: applicand.
1531
1532 // Check that the receiver really is a JavaScript object.
1533 __ ldr(r0, MemOperand(sp, 0));
1534 __ BranchOnSmi(r0, &build_args);
1535 // We allow all JSObjects including JSFunctions. As long as
1536 // JS_FUNCTION_TYPE is the last instance type and it is right
1537 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1538 // bound.
1539 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1540 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
1541 __ CompareObjectType(r0, r1, r2, FIRST_JS_OBJECT_TYPE);
1542 __ b(lt, &build_args);
1543
1544 // Check that applicand.apply is Function.prototype.apply.
1545 __ ldr(r0, MemOperand(sp, kPointerSize));
1546 __ BranchOnSmi(r0, &build_args);
1547 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1548 __ b(ne, &build_args);
1549 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
1550 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
1551 __ ldr(r1, FieldMemOperand(r0, SharedFunctionInfo::kCodeOffset));
1552 __ cmp(r1, Operand(apply_code));
1553 __ b(ne, &build_args);
1554
1555 // Check that applicand is a function.
1556 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1557 __ BranchOnSmi(r1, &build_args);
1558 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1559 __ b(ne, &build_args);
1560
1561 // Copy the arguments to this function possibly from the
1562 // adaptor frame below it.
1563 Label invoke, adapted;
1564 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1565 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1566 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1567 __ b(eq, &adapted);
1568
1569 // No arguments adaptor frame. Copy fixed number of arguments.
1570 __ mov(r0, Operand(scope()->num_parameters()));
1571 for (int i = 0; i < scope()->num_parameters(); i++) {
1572 __ ldr(r2, frame_->ParameterAt(i));
1573 __ push(r2);
1574 }
1575 __ jmp(&invoke);
1576
1577 // Arguments adaptor frame present. Copy arguments from there, but
1578 // avoid copying too many arguments to avoid stack overflows.
1579 __ bind(&adapted);
1580 static const uint32_t kArgumentsLimit = 1 * KB;
1581 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1582 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1583 __ mov(r3, r0);
1584 __ cmp(r0, Operand(kArgumentsLimit));
1585 __ b(gt, &build_args);
1586
1587 // Loop through the arguments pushing them onto the execution
1588 // stack. We don't inform the virtual frame of the push, so we don't
1589 // have to worry about getting rid of the elements from the virtual
1590 // frame.
1591 Label loop;
1592 // r3 is a small non-negative integer, due to the test above.
1593 __ cmp(r3, Operand(0));
1594 __ b(eq, &invoke);
1595 // Compute the address of the first argument.
1596 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1597 __ add(r2, r2, Operand(kPointerSize));
1598 __ bind(&loop);
1599 // Post-decrement argument address by kPointerSize on each iteration.
1600 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1601 __ push(r4);
1602 __ sub(r3, r3, Operand(1), SetCC);
1603 __ b(gt, &loop);
1604
1605 // Invoke the function.
1606 __ bind(&invoke);
1607 ParameterCount actual(r0);
1608 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1609 // Drop applicand.apply and applicand from the stack, and push
1610 // the result of the function call, but leave the spilled frame
1611 // unchanged, with 3 elements, so it is correct when we compile the
1612 // slow-case code.
1613 __ add(sp, sp, Operand(2 * kPointerSize));
1614 __ push(r0);
1615 // Stack now has 1 element:
1616 // sp[0]: result
1617 __ jmp(&done);
1618
1619 // Slow-case: Allocate the arguments object since we know it isn't
1620 // there, and fall-through to the slow-case where we call
1621 // applicand.apply.
1622 __ bind(&build_args);
1623 // Stack now has 3 elements, because we have jumped from where:
1624 // sp[0]: receiver
1625 // sp[1]: applicand.apply
1626 // sp[2]: applicand.
1627 StoreArgumentsObject(false);
1628
1629 // Stack and frame now have 4 elements.
1630 __ bind(&slow);
1631
1632 // Generic computation of x.apply(y, args) with no special optimization.
1633 // Flip applicand.apply and applicand on the stack, so
1634 // applicand looks like the receiver of the applicand.apply call.
1635 // Then process it as a normal function call.
1636 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1637 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001638 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001639
1640 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1641 frame_->CallStub(&call_function, 3);
1642 // The function and its two arguments have been dropped.
1643 frame_->Drop(); // Drop the receiver as well.
1644 frame_->EmitPush(r0);
1645 // Stack now has 1 element:
1646 // sp[0]: result
1647 __ bind(&done);
1648
1649 // Restore the context register after a call.
1650 __ ldr(cp, frame_->Context());
1651}
1652
1653
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001654void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001655 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 ASSERT(has_cc());
1657 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001658 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 cc_reg_ = al;
1660}
1661
1662
ager@chromium.org7c537e22008-10-16 08:43:32 +00001663void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001664 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001665 Comment cmnt(masm_, "[ check stack");
1666 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1667 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1668 // the implicit 8 byte offset that always applies to operations with pc and
1669 // gives a return address 12 bytes down.
1670 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1671 masm_->cmp(sp, Operand(ip));
1672 StackCheckStub stub;
1673 // Call the stub if lower.
1674 masm_->mov(pc,
1675 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1676 RelocInfo::CODE_TARGET),
1677 LeaveCC,
1678 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679}
1680
1681
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001682void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1683#ifdef DEBUG
1684 int original_height = frame_->height();
1685#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001686 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001688 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001689 }
1690 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1691}
1692
1693
ager@chromium.org7c537e22008-10-16 08:43:32 +00001694void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001695#ifdef DEBUG
1696 int original_height = frame_->height();
1697#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001698 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001700 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001701 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001702 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001703 if (node->break_target()->is_linked()) {
1704 node->break_target()->Bind();
1705 }
1706 node->break_target()->Unuse();
1707 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708}
1709
1710
ager@chromium.org7c537e22008-10-16 08:43:32 +00001711void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001712 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001713 frame_->EmitPush(Operand(pairs));
1714 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1715
1716 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001717 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001718 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719}
1720
1721
ager@chromium.org7c537e22008-10-16 08:43:32 +00001722void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001723#ifdef DEBUG
1724 int original_height = frame_->height();
1725#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726 Comment cmnt(masm_, "[ Declaration");
1727 Variable* var = node->proxy()->var();
1728 ASSERT(var != NULL); // must have been resolved
1729 Slot* slot = var->slot();
1730
1731 // If it was not possible to allocate the variable at compile time,
1732 // we need to "declare" it at runtime to make sure it actually
1733 // exists in the local context.
1734 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1735 // Variables with a "LOOKUP" slot were introduced as non-locals
1736 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001737 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001740 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 // Declaration nodes are always declared in only two modes.
1742 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1743 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001744 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 // Push initial value, if any.
1746 // Note: For variables we must not push an initial value (such as
1747 // 'undefined') because we may have a (legal) redeclaration and we
1748 // must not destroy the current value.
1749 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001750 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001752 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001754 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001756
1757 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001759 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001760
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001761 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 return;
1763 }
1764
1765 ASSERT(!var->is_global());
1766
1767 // If we have a function or a constant, we need to initialize the variable.
1768 Expression* val = NULL;
1769 if (node->mode() == Variable::CONST) {
1770 val = new Literal(Factory::the_hole_value());
1771 } else {
1772 val = node->fun(); // NULL if we don't have a function
1773 }
1774
1775 if (val != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001776 // Set initial value.
1777 Reference target(this, node->proxy());
1778 Load(val);
1779 target.SetValue(NOT_CONST_INIT);
1780
iposva@chromium.org245aa852009-02-10 00:49:54 +00001781 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001784 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785}
1786
1787
ager@chromium.org7c537e22008-10-16 08:43:32 +00001788void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789#ifdef DEBUG
1790 int original_height = frame_->height();
1791#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 Expression* expression = node->expression();
1795 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001796 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 frame_->Drop();
1798 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799}
1800
1801
ager@chromium.org7c537e22008-10-16 08:43:32 +00001802void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803#ifdef DEBUG
1804 int original_height = frame_->height();
1805#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810}
1811
1812
ager@chromium.org7c537e22008-10-16 08:43:32 +00001813void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814#ifdef DEBUG
1815 int original_height = frame_->height();
1816#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001818 // Generate different code depending on which parts of the if statement
1819 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 bool has_then_stm = node->HasThenStatement();
1821 bool has_else_stm = node->HasElseStatement();
1822
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001825 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001827 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001828 JumpTarget then;
1829 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001831 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 if (frame_ != NULL) {
1833 Branch(false, &else_);
1834 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001836 if (frame_ != NULL || then.is_linked()) {
1837 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001838 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839 }
1840 if (frame_ != NULL) {
1841 exit.Jump();
1842 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 if (else_.is_linked()) {
1845 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001846 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848
1849 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001850 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001852 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001854 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 if (frame_ != NULL) {
1856 Branch(false, &exit);
1857 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 if (frame_ != NULL || then.is_linked()) {
1860 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001861 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863
1864 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001865 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001867 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001869 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 if (frame_ != NULL) {
1871 Branch(true, &exit);
1872 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 if (frame_ != NULL || else_.is_linked()) {
1875 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001876 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878
1879 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001880 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 ASSERT(!has_then_stm && !has_else_stm);
1882 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001883 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001884 if (frame_ != NULL) {
1885 if (has_cc()) {
1886 cc_reg_ = al;
1887 } else {
1888 frame_->Drop();
1889 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 }
1891 }
1892
1893 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 if (exit.is_linked()) {
1895 exit.Bind();
1896 }
1897 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898}
1899
1900
ager@chromium.org7c537e22008-10-16 08:43:32 +00001901void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001902 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 CodeForStatementPosition(node);
1905 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906}
1907
1908
ager@chromium.org7c537e22008-10-16 08:43:32 +00001909void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001910 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 CodeForStatementPosition(node);
1913 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914}
1915
1916
ager@chromium.org7c537e22008-10-16 08:43:32 +00001917void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001918 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001920
ager@chromium.org4af710e2009-09-15 12:20:11 +00001921 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001922 Load(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001924 frame_->EmitPop(r0);
1925 function_return_.Jump();
1926 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001927 // Pop the result from the frame and prepare the frame for
1928 // returning thus making it easier to merge.
1929 frame_->EmitPop(r0);
1930 frame_->PrepareForReturn();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001931 if (function_return_.is_bound()) {
1932 // If the function return label is already bound we reuse the
1933 // code by jumping to the return site.
1934 function_return_.Jump();
1935 } else {
1936 function_return_.Bind();
1937 GenerateReturnSequence();
1938 }
1939 }
1940}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001942
1943void CodeGenerator::GenerateReturnSequence() {
1944 if (FLAG_trace) {
1945 // Push the return value on the stack as the parameter.
1946 // Runtime::TraceExit returns the parameter as it is.
1947 frame_->EmitPush(r0);
1948 frame_->CallRuntime(Runtime::kTraceExit, 1);
1949 }
1950
1951#ifdef DEBUG
1952 // Add a label for checking the size of the code used for returning.
1953 Label check_exit_codesize;
1954 masm_->bind(&check_exit_codesize);
1955#endif
1956 // Make sure that the constant pool is not emitted inside of the return
1957 // sequence.
1958 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1959 // Tear down the frame which will restore the caller's frame pointer and
1960 // the link register.
1961 frame_->Exit();
1962
1963 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1964 // tool from instrumenting as we rely on the code size here.
1965 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1966 masm_->add(sp, sp, Operand(sp_delta));
1967 masm_->Jump(lr);
1968 DeleteFrame();
1969
1970#ifdef DEBUG
1971 // Check that the size of the code used for returning matches what is
1972 // expected by the debugger. If the sp_delts above cannot be encoded in
1973 // the add instruction the add will generate two instructions.
1974 int return_sequence_length =
1975 masm_->InstructionsGeneratedSince(&check_exit_codesize);
1976 CHECK(return_sequence_length ==
1977 Assembler::kJSReturnSequenceInstructions ||
1978 return_sequence_length ==
1979 Assembler::kJSReturnSequenceInstructions + 1);
1980#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001982}
1983
1984
ager@chromium.org7c537e22008-10-16 08:43:32 +00001985void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986#ifdef DEBUG
1987 int original_height = frame_->height();
1988#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001989 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001991 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001992 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001993 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001995 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001996 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001997 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001998#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001999 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002000 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002001 verified_true.Branch(eq);
2002 __ stop("PushContext: r0 is expected to be the same as cp");
2003 verified_true.Bind();
2004#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002006 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002007 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008}
2009
2010
ager@chromium.org7c537e22008-10-16 08:43:32 +00002011void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012#ifdef DEBUG
2013 int original_height = frame_->height();
2014#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002015 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002017 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 // Pop context.
2019 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2020 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002021 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023}
2024
2025
ager@chromium.org7c537e22008-10-16 08:43:32 +00002026void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002027#ifdef DEBUG
2028 int original_height = frame_->height();
2029#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002030 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002032 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002033 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002035 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002036
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002037 JumpTarget next_test;
2038 JumpTarget fall_through;
2039 JumpTarget default_entry;
2040 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 ZoneList<CaseClause*>* cases = node->cases();
2042 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044
2045 for (int i = 0; i < length; i++) {
2046 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048 // Remember the default clause and compile it at the end.
2049 default_clause = clause;
2050 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 }
2052
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002053 Comment cmnt(masm_, "[ Case clause");
2054 // Compile the test.
2055 next_test.Bind();
2056 next_test.Unuse();
2057 // Duplicate TOS.
2058 __ ldr(r0, frame_->Top());
2059 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002060 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002063 // Before entering the body from the test, remove the switch value from
2064 // the stack.
2065 frame_->Drop();
2066
2067 // Label the body so that fall through is enabled.
2068 if (i > 0 && cases->at(i - 1)->is_default()) {
2069 default_exit.Bind();
2070 } else {
2071 fall_through.Bind();
2072 fall_through.Unuse();
2073 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002074 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075
2076 // If control flow can fall through from the body, jump to the next body
2077 // or the end of the statement.
2078 if (frame_ != NULL) {
2079 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2080 default_entry.Jump();
2081 } else {
2082 fall_through.Jump();
2083 }
2084 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 }
2086
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 // The final "test" removes the switch value.
2088 next_test.Bind();
2089 frame_->Drop();
2090
2091 // If there is a default clause, compile it.
2092 if (default_clause != NULL) {
2093 Comment cmnt(masm_, "[ Default clause");
2094 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002095 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096 // If control flow can fall out of the default and there is a case after
2097 // it, jup to that case's body.
2098 if (frame_ != NULL && default_exit.is_bound()) {
2099 default_exit.Jump();
2100 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002101 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002103 if (fall_through.is_linked()) {
2104 fall_through.Bind();
2105 }
2106
2107 if (node->break_target()->is_linked()) {
2108 node->break_target()->Bind();
2109 }
2110 node->break_target()->Unuse();
2111 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112}
2113
2114
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002115void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116#ifdef DEBUG
2117 int original_height = frame_->height();
2118#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002119 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002120 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002122 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002123 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002124 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002126 // Label the top of the loop for the backward CFG edge. If the test
2127 // is always true we can use the continue target, and if the test is
2128 // always false there is no need.
2129 ConditionAnalysis info = AnalyzeCondition(node->cond());
2130 switch (info) {
2131 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002132 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002134 break;
2135 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002136 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002137 break;
2138 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002139 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002140 body.Bind();
2141 break;
2142 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002144 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002145 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002146
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002147 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002148 switch (info) {
2149 case ALWAYS_TRUE:
2150 // If control can fall off the end of the body, jump back to the
2151 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002152 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002153 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002154 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002156 case ALWAYS_FALSE:
2157 // If we have a continue in the body, we only have to bind its
2158 // jump target.
2159 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002162 break;
2163 case DONT_KNOW:
2164 // We have to compile the test expression if it can be reached by
2165 // control flow falling out of the body or via continue.
2166 if (node->continue_target()->is_linked()) {
2167 node->continue_target()->Bind();
2168 }
2169 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002170 Comment cmnt(masm_, "[ DoWhileCondition");
2171 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002172 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002174 // A invalid frame here indicates that control did not
2175 // fall out of the test expression.
2176 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177 }
2178 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 break;
2180 }
2181
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 if (node->break_target()->is_linked()) {
2183 node->break_target()->Bind();
2184 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002185 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002186 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2187}
2188
2189
2190void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2191#ifdef DEBUG
2192 int original_height = frame_->height();
2193#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002194 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002195 Comment cmnt(masm_, "[ WhileStatement");
2196 CodeForStatementPosition(node);
2197
2198 // If the test is never true and has no side effects there is no need
2199 // to compile the test or body.
2200 ConditionAnalysis info = AnalyzeCondition(node->cond());
2201 if (info == ALWAYS_FALSE) return;
2202
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002203 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002204 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002205
2206 // Label the top of the loop with the continue target for the backward
2207 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002208 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002209 node->continue_target()->Bind();
2210
2211 if (info == DONT_KNOW) {
2212 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002213 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002214 if (has_valid_frame()) {
2215 // A NULL frame indicates that control did not fall out of the
2216 // test expression.
2217 Branch(false, node->break_target());
2218 }
2219 if (has_valid_frame() || body.is_linked()) {
2220 body.Bind();
2221 }
2222 }
2223
2224 if (has_valid_frame()) {
2225 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002226 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002227
2228 // If control flow can fall out of the body, jump back to the top.
2229 if (has_valid_frame()) {
2230 node->continue_target()->Jump();
2231 }
2232 }
2233 if (node->break_target()->is_linked()) {
2234 node->break_target()->Bind();
2235 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002236 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002237 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2238}
2239
2240
2241void CodeGenerator::VisitForStatement(ForStatement* node) {
2242#ifdef DEBUG
2243 int original_height = frame_->height();
2244#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002245 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002246 Comment cmnt(masm_, "[ ForStatement");
2247 CodeForStatementPosition(node);
2248 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002249 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002250 }
2251
2252 // If the test is never true there is no need to compile the test or
2253 // body.
2254 ConditionAnalysis info = AnalyzeCondition(node->cond());
2255 if (info == ALWAYS_FALSE) return;
2256
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002257 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002258 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002259
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002260 // We know that the loop index is a smi if it is not modified in the
2261 // loop body and it is checked against a constant limit in the loop
2262 // condition. In this case, we reset the static type information of the
2263 // loop index to smi before compiling the body, the update expression, and
2264 // the bottom check of the loop condition.
2265 TypeInfoCodeGenState type_info_scope(this,
2266 node->is_fast_smi_loop() ?
2267 node->loop_variable()->slot() :
2268 NULL,
2269 TypeInfo::Smi());
2270
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002271 // If there is no update statement, label the top of the loop with the
2272 // continue target, otherwise with the loop target.
2273 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2274 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002275 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002276 node->continue_target()->Bind();
2277 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002278 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002279 loop.Bind();
2280 }
2281
2282 // If the test is always true, there is no need to compile it.
2283 if (info == DONT_KNOW) {
2284 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002285 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002286 if (has_valid_frame()) {
2287 Branch(false, node->break_target());
2288 }
2289 if (has_valid_frame() || body.is_linked()) {
2290 body.Bind();
2291 }
2292 }
2293
2294 if (has_valid_frame()) {
2295 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002296 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002297
2298 if (node->next() == NULL) {
2299 // If there is no update statement and control flow can fall out
2300 // of the loop, jump directly to the continue label.
2301 if (has_valid_frame()) {
2302 node->continue_target()->Jump();
2303 }
2304 } else {
2305 // If there is an update statement and control flow can reach it
2306 // via falling out of the body of the loop or continuing, we
2307 // compile the update statement.
2308 if (node->continue_target()->is_linked()) {
2309 node->continue_target()->Bind();
2310 }
2311 if (has_valid_frame()) {
2312 // Record source position of the statement as this code which is
2313 // after the code for the body actually belongs to the loop
2314 // statement and not the body.
2315 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002316 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002317 loop.Jump();
2318 }
2319 }
2320 }
2321 if (node->break_target()->is_linked()) {
2322 node->break_target()->Bind();
2323 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002324 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002325 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326}
2327
2328
ager@chromium.org7c537e22008-10-16 08:43:32 +00002329void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002330#ifdef DEBUG
2331 int original_height = frame_->height();
2332#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002333 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002337 JumpTarget primitive;
2338 JumpTarget jsobject;
2339 JumpTarget fixed_array;
2340 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2341 JumpTarget end_del_check;
2342 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343
2344 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002345 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346
2347 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2348 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002350 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2351 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002352 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002353 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2354 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356
2357 // Stack layout in body:
2358 // [iteration counter (Smi)]
2359 // [length of array]
2360 // [FixedArray]
2361 // [Map or 0]
2362 // [Object]
2363
2364 // Check if enumerable is already a JSObject
2365 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002366 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002367 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002370 primitive.Bind();
2371 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002372 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002374 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002376 // r0: value to be iterated over
2377 frame_->EmitPush(r0); // Push the object being iterated over.
2378
2379 // Check cache validity in generated code. This is a fast case for
2380 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2381 // guarantee cache validity, call the runtime system to check cache
2382 // validity or get the property names in a fixed array.
2383 JumpTarget call_runtime;
2384 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2385 JumpTarget check_prototype;
2386 JumpTarget use_cache;
2387 __ mov(r1, Operand(r0));
2388 loop.Bind();
2389 // Check that there are no elements.
2390 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2391 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2392 __ cmp(r2, r4);
2393 call_runtime.Branch(ne);
2394 // Check that instance descriptors are not empty so that we can
2395 // check for an enum cache. Leave the map in r3 for the subsequent
2396 // prototype load.
2397 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2398 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2399 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2400 __ cmp(r2, ip);
2401 call_runtime.Branch(eq);
2402 // Check that there in an enum cache in the non-empty instance
2403 // descriptors. This is the case if the next enumeration index
2404 // field does not contain a smi.
2405 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2406 __ tst(r2, Operand(kSmiTagMask));
2407 call_runtime.Branch(eq);
2408 // For all objects but the receiver, check that the cache is empty.
2409 // r4: empty fixed array root.
2410 __ cmp(r1, r0);
2411 check_prototype.Branch(eq);
2412 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2413 __ cmp(r2, r4);
2414 call_runtime.Branch(ne);
2415 check_prototype.Bind();
2416 // Load the prototype from the map and loop if non-null.
2417 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2418 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2419 __ cmp(r1, ip);
2420 loop.Branch(ne);
2421 // The enum cache is valid. Load the map of the object being
2422 // iterated over and use the cache for the iteration.
2423 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2424 use_cache.Jump();
2425
2426 call_runtime.Bind();
2427 // Call the runtime to get the property names for the object.
2428 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002429 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002431 // If we got a map from the runtime call, we can do a fast
2432 // modification check. Otherwise, we got a fixed array, and we have
2433 // to do a slow check.
2434 // r0: map or fixed array (result from call to
2435 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 __ mov(r2, Operand(r0));
2437 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002438 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2439 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002442 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002444 // r0: map (either the result from a call to
2445 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2446 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 __ mov(r1, Operand(r0));
2448 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2449 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2450 __ ldr(r2,
2451 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2452
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002453 frame_->EmitPush(r0); // map
2454 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002455 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002457 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 frame_->EmitPush(r0);
2459 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002463 frame_->EmitPush(r1); // insert 0 in place of Map
2464 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465
2466 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002467 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002468 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002469 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471
2472 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002474 // sp[0] : index
2475 // sp[1] : array/enum cache length
2476 // sp[2] : array or enum cache
2477 // sp[3] : 0 or map
2478 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002479 // Grab the current frame's height for the break and continue
2480 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002481 node->break_target()->SetExpectedHeight();
2482 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002484 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002485 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002486 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 node->break_target()->Branch(hs);
2488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2492 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2493
2494 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002495 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 // Check if this (still) matches the map of the enumerable.
2497 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2500 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002501 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502
2503 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2505 frame_->EmitPush(r0);
2506 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002507 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002508 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509
2510 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002511 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2512 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002513 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 end_del_check.Bind();
2516 // Store the entry in the 'each' expression and take another spin in the
2517 // loop. r3: i'th entry of the enum cache (or string there of)
2518 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 { Reference each(this, node->each());
2520 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002521 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002522 __ ldr(r0, frame_->ElementAt(each.size()));
2523 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002524 each.SetValue(NOT_CONST_INIT);
2525 frame_->Drop(2);
2526 } else {
2527 // If the reference was to a slot we rely on the convenient property
2528 // that it doesn't matter whether a value (eg, r3 pushed above) is
2529 // right on top of or right underneath a zero-sized reference.
2530 each.SetValue(NOT_CONST_INIT);
2531 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002532 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 }
2534 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002535 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002537 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002538
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002539 // Next. Reestablish a spilled frame in case we are coming here via
2540 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002541 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002542 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 frame_->EmitPop(r0);
2544 __ add(r0, r0, Operand(Smi::FromInt(1)));
2545 frame_->EmitPush(r0);
2546 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002548 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2549 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002551 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552
2553 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 exit.Bind();
2555 node->continue_target()->Unuse();
2556 node->break_target()->Unuse();
2557 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558}
2559
2560
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002561void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562#ifdef DEBUG
2563 int original_height = frame_->height();
2564#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002565 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002566 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002569 JumpTarget try_block;
2570 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
2576 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002577 Variable* catch_var = node->catch_var()->var();
2578 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2579 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580
2581 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002584 VisitStatements(node->catch_block()->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002585 if (frame_ != NULL) {
2586 exit.Jump();
2587 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588
2589
2590 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002593 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2594 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002596 // Shadow the labels for all escapes from the try block, including
2597 // returns. During shadowing, the original label is hidden as the
2598 // LabelShadow and operations on the original actually affect the
2599 // shadowing label.
2600 //
2601 // We should probably try to unify the escaping labels and the return
2602 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 int nof_escapes = node->escaping_targets()->length();
2604 List<ShadowTarget*> shadows(1 + nof_escapes);
2605
2606 // Add the shadow target for the function return.
2607 static const int kReturnShadowIndex = 0;
2608 shadows.Add(new ShadowTarget(&function_return_));
2609 bool function_return_was_shadowed = function_return_is_shadowed_;
2610 function_return_is_shadowed_ = true;
2611 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2612
2613 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002615 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 }
2617
2618 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002619 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620
2621 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002622 // After shadowing stops, the original labels are unshadowed and the
2623 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002624 bool has_unlinks = false;
2625 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002627 has_unlinks = has_unlinks || shadows[i]->is_linked();
2628 }
2629 function_return_is_shadowed_ = function_return_was_shadowed;
2630
2631 // Get an external reference to the handler address.
2632 ExternalReference handler_address(Top::k_handler_address);
2633
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002634 // If we can fall off the end of the try block, unlink from try chain.
2635 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002636 // The next handler address is on top of the frame. Unlink from
2637 // the handler list and drop the rest of this handler from the
2638 // frame.
2639 ASSERT(StackHandlerConstants::kNextOffset == 0);
2640 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641 __ mov(r3, Operand(handler_address));
2642 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002643 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 if (has_unlinks) {
2645 exit.Jump();
2646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 }
2648
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002649 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002650 // jumped to. Deallocate each shadow target.
2651 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002653 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 shadows[i]->Bind();
2655 // Because we can be jumping here (to spilled code) from unspilled
2656 // code, we need to reestablish a spilled frame at this block.
2657 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 // Reload sp from the top handler, because some statements that we
2660 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002663 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002665 ASSERT(StackHandlerConstants::kNextOffset == 0);
2666 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002668 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2671 frame_->PrepareForReturn();
2672 }
2673 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 }
2675 }
2676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 exit.Bind();
2678 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679}
2680
2681
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002682void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683#ifdef DEBUG
2684 int original_height = frame_->height();
2685#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002686 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002687 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689
2690 // State: Used to keep track of reason for entering the finally
2691 // block. Should probably be extended to hold information for
2692 // break/continue from within the try block.
2693 enum { FALLING, THROWING, JUMPING };
2694
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002695 JumpTarget try_block;
2696 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002700 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 // In case of thrown exceptions, this is where we continue.
2702 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002703 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704
2705 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002706 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002708 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2709 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002711 // Shadow the labels for all escapes from the try block, including
2712 // returns. Shadowing hides the original label as the LabelShadow and
2713 // operations on the original actually affect the shadowing label.
2714 //
2715 // We should probably try to unify the escaping labels and the return
2716 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002717 int nof_escapes = node->escaping_targets()->length();
2718 List<ShadowTarget*> shadows(1 + nof_escapes);
2719
2720 // Add the shadow target for the function return.
2721 static const int kReturnShadowIndex = 0;
2722 shadows.Add(new ShadowTarget(&function_return_));
2723 bool function_return_was_shadowed = function_return_is_shadowed_;
2724 function_return_is_shadowed_ = true;
2725 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2726
2727 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002729 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 }
2731
2732 // Generate code for the statements in the try block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002733 VisitStatements(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002735 // Stop the introduced shadowing and count the number of required unlinks.
2736 // After shadowing stops, the original labels are unshadowed and the
2737 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002739 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 shadows[i]->StopShadowing();
2741 if (shadows[i]->is_linked()) nof_unlinks++;
2742 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002743 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 // Get an external reference to the handler address.
2746 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002748 // If we can fall off the end of the try block, unlink from the try
2749 // chain and set the state on the frame to FALLING.
2750 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002751 // The next handler address is on top of the frame.
2752 ASSERT(StackHandlerConstants::kNextOffset == 0);
2753 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 __ mov(r3, Operand(handler_address));
2755 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002756 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757
2758 // Fake a top of stack value (unneeded when FALLING) and set the
2759 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002760 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002761 frame_->EmitPush(r0);
2762 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2763 if (nof_unlinks > 0) {
2764 finally_block.Jump();
2765 }
2766 }
2767
2768 // Generate code to unlink and set the state for the (formerly)
2769 // shadowing targets that have been jumped to.
2770 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002772 // If we have come from the shadowed return, the return value is
2773 // in (a non-refcounted reference to) r0. We must preserve it
2774 // until it is pushed.
2775 //
2776 // Because we can be jumping here (to spilled code) from
2777 // unspilled code, we need to reestablish a spilled frame at
2778 // this block.
2779 shadows[i]->Bind();
2780 frame_->SpillAll();
2781
2782 // Reload sp from the top handler, because some statements that
2783 // we break from (eg, for...in) may have left stuff on the
2784 // stack.
2785 __ mov(r3, Operand(handler_address));
2786 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002787 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002788
2789 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002790 // handler address is currently on top of the frame.
2791 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002792 frame_->EmitPop(r1);
2793 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002794 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002795
2796 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002797 // If this label shadowed the function return, materialize the
2798 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002799 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002800 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002801 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002802 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002803 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804 }
2805 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002806 if (--nof_unlinks > 0) {
2807 // If this is not the last unlink block, jump around the next.
2808 finally_block.Jump();
2809 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 }
2811 }
2812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002814 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002816 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002817 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002818
2819 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002820 // and the state - while evaluating the finally block.
2821 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 // Generate code for the statements in the finally block.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002823 VisitStatements(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002825 if (has_valid_frame()) {
2826 // Restore state and return value or faked TOS.
2827 frame_->EmitPop(r2);
2828 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 }
2830
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002831 // Generate code to jump to the right destination for all used
2832 // formerly shadowing targets. Deallocate each shadow target.
2833 for (int i = 0; i < shadows.length(); i++) {
2834 if (has_valid_frame() && shadows[i]->is_bound()) {
2835 JumpTarget* original = shadows[i]->other_target();
2836 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2837 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002838 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 skip.Branch(ne);
2840 frame_->PrepareForReturn();
2841 original->Jump();
2842 skip.Bind();
2843 } else {
2844 original->Branch(eq);
2845 }
2846 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002848
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002849 if (has_valid_frame()) {
2850 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002851 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002852 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2853 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002855 // Rethrow exception.
2856 frame_->EmitPush(r0);
2857 frame_->CallRuntime(Runtime::kReThrow, 1);
2858
2859 // Done.
2860 exit.Bind();
2861 }
2862 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863}
2864
2865
ager@chromium.org7c537e22008-10-16 08:43:32 +00002866void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002867#ifdef DEBUG
2868 int original_height = frame_->height();
2869#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002871 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002872#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002873 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002874#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002875 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002876 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877}
2878
2879
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002880void CodeGenerator::InstantiateFunction(
2881 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002882 // Use the fast case closure allocation code that allocates in new
2883 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002884 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002885 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002886 frame_->EmitPush(Operand(function_info));
2887 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002888 frame_->CallStub(&stub, 1);
2889 frame_->EmitPush(r0);
2890 } else {
2891 // Create a new closure.
2892 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002893 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002894 frame_->CallRuntime(Runtime::kNewClosure, 2);
2895 frame_->EmitPush(r0);
2896 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897}
2898
2899
ager@chromium.org7c537e22008-10-16 08:43:32 +00002900void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002901#ifdef DEBUG
2902 int original_height = frame_->height();
2903#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 Comment cmnt(masm_, "[ FunctionLiteral");
2905
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002906 // Build the function info and instantiate it.
2907 Handle<SharedFunctionInfo> function_info =
2908 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002909 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002910 if (HasStackOverflow()) {
2911 ASSERT(frame_->height() == original_height);
2912 return;
2913 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002914 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002915 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916}
2917
2918
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002919void CodeGenerator::VisitSharedFunctionInfoLiteral(
2920 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002921#ifdef DEBUG
2922 int original_height = frame_->height();
2923#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002924 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2925 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002926 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002927}
2928
2929
ager@chromium.org7c537e22008-10-16 08:43:32 +00002930void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002931#ifdef DEBUG
2932 int original_height = frame_->height();
2933#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002934 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002936 JumpTarget then;
2937 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002938 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002939 if (has_valid_frame()) {
2940 Branch(false, &else_);
2941 }
2942 if (has_valid_frame() || then.is_linked()) {
2943 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002944 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002945 }
2946 if (else_.is_linked()) {
2947 JumpTarget exit;
2948 if (has_valid_frame()) exit.Jump();
2949 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002950 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002951 if (exit.is_linked()) exit.Bind();
2952 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002953 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954}
2955
2956
ager@chromium.org7c537e22008-10-16 08:43:32 +00002957void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2958 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002959 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002961 // JumpTargets do not yet support merging frames so the frame must be
2962 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002963 JumpTarget slow;
2964 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002965
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002966 // Generate fast case for loading from slots that correspond to
2967 // local/global variables or arguments unless they are shadowed by
2968 // eval-introduced bindings.
2969 EmitDynamicLoadFromSlotFastCase(slot,
2970 typeof_state,
2971 &slow,
2972 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002973
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002974 slow.Bind();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002975 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002976 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002977 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002978 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979
ager@chromium.org7c537e22008-10-16 08:43:32 +00002980 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002981 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002982 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002983 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002984 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002985
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002986 done.Bind();
2987 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988
2989 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002990 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002991 TypeInfo info = type_info(slot);
2992 frame_->EmitPush(SlotOperand(slot, scratch), info);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002993 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002994 // Const slots may contain 'the hole' value (the constant hasn't been
2995 // initialized yet) which needs to be converted into the 'undefined'
2996 // value.
2997 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002998 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002999 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003000 __ cmp(scratch, ip);
3001 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
3002 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 }
3004 }
3005}
3006
3007
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003008void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3009 TypeofState state) {
3010 LoadFromSlot(slot, state);
3011
3012 // Bail out quickly if we're not using lazy arguments allocation.
3013 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3014
3015 // ... or if the slot isn't a non-parameter arguments slot.
3016 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3017
3018 VirtualFrame::SpilledScope spilled_scope(frame_);
3019
3020 // Load the loaded value from the stack into r0 but leave it on the
3021 // stack.
3022 __ ldr(r0, MemOperand(sp, 0));
3023
3024 // If the loaded value is the sentinel that indicates that we
3025 // haven't loaded the arguments object yet, we need to do it now.
3026 JumpTarget exit;
3027 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3028 __ cmp(r0, ip);
3029 exit.Branch(ne);
3030 frame_->Drop();
3031 StoreArgumentsObject(false);
3032 exit.Bind();
3033}
3034
3035
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003036void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3037 ASSERT(slot != NULL);
3038 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003039 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003040 ASSERT(slot->var()->is_dynamic());
3041
3042 // For now, just do a runtime call.
3043 frame_->EmitPush(cp);
3044 __ mov(r0, Operand(slot->var()->name()));
3045 frame_->EmitPush(r0);
3046
3047 if (init_state == CONST_INIT) {
3048 // Same as the case for a normal store, but ignores attribute
3049 // (e.g. READ_ONLY) of context slot so that we can initialize
3050 // const properties (introduced via eval("const foo = (some
3051 // expr);")). Also, uses the current function context instead of
3052 // the top context.
3053 //
3054 // Note that we must declare the foo upon entry of eval(), via a
3055 // context slot declaration, but we cannot initialize it at the
3056 // same time, because the const declaration may be at the end of
3057 // the eval code (sigh...) and the const variable may have been
3058 // used before (where its value is 'undefined'). Thus, we can only
3059 // do the initialization when we actually encounter the expression
3060 // and when the expression operands are defined and valid, and
3061 // thus we need the split into 2 operations: declaration of the
3062 // context slot followed by initialization.
3063 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3064 } else {
3065 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3066 }
3067 // Storing a variable must keep the (new) value on the expression
3068 // stack. This is necessary for compiling assignment expressions.
3069 frame_->EmitPush(r0);
3070
3071 } else {
3072 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003073 Register scratch = VirtualFrame::scratch0();
3074 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003075
ager@chromium.org357bf652010-04-12 11:30:10 +00003076 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003077 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003078
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003079 if (init_state == CONST_INIT) {
3080 ASSERT(slot->var()->mode() == Variable::CONST);
3081 // Only the first const initialization must be executed (the slot
3082 // still contains 'the hole' value). When the assignment is
3083 // executed, the code is identical to a normal store (see below).
3084 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003085 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003086 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003087 __ cmp(scratch, ip);
3088 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003089 exit.Branch(ne);
3090 }
3091
3092 // We must execute the store. Storing a variable must keep the
3093 // (new) value on the stack. This is necessary for compiling
3094 // assignment expressions.
3095 //
3096 // Note: We will reach here even with slot->var()->mode() ==
3097 // Variable::CONST because of const declarations which will
3098 // initialize consts to 'the hole' value and by doing so, end up
3099 // calling this code. r2 may be loaded with context; used below in
3100 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003101 Register tos = frame_->Peek();
3102 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003103 if (slot->type() == Slot::CONTEXT) {
3104 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003105 __ tst(tos, Operand(kSmiTagMask));
3106 // We don't use tos any more after here.
3107 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003108 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003109 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003110 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
ager@chromium.org357bf652010-04-12 11:30:10 +00003111 // r1 could be identical with tos, but that doesn't matter.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003112 __ RecordWrite(scratch, Operand(offset), r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003113 }
3114 // If we definitely did not jump over the assignment, we do not need
3115 // to bind the exit label. Doing so can defeat peephole
3116 // optimization.
3117 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003118 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003119 exit.Bind();
3120 }
3121 }
3122}
3123
3124
ager@chromium.org381abbb2009-02-25 13:23:22 +00003125void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3126 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003127 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003128 // Check that no extension objects have been created by calls to
3129 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003130 Register tmp = frame_->scratch0();
3131 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003132 Register context = cp;
3133 Scope* s = scope();
3134 while (s != NULL) {
3135 if (s->num_heap_slots() > 0) {
3136 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003137 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003138 // Check that extension is NULL.
3139 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3140 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003141 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003142 }
3143 // Load next context in chain.
3144 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3145 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3146 context = tmp;
3147 }
3148 // If no outer scope calls eval, we do not need to check more
3149 // context extensions.
3150 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3151 s = s->outer_scope();
3152 }
3153
3154 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003155 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003156 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003157 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003158 __ bind(&next);
3159 // Terminate at global context.
3160 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003161 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3162 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003163 __ b(eq, &fast);
3164 // Check that extension is NULL.
3165 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3166 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003167 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003168 // Load next context in chain.
3169 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3170 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3171 __ b(&next);
3172 __ bind(&fast);
3173 }
3174
ager@chromium.org381abbb2009-02-25 13:23:22 +00003175 // Load the global object.
3176 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003177 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003178 frame_->CallLoadIC(slot->var()->name(),
3179 typeof_state == INSIDE_TYPEOF
3180 ? RelocInfo::CODE_TARGET
3181 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003182}
3183
3184
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003185void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3186 TypeofState typeof_state,
3187 JumpTarget* slow,
3188 JumpTarget* done) {
3189 // Generate fast-case code for variables that might be shadowed by
3190 // eval-introduced variables. Eval is used a lot without
3191 // introducing variables. In those cases, we do not want to
3192 // perform a runtime call for all variables in the scope
3193 // containing the eval.
3194 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3195 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3196 frame_->SpillAll();
3197 done->Jump();
3198
3199 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3200 frame_->SpillAll();
3201 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3202 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3203 if (potential_slot != NULL) {
3204 // Generate fast case for locals that rewrite to slots.
3205 __ ldr(r0,
3206 ContextSlotOperandCheckExtensions(potential_slot,
3207 r1,
3208 r2,
3209 slow));
3210 if (potential_slot->var()->mode() == Variable::CONST) {
3211 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3212 __ cmp(r0, ip);
3213 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3214 }
3215 done->Jump();
3216 } else if (rewrite != NULL) {
3217 // Generate fast case for argument loads.
3218 Property* property = rewrite->AsProperty();
3219 if (property != NULL) {
3220 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3221 Literal* key_literal = property->key()->AsLiteral();
3222 if (obj_proxy != NULL &&
3223 key_literal != NULL &&
3224 obj_proxy->IsArguments() &&
3225 key_literal->handle()->IsSmi()) {
3226 // Load arguments object if there are no eval-introduced
3227 // variables. Then load the argument from the arguments
3228 // object using keyed load.
3229 __ ldr(r0,
3230 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3231 r1,
3232 r2,
3233 slow));
3234 frame_->EmitPush(r0);
3235 __ mov(r1, Operand(key_literal->handle()));
3236 frame_->EmitPush(r1);
3237 EmitKeyedLoad();
3238 done->Jump();
3239 }
3240 }
3241 }
3242 }
3243}
3244
3245
ager@chromium.org7c537e22008-10-16 08:43:32 +00003246void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003247#ifdef DEBUG
3248 int original_height = frame_->height();
3249#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003250 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003251 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003252 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003253}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254
ager@chromium.org7c537e22008-10-16 08:43:32 +00003255
3256void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003257#ifdef DEBUG
3258 int original_height = frame_->height();
3259#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260 Comment cmnt(masm_, "[ VariableProxy");
3261
3262 Variable* var = node->var();
3263 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003264 if (expr != NULL) {
3265 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003267 ASSERT(var->is_global());
3268 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003269 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003271 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272}
3273
3274
ager@chromium.org7c537e22008-10-16 08:43:32 +00003275void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276#ifdef DEBUG
3277 int original_height = frame_->height();
3278#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003280 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003281 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003282 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003283 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003284 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285}
3286
3287
ager@chromium.org7c537e22008-10-16 08:43:32 +00003288void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003289#ifdef DEBUG
3290 int original_height = frame_->height();
3291#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003292 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 Comment cmnt(masm_, "[ RexExp Literal");
3294
3295 // Retrieve the literal array and check the allocated entry.
3296
3297 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003298 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299
3300 // Load the literals array of the function.
3301 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3302
3303 // Load the literal at the ast saved index.
3304 int literal_offset =
3305 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3306 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3307
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003308 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003309 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3310 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003311 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312
3313 // If the entry is undefined we call the runtime system to computed
3314 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003315 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003316 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003317 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003318 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003319 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003320 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003321 frame_->EmitPush(r0);
3322 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003323 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003324
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003325 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003327 frame_->EmitPush(r2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003328 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329}
3330
3331
ager@chromium.org7c537e22008-10-16 08:43:32 +00003332void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003333#ifdef DEBUG
3334 int original_height = frame_->height();
3335#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003336 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003337 Comment cmnt(masm_, "[ ObjectLiteral");
3338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003340 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003341 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003342 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003343 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003344 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003345 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003346 __ mov(r1, Operand(node->constant_properties()));
3347 // Should the object literal have fast elements?
3348 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3349 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003350 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003351 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003352 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003353 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003354 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003355 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003356 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003357 // At the start of each iteration, the top of stack contains
3358 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359 ObjectLiteral::Property* property = node->properties()->at(i);
3360 Literal* key = property->key();
3361 Expression* value = property->value();
3362 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003363 case ObjectLiteral::Property::CONSTANT:
3364 break;
3365 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3366 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3367 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003368 case ObjectLiteral::Property::COMPUTED:
3369 if (key->handle()->IsSymbol()) {
3370 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003371 Load(value);
ager@chromium.org5c838252010-02-19 08:53:10 +00003372 frame_->EmitPop(r0);
3373 __ mov(r2, Operand(key->handle()));
3374 __ ldr(r1, frame_->Top()); // Load the receiver.
3375 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3376 break;
3377 }
3378 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003379 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003380 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003381 frame_->EmitPush(r0); // dup the result
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003382 Load(key);
3383 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003384 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003385 break;
3386 }
3387 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003388 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003389 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003390 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003391 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003392 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003393 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003394 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 break;
3396 }
3397 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003398 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003399 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003400 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003401 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003402 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003403 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003404 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 break;
3406 }
3407 }
3408 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003409 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003410}
3411
3412
ager@chromium.org7c537e22008-10-16 08:43:32 +00003413void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003414#ifdef DEBUG
3415 int original_height = frame_->height();
3416#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003417 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003419
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003420 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003421 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003422 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003423 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003424 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003425 __ mov(r0, Operand(node->constant_elements()));
3426 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003427 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003428 if (node->depth() > 1) {
3429 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003430 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003431 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003432 } else {
3433 FastCloneShallowArrayStub stub(length);
3434 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003435 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003436 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003437 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003438
3439 // Generate code to set the elements in the array that are not
3440 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 for (int i = 0; i < node->values()->length(); i++) {
3442 Expression* value = node->values()->at(i);
3443
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003444 // If value is a literal the property value is already set in the
3445 // boilerplate object.
3446 if (value->AsLiteral() != NULL) continue;
3447 // If value is a materialized literal the property value is already set
3448 // in the boilerplate object if it is simple.
3449 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003451 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003452 Load(value);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003453 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003454
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003455 // Fetch the object literal.
3456 __ ldr(r1, frame_->Top());
3457 // Get the elements array.
3458 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003460 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003461 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003462 __ str(r0, FieldMemOperand(r1, offset));
3463
3464 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003465 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003467 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003468}
3469
3470
ager@chromium.org32912102009-01-16 10:38:43 +00003471void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003472#ifdef DEBUG
3473 int original_height = frame_->height();
3474#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003475 // Call runtime routine to allocate the catch extension object and
3476 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003477 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003478 Load(node->key());
3479 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003480 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3481 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003482 ASSERT_EQ(original_height + 1, frame_->height());
3483}
3484
3485
3486void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3487#ifdef DEBUG
3488 int original_height = frame_->height();
3489#endif
3490 Comment cmnt(masm(), "[ Variable Assignment");
3491 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3492 ASSERT(var != NULL);
3493 Slot* slot = var->slot();
3494 ASSERT(slot != NULL);
3495
3496 // Evaluate the right-hand side.
3497 if (node->is_compound()) {
3498 // For a compound assignment the right-hand side is a binary operation
3499 // between the current property value and the actual right-hand side.
3500 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3501
3502 // Perform the binary operation.
3503 Literal* literal = node->value()->AsLiteral();
3504 bool overwrite_value =
3505 (node->value()->AsBinaryOperation() != NULL &&
3506 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3507 if (literal != NULL && literal->handle()->IsSmi()) {
3508 SmiOperation(node->binary_op(),
3509 literal->handle(),
3510 false,
3511 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3512 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003513 GenerateInlineSmi inline_smi =
3514 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3515 if (literal != NULL) {
3516 ASSERT(!literal->handle()->IsSmi());
3517 inline_smi = DONT_GENERATE_INLINE_SMI;
3518 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003519 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003520 GenericBinaryOperation(node->binary_op(),
3521 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3522 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003523 }
3524 } else {
3525 Load(node->value());
3526 }
3527
3528 // Perform the assignment.
3529 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3530 CodeForSourcePosition(node->position());
3531 StoreToSlot(slot,
3532 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3533 }
3534 ASSERT_EQ(original_height + 1, frame_->height());
3535}
3536
3537
3538void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3539#ifdef DEBUG
3540 int original_height = frame_->height();
3541#endif
3542 Comment cmnt(masm(), "[ Named Property Assignment");
3543 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3544 Property* prop = node->target()->AsProperty();
3545 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3546
3547 // Initialize name and evaluate the receiver sub-expression if necessary. If
3548 // the receiver is trivial it is not placed on the stack at this point, but
3549 // loaded whenever actually needed.
3550 Handle<String> name;
3551 bool is_trivial_receiver = false;
3552 if (var != NULL) {
3553 name = var->name();
3554 } else {
3555 Literal* lit = prop->key()->AsLiteral();
3556 ASSERT_NOT_NULL(lit);
3557 name = Handle<String>::cast(lit->handle());
3558 // Do not materialize the receiver on the frame if it is trivial.
3559 is_trivial_receiver = prop->obj()->IsTrivial();
3560 if (!is_trivial_receiver) Load(prop->obj());
3561 }
3562
3563 // Change to slow case in the beginning of an initialization block to
3564 // avoid the quadratic behavior of repeatedly adding fast properties.
3565 if (node->starts_initialization_block()) {
3566 // Initialization block consists of assignments of the form expr.x = ..., so
3567 // this will never be an assignment to a variable, so there must be a
3568 // receiver object.
3569 ASSERT_EQ(NULL, var);
3570 if (is_trivial_receiver) {
3571 Load(prop->obj());
3572 } else {
3573 frame_->Dup();
3574 }
3575 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3576 }
3577
3578 // Change to fast case at the end of an initialization block. To prepare for
3579 // that add an extra copy of the receiver to the frame, so that it can be
3580 // converted back to fast case after the assignment.
3581 if (node->ends_initialization_block() && !is_trivial_receiver) {
3582 frame_->Dup();
3583 }
3584
3585 // Stack layout:
3586 // [tos] : receiver (only materialized if non-trivial)
3587 // [tos+1] : receiver if at the end of an initialization block
3588
3589 // Evaluate the right-hand side.
3590 if (node->is_compound()) {
3591 // For a compound assignment the right-hand side is a binary operation
3592 // between the current property value and the actual right-hand side.
3593 if (is_trivial_receiver) {
3594 Load(prop->obj());
3595 } else if (var != NULL) {
3596 LoadGlobal();
3597 } else {
3598 frame_->Dup();
3599 }
3600 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003601
3602 // Perform the binary operation.
3603 Literal* literal = node->value()->AsLiteral();
3604 bool overwrite_value =
3605 (node->value()->AsBinaryOperation() != NULL &&
3606 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3607 if (literal != NULL && literal->handle()->IsSmi()) {
3608 SmiOperation(node->binary_op(),
3609 literal->handle(),
3610 false,
3611 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3612 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003613 GenerateInlineSmi inline_smi =
3614 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3615 if (literal != NULL) {
3616 ASSERT(!literal->handle()->IsSmi());
3617 inline_smi = DONT_GENERATE_INLINE_SMI;
3618 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003619 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003620 GenericBinaryOperation(node->binary_op(),
3621 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3622 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003623 }
3624 } else {
3625 // For non-compound assignment just load the right-hand side.
3626 Load(node->value());
3627 }
3628
3629 // Stack layout:
3630 // [tos] : value
3631 // [tos+1] : receiver (only materialized if non-trivial)
3632 // [tos+2] : receiver if at the end of an initialization block
3633
3634 // Perform the assignment. It is safe to ignore constants here.
3635 ASSERT(var == NULL || var->mode() != Variable::CONST);
3636 ASSERT_NE(Token::INIT_CONST, node->op());
3637 if (is_trivial_receiver) {
3638 // Load the receiver and swap with the value.
3639 Load(prop->obj());
3640 Register t0 = frame_->PopToRegister();
3641 Register t1 = frame_->PopToRegister(t0);
3642 frame_->EmitPush(t0);
3643 frame_->EmitPush(t1);
3644 }
3645 CodeForSourcePosition(node->position());
3646 bool is_contextual = (var != NULL);
3647 EmitNamedStore(name, is_contextual);
3648 frame_->EmitPush(r0);
3649
3650 // Change to fast case at the end of an initialization block.
3651 if (node->ends_initialization_block()) {
3652 ASSERT_EQ(NULL, var);
3653 // The argument to the runtime call is the receiver.
3654 if (is_trivial_receiver) {
3655 Load(prop->obj());
3656 } else {
3657 // A copy of the receiver is below the value of the assignment. Swap
3658 // the receiver and the value of the assignment expression.
3659 Register t0 = frame_->PopToRegister();
3660 Register t1 = frame_->PopToRegister(t0);
3661 frame_->EmitPush(t0);
3662 frame_->EmitPush(t1);
3663 }
3664 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3665 }
3666
3667 // Stack layout:
3668 // [tos] : result
3669
3670 ASSERT_EQ(original_height + 1, frame_->height());
3671}
3672
3673
3674void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3675#ifdef DEBUG
3676 int original_height = frame_->height();
3677#endif
3678 Comment cmnt(masm_, "[ Keyed Property Assignment");
3679 Property* prop = node->target()->AsProperty();
3680 ASSERT_NOT_NULL(prop);
3681
3682 // Evaluate the receiver subexpression.
3683 Load(prop->obj());
3684
3685 // Change to slow case in the beginning of an initialization block to
3686 // avoid the quadratic behavior of repeatedly adding fast properties.
3687 if (node->starts_initialization_block()) {
3688 frame_->Dup();
3689 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3690 }
3691
3692 // Change to fast case at the end of an initialization block. To prepare for
3693 // that add an extra copy of the receiver to the frame, so that it can be
3694 // converted back to fast case after the assignment.
3695 if (node->ends_initialization_block()) {
3696 frame_->Dup();
3697 }
3698
3699 // Evaluate the key subexpression.
3700 Load(prop->key());
3701
3702 // Stack layout:
3703 // [tos] : key
3704 // [tos+1] : receiver
3705 // [tos+2] : receiver if at the end of an initialization block
3706
3707 // Evaluate the right-hand side.
3708 if (node->is_compound()) {
3709 // For a compound assignment the right-hand side is a binary operation
3710 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003711 // Duplicate receiver and key for loading the current property value.
3712 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003713 EmitKeyedLoad();
3714 frame_->EmitPush(r0);
3715
3716 // Perform the binary operation.
3717 Literal* literal = node->value()->AsLiteral();
3718 bool overwrite_value =
3719 (node->value()->AsBinaryOperation() != NULL &&
3720 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3721 if (literal != NULL && literal->handle()->IsSmi()) {
3722 SmiOperation(node->binary_op(),
3723 literal->handle(),
3724 false,
3725 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3726 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003727 GenerateInlineSmi inline_smi =
3728 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3729 if (literal != NULL) {
3730 ASSERT(!literal->handle()->IsSmi());
3731 inline_smi = DONT_GENERATE_INLINE_SMI;
3732 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003733 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003734 GenericBinaryOperation(node->binary_op(),
3735 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3736 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003737 }
3738 } else {
3739 // For non-compound assignment just load the right-hand side.
3740 Load(node->value());
3741 }
3742
3743 // Stack layout:
3744 // [tos] : value
3745 // [tos+1] : key
3746 // [tos+2] : receiver
3747 // [tos+3] : receiver if at the end of an initialization block
3748
3749 // Perform the assignment. It is safe to ignore constants here.
3750 ASSERT(node->op() != Token::INIT_CONST);
3751 CodeForSourcePosition(node->position());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003752 EmitKeyedStore(prop->key()->type());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003753 frame_->EmitPush(r0);
3754
3755 // Stack layout:
3756 // [tos] : result
3757 // [tos+1] : receiver if at the end of an initialization block
3758
3759 // Change to fast case at the end of an initialization block.
3760 if (node->ends_initialization_block()) {
3761 // The argument to the runtime call is the extra copy of the receiver,
3762 // which is below the value of the assignment. Swap the receiver and
3763 // the value of the assignment expression.
3764 Register t0 = frame_->PopToRegister();
3765 Register t1 = frame_->PopToRegister(t0);
3766 frame_->EmitPush(t1);
3767 frame_->EmitPush(t0);
3768 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3769 }
3770
3771 // Stack layout:
3772 // [tos] : result
3773
3774 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003775}
3776
3777
ager@chromium.org7c537e22008-10-16 08:43:32 +00003778void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003779 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003780#ifdef DEBUG
3781 int original_height = frame_->height();
3782#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003783 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003784
ager@chromium.orgac091b72010-05-05 07:34:42 +00003785 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3786 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787
ager@chromium.orgac091b72010-05-05 07:34:42 +00003788 if (var != NULL && !var->is_global()) {
3789 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003790
ager@chromium.orgac091b72010-05-05 07:34:42 +00003791 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3792 (var != NULL && var->is_global())) {
3793 // Properties whose keys are property names and global variables are
3794 // treated as named property references. We do not need to consider
3795 // global 'this' because it is not a valid left-hand side.
3796 EmitNamedPropertyAssignment(node);
3797
3798 } else if (prop != NULL) {
3799 // Other properties (including rewritten parameters for a function that
3800 // uses arguments) are keyed property assignments.
3801 EmitKeyedPropertyAssignment(node);
3802
3803 } else {
3804 // Invalid left-hand side.
3805 Load(node->target());
3806 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3807 // The runtime call doesn't actually return but the code generator will
3808 // still generate code and expects a certain frame height.
3809 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003810 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003811 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003812}
3813
3814
ager@chromium.org7c537e22008-10-16 08:43:32 +00003815void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003816#ifdef DEBUG
3817 int original_height = frame_->height();
3818#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003819 Comment cmnt(masm_, "[ Throw");
3820
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003821 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003822 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823 frame_->CallRuntime(Runtime::kThrow, 1);
3824 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003825 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003826}
3827
3828
ager@chromium.org7c537e22008-10-16 08:43:32 +00003829void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003830#ifdef DEBUG
3831 int original_height = frame_->height();
3832#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003834
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003835 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003836 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003837 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003838 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003839}
3840
3841
ager@chromium.org7c537e22008-10-16 08:43:32 +00003842void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003843#ifdef DEBUG
3844 int original_height = frame_->height();
3845#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846 Comment cmnt(masm_, "[ Call");
3847
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003848 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849 ZoneList<Expression*>* args = node->arguments();
3850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003851 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 Variable* var = function->AsVariableProxy()->AsVariable();
3854 Property* property = function->AsProperty();
3855
3856 // ------------------------------------------------------------------------
3857 // Fast-case: Use inline caching.
3858 // ---
3859 // According to ECMA-262, section 11.2.3, page 44, the function to call
3860 // must be resolved after the arguments have been evaluated. The IC code
3861 // automatically handles this by loading the arguments before the function
3862 // is resolved in cache misses (this also holds for megamorphic calls).
3863 // ------------------------------------------------------------------------
3864
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003865 if (var != NULL && var->is_possibly_eval()) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003866 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003867 // ----------------------------------
3868 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3869 // ----------------------------------
3870
3871 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3872 // resolve the function we need to call and the receiver of the
3873 // call. Then we call the resolved function using the given
3874 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003875
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003876 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003877 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003878
3879 // Allocate a frame slot for the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003880 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003881 frame_->EmitPush(r2);
3882
3883 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003884 int arg_count = args->length();
3885 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003886 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003887 }
3888
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003889 // If we know that eval can only be shadowed by eval-introduced
3890 // variables we attempt to load the global eval function directly
3891 // in generated code. If we succeed, there is no need to perform a
3892 // context lookup in the runtime system.
3893 JumpTarget done;
3894 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3895 ASSERT(var->slot()->type() == Slot::LOOKUP);
3896 JumpTarget slow;
3897 // Prepare the stack for the call to
3898 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3899 // function, the first argument to the eval call and the
3900 // receiver.
3901 LoadFromGlobalSlotCheckExtensions(var->slot(),
3902 NOT_INSIDE_TYPEOF,
3903 &slow);
3904 frame_->EmitPush(r0);
3905 if (arg_count > 0) {
3906 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3907 frame_->EmitPush(r1);
3908 } else {
3909 frame_->EmitPush(r2);
3910 }
3911 __ ldr(r1, frame_->Receiver());
3912 frame_->EmitPush(r1);
3913
3914 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3915
3916 done.Jump();
3917 slow.Bind();
3918 }
3919
3920 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3921 // pushing the loaded function, the first argument to the eval
3922 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003923 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3924 frame_->EmitPush(r1);
3925 if (arg_count > 0) {
3926 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3927 frame_->EmitPush(r1);
3928 } else {
3929 frame_->EmitPush(r2);
3930 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003931 __ ldr(r1, frame_->Receiver());
3932 frame_->EmitPush(r1);
3933
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003934 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003935 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003936
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003937 // If we generated fast-case code bind the jump-target where fast
3938 // and slow case merge.
3939 if (done.is_linked()) done.Bind();
3940
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003941 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003942 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003943 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3944
3945 // Call the function.
3946 CodeForSourcePosition(node->position());
3947
3948 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003949 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003950 frame_->CallStub(&call_function, arg_count + 1);
3951
3952 __ ldr(cp, frame_->Context());
3953 // Remove the function from the stack.
3954 frame_->Drop();
3955 frame_->EmitPush(r0);
3956
3957 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958 // ----------------------------------
3959 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3960 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003961 // Pass the global object as the receiver and let the IC stub
3962 // patch the stack to use the global proxy as 'this' in the
3963 // invoked function.
3964 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
3966 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003967 int arg_count = args->length();
3968 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003969 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003970 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003972 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00003973 // Setup the name register and call the IC initialization code.
3974 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003975 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3976 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003977 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003978 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3979 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003980 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003981 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982
3983 } else if (var != NULL && var->slot() != NULL &&
3984 var->slot()->type() == Slot::LOOKUP) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003985 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003987 // JavaScript examples:
3988 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003989 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003990 //
3991 // function f() {};
3992 // function g() {
3993 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003994 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003995 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996 // ----------------------------------
3997
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003998 // JumpTargets do not yet support merging frames so the frame must be
3999 // spilled when jumping to these targets.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004000 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004001
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004002 // Generate fast case for loading functions from slots that
4003 // correspond to local/global variables or arguments unless they
4004 // are shadowed by eval-introduced bindings.
4005 EmitDynamicLoadFromSlotFastCase(var->slot(),
4006 NOT_INSIDE_TYPEOF,
4007 &slow,
4008 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004009
4010 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004012 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004013 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004014 frame_->EmitPush(r0);
4015 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016 // r0: slot value; r1: receiver
4017
4018 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 frame_->EmitPush(r0); // function
4020 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004022 // If fast case code has been generated, emit code to push the
4023 // function and receiver and have the slow path jump around this
4024 // code.
4025 if (done.is_linked()) {
4026 JumpTarget call;
4027 call.Jump();
4028 done.Bind();
4029 frame_->EmitPush(r0); // function
4030 LoadGlobalReceiver(r1); // receiver
4031 call.Bind();
4032 }
4033
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004034 // Call the function. At this point, everything is spilled but the
4035 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004036 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004037 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038
4039 } else if (property != NULL) {
4040 // Check if the key is a literal string.
4041 Literal* literal = property->key()->AsLiteral();
4042
4043 if (literal != NULL && literal->handle()->IsSymbol()) {
4044 // ------------------------------------------------------------------
4045 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4046 // ------------------------------------------------------------------
4047
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004048 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004049
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004050 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4051 name->IsEqualTo(CStrVector("apply")) &&
4052 args->length() == 2 &&
4053 args->at(1)->AsVariableProxy() != NULL &&
4054 args->at(1)->AsVariableProxy()->IsArguments()) {
4055 // Use the optimized Function.prototype.apply that avoids
4056 // allocating lazily allocated arguments objects.
4057 CallApplyLazy(property->obj(),
4058 args->at(0),
4059 args->at(1)->AsVariableProxy(),
4060 node->position());
4061
4062 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004063 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004064 // Load the arguments.
4065 int arg_count = args->length();
4066 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004067 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004068 }
4069
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004070 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004071 // Set the name register and call the IC initialization code.
4072 __ mov(r2, Operand(name));
4073 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4074 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4075 CodeForSourcePosition(node->position());
4076 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4077 __ ldr(cp, frame_->Context());
4078 frame_->EmitPush(r0);
4079 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080
4081 } else {
4082 // -------------------------------------------
4083 // JavaScript example: 'array[index](1, 2, 3)'
4084 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004085 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004087 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004089 Load(property->key());
4090 EmitKeyedLoad();
4091 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004092 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004093 frame_->EmitPush(r0); // Function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004094 LoadGlobalReceiver(r0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004095 // Call the function.
4096 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4097 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004098 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004099 // Load the arguments.
4100 int arg_count = args->length();
4101 for (int i = 0; i < arg_count; i++) {
4102 Load(args->at(i));
4103 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004104
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004105 // Set the name register and call the IC initialization code.
4106 Load(property->key());
4107 frame_->EmitPop(r2); // Function name.
4108
4109 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4110 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4111 CodeForSourcePosition(node->position());
4112 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4113 __ ldr(cp, frame_->Context());
4114 frame_->EmitPush(r0);
4115 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116 }
4117
4118 } else {
4119 // ----------------------------------
4120 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4121 // ----------------------------------
4122
4123 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004124 Load(function);
4125
4126 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004127
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004128 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004129 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004131 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004132 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004133 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004134 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004135 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004136}
4137
4138
ager@chromium.org7c537e22008-10-16 08:43:32 +00004139void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004140#ifdef DEBUG
4141 int original_height = frame_->height();
4142#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143 Comment cmnt(masm_, "[ CallNew");
4144
4145 // According to ECMA-262, section 11.2.2, page 44, the function
4146 // expression in new calls must be evaluated before the
4147 // arguments. This is different from ordinary calls, where the
4148 // actual function to call is resolved after the arguments have been
4149 // evaluated.
4150
4151 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004152 // receiver. There is no need to use the global proxy here because
4153 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004154 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004155 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156
4157 // Push the arguments ("left-to-right") on the stack.
4158 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159 int arg_count = args->length();
4160 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004161 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004162 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004164 VirtualFrame::SpilledScope spilled_scope(frame_);
4165
mads.s.ager31e71382008-08-13 09:32:07 +00004166 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004167 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004168 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004169 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004171 // Call the construct call builtin that handles allocation and
4172 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004173 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004174 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004175 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004176
4177 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004178 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004179 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004180}
4181
4182
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004183void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004184 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004185 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004186 JumpTarget leave, null, function, non_function_constructor;
4187
4188 // Load the object into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004189 Load(args->at(0));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004190 frame_->EmitPop(r0);
4191
4192 // If the object is a smi, we return null.
4193 __ tst(r0, Operand(kSmiTagMask));
4194 null.Branch(eq);
4195
4196 // Check that the object is a JS object but take special care of JS
4197 // functions to make sure they have 'Function' as their class.
4198 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
4199 null.Branch(lt);
4200
4201 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4202 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4203 // LAST_JS_OBJECT_TYPE.
4204 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4205 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
4206 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4207 function.Branch(eq);
4208
4209 // Check if the constructor in the map is a function.
4210 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
4211 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
4212 non_function_constructor.Branch(ne);
4213
4214 // The r0 register now contains the constructor function. Grab the
4215 // instance class name from there.
4216 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
4217 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004218 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004219 leave.Jump();
4220
4221 // Functions have class 'Function'.
4222 function.Bind();
4223 __ mov(r0, Operand(Factory::function_class_symbol()));
4224 frame_->EmitPush(r0);
4225 leave.Jump();
4226
4227 // Objects with a non-function constructor have class 'Object'.
4228 non_function_constructor.Bind();
4229 __ mov(r0, Operand(Factory::Object_symbol()));
4230 frame_->EmitPush(r0);
4231 leave.Jump();
4232
4233 // Non-JS objects have class null.
4234 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004235 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004236 frame_->EmitPush(r0);
4237
4238 // All done.
4239 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004240}
4241
4242
ager@chromium.org7c537e22008-10-16 08:43:32 +00004243void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004244 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004245 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004246 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004247 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004249 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004250 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004251 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004252 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4253 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004254 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255 // Load the value.
4256 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004257 leave.Bind();
4258 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259}
4260
4261
ager@chromium.org7c537e22008-10-16 08:43:32 +00004262void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004263 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004264 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004265 JumpTarget leave;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004266 Load(args->at(0)); // Load the object.
4267 Load(args->at(1)); // Load the value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004268 frame_->EmitPop(r0); // r0 contains value
4269 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004270 // if (object->IsSmi()) return object.
4271 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004272 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004273 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
4274 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276 // Store the value.
4277 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
4278 // Update the write barrier.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004279 __ RecordWrite(r1, Operand(JSValue::kValueOffset - kHeapObjectTag), r2, r3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004280 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004281 leave.Bind();
4282 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004283}
4284
4285
ager@chromium.org7c537e22008-10-16 08:43:32 +00004286void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004287 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004288 Load(args->at(0));
4289 Register reg = frame_->PopToRegister();
4290 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004291 cc_reg_ = eq;
4292}
4293
4294
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004295void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4296 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4297 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004298#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004299 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004300 Load(args->at(1));
4301 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004302 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004303 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004304#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004305 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004306}
4307
4308
ager@chromium.org7c537e22008-10-16 08:43:32 +00004309void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004310 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004311 Load(args->at(0));
4312 Register reg = frame_->PopToRegister();
4313 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004314 cc_reg_ = eq;
4315}
4316
4317
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004318// Generates the Math.pow method - currently just calls runtime.
4319void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4320 ASSERT(args->length() == 2);
4321 Load(args->at(0));
4322 Load(args->at(1));
4323 frame_->CallRuntime(Runtime::kMath_pow, 2);
4324 frame_->EmitPush(r0);
4325}
4326
4327
4328// Generates the Math.sqrt method - currently just calls runtime.
4329void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4330 ASSERT(args->length() == 1);
4331 Load(args->at(0));
4332 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4333 frame_->EmitPush(r0);
4334}
4335
4336
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004337class DeferredStringCharCodeAt : public DeferredCode {
4338 public:
4339 DeferredStringCharCodeAt(Register object,
4340 Register index,
4341 Register scratch,
4342 Register result)
4343 : result_(result),
4344 char_code_at_generator_(object,
4345 index,
4346 scratch,
4347 result,
4348 &need_conversion_,
4349 &need_conversion_,
4350 &index_out_of_range_,
4351 STRING_INDEX_IS_NUMBER) {}
4352
4353 StringCharCodeAtGenerator* fast_case_generator() {
4354 return &char_code_at_generator_;
4355 }
4356
4357 virtual void Generate() {
4358 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4359 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4360
4361 __ bind(&need_conversion_);
4362 // Move the undefined value into the result register, which will
4363 // trigger conversion.
4364 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4365 __ jmp(exit_label());
4366
4367 __ bind(&index_out_of_range_);
4368 // When the index is out of range, the spec requires us to return
4369 // NaN.
4370 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4371 __ jmp(exit_label());
4372 }
4373
4374 private:
4375 Register result_;
4376
4377 Label need_conversion_;
4378 Label index_out_of_range_;
4379
4380 StringCharCodeAtGenerator char_code_at_generator_;
4381};
4382
4383
4384// This generates code that performs a String.prototype.charCodeAt() call
4385// or returns a smi in order to trigger conversion.
4386void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
4387 VirtualFrame::SpilledScope spilled_scope(frame_);
4388 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004389 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004390
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004391 Load(args->at(0));
4392 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004393
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004394 Register index = r1;
4395 Register object = r2;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004396
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004397 frame_->EmitPop(r1);
4398 frame_->EmitPop(r2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004399
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004400 // We need two extra registers.
4401 Register scratch = r3;
4402 Register result = r0;
4403
4404 DeferredStringCharCodeAt* deferred =
4405 new DeferredStringCharCodeAt(object,
4406 index,
4407 scratch,
4408 result);
4409 deferred->fast_case_generator()->GenerateFast(masm_);
4410 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004411 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004412}
4413
4414
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004415class DeferredStringCharFromCode : public DeferredCode {
4416 public:
4417 DeferredStringCharFromCode(Register code,
4418 Register result)
4419 : char_from_code_generator_(code, result) {}
4420
4421 StringCharFromCodeGenerator* fast_case_generator() {
4422 return &char_from_code_generator_;
4423 }
4424
4425 virtual void Generate() {
4426 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4427 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4428 }
4429
4430 private:
4431 StringCharFromCodeGenerator char_from_code_generator_;
4432};
4433
4434
4435// Generates code for creating a one-char string from a char code.
4436void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
4437 VirtualFrame::SpilledScope spilled_scope(frame_);
4438 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004439 ASSERT(args->length() == 1);
4440
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004441 Load(args->at(0));
4442
ager@chromium.orgac091b72010-05-05 07:34:42 +00004443 Register code = r1;
ager@chromium.orgac091b72010-05-05 07:34:42 +00004444 Register result = r0;
4445
ager@chromium.orgac091b72010-05-05 07:34:42 +00004446 frame_->EmitPop(code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004447
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004448 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4449 code, result);
4450 deferred->fast_case_generator()->GenerateFast(masm_);
4451 deferred->BindExit();
4452 frame_->EmitPush(result);
4453}
4454
4455
4456class DeferredStringCharAt : public DeferredCode {
4457 public:
4458 DeferredStringCharAt(Register object,
4459 Register index,
4460 Register scratch1,
4461 Register scratch2,
4462 Register result)
4463 : result_(result),
4464 char_at_generator_(object,
4465 index,
4466 scratch1,
4467 scratch2,
4468 result,
4469 &need_conversion_,
4470 &need_conversion_,
4471 &index_out_of_range_,
4472 STRING_INDEX_IS_NUMBER) {}
4473
4474 StringCharAtGenerator* fast_case_generator() {
4475 return &char_at_generator_;
4476 }
4477
4478 virtual void Generate() {
4479 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4480 char_at_generator_.GenerateSlow(masm(), call_helper);
4481
4482 __ bind(&need_conversion_);
4483 // Move smi zero into the result register, which will trigger
4484 // conversion.
4485 __ mov(result_, Operand(Smi::FromInt(0)));
4486 __ jmp(exit_label());
4487
4488 __ bind(&index_out_of_range_);
4489 // When the index is out of range, the spec requires us to return
4490 // the empty string.
4491 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4492 __ jmp(exit_label());
4493 }
4494
4495 private:
4496 Register result_;
4497
4498 Label need_conversion_;
4499 Label index_out_of_range_;
4500
4501 StringCharAtGenerator char_at_generator_;
4502};
4503
4504
4505// This generates code that performs a String.prototype.charAt() call
4506// or returns a smi in order to trigger conversion.
4507void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
4508 VirtualFrame::SpilledScope spilled_scope(frame_);
4509 Comment(masm_, "[ GenerateStringCharAt");
4510 ASSERT(args->length() == 2);
4511
4512 Load(args->at(0));
4513 Load(args->at(1));
4514
4515 Register index = r1;
4516 Register object = r2;
4517
4518 frame_->EmitPop(r1);
4519 frame_->EmitPop(r2);
4520
4521 // We need three extra registers.
4522 Register scratch1 = r3;
4523 Register scratch2 = r4;
4524 Register result = r0;
4525
4526 DeferredStringCharAt* deferred =
4527 new DeferredStringCharAt(object,
4528 index,
4529 scratch1,
4530 scratch2,
4531 result);
4532 deferred->fast_case_generator()->GenerateFast(masm_);
4533 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004534 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004535}
4536
4537
ager@chromium.org7c537e22008-10-16 08:43:32 +00004538void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004539 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004540 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004541 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004542 // We need the CC bits to come out as not_equal in the case where the
4543 // object is a smi. This can't be done with the usual test opcode so
4544 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004545 Register possible_array = frame_->PopToRegister();
4546 Register scratch = VirtualFrame::scratch0();
4547 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4548 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004549 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004550 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004551 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004552 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004553 cc_reg_ = eq;
4554}
4555
4556
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004557void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004558 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004559 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004560 JumpTarget answer;
4561 // We need the CC bits to come out as not_equal in the case where the
4562 // object is a smi. This can't be done with the usual test opcode so
4563 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004564 Register possible_regexp = frame_->PopToRegister();
4565 Register scratch = VirtualFrame::scratch0();
4566 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4567 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004568 answer.Branch(ne);
4569 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004570 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004571 answer.Bind();
4572 cc_reg_ = eq;
4573}
4574
4575
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004576void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4577 // This generates a fast version of:
4578 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004579 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004580 Load(args->at(0));
4581 Register possible_object = frame_->PopToRegister();
4582 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004583 false_target()->Branch(eq);
4584
4585 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004586 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004587 true_target()->Branch(eq);
4588
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004589 Register map_reg = VirtualFrame::scratch0();
4590 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004591 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004592 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4593 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004594 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004595
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004596 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4597 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004598 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004599 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004600 cc_reg_ = le;
4601}
4602
4603
4604void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4605 // This generates a fast version of:
4606 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004607 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004608 Load(args->at(0));
4609 Register possible_function = frame_->PopToRegister();
4610 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004611 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004612 Register map_reg = VirtualFrame::scratch0();
4613 Register scratch = VirtualFrame::scratch1();
4614 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004615 cc_reg_ = eq;
4616}
4617
4618
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004619void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004620 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004621 Load(args->at(0));
4622 Register possible_undetectable = frame_->PopToRegister();
4623 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004624 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004625 Register scratch = VirtualFrame::scratch0();
4626 __ ldr(scratch,
4627 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4628 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4629 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004630 cc_reg_ = ne;
4631}
4632
4633
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004634void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004635 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004636
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004637 Register scratch0 = VirtualFrame::scratch0();
4638 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004639 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004640 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004641
4642 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004643 __ ldr(scratch1,
4644 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4645 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4646 __ ldr(scratch0,
4647 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004648
4649 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004650 __ ldr(scratch1,
4651 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
4652 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004653 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004654}
4655
4656
ager@chromium.org7c537e22008-10-16 08:43:32 +00004657void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004658 ASSERT(args->length() == 0);
4659
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004660 Register tos = frame_->GetTOSRegister();
4661 Register scratch0 = VirtualFrame::scratch0();
4662 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004663
lrn@chromium.org25156de2010-04-06 13:10:27 +00004664 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004665 __ ldr(scratch0,
4666 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4667 __ ldr(scratch1,
4668 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
4669 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4670
4671 // Get the number of formal parameters.
4672 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004673
4674 // Arguments adaptor case: Read the arguments length from the
4675 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004676 __ ldr(tos,
4677 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
4678 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00004679
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004680 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004681}
4682
4683
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004684void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004685 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004686 ASSERT(args->length() == 1);
4687
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004688 // Satisfy contract with ArgumentsAccessStub:
4689 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004690 Load(args->at(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004691 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004692 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004693
4694 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004695 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004696 frame_->CallStub(&stub, 0);
4697 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004698}
4699
4700
ager@chromium.org357bf652010-04-12 11:30:10 +00004701void CodeGenerator::GenerateRandomHeapNumber(
4702 ZoneList<Expression*>* args) {
4703 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004704 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00004705
4706 Label slow_allocate_heapnumber;
4707 Label heapnumber_allocated;
4708
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004709 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
4710 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00004711 __ jmp(&heapnumber_allocated);
4712
4713 __ bind(&slow_allocate_heapnumber);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004714 // To allocate a heap number, and ensure that it is not a smi, we
4715 // call the runtime function FUnaryMinus on 0, returning the double
4716 // -0.0. A new, distinct heap number is returned each time.
ager@chromium.org357bf652010-04-12 11:30:10 +00004717 __ mov(r0, Operand(Smi::FromInt(0)));
4718 __ push(r0);
4719 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004720 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00004721
4722 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004723
4724 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
4725 // by computing:
4726 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
4727 if (CpuFeatures::IsSupported(VFP3)) {
4728 __ PrepareCallCFunction(0, r1);
4729 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
4730
4731 CpuFeatures::Scope scope(VFP3);
4732 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
4733 // Create this constant using mov/orr to avoid PC relative load.
4734 __ mov(r1, Operand(0x41000000));
4735 __ orr(r1, r1, Operand(0x300000));
4736 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
4737 __ vmov(d7, r0, r1);
4738 // Move 0x4130000000000000 to VFP.
4739 __ mov(r0, Operand(0));
4740 __ vmov(d8, r0, r1);
4741 // Subtract and store the result in the heap number.
4742 __ vsub(d7, d7, d8);
4743 __ sub(r0, r4, Operand(kHeapObjectTag));
4744 __ vstr(d7, r0, HeapNumber::kValueOffset);
4745 frame_->EmitPush(r4);
4746 } else {
4747 __ mov(r0, Operand(r4));
4748 __ PrepareCallCFunction(1, r1);
4749 __ CallCFunction(
4750 ExternalReference::fill_heap_number_with_random_function(), 1);
4751 frame_->EmitPush(r0);
4752 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004753}
4754
4755
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004756void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
4757 ASSERT_EQ(2, args->length());
4758
4759 Load(args->at(0));
4760 Load(args->at(1));
4761
ager@chromium.org5c838252010-02-19 08:53:10 +00004762 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004763 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004764 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004765 frame_->EmitPush(r0);
4766}
4767
4768
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004769void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4770 ASSERT_EQ(3, args->length());
4771
4772 Load(args->at(0));
4773 Load(args->at(1));
4774 Load(args->at(2));
4775
ager@chromium.org5c838252010-02-19 08:53:10 +00004776 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004777 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00004778 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004779 frame_->EmitPush(r0);
4780}
4781
4782
4783void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4784 ASSERT_EQ(2, args->length());
4785
4786 Load(args->at(0));
4787 Load(args->at(1));
4788
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004789 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004790 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004791 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004792 frame_->EmitPush(r0);
4793}
4794
4795
4796void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4797 ASSERT_EQ(4, args->length());
4798
4799 Load(args->at(0));
4800 Load(args->at(1));
4801 Load(args->at(2));
4802 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004803 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004804 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004805 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004806 frame_->EmitPush(r0);
4807}
4808
4809
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004810void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4811 // No stub. This code only occurs a few times in regexp.js.
4812 const int kMaxInlineLength = 100;
4813 ASSERT_EQ(3, args->length());
4814 Load(args->at(0)); // Size of array, smi.
4815 Load(args->at(1)); // "index" property value.
4816 Load(args->at(2)); // "input" property value.
4817 {
4818 VirtualFrame::SpilledScope spilled_scope(frame_);
4819 Label slowcase;
4820 Label done;
4821 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4822 STATIC_ASSERT(kSmiTag == 0);
4823 STATIC_ASSERT(kSmiTagSize == 1);
4824 __ tst(r1, Operand(kSmiTagMask));
4825 __ b(ne, &slowcase);
4826 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4827 __ b(hi, &slowcase);
4828 // Smi-tagging is equivalent to multiplying by 2.
4829 // Allocate RegExpResult followed by FixedArray with size in ebx.
4830 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4831 // Elements: [Map][Length][..elements..]
4832 // Size of JSArray with two in-object properties and the header of a
4833 // FixedArray.
4834 int objects_size =
4835 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4836 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4837 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004838 __ AllocateInNewSpace(
4839 r2, // In: Size, in words.
4840 r0, // Out: Start of allocation (tagged).
4841 r3, // Scratch register.
4842 r4, // Scratch register.
4843 &slowcase,
4844 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004845 // r0: Start of allocated area, object-tagged.
4846 // r1: Number of elements in array, as smi.
4847 // r5: Number of elements, untagged.
4848
4849 // Set JSArray map to global.regexp_result_map().
4850 // Set empty properties FixedArray.
4851 // Set elements to point to FixedArray allocated right after the JSArray.
4852 // Interleave operations for better latency.
4853 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4854 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4855 __ mov(r4, Operand(Factory::empty_fixed_array()));
4856 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4857 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4858 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4859 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4860 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4861
4862 // Set input, index and length fields from arguments.
4863 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4864 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4865 __ add(sp, sp, Operand(kPointerSize));
4866 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4867 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4868
4869 // Fill out the elements FixedArray.
4870 // r0: JSArray, tagged.
4871 // r3: FixedArray, tagged.
4872 // r5: Number of elements in array, untagged.
4873
4874 // Set map.
4875 __ mov(r2, Operand(Factory::fixed_array_map()));
4876 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4877 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004878 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
4879 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004880 // Fill contents of fixed-array with the-hole.
4881 __ mov(r2, Operand(Factory::the_hole_value()));
4882 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4883 // Fill fixed array elements with hole.
4884 // r0: JSArray, tagged.
4885 // r2: the hole.
4886 // r3: Start of elements in FixedArray.
4887 // r5: Number of elements to fill.
4888 Label loop;
4889 __ tst(r5, Operand(r5));
4890 __ bind(&loop);
4891 __ b(le, &done); // Jump if r1 is negative or zero.
4892 __ sub(r5, r5, Operand(1), SetCC);
4893 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4894 __ jmp(&loop);
4895
4896 __ bind(&slowcase);
4897 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4898
4899 __ bind(&done);
4900 }
4901 frame_->Forget(3);
4902 frame_->EmitPush(r0);
4903}
4904
4905
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004906class DeferredSearchCache: public DeferredCode {
4907 public:
4908 DeferredSearchCache(Register dst, Register cache, Register key)
4909 : dst_(dst), cache_(cache), key_(key) {
4910 set_comment("[ DeferredSearchCache");
4911 }
4912
4913 virtual void Generate();
4914
4915 private:
4916 Register dst_, cache_, key_;
4917};
4918
4919
4920void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00004921 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004922 __ CallRuntime(Runtime::kGetFromCache, 2);
4923 if (!dst_.is(r0)) {
4924 __ mov(dst_, r0);
4925 }
4926}
4927
4928
4929void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4930 ASSERT_EQ(2, args->length());
4931
4932 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4933 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4934
4935 Handle<FixedArray> jsfunction_result_caches(
4936 Top::global_context()->jsfunction_result_caches());
4937 if (jsfunction_result_caches->length() <= cache_id) {
4938 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004939 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004940 return;
4941 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004942
4943 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004944
4945 VirtualFrame::SpilledScope spilled_scope(frame_);
4946
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004947 frame_->EmitPop(r2);
4948
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004949 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
4950 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
4951 __ ldr(r1, ContextOperand(r1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
4952 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(cache_id)));
4953
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004954 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4955
4956 const int kFingerOffset =
4957 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4958 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004959 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4960 // r0 now holds finger offset as a smi.
4961 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4962 // r3 now points to the start of fixed array elements.
4963 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4964 // Note side effect of PreIndex: r3 now points to the key of the pair.
4965 __ cmp(r2, r0);
4966 deferred->Branch(ne);
4967
4968 __ ldr(r0, MemOperand(r3, kPointerSize));
4969
4970 deferred->BindExit();
4971 frame_->EmitPush(r0);
4972}
4973
4974
ager@chromium.org5c838252010-02-19 08:53:10 +00004975void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4976 ASSERT_EQ(args->length(), 1);
4977
4978 // Load the argument on the stack and jump to the runtime.
4979 Load(args->at(0));
4980
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004981 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004982 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004983 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004984 frame_->EmitPush(r0);
4985}
4986
4987
ager@chromium.orgac091b72010-05-05 07:34:42 +00004988class DeferredSwapElements: public DeferredCode {
4989 public:
4990 DeferredSwapElements(Register object, Register index1, Register index2)
4991 : object_(object), index1_(index1), index2_(index2) {
4992 set_comment("[ DeferredSwapElements");
4993 }
4994
4995 virtual void Generate();
4996
4997 private:
4998 Register object_, index1_, index2_;
4999};
5000
5001
5002void DeferredSwapElements::Generate() {
5003 __ push(object_);
5004 __ push(index1_);
5005 __ push(index2_);
5006 __ CallRuntime(Runtime::kSwapElements, 3);
5007}
5008
5009
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005010void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5011 Comment cmnt(masm_, "[ GenerateSwapElements");
5012
5013 ASSERT_EQ(3, args->length());
5014
5015 Load(args->at(0));
5016 Load(args->at(1));
5017 Load(args->at(2));
5018
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005019 VirtualFrame::SpilledScope spilled_scope(frame_);
5020
ager@chromium.orgac091b72010-05-05 07:34:42 +00005021 Register index2 = r2;
5022 Register index1 = r1;
5023 Register object = r0;
5024 Register tmp1 = r3;
5025 Register tmp2 = r4;
5026
5027 frame_->EmitPop(index2);
5028 frame_->EmitPop(index1);
5029 frame_->EmitPop(object);
5030
5031 DeferredSwapElements* deferred =
5032 new DeferredSwapElements(object, index1, index2);
5033
5034 // Fetch the map and check if array is in fast case.
5035 // Check that object doesn't require security checks and
5036 // has no indexed interceptor.
5037 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5038 deferred->Branch(lt);
5039 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5040 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5041 deferred->Branch(nz);
5042
5043 // Check the object's elements are in fast case.
5044 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5045 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5046 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5047 __ cmp(tmp2, ip);
5048 deferred->Branch(ne);
5049
5050 // Smi-tagging is equivalent to multiplying by 2.
5051 STATIC_ASSERT(kSmiTag == 0);
5052 STATIC_ASSERT(kSmiTagSize == 1);
5053
5054 // Check that both indices are smis.
5055 __ mov(tmp2, index1);
5056 __ orr(tmp2, tmp2, index2);
5057 __ tst(tmp2, Operand(kSmiTagMask));
5058 deferred->Branch(nz);
5059
5060 // Bring the offsets into the fixed array in tmp1 into index1 and
5061 // index2.
5062 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5063 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5064 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5065
5066 // Swap elements.
5067 Register tmp3 = object;
5068 object = no_reg;
5069 __ ldr(tmp3, MemOperand(tmp1, index1));
5070 __ ldr(tmp2, MemOperand(tmp1, index2));
5071 __ str(tmp3, MemOperand(tmp1, index2));
5072 __ str(tmp2, MemOperand(tmp1, index1));
5073
5074 Label done;
5075 __ InNewSpace(tmp1, tmp2, eq, &done);
5076 // Possible optimization: do a check that both values are Smis
5077 // (or them and test against Smi mask.)
5078
5079 __ mov(tmp2, tmp1);
5080 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5081 __ CallStub(&recordWrite1);
5082
5083 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5084 __ CallStub(&recordWrite2);
5085
5086 __ bind(&done);
5087
5088 deferred->BindExit();
5089 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5090 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005091}
5092
5093
ager@chromium.org357bf652010-04-12 11:30:10 +00005094void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5095 Comment cmnt(masm_, "[ GenerateCallFunction");
5096
5097 ASSERT(args->length() >= 2);
5098
5099 int n_args = args->length() - 2; // for receiver and function.
5100 Load(args->at(0)); // receiver
5101 for (int i = 0; i < n_args; i++) {
5102 Load(args->at(i + 1));
5103 }
5104 Load(args->at(n_args + 1)); // function
5105 frame_->CallJSFunction(n_args);
5106 frame_->EmitPush(r0);
5107}
5108
5109
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005110void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5111 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005112 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005113 if (CpuFeatures::IsSupported(VFP3)) {
5114 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5115 frame_->SpillAllButCopyTOSToR0();
5116 frame_->CallStub(&stub, 1);
5117 } else {
5118 frame_->CallRuntime(Runtime::kMath_sin, 1);
5119 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005120 frame_->EmitPush(r0);
5121}
5122
5123
5124void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5125 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005126 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005127 if (CpuFeatures::IsSupported(VFP3)) {
5128 TranscendentalCacheStub stub(TranscendentalCache::COS);
5129 frame_->SpillAllButCopyTOSToR0();
5130 frame_->CallStub(&stub, 1);
5131 } else {
5132 frame_->CallRuntime(Runtime::kMath_cos, 1);
5133 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005134 frame_->EmitPush(r0);
5135}
5136
5137
ager@chromium.org7c537e22008-10-16 08:43:32 +00005138void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005139 ASSERT(args->length() == 2);
5140
5141 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005142 Load(args->at(0));
5143 Load(args->at(1));
5144 Register lhs = frame_->PopToRegister();
5145 Register rhs = frame_->PopToRegister(lhs);
5146 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005147 cc_reg_ = eq;
5148}
5149
5150
ager@chromium.org7c537e22008-10-16 08:43:32 +00005151void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005152#ifdef DEBUG
5153 int original_height = frame_->height();
5154#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005155 if (CheckForInlineRuntimeCall(node)) {
5156 ASSERT((has_cc() && frame_->height() == original_height) ||
5157 (!has_cc() && frame_->height() == original_height + 1));
5158 return;
5159 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005160
5161 ZoneList<Expression*>* args = node->arguments();
5162 Comment cmnt(masm_, "[ CallRuntime");
5163 Runtime::Function* function = node->function();
5164
ager@chromium.org41826e72009-03-30 13:30:57 +00005165 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005166 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005167 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005168 Register scratch = VirtualFrame::scratch0();
5169 __ ldr(scratch, GlobalObject());
5170 Register builtins = frame_->GetTOSRegister();
5171 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5172 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005173 }
mads.s.ager31e71382008-08-13 09:32:07 +00005174
ager@chromium.org41826e72009-03-30 13:30:57 +00005175 // Push the arguments ("left-to-right").
5176 int arg_count = args->length();
5177 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005178 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005179 }
mads.s.ager31e71382008-08-13 09:32:07 +00005180
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005181 VirtualFrame::SpilledScope spilled_scope(frame_);
5182
ager@chromium.org41826e72009-03-30 13:30:57 +00005183 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005184 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005185 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005186 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5187 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005188 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005189 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005190 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005191 } else {
5192 // Call the C runtime function.
5193 frame_->CallRuntime(function, arg_count);
5194 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005195 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005196 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005197}
5198
5199
ager@chromium.org7c537e22008-10-16 08:43:32 +00005200void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005201#ifdef DEBUG
5202 int original_height = frame_->height();
5203#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00005204 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005205 Comment cmnt(masm_, "[ UnaryOperation");
5206
5207 Token::Value op = node->op();
5208
5209 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005210 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005211 // LoadCondition may (and usually does) leave a test and branch to
5212 // be emitted by the caller. In that case, negate the condition.
5213 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005214
5215 } else if (op == Token::DELETE) {
5216 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005217 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005218 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005219 Load(property->obj());
5220 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005221 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005222 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005223
mads.s.ager31e71382008-08-13 09:32:07 +00005224 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005225 Slot* slot = variable->slot();
5226 if (variable->is_global()) {
5227 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005228 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005229 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005230 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005231
5232 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5233 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005234 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005235 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005236 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005237 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005238 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005239 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005240 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005241 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005242
mads.s.ager31e71382008-08-13 09:32:07 +00005243 } else {
5244 // Default: Result of deleting non-global, not dynamically
5245 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005246 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005247 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005248
5249 } else {
5250 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005251 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005252 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005253 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005254 }
5255
5256 } else if (op == Token::TYPEOF) {
5257 // Special case for loading the typeof expression; see comment on
5258 // LoadTypeofExpression().
5259 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005260 frame_->CallRuntime(Runtime::kTypeof, 1);
5261 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005262
5263 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005264 bool overwrite =
5265 (node->expression()->AsBinaryOperation() != NULL &&
5266 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005267 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005268 switch (op) {
5269 case Token::NOT:
5270 case Token::DELETE:
5271 case Token::TYPEOF:
5272 UNREACHABLE(); // handled above
5273 break;
5274
5275 case Token::SUB: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005276 VirtualFrame::SpilledScope spilled(frame_);
5277 frame_->EmitPop(r0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005278 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005279 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005280 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005281 break;
5282 }
5283
5284 case Token::BIT_NOT: {
5285 // smi check
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005286 VirtualFrame::SpilledScope spilled(frame_);
5287 frame_->EmitPop(r0);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005288 JumpTarget smi_label;
5289 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005290 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005291 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005292
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005293 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5294 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005295 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005297 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005298 __ mvn(r0, Operand(r0));
5299 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005300 continue_label.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005301 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005302 break;
5303 }
5304
5305 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005306 frame_->Drop();
5307 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005308 break;
5309
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005310 case Token::ADD: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005311 VirtualFrame::SpilledScope spilled(frame_);
5312 frame_->EmitPop(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005313 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005314 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005315 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005316 continue_label.Branch(eq);
5317 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005318 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005319 continue_label.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005320 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005321 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005322 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005323 default:
5324 UNREACHABLE();
5325 }
5326 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005327 ASSERT(!has_valid_frame() ||
5328 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005329 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005330}
5331
5332
ager@chromium.org7c537e22008-10-16 08:43:32 +00005333void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005334#ifdef DEBUG
5335 int original_height = frame_->height();
5336#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005337 Comment cmnt(masm_, "[ CountOperation");
5338
5339 bool is_postfix = node->is_postfix();
5340 bool is_increment = node->op() == Token::INC;
5341
5342 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5343 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005344 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005345
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005346 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5347 // The type info declares that this variable is always a Smi. That
5348 // means it is a Smi both before and after the increment/decrement.
5349 // Lets make use of that to make a very minimal count.
5350 Reference target(this, node->expression(), !is_const);
5351 ASSERT(!target.is_illegal());
5352 target.GetValue(); // Pushes the value.
5353 Register value = frame_->PopToRegister();
5354 if (is_postfix) frame_->EmitPush(value);
5355 if (is_increment) {
5356 __ add(value, value, Operand(Smi::FromInt(1)));
5357 } else {
5358 __ sub(value, value, Operand(Smi::FromInt(1)));
5359 }
5360 frame_->EmitPush(value);
5361 target.SetValue(NOT_CONST_INIT);
5362 if (is_postfix) frame_->Pop();
5363 ASSERT_EQ(original_height + 1, frame_->height());
5364 return;
5365 }
5366
5367 // If it's a postfix expression and its result is not ignored and the
5368 // reference is non-trivial, then push a placeholder on the stack now
5369 // to hold the result of the expression.
5370 bool placeholder_pushed = false;
5371 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005372 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005373 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005374 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005375
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005376 // A constant reference is not saved to, so a constant reference is not a
5377 // compound assignment reference.
5378 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005379 if (target.is_illegal()) {
5380 // Spoof the virtual frame to have the expected height (one higher
5381 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005382 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005383 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005384 return;
5385 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005386
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005387 // This pushes 0, 1 or 2 words on the object to be used later when updating
5388 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005389 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005390
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005391 JumpTarget slow;
5392 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005393
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005394 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005395
5396 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005397 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005398 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005399 } else if (is_postfix) {
5400 frame_->EmitPush(value);
5401 __ mov(VirtualFrame::scratch0(), value);
5402 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005403 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005404
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005405 // Check for smi operand.
5406 __ tst(value, Operand(kSmiTagMask));
5407 slow.Branch(ne);
5408
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005409 // Perform optimistic increment/decrement.
5410 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005411 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005412 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005413 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005414 }
5415
5416 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005417 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005418
5419 // Revert optimistic increment/decrement.
5420 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005421 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005422 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005423 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005424 }
5425
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005426 // Slow case: Convert to number. At this point the
5427 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005428 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005429
5430 // Convert the operand to a number.
5431 frame_->EmitPush(value);
5432
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005433 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005434 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005435 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005436
5437 if (is_postfix) {
5438 // Postfix: store to result (on the stack).
5439 __ str(r0, frame_->ElementAt(target.size()));
5440 }
5441
5442 // Compute the new value.
5443 frame_->EmitPush(r0);
5444 frame_->EmitPush(Operand(Smi::FromInt(1)));
5445 if (is_increment) {
5446 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5447 } else {
5448 frame_->CallRuntime(Runtime::kNumberSub, 2);
5449 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005450 }
5451
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005452 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005453 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005454 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005455 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005456 frame_->EmitPush(value);
5457 // Set the target with the result, leaving the result on
5458 // top of the stack. Removes the target from the stack if
5459 // it has a non-zero size.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005460 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005461 }
5462
5463 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005464 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005465 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005466}
5467
5468
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005469void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005470 // According to ECMA-262 section 11.11, page 58, the binary logical
5471 // operators must yield the result of one of the two expressions
5472 // before any ToBoolean() conversions. This means that the value
5473 // produced by a && or || operator is not necessarily a boolean.
5474
5475 // NOTE: If the left hand side produces a materialized value (not in
5476 // the CC register), we force the right hand side to do the
5477 // same. This is necessary because we may have to branch to the exit
5478 // after evaluating the left hand side (due to the shortcut
5479 // semantics), but the compiler must (statically) know if the result
5480 // of compiling the binary operation is materialized or not.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005481 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005482 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005483 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005484 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005485 if (has_valid_frame() && !has_cc()) {
5486 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005487 JumpTarget pop_and_continue;
5488 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005489
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005490 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005491 // Avoid popping the result if it converts to 'false' using the
5492 // standard ToBoolean() conversion as described in ECMA-262,
5493 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005494 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005495 Branch(false, &exit);
5496
5497 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005498 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005499 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005500
5501 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005502 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005503 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005504
5505 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005506 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005507 } else if (has_cc() || is_true.is_linked()) {
5508 // The left-hand side is either (a) partially compiled to
5509 // control flow with a final branch left to emit or (b) fully
5510 // compiled to control flow and possibly true.
5511 if (has_cc()) {
5512 Branch(false, false_target());
5513 }
5514 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005515 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005516 } else {
5517 // Nothing to do.
5518 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005519 }
5520
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005521 } else {
5522 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005523 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005524 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005525 if (has_valid_frame() && !has_cc()) {
5526 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005527 JumpTarget pop_and_continue;
5528 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005529
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005530 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005531 // Avoid popping the result if it converts to 'true' using the
5532 // standard ToBoolean() conversion as described in ECMA-262,
5533 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005534 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005535 Branch(true, &exit);
5536
5537 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005538 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005539 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005540
5541 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005542 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005543 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005544
5545 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005546 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005547 } else if (has_cc() || is_false.is_linked()) {
5548 // The left-hand side is either (a) partially compiled to
5549 // control flow with a final branch left to emit or (b) fully
5550 // compiled to control flow and possibly false.
5551 if (has_cc()) {
5552 Branch(true, true_target());
5553 }
5554 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005555 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005556 } else {
5557 // Nothing to do.
5558 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005559 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005560 }
5561}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005562
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005563
5564void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5565#ifdef DEBUG
5566 int original_height = frame_->height();
5567#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005568 Comment cmnt(masm_, "[ BinaryOperation");
5569
5570 if (node->op() == Token::AND || node->op() == Token::OR) {
5571 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005572 } else {
5573 // Optimize for the case where (at least) one of the expressions
5574 // is a literal small integer.
5575 Literal* lliteral = node->left()->AsLiteral();
5576 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005577 // NOTE: The code below assumes that the slow cases (calls to runtime)
5578 // never return a constant/immutable object.
5579 bool overwrite_left =
5580 (node->left()->AsBinaryOperation() != NULL &&
5581 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5582 bool overwrite_right =
5583 (node->right()->AsBinaryOperation() != NULL &&
5584 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005585
5586 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005587 VirtualFrame::RegisterAllocationScope scope(this);
5588 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005589 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005590 SmiOperation(node->op(),
5591 rliteral->handle(),
5592 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005593 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005594 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005595 VirtualFrame::RegisterAllocationScope scope(this);
5596 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005597 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005598 SmiOperation(node->op(),
5599 lliteral->handle(),
5600 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005601 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005602 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005603 GenerateInlineSmi inline_smi =
5604 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
5605 if (lliteral != NULL) {
5606 ASSERT(!lliteral->handle()->IsSmi());
5607 inline_smi = DONT_GENERATE_INLINE_SMI;
5608 }
5609 if (rliteral != NULL) {
5610 ASSERT(!rliteral->handle()->IsSmi());
5611 inline_smi = DONT_GENERATE_INLINE_SMI;
5612 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005613 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005614 OverwriteMode overwrite_mode = NO_OVERWRITE;
5615 if (overwrite_left) {
5616 overwrite_mode = OVERWRITE_LEFT;
5617 } else if (overwrite_right) {
5618 overwrite_mode = OVERWRITE_RIGHT;
5619 }
ager@chromium.org357bf652010-04-12 11:30:10 +00005620 Load(node->left());
5621 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005622 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005623 }
5624 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005625 ASSERT(!has_valid_frame() ||
5626 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005627 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005628}
5629
5630
ager@chromium.org7c537e22008-10-16 08:43:32 +00005631void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005632#ifdef DEBUG
5633 int original_height = frame_->height();
5634#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005635 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005636 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005637}
5638
5639
ager@chromium.org7c537e22008-10-16 08:43:32 +00005640void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005641#ifdef DEBUG
5642 int original_height = frame_->height();
5643#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005644 Comment cmnt(masm_, "[ CompareOperation");
5645
ager@chromium.org357bf652010-04-12 11:30:10 +00005646 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
5647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005648 // Get the expressions from the node.
5649 Expression* left = node->left();
5650 Expression* right = node->right();
5651 Token::Value op = node->op();
5652
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005653 // To make null checks efficient, we check if either left or right is the
5654 // literal 'null'. If so, we optimize the code by inlining a null check
5655 // instead of calling the (very) general runtime routine for checking
5656 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005657 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005658 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005659 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005660 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005661 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
5662 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005663 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005664 Load(left_is_null ? right : left);
5665 Register tos = frame_->PopToRegister();
5666 // JumpTargets can't cope with register allocation yet.
5667 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005668 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005669 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005670
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005671 // The 'null' value is only equal to 'undefined' if using non-strict
5672 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005673 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005674 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005675
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005676 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005677 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005678 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005679
ager@chromium.org357bf652010-04-12 11:30:10 +00005680 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005681 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005682
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005683 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005684 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5685 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
5686 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5687 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005688 }
5689
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005691 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005692 return;
5693 }
5694 }
5695
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005696 // To make typeof testing for natives implemented in JavaScript really
5697 // efficient, we generate special code for expressions of the form:
5698 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005699 UnaryOperation* operation = left->AsUnaryOperation();
5700 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
5701 (operation != NULL && operation->op() == Token::TYPEOF) &&
5702 (right->AsLiteral() != NULL &&
5703 right->AsLiteral()->handle()->IsString())) {
5704 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
5705
ager@chromium.org357bf652010-04-12 11:30:10 +00005706 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005707 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00005708 Register tos = frame_->PopToRegister();
5709
5710 // JumpTargets can't cope with register allocation yet.
5711 frame_->SpillAll();
5712
5713 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005714
5715 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005716 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005717 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005718 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005719 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005720 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005721 cc_reg_ = eq;
5722
5723 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005724 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005725 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005726
ager@chromium.org357bf652010-04-12 11:30:10 +00005727 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005728
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005729 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005730 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5731 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5732 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005733 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005734
ager@chromium.org357bf652010-04-12 11:30:10 +00005735 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
5736 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005737 cc_reg_ = lt;
5738
5739 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005740 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005741 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005742 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005743 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005744 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005745 cc_reg_ = eq;
5746
5747 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005748 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005749 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005750 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005751
ager@chromium.org357bf652010-04-12 11:30:10 +00005752 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005753 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005754
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005755 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005756 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
5757 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
5758 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
5759 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005760
5761 cc_reg_ = eq;
5762
5763 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005764 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005765 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00005766 Register map_reg = scratch;
5767 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005768 true_target()->Branch(eq);
5769 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00005770 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005771 cc_reg_ = eq;
5772
5773 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005774 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005775 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005776
ager@chromium.orgab99eea2009-08-25 07:05:41 +00005777 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00005778 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005779 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005780
ager@chromium.org357bf652010-04-12 11:30:10 +00005781 Register map_reg = scratch;
5782 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005783 false_target()->Branch(eq);
5784
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005785 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00005786 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
5787 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
5788 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005789 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005790
ager@chromium.org357bf652010-04-12 11:30:10 +00005791 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
5792 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005793 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00005794 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005795 cc_reg_ = le;
5796
5797 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005798 // Uncommon case: typeof testing against a string literal that is
5799 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005800 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005801 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005802 ASSERT(!has_valid_frame() ||
5803 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005804 return;
5805 }
5806
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005807 switch (op) {
5808 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005809 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005810 break;
5811
5812 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005813 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005814 break;
5815
5816 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005817 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005818 break;
5819
5820 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005821 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005822 break;
5823
5824 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005825 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005826 break;
5827
5828 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005829 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005830 break;
5831
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005832 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005833 VirtualFrame::SpilledScope scope(frame_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005834 Load(left);
5835 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005836 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005837 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005838 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005839 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005840
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005841 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005842 VirtualFrame::SpilledScope scope(frame_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005843 Load(left);
5844 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005845 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005846 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005847 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005848 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005849 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005850 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005851 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005852
5853 default:
5854 UNREACHABLE();
5855 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005856 ASSERT((has_cc() && frame_->height() == original_height) ||
5857 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005858}
5859
5860
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005861class DeferredReferenceGetNamedValue: public DeferredCode {
5862 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005863 explicit DeferredReferenceGetNamedValue(Register receiver,
5864 Handle<String> name)
5865 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005866 set_comment("[ DeferredReferenceGetNamedValue");
5867 }
5868
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005869 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005870
5871 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005872 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005873 Handle<String> name_;
5874};
5875
5876
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005877// Convention for this is that on entry the receiver is in a register that
5878// is not used by the stack. On exit the answer is found in that same
5879// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005880void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005881#ifdef DEBUG
5882 int expected_height = frame_state()->frame()->height();
5883#endif
5884 VirtualFrame copied_frame(*frame_state()->frame());
5885 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005886
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005887 Register scratch1 = VirtualFrame::scratch0();
5888 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005889 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005890 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
5891 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
5892
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005893 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
5894 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005895 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005896
5897 // The rest of the instructions in the deferred code must be together.
5898 { Assembler::BlockConstPoolScope block_const_pool(masm_);
5899 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
5900 __ Call(ic, RelocInfo::CODE_TARGET);
5901 // The call must be followed by a nop(1) instruction to indicate that the
5902 // in-object has been inlined.
5903 __ nop(PROPERTY_ACCESS_INLINED);
5904
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005905 // At this point the answer is in r0. We move it to the expected register
5906 // if necessary.
5907 __ Move(receiver_, r0);
5908
5909 // Now go back to the frame that we entered with. This will not overwrite
5910 // the receiver register since that register was not in use when we came
5911 // in. The instructions emitted by this merge are skipped over by the
5912 // inline load patching mechanism when looking for the branch instruction
5913 // that tells it where the code to patch is.
5914 copied_frame.MergeTo(frame_state()->frame());
5915
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005916 // Block the constant pool for one more instruction after leaving this
5917 // constant pool block scope to include the branch instruction ending the
5918 // deferred code.
5919 __ BlockConstPoolFor(1);
5920 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005921 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005922}
5923
5924
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005925class DeferredReferenceGetKeyedValue: public DeferredCode {
5926 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005927 DeferredReferenceGetKeyedValue(Register key, Register receiver)
5928 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005929 set_comment("[ DeferredReferenceGetKeyedValue");
5930 }
5931
5932 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005933
5934 private:
5935 Register key_;
5936 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005937};
5938
5939
5940void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005941 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
5942 (key_.is(r1) && receiver_.is(r0)));
5943
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005944 Register scratch1 = VirtualFrame::scratch0();
5945 Register scratch2 = VirtualFrame::scratch1();
5946 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
5947 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
5948
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005949 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
5950 // convention.
5951 if (key_.is(r1)) {
5952 __ Swap(r0, r1, ip);
5953 }
5954
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005955 // The rest of the instructions in the deferred code must be together.
5956 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005957 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005958 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
5959 __ Call(ic, RelocInfo::CODE_TARGET);
5960 // The call must be followed by a nop instruction to indicate that the
5961 // keyed load has been inlined.
5962 __ nop(PROPERTY_ACCESS_INLINED);
5963
5964 // Block the constant pool for one more instruction after leaving this
5965 // constant pool block scope to include the branch instruction ending the
5966 // deferred code.
5967 __ BlockConstPoolFor(1);
5968 }
5969}
5970
5971
5972class DeferredReferenceSetKeyedValue: public DeferredCode {
5973 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005974 DeferredReferenceSetKeyedValue(Register value,
5975 Register key,
5976 Register receiver)
5977 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005978 set_comment("[ DeferredReferenceSetKeyedValue");
5979 }
5980
5981 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005982
5983 private:
5984 Register value_;
5985 Register key_;
5986 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005987};
5988
5989
5990void DeferredReferenceSetKeyedValue::Generate() {
5991 Register scratch1 = VirtualFrame::scratch0();
5992 Register scratch2 = VirtualFrame::scratch1();
5993 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
5994 __ IncrementCounter(
5995 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
5996
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005997 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
5998 // calling convention.
5999 if (value_.is(r1)) {
6000 __ Swap(r0, r1, ip);
6001 }
6002 ASSERT(receiver_.is(r2));
6003
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006004 // The rest of the instructions in the deferred code must be together.
6005 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006006 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6007 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006008 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6009 __ Call(ic, RelocInfo::CODE_TARGET);
6010 // The call must be followed by a nop instruction to indicate that the
6011 // keyed store has been inlined.
6012 __ nop(PROPERTY_ACCESS_INLINED);
6013
6014 // Block the constant pool for one more instruction after leaving this
6015 // constant pool block scope to include the branch instruction ending the
6016 // deferred code.
6017 __ BlockConstPoolFor(1);
6018 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006019}
6020
6021
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006022// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006023void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6024 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6025 Comment cmnt(masm(), "[ Load from named Property");
6026 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006027 frame_->CallLoadIC(name,
6028 is_contextual
6029 ? RelocInfo::CODE_TARGET_CONTEXT
6030 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006031 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006032 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006033 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006034 Comment cmnt(masm(), "[ Inlined named property load");
6035
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006036 // Counter will be decremented in the deferred code. Placed here to avoid
6037 // having it in the instruction stream below where patching will occur.
6038 __ IncrementCounter(&Counters::named_load_inline, 1,
6039 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006040
6041 // The following instructions are the inlined load of an in-object property.
6042 // Parts of this code is patched, so the exact instructions generated needs
6043 // to be fixed. Therefore the instruction pool is blocked when generating
6044 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006045
6046 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006047 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006048
6049 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006050 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006051
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006052#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006053 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006054 Label check_inlined_codesize;
6055 masm_->bind(&check_inlined_codesize);
6056#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006057
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006058 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006059 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006060 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006061 deferred->Branch(eq);
6062
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006063 Register scratch = VirtualFrame::scratch0();
6064 Register scratch2 = VirtualFrame::scratch1();
6065
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006066 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006067 // code. Therefore we can't use a LoadRoot call.
6068 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6069 __ mov(scratch2, Operand(Factory::null_value()));
6070 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006071 deferred->Branch(ne);
6072
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006073 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006074 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006075 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006076
6077 // Make sure that the expected number of instructions are generated.
6078 ASSERT_EQ(kInlinedNamedLoadInstructions,
6079 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006080 }
6081
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006082 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006083 // At this point the receiver register has the result, either from the
6084 // deferred code or from the inlined code.
6085 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006086 }
6087}
6088
6089
ager@chromium.orgac091b72010-05-05 07:34:42 +00006090void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6091#ifdef DEBUG
6092 int expected_height = frame_->height() - (is_contextual ? 1 : 2);
6093#endif
6094 frame_->CallStoreIC(name, is_contextual);
6095
6096 ASSERT_EQ(expected_height, frame_->height());
6097}
6098
6099
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006100void CodeGenerator::EmitKeyedLoad() {
6101 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006102 Comment cmnt(masm_, "[ Load from keyed property");
6103 frame_->CallKeyedLoadIC();
6104 } else {
6105 // Inline the keyed load.
6106 Comment cmnt(masm_, "[ Inlined load from keyed property");
6107
6108 // Counter will be decremented in the deferred code. Placed here to avoid
6109 // having it in the instruction stream below where patching will occur.
6110 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6111 frame_->scratch0(), frame_->scratch1());
6112
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006113 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006114 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006115 Register key = frame_->PopToRegister();
6116 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006117 VirtualFrame::SpilledScope spilled(frame_);
6118
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006119 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006120 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006121 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006122
6123 // Check that the receiver is a heap object.
6124 __ tst(receiver, Operand(kSmiTagMask));
6125 deferred->Branch(eq);
6126
6127 // The following instructions are the part of the inlined load keyed
6128 // property code which can be patched. Therefore the exact number of
6129 // instructions generated need to be fixed, so the constant pool is blocked
6130 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006131 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6132 Register scratch1 = VirtualFrame::scratch0();
6133 Register scratch2 = VirtualFrame::scratch1();
6134 // Check the map. The null map used below is patched by the inline cache
6135 // code.
6136 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006137
6138 // Check that the key is a smi.
6139 if (!key_is_known_smi) {
6140 __ tst(key, Operand(kSmiTagMask));
6141 deferred->Branch(ne);
6142 }
6143
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006144#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006145 Label check_inlined_codesize;
6146 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006147#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006148 __ mov(scratch2, Operand(Factory::null_value()));
6149 __ cmp(scratch1, scratch2);
6150 deferred->Branch(ne);
6151
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006152 // Get the elements array from the receiver and check that it
6153 // is not a dictionary.
6154 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6155 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
6156 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
6157 __ cmp(scratch2, ip);
6158 deferred->Branch(ne);
6159
6160 // Check that key is within bounds. Use unsigned comparison to handle
6161 // negative keys.
6162 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006163 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006164 deferred->Branch(ls); // Unsigned less equal.
6165
6166 // Load and check that the result is not the hole (key is a smi).
6167 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6168 __ add(scratch1,
6169 scratch1,
6170 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006171 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006172 MemOperand(scratch1, key, LSL,
6173 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006174 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006175 deferred->Branch(eq);
6176
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006177 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006178 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006179 ASSERT_EQ(kInlinedKeyedLoadInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006180 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6181 }
6182
6183 deferred->BindExit();
6184 }
6185}
6186
6187
6188void CodeGenerator::EmitKeyedStore(StaticType* key_type) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006189 // Generate inlined version of the keyed store if the code is in a loop
6190 // and the key is likely to be a smi.
6191 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6192 // Inline the keyed store.
6193 Comment cmnt(masm_, "[ Inlined store to keyed property");
6194
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006195 Register scratch1 = VirtualFrame::scratch0();
6196 Register scratch2 = VirtualFrame::scratch1();
6197 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006198
6199 // Counter will be decremented in the deferred code. Placed here to avoid
6200 // having it in the instruction stream below where patching will occur.
6201 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006202 scratch1, scratch2);
6203
6204 // Load the value, key and receiver from the stack.
6205 Register value = frame_->PopToRegister();
6206 Register key = frame_->PopToRegister(value);
6207 Register receiver = r2;
6208 frame_->EmitPop(receiver);
6209 VirtualFrame::SpilledScope spilled(frame_);
6210
6211 // The deferred code expects value, key and receiver in registers.
6212 DeferredReferenceSetKeyedValue* deferred =
6213 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006214
6215 // Check that the value is a smi. As this inlined code does not set the
6216 // write barrier it is only possible to store smi values.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006217 __ tst(value, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006218 deferred->Branch(ne);
6219
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006220 // Check that the key is a smi.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006221 __ tst(key, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006222 deferred->Branch(ne);
6223
6224 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006225 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006226 deferred->Branch(eq);
6227
6228 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006229 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006230 deferred->Branch(ne);
6231
6232 // Check that the key is within bounds. Both the key and the length of
6233 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006234 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6235 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006236 deferred->Branch(ls); // Unsigned less equal.
6237
6238 // The following instructions are the part of the inlined store keyed
6239 // property code which can be patched. Therefore the exact number of
6240 // instructions generated need to be fixed, so the constant pool is blocked
6241 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006242 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6243 // Get the elements array from the receiver and check that it
6244 // is not a dictionary.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006245 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6246 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006247 // Read the fixed array map from the constant pool (not from the root
6248 // array) so that the value can be patched. When debugging, we patch this
6249 // comparison to always fail so that we will hit the IC call in the
6250 // deferred code which will allow the debugger to break for fast case
6251 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006252#ifdef DEBUG
6253 Label check_inlined_codesize;
6254 masm_->bind(&check_inlined_codesize);
6255#endif
6256 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6257 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006258 deferred->Branch(ne);
6259
6260 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006261 __ add(scratch1, scratch1,
6262 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6263 __ str(value,
6264 MemOperand(scratch1, key, LSL,
6265 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006266
6267 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006268 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006269 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6270 }
6271
6272 deferred->BindExit();
6273 } else {
6274 frame()->CallKeyedStoreIC();
6275 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006276}
6277
6278
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006279#ifdef DEBUG
6280bool CodeGenerator::HasValidEntryRegisters() { return true; }
6281#endif
6282
6283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006284#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006285#define __ ACCESS_MASM(masm)
6286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006287
ager@chromium.org7c537e22008-10-16 08:43:32 +00006288Handle<String> Reference::GetName() {
6289 ASSERT(type_ == NAMED);
6290 Property* property = expression_->AsProperty();
6291 if (property == NULL) {
6292 // Global variable reference treated as a named property reference.
6293 VariableProxy* proxy = expression_->AsVariableProxy();
6294 ASSERT(proxy->AsVariable() != NULL);
6295 ASSERT(proxy->AsVariable()->is_global());
6296 return proxy->name();
6297 } else {
6298 Literal* raw_name = property->key()->AsLiteral();
6299 ASSERT(raw_name != NULL);
6300 return Handle<String>(String::cast(*raw_name->handle()));
6301 }
6302}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006303
ager@chromium.org7c537e22008-10-16 08:43:32 +00006304
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006305void Reference::DupIfPersist() {
6306 if (persist_after_get_) {
6307 switch (type_) {
6308 case KEYED:
6309 cgen_->frame()->Dup2();
6310 break;
6311 case NAMED:
6312 cgen_->frame()->Dup();
6313 // Fall through.
6314 case UNLOADED:
6315 case ILLEGAL:
6316 case SLOT:
6317 // Do nothing.
6318 ;
6319 }
6320 } else {
6321 set_unloaded();
6322 }
6323}
6324
6325
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006326void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006327 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006328 ASSERT(!is_illegal());
6329 ASSERT(!cgen_->has_cc());
6330 MacroAssembler* masm = cgen_->masm();
6331 Property* property = expression_->AsProperty();
6332 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006333 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006334 }
6335
6336 switch (type_) {
6337 case SLOT: {
6338 Comment cmnt(masm, "[ Load from Slot");
6339 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6340 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006341 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006342 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006343 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006344 }
6345
ager@chromium.org7c537e22008-10-16 08:43:32 +00006346 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006347 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006348 bool is_global = var != NULL;
6349 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006350 Handle<String> name = GetName();
6351 DupIfPersist();
6352 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006353 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006354 }
6355
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006356 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006357 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006358 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006359 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006360 cgen_->frame()->EmitPush(r0);
6361 break;
6362 }
6363
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006364 default:
6365 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006366 }
6367}
6368
6369
ager@chromium.org7c537e22008-10-16 08:43:32 +00006370void Reference::SetValue(InitState init_state) {
6371 ASSERT(!is_illegal());
6372 ASSERT(!cgen_->has_cc());
6373 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006374 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006375 Property* property = expression_->AsProperty();
6376 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006377 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006378 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006379
ager@chromium.org7c537e22008-10-16 08:43:32 +00006380 switch (type_) {
6381 case SLOT: {
6382 Comment cmnt(masm, "[ Store to Slot");
6383 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006384 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006385 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006386 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006387 }
6388
ager@chromium.org7c537e22008-10-16 08:43:32 +00006389 case NAMED: {
6390 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00006391 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006392 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00006393 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006394 break;
6395 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006396
ager@chromium.org7c537e22008-10-16 08:43:32 +00006397 case KEYED: {
6398 Comment cmnt(masm, "[ Store to keyed Property");
6399 Property* property = expression_->AsProperty();
6400 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006401 cgen_->CodeForSourcePosition(property->position());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006402 cgen_->EmitKeyedStore(property->key()->type());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006403 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006404 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006405 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006406 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00006407
6408 default:
6409 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006410 }
6411}
6412
6413
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006414void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006415 // Create a new closure from the given function info in new
6416 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006417 Label gc;
6418
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006419 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006420 __ pop(r3);
6421
6422 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006423 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006424 r0,
6425 r1,
6426 r2,
6427 &gc,
6428 TAG_OBJECT);
6429
6430 // Compute the function map in the current global context and set that
6431 // as the map of the allocated object.
6432 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6433 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
6434 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
6435 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
6436
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006437 // Initialize the rest of the function. We don't have to update the
6438 // write barrier because the allocated object is in new space.
6439 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
6440 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
6441 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
6442 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
6443 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
6444 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
6445 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
6446 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006447
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006448 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006449 __ Ret();
6450
6451 // Create a new closure through the slower runtime call.
6452 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006453 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006454 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006455}
6456
6457
6458void FastNewContextStub::Generate(MacroAssembler* masm) {
6459 // Try to allocate the context in new space.
6460 Label gc;
6461 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
6462
6463 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006464 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006465 r0,
6466 r1,
6467 r2,
6468 &gc,
6469 TAG_OBJECT);
6470
6471 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00006472 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006473
6474 // Setup the object header.
6475 __ LoadRoot(r2, Heap::kContextMapRootIndex);
6476 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006477 __ mov(r2, Operand(Smi::FromInt(length)));
6478 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006479
6480 // Setup the fixed slots.
6481 __ mov(r1, Operand(Smi::FromInt(0)));
6482 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
6483 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
6484 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
6485 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
6486
6487 // Copy the global object from the surrounding context.
6488 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6489 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
6490
6491 // Initialize the rest of the slots to undefined.
6492 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
6493 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
6494 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
6495 }
6496
6497 // Remove the on-stack argument and return.
6498 __ mov(cp, r0);
6499 __ pop();
6500 __ Ret();
6501
6502 // Need to collect. Call into runtime system.
6503 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006504 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006505}
6506
6507
ager@chromium.org5c838252010-02-19 08:53:10 +00006508void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
6509 // Stack layout on entry:
6510 //
6511 // [sp]: constant elements.
6512 // [sp + kPointerSize]: literal index.
6513 // [sp + (2 * kPointerSize)]: literals array.
6514
6515 // All sizes here are multiples of kPointerSize.
6516 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
6517 int size = JSArray::kSize + elements_size;
6518
6519 // Load boilerplate object into r3 and check if we need to create a
6520 // boilerplate.
6521 Label slow_case;
6522 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6523 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6524 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6525 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6526 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
6527 __ cmp(r3, ip);
6528 __ b(eq, &slow_case);
6529
6530 // Allocate both the JS array and the elements array in one big
6531 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006532 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00006533 r0,
6534 r1,
6535 r2,
6536 &slow_case,
6537 TAG_OBJECT);
6538
6539 // Copy the JS array part.
6540 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
6541 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
6542 __ ldr(r1, FieldMemOperand(r3, i));
6543 __ str(r1, FieldMemOperand(r0, i));
6544 }
6545 }
6546
6547 if (length_ > 0) {
6548 // Get hold of the elements array of the boilerplate and setup the
6549 // elements pointer in the resulting object.
6550 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
6551 __ add(r2, r0, Operand(JSArray::kSize));
6552 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
6553
6554 // Copy the elements array.
6555 for (int i = 0; i < elements_size; i += kPointerSize) {
6556 __ ldr(r1, FieldMemOperand(r3, i));
6557 __ str(r1, FieldMemOperand(r2, i));
6558 }
6559 }
6560
6561 // Return and remove the on-stack parameters.
6562 __ add(sp, sp, Operand(3 * kPointerSize));
6563 __ Ret();
6564
6565 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006566 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00006567}
6568
6569
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006570// Takes a Smi and converts to an IEEE 64 bit floating point value in two
6571// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
6572// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
6573// scratch register. Destroys the source register. No GC occurs during this
6574// stub so you don't have to set up the frame.
6575class ConvertToDoubleStub : public CodeStub {
6576 public:
6577 ConvertToDoubleStub(Register result_reg_1,
6578 Register result_reg_2,
6579 Register source_reg,
6580 Register scratch_reg)
6581 : result1_(result_reg_1),
6582 result2_(result_reg_2),
6583 source_(source_reg),
6584 zeros_(scratch_reg) { }
6585
6586 private:
6587 Register result1_;
6588 Register result2_;
6589 Register source_;
6590 Register zeros_;
6591
6592 // Minor key encoding in 16 bits.
6593 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
6594 class OpBits: public BitField<Token::Value, 2, 14> {};
6595
6596 Major MajorKey() { return ConvertToDouble; }
6597 int MinorKey() {
6598 // Encode the parameters in a unique 16 bit value.
6599 return result1_.code() +
6600 (result2_.code() << 4) +
6601 (source_.code() << 8) +
6602 (zeros_.code() << 12);
6603 }
6604
6605 void Generate(MacroAssembler* masm);
6606
6607 const char* GetName() { return "ConvertToDoubleStub"; }
6608
6609#ifdef DEBUG
6610 void Print() { PrintF("ConvertToDoubleStub\n"); }
6611#endif
6612};
6613
6614
6615void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
6616#ifndef BIG_ENDIAN_FLOATING_POINT
6617 Register exponent = result1_;
6618 Register mantissa = result2_;
6619#else
6620 Register exponent = result2_;
6621 Register mantissa = result1_;
6622#endif
6623 Label not_special;
6624 // Convert from Smi to integer.
6625 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
6626 // Move sign bit from source to destination. This works because the sign bit
6627 // in the exponent word of the double has the same position and polarity as
6628 // the 2's complement sign bit in a Smi.
6629 ASSERT(HeapNumber::kSignMask == 0x80000000u);
6630 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
6631 // Subtract from 0 if source was negative.
6632 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006633
6634 // We have -1, 0 or 1, which we treat specially. Register source_ contains
6635 // absolute value: it is either equal to 1 (special case of -1 and 1),
6636 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006637 __ cmp(source_, Operand(1));
6638 __ b(gt, &not_special);
6639
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006640 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
6641 static const uint32_t exponent_word_for_1 =
6642 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006643 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006644 // 1, 0 and -1 all have 0 for the second word.
6645 __ mov(mantissa, Operand(0));
6646 __ Ret();
6647
6648 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006649 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006650 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006651 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006652 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00006653 // We use mantissa as a scratch register here. Use a fudge factor to
6654 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
6655 // that fit in the ARM's constant field.
6656 int fudge = 0x400;
6657 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
6658 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006659 __ orr(exponent,
6660 exponent,
6661 Operand(mantissa, LSL, HeapNumber::kExponentShift));
6662 // Shift up the source chopping the top bit off.
6663 __ add(zeros_, zeros_, Operand(1));
6664 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
6665 __ mov(source_, Operand(source_, LSL, zeros_));
6666 // Compute lower part of fraction (last 12 bits).
6667 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
6668 // And the top (top 20 bits).
6669 __ orr(exponent,
6670 exponent,
6671 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
6672 __ Ret();
6673}
6674
6675
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006676// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006677void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006678 Label max_negative_int;
6679 // the_int_ has the answer which is a signed int32 but not a Smi.
6680 // We test for the special value that has a different exponent. This test
6681 // has the neat side effect of setting the flags according to the sign.
6682 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006683 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006684 __ b(eq, &max_negative_int);
6685 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
6686 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
6687 uint32_t non_smi_exponent =
6688 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6689 __ mov(scratch_, Operand(non_smi_exponent));
6690 // Set the sign bit in scratch_ if the value was negative.
6691 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
6692 // Subtract from 0 if the value was negative.
6693 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
6694 // We should be masking the implict first digit of the mantissa away here,
6695 // but it just ends up combining harmlessly with the last digit of the
6696 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
6697 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
6698 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
6699 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6700 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
6701 __ str(scratch_, FieldMemOperand(the_heap_number_,
6702 HeapNumber::kExponentOffset));
6703 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
6704 __ str(scratch_, FieldMemOperand(the_heap_number_,
6705 HeapNumber::kMantissaOffset));
6706 __ Ret();
6707
6708 __ bind(&max_negative_int);
6709 // The max negative int32 is stored as a positive number in the mantissa of
6710 // a double because it uses a sign bit instead of using two's complement.
6711 // The actual mantissa bits stored are all 0 because the implicit most
6712 // significant 1 bit is not stored.
6713 non_smi_exponent += 1 << HeapNumber::kExponentShift;
6714 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
6715 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
6716 __ mov(ip, Operand(0));
6717 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
6718 __ Ret();
6719}
6720
6721
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006722// Handle the case where the lhs and rhs are the same object.
6723// Equality is almost reflexive (everything but NaN), so this is a test
6724// for "identity and not NaN".
6725static void EmitIdenticalObjectComparison(MacroAssembler* masm,
6726 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006727 Condition cc,
6728 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006729 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006730 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006731 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006732 __ b(ne, &not_identical);
6733
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006734 // The two objects are identical. If we know that one of them isn't NaN then
6735 // we now know they test equal.
6736 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006737 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
6738 // so we do the second best thing - test it ourselves.
6739 // They are both equal and they are not both Smis so both of them are not
6740 // Smis. If it's not a heap number, then return equal.
6741 if (cc == lt || cc == gt) {
6742 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006743 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006744 } else {
6745 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6746 __ b(eq, &heap_number);
6747 // Comparing JS objects with <=, >= is complicated.
6748 if (cc != eq) {
6749 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
6750 __ b(ge, slow);
6751 // Normally here we fall through to return_equal, but undefined is
6752 // special: (undefined == undefined) == true, but
6753 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
6754 if (cc == le || cc == ge) {
6755 __ cmp(r4, Operand(ODDBALL_TYPE));
6756 __ b(ne, &return_equal);
6757 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006758 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006759 __ b(ne, &return_equal);
6760 if (cc == le) {
6761 // undefined <= undefined should fail.
6762 __ mov(r0, Operand(GREATER));
6763 } else {
6764 // undefined >= undefined should fail.
6765 __ mov(r0, Operand(LESS));
6766 }
6767 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006768 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006769 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006770 }
6771 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006772
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006773 __ bind(&return_equal);
6774 if (cc == lt) {
6775 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
6776 } else if (cc == gt) {
6777 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
6778 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006779 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006780 }
6781 __ mov(pc, Operand(lr)); // Return.
6782
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006783 if (cc != eq || !never_nan_nan) {
6784 // For less and greater we don't have to check for NaN since the result of
6785 // x < x is false regardless. For the others here is some code to check
6786 // for NaN.
6787 if (cc != lt && cc != gt) {
6788 __ bind(&heap_number);
6789 // It is a heap number, so return non-equal if it's NaN and equal if it's
6790 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006791
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006792 // The representation of NaN values has all exponent bits (52..62) set,
6793 // and not all mantissa bits (0..51) clear.
6794 // Read top bits of double representation (second word of value).
6795 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6796 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00006797 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
6798 // NaNs have all-one exponents so they sign extend to -1.
6799 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006800 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006801
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006802 // Shift out flag and all exponent bits, retaining only mantissa.
6803 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
6804 // Or with all low-bits of mantissa.
6805 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6806 __ orr(r0, r3, Operand(r2), SetCC);
6807 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006808 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
6809 // not (it's a NaN). For <= and >= we need to load r0 with the failing
6810 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006811 if (cc != eq) {
6812 // All-zero means Infinity means equal.
6813 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
6814 if (cc == le) {
6815 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
6816 } else {
6817 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
6818 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006819 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006820 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006821 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006822 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006823 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006824
6825 __ bind(&not_identical);
6826}
6827
6828
6829// See comment at call site.
6830static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006831 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006832 Label* slow,
6833 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006834 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006835 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006836 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006837
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006838 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006839 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6840 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006841 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006842 // succeed. Return non-equal (r0 is already not zero)
6843 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6844 } else {
6845 // Smi compared non-strictly with a non-Smi non-heap-number. Call
6846 // the runtime.
6847 __ b(ne, slow);
6848 }
6849
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006850 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006851 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006852 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006853 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006854 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6855 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006856 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006857 // Load the double from rhs, tagged HeapNumber r0, to d6.
6858 __ sub(r7, r0, Operand(kHeapObjectTag));
6859 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006860 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006861 __ push(lr);
6862 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006863 __ mov(r7, Operand(r1));
6864 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6865 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006866 // Load rhs to a double in r0, r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006867 __ Ldrd(r0, r1, FieldMemOperand(r0, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006868 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006869 }
6870
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006871 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006872 // since it's a smi.
6873 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006874
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006875 __ bind(&rhs_is_smi);
6876 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006877 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6878 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006879 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006880 // succeed. Return non-equal.
6881 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
6882 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
6883 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006884 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006885 // the runtime.
6886 __ b(ne, slow);
6887 }
6888
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006889 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006890 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006891 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006892 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006893 // Load the double from lhs, tagged HeapNumber r1, to d7.
6894 __ sub(r7, r1, Operand(kHeapObjectTag));
6895 __ vldr(d7, r7, HeapNumber::kValueOffset);
6896 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6897 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006898 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006899 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006900 __ push(lr);
6901 // Load lhs to a double in r2, r3.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006902 __ Ldrd(r2, r3, FieldMemOperand(r1, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006903 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006904 __ mov(r7, Operand(r0));
6905 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6906 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006907 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006908 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006909 // Fall through to both_loaded_as_doubles.
6910}
6911
6912
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006913void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006914 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006915 Register rhs_exponent = exp_first ? r0 : r1;
6916 Register lhs_exponent = exp_first ? r2 : r3;
6917 Register rhs_mantissa = exp_first ? r1 : r0;
6918 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006919 Label one_is_nan, neither_is_nan;
6920
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00006921 __ Sbfx(r4,
6922 lhs_exponent,
6923 HeapNumber::kExponentShift,
6924 HeapNumber::kExponentBits);
6925 // NaNs have all-one exponents so they sign extend to -1.
6926 __ cmp(r4, Operand(-1));
6927 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006928 __ mov(r4,
6929 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6930 SetCC);
6931 __ b(ne, &one_is_nan);
6932 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006933 __ b(ne, &one_is_nan);
6934
6935 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00006936 __ Sbfx(r4,
6937 rhs_exponent,
6938 HeapNumber::kExponentShift,
6939 HeapNumber::kExponentBits);
6940 // NaNs have all-one exponents so they sign extend to -1.
6941 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006942 __ b(ne, &neither_is_nan);
6943 __ mov(r4,
6944 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
6945 SetCC);
6946 __ b(ne, &one_is_nan);
6947 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006948 __ b(eq, &neither_is_nan);
6949
6950 __ bind(&one_is_nan);
6951 // NaN comparisons always fail.
6952 // Load whatever we need in r0 to make the comparison fail.
6953 if (cc == lt || cc == le) {
6954 __ mov(r0, Operand(GREATER));
6955 } else {
6956 __ mov(r0, Operand(LESS));
6957 }
6958 __ mov(pc, Operand(lr)); // Return.
6959
6960 __ bind(&neither_is_nan);
6961}
6962
6963
6964// See comment at call site.
6965static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
6966 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006967 Register rhs_exponent = exp_first ? r0 : r1;
6968 Register lhs_exponent = exp_first ? r2 : r3;
6969 Register rhs_mantissa = exp_first ? r1 : r0;
6970 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006971
6972 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
6973 if (cc == eq) {
6974 // Doubles are not equal unless they have the same bit pattern.
6975 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006976 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
6977 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006978 // Return non-zero if the numbers are unequal.
6979 __ mov(pc, Operand(lr), LeaveCC, ne);
6980
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006981 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006982 // If exponents are equal then return 0.
6983 __ mov(pc, Operand(lr), LeaveCC, eq);
6984
6985 // Exponents are unequal. The only way we can return that the numbers
6986 // are equal is if one is -0 and the other is 0. We already dealt
6987 // with the case where both are -0 or both are 0.
6988 // We start by seeing if the mantissas (that are equal) or the bottom
6989 // 31 bits of the rhs exponent are non-zero. If so we return not
6990 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006991 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006992 __ mov(r0, Operand(r4), LeaveCC, ne);
6993 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
6994 // Now they are equal if and only if the lhs exponent is zero in its
6995 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006996 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006997 __ mov(pc, Operand(lr));
6998 } else {
6999 // Call a native function to do a comparison between two non-NaNs.
7000 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007001 __ push(lr);
7002 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7003 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7004 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007005 }
7006}
7007
7008
7009// See comment at call site.
7010static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
7011 // If either operand is a JSObject or an oddball value, then they are
7012 // not equal since their pointers are different.
7013 // There is no test for undetectability in strict equality.
7014 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
7015 Label first_non_object;
7016 // Get the type of the first operand into r2 and compare it with
7017 // FIRST_JS_OBJECT_TYPE.
7018 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
7019 __ b(lt, &first_non_object);
7020
7021 // Return non-zero (r0 is not zero)
7022 Label return_not_equal;
7023 __ bind(&return_not_equal);
7024 __ mov(pc, Operand(lr)); // Return.
7025
7026 __ bind(&first_non_object);
7027 // Check for oddballs: true, false, null, undefined.
7028 __ cmp(r2, Operand(ODDBALL_TYPE));
7029 __ b(eq, &return_not_equal);
7030
7031 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
7032 __ b(ge, &return_not_equal);
7033
7034 // Check for oddballs: true, false, null, undefined.
7035 __ cmp(r3, Operand(ODDBALL_TYPE));
7036 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007037
7038 // Now that we have the types we might as well check for symbol-symbol.
7039 // Ensure that no non-strings have the symbol bit set.
7040 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
7041 ASSERT(kSymbolTag != 0);
7042 __ and_(r2, r2, Operand(r3));
7043 __ tst(r2, Operand(kIsSymbolMask));
7044 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007045}
7046
7047
7048// See comment at call site.
7049static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
7050 Label* both_loaded_as_doubles,
7051 Label* not_heap_numbers,
7052 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007053 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007054 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007055 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
7056 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007057 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7058
7059 // Both are heap numbers. Load them up then jump to the code we have
7060 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007061 if (CpuFeatures::IsSupported(VFP3)) {
7062 CpuFeatures::Scope scope(VFP3);
7063 __ sub(r7, r0, Operand(kHeapObjectTag));
7064 __ vldr(d6, r7, HeapNumber::kValueOffset);
7065 __ sub(r7, r1, Operand(kHeapObjectTag));
7066 __ vldr(d7, r7, HeapNumber::kValueOffset);
7067 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00007068 __ Ldrd(r2, r3, FieldMemOperand(r1, HeapNumber::kValueOffset));
7069 __ Ldrd(r0, r1, FieldMemOperand(r0, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007070 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007071 __ jmp(both_loaded_as_doubles);
7072}
7073
7074
7075// Fast negative check for symbol-to-symbol equality.
7076static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
7077 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007078 // Ensure that no non-strings have the symbol bit set.
7079 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
7080 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007081 __ tst(r2, Operand(kIsSymbolMask));
7082 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007083 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
7084 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007085 __ tst(r3, Operand(kIsSymbolMask));
7086 __ b(eq, slow);
7087
7088 // Both are symbols. We already checked they weren't the same pointer
7089 // so they are not equal.
7090 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
7091 __ mov(pc, Operand(lr)); // Return.
7092}
7093
7094
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007095void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7096 Register object,
7097 Register result,
7098 Register scratch1,
7099 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007100 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007101 bool object_is_smi,
7102 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007103 // Use of registers. Register result is used as a temporary.
7104 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007105 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007106
7107 // Load the number string cache.
7108 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7109
7110 // Make the hash mask from the length of the number string cache. It
7111 // contains two elements (number and string) for each cache entry.
7112 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007113 // Divide length by two (length is a smi).
7114 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007115 __ sub(mask, mask, Operand(1)); // Make mask.
7116
7117 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007118 // number string cache for smis is just the smi value, and the hash for
7119 // doubles is the xor of the upper and lower words. See
7120 // Heap::GetNumberStringCache.
7121 Label is_smi;
7122 Label load_result_from_cache;
7123 if (!object_is_smi) {
7124 __ BranchOnSmi(object, &is_smi);
7125 if (CpuFeatures::IsSupported(VFP3)) {
7126 CpuFeatures::Scope scope(VFP3);
7127 __ CheckMap(object,
7128 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007129 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007130 not_found,
7131 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007132
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007133 ASSERT_EQ(8, kDoubleSize);
7134 __ add(scratch1,
7135 object,
7136 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7137 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7138 __ eor(scratch1, scratch1, Operand(scratch2));
7139 __ and_(scratch1, scratch1, Operand(mask));
7140
7141 // Calculate address of entry in string cache: each entry consists
7142 // of two pointer sized fields.
7143 __ add(scratch1,
7144 number_string_cache,
7145 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7146
7147 Register probe = mask;
7148 __ ldr(probe,
7149 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7150 __ BranchOnSmi(probe, not_found);
7151 __ sub(scratch2, object, Operand(kHeapObjectTag));
7152 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7153 __ sub(probe, probe, Operand(kHeapObjectTag));
7154 __ vldr(d1, probe, HeapNumber::kValueOffset);
7155 __ vcmp(d0, d1);
7156 __ vmrs(pc);
7157 __ b(ne, not_found); // The cache did not contain this value.
7158 __ b(&load_result_from_cache);
7159 } else {
7160 __ b(not_found);
7161 }
7162 }
7163
7164 __ bind(&is_smi);
7165 Register scratch = scratch1;
7166 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007167 // Calculate address of entry in string cache: each entry consists
7168 // of two pointer sized fields.
7169 __ add(scratch,
7170 number_string_cache,
7171 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7172
7173 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007174 Register probe = mask;
7175 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7176 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007177 __ b(ne, not_found);
7178
7179 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007180 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007181 __ ldr(result,
7182 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007183 __ IncrementCounter(&Counters::number_to_string_native,
7184 1,
7185 scratch1,
7186 scratch2);
7187}
7188
7189
7190void NumberToStringStub::Generate(MacroAssembler* masm) {
7191 Label runtime;
7192
7193 __ ldr(r1, MemOperand(sp, 0));
7194
7195 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007196 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007197 __ add(sp, sp, Operand(1 * kPointerSize));
7198 __ Ret();
7199
7200 __ bind(&runtime);
7201 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007202 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007203}
7204
7205
ager@chromium.orgac091b72010-05-05 07:34:42 +00007206void RecordWriteStub::Generate(MacroAssembler* masm) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007207 __ RecordWriteHelper(object_, Operand(offset_), offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007208 __ Ret();
7209}
7210
7211
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007212// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
7213// On exit r0 is 0, positive or negative to indicate the result of
7214// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007215void CompareStub::Generate(MacroAssembler* masm) {
7216 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007217 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007218
7219 // NOTICE! This code is only reached after a smi-fast-case check, so
7220 // it is certain that at least one operand isn't a smi.
7221
7222 // Handle the case where the objects are identical. Either returns the answer
7223 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007224 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007225
7226 // If either is a Smi (we know that not both are), then they can only
7227 // be strictly equal if the other is a HeapNumber.
7228 ASSERT_EQ(0, kSmiTag);
7229 ASSERT_EQ(0, Smi::FromInt(0));
7230 __ and_(r2, r0, Operand(r1));
7231 __ tst(r2, Operand(kSmiTagMask));
7232 __ b(ne, &not_smis);
7233 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7234 // 1) Return the answer.
7235 // 2) Go to slow.
7236 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007237 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007238 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007239 // comparison. If VFP3 is supported the double values of the numbers have
7240 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7241 // into r0, r1, r2, and r3.
7242 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007243
7244 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007245 // The arguments have been converted to doubles and stored in d6 and d7, if
7246 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007247 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007248 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007249 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007250 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007251 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007252 __ vcmp(d7, d6);
7253 __ vmrs(pc); // Move vector status bits to normal status bits.
7254 Label nan;
7255 __ b(vs, &nan);
7256 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7257 __ mov(r0, Operand(LESS), LeaveCC, lt);
7258 __ mov(r0, Operand(GREATER), LeaveCC, gt);
7259 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007260
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007261 __ bind(&nan);
7262 // If one of the sides was a NaN then the v flag is set. Load r0 with
7263 // whatever it takes to make the comparison fail, since comparisons with NaN
7264 // always fail.
7265 if (cc_ == lt || cc_ == le) {
7266 __ mov(r0, Operand(GREATER));
7267 } else {
7268 __ mov(r0, Operand(LESS));
7269 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007270 __ mov(pc, Operand(lr));
7271 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007272 // Checks for NaN in the doubles we have loaded. Can return the answer or
7273 // fall through if neither is a NaN. Also binds lhs_not_nan.
7274 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007275 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7276 // answer. Never falls through.
7277 EmitTwoNonNanDoubleComparison(masm, cc_);
7278 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007279
7280 __ bind(&not_smis);
7281 // At this point we know we are dealing with two different objects,
7282 // and neither of them is a Smi. The objects are in r0 and r1.
7283 if (strict_) {
7284 // This returns non-equal for some object types, or falls through if it
7285 // was not lucky.
7286 EmitStrictTwoHeapObjectCompare(masm);
7287 }
7288
7289 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007290 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007291 // Check for heap-number-heap-number comparison. Can jump to slow case,
7292 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7293 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007294 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007295 EmitCheckForTwoHeapNumbers(masm,
7296 &both_loaded_as_doubles,
7297 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007298 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007299
7300 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007301 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7302 // symbols.
7303 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007304 // Either jumps to slow or returns the answer. Assumes that r2 is the type
7305 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007306 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007307 }
7308
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007309 // Check for both being sequential ASCII strings, and inline if that is the
7310 // case.
7311 __ bind(&flat_string_check);
7312
7313 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
7314
7315 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7316 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
7317 r1,
7318 r0,
7319 r2,
7320 r3,
7321 r4,
7322 r5);
7323 // Never falls through to here.
7324
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007325 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007326
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007327 __ Push(r1, r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007328 // Figure out which native to call and setup the arguments.
7329 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007330 if (cc_ == eq) {
7331 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7332 } else {
7333 native = Builtins::COMPARE;
7334 int ncr; // NaN compare result
7335 if (cc_ == lt || cc_ == le) {
7336 ncr = GREATER;
7337 } else {
7338 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7339 ncr = LESS;
7340 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007341 __ mov(r0, Operand(Smi::FromInt(ncr)));
7342 __ push(r0);
7343 }
7344
7345 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7346 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007347 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007348}
7349
7350
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00007351// We fall into this code if the operands were Smis, but the result was
7352// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007353// the operands were not both Smi. The operands are in r0 and r1. In order
7354// to call the C-implemented binary fp operation routines we need to end up
7355// with the double precision floating point operands in r0 and r1 (for the
7356// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00007357void GenericBinaryOpStub::HandleBinaryOpSlowCases(
7358 MacroAssembler* masm,
7359 Label* not_smi,
7360 Register lhs,
7361 Register rhs,
7362 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007363 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00007364 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007365
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007366 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007367 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00007368
7369 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007370 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7371
ager@chromium.org357bf652010-04-12 11:30:10 +00007372 // Smi-smi case (overflow).
7373 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007374 // The new heap number is in r5. r3 and r7 are scratch.
7375 __ AllocateHeapNumber(
7376 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007377
7378 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
7379 // using registers d7 and d6 for the double values.
7380 if (use_fp_registers) {
7381 CpuFeatures::Scope scope(VFP3);
7382 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
7383 __ vmov(s15, r7);
7384 __ vcvt_f64_s32(d7, s15);
7385 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
7386 __ vmov(s13, r7);
7387 __ vcvt_f64_s32(d6, s13);
7388 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007389 // Write Smi from rhs to r3 and r2 in double format. r3 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007390 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007391 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007392 __ push(lr);
7393 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007394 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00007395 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007396 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007397 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
7398 __ pop(lr);
7399 }
7400 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007401 }
7402
ager@chromium.org357bf652010-04-12 11:30:10 +00007403 // We branch here if at least one of r0 and r1 is not a Smi.
7404 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007405 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007406
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007407 // After this point we have the left hand side in r1 and the right hand side
7408 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00007409 if (lhs.is(r0)) {
7410 __ Swap(r0, r1, ip);
7411 }
7412
7413 if (ShouldGenerateFPCode()) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007414 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
7415
ager@chromium.org357bf652010-04-12 11:30:10 +00007416 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
7417 switch (op_) {
7418 case Token::ADD:
7419 case Token::SUB:
7420 case Token::MUL:
7421 case Token::DIV:
7422 GenerateTypeTransition(masm);
7423 break;
7424
7425 default:
7426 break;
7427 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007428 // Restore heap number map register.
7429 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00007430 }
7431
7432 if (mode_ == NO_OVERWRITE) {
7433 // In the case where there is no chance of an overwritable float we may as
7434 // well do the allocation immediately while r0 and r1 are untouched.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007435 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007436 }
7437
7438 // Move r0 to a double in r2-r3.
7439 __ tst(r0, Operand(kSmiTagMask));
7440 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007441 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7442 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7443 __ cmp(r4, heap_number_map);
ager@chromium.org357bf652010-04-12 11:30:10 +00007444 __ b(ne, &slow);
7445 if (mode_ == OVERWRITE_RIGHT) {
7446 __ mov(r5, Operand(r0)); // Overwrite this heap number.
7447 }
7448 if (use_fp_registers) {
7449 CpuFeatures::Scope scope(VFP3);
7450 // Load the double from tagged HeapNumber r0 to d7.
7451 __ sub(r7, r0, Operand(kHeapObjectTag));
7452 __ vldr(d7, r7, HeapNumber::kValueOffset);
7453 } else {
7454 // Calling convention says that second double is in r2 and r3.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00007455 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007456 }
7457 __ jmp(&finished_loading_r0);
7458 __ bind(&r0_is_smi);
7459 if (mode_ == OVERWRITE_RIGHT) {
7460 // We can't overwrite a Smi so get address of new heap number into r5.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007461 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007462 }
7463
7464 if (use_fp_registers) {
7465 CpuFeatures::Scope scope(VFP3);
7466 // Convert smi in r0 to double in d7.
7467 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
7468 __ vmov(s15, r7);
7469 __ vcvt_f64_s32(d7, s15);
7470 } else {
7471 // Write Smi from r0 to r3 and r2 in double format.
7472 __ mov(r7, Operand(r0));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007473 ConvertToDoubleStub stub3(r3, r2, r7, r4);
ager@chromium.org357bf652010-04-12 11:30:10 +00007474 __ push(lr);
7475 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
7476 __ pop(lr);
7477 }
7478
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007479 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
7480 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
7481 Label r1_is_not_smi;
7482 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
7483 __ tst(r1, Operand(kSmiTagMask));
7484 __ b(ne, &r1_is_not_smi);
7485 GenerateTypeTransition(masm);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007486 // Restore heap number map register.
7487 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007488 __ jmp(&r1_is_smi);
7489 }
7490
ager@chromium.org357bf652010-04-12 11:30:10 +00007491 __ bind(&finished_loading_r0);
7492
7493 // Move r1 to a double in r0-r1.
7494 __ tst(r1, Operand(kSmiTagMask));
7495 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007496 __ bind(&r1_is_not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007497 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
7498 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7499 __ cmp(r4, heap_number_map);
ager@chromium.org357bf652010-04-12 11:30:10 +00007500 __ b(ne, &slow);
7501 if (mode_ == OVERWRITE_LEFT) {
7502 __ mov(r5, Operand(r1)); // Overwrite this heap number.
7503 }
7504 if (use_fp_registers) {
7505 CpuFeatures::Scope scope(VFP3);
7506 // Load the double from tagged HeapNumber r1 to d6.
7507 __ sub(r7, r1, Operand(kHeapObjectTag));
7508 __ vldr(d6, r7, HeapNumber::kValueOffset);
7509 } else {
7510 // Calling convention says that first double is in r0 and r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00007511 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007512 }
7513 __ jmp(&finished_loading_r1);
7514 __ bind(&r1_is_smi);
7515 if (mode_ == OVERWRITE_LEFT) {
7516 // We can't overwrite a Smi so get address of new heap number into r5.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007517 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007518 }
7519
7520 if (use_fp_registers) {
7521 CpuFeatures::Scope scope(VFP3);
7522 // Convert smi in r1 to double in d6.
7523 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
7524 __ vmov(s13, r7);
7525 __ vcvt_f64_s32(d6, s13);
7526 } else {
7527 // Write Smi from r1 to r1 and r0 in double format.
7528 __ mov(r7, Operand(r1));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007529 ConvertToDoubleStub stub4(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00007530 __ push(lr);
7531 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
7532 __ pop(lr);
7533 }
7534
7535 __ bind(&finished_loading_r1);
7536
7537 __ bind(&do_the_call);
7538 // If we are inlining the operation using VFP3 instructions for
7539 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
7540 if (use_fp_registers) {
7541 CpuFeatures::Scope scope(VFP3);
7542 // ARMv7 VFP3 instructions to implement
7543 // double precision, add, subtract, multiply, divide.
7544
7545 if (Token::MUL == op_) {
7546 __ vmul(d5, d6, d7);
7547 } else if (Token::DIV == op_) {
7548 __ vdiv(d5, d6, d7);
7549 } else if (Token::ADD == op_) {
7550 __ vadd(d5, d6, d7);
7551 } else if (Token::SUB == op_) {
7552 __ vsub(d5, d6, d7);
7553 } else {
7554 UNREACHABLE();
7555 }
7556 __ sub(r0, r5, Operand(kHeapObjectTag));
7557 __ vstr(d5, r0, HeapNumber::kValueOffset);
7558 __ add(r0, r0, Operand(kHeapObjectTag));
7559 __ mov(pc, lr);
7560 } else {
7561 // If we did not inline the operation, then the arguments are in:
7562 // r0: Left value (least significant part of mantissa).
7563 // r1: Left value (sign, exponent, top of mantissa).
7564 // r2: Right value (least significant part of mantissa).
7565 // r3: Right value (sign, exponent, top of mantissa).
7566 // r5: Address of heap number for result.
7567
7568 __ push(lr); // For later.
7569 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
7570 // Call C routine that may not cause GC or other trouble. r5 is callee
7571 // save.
7572 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
7573 // Store answer in the overwritable heap number.
7574 #if !defined(USE_ARM_EABI)
7575 // Double returned in fp coprocessor register 0 and 1, encoded as register
7576 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
7577 // substract the tag from r5.
7578 __ sub(r4, r5, Operand(kHeapObjectTag));
7579 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
7580 #else
7581 // Double returned in registers 0 and 1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00007582 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00007583 #endif
7584 __ mov(r0, Operand(r5));
7585 // And we are done.
7586 __ pop(pc);
7587 }
7588 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007589
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007590 if (lhs.is(r0)) {
7591 __ b(&slow);
7592 __ bind(&slow_reverse);
7593 __ Swap(r0, r1, ip);
7594 }
7595
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007596 heap_number_map = no_reg; // Don't use this any more from here on.
7597
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007598 // We jump to here if something goes wrong (one param is not a number of any
7599 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007600 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007601
7602 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007603 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007604
ager@chromium.org357bf652010-04-12 11:30:10 +00007605 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007606 // Test for string arguments before calling runtime.
7607 // r1 : first argument
7608 // r0 : second argument
7609 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00007610 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007611
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007612 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007613 __ tst(r1, Operand(kSmiTagMask));
7614 __ b(eq, &not_string1);
7615 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
7616 __ b(ge, &not_string1);
7617
7618 // First argument is a a string, test second.
7619 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007620 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007621 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
7622 __ b(ge, &string1);
7623
7624 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007625 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
7626 __ TailCallStub(&string_add_stub);
7627
7628 __ bind(&string1_smi2);
7629 // First argument is a string, second is a smi. Try to lookup the number
7630 // string for the smi in the number string cache.
7631 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007632 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007633
7634 // Replace second argument on stack and tailcall string add stub to make
7635 // the result.
7636 __ str(r2, MemOperand(sp, 0));
7637 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007638
7639 // Only first argument is a string.
7640 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007641 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
7642
7643 // First argument was not a string, test second.
7644 __ bind(&not_string1);
7645 __ tst(r0, Operand(kSmiTagMask));
7646 __ b(eq, &not_strings);
7647 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
7648 __ b(ge, &not_strings);
7649
7650 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007651 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
7652
7653 __ bind(&not_strings);
7654 }
7655
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007656 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007657}
7658
7659
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007660// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007661// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007662// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
7663// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007664// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
7665// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007666static void GetInt32(MacroAssembler* masm,
7667 Register source,
7668 Register dest,
7669 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007670 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007671 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007672 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007673 // Get exponent word.
7674 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
7675 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007676 __ Ubfx(scratch2,
7677 scratch,
7678 HeapNumber::kExponentShift,
7679 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007680 // Load dest with zero. We use this either for the final shift or
7681 // for the answer.
7682 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007683 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007684 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
7685 // the exponent that we are fastest at and also the highest exponent we can
7686 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007687 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
7688 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
7689 // split it up to avoid a constant pool entry. You can't do that in general
7690 // for cmp because of the overflow flag, but we know the exponent is in the
7691 // range 0-2047 so there is no overflow.
7692 int fudge_factor = 0x400;
7693 __ sub(scratch2, scratch2, Operand(fudge_factor));
7694 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007695 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
7696 __ b(eq, &right_exponent);
7697 // If the exponent is higher than that then go to slow case. This catches
7698 // numbers that don't fit in a signed int32, infinities and NaNs.
7699 __ b(gt, slow);
7700
7701 // We know the exponent is smaller than 30 (biased). If it is less than
7702 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
7703 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007704 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
7705 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007706 // Dest already has a Smi zero.
7707 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007708 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007709 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
7710 // get how much to shift down.
7711 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007712 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007713 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007714 if (CpuFeatures::IsSupported(VFP3)) {
7715 CpuFeatures::Scope scope(VFP3);
7716 // ARMv7 VFP3 instructions implementing double precision to integer
7717 // conversion using round to zero.
7718 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007719 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007720 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007721 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007722 } else {
7723 // Get the top bits of the mantissa.
7724 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
7725 // Put back the implicit 1.
7726 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
7727 // Shift up the mantissa bits to take up the space the exponent used to
7728 // take. We just orred in the implicit bit so that took care of one and
7729 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
7730 // distance.
7731 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7732 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
7733 // Put sign in zero flag.
7734 __ tst(scratch, Operand(HeapNumber::kSignMask));
7735 // Get the second half of the double. For some exponents we don't
7736 // actually need this because the bits get shifted out again, but
7737 // it's probably slower to test than just to do it.
7738 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
7739 // Shift down 22 bits to get the last 10 bits.
7740 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
7741 // Move down according to the exponent.
7742 __ mov(dest, Operand(scratch, LSR, dest));
7743 // Fix sign if sign bit was set.
7744 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
7745 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00007746 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007747}
7748
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007749// For bitwise ops where the inputs are not both Smis we here try to determine
7750// whether both inputs are either Smis or at least heap numbers that can be
7751// represented by a 32 bit signed value. We truncate towards zero as required
7752// by the ES spec. If this is the case we do the bitwise op and see if the
7753// result is a Smi. If so, great, otherwise we try to find a heap number to
7754// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00007755// On entry the operands are in lhs and rhs. On exit the answer is in r0.
7756void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
7757 Register lhs,
7758 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007759 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00007760 Label rhs_is_smi, lhs_is_smi;
7761 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007762
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007763 Register heap_number_map = r6;
7764 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
7765
ager@chromium.org357bf652010-04-12 11:30:10 +00007766 __ tst(lhs, Operand(kSmiTagMask));
7767 __ 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 +00007768 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
7769 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007770 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007771 GetInt32(masm, lhs, r3, r5, r4, &slow);
7772 __ jmp(&done_checking_lhs);
7773 __ bind(&lhs_is_smi);
7774 __ mov(r3, Operand(lhs, ASR, 1));
7775 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007776
ager@chromium.org357bf652010-04-12 11:30:10 +00007777 __ tst(rhs, Operand(kSmiTagMask));
7778 __ 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 +00007779 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
7780 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007781 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00007782 GetInt32(masm, rhs, r2, r5, r4, &slow);
7783 __ jmp(&done_checking_rhs);
7784 __ bind(&rhs_is_smi);
7785 __ mov(r2, Operand(rhs, ASR, 1));
7786 __ bind(&done_checking_rhs);
7787
7788 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007789
7790 // r0 and r1: Original operands (Smi or heap numbers).
7791 // r2 and r3: Signed int32 operands.
7792 switch (op_) {
7793 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
7794 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
7795 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
7796 case Token::SAR:
7797 // Use only the 5 least significant bits of the shift count.
7798 __ and_(r2, r2, Operand(0x1f));
7799 __ mov(r2, Operand(r3, ASR, r2));
7800 break;
7801 case Token::SHR:
7802 // Use only the 5 least significant bits of the shift count.
7803 __ and_(r2, r2, Operand(0x1f));
7804 __ mov(r2, Operand(r3, LSR, r2), SetCC);
7805 // SHR is special because it is required to produce a positive answer.
7806 // The code below for writing into heap numbers isn't capable of writing
7807 // the register as an unsigned int so we go to slow case if we hit this
7808 // case.
7809 __ b(mi, &slow);
7810 break;
7811 case Token::SHL:
7812 // Use only the 5 least significant bits of the shift count.
7813 __ and_(r2, r2, Operand(0x1f));
7814 __ mov(r2, Operand(r3, LSL, r2));
7815 break;
7816 default: UNREACHABLE();
7817 }
7818 // check that the *signed* result fits in a smi
7819 __ add(r3, r2, Operand(0x40000000), SetCC);
7820 __ b(mi, &result_not_a_smi);
7821 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
7822 __ Ret();
7823
7824 Label have_to_allocate, got_a_heap_number;
7825 __ bind(&result_not_a_smi);
7826 switch (mode_) {
7827 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007828 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007829 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007830 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007831 break;
7832 }
7833 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00007834 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007835 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00007836 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007837 break;
7838 }
7839 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007840 // Get a new heap number in r5. r4 and r7 are scratch.
7841 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007842 }
7843 default: break;
7844 }
7845 __ bind(&got_a_heap_number);
7846 // r2: Answer as signed int32.
7847 // r5: Heap number to write answer into.
7848
7849 // Nothing can go wrong now, so move the heap number to r0, which is the
7850 // result.
7851 __ mov(r0, Operand(r5));
7852
7853 // Tail call that writes the int32 in r2 to the heap number in r0, using
7854 // r3 as scratch. r0 is preserved and returned.
7855 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
7856 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
7857
7858 if (mode_ != NO_OVERWRITE) {
7859 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007860 // Get a new heap number in r5. r4 and r7 are scratch.
7861 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007862 __ jmp(&got_a_heap_number);
7863 }
7864
7865 // If all else failed then we go to the runtime system.
7866 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007867 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007868 switch (op_) {
7869 case Token::BIT_OR:
7870 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
7871 break;
7872 case Token::BIT_AND:
7873 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
7874 break;
7875 case Token::BIT_XOR:
7876 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
7877 break;
7878 case Token::SAR:
7879 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
7880 break;
7881 case Token::SHR:
7882 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
7883 break;
7884 case Token::SHL:
7885 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
7886 break;
7887 default:
7888 UNREACHABLE();
7889 }
7890}
7891
7892
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007893// Can we multiply by x with max two shifts and an add.
7894// This answers yes to all integers from 2 to 10.
7895static bool IsEasyToMultiplyBy(int x) {
7896 if (x < 2) return false; // Avoid special cases.
7897 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
7898 if (IsPowerOf2(x)) return true; // Simple shift.
7899 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
7900 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
7901 return false;
7902}
7903
7904
7905// Can multiply by anything that IsEasyToMultiplyBy returns true for.
7906// Source and destination may be the same register. This routine does
7907// not set carry and overflow the way a mul instruction would.
7908static void MultiplyByKnownInt(MacroAssembler* masm,
7909 Register source,
7910 Register destination,
7911 int known_int) {
7912 if (IsPowerOf2(known_int)) {
7913 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
7914 } else if (PopCountLessThanEqual2(known_int)) {
7915 int first_bit = BitPosition(known_int);
7916 int second_bit = BitPosition(known_int ^ (1 << first_bit));
7917 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
7918 if (first_bit != 0) {
7919 __ mov(destination, Operand(destination, LSL, first_bit));
7920 }
7921 } else {
7922 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
7923 int the_bit = BitPosition(known_int + 1);
7924 __ rsb(destination, source, Operand(source, LSL, the_bit));
7925 }
7926}
7927
7928
7929// This function (as opposed to MultiplyByKnownInt) takes the known int in a
7930// a register for the cases where it doesn't know a good trick, and may deliver
7931// a result that needs shifting.
7932static void MultiplyByKnownInt2(
7933 MacroAssembler* masm,
7934 Register result,
7935 Register source,
7936 Register known_int_register, // Smi tagged.
7937 int known_int,
7938 int* required_shift) { // Including Smi tag shift
7939 switch (known_int) {
7940 case 3:
7941 __ add(result, source, Operand(source, LSL, 1));
7942 *required_shift = 1;
7943 break;
7944 case 5:
7945 __ add(result, source, Operand(source, LSL, 2));
7946 *required_shift = 1;
7947 break;
7948 case 6:
7949 __ add(result, source, Operand(source, LSL, 1));
7950 *required_shift = 2;
7951 break;
7952 case 7:
7953 __ rsb(result, source, Operand(source, LSL, 3));
7954 *required_shift = 1;
7955 break;
7956 case 9:
7957 __ add(result, source, Operand(source, LSL, 3));
7958 *required_shift = 1;
7959 break;
7960 case 10:
7961 __ add(result, source, Operand(source, LSL, 2));
7962 *required_shift = 2;
7963 break;
7964 default:
7965 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
7966 __ mul(result, source, known_int_register);
7967 *required_shift = 0;
7968 }
7969}
7970
7971
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007972const char* GenericBinaryOpStub::GetName() {
7973 if (name_ != NULL) return name_;
7974 const int len = 100;
7975 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
7976 if (name_ == NULL) return "OOM";
7977 const char* op_name = Token::Name(op_);
7978 const char* overwrite_name;
7979 switch (mode_) {
7980 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
7981 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
7982 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
7983 default: overwrite_name = "UnknownOverwrite"; break;
7984 }
7985
7986 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007987 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007988 op_name,
7989 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00007990 specialized_on_rhs_ ? "_ConstantRhs" : "",
7991 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00007992 return name_;
7993}
7994
7995
ager@chromium.org5c838252010-02-19 08:53:10 +00007996
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007997void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00007998 // lhs_ : x
7999 // rhs_ : y
8000 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008001
ager@chromium.org357bf652010-04-12 11:30:10 +00008002 Register result = r0;
8003 Register lhs = lhs_;
8004 Register rhs = rhs_;
8005
8006 // This code can't cope with other register allocations yet.
8007 ASSERT(result.is(r0) &&
8008 ((lhs.is(r0) && rhs.is(r1)) ||
8009 (lhs.is(r1) && rhs.is(r0))));
8010
8011 Register smi_test_reg = VirtualFrame::scratch0();
8012 Register scratch = VirtualFrame::scratch1();
8013
8014 // All ops need to know whether we are dealing with two Smis. Set up
8015 // smi_test_reg to tell us that.
8016 if (ShouldGenerateSmiCode()) {
8017 __ orr(smi_test_reg, lhs, Operand(rhs));
8018 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008019
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008020 switch (op_) {
8021 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008022 Label not_smi;
8023 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008024 if (ShouldGenerateSmiCode()) {
8025 ASSERT(kSmiTag == 0); // Adjust code below.
8026 __ tst(smi_test_reg, Operand(kSmiTagMask));
8027 __ b(ne, &not_smi);
8028 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8029 // Return if no overflow.
8030 __ Ret(vc);
8031 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8032 }
8033 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008034 break;
8035 }
8036
8037 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008038 Label not_smi;
8039 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008040 if (ShouldGenerateSmiCode()) {
8041 ASSERT(kSmiTag == 0); // Adjust code below.
8042 __ tst(smi_test_reg, Operand(kSmiTagMask));
8043 __ b(ne, &not_smi);
8044 if (lhs.is(r1)) {
8045 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8046 // Return if no overflow.
8047 __ Ret(vc);
8048 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8049 } else {
8050 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8051 // Return if no overflow.
8052 __ Ret(vc);
8053 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8054 }
8055 }
8056 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008057 break;
8058 }
8059
8060 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008061 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008062 if (ShouldGenerateSmiCode()) {
8063 ASSERT(kSmiTag == 0); // adjust code below
8064 __ tst(smi_test_reg, Operand(kSmiTagMask));
8065 Register scratch2 = smi_test_reg;
8066 smi_test_reg = no_reg;
8067 __ b(ne, &not_smi);
8068 // Remove tag from one operand (but keep sign), so that result is Smi.
8069 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8070 // Do multiplication
8071 // scratch = lower 32 bits of ip * lhs.
8072 __ smull(scratch, scratch2, lhs, ip);
8073 // Go slow on overflows (overflow bit is not set).
8074 __ mov(ip, Operand(scratch, ASR, 31));
8075 // No overflow if higher 33 bits are identical.
8076 __ cmp(ip, Operand(scratch2));
8077 __ b(ne, &slow);
8078 // Go slow on zero result to handle -0.
8079 __ tst(scratch, Operand(scratch));
8080 __ mov(result, Operand(scratch), LeaveCC, ne);
8081 __ Ret(ne);
8082 // We need -0 if we were multiplying a negative number with 0 to get 0.
8083 // We know one of them was zero.
8084 __ add(scratch2, rhs, Operand(lhs), SetCC);
8085 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
8086 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
8087 // Slow case. We fall through here if we multiplied a negative number
8088 // with 0, because that would mean we should produce -0.
8089 __ bind(&slow);
8090 }
8091 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008092 break;
8093 }
8094
8095 case Token::DIV:
8096 case Token::MOD: {
8097 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008098 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008099 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00008100 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008101 if (IsPowerOf2(constant_rhs_)) {
8102 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008103 __ and_(rhs,
8104 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008105 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
8106 SetCC);
8107 // We now have the answer, but if the input was negative we also
8108 // have the sign bit. Our work is done if the result is
8109 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00008110 if (!rhs.is(r0)) {
8111 __ mov(r0, rhs, LeaveCC, pl);
8112 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008113 __ Ret(pl);
8114 // A mod of a negative left hand side must return a negative number.
8115 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00008116 // already optimistically trashed rhs so we may need to restore it.
8117 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008118 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008119 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008120 __ b(eq, &smi_is_unsuitable);
8121 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00008122 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008123 } else {
8124 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00008125 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008126 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
8127 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
8128 int shift = 0;
8129 int d = constant_rhs_;
8130 while ((d & 1) == 0) {
8131 d >>= 1;
8132 shift++;
8133 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008134 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008135 __ bic(r0, r0, Operand(kSmiTagMask));
8136 }
8137 } else {
8138 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00008139 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008140 __ b(ne, &smi_is_unsuitable);
8141 // Find a fixed point reciprocal of the divisor so we can divide by
8142 // multiplying.
8143 double divisor = 1.0 / constant_rhs_;
8144 int shift = 32;
8145 double scale = 4294967296.0; // 1 << 32.
8146 uint32_t mul;
8147 // Maximise the precision of the fixed point reciprocal.
8148 while (true) {
8149 mul = static_cast<uint32_t>(scale * divisor);
8150 if (mul >= 0x7fffffff) break;
8151 scale *= 2.0;
8152 shift++;
8153 }
8154 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00008155 Register scratch2 = smi_test_reg;
8156 smi_test_reg = no_reg;
8157 __ mov(scratch2, Operand(mul));
8158 __ umull(scratch, scratch2, scratch2, lhs);
8159 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
8160 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
8161 // rhs is still the known rhs. rhs is Smi tagged.
8162 // lhs is still the unkown lhs. lhs is Smi tagged.
8163 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
8164 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008165 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00008166 scratch,
8167 scratch2,
8168 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008169 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00008170 &required_scratch_shift);
8171 // scratch << required_scratch_shift is now the Smi tagged rhs *
8172 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008173 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008174 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008175 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00008176 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008177 } else {
8178 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00008179 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008180 }
8181 }
8182 __ Ret();
8183 __ bind(&smi_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008184 } else if (op_ == Token::MOD &&
8185 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
8186 runtime_operands_type_ != BinaryOpIC::STRINGS) {
8187 // Do generate a bit of smi code for modulus even though the default for
8188 // modulus is not to do it, but as the ARM processor has no coprocessor
8189 // support for modulus checking for smis makes sense.
8190 Label slow;
8191 ASSERT(!ShouldGenerateSmiCode());
8192 ASSERT(kSmiTag == 0); // Adjust code below.
8193 // Check for two positive smis.
8194 __ orr(smi_test_reg, lhs, Operand(rhs));
8195 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
8196 __ b(ne, &slow);
8197 // Check that rhs is a power of two and not zero.
8198 __ sub(scratch, rhs, Operand(1), SetCC);
8199 __ b(mi, &slow);
8200 __ tst(rhs, scratch);
8201 __ b(ne, &slow);
8202 // Calculate power of two modulus.
8203 __ and_(result, lhs, Operand(scratch));
8204 __ Ret();
8205 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008206 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008207 HandleBinaryOpSlowCases(
8208 masm,
8209 &not_smi,
8210 lhs,
8211 rhs,
8212 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008213 break;
8214 }
8215
8216 case Token::BIT_OR:
8217 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008218 case Token::BIT_XOR:
8219 case Token::SAR:
8220 case Token::SHR:
8221 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008222 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008223 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008224 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008225 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008226 Register scratch2 = smi_test_reg;
8227 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008228 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008229 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
8230 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
8231 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008232 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008233 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00008234 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8235 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008236 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008237 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008238 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008239 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008240 // Remove tags from operands. We can't do this on a 31 bit number
8241 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00008242 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8243 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8244 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008245 // Unsigned shift is not allowed to produce a negative number, so
8246 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00008247 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008248 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008249 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00008250 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008251 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008252 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008253 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00008254 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
8255 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
8256 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008257 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00008258 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008259 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008260 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008261 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008262 default: UNREACHABLE();
8263 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008264 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008265 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008266 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008267 break;
8268 }
8269
8270 default: UNREACHABLE();
8271 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008272 // This code should be unreachable.
8273 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00008274
8275 // Generate an unreachable reference to the DEFAULT stub so that it can be
8276 // found at the end of this stub when clearing ICs at GC.
8277 // TODO(kaznacheev): Check performance impact and get rid of this.
8278 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
8279 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
8280 __ CallStub(&uninit);
8281 }
8282}
8283
8284
8285void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
8286 Label get_result;
8287
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008288 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00008289
8290 // Internal frame is necessary to handle exceptions properly.
8291 __ EnterInternalFrame();
8292 // Call the stub proper to get the result in r0.
8293 __ Call(&get_result);
8294 __ LeaveInternalFrame();
8295
8296 __ push(r0);
8297
8298 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
8299 __ push(r0);
8300 __ mov(r0, Operand(Smi::FromInt(op_)));
8301 __ push(r0);
8302 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
8303 __ push(r0);
8304
8305 __ TailCallExternalReference(
8306 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
8307 6,
8308 1);
8309
8310 // The entry point for the result calculation is assumed to be immediately
8311 // after this sequence.
8312 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008313}
8314
8315
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008316Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008317 GenericBinaryOpStub stub(key, type_info);
8318 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008319}
8320
8321
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008322void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
8323 // Argument is a number and is on stack and in r0.
8324 Label runtime_call;
8325 Label input_not_smi;
8326 Label loaded;
8327
8328 if (CpuFeatures::IsSupported(VFP3)) {
8329 // Load argument and check if it is a smi.
8330 __ BranchOnNotSmi(r0, &input_not_smi);
8331
8332 CpuFeatures::Scope scope(VFP3);
8333 // Input is a smi. Convert to double and load the low and high words
8334 // of the double into r2, r3.
8335 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
8336 __ b(&loaded);
8337
8338 __ bind(&input_not_smi);
8339 // Check if input is a HeapNumber.
8340 __ CheckMap(r0,
8341 r1,
8342 Heap::kHeapNumberMapRootIndex,
8343 &runtime_call,
8344 true);
8345 // Input is a HeapNumber. Load it to a double register and store the
8346 // low and high words into r2, r3.
8347 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
8348
8349 __ bind(&loaded);
8350 // r2 = low 32 bits of double value
8351 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008352 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008353 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
8354 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008355 __ eor(r1, r1, Operand(r1, ASR, 16));
8356 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008357 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008358 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00008359
8360 // r2 = low 32 bits of double value.
8361 // r3 = high 32 bits of double value.
8362 // r1 = TranscendentalCache::hash(double value).
8363 __ mov(r0,
8364 Operand(ExternalReference::transcendental_cache_array_address()));
8365 // r0 points to cache array.
8366 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
8367 // r0 points to the cache for the type type_.
8368 // If NULL, the cache hasn't been initialized yet, so go through runtime.
8369 __ cmp(r0, Operand(0));
8370 __ b(eq, &runtime_call);
8371
8372#ifdef DEBUG
8373 // Check that the layout of cache elements match expectations.
8374 { TranscendentalCache::Element test_elem[2];
8375 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
8376 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
8377 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
8378 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
8379 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
8380 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
8381 CHECK_EQ(0, elem_in0 - elem_start);
8382 CHECK_EQ(kIntSize, elem_in1 - elem_start);
8383 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
8384 }
8385#endif
8386
8387 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
8388 __ add(r1, r1, Operand(r1, LSL, 1));
8389 __ add(r0, r0, Operand(r1, LSL, 2));
8390 // Check if cache matches: Double value is stored in uint32_t[2] array.
8391 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
8392 __ cmp(r2, r4);
8393 __ b(ne, &runtime_call);
8394 __ cmp(r3, r5);
8395 __ b(ne, &runtime_call);
8396 // Cache hit. Load result, pop argument and return.
8397 __ mov(r0, Operand(r6));
8398 __ pop();
8399 __ Ret();
8400 }
8401
8402 __ bind(&runtime_call);
8403 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
8404}
8405
8406
8407Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
8408 switch (type_) {
8409 // Add more cases when necessary.
8410 case TranscendentalCache::SIN: return Runtime::kMath_sin;
8411 case TranscendentalCache::COS: return Runtime::kMath_cos;
8412 default:
8413 UNIMPLEMENTED();
8414 return Runtime::kAbort;
8415 }
8416}
8417
8418
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008419void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00008420 // Do tail-call to runtime routine. Runtime routines expect at least one
8421 // argument, so give it a Smi.
8422 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008423 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008424 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008425
8426 __ StubReturn(1);
8427}
8428
8429
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008430void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008431 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008432
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008433 Register heap_number_map = r6;
8434 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8435
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008436 if (op_ == Token::SUB) {
8437 // Check whether the value is a smi.
8438 Label try_float;
8439 __ tst(r0, Operand(kSmiTagMask));
8440 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008441
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008442 // Go slow case if the value of the expression is zero
8443 // to make sure that we switch between 0 and -0.
8444 __ cmp(r0, Operand(0));
8445 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008446
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008447 // The value of the expression is a smi that is not zero. Try
8448 // optimistic subtraction '0 - value'.
8449 __ rsb(r1, r0, Operand(0), SetCC);
8450 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008451
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008452 __ mov(r0, Operand(r1)); // Set r0 to result.
8453 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008454
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008455 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008456 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
8457 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8458 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008459 __ b(ne, &slow);
8460 // r0 is a heap number. Get a new heap number in r1.
8461 if (overwrite_) {
8462 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
8463 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
8464 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
8465 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008466 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008467 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
8468 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
8469 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
8470 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
8471 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
8472 __ mov(r0, Operand(r1));
8473 }
8474 } else if (op_ == Token::BIT_NOT) {
8475 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008476 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
8477 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8478 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008479 __ b(ne, &slow);
8480
8481 // Convert the heap number is r0 to an untagged integer in r1.
8482 GetInt32(masm, r0, r1, r2, r3, &slow);
8483
8484 // Do the bitwise operation (move negated) and check if the result
8485 // fits in a smi.
8486 Label try_float;
8487 __ mvn(r1, Operand(r1));
8488 __ add(r2, r1, Operand(0x40000000), SetCC);
8489 __ b(mi, &try_float);
8490 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
8491 __ b(&done);
8492
8493 __ bind(&try_float);
8494 if (!overwrite_) {
8495 // Allocate a fresh heap number, but don't overwrite r0 until
8496 // we're sure we can do it without going through the slow case
8497 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008498 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008499 __ mov(r0, Operand(r2));
8500 }
8501
8502 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
8503 // have to set up a frame.
8504 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
8505 __ push(lr);
8506 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
8507 __ pop(lr);
8508 } else {
8509 UNIMPLEMENTED();
8510 }
8511
8512 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008513 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008514
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008515 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008516 __ bind(&slow);
8517 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008518 switch (op_) {
8519 case Token::SUB:
8520 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
8521 break;
8522 case Token::BIT_NOT:
8523 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
8524 break;
8525 default:
8526 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008527 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00008528}
8529
8530
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008531void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008532 // r0 holds the exception.
8533
8534 // Adjust this code if not the case.
8535 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
8536
8537 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008538 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
8539 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008540
8541 // Restore the next handler and frame pointer, discard handler state.
8542 ASSERT(StackHandlerConstants::kNextOffset == 0);
8543 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008544 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008545 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
8546 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
8547
8548 // Before returning we restore the context from the frame pointer if
8549 // not NULL. The frame pointer is NULL in the exception handler of a
8550 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008551 __ cmp(fp, Operand(0));
8552 // Set cp to NULL if fp is NULL.
8553 __ mov(cp, Operand(0), LeaveCC, eq);
8554 // Restore cp otherwise.
8555 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008556#ifdef DEBUG
8557 if (FLAG_debug_code) {
8558 __ mov(lr, Operand(pc));
8559 }
8560#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008561 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008562 __ pop(pc);
8563}
8564
8565
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008566void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
8567 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008568 // Adjust this code if not the case.
8569 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
8570
8571 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008572 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008573 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008574
8575 // Unwind the handlers until the ENTRY handler is found.
8576 Label loop, done;
8577 __ bind(&loop);
8578 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008579 const int kStateOffset = StackHandlerConstants::kStateOffset;
8580 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008581 __ cmp(r2, Operand(StackHandler::ENTRY));
8582 __ b(eq, &done);
8583 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008584 const int kNextOffset = StackHandlerConstants::kNextOffset;
8585 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008586 __ jmp(&loop);
8587 __ bind(&done);
8588
8589 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008590 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008591 __ pop(r2);
8592 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008593
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008594 if (type == OUT_OF_MEMORY) {
8595 // Set external caught exception to false.
8596 ExternalReference external_caught(Top::k_external_caught_exception_address);
8597 __ mov(r0, Operand(false));
8598 __ mov(r2, Operand(external_caught));
8599 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008600
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008601 // Set pending exception and r0 to out of memory exception.
8602 Failure* out_of_memory = Failure::OutOfMemoryException();
8603 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
8604 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
8605 __ str(r0, MemOperand(r2));
8606 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008607
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008608 // Stack layout at this point. See also StackHandlerConstants.
8609 // sp -> state (ENTRY)
8610 // fp
8611 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008612
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008613 // Discard handler state (r2 is not used) and restore frame pointer.
8614 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
8615 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
8616 // Before returning we restore the context from the frame pointer if
8617 // not NULL. The frame pointer is NULL in the exception handler of a
8618 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008619 __ cmp(fp, Operand(0));
8620 // Set cp to NULL if fp is NULL.
8621 __ mov(cp, Operand(0), LeaveCC, eq);
8622 // Restore cp otherwise.
8623 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008624#ifdef DEBUG
8625 if (FLAG_debug_code) {
8626 __ mov(lr, Operand(pc));
8627 }
8628#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008629 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008630 __ pop(pc);
8631}
8632
8633
8634void CEntryStub::GenerateCore(MacroAssembler* masm,
8635 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008636 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008637 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008638 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008639 bool always_allocate,
8640 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008641 // r0: result parameter for PerformGC, if any
8642 // r4: number of arguments including receiver (C callee-saved)
8643 // r5: pointer to builtin function (C callee-saved)
8644 // r6: pointer to the first argument (C callee-saved)
8645
8646 if (do_gc) {
8647 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008648 __ PrepareCallCFunction(1, r1);
8649 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008650 }
8651
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008652 ExternalReference scope_depth =
8653 ExternalReference::heap_always_allocate_scope_depth();
8654 if (always_allocate) {
8655 __ mov(r0, Operand(scope_depth));
8656 __ ldr(r1, MemOperand(r0));
8657 __ add(r1, r1, Operand(1));
8658 __ str(r1, MemOperand(r0));
8659 }
8660
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008661 // Call C built-in.
8662 // r0 = argc, r1 = argv
8663 __ mov(r0, Operand(r4));
8664 __ mov(r1, Operand(r6));
8665
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008666 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
8667 int frame_alignment_mask = frame_alignment - 1;
8668#if defined(V8_HOST_ARCH_ARM)
8669 if (FLAG_debug_code) {
8670 if (frame_alignment > kPointerSize) {
8671 Label alignment_as_expected;
8672 ASSERT(IsPowerOf2(frame_alignment));
8673 __ sub(r2, sp, Operand(frame_alignment_skew));
8674 __ tst(r2, Operand(frame_alignment_mask));
8675 __ b(eq, &alignment_as_expected);
8676 // Don't use Check here, as it will call Runtime_Abort re-entering here.
8677 __ stop("Unexpected alignment");
8678 __ bind(&alignment_as_expected);
8679 }
8680 }
8681#endif
8682
8683 // Just before the call (jump) below lr is pushed, so the actual alignment is
8684 // adding one to the current skew.
8685 int alignment_before_call =
8686 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
8687 if (alignment_before_call > 0) {
8688 // Push until the alignment before the call is met.
8689 __ mov(r2, Operand(0));
8690 for (int i = alignment_before_call;
8691 (i & frame_alignment_mask) != 0;
8692 i += kPointerSize) {
8693 __ push(r2);
8694 }
8695 }
8696
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008697 // TODO(1242173): To let the GC traverse the return address of the exit
8698 // frames, we need to know where the return address is. Right now,
8699 // we push it on the stack to be able to find it again, but we never
8700 // restore from it in case of changes, which makes it impossible to
8701 // support moving the C entry code stub. This should be fixed, but currently
8702 // this is OK because the CEntryStub gets generated so early in the V8 boot
8703 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008704 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008705 masm->push(lr);
8706 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008707
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008708 // Restore sp back to before aligning the stack.
8709 if (alignment_before_call > 0) {
8710 __ add(sp, sp, Operand(alignment_before_call));
8711 }
8712
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008713 if (always_allocate) {
8714 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
8715 // though (contain the result).
8716 __ mov(r2, Operand(scope_depth));
8717 __ ldr(r3, MemOperand(r2));
8718 __ sub(r3, r3, Operand(1));
8719 __ str(r3, MemOperand(r2));
8720 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008721
8722 // check for failure result
8723 Label failure_returned;
8724 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
8725 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
8726 __ add(r2, r0, Operand(1));
8727 __ tst(r2, Operand(kFailureTagMask));
8728 __ b(eq, &failure_returned);
8729
8730 // Exit C frame and return.
8731 // r0:r1: result
8732 // sp: stack pointer
8733 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008734 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008735
8736 // check if we should retry or throw exception
8737 Label retry;
8738 __ bind(&failure_returned);
8739 ASSERT(Failure::RETRY_AFTER_GC == 0);
8740 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
8741 __ b(eq, &retry);
8742
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008743 // Special handling of out of memory exceptions.
8744 Failure* out_of_memory = Failure::OutOfMemoryException();
8745 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
8746 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008747
8748 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00008749 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008750 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008751 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008752 __ ldr(r0, MemOperand(ip));
8753 __ str(r3, MemOperand(ip));
8754
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008755 // Special handling of termination exceptions which are uncatchable
8756 // by javascript code.
8757 __ cmp(r0, Operand(Factory::termination_exception()));
8758 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008759
8760 // Handle normal exception.
8761 __ jmp(throw_normal_exception);
8762
8763 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
8764}
8765
8766
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008767void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008768 // Called from JavaScript; parameters are on stack as if calling JS function
8769 // r0: number of arguments including receiver
8770 // r1: pointer to builtin function
8771 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008772 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008773 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008774
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008775 // Result returned in r0 or r0+r1 by default.
8776
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008777 // NOTE: Invocations of builtins may return failure objects
8778 // instead of a proper result. The builtin entry handles
8779 // this by performing a garbage collection and retrying the
8780 // builtin once.
8781
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008782 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008783 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008784
8785 // r4: number of arguments (C callee-saved)
8786 // r5: pointer to builtin function (C callee-saved)
8787 // r6: pointer to first argument (C callee-saved)
8788
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008789 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008790 Label throw_termination_exception;
8791 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008792
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008793 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008794 GenerateCore(masm,
8795 &throw_normal_exception,
8796 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008797 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00008798 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008799 false,
8800 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008801
8802 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008803 GenerateCore(masm,
8804 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008805 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008806 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008807 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008808 false,
8809 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008810
8811 // Do full GC and retry runtime call one final time.
8812 Failure* failure = Failure::InternalError();
8813 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
8814 GenerateCore(masm,
8815 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008816 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008817 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00008818 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008819 true,
8820 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008821
8822 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00008823 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
8824
8825 __ bind(&throw_termination_exception);
8826 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008827
8828 __ bind(&throw_normal_exception);
8829 GenerateThrowTOS(masm);
8830}
8831
8832
8833void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
8834 // r0: code entry
8835 // r1: function
8836 // r2: receiver
8837 // r3: argc
8838 // [sp+0]: argv
8839
8840 Label invoke, exit;
8841
8842 // Called from C, so do not pop argc and args on exit (preserve sp)
8843 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008844 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008845 __ stm(db_w, sp, kCalleeSaved | lr.bit());
8846
8847 // Get address of argv, see stm above.
8848 // r0: code entry
8849 // r1: function
8850 // r2: receiver
8851 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00008852 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008853
8854 // Push a frame with special values setup to mark it as an entry frame.
8855 // r0: code entry
8856 // r1: function
8857 // r2: receiver
8858 // r3: argc
8859 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008860 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00008861 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
8862 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008863 __ mov(r6, Operand(Smi::FromInt(marker)));
8864 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8865 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008866 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008867
8868 // Setup frame pointer for the frame to be pushed.
8869 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8870
8871 // Call a faked try-block that does the invoke.
8872 __ bl(&invoke);
8873
8874 // Caught exception: Store result (exception) in the pending
8875 // exception field in the JSEnv and return a failure sentinel.
8876 // Coming in here the fp will be invalid because the PushTryHandler below
8877 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00008878 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008879 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008880 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008881 __ b(&exit);
8882
8883 // Invoke: Link this frame into the handler chain.
8884 __ bind(&invoke);
8885 // Must preserve r0-r4, r5-r7 are available.
8886 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008887 // If an exception not caught by another handler occurs, this handler
8888 // returns control to the code after the bl(&invoke) above, which
8889 // restores all kCalleeSaved registers (including cp and fp) to their
8890 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008891
8892 // Clear any pending exceptions.
8893 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
8894 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00008895 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008896 __ str(r5, MemOperand(ip));
8897
8898 // Invoke the function by calling through JS entry trampoline builtin.
8899 // Notice that we cannot store a reference to the trampoline code directly in
8900 // this stub, because runtime stubs are not traversed when doing GC.
8901
8902 // Expected registers by Builtins::JSEntryTrampoline
8903 // r0: code entry
8904 // r1: function
8905 // r2: receiver
8906 // r3: argc
8907 // r4: argv
8908 if (is_construct) {
8909 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
8910 __ mov(ip, Operand(construct_entry));
8911 } else {
8912 ExternalReference entry(Builtins::JSEntryTrampoline);
8913 __ mov(ip, Operand(entry));
8914 }
8915 __ ldr(ip, MemOperand(ip)); // deref address
8916
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008917 // Branch and link to JSEntryTrampoline. We don't use the double underscore
8918 // macro for the add instruction because we don't want the coverage tool
8919 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008920 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008921 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008922
8923 // Unlink this frame from the handler chain. When reading the
8924 // address of the next handler, there is no need to use the address
8925 // displacement since the current stack pointer (sp) points directly
8926 // to the stack handler.
8927 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
8928 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
8929 __ str(r3, MemOperand(ip));
8930 // No need to restore registers
8931 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
8932
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008933
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008934 __ bind(&exit); // r0 holds result
8935 // Restore the top frame descriptors from the stack.
8936 __ pop(r3);
8937 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
8938 __ str(r3, MemOperand(ip));
8939
8940 // Reset the stack to the callee saved registers.
8941 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
8942
8943 // Restore callee-saved registers and return.
8944#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008945 if (FLAG_debug_code) {
8946 __ mov(lr, Operand(pc));
8947 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008948#endif
8949 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
8950}
8951
8952
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008953// This stub performs an instanceof, calling the builtin function if
8954// necessary. Uses r1 for the object, r0 for the function that it may
8955// be an instance of (these are fetched from the stack).
8956void InstanceofStub::Generate(MacroAssembler* masm) {
8957 // Get the object - slow case for smis (we may need to throw an exception
8958 // depending on the rhs).
8959 Label slow, loop, is_instance, is_not_instance;
8960 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
8961 __ BranchOnSmi(r0, &slow);
8962
8963 // Check that the left hand is a JS object and put map in r3.
8964 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
8965 __ b(lt, &slow);
8966 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
8967 __ b(gt, &slow);
8968
8969 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00008970 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008971 // r1 is function, r3 is map.
8972
8973 // Look up the function and the map in the instanceof cache.
8974 Label miss;
8975 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
8976 __ cmp(r1, ip);
8977 __ b(ne, &miss);
8978 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
8979 __ cmp(r3, ip);
8980 __ b(ne, &miss);
8981 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
8982 __ pop();
8983 __ pop();
8984 __ mov(pc, Operand(lr));
8985
8986 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008987 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
8988
8989 // Check that the function prototype is a JS object.
8990 __ BranchOnSmi(r4, &slow);
8991 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
8992 __ b(lt, &slow);
8993 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
8994 __ b(gt, &slow);
8995
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00008996 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
8997 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
8998
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008999 // Register mapping: r3 is object map and r4 is function prototype.
9000 // Get prototype of object into r2.
9001 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9002
9003 // Loop through the prototype chain looking for the function prototype.
9004 __ bind(&loop);
9005 __ cmp(r2, Operand(r4));
9006 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009007 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9008 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009009 __ b(eq, &is_not_instance);
9010 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9011 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9012 __ jmp(&loop);
9013
9014 __ bind(&is_instance);
9015 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009016 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009017 __ pop();
9018 __ pop();
9019 __ mov(pc, Operand(lr)); // Return.
9020
9021 __ bind(&is_not_instance);
9022 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009023 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009024 __ pop();
9025 __ pop();
9026 __ mov(pc, Operand(lr)); // Return.
9027
9028 // Slow-case. Tail call builtin.
9029 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009030 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9031}
9032
9033
ager@chromium.org7c537e22008-10-16 08:43:32 +00009034void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9035 // The displacement is the offset of the last parameter (if any)
9036 // relative to the frame pointer.
9037 static const int kDisplacement =
9038 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009039
ager@chromium.org7c537e22008-10-16 08:43:32 +00009040 // Check that the key is a smi.
9041 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009042 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009043
ager@chromium.org7c537e22008-10-16 08:43:32 +00009044 // Check if the calling frame is an arguments adaptor frame.
9045 Label adaptor;
9046 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9047 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009048 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009049 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009050
ager@chromium.org7c537e22008-10-16 08:43:32 +00009051 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00009052 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00009053 // check for free.
9054 __ cmp(r1, r0);
9055 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009056
ager@chromium.org7c537e22008-10-16 08:43:32 +00009057 // Read the argument from the stack and return it.
9058 __ sub(r3, r0, r1);
9059 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9060 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009061 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009062
9063 // Arguments adaptor case: Check index against actual arguments
9064 // limit found in the arguments adaptor frame. Use unsigned
9065 // comparison to get negative check for free.
9066 __ bind(&adaptor);
9067 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9068 __ cmp(r1, r0);
9069 __ b(cs, &slow);
9070
9071 // Read the argument from the adaptor frame and return it.
9072 __ sub(r3, r0, r1);
9073 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
9074 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00009075 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009076
9077 // Slow-case: Handle non-smi or out-of-bounds access to arguments
9078 // by calling the runtime system.
9079 __ bind(&slow);
9080 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009081 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009082}
9083
9084
9085void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009086 // sp[0] : number of parameters
9087 // sp[4] : receiver displacement
9088 // sp[8] : function
9089
ager@chromium.org7c537e22008-10-16 08:43:32 +00009090 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00009091 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00009092 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9093 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009094 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00009095 __ b(eq, &adaptor_frame);
9096
9097 // Get the length from the frame.
9098 __ ldr(r1, MemOperand(sp, 0));
9099 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00009100
9101 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00009102 __ bind(&adaptor_frame);
9103 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
9104 __ str(r1, MemOperand(sp, 0));
9105 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009106 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
9107 __ str(r3, MemOperand(sp, 1 * kPointerSize));
9108
ager@chromium.org5c838252010-02-19 08:53:10 +00009109 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009110 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +00009111 Label add_arguments_object;
9112 __ bind(&try_allocate);
9113 __ cmp(r1, Operand(0));
9114 __ b(eq, &add_arguments_object);
9115 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
9116 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
9117 __ bind(&add_arguments_object);
9118 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
9119
9120 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009121 __ AllocateInNewSpace(
9122 r1,
9123 r0,
9124 r2,
9125 r3,
9126 &runtime,
9127 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +00009128
9129 // Get the arguments boilerplate from the current (global) context.
9130 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
9131 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
9132 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
9133 __ ldr(r4, MemOperand(r4, offset));
9134
9135 // Copy the JS object part.
9136 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
9137 __ ldr(r3, FieldMemOperand(r4, i));
9138 __ str(r3, FieldMemOperand(r0, i));
9139 }
9140
9141 // Setup the callee in-object property.
9142 ASSERT(Heap::arguments_callee_index == 0);
9143 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
9144 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
9145
9146 // Get the length (smi tagged) and set that as an in-object property too.
9147 ASSERT(Heap::arguments_length_index == 1);
9148 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
9149 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
9150
9151 // If there are no actual arguments, we're done.
9152 Label done;
9153 __ cmp(r1, Operand(0));
9154 __ b(eq, &done);
9155
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009156 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00009157 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +00009158
9159 // Setup the elements pointer in the allocated arguments object and
9160 // initialize the header in the elements fixed array.
9161 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
9162 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
9163 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
9164 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
9165 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009166 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +00009167
9168 // Copy the fixed array slots.
9169 Label loop;
9170 // Setup r4 to point to the first array slot.
9171 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
9172 __ bind(&loop);
9173 // Pre-decrement r2 with kPointerSize on each iteration.
9174 // Pre-decrement in order to skip receiver.
9175 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
9176 // Post-increment r4 with kPointerSize on each iteration.
9177 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
9178 __ sub(r1, r1, Operand(1));
9179 __ cmp(r1, Operand(0));
9180 __ b(ne, &loop);
9181
9182 // Return and remove the on-stack parameters.
9183 __ bind(&done);
9184 __ add(sp, sp, Operand(3 * kPointerSize));
9185 __ Ret();
9186
ager@chromium.org7c537e22008-10-16 08:43:32 +00009187 // Do the runtime call to allocate the arguments object.
9188 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009189 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009190}
9191
9192
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009193void RegExpExecStub::Generate(MacroAssembler* masm) {
9194 // Just jump directly to runtime if native RegExp is not selected at compile
9195 // time or if regexp entry in generated code is turned off runtime switch or
9196 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009197#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009198 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009199#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009200 if (!FLAG_regexp_entry_native) {
9201 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
9202 return;
9203 }
9204
9205 // Stack frame on entry.
9206 // sp[0]: last_match_info (expected JSArray)
9207 // sp[4]: previous index
9208 // sp[8]: subject string
9209 // sp[12]: JSRegExp object
9210
9211 static const int kLastMatchInfoOffset = 0 * kPointerSize;
9212 static const int kPreviousIndexOffset = 1 * kPointerSize;
9213 static const int kSubjectOffset = 2 * kPointerSize;
9214 static const int kJSRegExpOffset = 3 * kPointerSize;
9215
9216 Label runtime, invoke_regexp;
9217
9218 // Allocation of registers for this function. These are in callee save
9219 // registers and will be preserved by the call to the native RegExp code, as
9220 // this code is called using the normal C calling convention. When calling
9221 // directly from generated code the native RegExp code will not do a GC and
9222 // therefore the content of these registers are safe to use after the call.
9223 Register subject = r4;
9224 Register regexp_data = r5;
9225 Register last_match_info_elements = r6;
9226
9227 // Ensure that a RegExp stack is allocated.
9228 ExternalReference address_of_regexp_stack_memory_address =
9229 ExternalReference::address_of_regexp_stack_memory_address();
9230 ExternalReference address_of_regexp_stack_memory_size =
9231 ExternalReference::address_of_regexp_stack_memory_size();
9232 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
9233 __ ldr(r0, MemOperand(r0, 0));
9234 __ tst(r0, Operand(r0));
9235 __ b(eq, &runtime);
9236
9237 // Check that the first argument is a JSRegExp object.
9238 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
9239 ASSERT_EQ(0, kSmiTag);
9240 __ tst(r0, Operand(kSmiTagMask));
9241 __ b(eq, &runtime);
9242 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
9243 __ b(ne, &runtime);
9244
9245 // Check that the RegExp has been compiled (data contains a fixed array).
9246 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
9247 if (FLAG_debug_code) {
9248 __ tst(regexp_data, Operand(kSmiTagMask));
9249 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
9250 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
9251 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
9252 }
9253
9254 // regexp_data: RegExp data (FixedArray)
9255 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
9256 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
9257 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
9258 __ b(ne, &runtime);
9259
9260 // regexp_data: RegExp data (FixedArray)
9261 // Check that the number of captures fit in the static offsets vector buffer.
9262 __ ldr(r2,
9263 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
9264 // Calculate number of capture registers (number_of_captures + 1) * 2. This
9265 // uses the asumption that smis are 2 * their untagged value.
9266 ASSERT_EQ(0, kSmiTag);
9267 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
9268 __ add(r2, r2, Operand(2)); // r2 was a smi.
9269 // Check that the static offsets vector buffer is large enough.
9270 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
9271 __ b(hi, &runtime);
9272
9273 // r2: Number of capture registers
9274 // regexp_data: RegExp data (FixedArray)
9275 // Check that the second argument is a string.
9276 __ ldr(subject, MemOperand(sp, kSubjectOffset));
9277 __ tst(subject, Operand(kSmiTagMask));
9278 __ b(eq, &runtime);
9279 Condition is_string = masm->IsObjectStringType(subject, r0);
9280 __ b(NegateCondition(is_string), &runtime);
9281 // Get the length of the string to r3.
9282 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
9283
9284 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +00009285 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009286 // subject: Subject string
9287 // regexp_data: RegExp data (FixedArray)
9288 // Check that the third argument is a positive smi less than the subject
9289 // string length. A negative value will be greater (unsigned comparison).
9290 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009291 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009292 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009293 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009294 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009295
9296 // r2: Number of capture registers
9297 // subject: Subject string
9298 // regexp_data: RegExp data (FixedArray)
9299 // Check that the fourth object is a JSArray object.
9300 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
9301 __ tst(r0, Operand(kSmiTagMask));
9302 __ b(eq, &runtime);
9303 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
9304 __ b(ne, &runtime);
9305 // Check that the JSArray is in fast case.
9306 __ ldr(last_match_info_elements,
9307 FieldMemOperand(r0, JSArray::kElementsOffset));
9308 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009309 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00009310 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009311 __ b(ne, &runtime);
9312 // Check that the last match info has space for the capture registers and the
9313 // additional information.
9314 __ ldr(r0,
9315 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
9316 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009317 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009318 __ b(gt, &runtime);
9319
9320 // subject: Subject string
9321 // regexp_data: RegExp data (FixedArray)
9322 // Check the representation and encoding of the subject string.
9323 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009324 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
9325 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009326 // First check for flat string.
9327 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
9328 ASSERT_EQ(0, kStringTag | kSeqStringTag);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009329 __ b(eq, &seq_string);
9330
9331 // subject: Subject string
9332 // regexp_data: RegExp data (FixedArray)
9333 // Check for flat cons string.
9334 // A flat cons string is a cons string where the second part is the empty
9335 // string. In that case the subject string is just the first part of the cons
9336 // string. Also in this case the first part of the cons string is known to be
9337 // a sequential string or an external string.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009338 ASSERT(kExternalStringTag !=0);
9339 ASSERT_EQ(0, kConsStringTag & kExternalStringTag);
9340 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009341 __ b(ne, &runtime);
9342 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
9343 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
9344 __ cmp(r0, r1);
9345 __ b(ne, &runtime);
9346 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
9347 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
9348 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009349 // Is first part a flat string?
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009350 ASSERT_EQ(0, kSeqStringTag);
9351 __ tst(r0, Operand(kStringRepresentationMask));
9352 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009353
9354 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009355 // subject: Subject string
9356 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009357 // r0: Instance type of subject string
9358 ASSERT_EQ(4, kAsciiStringTag);
9359 ASSERT_EQ(0, kTwoByteStringTag);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009360 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009361 __ and_(r0, r0, Operand(kStringEncodingMask));
9362 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009363 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
9364 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
9365
9366 // Check that the irregexp code has been generated for the actual string
9367 // encoding. If it has, the field contains a code object otherwise it contains
9368 // the hole.
9369 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
9370 __ b(ne, &runtime);
9371
9372 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
9373 // r7: code
9374 // subject: Subject string
9375 // regexp_data: RegExp data (FixedArray)
9376 // Load used arguments before starting to push arguments for call to native
9377 // RegExp code to avoid handling changing stack height.
9378 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
9379 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
9380
9381 // r1: previous index
9382 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
9383 // r7: code
9384 // subject: Subject string
9385 // regexp_data: RegExp data (FixedArray)
9386 // All checks done. Now push arguments for native regexp code.
9387 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
9388
9389 static const int kRegExpExecuteArguments = 7;
9390 __ push(lr);
9391 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
9392
9393 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
9394 __ mov(r0, Operand(1));
9395 __ str(r0, MemOperand(sp, 2 * kPointerSize));
9396
9397 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
9398 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
9399 __ ldr(r0, MemOperand(r0, 0));
9400 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
9401 __ ldr(r2, MemOperand(r2, 0));
9402 __ add(r0, r0, Operand(r2));
9403 __ str(r0, MemOperand(sp, 1 * kPointerSize));
9404
9405 // Argument 5 (sp[0]): static offsets vector buffer.
9406 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
9407 __ str(r0, MemOperand(sp, 0 * kPointerSize));
9408
9409 // For arguments 4 and 3 get string length, calculate start of string data and
9410 // calculate the shift of the index (0 for ASCII and 1 for two byte).
9411 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009412 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009413 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
9414 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
9415 __ eor(r3, r3, Operand(1));
9416 // Argument 4 (r3): End of string data
9417 // Argument 3 (r2): Start of string data
9418 __ add(r2, r9, Operand(r1, LSL, r3));
9419 __ add(r3, r9, Operand(r0, LSL, r3));
9420
9421 // Argument 2 (r1): Previous index.
9422 // Already there
9423
9424 // Argument 1 (r0): Subject string.
9425 __ mov(r0, subject);
9426
9427 // Locate the code entry and call it.
9428 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
9429 __ CallCFunction(r7, kRegExpExecuteArguments);
9430 __ pop(lr);
9431
9432 // r0: result
9433 // subject: subject string (callee saved)
9434 // regexp_data: RegExp data (callee saved)
9435 // last_match_info_elements: Last match info elements (callee saved)
9436
9437 // Check the result.
9438 Label success;
9439 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
9440 __ b(eq, &success);
9441 Label failure;
9442 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
9443 __ b(eq, &failure);
9444 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
9445 // If not exception it can only be retry. Handle that in the runtime system.
9446 __ b(ne, &runtime);
9447 // Result must now be exception. If there is no pending exception already a
9448 // stack overflow (on the backtrack stack) was detected in RegExp code but
9449 // haven't created the exception yet. Handle that in the runtime system.
9450 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
9451 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
9452 __ ldr(r0, MemOperand(r0, 0));
9453 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
9454 __ ldr(r1, MemOperand(r1, 0));
9455 __ cmp(r0, r1);
9456 __ b(eq, &runtime);
9457 __ bind(&failure);
9458 // For failure and exception return null.
9459 __ mov(r0, Operand(Factory::null_value()));
9460 __ add(sp, sp, Operand(4 * kPointerSize));
9461 __ Ret();
9462
9463 // Process the result from the native regexp code.
9464 __ bind(&success);
9465 __ ldr(r1,
9466 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
9467 // Calculate number of capture registers (number_of_captures + 1) * 2.
9468 ASSERT_EQ(0, kSmiTag);
9469 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
9470 __ add(r1, r1, Operand(2)); // r1 was a smi.
9471
9472 // r1: number of capture registers
9473 // r4: subject string
9474 // Store the capture count.
9475 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
9476 __ str(r2, FieldMemOperand(last_match_info_elements,
9477 RegExpImpl::kLastCaptureCountOffset));
9478 // Store last subject and last input.
9479 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009480 __ str(subject,
9481 FieldMemOperand(last_match_info_elements,
9482 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009483 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009484 __ str(subject,
9485 FieldMemOperand(last_match_info_elements,
9486 RegExpImpl::kLastInputOffset));
9487 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009488 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009489
9490 // Get the static offsets vector filled by the native regexp code.
9491 ExternalReference address_of_static_offsets_vector =
9492 ExternalReference::address_of_static_offsets_vector();
9493 __ mov(r2, Operand(address_of_static_offsets_vector));
9494
9495 // r1: number of capture registers
9496 // r2: offsets vector
9497 Label next_capture, done;
9498 // Capture register counter starts from number of capture registers and
9499 // counts down until wraping after zero.
9500 __ add(r0,
9501 last_match_info_elements,
9502 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
9503 __ bind(&next_capture);
9504 __ sub(r1, r1, Operand(1), SetCC);
9505 __ b(mi, &done);
9506 // Read the value from the static offsets vector buffer.
9507 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
9508 // Store the smi value in the last match info.
9509 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
9510 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
9511 __ jmp(&next_capture);
9512 __ bind(&done);
9513
9514 // Return last match info.
9515 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
9516 __ add(sp, sp, Operand(4 * kPointerSize));
9517 __ Ret();
9518
9519 // Do the runtime call to execute the regexp.
9520 __ bind(&runtime);
9521 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00009522#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009523}
9524
9525
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009526void CallFunctionStub::Generate(MacroAssembler* masm) {
9527 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009528
9529 // If the receiver might be a value (string, number or boolean) check for this
9530 // and box it if it is.
9531 if (ReceiverMightBeValue()) {
9532 // Get the receiver from the stack.
9533 // function, receiver [, arguments]
9534 Label receiver_is_value, receiver_is_js_object;
9535 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
9536
9537 // Check if receiver is a smi (which is a number value).
9538 __ BranchOnSmi(r1, &receiver_is_value);
9539
9540 // Check if the receiver is a valid JS object.
9541 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
9542 __ b(ge, &receiver_is_js_object);
9543
9544 // Call the runtime to box the value.
9545 __ bind(&receiver_is_value);
9546 __ EnterInternalFrame();
9547 __ push(r1);
9548 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
9549 __ LeaveInternalFrame();
9550 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
9551
9552 __ bind(&receiver_is_js_object);
9553 }
9554
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009555 // Get the function to call from the stack.
9556 // function, receiver [, arguments]
9557 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
9558
9559 // Check that the function is really a JavaScript function.
9560 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009561 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009562 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009563 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009564 __ b(ne, &slow);
9565
9566 // Fast-case: Invoke the function now.
9567 // r1: pushed function
9568 ParameterCount actual(argc_);
9569 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
9570
9571 // Slow-case: Non-function called.
9572 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00009573 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
9574 // of the original receiver from the call site).
9575 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009576 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009577 __ mov(r2, Operand(0));
9578 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
9579 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
9580 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009581}
9582
9583
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009584// Unfortunately you have to run without snapshots to see most of these
9585// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009586const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009587 if (name_ != NULL) return name_;
9588 const int kMaxNameLength = 100;
9589 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
9590 if (name_ == NULL) return "OOM";
9591
9592 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009593 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009594 case lt: cc_name = "LT"; break;
9595 case gt: cc_name = "GT"; break;
9596 case le: cc_name = "LE"; break;
9597 case ge: cc_name = "GE"; break;
9598 case eq: cc_name = "EQ"; break;
9599 case ne: cc_name = "NE"; break;
9600 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009601 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009602
9603 const char* strict_name = "";
9604 if (strict_ && (cc_ == eq || cc_ == ne)) {
9605 strict_name = "_STRICT";
9606 }
9607
9608 const char* never_nan_nan_name = "";
9609 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
9610 never_nan_nan_name = "_NO_NAN";
9611 }
9612
9613 const char* include_number_compare_name = "";
9614 if (!include_number_compare_) {
9615 include_number_compare_name = "_NO_NUMBER";
9616 }
9617
9618 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
9619 "CompareStub_%s%s%s%s",
9620 cc_name,
9621 strict_name,
9622 never_nan_nan_name,
9623 include_number_compare_name);
9624 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009625}
9626
9627
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009628int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00009629 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
9630 // stubs the never NaN NaN condition is only taken into account if the
9631 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009632 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00009633 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
9634 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00009635 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
9636 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009637}
9638
9639
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009640// StringCharCodeAtGenerator
9641
9642void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
9643 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +00009644 Label ascii_string;
9645 Label got_char_code;
9646
9647 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009648 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009649
9650 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009651 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
9652 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009653 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009654 __ tst(result_, Operand(kIsNotStringMask));
9655 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009656
9657 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009658 __ BranchOnNotSmi(index_, &index_not_smi_);
9659
9660 // Put smi-tagged index into scratch register.
9661 __ mov(scratch_, index_);
9662 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009663
9664 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009665 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
9666 __ cmp(ip, Operand(scratch_));
9667 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009668
9669 // We need special handling for non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009670 ASSERT(kSeqStringTag == 0);
9671 __ tst(result_, Operand(kStringRepresentationMask));
9672 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009673
9674 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009675 __ tst(result_, Operand(kIsConsStringMask));
9676 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009677
9678 // ConsString.
9679 // Check whether the right hand side is the empty string (i.e. if
9680 // this is really a flat string in a cons string). If that is not
9681 // the case we would rather go to the runtime system now to flatten
9682 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009683 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
9684 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
9685 __ cmp(result_, Operand(ip));
9686 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009687 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009688 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
9689 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
9690 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
9691 // If the first cons component is also non-flat, then go to runtime.
9692 ASSERT(kSeqStringTag == 0);
9693 __ tst(result_, Operand(kStringRepresentationMask));
9694 __ b(nz, &call_runtime_);
9695
9696 // Check for 1-byte or 2-byte string.
9697 __ bind(&flat_string);
9698 ASSERT(kAsciiStringTag != 0);
9699 __ tst(result_, Operand(kStringEncodingMask));
9700 __ b(nz, &ascii_string);
9701
9702 // 2-byte string.
9703 // Load the 2-byte character code into the result register. We can
9704 // add without shifting since the smi tag size is the log2 of the
9705 // number of bytes in a two-byte character.
9706 ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
9707 __ add(scratch_, object_, Operand(scratch_));
9708 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
9709 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009710
9711 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009712 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009713 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009714 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
9715 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +00009716
9717 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009718 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
9719 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009720}
9721
9722
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009723void StringCharCodeAtGenerator::GenerateSlow(
9724 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
9725 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +00009726
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009727 // Index is not a smi.
9728 __ bind(&index_not_smi_);
9729 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009730 __ CheckMap(index_,
9731 scratch_,
9732 Heap::kHeapNumberMapRootIndex,
9733 index_not_number_,
9734 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009735 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00009736 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009737 __ push(index_); // Consumed by runtime conversion function.
9738 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
9739 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
9740 } else {
9741 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
9742 // NumberToSmi discards numbers that are not exact integers.
9743 __ CallRuntime(Runtime::kNumberToSmi, 1);
9744 }
9745 if (!scratch_.is(r0)) {
9746 // Save the conversion result before the pop instructions below
9747 // have a chance to overwrite it.
9748 __ mov(scratch_, r0);
9749 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009750 __ pop(index_);
9751 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00009752 // Reload the instance type.
9753 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
9754 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009755 call_helper.AfterCall(masm);
9756 // If index is still not a smi, it must be out of range.
9757 __ BranchOnNotSmi(scratch_, index_out_of_range_);
9758 // Otherwise, return to the fast path.
9759 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009760
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009761 // Call runtime. We get here when the receiver is a string and the
9762 // index is a number, but the code of getting the actual character
9763 // is too complex (e.g., when the string needs to be flattened).
9764 __ bind(&call_runtime_);
9765 call_helper.BeforeCall(masm);
9766 __ Push(object_, index_);
9767 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
9768 if (!result_.is(r0)) {
9769 __ mov(result_, r0);
9770 }
9771 call_helper.AfterCall(masm);
9772 __ jmp(&exit_);
9773
9774 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
9775}
9776
9777
9778// -------------------------------------------------------------------------
9779// StringCharFromCodeGenerator
9780
9781void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00009782 // Fast case of Heap::LookupSingleCharacterStringFromCode.
9783 ASSERT(kSmiTag == 0);
9784 ASSERT(kSmiShiftSize == 0);
9785 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009786 __ tst(code_,
9787 Operand(kSmiTagMask |
9788 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
9789 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009790
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009791 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
9792 // At this point code register contains smi tagged ascii char code.
ager@chromium.orgac091b72010-05-05 07:34:42 +00009793 ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009794 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
9795 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
9796 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
9797 __ cmp(result_, Operand(ip));
9798 __ b(eq, &slow_case_);
9799 __ bind(&exit_);
9800}
ager@chromium.orgac091b72010-05-05 07:34:42 +00009801
ager@chromium.orgac091b72010-05-05 07:34:42 +00009802
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009803void StringCharFromCodeGenerator::GenerateSlow(
9804 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
9805 __ Abort("Unexpected fallthrough to CharFromCode slow case");
9806
9807 __ bind(&slow_case_);
9808 call_helper.BeforeCall(masm);
9809 __ push(code_);
9810 __ CallRuntime(Runtime::kCharFromCode, 1);
9811 if (!result_.is(r0)) {
9812 __ mov(result_, r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009813 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00009814 call_helper.AfterCall(masm);
9815 __ jmp(&exit_);
9816
9817 __ Abort("Unexpected fallthrough from CharFromCode slow case");
9818}
9819
9820
9821// -------------------------------------------------------------------------
9822// StringCharAtGenerator
9823
9824void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
9825 char_code_at_generator_.GenerateFast(masm);
9826 char_from_code_generator_.GenerateFast(masm);
9827}
9828
9829
9830void StringCharAtGenerator::GenerateSlow(
9831 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
9832 char_code_at_generator_.GenerateSlow(masm, call_helper);
9833 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +00009834}
9835
9836
9837void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
9838 Register dest,
9839 Register src,
9840 Register count,
9841 Register scratch,
9842 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009843 Label loop;
9844 Label done;
9845 // This loop just copies one character at a time, as it is only used for very
9846 // short strings.
9847 if (!ascii) {
9848 __ add(count, count, Operand(count), SetCC);
9849 } else {
9850 __ cmp(count, Operand(0));
9851 }
9852 __ b(eq, &done);
9853
9854 __ bind(&loop);
9855 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
9856 // Perform sub between load and dependent store to get the load time to
9857 // complete.
9858 __ sub(count, count, Operand(1), SetCC);
9859 __ strb(scratch, MemOperand(dest, 1, PostIndex));
9860 // last iteration.
9861 __ b(gt, &loop);
9862
9863 __ bind(&done);
9864}
9865
9866
9867enum CopyCharactersFlags {
9868 COPY_ASCII = 1,
9869 DEST_ALWAYS_ALIGNED = 2
9870};
9871
9872
ager@chromium.orgac091b72010-05-05 07:34:42 +00009873void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
9874 Register dest,
9875 Register src,
9876 Register count,
9877 Register scratch1,
9878 Register scratch2,
9879 Register scratch3,
9880 Register scratch4,
9881 Register scratch5,
9882 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +00009883 bool ascii = (flags & COPY_ASCII) != 0;
9884 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
9885
9886 if (dest_always_aligned && FLAG_debug_code) {
9887 // Check that destination is actually word aligned if the flag says
9888 // that it is.
9889 __ tst(dest, Operand(kPointerAlignmentMask));
9890 __ Check(eq, "Destination of copy not aligned.");
9891 }
9892
9893 const int kReadAlignment = 4;
9894 const int kReadAlignmentMask = kReadAlignment - 1;
9895 // Ensure that reading an entire aligned word containing the last character
9896 // of a string will not read outside the allocated area (because we pad up
9897 // to kObjectAlignment).
9898 ASSERT(kObjectAlignment >= kReadAlignment);
9899 // Assumes word reads and writes are little endian.
9900 // Nothing to do for zero characters.
9901 Label done;
9902 if (!ascii) {
9903 __ add(count, count, Operand(count), SetCC);
9904 } else {
9905 __ cmp(count, Operand(0));
9906 }
9907 __ b(eq, &done);
9908
9909 // Assume that you cannot read (or write) unaligned.
9910 Label byte_loop;
9911 // Must copy at least eight bytes, otherwise just do it one byte at a time.
9912 __ cmp(count, Operand(8));
9913 __ add(count, dest, Operand(count));
9914 Register limit = count; // Read until src equals this.
9915 __ b(lt, &byte_loop);
9916
9917 if (!dest_always_aligned) {
9918 // Align dest by byte copying. Copies between zero and three bytes.
9919 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
9920 Label dest_aligned;
9921 __ b(eq, &dest_aligned);
9922 __ cmp(scratch4, Operand(2));
9923 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
9924 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
9925 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
9926 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9927 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
9928 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
9929 __ bind(&dest_aligned);
9930 }
9931
9932 Label simple_loop;
9933
9934 __ sub(scratch4, dest, Operand(src));
9935 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
9936 __ b(eq, &simple_loop);
9937 // Shift register is number of bits in a source word that
9938 // must be combined with bits in the next source word in order
9939 // to create a destination word.
9940
9941 // Complex loop for src/dst that are not aligned the same way.
9942 {
9943 Label loop;
9944 __ mov(scratch4, Operand(scratch4, LSL, 3));
9945 Register left_shift = scratch4;
9946 __ and_(src, src, Operand(~3)); // Round down to load previous word.
9947 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9948 // Store the "shift" most significant bits of scratch in the least
9949 // signficant bits (i.e., shift down by (32-shift)).
9950 __ rsb(scratch2, left_shift, Operand(32));
9951 Register right_shift = scratch2;
9952 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
9953
9954 __ bind(&loop);
9955 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
9956 __ sub(scratch5, limit, Operand(dest));
9957 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
9958 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9959 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
9960 // Loop if four or more bytes left to copy.
9961 // Compare to eight, because we did the subtract before increasing dst.
9962 __ sub(scratch5, scratch5, Operand(8), SetCC);
9963 __ b(ge, &loop);
9964 }
9965 // There is now between zero and three bytes left to copy (negative that
9966 // number is in scratch5), and between one and three bytes already read into
9967 // scratch1 (eight times that number in scratch4). We may have read past
9968 // the end of the string, but because objects are aligned, we have not read
9969 // past the end of the object.
9970 // Find the minimum of remaining characters to move and preloaded characters
9971 // and write those as bytes.
9972 __ add(scratch5, scratch5, Operand(4), SetCC);
9973 __ b(eq, &done);
9974 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
9975 // Move minimum of bytes read and bytes left to copy to scratch4.
9976 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
9977 // Between one and three (value in scratch5) characters already read into
9978 // scratch ready to write.
9979 __ cmp(scratch5, Operand(2));
9980 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
9981 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
9982 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
9983 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
9984 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
9985 // Copy any remaining bytes.
9986 __ b(&byte_loop);
9987
9988 // Simple loop.
9989 // Copy words from src to dst, until less than four bytes left.
9990 // Both src and dest are word aligned.
9991 __ bind(&simple_loop);
9992 {
9993 Label loop;
9994 __ bind(&loop);
9995 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
9996 __ sub(scratch3, limit, Operand(dest));
9997 __ str(scratch1, MemOperand(dest, 4, PostIndex));
9998 // Compare to 8, not 4, because we do the substraction before increasing
9999 // dest.
10000 __ cmp(scratch3, Operand(8));
10001 __ b(ge, &loop);
10002 }
10003
10004 // Copy bytes from src to dst until dst hits limit.
10005 __ bind(&byte_loop);
10006 __ cmp(dest, Operand(limit));
10007 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10008 __ b(ge, &done);
10009 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10010 __ b(&byte_loop);
10011
10012 __ bind(&done);
10013}
10014
10015
ager@chromium.orgac091b72010-05-05 07:34:42 +000010016void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10017 Register c1,
10018 Register c2,
10019 Register scratch1,
10020 Register scratch2,
10021 Register scratch3,
10022 Register scratch4,
10023 Register scratch5,
10024 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010025 // Register scratch3 is the general scratch register in this function.
10026 Register scratch = scratch3;
10027
10028 // Make sure that both characters are not digits as such strings has a
10029 // different hash algorithm. Don't try to look for these in the symbol table.
10030 Label not_array_index;
10031 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10032 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10033 __ b(hi, &not_array_index);
10034 __ sub(scratch, c2, Operand(static_cast<int>('0')));
10035 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10036
10037 // If check failed combine both characters into single halfword.
10038 // This is required by the contract of the method: code at the
10039 // not_found branch expects this combination in c1 register
10040 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
10041 __ b(ls, not_found);
10042
10043 __ bind(&not_array_index);
10044 // Calculate the two character string hash.
10045 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010046 StringHelper::GenerateHashInit(masm, hash, c1);
10047 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
10048 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010049
10050 // Collect the two characters in a register.
10051 Register chars = c1;
10052 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
10053
10054 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10055 // hash: hash of two character string.
10056
10057 // Load symbol table
10058 // Load address of first element of the symbol table.
10059 Register symbol_table = c2;
10060 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
10061
10062 // Load undefined value
10063 Register undefined = scratch4;
10064 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
10065
10066 // Calculate capacity mask from the symbol table capacity.
10067 Register mask = scratch2;
10068 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
10069 __ mov(mask, Operand(mask, ASR, 1));
10070 __ sub(mask, mask, Operand(1));
10071
10072 // Calculate untagged address of the first element of the symbol table.
10073 Register first_symbol_table_element = symbol_table;
10074 __ add(first_symbol_table_element, symbol_table,
10075 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
10076
10077 // Registers
10078 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
10079 // hash: hash of two character string
10080 // mask: capacity mask
10081 // first_symbol_table_element: address of the first element of
10082 // the symbol table
10083 // scratch: -
10084
10085 // Perform a number of probes in the symbol table.
10086 static const int kProbes = 4;
10087 Label found_in_symbol_table;
10088 Label next_probe[kProbes];
10089 for (int i = 0; i < kProbes; i++) {
10090 Register candidate = scratch5; // Scratch register contains candidate.
10091
10092 // Calculate entry in symbol table.
10093 if (i > 0) {
10094 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
10095 } else {
10096 __ mov(candidate, hash);
10097 }
10098
10099 __ and_(candidate, candidate, Operand(mask));
10100
10101 // Load the entry from the symble table.
10102 ASSERT_EQ(1, SymbolTable::kEntrySize);
10103 __ ldr(candidate,
10104 MemOperand(first_symbol_table_element,
10105 candidate,
10106 LSL,
10107 kPointerSizeLog2));
10108
10109 // If entry is undefined no string with this hash can be found.
10110 __ cmp(candidate, undefined);
10111 __ b(eq, not_found);
10112
10113 // If length is not 2 the string is not a candidate.
10114 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010115 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010116 __ b(ne, &next_probe[i]);
10117
10118 // Check that the candidate is a non-external ascii string.
10119 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
10120 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
10121 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
10122 &next_probe[i]);
10123
10124 // Check if the two characters match.
10125 // Assumes that word load is little endian.
10126 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
10127 __ cmp(chars, scratch);
10128 __ b(eq, &found_in_symbol_table);
10129 __ bind(&next_probe[i]);
10130 }
10131
10132 // No matching 2 character string found by probing.
10133 __ jmp(not_found);
10134
10135 // Scratch register contains result when we fall through to here.
10136 Register result = scratch;
10137 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000010138 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010139}
10140
10141
ager@chromium.orgac091b72010-05-05 07:34:42 +000010142void StringHelper::GenerateHashInit(MacroAssembler* masm,
10143 Register hash,
10144 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010145 // hash = character + (character << 10);
10146 __ add(hash, character, Operand(character, LSL, 10));
10147 // hash ^= hash >> 6;
10148 __ eor(hash, hash, Operand(hash, ASR, 6));
10149}
10150
10151
ager@chromium.orgac091b72010-05-05 07:34:42 +000010152void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
10153 Register hash,
10154 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010155 // hash += character;
10156 __ add(hash, hash, Operand(character));
10157 // hash += hash << 10;
10158 __ add(hash, hash, Operand(hash, LSL, 10));
10159 // hash ^= hash >> 6;
10160 __ eor(hash, hash, Operand(hash, ASR, 6));
10161}
10162
10163
ager@chromium.orgac091b72010-05-05 07:34:42 +000010164void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
10165 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010166 // hash += hash << 3;
10167 __ add(hash, hash, Operand(hash, LSL, 3));
10168 // hash ^= hash >> 11;
10169 __ eor(hash, hash, Operand(hash, ASR, 11));
10170 // hash += hash << 15;
10171 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
10172
10173 // if (hash == 0) hash = 27;
10174 __ mov(hash, Operand(27), LeaveCC, nz);
10175}
10176
10177
ager@chromium.org5c838252010-02-19 08:53:10 +000010178void SubStringStub::Generate(MacroAssembler* masm) {
10179 Label runtime;
10180
10181 // Stack frame on entry.
10182 // lr: return address
10183 // sp[0]: to
10184 // sp[4]: from
10185 // sp[8]: string
10186
10187 // This stub is called from the native-call %_SubString(...), so
10188 // nothing can be assumed about the arguments. It is tested that:
10189 // "string" is a sequential string,
10190 // both "from" and "to" are smis, and
10191 // 0 <= from <= to <= string.length.
10192 // If any of these assumptions fail, we call the runtime system.
10193
10194 static const int kToOffset = 0 * kPointerSize;
10195 static const int kFromOffset = 1 * kPointerSize;
10196 static const int kStringOffset = 2 * kPointerSize;
10197
10198
10199 // Check bounds and smi-ness.
10200 __ ldr(r7, MemOperand(sp, kToOffset));
10201 __ ldr(r6, MemOperand(sp, kFromOffset));
10202 ASSERT_EQ(0, kSmiTag);
10203 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
10204 // I.e., arithmetic shift right by one un-smi-tags.
10205 __ mov(r2, Operand(r7, ASR, 1), SetCC);
10206 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
10207 // If either r2 or r6 had the smi tag bit set, then carry is set now.
10208 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
10209 __ b(mi, &runtime); // From is negative.
10210
10211 __ sub(r2, r2, Operand(r3), SetCC);
10212 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010213 // Special handling of sub-strings of length 1 and 2. One character strings
10214 // are handled in the runtime system (looked up in the single character
10215 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000010216 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010217 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000010218
10219 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010220 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010221 // r6: from (smi)
10222 // r7: to (smi)
10223
10224 // Make sure first argument is a sequential (or flat) string.
10225 __ ldr(r5, MemOperand(sp, kStringOffset));
10226 ASSERT_EQ(0, kSmiTag);
10227 __ tst(r5, Operand(kSmiTagMask));
10228 __ b(eq, &runtime);
10229 Condition is_string = masm->IsObjectStringType(r5, r1);
10230 __ b(NegateCondition(is_string), &runtime);
10231
10232 // r1: instance type
10233 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010234 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010235 // r5: string
10236 // r6: from (smi)
10237 // r7: to (smi)
10238 Label seq_string;
10239 __ and_(r4, r1, Operand(kStringRepresentationMask));
10240 ASSERT(kSeqStringTag < kConsStringTag);
10241 ASSERT(kExternalStringTag > kConsStringTag);
10242 __ cmp(r4, Operand(kConsStringTag));
10243 __ b(gt, &runtime); // External strings go to runtime.
10244 __ b(lt, &seq_string); // Sequential strings are handled directly.
10245
10246 // Cons string. Try to recurse (once) on the first substring.
10247 // (This adds a little more generality than necessary to handle flattened
10248 // cons strings, but not much).
10249 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
10250 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
10251 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10252 __ tst(r1, Operand(kStringRepresentationMask));
10253 ASSERT_EQ(0, kSeqStringTag);
10254 __ b(ne, &runtime); // Cons and External strings go to runtime.
10255
10256 // Definitly a sequential string.
10257 __ bind(&seq_string);
10258
10259 // r1: instance type.
10260 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010261 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010262 // r5: string
10263 // r6: from (smi)
10264 // r7: to (smi)
10265 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010266 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000010267 __ b(lt, &runtime); // Fail if to > length.
10268
10269 // r1: instance type.
10270 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010271 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000010272 // r5: string.
10273 // r6: from offset (smi)
10274 // Check for flat ascii string.
10275 Label non_ascii_flat;
10276 __ tst(r1, Operand(kStringEncodingMask));
10277 ASSERT_EQ(0, kTwoByteStringTag);
10278 __ b(eq, &non_ascii_flat);
10279
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010280 Label result_longer_than_two;
10281 __ cmp(r2, Operand(2));
10282 __ b(gt, &result_longer_than_two);
10283
10284 // Sub string of length 2 requested.
10285 // Get the two characters forming the sub string.
10286 __ add(r5, r5, Operand(r3));
10287 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
10288 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
10289
10290 // Try to lookup two character string in symbol table.
10291 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010292 StringHelper::GenerateTwoCharacterSymbolTableProbe(
10293 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010294 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10295 __ add(sp, sp, Operand(3 * kPointerSize));
10296 __ Ret();
10297
10298 // r2: result string length.
10299 // r3: two characters combined into halfword in little endian byte order.
10300 __ bind(&make_two_character_string);
10301 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
10302 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
10303 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10304 __ add(sp, sp, Operand(3 * kPointerSize));
10305 __ Ret();
10306
10307 __ bind(&result_longer_than_two);
10308
ager@chromium.org5c838252010-02-19 08:53:10 +000010309 // Allocate the result.
10310 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
10311
10312 // r0: result string.
10313 // r2: result string length.
10314 // r5: string.
10315 // r6: from offset (smi)
10316 // Locate first character of result.
10317 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10318 // Locate 'from' character of string.
10319 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10320 __ add(r5, r5, Operand(r6, ASR, 1));
10321
10322 // r0: result string.
10323 // r1: first character of result string.
10324 // r2: result string length.
10325 // r5: first character of sub string to copy.
10326 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010327 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
10328 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000010329 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10330 __ add(sp, sp, Operand(3 * kPointerSize));
10331 __ Ret();
10332
10333 __ bind(&non_ascii_flat);
10334 // r2: result string length.
10335 // r5: string.
10336 // r6: from offset (smi)
10337 // Check for flat two byte string.
10338
10339 // Allocate the result.
10340 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
10341
10342 // r0: result string.
10343 // r2: result string length.
10344 // r5: string.
10345 // Locate first character of result.
10346 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
10347 // Locate 'from' character of string.
10348 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
10349 // As "from" is a smi it is 2 times the value which matches the size of a two
10350 // byte character.
10351 __ add(r5, r5, Operand(r6));
10352
10353 // r0: result string.
10354 // r1: first character of result.
10355 // r2: result length.
10356 // r5: first character of string to copy.
10357 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010358 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
10359 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000010360 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
10361 __ add(sp, sp, Operand(3 * kPointerSize));
10362 __ Ret();
10363
10364 // Just jump to runtime to create the sub string.
10365 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010366 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010367}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010368
10369
10370void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
10371 Register left,
10372 Register right,
10373 Register scratch1,
10374 Register scratch2,
10375 Register scratch3,
10376 Register scratch4) {
10377 Label compare_lengths;
10378 // Find minimum length and length difference.
10379 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
10380 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
10381 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
10382 Register length_delta = scratch3;
10383 __ mov(scratch1, scratch2, LeaveCC, gt);
10384 Register min_length = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010385 ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010386 __ tst(min_length, Operand(min_length));
10387 __ b(eq, &compare_lengths);
10388
ager@chromium.orgac091b72010-05-05 07:34:42 +000010389 // Untag smi.
10390 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
10391
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010392 // Setup registers so that we only need to increment one register
10393 // in the loop.
10394 __ add(scratch2, min_length,
10395 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10396 __ add(left, left, Operand(scratch2));
10397 __ add(right, right, Operand(scratch2));
10398 // Registers left and right points to the min_length character of strings.
10399 __ rsb(min_length, min_length, Operand(-1));
10400 Register index = min_length;
10401 // Index starts at -min_length.
10402
10403 {
10404 // Compare loop.
10405 Label loop;
10406 __ bind(&loop);
10407 // Compare characters.
10408 __ add(index, index, Operand(1), SetCC);
10409 __ ldrb(scratch2, MemOperand(left, index), ne);
10410 __ ldrb(scratch4, MemOperand(right, index), ne);
10411 // Skip to compare lengths with eq condition true.
10412 __ b(eq, &compare_lengths);
10413 __ cmp(scratch2, scratch4);
10414 __ b(eq, &loop);
10415 // Fallthrough with eq condition false.
10416 }
10417 // Compare lengths - strings up to min-length are equal.
10418 __ bind(&compare_lengths);
10419 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
10420 // Use zero length_delta as result.
10421 __ mov(r0, Operand(length_delta), SetCC, eq);
10422 // Fall through to here if characters compare not-equal.
10423 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
10424 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
10425 __ Ret();
10426}
10427
10428
10429void StringCompareStub::Generate(MacroAssembler* masm) {
10430 Label runtime;
10431
10432 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000010433 // sp[0]: right string
10434 // sp[4]: left string
10435 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
10436 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010437
10438 Label not_same;
10439 __ cmp(r0, r1);
10440 __ b(ne, &not_same);
10441 ASSERT_EQ(0, EQUAL);
10442 ASSERT_EQ(0, kSmiTag);
10443 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
10444 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
10445 __ add(sp, sp, Operand(2 * kPointerSize));
10446 __ Ret();
10447
10448 __ bind(&not_same);
10449
10450 // Check that both objects are sequential ascii strings.
10451 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
10452
10453 // Compare flat ascii strings natively. Remove arguments from stack first.
10454 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
10455 __ add(sp, sp, Operand(2 * kPointerSize));
10456 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
10457
10458 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
10459 // tagged as a small integer.
10460 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010461 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010462}
10463
10464
ager@chromium.org5c838252010-02-19 08:53:10 +000010465void StringAddStub::Generate(MacroAssembler* masm) {
10466 Label string_add_runtime;
10467 // Stack on entry:
10468 // sp[0]: second argument.
10469 // sp[4]: first argument.
10470
10471 // Load the two arguments.
10472 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
10473 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
10474
10475 // Make sure that both arguments are strings if not known in advance.
10476 if (string_check_) {
10477 ASSERT_EQ(0, kSmiTag);
10478 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
10479 // Load instance types.
10480 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
10481 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
10482 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10483 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
10484 ASSERT_EQ(0, kStringTag);
10485 // If either is not a string, go to runtime.
10486 __ tst(r4, Operand(kIsNotStringMask));
10487 __ tst(r5, Operand(kIsNotStringMask), eq);
10488 __ b(ne, &string_add_runtime);
10489 }
10490
10491 // Both arguments are strings.
10492 // r0: first string
10493 // r1: second string
10494 // r4: first string instance type (if string_check_)
10495 // r5: second string instance type (if string_check_)
10496 {
10497 Label strings_not_empty;
10498 // Check if either of the strings are empty. In that case return the other.
10499 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
10500 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010501 ASSERT(kSmiTag == 0);
10502 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000010503 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010504 ASSERT(kSmiTag == 0);
10505 // Else test if second string is empty.
10506 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000010507 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
10508
10509 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10510 __ add(sp, sp, Operand(2 * kPointerSize));
10511 __ Ret();
10512
10513 __ bind(&strings_not_empty);
10514 }
10515
ager@chromium.orgac091b72010-05-05 07:34:42 +000010516 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
10517 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000010518 // Both strings are non-empty.
10519 // r0: first string
10520 // r1: second string
10521 // r2: length of first string
10522 // r3: length of second string
10523 // r4: first string instance type (if string_check_)
10524 // r5: second string instance type (if string_check_)
10525 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010526 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000010527 // Adding two lengths can't overflow.
10528 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
10529 __ add(r6, r2, Operand(r3));
10530 // Use the runtime system when adding two one character strings, as it
10531 // contains optimizations for this specific case using the symbol table.
10532 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010533 __ b(ne, &longer_than_two);
10534
10535 // Check that both strings are non-external ascii strings.
10536 if (!string_check_) {
10537 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
10538 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
10539 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10540 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
10541 }
10542 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
10543 &string_add_runtime);
10544
10545 // Get the two characters forming the sub string.
10546 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
10547 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
10548
10549 // Try to lookup two character string in symbol table. If it is not found
10550 // just allocate a new one.
10551 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010552 StringHelper::GenerateTwoCharacterSymbolTableProbe(
10553 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010554 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10555 __ add(sp, sp, Operand(2 * kPointerSize));
10556 __ Ret();
10557
10558 __ bind(&make_two_character_string);
10559 // Resulting string has length 2 and first chars of two strings
10560 // are combined into single halfword in r2 register.
10561 // So we can fill resulting string without two loops by a single
10562 // halfword store instruction (which assumes that processor is
10563 // in a little endian mode)
10564 __ mov(r6, Operand(2));
10565 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
10566 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
10567 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10568 __ add(sp, sp, Operand(2 * kPointerSize));
10569 __ Ret();
10570
10571 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000010572 // Check if resulting string will be flat.
10573 __ cmp(r6, Operand(String::kMinNonFlatLength));
10574 __ b(lt, &string_add_flat_result);
10575 // Handle exceptionally long strings in the runtime system.
10576 ASSERT((String::kMaxLength & 0x80000000) == 0);
10577 ASSERT(IsPowerOf2(String::kMaxLength + 1));
10578 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
10579 __ cmp(r6, Operand(String::kMaxLength + 1));
10580 __ b(hs, &string_add_runtime);
10581
10582 // If result is not supposed to be flat, allocate a cons string object.
10583 // If both strings are ascii the result is an ascii cons string.
10584 if (!string_check_) {
10585 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
10586 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
10587 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10588 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
10589 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010590 Label non_ascii, allocated, ascii_data;
ager@chromium.org5c838252010-02-19 08:53:10 +000010591 ASSERT_EQ(0, kTwoByteStringTag);
10592 __ tst(r4, Operand(kStringEncodingMask));
10593 __ tst(r5, Operand(kStringEncodingMask), ne);
10594 __ b(eq, &non_ascii);
10595
10596 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010597 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000010598 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
10599 __ bind(&allocated);
10600 // Fill the fields of the cons string.
10601 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
10602 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
10603 __ mov(r0, Operand(r7));
10604 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10605 __ add(sp, sp, Operand(2 * kPointerSize));
10606 __ Ret();
10607
10608 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010609 // At least one of the strings is two-byte. Check whether it happens
10610 // to contain only ascii characters.
10611 // r4: first instance type.
10612 // r5: second instance type.
10613 __ tst(r4, Operand(kAsciiDataHintMask));
10614 __ tst(r5, Operand(kAsciiDataHintMask), ne);
10615 __ b(ne, &ascii_data);
10616 __ eor(r4, r4, Operand(r5));
10617 ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
10618 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
10619 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
10620 __ b(eq, &ascii_data);
10621
ager@chromium.org5c838252010-02-19 08:53:10 +000010622 // Allocate a two byte cons string.
10623 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
10624 __ jmp(&allocated);
10625
10626 // Handle creating a flat result. First check that both strings are
10627 // sequential and that they have the same encoding.
10628 // r0: first string
10629 // r1: second string
10630 // r2: length of first string
10631 // r3: length of second string
10632 // r4: first string instance type (if string_check_)
10633 // r5: second string instance type (if string_check_)
10634 // r6: sum of lengths.
10635 __ bind(&string_add_flat_result);
10636 if (!string_check_) {
10637 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
10638 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
10639 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
10640 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
10641 }
10642 // Check that both strings are sequential.
10643 ASSERT_EQ(0, kSeqStringTag);
10644 __ tst(r4, Operand(kStringRepresentationMask));
10645 __ tst(r5, Operand(kStringRepresentationMask), eq);
10646 __ b(ne, &string_add_runtime);
10647 // Now check if both strings have the same encoding (ASCII/Two-byte).
10648 // r0: first string.
10649 // r1: second string.
10650 // r2: length of first string.
10651 // r3: length of second string.
10652 // r6: sum of lengths..
10653 Label non_ascii_string_add_flat_result;
10654 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
10655 __ eor(r7, r4, Operand(r5));
10656 __ tst(r7, Operand(kStringEncodingMask));
10657 __ b(ne, &string_add_runtime);
10658 // And see if it's ASCII or two-byte.
10659 __ tst(r4, Operand(kStringEncodingMask));
10660 __ b(eq, &non_ascii_string_add_flat_result);
10661
10662 // Both strings are sequential ASCII strings. We also know that they are
10663 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010664 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000010665 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
10666 // Locate first character of result.
10667 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10668 // Locate first character of first argument.
10669 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10670 // r0: first character of first string.
10671 // r1: second string.
10672 // r2: length of first string.
10673 // r3: length of second string.
10674 // r6: first character of result.
10675 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010676 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000010677
10678 // Load second argument and locate first character.
10679 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10680 // r1: first character of second string.
10681 // r3: length of second string.
10682 // r6: next character of result.
10683 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010684 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000010685 __ mov(r0, Operand(r7));
10686 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10687 __ add(sp, sp, Operand(2 * kPointerSize));
10688 __ Ret();
10689
10690 __ bind(&non_ascii_string_add_flat_result);
10691 // Both strings are sequential two byte strings.
10692 // r0: first string.
10693 // r1: second string.
10694 // r2: length of first string.
10695 // r3: length of second string.
10696 // r6: sum of length of strings.
10697 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
10698 // r0: first string.
10699 // r1: second string.
10700 // r2: length of first string.
10701 // r3: length of second string.
10702 // r7: result string.
10703
10704 // Locate first character of result.
10705 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
10706 // Locate first character of first argument.
10707 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
10708
10709 // r0: first character of first string.
10710 // r1: second string.
10711 // r2: length of first string.
10712 // r3: length of second string.
10713 // r6: first character of result.
10714 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010715 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000010716
10717 // Locate first character of second argument.
10718 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
10719
10720 // r1: first character of second string.
10721 // r3: length of second string.
10722 // r6: next character of result (after copy of first string).
10723 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010724 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000010725
10726 __ mov(r0, Operand(r7));
10727 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
10728 __ add(sp, sp, Operand(2 * kPointerSize));
10729 __ Ret();
10730
10731 // Just jump to runtime to add the two strings.
10732 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010733 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010734}
10735
10736
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010737#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000010738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000010739} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000010740
10741#endif // V8_TARGET_ARCH_ARM