blob: e20be008a485cf026a0ce60b894606ce90dc6dbf [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_ARM)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
33#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000034#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "debug.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000036#include "ic-inl.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000037#include "jsregexp.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000038#include "jump-target-light-inl.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000039#include "parser.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000040#include "regexp-macro-assembler.h"
41#include "regexp-stack.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000042#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000044#include "scopes.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000045#include "virtual-frame-inl.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000046#include "virtual-frame-arm-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000051
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000052static void EmitIdenticalObjectComparison(MacroAssembler* masm,
53 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000054 Condition cc,
55 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000056static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +000057 Register lhs,
58 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000059 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000060 Label* slow,
61 bool strict);
62static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
ager@chromium.orgb5737492010-07-15 09:29:43 +000063static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
64 Register lhs,
65 Register rhs);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000066static void MultiplyByKnownInt(MacroAssembler* masm,
67 Register source,
68 Register destination,
69 int known_int);
70static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000071
72
ricow@chromium.org30ce4112010-05-31 10:38:25 +000073#define __ ACCESS_MASM(masm_)
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000074
ager@chromium.orge2902be2009-06-08 12:21:35 +000075// -------------------------------------------------------------------------
76// Platform-specific DeferredCode functions.
77
78void DeferredCode::SaveRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000079 // On ARM you either have a completely spilled frame or you
80 // handle it yourself, but at the moment there's no automation
81 // of registers and deferred code.
ager@chromium.orge2902be2009-06-08 12:21:35 +000082}
83
84
85void DeferredCode::RestoreRegisters() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000086}
87
88
89// -------------------------------------------------------------------------
90// Platform-specific RuntimeCallHelper functions.
91
92void VirtualFrameRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
93 frame_state_->frame()->AssertIsSpilled();
94}
95
96
97void VirtualFrameRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
98}
99
100
101void ICRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
102 masm->EnterInternalFrame();
103}
104
105
106void ICRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
107 masm->LeaveInternalFrame();
ager@chromium.orge2902be2009-06-08 12:21:35 +0000108}
109
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000110
111// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000112// CodeGenState implementation.
113
ager@chromium.org7c537e22008-10-16 08:43:32 +0000114CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000115 : owner_(owner),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000116 previous_(owner->state()) {
117 owner->set_state(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000118}
119
120
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000121ConditionCodeGenState::ConditionCodeGenState(CodeGenerator* owner,
122 JumpTarget* true_target,
123 JumpTarget* false_target)
124 : CodeGenState(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000125 true_target_(true_target),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000126 false_target_(false_target) {
127 owner->set_state(this);
128}
129
130
131TypeInfoCodeGenState::TypeInfoCodeGenState(CodeGenerator* owner,
132 Slot* slot,
133 TypeInfo type_info)
134 : CodeGenState(owner),
135 slot_(slot) {
136 owner->set_state(this);
137 old_type_info_ = owner->set_type_info(slot, type_info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000138}
139
140
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000141CodeGenState::~CodeGenState() {
142 ASSERT(owner_->state() == this);
143 owner_->set_state(previous_);
144}
145
146
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000147TypeInfoCodeGenState::~TypeInfoCodeGenState() {
148 owner()->set_type_info(slot_, old_type_info_);
149}
150
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000151// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000152// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000154int CodeGenerator::inlined_write_barrier_size_ = -1;
155
ager@chromium.org5c838252010-02-19 08:53:10 +0000156CodeGenerator::CodeGenerator(MacroAssembler* masm)
157 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000158 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000159 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000160 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000161 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 cc_reg_(al),
163 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000164 loop_nesting_(0),
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000165 type_info_(NULL),
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000166 function_return_(JumpTarget::BIDIRECTIONAL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000167 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168}
169
170
171// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000172// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175// cp: callee's context
176
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000177void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000178 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000180 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
182 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000183 info_ = info;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000184
185 int slots = scope()->num_parameters() + scope()->num_stack_slots();
186 ScopedVector<TypeInfo> type_info_array(slots);
187 type_info_ = &type_info_array;
188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000189 ASSERT(allocator_ == NULL);
190 RegisterAllocator register_allocator(this);
191 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000192 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000193 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000194 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000195
196 // Adjust for function-level loop nesting.
197 ASSERT_EQ(0, loop_nesting_);
198 loop_nesting_ = info->loop_nesting();
199
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000200 {
201 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000203 // Entry:
204 // Stack: receiver, arguments
205 // lr: return address
206 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000208 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000210 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212#ifdef DEBUG
213 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000214 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000215 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000216 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 }
218#endif
219
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000220 frame_->Enter();
221 // tos: code slot
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000222
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000223 // Allocate space for locals and initialize them. This also checks
224 // for stack overflow.
225 frame_->AllocateStackSlots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000227 frame_->AssertIsSpilled();
228 int heap_slots = scope()->num_heap_slots() - Context::MIN_CONTEXT_SLOTS;
229 if (heap_slots > 0) {
230 // Allocate local context.
231 // Get outer context and create a new context based on it.
232 __ ldr(r0, frame_->Function());
233 frame_->EmitPush(r0);
234 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
235 FastNewContextStub stub(heap_slots);
236 frame_->CallStub(&stub, 1);
237 } else {
238 frame_->CallRuntime(Runtime::kNewContext, 1);
239 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000240
241#ifdef DEBUG
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000242 JumpTarget verified_true;
243 __ cmp(r0, cp);
244 verified_true.Branch(eq);
245 __ stop("NewContext: r0 is expected to be the same as cp");
246 verified_true.Bind();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000247#endif
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000248 // Update context local.
249 __ str(cp, frame_->Context());
250 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000251
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000252 // TODO(1241774): Improve this code:
253 // 1) only needed if we have a context
254 // 2) no need to recompute context ptr every single time
255 // 3) don't copy parameter operand code from SlotOperand!
256 {
257 Comment cmnt2(masm_, "[ copy context parameters into .context");
258 // Note that iteration order is relevant here! If we have the same
259 // parameter twice (e.g., function (x, y, x)), and that parameter
260 // needs to be copied into the context, it must be the last argument
261 // passed to the parameter that needs to be copied. This is a rare
262 // case so we don't check for it, instead we rely on the copying
263 // order: such a parameter is copied repeatedly into the same
264 // context location and thus the last value is what is seen inside
265 // the function.
266 frame_->AssertIsSpilled();
267 for (int i = 0; i < scope()->num_parameters(); i++) {
268 Variable* par = scope()->parameter(i);
269 Slot* slot = par->slot();
270 if (slot != NULL && slot->type() == Slot::CONTEXT) {
271 ASSERT(!scope()->is_global_scope()); // No params in global scope.
272 __ ldr(r1, frame_->ParameterAt(i));
273 // Loads r2 with context; used below in RecordWrite.
274 __ str(r1, SlotOperand(slot, r2));
275 // Load the offset into r3.
276 int slot_offset =
277 FixedArray::kHeaderSize + slot->index() * kPointerSize;
278 __ RecordWrite(r2, Operand(slot_offset), r3, r1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000279 }
280 }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000281 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000282
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000283 // Store the arguments object. This must happen after context
284 // initialization because the arguments object may be stored in
285 // the context.
286 if (ArgumentsMode() != NO_ARGUMENTS_ALLOCATION) {
287 StoreArgumentsObject(true);
288 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000289
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000290 // Initialize ThisFunction reference if present.
291 if (scope()->is_function_scope() && scope()->function() != NULL) {
292 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
293 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000294 }
295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000296 // Initialize the function return target after the locals are set
297 // up, because it needs the expected frame height from the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000298 function_return_.SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000299 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000301 // Generate code to 'execute' declarations and initialize functions
302 // (source elements). In case of an illegal redeclaration we need to
303 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000304 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000306 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 } else {
308 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000309 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000310 // Bail out if a stack-overflow exception occurred when processing
311 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000312 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 }
314
mads.s.ager31e71382008-08-13 09:32:07 +0000315 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000317 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319
320 // Compile the body of the function in a vanilla state. Don't
321 // bother compiling all the code if the scope has an illegal
322 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000323 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 Comment cmnt(masm_, "[ function body");
325#ifdef DEBUG
326 bool is_builtin = Bootstrapper::IsActive();
327 bool should_trace =
328 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000329 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000330 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000331 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333#endif
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000334 VisitStatements(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 }
337
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000338 // Handle the return from the function.
339 if (has_valid_frame()) {
340 // If there is a valid frame, control flow can fall off the end of
341 // the body. In that case there is an implicit return statement.
342 ASSERT(!function_return_is_shadowed_);
343 frame_->PrepareForReturn();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000344 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000345 if (function_return_.is_bound()) {
346 function_return_.Jump();
347 } else {
348 function_return_.Bind();
349 GenerateReturnSequence();
350 }
351 } else if (function_return_.is_linked()) {
352 // If the return target has dangling jumps to it, then we have not
353 // yet generated the return sequence. This can happen when (a)
354 // control does not flow off the end of the body so we did not
355 // compile an artificial return statement just above, and (b) there
356 // are return statements in the body but (c) they are all shadowed.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000357 function_return_.Bind();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000358 GenerateReturnSequence();
mads.s.ager31e71382008-08-13 09:32:07 +0000359 }
360
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000361 // Adjust for function-level loop nesting.
362 ASSERT(loop_nesting_ == info->loop_nesting());
363 loop_nesting_ = 0;
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 ASSERT(!has_cc());
367 ASSERT(state_ == NULL);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000368 ASSERT(loop_nesting() == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000369 ASSERT(!function_return_is_shadowed_);
370 function_return_.Unuse();
371 DeleteFrame();
372
373 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000374 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 ProcessDeferred();
376 }
377
378 allocator_ = NULL;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000379 type_info_ = NULL;
380}
381
382
383int CodeGenerator::NumberOfSlot(Slot* slot) {
384 if (slot == NULL) return kInvalidSlotNumber;
385 switch (slot->type()) {
386 case Slot::PARAMETER:
387 return slot->index();
388 case Slot::LOCAL:
389 return slot->index() + scope()->num_parameters();
390 default:
391 break;
392 }
393 return kInvalidSlotNumber;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394}
395
396
ager@chromium.org7c537e22008-10-16 08:43:32 +0000397MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
398 // Currently, this assertion will fail if we try to assign to
399 // a constant variable that is constant because it is read-only
400 // (such as the variable referring to a named function expression).
401 // We need to implement assignments to read-only variables.
402 // Ideally, we should do this during AST generation (by converting
403 // such assignments into expression statements); however, in general
404 // we may not be able to make the decision until past AST generation,
405 // that is when the entire program is known.
406 ASSERT(slot != NULL);
407 int index = slot->index();
408 switch (slot->type()) {
409 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000410 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000411
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000412 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000413 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000414
415 case Slot::CONTEXT: {
416 // Follow the context chain if necessary.
417 ASSERT(!tmp.is(cp)); // do not overwrite context register
418 Register context = cp;
419 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000420 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000421 // Load the closure.
422 // (All contexts, even 'with' contexts, have a closure,
423 // and it is the same for all contexts inside a function.
424 // There is no need to go to the function context first.)
425 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
426 // Load the function context (which is the incoming, outer context).
427 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
428 context = tmp;
429 }
430 // We may have a 'with' context now. Get the function context.
431 // (In fact this mov may never be the needed, since the scope analysis
432 // may not permit a direct context access in this case and thus we are
433 // always at a function context. However it is safe to dereference be-
434 // cause the function context of a function context is itself. Before
435 // deleting this mov we should try to create a counter-example first,
436 // though...)
437 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
438 return ContextOperand(tmp, index);
439 }
440
441 default:
442 UNREACHABLE();
443 return MemOperand(r0, 0);
444 }
445}
446
447
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000448MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
449 Slot* slot,
450 Register tmp,
451 Register tmp2,
452 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000453 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000454 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000455
ager@chromium.org381abbb2009-02-25 13:23:22 +0000456 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
457 if (s->num_heap_slots() > 0) {
458 if (s->calls_eval()) {
459 // Check that extension is NULL.
460 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
461 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000462 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000463 }
464 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
465 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
466 context = tmp;
467 }
468 }
469 // Check that last extension is NULL.
470 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
471 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000472 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000473 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000475}
476
477
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000478// Loads a value on TOS. If it is a boolean value, the result may have been
479// (partially) translated into branches, or it may have set the condition
480// code register. If force_cc is set, the value is forced to set the
481// condition code register and no value is pushed. If the condition code
482// register was set, has_cc() is true and cc_reg_ contains the condition to
483// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000484void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 JumpTarget* true_target,
486 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000487 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000488 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000489 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000491 { ConditionCodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000492 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000493
494 // If we hit a stack overflow, we may not have actually visited
495 // the expression. In that case, we ensure that we have a
496 // valid-looking frame state because we will continue to generate
497 // code as we unwind the C++ stack.
498 //
499 // It's possible to have both a stack overflow and a valid frame
500 // state (eg, a subexpression overflowed, visiting it returned
501 // with a dummied frame state, and visiting this expression
502 // returned with a normal-looking state).
503 if (HasStackOverflow() &&
504 has_valid_frame() &&
505 !has_cc() &&
506 frame_->height() == original_height) {
507 true_target->Jump();
508 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000509 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000511 // Convert the TOS value to a boolean in the condition code register.
512 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 ASSERT(!force_cc || !has_valid_frame() || has_cc());
515 ASSERT(!has_valid_frame() ||
516 (has_cc() && frame_->height() == original_height) ||
517 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518}
519
520
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000521void CodeGenerator::Load(Expression* expr) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000522 // We generally assume that we are not in a spilled scope for most
523 // of the code generator. A failure to ensure this caused issue 815
524 // and this assert is designed to catch similar issues.
525 frame_->AssertIsNotSpilled();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526#ifdef DEBUG
527 int original_height = frame_->height();
528#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000529 JumpTarget true_target;
530 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000531 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000535 JumpTarget loaded;
536 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 materialize_true.Branch(cc_reg_);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000538 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000539 loaded.Jump();
540 materialize_true.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000541 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 cc_reg_ = al;
544 }
545
546 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 // We have at least one condition value that has been "translated"
548 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000549 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 if (frame_ != NULL) {
551 loaded.Jump(); // Don't lose the current TOS.
552 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 true_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000557 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000559 // If both "true" and "false" need to be loaded jump across the code for
560 // "false".
561 if (both) {
562 loaded.Jump();
563 }
564 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000566 false_target.Bind();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000567 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 // A value is loaded on all paths reaching this point.
570 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000572 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 ASSERT(!has_cc());
ager@chromium.orgac091b72010-05-05 07:34:42 +0000574 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575}
576
577
ager@chromium.org7c537e22008-10-16 08:43:32 +0000578void CodeGenerator::LoadGlobal() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000579 Register reg = frame_->GetTOSRegister();
580 __ ldr(reg, GlobalObject());
581 frame_->EmitPush(reg);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582}
583
584
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000585void CodeGenerator::LoadGlobalReceiver(Register scratch) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000586 Register reg = frame_->GetTOSRegister();
587 __ ldr(reg, ContextOperand(cp, Context::GLOBAL_INDEX));
588 __ ldr(reg,
589 FieldMemOperand(reg, GlobalObject::kGlobalReceiverOffset));
590 frame_->EmitPush(reg);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000591}
592
593
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000594ArgumentsAllocationMode CodeGenerator::ArgumentsMode() {
595 if (scope()->arguments() == NULL) return NO_ARGUMENTS_ALLOCATION;
596 ASSERT(scope()->arguments_shadow() != NULL);
597 // We don't want to do lazy arguments allocation for functions that
598 // have heap-allocated contexts, because it interfers with the
599 // uninitialized const tracking in the context objects.
600 return (scope()->num_heap_slots() > 0)
601 ? EAGER_ARGUMENTS_ALLOCATION
602 : LAZY_ARGUMENTS_ALLOCATION;
603}
604
605
606void CodeGenerator::StoreArgumentsObject(bool initial) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000607 ArgumentsAllocationMode mode = ArgumentsMode();
608 ASSERT(mode != NO_ARGUMENTS_ALLOCATION);
609
610 Comment cmnt(masm_, "[ store arguments object");
611 if (mode == LAZY_ARGUMENTS_ALLOCATION && initial) {
612 // When using lazy arguments allocation, we store the hole value
613 // as a sentinel indicating that the arguments object hasn't been
614 // allocated yet.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000615 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000616 } else {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000617 frame_->SpillAll();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000618 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
619 __ ldr(r2, frame_->Function());
620 // The receiver is below the arguments, the return address, and the
621 // frame pointer on the stack.
622 const int kReceiverDisplacement = 2 + scope()->num_parameters();
623 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
624 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
625 frame_->Adjust(3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000626 __ Push(r2, r1, r0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000627 frame_->CallStub(&stub, 3);
628 frame_->EmitPush(r0);
629 }
630
631 Variable* arguments = scope()->arguments()->var();
632 Variable* shadow = scope()->arguments_shadow()->var();
633 ASSERT(arguments != NULL && arguments->slot() != NULL);
634 ASSERT(shadow != NULL && shadow->slot() != NULL);
635 JumpTarget done;
636 if (mode == LAZY_ARGUMENTS_ALLOCATION && !initial) {
637 // We have to skip storing into the arguments slot if it has
638 // already been written to. This can happen if the a function
639 // has a local variable named 'arguments'.
640 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000641 Register arguments = frame_->PopToRegister();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000642 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000643 __ cmp(arguments, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000644 done.Branch(ne);
645 }
646 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
647 if (mode == LAZY_ARGUMENTS_ALLOCATION) done.Bind();
648 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
649}
650
651
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000652void CodeGenerator::LoadTypeofExpression(Expression* expr) {
653 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000654 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000656 // For a global variable we build the property reference
657 // <global>.<variable> and perform a (regular non-contextual) property
658 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
660 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000661 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000662 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000663 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000664 } else if (variable != NULL && variable->slot() != NULL) {
665 // For a variable that rewrites to a slot, we signal it is the immediate
666 // subexpression of a typeof.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +0000667 LoadFromSlotCheckForArguments(variable->slot(), INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000669 // Anything else can be handled normally.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000670 Load(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 }
672}
673
674
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000675Reference::Reference(CodeGenerator* cgen,
676 Expression* expression,
677 bool persist_after_get)
678 : cgen_(cgen),
679 expression_(expression),
680 type_(ILLEGAL),
681 persist_after_get_(persist_after_get) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000682 // We generally assume that we are not in a spilled scope for most
683 // of the code generator. A failure to ensure this caused issue 815
684 // and this assert is designed to catch similar issues.
685 cgen->frame()->AssertIsNotSpilled();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 cgen->LoadReference(this);
687}
688
689
690Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000691 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692}
693
694
ager@chromium.org7c537e22008-10-16 08:43:32 +0000695void CodeGenerator::LoadReference(Reference* ref) {
696 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 Expression* e = ref->expression();
698 Property* property = e->AsProperty();
699 Variable* var = e->AsVariableProxy()->AsVariable();
700
701 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000702 // The expression is either a property or a variable proxy that rewrites
703 // to a property.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000704 Load(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000705 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 ref->set_type(Reference::NAMED);
707 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000708 Load(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 ref->set_type(Reference::KEYED);
710 }
711 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000712 // The expression is a variable proxy that does not rewrite to a
713 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000714 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 LoadGlobal();
716 ref->set_type(Reference::NAMED);
717 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000718 ASSERT(var->slot() != NULL);
719 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 }
721 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000722 // Anything else is a runtime error.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000723 Load(e);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000724 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 }
726}
727
728
ager@chromium.org7c537e22008-10-16 08:43:32 +0000729void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000731 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000732 if (size == 0) return;
733
734 // Pop a reference from the stack while preserving TOS.
735 VirtualFrame::RegisterAllocationScope scope(this);
736 Comment cmnt(masm_, "[ UnloadReference");
737 if (size > 0) {
738 Register tos = frame_->PopToRegister();
739 frame_->Drop(size);
740 frame_->EmitPush(tos);
741 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742}
743
744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
746// register to a boolean in the condition code register. The code
747// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748void CodeGenerator::ToBoolean(JumpTarget* true_target,
749 JumpTarget* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000750 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 // Only the condition code should be set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000752 bool known_smi = frame_->KnownSmiAt(0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000753 Register tos = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
755 // Fast case checks
756
mads.s.ager31e71382008-08-13 09:32:07 +0000757 // Check if the value is 'false'.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000758 if (!known_smi) {
759 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
760 __ cmp(tos, ip);
761 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000763 // Check if the value is 'true'.
764 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
765 __ cmp(tos, ip);
766 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000768 // Check if the value is 'undefined'.
769 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
770 __ cmp(tos, ip);
771 false_target->Branch(eq);
772 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
mads.s.ager31e71382008-08-13 09:32:07 +0000774 // Check if the value is a smi.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000775 __ cmp(tos, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000777 if (!known_smi) {
778 false_target->Branch(eq);
779 __ tst(tos, Operand(kSmiTagMask));
780 true_target->Branch(eq);
781
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000782 // Slow case.
783 if (CpuFeatures::IsSupported(VFP3)) {
784 CpuFeatures::Scope scope(VFP3);
785 // Implements the slow case by using ToBooleanStub.
786 // The ToBooleanStub takes a single argument, and
787 // returns a non-zero value for true, or zero for false.
788 // Both the argument value and the return value use the
789 // register assigned to tos_
790 ToBooleanStub stub(tos);
791 frame_->CallStub(&stub, 0);
792 // Convert the result in "tos" to a condition code.
793 __ cmp(tos, Operand(0));
794 } else {
795 // Implements slow case by calling the runtime.
796 frame_->EmitPush(tos);
797 frame_->CallRuntime(Runtime::kToBool, 1);
798 // Convert the result (r0) to a condition code.
799 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
800 __ cmp(r0, ip);
801 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000802 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803
804 cc_reg_ = ne;
805}
806
807
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000808void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000809 OverwriteMode overwrite_mode,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000810 GenerateInlineSmi inline_smi,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000811 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000812 // top of virtual frame: y
813 // 2nd elt. on virtual frame : x
814 // result : top of virtual frame
815
816 // Stub is entered with a call: 'return address' is in lr.
817 switch (op) {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000818 case Token::ADD:
819 case Token::SUB:
820 if (inline_smi) {
821 JumpTarget done;
822 Register rhs = frame_->PopToRegister();
823 Register lhs = frame_->PopToRegister(rhs);
824 Register scratch = VirtualFrame::scratch0();
825 __ orr(scratch, rhs, Operand(lhs));
826 // Check they are both small and positive.
827 __ tst(scratch, Operand(kSmiTagMask | 0xc0000000));
828 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000829 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000830 if (op == Token::ADD) {
831 __ add(r0, lhs, Operand(rhs), LeaveCC, eq);
832 } else {
833 __ sub(r0, lhs, Operand(rhs), LeaveCC, eq);
834 }
835 done.Branch(eq);
836 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
837 frame_->SpillAll();
838 frame_->CallStub(&stub, 0);
839 done.Bind();
840 frame_->EmitPush(r0);
841 break;
842 } else {
843 // Fall through!
844 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000845 case Token::BIT_OR:
846 case Token::BIT_AND:
847 case Token::BIT_XOR:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000848 if (inline_smi) {
849 bool rhs_is_smi = frame_->KnownSmiAt(0);
850 bool lhs_is_smi = frame_->KnownSmiAt(1);
851 Register rhs = frame_->PopToRegister();
852 Register lhs = frame_->PopToRegister(rhs);
853 Register smi_test_reg;
854 Condition cond;
855 if (!rhs_is_smi || !lhs_is_smi) {
856 if (rhs_is_smi) {
857 smi_test_reg = lhs;
858 } else if (lhs_is_smi) {
859 smi_test_reg = rhs;
860 } else {
861 smi_test_reg = VirtualFrame::scratch0();
862 __ orr(smi_test_reg, rhs, Operand(lhs));
863 }
864 // Check they are both Smis.
865 __ tst(smi_test_reg, Operand(kSmiTagMask));
866 cond = eq;
867 } else {
868 cond = al;
869 }
870 ASSERT(rhs.is(r0) || lhs.is(r0)); // r0 is free now.
871 if (op == Token::BIT_OR) {
872 __ orr(r0, lhs, Operand(rhs), LeaveCC, cond);
873 } else if (op == Token::BIT_AND) {
874 __ and_(r0, lhs, Operand(rhs), LeaveCC, cond);
875 } else {
876 ASSERT(op == Token::BIT_XOR);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000877 STATIC_ASSERT(kSmiTag == 0);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000878 __ eor(r0, lhs, Operand(rhs), LeaveCC, cond);
879 }
880 if (cond != al) {
881 JumpTarget done;
882 done.Branch(cond);
883 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
884 frame_->SpillAll();
885 frame_->CallStub(&stub, 0);
886 done.Bind();
887 }
888 frame_->EmitPush(r0);
889 break;
890 } else {
891 // Fall through!
892 }
893 case Token::MUL:
894 case Token::DIV:
895 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896 case Token::SHL:
897 case Token::SHR:
898 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000899 Register rhs = frame_->PopToRegister();
900 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000901 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
902 frame_->SpillAll();
903 frame_->CallStub(&stub, 0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000904 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 break;
906 }
907
ager@chromium.org357bf652010-04-12 11:30:10 +0000908 case Token::COMMA: {
909 Register scratch = frame_->PopToRegister();
910 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000911 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000912 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
916 default:
917 // Other cases should have been handled before this point.
918 UNREACHABLE();
919 break;
920 }
921}
922
923
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000924class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000925 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000926 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000927 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000928 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000929 OverwriteMode overwrite_mode,
930 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000931 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000932 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000933 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000934 overwrite_mode_(overwrite_mode),
935 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000936 set_comment("[ DeferredInlinedSmiOperation");
937 }
938
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000939 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000940
941 private:
942 Token::Value op_;
943 int value_;
944 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000945 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000946 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000947};
948
949
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000950
951// On entry the non-constant side of the binary operation is in tos_register_
952// and the constant smi side is nowhere. The tos_register_ is not used by the
953// virtual frame. On exit the answer is in the tos_register_ and the virtual
954// frame is unchanged.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000955void DeferredInlineSmiOperation::Generate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000956 VirtualFrame copied_frame(*frame_state()->frame());
957 copied_frame.SpillAll();
958
ager@chromium.org357bf652010-04-12 11:30:10 +0000959 Register lhs = r1;
960 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000961 switch (op_) {
962 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000963 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000964 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000965 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000966 __ mov(r1, Operand(Smi::FromInt(value_)));
967 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000968 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000969 __ mov(r0, Operand(Smi::FromInt(value_)));
970 }
971 break;
972 }
973
974 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000975 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000976 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000977 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000978 __ mov(r1, Operand(Smi::FromInt(value_)));
979 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000980 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000981 __ mov(r0, Operand(Smi::FromInt(value_)));
982 }
983 break;
984 }
985
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000986 // For these operations there is no optimistic operation that needs to be
987 // reverted.
988 case Token::MUL:
989 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000990 case Token::BIT_OR:
991 case Token::BIT_XOR:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000992 case Token::BIT_AND:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000993 case Token::SHL:
994 case Token::SHR:
995 case Token::SAR: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000996 if (tos_register_.is(r1)) {
997 __ mov(r0, Operand(Smi::FromInt(value_)));
998 } else {
999 ASSERT(tos_register_.is(r0));
1000 __ mov(r1, Operand(Smi::FromInt(value_)));
1001 }
1002 if (reversed_ == tos_register_.is(r1)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001003 lhs = r0;
1004 rhs = r1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001005 }
1006 break;
1007 }
1008
1009 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +00001010 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001011 UNREACHABLE();
1012 break;
1013 }
1014
ager@chromium.org357bf652010-04-12 11:30:10 +00001015 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001016 __ CallStub(&stub);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001017
ager@chromium.org357bf652010-04-12 11:30:10 +00001018 // The generic stub returns its value in r0, but that's not
1019 // necessarily what we want. We want whatever the inlined code
1020 // expected, which is that the answer is in the same register as
1021 // the operand was.
1022 __ Move(tos_register_, r0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001023
1024 // The tos register was not in use for the virtual frame that we
1025 // came into this function with, so we can merge back to that frame
1026 // without trashing it.
1027 copied_frame.MergeTo(frame_state()->frame());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028}
1029
1030
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001031static bool PopCountLessThanEqual2(unsigned int x) {
1032 x &= x - 1;
1033 return (x & (x - 1)) == 0;
1034}
1035
1036
1037// Returns the index of the lowest bit set.
1038static int BitPosition(unsigned x) {
1039 int bit_posn = 0;
1040 while ((x & 0xf) == 0) {
1041 bit_posn += 4;
1042 x >>= 4;
1043 }
1044 while ((x & 1) == 0) {
1045 bit_posn++;
1046 x >>= 1;
1047 }
1048 return bit_posn;
1049}
1050
1051
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00001052void CodeGenerator::SmiOperation(Token::Value op,
1053 Handle<Object> value,
1054 bool reversed,
1055 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001056 int int_value = Smi::cast(*value)->value();
1057
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001058 bool both_sides_are_smi = frame_->KnownSmiAt(0);
1059
ager@chromium.org357bf652010-04-12 11:30:10 +00001060 bool something_to_inline;
1061 switch (op) {
1062 case Token::ADD:
1063 case Token::SUB:
1064 case Token::BIT_AND:
1065 case Token::BIT_OR:
1066 case Token::BIT_XOR: {
1067 something_to_inline = true;
1068 break;
1069 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001070 case Token::SHL: {
1071 something_to_inline = (both_sides_are_smi || !reversed);
1072 break;
1073 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001074 case Token::SHR:
1075 case Token::SAR: {
1076 if (reversed) {
1077 something_to_inline = false;
1078 } else {
1079 something_to_inline = true;
1080 }
1081 break;
1082 }
1083 case Token::MOD: {
1084 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1085 something_to_inline = false;
1086 } else {
1087 something_to_inline = true;
1088 }
1089 break;
1090 }
1091 case Token::MUL: {
1092 if (!IsEasyToMultiplyBy(int_value)) {
1093 something_to_inline = false;
1094 } else {
1095 something_to_inline = true;
1096 }
1097 break;
1098 }
1099 default: {
1100 something_to_inline = false;
1101 break;
1102 }
1103 }
1104
1105 if (!something_to_inline) {
1106 if (!reversed) {
1107 // Push the rhs onto the virtual frame by putting it in a TOS register.
1108 Register rhs = frame_->GetTOSRegister();
1109 __ mov(rhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001110 frame_->EmitPush(rhs, TypeInfo::Smi());
1111 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, int_value);
ager@chromium.org357bf652010-04-12 11:30:10 +00001112 } else {
1113 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1114 // at most one pop, the rest takes place in TOS registers.
1115 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1116 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1117 __ mov(lhs, Operand(value));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001118 frame_->EmitPush(lhs, TypeInfo::Smi());
1119 TypeInfo t = both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Unknown();
1120 frame_->EmitPush(rhs, t);
1121 GenericBinaryOperation(op, mode, GENERATE_INLINE_SMI, kUnknownIntValue);
ager@chromium.org357bf652010-04-12 11:30:10 +00001122 }
1123 return;
1124 }
1125
1126 // We move the top of stack to a register (normally no move is invoved).
1127 Register tos = frame_->PopToRegister();
ager@chromium.org357bf652010-04-12 11:30:10 +00001128 switch (op) {
1129 case Token::ADD: {
1130 DeferredCode* deferred =
1131 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1132
1133 __ add(tos, tos, Operand(value), SetCC);
1134 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001135 if (!both_sides_are_smi) {
1136 __ tst(tos, Operand(kSmiTagMask));
1137 deferred->Branch(ne);
1138 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001139 deferred->BindExit();
1140 frame_->EmitPush(tos);
1141 break;
1142 }
1143
1144 case Token::SUB: {
1145 DeferredCode* deferred =
1146 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1147
1148 if (reversed) {
1149 __ rsb(tos, tos, Operand(value), SetCC);
1150 } else {
1151 __ sub(tos, tos, Operand(value), SetCC);
1152 }
1153 deferred->Branch(vs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001154 if (!both_sides_are_smi) {
1155 __ tst(tos, Operand(kSmiTagMask));
1156 deferred->Branch(ne);
1157 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001158 deferred->BindExit();
1159 frame_->EmitPush(tos);
1160 break;
1161 }
1162
1163
1164 case Token::BIT_OR:
1165 case Token::BIT_XOR:
1166 case Token::BIT_AND: {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001167 if (both_sides_are_smi) {
1168 switch (op) {
1169 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1170 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001171 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001172 default: UNREACHABLE();
1173 }
1174 frame_->EmitPush(tos, TypeInfo::Smi());
1175 } else {
1176 DeferredCode* deferred =
1177 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1178 __ tst(tos, Operand(kSmiTagMask));
1179 deferred->Branch(ne);
1180 switch (op) {
1181 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1182 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001183 case Token::BIT_AND: __ And(tos, tos, Operand(value)); break;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001184 default: UNREACHABLE();
1185 }
1186 deferred->BindExit();
1187 TypeInfo result_type =
1188 (op == Token::BIT_AND) ? TypeInfo::Smi() : TypeInfo::Integer32();
1189 frame_->EmitPush(tos, result_type);
ager@chromium.org357bf652010-04-12 11:30:10 +00001190 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001191 break;
1192 }
1193
1194 case Token::SHL:
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001195 if (reversed) {
1196 ASSERT(both_sides_are_smi);
1197 int max_shift = 0;
1198 int max_result = int_value == 0 ? 1 : int_value;
1199 while (Smi::IsValid(max_result << 1)) {
1200 max_shift++;
1201 max_result <<= 1;
1202 }
1203 DeferredCode* deferred =
1204 new DeferredInlineSmiOperation(op, int_value, true, mode, tos);
1205 // Mask off the last 5 bits of the shift operand (rhs). This is part
1206 // of the definition of shift in JS and we know we have a Smi so we
1207 // can safely do this. The masked version gets passed to the
1208 // deferred code, but that makes no difference.
1209 __ and_(tos, tos, Operand(Smi::FromInt(0x1f)));
1210 __ cmp(tos, Operand(Smi::FromInt(max_shift)));
1211 deferred->Branch(ge);
1212 Register scratch = VirtualFrame::scratch0();
1213 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Untag.
1214 __ mov(tos, Operand(Smi::FromInt(int_value))); // Load constant.
1215 __ mov(tos, Operand(tos, LSL, scratch)); // Shift constant.
1216 deferred->BindExit();
1217 TypeInfo result = TypeInfo::Integer32();
1218 frame_->EmitPush(tos, result);
1219 break;
1220 }
1221 // Fall through!
ager@chromium.org357bf652010-04-12 11:30:10 +00001222 case Token::SHR:
1223 case Token::SAR: {
1224 ASSERT(!reversed);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001225 int shift_amount = int_value & 0x1f;
1226 TypeInfo result = TypeInfo::Number();
1227
1228 if (op == Token::SHR) {
1229 if (shift_amount > 1) {
1230 result = TypeInfo::Smi();
1231 } else if (shift_amount > 0) {
1232 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001233 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001234 } else if (op == Token::SAR) {
1235 if (shift_amount > 0) {
1236 result = TypeInfo::Smi();
1237 } else {
1238 result = TypeInfo::Integer32();
1239 }
1240 } else {
1241 ASSERT(op == Token::SHL);
1242 result = TypeInfo::Integer32();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001243 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001244
ager@chromium.org357bf652010-04-12 11:30:10 +00001245 Register scratch = VirtualFrame::scratch0();
1246 Register scratch2 = VirtualFrame::scratch1();
1247 int shift_value = int_value & 0x1f; // least significant 5 bits
1248 DeferredCode* deferred =
1249 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001250 uint32_t problematic_mask = kSmiTagMask;
1251 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001252 bool skip_smi_test = both_sides_are_smi;
1253 if (shift_value == 0 && op == Token::SHR) {
1254 problematic_mask |= 0x80000000;
1255 skip_smi_test = false;
1256 }
1257 if (!skip_smi_test) {
1258 __ tst(tos, Operand(problematic_mask));
1259 deferred->Branch(ne); // Go slow for problematic input.
1260 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001261 switch (op) {
1262 case Token::SHL: {
1263 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001264 int adjusted_shift = shift_value - kSmiTagSize;
1265 ASSERT(adjusted_shift >= 0);
1266 if (adjusted_shift != 0) {
1267 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1268 // Check that the *signed* result fits in a smi.
1269 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1270 deferred->Branch(mi);
1271 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1272 } else {
1273 // Check that the *signed* result fits in a smi.
1274 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1275 deferred->Branch(mi);
1276 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1277 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001278 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001279 break;
1280 }
1281 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001282 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001283 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1284 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001285 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001286 if (shift_value == 1) {
1287 // check that the *unsigned* result fits in a smi
1288 // neither of the two high-order bits can be set:
1289 // - 0x80000000: high bit would be lost when smi tagging
1290 // - 0x40000000: this number would convert to negative when
1291 // smi tagging these two cases can only happen with shifts
1292 // by 0 or 1 when handed a valid smi
1293 __ tst(scratch, Operand(0xc0000000));
1294 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001295 } else {
1296 ASSERT(shift_value >= 2);
1297 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001298 }
1299 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001300 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001301 break;
1302 }
1303 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001304 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1305 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001306 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001307 // Do the shift and the tag removal in one operation. If the shift
1308 // is 31 bits (the highest possible value) then we emit the
1309 // instruction as a shift by 0 which means shift arithmetically by
1310 // 32.
1311 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1312 // Put tag back.
1313 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001314 // SAR by at least 1 gives a Smi.
1315 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001316 }
1317 break;
1318 }
1319 default: UNREACHABLE();
1320 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001321 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001322 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001323 break;
1324 }
1325
1326 case Token::MOD: {
1327 ASSERT(!reversed);
1328 ASSERT(int_value >= 2);
1329 ASSERT(IsPowerOf2(int_value));
1330 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001331 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001332 unsigned mask = (0x80000000u | kSmiTagMask);
1333 __ tst(tos, Operand(mask));
1334 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1335 mask = (int_value << kSmiTagSize) - 1;
1336 __ and_(tos, tos, Operand(mask));
1337 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001338 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1339 frame_->EmitPush(
1340 tos,
1341 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001342 break;
1343 }
1344
1345 case Token::MUL: {
1346 ASSERT(IsEasyToMultiplyBy(int_value));
1347 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001348 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001349 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1350 max_smi_that_wont_overflow <<= kSmiTagSize;
1351 unsigned mask = 0x80000000u;
1352 while ((mask & max_smi_that_wont_overflow) == 0) {
1353 mask |= mask >> 1;
1354 }
1355 mask |= kSmiTagMask;
1356 // This does a single mask that checks for a too high value in a
1357 // conservative way and for a non-Smi. It also filters out negative
1358 // numbers, unfortunately, but since this code is inline we prefer
1359 // brevity to comprehensiveness.
1360 __ tst(tos, Operand(mask));
1361 deferred->Branch(ne);
1362 MultiplyByKnownInt(masm_, tos, tos, int_value);
1363 deferred->BindExit();
1364 frame_->EmitPush(tos);
1365 break;
1366 }
1367
1368 default:
1369 UNREACHABLE();
1370 break;
1371 }
1372}
1373
1374
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001375void CodeGenerator::Comparison(Condition cc,
1376 Expression* left,
1377 Expression* right,
1378 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001379 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001380
ager@chromium.org357bf652010-04-12 11:30:10 +00001381 if (left != NULL) Load(left);
1382 if (right != NULL) Load(right);
1383
mads.s.ager31e71382008-08-13 09:32:07 +00001384 // sp[0] : y
1385 // sp[1] : x
1386 // result : cc register
1387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 // Strict only makes sense for equality comparisons.
1389 ASSERT(!strict || cc == eq);
1390
ager@chromium.org357bf652010-04-12 11:30:10 +00001391 Register lhs;
1392 Register rhs;
1393
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001394 bool lhs_is_smi;
1395 bool rhs_is_smi;
1396
ager@chromium.org357bf652010-04-12 11:30:10 +00001397 // We load the top two stack positions into registers chosen by the virtual
1398 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001399 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1400 if (cc == gt || cc == le) {
1401 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001402 lhs_is_smi = frame_->KnownSmiAt(0);
1403 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001404 lhs = frame_->PopToRegister();
1405 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001406 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001407 rhs_is_smi = frame_->KnownSmiAt(0);
1408 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001409 rhs = frame_->PopToRegister();
1410 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001411 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001412
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001413 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1414
ager@chromium.org357bf652010-04-12 11:30:10 +00001415 ASSERT(rhs.is(r0) || rhs.is(r1));
1416 ASSERT(lhs.is(r0) || lhs.is(r1));
1417
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001418 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001420 if (!both_sides_are_smi) {
1421 // Now we have the two sides in r0 and r1. We flush any other registers
1422 // because the stub doesn't know about register allocation.
1423 frame_->SpillAll();
1424 Register scratch = VirtualFrame::scratch0();
1425 Register smi_test_reg;
1426 if (lhs_is_smi) {
1427 smi_test_reg = rhs;
1428 } else if (rhs_is_smi) {
1429 smi_test_reg = lhs;
1430 } else {
1431 __ orr(scratch, lhs, Operand(rhs));
1432 smi_test_reg = scratch;
1433 }
1434 __ tst(smi_test_reg, Operand(kSmiTagMask));
1435 JumpTarget smi;
1436 smi.Branch(eq);
1437
1438 // Perform non-smi comparison by stub.
1439 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1440 // We call with 0 args because there are 0 on the stack.
ager@chromium.orgb5737492010-07-15 09:29:43 +00001441 CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001442 frame_->CallStub(&stub, 0);
1443 __ cmp(r0, Operand(0));
1444 exit.Jump();
1445
1446 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001447 }
1448
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001449 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001450 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 cc_reg_ = cc;
1454}
1455
1456
mads.s.ager31e71382008-08-13 09:32:07 +00001457// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001458void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001459 CallFunctionFlags flags,
1460 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001462 int arg_count = args->length();
1463 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001464 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001465 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466
kasper.lund7276f142008-07-30 08:49:36 +00001467 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001468 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
kasper.lund7276f142008-07-30 08:49:36 +00001470 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001471 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001473 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474
1475 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001476 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001477 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478}
1479
1480
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001481void CodeGenerator::CallApplyLazy(Expression* applicand,
1482 Expression* receiver,
1483 VariableProxy* arguments,
1484 int position) {
1485 // An optimized implementation of expressions of the form
1486 // x.apply(y, arguments).
1487 // If the arguments object of the scope has not been allocated,
1488 // and x.apply is Function.prototype.apply, this optimization
1489 // just copies y and the arguments of the current function on the
1490 // stack, as receiver and arguments, and calls x.
1491 // In the implementation comments, we call x the applicand
1492 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001493
1494 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1495 ASSERT(arguments->IsArguments());
1496
1497 // Load applicand.apply onto the stack. This will usually
1498 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001499 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001500 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001501 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001502 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001503 frame_->EmitPush(r0);
1504
1505 // Load the receiver and the existing arguments object onto the
1506 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001507 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001508 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1509
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001510 // At this point the top two stack elements are probably in registers
1511 // since they were just loaded. Ensure they are in regs and get the
1512 // regs.
1513 Register receiver_reg = frame_->Peek2();
1514 Register arguments_reg = frame_->Peek();
1515
1516 // From now on the frame is spilled.
1517 frame_->SpillAll();
1518
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001519 // Emit the source position information after having loaded the
1520 // receiver and the arguments.
1521 CodeForSourcePosition(position);
1522 // Contents of the stack at this point:
1523 // sp[0]: arguments object of the current function or the hole.
1524 // sp[1]: receiver
1525 // sp[2]: applicand.apply
1526 // sp[3]: applicand.
1527
1528 // Check if the arguments object has been lazily allocated
1529 // already. If so, just use that instead of copying the arguments
1530 // from the stack. This also deals with cases where a local variable
1531 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001532 JumpTarget slow;
1533 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001534 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001535 __ cmp(ip, arguments_reg);
1536 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001537
1538 Label build_args;
1539 // Get rid of the arguments object probe.
1540 frame_->Drop();
1541 // Stack now has 3 elements on it.
1542 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001543 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001544 // sp[1]: applicand.apply
1545 // sp[2]: applicand.
1546
1547 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001548 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001549 // We allow all JSObjects including JSFunctions. As long as
1550 // JS_FUNCTION_TYPE is the last instance type and it is right
1551 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1552 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001553 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1554 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001555 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001556 __ b(lt, &build_args);
1557
1558 // Check that applicand.apply is Function.prototype.apply.
1559 __ ldr(r0, MemOperand(sp, kPointerSize));
1560 __ BranchOnSmi(r0, &build_args);
1561 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1562 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001563 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001564 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
1565 __ sub(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001566 __ cmp(r1, Operand(apply_code));
1567 __ b(ne, &build_args);
1568
1569 // Check that applicand is a function.
1570 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1571 __ BranchOnSmi(r1, &build_args);
1572 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1573 __ b(ne, &build_args);
1574
1575 // Copy the arguments to this function possibly from the
1576 // adaptor frame below it.
1577 Label invoke, adapted;
1578 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1579 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1580 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1581 __ b(eq, &adapted);
1582
1583 // No arguments adaptor frame. Copy fixed number of arguments.
1584 __ mov(r0, Operand(scope()->num_parameters()));
1585 for (int i = 0; i < scope()->num_parameters(); i++) {
1586 __ ldr(r2, frame_->ParameterAt(i));
1587 __ push(r2);
1588 }
1589 __ jmp(&invoke);
1590
1591 // Arguments adaptor frame present. Copy arguments from there, but
1592 // avoid copying too many arguments to avoid stack overflows.
1593 __ bind(&adapted);
1594 static const uint32_t kArgumentsLimit = 1 * KB;
1595 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1596 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1597 __ mov(r3, r0);
1598 __ cmp(r0, Operand(kArgumentsLimit));
1599 __ b(gt, &build_args);
1600
1601 // Loop through the arguments pushing them onto the execution
1602 // stack. We don't inform the virtual frame of the push, so we don't
1603 // have to worry about getting rid of the elements from the virtual
1604 // frame.
1605 Label loop;
1606 // r3 is a small non-negative integer, due to the test above.
1607 __ cmp(r3, Operand(0));
1608 __ b(eq, &invoke);
1609 // Compute the address of the first argument.
1610 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1611 __ add(r2, r2, Operand(kPointerSize));
1612 __ bind(&loop);
1613 // Post-decrement argument address by kPointerSize on each iteration.
1614 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1615 __ push(r4);
1616 __ sub(r3, r3, Operand(1), SetCC);
1617 __ b(gt, &loop);
1618
1619 // Invoke the function.
1620 __ bind(&invoke);
1621 ParameterCount actual(r0);
1622 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1623 // Drop applicand.apply and applicand from the stack, and push
1624 // the result of the function call, but leave the spilled frame
1625 // unchanged, with 3 elements, so it is correct when we compile the
1626 // slow-case code.
1627 __ add(sp, sp, Operand(2 * kPointerSize));
1628 __ push(r0);
1629 // Stack now has 1 element:
1630 // sp[0]: result
1631 __ jmp(&done);
1632
1633 // Slow-case: Allocate the arguments object since we know it isn't
1634 // there, and fall-through to the slow-case where we call
1635 // applicand.apply.
1636 __ bind(&build_args);
1637 // Stack now has 3 elements, because we have jumped from where:
1638 // sp[0]: receiver
1639 // sp[1]: applicand.apply
1640 // sp[2]: applicand.
1641 StoreArgumentsObject(false);
1642
1643 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001644 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001645
1646 // Generic computation of x.apply(y, args) with no special optimization.
1647 // Flip applicand.apply and applicand on the stack, so
1648 // applicand looks like the receiver of the applicand.apply call.
1649 // Then process it as a normal function call.
1650 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1651 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001652 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001653
1654 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1655 frame_->CallStub(&call_function, 3);
1656 // The function and its two arguments have been dropped.
1657 frame_->Drop(); // Drop the receiver as well.
1658 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001659 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001660 // Stack now has 1 element:
1661 // sp[0]: result
1662 __ bind(&done);
1663
1664 // Restore the context register after a call.
1665 __ ldr(cp, frame_->Context());
1666}
1667
1668
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001669void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 ASSERT(has_cc());
1671 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001672 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 cc_reg_ = al;
1674}
1675
1676
ager@chromium.org7c537e22008-10-16 08:43:32 +00001677void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001678 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001679 Comment cmnt(masm_, "[ check stack");
1680 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1681 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1682 // the implicit 8 byte offset that always applies to operations with pc and
1683 // gives a return address 12 bytes down.
1684 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1685 masm_->cmp(sp, Operand(ip));
1686 StackCheckStub stub;
1687 // Call the stub if lower.
1688 masm_->mov(pc,
1689 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1690 RelocInfo::CODE_TARGET),
1691 LeaveCC,
1692 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693}
1694
1695
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001696void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1697#ifdef DEBUG
1698 int original_height = frame_->height();
1699#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001700 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001701 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001702 }
1703 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1704}
1705
1706
ager@chromium.org7c537e22008-10-16 08:43:32 +00001707void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001708#ifdef DEBUG
1709 int original_height = frame_->height();
1710#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001712 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001713 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001714 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001715 if (node->break_target()->is_linked()) {
1716 node->break_target()->Bind();
1717 }
1718 node->break_target()->Unuse();
1719 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720}
1721
1722
ager@chromium.org7c537e22008-10-16 08:43:32 +00001723void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001724 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001725 frame_->EmitPush(Operand(pairs));
1726 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1727
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001728 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001729 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730}
1731
1732
ager@chromium.org7c537e22008-10-16 08:43:32 +00001733void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001734#ifdef DEBUG
1735 int original_height = frame_->height();
1736#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 Comment cmnt(masm_, "[ Declaration");
1738 Variable* var = node->proxy()->var();
1739 ASSERT(var != NULL); // must have been resolved
1740 Slot* slot = var->slot();
1741
1742 // If it was not possible to allocate the variable at compile time,
1743 // we need to "declare" it at runtime to make sure it actually
1744 // exists in the local context.
1745 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1746 // Variables with a "LOOKUP" slot were introduced as non-locals
1747 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001748 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001750 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001751 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 // Declaration nodes are always declared in only two modes.
1753 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1754 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001755 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 // Push initial value, if any.
1757 // Note: For variables we must not push an initial value (such as
1758 // 'undefined') because we may have a (legal) redeclaration and we
1759 // must not destroy the current value.
1760 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001761 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001763 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001765 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001767
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001769 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001770
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001771 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 return;
1773 }
1774
1775 ASSERT(!var->is_global());
1776
1777 // If we have a function or a constant, we need to initialize the variable.
1778 Expression* val = NULL;
1779 if (node->mode() == Variable::CONST) {
1780 val = new Literal(Factory::the_hole_value());
1781 } else {
1782 val = node->fun(); // NULL if we don't have a function
1783 }
1784
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001787 WriteBarrierCharacter wb_info =
1788 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1789 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001790 // Set initial value.
1791 Reference target(this, node->proxy());
1792 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001793 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001794
iposva@chromium.org245aa852009-02-10 00:49:54 +00001795 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001796 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001798 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::VisitExpressionStatement(ExpressionStatement* 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_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 Expression* expression = node->expression();
1809 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001810 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001811 frame_->Drop();
1812 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813}
1814
1815
ager@chromium.org7c537e22008-10-16 08:43:32 +00001816void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817#ifdef DEBUG
1818 int original_height = frame_->height();
1819#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824}
1825
1826
ager@chromium.org7c537e22008-10-16 08:43:32 +00001827void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828#ifdef DEBUG
1829 int original_height = frame_->height();
1830#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 // Generate different code depending on which parts of the if statement
1833 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 bool has_then_stm = node->HasThenStatement();
1835 bool has_else_stm = node->HasElseStatement();
1836
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001837 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001839 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001841 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001842 JumpTarget then;
1843 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001845 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 if (frame_ != NULL) {
1847 Branch(false, &else_);
1848 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001850 if (frame_ != NULL || then.is_linked()) {
1851 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001852 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001853 }
1854 if (frame_ != NULL) {
1855 exit.Jump();
1856 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001858 if (else_.is_linked()) {
1859 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001860 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
1863 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001864 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001866 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001868 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 if (frame_ != NULL) {
1870 Branch(false, &exit);
1871 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001873 if (frame_ != NULL || then.is_linked()) {
1874 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001875 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877
1878 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001879 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001881 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001883 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001884 if (frame_ != NULL) {
1885 Branch(true, &exit);
1886 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 if (frame_ != NULL || else_.is_linked()) {
1889 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001890 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001891 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
1893 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001894 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 ASSERT(!has_then_stm && !has_else_stm);
1896 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001897 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 if (frame_ != NULL) {
1899 if (has_cc()) {
1900 cc_reg_ = al;
1901 } else {
1902 frame_->Drop();
1903 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904 }
1905 }
1906
1907 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 if (exit.is_linked()) {
1909 exit.Bind();
1910 }
1911 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912}
1913
1914
ager@chromium.org7c537e22008-10-16 08:43:32 +00001915void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 CodeForStatementPosition(node);
1918 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919}
1920
1921
ager@chromium.org7c537e22008-10-16 08:43:32 +00001922void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001924 CodeForStatementPosition(node);
1925 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926}
1927
1928
ager@chromium.org7c537e22008-10-16 08:43:32 +00001929void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001931
ager@chromium.org4af710e2009-09-15 12:20:11 +00001932 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001933 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001934 frame_->PopToR0();
1935 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001936 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 function_return_.Jump();
1938 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 // Pop the result from the frame and prepare the frame for
1940 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001941 if (function_return_.is_bound()) {
1942 // If the function return label is already bound we reuse the
1943 // code by jumping to the return site.
1944 function_return_.Jump();
1945 } else {
1946 function_return_.Bind();
1947 GenerateReturnSequence();
1948 }
1949 }
1950}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001951
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001952
1953void CodeGenerator::GenerateReturnSequence() {
1954 if (FLAG_trace) {
1955 // Push the return value on the stack as the parameter.
1956 // Runtime::TraceExit returns the parameter as it is.
1957 frame_->EmitPush(r0);
1958 frame_->CallRuntime(Runtime::kTraceExit, 1);
1959 }
1960
1961#ifdef DEBUG
1962 // Add a label for checking the size of the code used for returning.
1963 Label check_exit_codesize;
1964 masm_->bind(&check_exit_codesize);
1965#endif
1966 // Make sure that the constant pool is not emitted inside of the return
1967 // sequence.
1968 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1969 // Tear down the frame which will restore the caller's frame pointer and
1970 // the link register.
1971 frame_->Exit();
1972
1973 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1974 // tool from instrumenting as we rely on the code size here.
1975 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1976 masm_->add(sp, sp, Operand(sp_delta));
1977 masm_->Jump(lr);
1978 DeleteFrame();
1979
1980#ifdef DEBUG
1981 // Check that the size of the code used for returning matches what is
1982 // expected by the debugger. If the sp_delts above cannot be encoded in
1983 // the add instruction the add will generate two instructions.
1984 int return_sequence_length =
1985 masm_->InstructionsGeneratedSince(&check_exit_codesize);
1986 CHECK(return_sequence_length ==
1987 Assembler::kJSReturnSequenceInstructions ||
1988 return_sequence_length ==
1989 Assembler::kJSReturnSequenceInstructions + 1);
1990#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001991 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992}
1993
1994
ager@chromium.org7c537e22008-10-16 08:43:32 +00001995void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001996#ifdef DEBUG
1997 int original_height = frame_->height();
1998#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002001 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002002 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002003 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002004 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002006 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002007#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002008 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002009 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002010 verified_true.Branch(eq);
2011 __ stop("PushContext: r0 is expected to be the same as cp");
2012 verified_true.Bind();
2013#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002015 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002016 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017}
2018
2019
ager@chromium.org7c537e22008-10-16 08:43:32 +00002020void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021#ifdef DEBUG
2022 int original_height = frame_->height();
2023#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002025 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 // Pop context.
2027 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2028 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002029 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002030 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031}
2032
2033
ager@chromium.org7c537e22008-10-16 08:43:32 +00002034void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035#ifdef DEBUG
2036 int original_height = frame_->height();
2037#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002040 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002042 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002043
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002044 JumpTarget next_test;
2045 JumpTarget fall_through;
2046 JumpTarget default_entry;
2047 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 ZoneList<CaseClause*>* cases = node->cases();
2049 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
2052 for (int i = 0; i < length; i++) {
2053 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002055 // Remember the default clause and compile it at the end.
2056 default_clause = clause;
2057 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 }
2059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002060 Comment cmnt(masm_, "[ Case clause");
2061 // Compile the test.
2062 next_test.Bind();
2063 next_test.Unuse();
2064 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002065 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002066 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002067 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002068
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 // Before entering the body from the test, remove the switch value from
2070 // the stack.
2071 frame_->Drop();
2072
2073 // Label the body so that fall through is enabled.
2074 if (i > 0 && cases->at(i - 1)->is_default()) {
2075 default_exit.Bind();
2076 } else {
2077 fall_through.Bind();
2078 fall_through.Unuse();
2079 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002080 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081
2082 // If control flow can fall through from the body, jump to the next body
2083 // or the end of the statement.
2084 if (frame_ != NULL) {
2085 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2086 default_entry.Jump();
2087 } else {
2088 fall_through.Jump();
2089 }
2090 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 }
2092
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002093 // The final "test" removes the switch value.
2094 next_test.Bind();
2095 frame_->Drop();
2096
2097 // If there is a default clause, compile it.
2098 if (default_clause != NULL) {
2099 Comment cmnt(masm_, "[ Default clause");
2100 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002101 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002102 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002103 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002104 if (frame_ != NULL && default_exit.is_bound()) {
2105 default_exit.Jump();
2106 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002107 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002109 if (fall_through.is_linked()) {
2110 fall_through.Bind();
2111 }
2112
2113 if (node->break_target()->is_linked()) {
2114 node->break_target()->Bind();
2115 }
2116 node->break_target()->Unuse();
2117 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118}
2119
2120
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002121void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122#ifdef DEBUG
2123 int original_height = frame_->height();
2124#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002125 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002127 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002128 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002129 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002131 // Label the top of the loop for the backward CFG edge. If the test
2132 // is always true we can use the continue target, and if the test is
2133 // always false there is no need.
2134 ConditionAnalysis info = AnalyzeCondition(node->cond());
2135 switch (info) {
2136 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002137 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002139 break;
2140 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002141 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002142 break;
2143 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002144 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002145 body.Bind();
2146 break;
2147 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002149 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002150 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002152 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002153 switch (info) {
2154 case ALWAYS_TRUE:
2155 // If control can fall off the end of the body, jump back to the
2156 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002158 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002161 case ALWAYS_FALSE:
2162 // If we have a continue in the body, we only have to bind its
2163 // jump target.
2164 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002167 break;
2168 case DONT_KNOW:
2169 // We have to compile the test expression if it can be reached by
2170 // control flow falling out of the body or via continue.
2171 if (node->continue_target()->is_linked()) {
2172 node->continue_target()->Bind();
2173 }
2174 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002175 Comment cmnt(masm_, "[ DoWhileCondition");
2176 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002177 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002178 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002179 // A invalid frame here indicates that control did not
2180 // fall out of the test expression.
2181 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 }
2183 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 break;
2185 }
2186
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002187 if (node->break_target()->is_linked()) {
2188 node->break_target()->Bind();
2189 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002190 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002191 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2192}
2193
2194
2195void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2196#ifdef DEBUG
2197 int original_height = frame_->height();
2198#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002199 Comment cmnt(masm_, "[ WhileStatement");
2200 CodeForStatementPosition(node);
2201
2202 // If the test is never true and has no side effects there is no need
2203 // to compile the test or body.
2204 ConditionAnalysis info = AnalyzeCondition(node->cond());
2205 if (info == ALWAYS_FALSE) return;
2206
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002207 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002208 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002209
2210 // Label the top of the loop with the continue target for the backward
2211 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002212 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002213 node->continue_target()->Bind();
2214
2215 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002216 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002217 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002218 if (has_valid_frame()) {
2219 // A NULL frame indicates that control did not fall out of the
2220 // test expression.
2221 Branch(false, node->break_target());
2222 }
2223 if (has_valid_frame() || body.is_linked()) {
2224 body.Bind();
2225 }
2226 }
2227
2228 if (has_valid_frame()) {
2229 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002230 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002231
2232 // If control flow can fall out of the body, jump back to the top.
2233 if (has_valid_frame()) {
2234 node->continue_target()->Jump();
2235 }
2236 }
2237 if (node->break_target()->is_linked()) {
2238 node->break_target()->Bind();
2239 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002240 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002241 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2242}
2243
2244
2245void CodeGenerator::VisitForStatement(ForStatement* node) {
2246#ifdef DEBUG
2247 int original_height = frame_->height();
2248#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002249 Comment cmnt(masm_, "[ ForStatement");
2250 CodeForStatementPosition(node);
2251 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002252 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002253 }
2254
2255 // If the test is never true there is no need to compile the test or
2256 // body.
2257 ConditionAnalysis info = AnalyzeCondition(node->cond());
2258 if (info == ALWAYS_FALSE) return;
2259
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002260 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002261 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002262
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002263 // We know that the loop index is a smi if it is not modified in the
2264 // loop body and it is checked against a constant limit in the loop
2265 // condition. In this case, we reset the static type information of the
2266 // loop index to smi before compiling the body, the update expression, and
2267 // the bottom check of the loop condition.
2268 TypeInfoCodeGenState type_info_scope(this,
2269 node->is_fast_smi_loop() ?
2270 node->loop_variable()->slot() :
2271 NULL,
2272 TypeInfo::Smi());
2273
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002274 // If there is no update statement, label the top of the loop with the
2275 // continue target, otherwise with the loop target.
2276 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2277 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002278 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002279 node->continue_target()->Bind();
2280 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002281 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002282 loop.Bind();
2283 }
2284
2285 // If the test is always true, there is no need to compile it.
2286 if (info == DONT_KNOW) {
2287 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002288 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002289 if (has_valid_frame()) {
2290 Branch(false, node->break_target());
2291 }
2292 if (has_valid_frame() || body.is_linked()) {
2293 body.Bind();
2294 }
2295 }
2296
2297 if (has_valid_frame()) {
2298 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002299 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002300
2301 if (node->next() == NULL) {
2302 // If there is no update statement and control flow can fall out
2303 // of the loop, jump directly to the continue label.
2304 if (has_valid_frame()) {
2305 node->continue_target()->Jump();
2306 }
2307 } else {
2308 // If there is an update statement and control flow can reach it
2309 // via falling out of the body of the loop or continuing, we
2310 // compile the update statement.
2311 if (node->continue_target()->is_linked()) {
2312 node->continue_target()->Bind();
2313 }
2314 if (has_valid_frame()) {
2315 // Record source position of the statement as this code which is
2316 // after the code for the body actually belongs to the loop
2317 // statement and not the body.
2318 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002319 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002320 loop.Jump();
2321 }
2322 }
2323 }
2324 if (node->break_target()->is_linked()) {
2325 node->break_target()->Bind();
2326 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002327 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329}
2330
2331
ager@chromium.org7c537e22008-10-16 08:43:32 +00002332void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002333#ifdef DEBUG
2334 int original_height = frame_->height();
2335#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002339 JumpTarget primitive;
2340 JumpTarget jsobject;
2341 JumpTarget fixed_array;
2342 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2343 JumpTarget end_del_check;
2344 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345
2346 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002347 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002349 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2351 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002352 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002353 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2354 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002356 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2357 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
2360 // Stack layout in body:
2361 // [iteration counter (Smi)]
2362 // [length of array]
2363 // [FixedArray]
2364 // [Map or 0]
2365 // [Object]
2366
2367 // Check if enumerable is already a JSObject
2368 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002370 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 primitive.Bind();
2374 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002375 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002377 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002379 // r0: value to be iterated over
2380 frame_->EmitPush(r0); // Push the object being iterated over.
2381
2382 // Check cache validity in generated code. This is a fast case for
2383 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2384 // guarantee cache validity, call the runtime system to check cache
2385 // validity or get the property names in a fixed array.
2386 JumpTarget call_runtime;
2387 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2388 JumpTarget check_prototype;
2389 JumpTarget use_cache;
2390 __ mov(r1, Operand(r0));
2391 loop.Bind();
2392 // Check that there are no elements.
2393 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2394 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2395 __ cmp(r2, r4);
2396 call_runtime.Branch(ne);
2397 // Check that instance descriptors are not empty so that we can
2398 // check for an enum cache. Leave the map in r3 for the subsequent
2399 // prototype load.
2400 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2401 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2402 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2403 __ cmp(r2, ip);
2404 call_runtime.Branch(eq);
2405 // Check that there in an enum cache in the non-empty instance
2406 // descriptors. This is the case if the next enumeration index
2407 // field does not contain a smi.
2408 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2409 __ tst(r2, Operand(kSmiTagMask));
2410 call_runtime.Branch(eq);
2411 // For all objects but the receiver, check that the cache is empty.
2412 // r4: empty fixed array root.
2413 __ cmp(r1, r0);
2414 check_prototype.Branch(eq);
2415 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2416 __ cmp(r2, r4);
2417 call_runtime.Branch(ne);
2418 check_prototype.Bind();
2419 // Load the prototype from the map and loop if non-null.
2420 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2421 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2422 __ cmp(r1, ip);
2423 loop.Branch(ne);
2424 // The enum cache is valid. Load the map of the object being
2425 // iterated over and use the cache for the iteration.
2426 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2427 use_cache.Jump();
2428
2429 call_runtime.Bind();
2430 // Call the runtime to get the property names for the object.
2431 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002434 // If we got a map from the runtime call, we can do a fast
2435 // modification check. Otherwise, we got a fixed array, and we have
2436 // to do a slow check.
2437 // r0: map or fixed array (result from call to
2438 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 __ mov(r2, Operand(r0));
2440 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002441 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2442 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002443 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002445 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002447 // r0: map (either the result from a call to
2448 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2449 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 __ mov(r1, Operand(r0));
2451 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2452 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2453 __ ldr(r2,
2454 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2455
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->EmitPush(r0); // map
2457 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002458 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002459 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002460 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 frame_->EmitPush(r0);
2462 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 frame_->EmitPush(r1); // insert 0 in place of Map
2467 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
2469 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002470 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002472 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474
2475 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002477 // sp[0] : index
2478 // sp[1] : array/enum cache length
2479 // sp[2] : array or enum cache
2480 // sp[3] : 0 or map
2481 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482 // Grab the current frame's height for the break and continue
2483 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002484 node->break_target()->SetExpectedHeight();
2485 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002487 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002488 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002489 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 node->break_target()->Branch(hs);
2491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2495 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2496
2497 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 // Check if this (still) matches the map of the enumerable.
2500 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002501 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2503 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505
2506 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002507 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2508 frame_->EmitPush(r0);
2509 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002510 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002511 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 // If the property has been removed while iterating, we just skip it.
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
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002519 { VirtualFrame::RegisterAllocationScope scope(this);
2520 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002522 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002523 // Loading a reference may leave the frame in an unspilled state.
2524 frame_->SpillAll(); // Sync stack to memory.
2525 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002526 __ ldr(r0, frame_->ElementAt(each.size()));
2527 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002528 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002529 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002530 } else {
2531 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002532 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002533 // right on top of or right underneath a zero-sized reference.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002534 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002535 frame_->Drop(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002536 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 }
2538 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002541 { VirtualFrame::RegisterAllocationScope scope(this);
2542 Visit(node->body());
2543 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002545 // Next. Reestablish a spilled frame in case we are coming here via
2546 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002547 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002548 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549 frame_->EmitPop(r0);
2550 __ add(r0, r0, Operand(Smi::FromInt(1)));
2551 frame_->EmitPush(r0);
2552 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002554 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2555 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002557 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558
2559 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 exit.Bind();
2561 node->continue_target()->Unuse();
2562 node->break_target()->Unuse();
2563 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564}
2565
2566
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002567void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568#ifdef DEBUG
2569 int original_height = frame_->height();
2570#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002571 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002572 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002575 JumpTarget try_block;
2576 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581
2582 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002583 Variable* catch_var = node->catch_var()->var();
2584 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2585 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586
2587 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002590 { VirtualFrame::RegisterAllocationScope scope(this);
2591 VisitStatements(node->catch_block()->statements());
2592 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002593 if (frame_ != NULL) {
2594 exit.Jump();
2595 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596
2597
2598 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2602 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002604 // Shadow the labels for all escapes from the try block, including
2605 // returns. During shadowing, the original label is hidden as the
2606 // LabelShadow and operations on the original actually affect the
2607 // shadowing label.
2608 //
2609 // We should probably try to unify the escaping labels and the return
2610 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002611 int nof_escapes = node->escaping_targets()->length();
2612 List<ShadowTarget*> shadows(1 + nof_escapes);
2613
2614 // Add the shadow target for the function return.
2615 static const int kReturnShadowIndex = 0;
2616 shadows.Add(new ShadowTarget(&function_return_));
2617 bool function_return_was_shadowed = function_return_is_shadowed_;
2618 function_return_is_shadowed_ = true;
2619 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2620
2621 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 }
2625
2626 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002627 { VirtualFrame::RegisterAllocationScope scope(this);
2628 VisitStatements(node->try_block()->statements());
2629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
2631 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002632 // After shadowing stops, the original labels are unshadowed and the
2633 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002634 bool has_unlinks = false;
2635 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002637 has_unlinks = has_unlinks || shadows[i]->is_linked();
2638 }
2639 function_return_is_shadowed_ = function_return_was_shadowed;
2640
2641 // Get an external reference to the handler address.
2642 ExternalReference handler_address(Top::k_handler_address);
2643
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 // If we can fall off the end of the try block, unlink from try chain.
2645 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002646 // The next handler address is on top of the frame. Unlink from
2647 // the handler list and drop the rest of this handler from the
2648 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002649 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002650 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651 __ mov(r3, Operand(handler_address));
2652 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002653 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 if (has_unlinks) {
2655 exit.Jump();
2656 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 }
2658
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002659 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660 // jumped to. Deallocate each shadow target.
2661 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002663 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 shadows[i]->Bind();
2665 // Because we can be jumping here (to spilled code) from unspilled
2666 // code, we need to reestablish a spilled frame at this block.
2667 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669 // Reload sp from the top handler, because some statements that we
2670 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002671 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002673 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002675 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002676 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002678 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002680 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2681 frame_->PrepareForReturn();
2682 }
2683 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 }
2685 }
2686
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687 exit.Bind();
2688 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689}
2690
2691
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002692void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002693#ifdef DEBUG
2694 int original_height = frame_->height();
2695#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002696 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002697 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699
2700 // State: Used to keep track of reason for entering the finally
2701 // block. Should probably be extended to hold information for
2702 // break/continue from within the try block.
2703 enum { FALLING, THROWING, JUMPING };
2704
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002705 JumpTarget try_block;
2706 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002708 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002710 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 // In case of thrown exceptions, this is where we continue.
2712 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002713 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714
2715 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002716 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002717
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002718 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2719 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002721 // Shadow the labels for all escapes from the try block, including
2722 // returns. Shadowing hides the original label as the LabelShadow and
2723 // operations on the original actually affect the shadowing label.
2724 //
2725 // We should probably try to unify the escaping labels and the return
2726 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002727 int nof_escapes = node->escaping_targets()->length();
2728 List<ShadowTarget*> shadows(1 + nof_escapes);
2729
2730 // Add the shadow target for the function return.
2731 static const int kReturnShadowIndex = 0;
2732 shadows.Add(new ShadowTarget(&function_return_));
2733 bool function_return_was_shadowed = function_return_is_shadowed_;
2734 function_return_is_shadowed_ = true;
2735 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2736
2737 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002739 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 }
2741
2742 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002743 { VirtualFrame::RegisterAllocationScope scope(this);
2744 VisitStatements(node->try_block()->statements());
2745 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002747 // Stop the introduced shadowing and count the number of required unlinks.
2748 // After shadowing stops, the original labels are unshadowed and the
2749 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002751 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 shadows[i]->StopShadowing();
2753 if (shadows[i]->is_linked()) nof_unlinks++;
2754 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002755 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 // Get an external reference to the handler address.
2758 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002759
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 // If we can fall off the end of the try block, unlink from the try
2761 // chain and set the state on the frame to FALLING.
2762 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002763 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002764 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002765 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002766 __ mov(r3, Operand(handler_address));
2767 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002768 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769
2770 // Fake a top of stack value (unneeded when FALLING) and set the
2771 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002772 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 frame_->EmitPush(r0);
2774 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2775 if (nof_unlinks > 0) {
2776 finally_block.Jump();
2777 }
2778 }
2779
2780 // Generate code to unlink and set the state for the (formerly)
2781 // shadowing targets that have been jumped to.
2782 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 // If we have come from the shadowed return, the return value is
2785 // in (a non-refcounted reference to) r0. We must preserve it
2786 // until it is pushed.
2787 //
2788 // Because we can be jumping here (to spilled code) from
2789 // unspilled code, we need to reestablish a spilled frame at
2790 // this block.
2791 shadows[i]->Bind();
2792 frame_->SpillAll();
2793
2794 // Reload sp from the top handler, because some statements that
2795 // we break from (eg, for...in) may have left stuff on the
2796 // stack.
2797 __ mov(r3, Operand(handler_address));
2798 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002799 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002800
2801 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002802 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002803 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002804 frame_->EmitPop(r1);
2805 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002806 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002807
2808 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002809 // If this label shadowed the function return, materialize the
2810 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002811 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002812 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002813 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002814 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002815 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 }
2817 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002818 if (--nof_unlinks > 0) {
2819 // If this is not the last unlink block, jump around the next.
2820 finally_block.Jump();
2821 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822 }
2823 }
2824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002826 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002828 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002829 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002830
2831 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002832 // and the state - while evaluating the finally block.
2833 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002835 { VirtualFrame::RegisterAllocationScope scope(this);
2836 VisitStatements(node->finally_block()->statements());
2837 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 if (has_valid_frame()) {
2840 // Restore state and return value or faked TOS.
2841 frame_->EmitPop(r2);
2842 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 }
2844
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002845 // Generate code to jump to the right destination for all used
2846 // formerly shadowing targets. Deallocate each shadow target.
2847 for (int i = 0; i < shadows.length(); i++) {
2848 if (has_valid_frame() && shadows[i]->is_bound()) {
2849 JumpTarget* original = shadows[i]->other_target();
2850 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2851 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002852 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002853 skip.Branch(ne);
2854 frame_->PrepareForReturn();
2855 original->Jump();
2856 skip.Bind();
2857 } else {
2858 original->Branch(eq);
2859 }
2860 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002861 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002863 if (has_valid_frame()) {
2864 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002865 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002866 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2867 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002869 // Rethrow exception.
2870 frame_->EmitPush(r0);
2871 frame_->CallRuntime(Runtime::kReThrow, 1);
2872
2873 // Done.
2874 exit.Bind();
2875 }
2876 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877}
2878
2879
ager@chromium.org7c537e22008-10-16 08:43:32 +00002880void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002881#ifdef DEBUG
2882 int original_height = frame_->height();
2883#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002885 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002886#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002887 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002888#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002889 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002890 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891}
2892
2893
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002894void CodeGenerator::InstantiateFunction(
2895 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002896 // Use the fast case closure allocation code that allocates in new
2897 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002898 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002899 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002900 frame_->EmitPush(Operand(function_info));
2901 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002902 frame_->CallStub(&stub, 1);
2903 frame_->EmitPush(r0);
2904 } else {
2905 // Create a new closure.
2906 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002907 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002908 frame_->CallRuntime(Runtime::kNewClosure, 2);
2909 frame_->EmitPush(r0);
2910 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911}
2912
2913
ager@chromium.org7c537e22008-10-16 08:43:32 +00002914void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002915#ifdef DEBUG
2916 int original_height = frame_->height();
2917#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 Comment cmnt(masm_, "[ FunctionLiteral");
2919
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002920 // Build the function info and instantiate it.
2921 Handle<SharedFunctionInfo> function_info =
2922 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002923 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924 if (HasStackOverflow()) {
2925 ASSERT(frame_->height() == original_height);
2926 return;
2927 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002928 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002929 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930}
2931
2932
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002933void CodeGenerator::VisitSharedFunctionInfoLiteral(
2934 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002935#ifdef DEBUG
2936 int original_height = frame_->height();
2937#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002938 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2939 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002940 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941}
2942
2943
ager@chromium.org7c537e22008-10-16 08:43:32 +00002944void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002945#ifdef DEBUG
2946 int original_height = frame_->height();
2947#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002948 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002949 JumpTarget then;
2950 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002951 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002952 if (has_valid_frame()) {
2953 Branch(false, &else_);
2954 }
2955 if (has_valid_frame() || then.is_linked()) {
2956 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002957 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002958 }
2959 if (else_.is_linked()) {
2960 JumpTarget exit;
2961 if (has_valid_frame()) exit.Jump();
2962 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002963 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002964 if (exit.is_linked()) exit.Bind();
2965 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002966 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967}
2968
2969
ager@chromium.org7c537e22008-10-16 08:43:32 +00002970void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2971 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002972 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002974 // JumpTargets do not yet support merging frames so the frame must be
2975 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002976 JumpTarget slow;
2977 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002978
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002979 // Generate fast case for loading from slots that correspond to
2980 // local/global variables or arguments unless they are shadowed by
2981 // eval-introduced bindings.
2982 EmitDynamicLoadFromSlotFastCase(slot,
2983 typeof_state,
2984 &slow,
2985 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002986
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002987 slow.Bind();
2988 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002989 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002990
ager@chromium.org7c537e22008-10-16 08:43:32 +00002991 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002993 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002994 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002996
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002997 done.Bind();
2998 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999
3000 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003001 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003002 TypeInfo info = type_info(slot);
3003 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003004
ager@chromium.org7c537e22008-10-16 08:43:32 +00003005 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003006 // Const slots may contain 'the hole' value (the constant hasn't been
3007 // initialized yet) which needs to be converted into the 'undefined'
3008 // value.
3009 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003010 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003011 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003012 __ cmp(tos, ip);
3013 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3014 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 }
3016 }
3017}
3018
3019
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003020void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3021 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003022 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003023 LoadFromSlot(slot, state);
3024
3025 // Bail out quickly if we're not using lazy arguments allocation.
3026 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3027
3028 // ... or if the slot isn't a non-parameter arguments slot.
3029 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3030
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003031 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003032 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003033 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003034
3035 // If the loaded value is the sentinel that indicates that we
3036 // haven't loaded the arguments object yet, we need to do it now.
3037 JumpTarget exit;
3038 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003039 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003040 exit.Branch(ne);
3041 frame_->Drop();
3042 StoreArgumentsObject(false);
3043 exit.Bind();
3044}
3045
3046
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003047void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3048 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003049 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003050 if (slot->type() == Slot::LOOKUP) {
3051 ASSERT(slot->var()->is_dynamic());
3052
3053 // For now, just do a runtime call.
3054 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003055 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003056
3057 if (init_state == CONST_INIT) {
3058 // Same as the case for a normal store, but ignores attribute
3059 // (e.g. READ_ONLY) of context slot so that we can initialize
3060 // const properties (introduced via eval("const foo = (some
3061 // expr);")). Also, uses the current function context instead of
3062 // the top context.
3063 //
3064 // Note that we must declare the foo upon entry of eval(), via a
3065 // context slot declaration, but we cannot initialize it at the
3066 // same time, because the const declaration may be at the end of
3067 // the eval code (sigh...) and the const variable may have been
3068 // used before (where its value is 'undefined'). Thus, we can only
3069 // do the initialization when we actually encounter the expression
3070 // and when the expression operands are defined and valid, and
3071 // thus we need the split into 2 operations: declaration of the
3072 // context slot followed by initialization.
3073 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3074 } else {
3075 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3076 }
3077 // Storing a variable must keep the (new) value on the expression
3078 // stack. This is necessary for compiling assignment expressions.
3079 frame_->EmitPush(r0);
3080
3081 } else {
3082 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003083 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003084 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003085
ager@chromium.org357bf652010-04-12 11:30:10 +00003086 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003087 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003088
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003089 if (init_state == CONST_INIT) {
3090 ASSERT(slot->var()->mode() == Variable::CONST);
3091 // Only the first const initialization must be executed (the slot
3092 // still contains 'the hole' value). When the assignment is
3093 // executed, the code is identical to a normal store (see below).
3094 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003095 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003096 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003097 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003098 exit.Branch(ne);
3099 }
3100
3101 // We must execute the store. Storing a variable must keep the
3102 // (new) value on the stack. This is necessary for compiling
3103 // assignment expressions.
3104 //
3105 // Note: We will reach here even with slot->var()->mode() ==
3106 // Variable::CONST because of const declarations which will
3107 // initialize consts to 'the hole' value and by doing so, end up
3108 // calling this code. r2 may be loaded with context; used below in
3109 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003110 Register tos = frame_->Peek();
3111 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003112 if (slot->type() == Slot::CONTEXT) {
3113 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003114 __ tst(tos, Operand(kSmiTagMask));
3115 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003116 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003117 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003118 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003119 // We need an extra register. Until we have a way to do that in the
3120 // virtual frame we will cheat and ask for a free TOS register.
3121 Register scratch3 = frame_->GetTOSRegister();
3122 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003123 }
3124 // If we definitely did not jump over the assignment, we do not need
3125 // to bind the exit label. Doing so can defeat peephole
3126 // optimization.
3127 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3128 exit.Bind();
3129 }
3130 }
3131}
3132
3133
ager@chromium.org381abbb2009-02-25 13:23:22 +00003134void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3135 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003136 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003137 // Check that no extension objects have been created by calls to
3138 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003139 Register tmp = frame_->scratch0();
3140 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003141 Register context = cp;
3142 Scope* s = scope();
3143 while (s != NULL) {
3144 if (s->num_heap_slots() > 0) {
3145 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003146 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003147 // Check that extension is NULL.
3148 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3149 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003150 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003151 }
3152 // Load next context in chain.
3153 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3154 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3155 context = tmp;
3156 }
3157 // If no outer scope calls eval, we do not need to check more
3158 // context extensions.
3159 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3160 s = s->outer_scope();
3161 }
3162
3163 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003164 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003165 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003166 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003167 __ bind(&next);
3168 // Terminate at global context.
3169 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003170 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3171 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003172 __ b(eq, &fast);
3173 // Check that extension is NULL.
3174 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3175 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003176 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003177 // Load next context in chain.
3178 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3179 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3180 __ b(&next);
3181 __ bind(&fast);
3182 }
3183
ager@chromium.org381abbb2009-02-25 13:23:22 +00003184 // Load the global object.
3185 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003186 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003187 frame_->CallLoadIC(slot->var()->name(),
3188 typeof_state == INSIDE_TYPEOF
3189 ? RelocInfo::CODE_TARGET
3190 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003191}
3192
3193
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003194void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3195 TypeofState typeof_state,
3196 JumpTarget* slow,
3197 JumpTarget* done) {
3198 // Generate fast-case code for variables that might be shadowed by
3199 // eval-introduced variables. Eval is used a lot without
3200 // introducing variables. In those cases, we do not want to
3201 // perform a runtime call for all variables in the scope
3202 // containing the eval.
3203 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3204 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3205 frame_->SpillAll();
3206 done->Jump();
3207
3208 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3209 frame_->SpillAll();
3210 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3211 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3212 if (potential_slot != NULL) {
3213 // Generate fast case for locals that rewrite to slots.
3214 __ ldr(r0,
3215 ContextSlotOperandCheckExtensions(potential_slot,
3216 r1,
3217 r2,
3218 slow));
3219 if (potential_slot->var()->mode() == Variable::CONST) {
3220 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3221 __ cmp(r0, ip);
3222 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3223 }
3224 done->Jump();
3225 } else if (rewrite != NULL) {
3226 // Generate fast case for argument loads.
3227 Property* property = rewrite->AsProperty();
3228 if (property != NULL) {
3229 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3230 Literal* key_literal = property->key()->AsLiteral();
3231 if (obj_proxy != NULL &&
3232 key_literal != NULL &&
3233 obj_proxy->IsArguments() &&
3234 key_literal->handle()->IsSmi()) {
3235 // Load arguments object if there are no eval-introduced
3236 // variables. Then load the argument from the arguments
3237 // object using keyed load.
3238 __ ldr(r0,
3239 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3240 r1,
3241 r2,
3242 slow));
3243 frame_->EmitPush(r0);
3244 __ mov(r1, Operand(key_literal->handle()));
3245 frame_->EmitPush(r1);
3246 EmitKeyedLoad();
3247 done->Jump();
3248 }
3249 }
3250 }
3251 }
3252}
3253
3254
ager@chromium.org7c537e22008-10-16 08:43:32 +00003255void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003256#ifdef DEBUG
3257 int original_height = frame_->height();
3258#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003259 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003260 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003261 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003262}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263
ager@chromium.org7c537e22008-10-16 08:43:32 +00003264
3265void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003266#ifdef DEBUG
3267 int original_height = frame_->height();
3268#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003269 Comment cmnt(masm_, "[ VariableProxy");
3270
3271 Variable* var = node->var();
3272 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003273 if (expr != NULL) {
3274 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003275 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003276 ASSERT(var->is_global());
3277 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003278 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003280 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281}
3282
3283
ager@chromium.org7c537e22008-10-16 08:43:32 +00003284void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003285#ifdef DEBUG
3286 int original_height = frame_->height();
3287#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003289 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003290 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003291 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003292 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003293 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294}
3295
3296
ager@chromium.org7c537e22008-10-16 08:43:32 +00003297void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003298#ifdef DEBUG
3299 int original_height = frame_->height();
3300#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301 Comment cmnt(masm_, "[ RexExp Literal");
3302
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003303 Register tmp = VirtualFrame::scratch0();
3304 // Free up a TOS register that can be used to push the literal.
3305 Register literal = frame_->GetTOSRegister();
3306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 // Retrieve the literal array and check the allocated entry.
3308
3309 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003310 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311
3312 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003313 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314
3315 // Load the literal at the ast saved index.
3316 int literal_offset =
3317 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003318 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003319
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003320 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003321 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003322 __ cmp(literal, ip);
3323 // This branch locks the virtual frame at the done label to match the
3324 // one we have here, where the literal register is not on the stack and
3325 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003326 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003328 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003330 // literal array (0)
3331 frame_->EmitPush(tmp);
3332 // literal index (1)
3333 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3334 // RegExp pattern (2)
3335 frame_->EmitPush(Operand(node->pattern()));
3336 // RegExp flags (3)
3337 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003338 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003339 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003341 materialized.Bind();
3342
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003343 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003344 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3345 frame_->EmitPush(Operand(Smi::FromInt(size)));
3346 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3347 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3348 // r0 is newly allocated space.
3349
3350 // Reuse literal variable with (possibly) a new register, still holding
3351 // the materialized boilerplate.
3352 literal = frame_->PopToRegister(r0);
3353
3354 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3355
3356 // Push the clone.
3357 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003358 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359}
3360
3361
ager@chromium.org7c537e22008-10-16 08:43:32 +00003362void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003363#ifdef DEBUG
3364 int original_height = frame_->height();
3365#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 Comment cmnt(masm_, "[ ObjectLiteral");
3367
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003368 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003369 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003370 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003371 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003372 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3373 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003374 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003375 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003376 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003377 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003378 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003379 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003380 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003381 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003382 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003383 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003384 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003385 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003386 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003387 // At the start of each iteration, the top of stack contains
3388 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389 ObjectLiteral::Property* property = node->properties()->at(i);
3390 Literal* key = property->key();
3391 Expression* value = property->value();
3392 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003393 case ObjectLiteral::Property::CONSTANT:
3394 break;
3395 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3396 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3397 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003398 case ObjectLiteral::Property::COMPUTED:
3399 if (key->handle()->IsSymbol()) {
3400 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003401 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003402 frame_->PopToR0();
3403 // Fetch the object literal.
3404 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003405 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003406 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3407 break;
3408 }
3409 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003411 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003412 Load(key);
3413 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003414 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 break;
3416 }
3417 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003418 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003419 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003420 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003421 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003422 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003423 break;
3424 }
3425 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003426 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003427 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003428 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003429 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431 break;
3432 }
3433 }
3434 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003435 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003436}
3437
3438
ager@chromium.org7c537e22008-10-16 08:43:32 +00003439void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440#ifdef DEBUG
3441 int original_height = frame_->height();
3442#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003444
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003445 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003446 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003447 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003448 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003449 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3450 frame_->EmitPush(tos);
3451 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3452 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003453 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003454 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3455 FastCloneShallowArrayStub stub(
3456 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3457 frame_->CallStub(&stub, 3);
3458 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3459 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003460 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003461 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003462 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003463 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003464 FastCloneShallowArrayStub stub(
3465 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003466 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003467 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003468 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003469 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003470
3471 // Generate code to set the elements in the array that are not
3472 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 for (int i = 0; i < node->values()->length(); i++) {
3474 Expression* value = node->values()->at(i);
3475
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003476 // If value is a literal the property value is already set in the
3477 // boilerplate object.
3478 if (value->AsLiteral() != NULL) continue;
3479 // If value is a materialized literal the property value is already set
3480 // in the boilerplate object if it is simple.
3481 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003483 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003484 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003485 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003486 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003487 frame_->SpillAllButCopyTOSToR1();
3488
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003489 // Get the elements array.
3490 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003492 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003493 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003494 __ str(r0, FieldMemOperand(r1, offset));
3495
3496 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003497 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003499 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500}
3501
3502
ager@chromium.org32912102009-01-16 10:38:43 +00003503void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003504#ifdef DEBUG
3505 int original_height = frame_->height();
3506#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003507 // Call runtime routine to allocate the catch extension object and
3508 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003509 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003510 Load(node->key());
3511 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003512 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3513 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003514 ASSERT_EQ(original_height + 1, frame_->height());
3515}
3516
3517
3518void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3519#ifdef DEBUG
3520 int original_height = frame_->height();
3521#endif
3522 Comment cmnt(masm(), "[ Variable Assignment");
3523 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3524 ASSERT(var != NULL);
3525 Slot* slot = var->slot();
3526 ASSERT(slot != NULL);
3527
3528 // Evaluate the right-hand side.
3529 if (node->is_compound()) {
3530 // For a compound assignment the right-hand side is a binary operation
3531 // between the current property value and the actual right-hand side.
3532 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3533
3534 // Perform the binary operation.
3535 Literal* literal = node->value()->AsLiteral();
3536 bool overwrite_value =
3537 (node->value()->AsBinaryOperation() != NULL &&
3538 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3539 if (literal != NULL && literal->handle()->IsSmi()) {
3540 SmiOperation(node->binary_op(),
3541 literal->handle(),
3542 false,
3543 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3544 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003545 GenerateInlineSmi inline_smi =
3546 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3547 if (literal != NULL) {
3548 ASSERT(!literal->handle()->IsSmi());
3549 inline_smi = DONT_GENERATE_INLINE_SMI;
3550 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003551 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003552 GenericBinaryOperation(node->binary_op(),
3553 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3554 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003555 }
3556 } else {
3557 Load(node->value());
3558 }
3559
3560 // Perform the assignment.
3561 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3562 CodeForSourcePosition(node->position());
3563 StoreToSlot(slot,
3564 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3565 }
3566 ASSERT_EQ(original_height + 1, frame_->height());
3567}
3568
3569
3570void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3571#ifdef DEBUG
3572 int original_height = frame_->height();
3573#endif
3574 Comment cmnt(masm(), "[ Named Property Assignment");
3575 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3576 Property* prop = node->target()->AsProperty();
3577 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3578
3579 // Initialize name and evaluate the receiver sub-expression if necessary. If
3580 // the receiver is trivial it is not placed on the stack at this point, but
3581 // loaded whenever actually needed.
3582 Handle<String> name;
3583 bool is_trivial_receiver = false;
3584 if (var != NULL) {
3585 name = var->name();
3586 } else {
3587 Literal* lit = prop->key()->AsLiteral();
3588 ASSERT_NOT_NULL(lit);
3589 name = Handle<String>::cast(lit->handle());
3590 // Do not materialize the receiver on the frame if it is trivial.
3591 is_trivial_receiver = prop->obj()->IsTrivial();
3592 if (!is_trivial_receiver) Load(prop->obj());
3593 }
3594
3595 // Change to slow case in the beginning of an initialization block to
3596 // avoid the quadratic behavior of repeatedly adding fast properties.
3597 if (node->starts_initialization_block()) {
3598 // Initialization block consists of assignments of the form expr.x = ..., so
3599 // this will never be an assignment to a variable, so there must be a
3600 // receiver object.
3601 ASSERT_EQ(NULL, var);
3602 if (is_trivial_receiver) {
3603 Load(prop->obj());
3604 } else {
3605 frame_->Dup();
3606 }
3607 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3608 }
3609
3610 // Change to fast case at the end of an initialization block. To prepare for
3611 // that add an extra copy of the receiver to the frame, so that it can be
3612 // converted back to fast case after the assignment.
3613 if (node->ends_initialization_block() && !is_trivial_receiver) {
3614 frame_->Dup();
3615 }
3616
3617 // Stack layout:
3618 // [tos] : receiver (only materialized if non-trivial)
3619 // [tos+1] : receiver if at the end of an initialization block
3620
3621 // Evaluate the right-hand side.
3622 if (node->is_compound()) {
3623 // For a compound assignment the right-hand side is a binary operation
3624 // between the current property value and the actual right-hand side.
3625 if (is_trivial_receiver) {
3626 Load(prop->obj());
3627 } else if (var != NULL) {
3628 LoadGlobal();
3629 } else {
3630 frame_->Dup();
3631 }
3632 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003633
3634 // Perform the binary operation.
3635 Literal* literal = node->value()->AsLiteral();
3636 bool overwrite_value =
3637 (node->value()->AsBinaryOperation() != NULL &&
3638 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3639 if (literal != NULL && literal->handle()->IsSmi()) {
3640 SmiOperation(node->binary_op(),
3641 literal->handle(),
3642 false,
3643 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3644 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003645 GenerateInlineSmi inline_smi =
3646 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3647 if (literal != NULL) {
3648 ASSERT(!literal->handle()->IsSmi());
3649 inline_smi = DONT_GENERATE_INLINE_SMI;
3650 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003651 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003652 GenericBinaryOperation(node->binary_op(),
3653 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3654 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003655 }
3656 } else {
3657 // For non-compound assignment just load the right-hand side.
3658 Load(node->value());
3659 }
3660
3661 // Stack layout:
3662 // [tos] : value
3663 // [tos+1] : receiver (only materialized if non-trivial)
3664 // [tos+2] : receiver if at the end of an initialization block
3665
3666 // Perform the assignment. It is safe to ignore constants here.
3667 ASSERT(var == NULL || var->mode() != Variable::CONST);
3668 ASSERT_NE(Token::INIT_CONST, node->op());
3669 if (is_trivial_receiver) {
3670 // Load the receiver and swap with the value.
3671 Load(prop->obj());
3672 Register t0 = frame_->PopToRegister();
3673 Register t1 = frame_->PopToRegister(t0);
3674 frame_->EmitPush(t0);
3675 frame_->EmitPush(t1);
3676 }
3677 CodeForSourcePosition(node->position());
3678 bool is_contextual = (var != NULL);
3679 EmitNamedStore(name, is_contextual);
3680 frame_->EmitPush(r0);
3681
3682 // Change to fast case at the end of an initialization block.
3683 if (node->ends_initialization_block()) {
3684 ASSERT_EQ(NULL, var);
3685 // The argument to the runtime call is the receiver.
3686 if (is_trivial_receiver) {
3687 Load(prop->obj());
3688 } else {
3689 // A copy of the receiver is below the value of the assignment. Swap
3690 // the receiver and the value of the assignment expression.
3691 Register t0 = frame_->PopToRegister();
3692 Register t1 = frame_->PopToRegister(t0);
3693 frame_->EmitPush(t0);
3694 frame_->EmitPush(t1);
3695 }
3696 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3697 }
3698
3699 // Stack layout:
3700 // [tos] : result
3701
3702 ASSERT_EQ(original_height + 1, frame_->height());
3703}
3704
3705
3706void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3707#ifdef DEBUG
3708 int original_height = frame_->height();
3709#endif
3710 Comment cmnt(masm_, "[ Keyed Property Assignment");
3711 Property* prop = node->target()->AsProperty();
3712 ASSERT_NOT_NULL(prop);
3713
3714 // Evaluate the receiver subexpression.
3715 Load(prop->obj());
3716
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003717 WriteBarrierCharacter wb_info;
3718
ager@chromium.orgac091b72010-05-05 07:34:42 +00003719 // Change to slow case in the beginning of an initialization block to
3720 // avoid the quadratic behavior of repeatedly adding fast properties.
3721 if (node->starts_initialization_block()) {
3722 frame_->Dup();
3723 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3724 }
3725
3726 // Change to fast case at the end of an initialization block. To prepare for
3727 // that add an extra copy of the receiver to the frame, so that it can be
3728 // converted back to fast case after the assignment.
3729 if (node->ends_initialization_block()) {
3730 frame_->Dup();
3731 }
3732
3733 // Evaluate the key subexpression.
3734 Load(prop->key());
3735
3736 // Stack layout:
3737 // [tos] : key
3738 // [tos+1] : receiver
3739 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003740 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003741 // Evaluate the right-hand side.
3742 if (node->is_compound()) {
3743 // For a compound assignment the right-hand side is a binary operation
3744 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003745 // Duplicate receiver and key for loading the current property value.
3746 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003747 EmitKeyedLoad();
3748 frame_->EmitPush(r0);
3749
3750 // Perform the binary operation.
3751 Literal* literal = node->value()->AsLiteral();
3752 bool overwrite_value =
3753 (node->value()->AsBinaryOperation() != NULL &&
3754 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3755 if (literal != NULL && literal->handle()->IsSmi()) {
3756 SmiOperation(node->binary_op(),
3757 literal->handle(),
3758 false,
3759 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3760 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003761 GenerateInlineSmi inline_smi =
3762 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3763 if (literal != NULL) {
3764 ASSERT(!literal->handle()->IsSmi());
3765 inline_smi = DONT_GENERATE_INLINE_SMI;
3766 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003767 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003768 GenericBinaryOperation(node->binary_op(),
3769 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3770 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003771 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003772 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003773 } else {
3774 // For non-compound assignment just load the right-hand side.
3775 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003776 wb_info = node->value()->AsLiteral() != NULL ?
3777 NEVER_NEWSPACE :
3778 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003779 }
3780
3781 // Stack layout:
3782 // [tos] : value
3783 // [tos+1] : key
3784 // [tos+2] : receiver
3785 // [tos+3] : receiver if at the end of an initialization block
3786
3787 // Perform the assignment. It is safe to ignore constants here.
3788 ASSERT(node->op() != Token::INIT_CONST);
3789 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003790 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003791 frame_->EmitPush(r0);
3792
3793 // Stack layout:
3794 // [tos] : result
3795 // [tos+1] : receiver if at the end of an initialization block
3796
3797 // Change to fast case at the end of an initialization block.
3798 if (node->ends_initialization_block()) {
3799 // The argument to the runtime call is the extra copy of the receiver,
3800 // which is below the value of the assignment. Swap the receiver and
3801 // the value of the assignment expression.
3802 Register t0 = frame_->PopToRegister();
3803 Register t1 = frame_->PopToRegister(t0);
3804 frame_->EmitPush(t1);
3805 frame_->EmitPush(t0);
3806 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3807 }
3808
3809 // Stack layout:
3810 // [tos] : result
3811
3812 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003813}
3814
3815
ager@chromium.org7c537e22008-10-16 08:43:32 +00003816void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003817 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818#ifdef DEBUG
3819 int original_height = frame_->height();
3820#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003821 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003822
ager@chromium.orgac091b72010-05-05 07:34:42 +00003823 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3824 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825
ager@chromium.orgac091b72010-05-05 07:34:42 +00003826 if (var != NULL && !var->is_global()) {
3827 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003828
ager@chromium.orgac091b72010-05-05 07:34:42 +00003829 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3830 (var != NULL && var->is_global())) {
3831 // Properties whose keys are property names and global variables are
3832 // treated as named property references. We do not need to consider
3833 // global 'this' because it is not a valid left-hand side.
3834 EmitNamedPropertyAssignment(node);
3835
3836 } else if (prop != NULL) {
3837 // Other properties (including rewritten parameters for a function that
3838 // uses arguments) are keyed property assignments.
3839 EmitKeyedPropertyAssignment(node);
3840
3841 } else {
3842 // Invalid left-hand side.
3843 Load(node->target());
3844 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3845 // The runtime call doesn't actually return but the code generator will
3846 // still generate code and expects a certain frame height.
3847 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003848 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003849 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850}
3851
3852
ager@chromium.org7c537e22008-10-16 08:43:32 +00003853void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003854#ifdef DEBUG
3855 int original_height = frame_->height();
3856#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857 Comment cmnt(masm_, "[ Throw");
3858
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003859 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003860 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003861 frame_->CallRuntime(Runtime::kThrow, 1);
3862 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003863 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003864}
3865
3866
ager@chromium.org7c537e22008-10-16 08:43:32 +00003867void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868#ifdef DEBUG
3869 int original_height = frame_->height();
3870#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003872
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003874 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003875 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003876 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877}
3878
3879
ager@chromium.org7c537e22008-10-16 08:43:32 +00003880void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881#ifdef DEBUG
3882 int original_height = frame_->height();
3883#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884 Comment cmnt(masm_, "[ Call");
3885
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003886 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 ZoneList<Expression*>* args = node->arguments();
3888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 Variable* var = function->AsVariableProxy()->AsVariable();
3892 Property* property = function->AsProperty();
3893
3894 // ------------------------------------------------------------------------
3895 // Fast-case: Use inline caching.
3896 // ---
3897 // According to ECMA-262, section 11.2.3, page 44, the function to call
3898 // must be resolved after the arguments have been evaluated. The IC code
3899 // automatically handles this by loading the arguments before the function
3900 // is resolved in cache misses (this also holds for megamorphic calls).
3901 // ------------------------------------------------------------------------
3902
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003903 if (var != NULL && var->is_possibly_eval()) {
3904 // ----------------------------------
3905 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3906 // ----------------------------------
3907
3908 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3909 // resolve the function we need to call and the receiver of the
3910 // call. Then we call the resolved function using the given
3911 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003912
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003913 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003914 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003915
3916 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003917 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003918
3919 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003920 int arg_count = args->length();
3921 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003922 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003923 }
3924
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003925 VirtualFrame::SpilledScope spilled_scope(frame_);
3926
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003927 // If we know that eval can only be shadowed by eval-introduced
3928 // variables we attempt to load the global eval function directly
3929 // in generated code. If we succeed, there is no need to perform a
3930 // context lookup in the runtime system.
3931 JumpTarget done;
3932 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3933 ASSERT(var->slot()->type() == Slot::LOOKUP);
3934 JumpTarget slow;
3935 // Prepare the stack for the call to
3936 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3937 // function, the first argument to the eval call and the
3938 // receiver.
3939 LoadFromGlobalSlotCheckExtensions(var->slot(),
3940 NOT_INSIDE_TYPEOF,
3941 &slow);
3942 frame_->EmitPush(r0);
3943 if (arg_count > 0) {
3944 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3945 frame_->EmitPush(r1);
3946 } else {
3947 frame_->EmitPush(r2);
3948 }
3949 __ ldr(r1, frame_->Receiver());
3950 frame_->EmitPush(r1);
3951
3952 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3953
3954 done.Jump();
3955 slow.Bind();
3956 }
3957
3958 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3959 // pushing the loaded function, the first argument to the eval
3960 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003961 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3962 frame_->EmitPush(r1);
3963 if (arg_count > 0) {
3964 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3965 frame_->EmitPush(r1);
3966 } else {
3967 frame_->EmitPush(r2);
3968 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003969 __ ldr(r1, frame_->Receiver());
3970 frame_->EmitPush(r1);
3971
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003972 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003973 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003974
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003975 // If we generated fast-case code bind the jump-target where fast
3976 // and slow case merge.
3977 if (done.is_linked()) done.Bind();
3978
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003979 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003980 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003981 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3982
3983 // Call the function.
3984 CodeForSourcePosition(node->position());
3985
3986 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003987 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003988 frame_->CallStub(&call_function, arg_count + 1);
3989
3990 __ ldr(cp, frame_->Context());
3991 // Remove the function from the stack.
3992 frame_->Drop();
3993 frame_->EmitPush(r0);
3994
3995 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996 // ----------------------------------
3997 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3998 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003999 // Pass the global object as the receiver and let the IC stub
4000 // patch the stack to use the global proxy as 'this' in the
4001 // invoked function.
4002 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003
4004 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004005 int arg_count = args->length();
4006 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004007 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004008 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004009
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004010 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004011 // Setup the name register and call the IC initialization code.
4012 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004013 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4014 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004015 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004016 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4017 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004018 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020
4021 } else if (var != NULL && var->slot() != NULL &&
4022 var->slot()->type() == Slot::LOOKUP) {
4023 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004024 // JavaScript examples:
4025 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004026 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004027 //
4028 // function f() {};
4029 // function g() {
4030 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004031 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004032 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 // ----------------------------------
4034
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004035 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004036
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004037 // Generate fast case for loading functions from slots that
4038 // correspond to local/global variables or arguments unless they
4039 // are shadowed by eval-introduced bindings.
4040 EmitDynamicLoadFromSlotFastCase(var->slot(),
4041 NOT_INSIDE_TYPEOF,
4042 &slow,
4043 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004044
4045 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004047 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004048 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004049 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050 // r0: slot value; r1: receiver
4051
4052 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004053 frame_->EmitPush(r0); // function
4054 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004055
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004056 // If fast case code has been generated, emit code to push the
4057 // function and receiver and have the slow path jump around this
4058 // code.
4059 if (done.is_linked()) {
4060 JumpTarget call;
4061 call.Jump();
4062 done.Bind();
4063 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004064 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004065 call.Bind();
4066 }
4067
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004068 // Call the function. At this point, everything is spilled but the
4069 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004070 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004071 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004072
4073 } else if (property != NULL) {
4074 // Check if the key is a literal string.
4075 Literal* literal = property->key()->AsLiteral();
4076
4077 if (literal != NULL && literal->handle()->IsSymbol()) {
4078 // ------------------------------------------------------------------
4079 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4080 // ------------------------------------------------------------------
4081
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004082 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004084 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4085 name->IsEqualTo(CStrVector("apply")) &&
4086 args->length() == 2 &&
4087 args->at(1)->AsVariableProxy() != NULL &&
4088 args->at(1)->AsVariableProxy()->IsArguments()) {
4089 // Use the optimized Function.prototype.apply that avoids
4090 // allocating lazily allocated arguments objects.
4091 CallApplyLazy(property->obj(),
4092 args->at(0),
4093 args->at(1)->AsVariableProxy(),
4094 node->position());
4095
4096 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004097 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004098 // Load the arguments.
4099 int arg_count = args->length();
4100 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004101 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004102 }
4103
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004104 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004105 // Set the name register and call the IC initialization code.
4106 __ mov(r2, Operand(name));
4107 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4108 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4109 CodeForSourcePosition(node->position());
4110 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4111 __ ldr(cp, frame_->Context());
4112 frame_->EmitPush(r0);
4113 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114
4115 } else {
4116 // -------------------------------------------
4117 // JavaScript example: 'array[index](1, 2, 3)'
4118 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004119 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004120 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004121 Load(property->key());
4122 EmitKeyedLoad();
4123 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004124 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004125 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004126 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004127 // Call the function.
4128 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4129 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004130 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004131 // Load the arguments.
4132 int arg_count = args->length();
4133 for (int i = 0; i < arg_count; i++) {
4134 Load(args->at(i));
4135 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004136
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004137 // Set the name register and call the IC initialization code.
4138 Load(property->key());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004139 frame_->SpillAll();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004140 frame_->EmitPop(r2); // Function name.
4141
4142 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4143 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4144 CodeForSourcePosition(node->position());
4145 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4146 __ ldr(cp, frame_->Context());
4147 frame_->EmitPush(r0);
4148 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004149 }
4150
4151 } else {
4152 // ----------------------------------
4153 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4154 // ----------------------------------
4155
4156 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004157 Load(function);
4158
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004159 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004160 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004162 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004163 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004164 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004165 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004166 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167}
4168
4169
ager@chromium.org7c537e22008-10-16 08:43:32 +00004170void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004171#ifdef DEBUG
4172 int original_height = frame_->height();
4173#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004174 Comment cmnt(masm_, "[ CallNew");
4175
4176 // According to ECMA-262, section 11.2.2, page 44, the function
4177 // expression in new calls must be evaluated before the
4178 // arguments. This is different from ordinary calls, where the
4179 // actual function to call is resolved after the arguments have been
4180 // evaluated.
4181
4182 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004183 // receiver. There is no need to use the global proxy here because
4184 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004185 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004186 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004187
4188 // Push the arguments ("left-to-right") on the stack.
4189 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004190 int arg_count = args->length();
4191 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004192 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004193 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004194
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004195 VirtualFrame::SpilledScope spilled_scope(frame_);
4196
mads.s.ager31e71382008-08-13 09:32:07 +00004197 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004198 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004199 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004200 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004201
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004202 // Call the construct call builtin that handles allocation and
4203 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004204 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004205 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004206 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004207
4208 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004209 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004210 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004211}
4212
4213
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004214void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004215 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004216 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004217
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004218 // Load the object into register.
4219 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004220 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004221 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004222
4223 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004224 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004225 null.Branch(eq);
4226
4227 // Check that the object is a JS object but take special care of JS
4228 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004229 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004230 null.Branch(lt);
4231
4232 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4233 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4234 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004235 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4236 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004237 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004238 function.Branch(eq);
4239
4240 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004241 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4242 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004243 non_function_constructor.Branch(ne);
4244
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004245 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004246 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004247 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4248 __ ldr(tos,
4249 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4250 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004251 leave.Jump();
4252
4253 // Functions have class 'Function'.
4254 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004255 __ mov(tos, Operand(Factory::function_class_symbol()));
4256 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004257 leave.Jump();
4258
4259 // Objects with a non-function constructor have class 'Object'.
4260 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004261 __ mov(tos, Operand(Factory::Object_symbol()));
4262 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004263 leave.Jump();
4264
4265 // Non-JS objects have class null.
4266 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004267 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4268 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004269
4270 // All done.
4271 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004272}
4273
4274
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004276 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004277 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004278
4279 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004280 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004281 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004282 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004283 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004284 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004285 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004286 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004287 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004288 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004289 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004290 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004291 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292}
4293
4294
ager@chromium.org7c537e22008-10-16 08:43:32 +00004295void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004296 Register scratch1 = VirtualFrame::scratch0();
4297 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004298 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004299
4300 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004301 Load(args->at(0)); // Load the object.
4302 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004303 Register value = frame_->PopToRegister();
4304 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004305 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004306 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004307 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004308 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004309 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004310 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004311 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004312 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004314 __ RecordWrite(object,
4315 Operand(JSValue::kValueOffset - kHeapObjectTag),
4316 scratch1,
4317 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004318 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004319 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004320 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004321}
4322
4323
ager@chromium.org7c537e22008-10-16 08:43:32 +00004324void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004325 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004326 Load(args->at(0));
4327 Register reg = frame_->PopToRegister();
4328 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004329 cc_reg_ = eq;
4330}
4331
4332
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004333void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4334 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4335 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004336#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004337 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004338 Load(args->at(1));
4339 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004340 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004341 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004342#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004343 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004344}
4345
4346
ager@chromium.org7c537e22008-10-16 08:43:32 +00004347void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004348 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004349 Load(args->at(0));
4350 Register reg = frame_->PopToRegister();
4351 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004352 cc_reg_ = eq;
4353}
4354
4355
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004356// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004357void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4358 ASSERT(args->length() == 2);
4359 Load(args->at(0));
4360 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004361
4362 if (!CpuFeatures::IsSupported(VFP3)) {
4363 frame_->CallRuntime(Runtime::kMath_pow, 2);
4364 frame_->EmitPush(r0);
4365 } else {
4366 CpuFeatures::Scope scope(VFP3);
4367 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004368 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004369
4370 Register scratch1 = VirtualFrame::scratch0();
4371 Register scratch2 = VirtualFrame::scratch1();
4372
4373 // Get base and exponent to registers.
4374 Register exponent = frame_->PopToRegister();
4375 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004376 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004377
4378 // Set the frame for the runtime jump target. The code below jumps to the
4379 // jump target label so the frame needs to be established before that.
4380 ASSERT(runtime.entry_frame() == NULL);
4381 runtime.set_entry_frame(frame_);
4382
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004383 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4384 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004385
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004386 heap_number_map = r6;
4387 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4388
4389 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4390 // d1.
4391 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4392 __ b(&powi);
4393
4394 __ bind(&base_nonsmi);
4395 // Exponent is smi and base is non smi. Get the double value from the base
4396 // into vfp register d1.
4397 __ ObjectToDoubleVFPRegister(base, d1,
4398 scratch1, scratch2, heap_number_map, s0,
4399 runtime.entry_label());
4400
4401 __ bind(&powi);
4402
4403 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004404 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004405
4406 // Get the absolute untagged value of the exponent and use that for the
4407 // calculation.
4408 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4409 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4410 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4411
4412 // Run through all the bits in the exponent. The result is calculated in d0
4413 // and d1 holds base^(bit^2).
4414 Label more_bits;
4415 __ bind(&more_bits);
4416 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4417 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4418 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4419 __ b(ne, &more_bits);
4420
4421 // If exponent is positive we are done.
4422 __ cmp(exponent, Operand(0));
4423 __ b(ge, &allocate_return);
4424
4425 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4426 // case). However if d0 has reached infinity this will not provide the
4427 // correct result, so call runtime if that is the case.
4428 __ mov(scratch2, Operand(0x7FF00000));
4429 __ mov(scratch1, Operand(0));
4430 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4431 __ vcmp(d0, d1);
4432 __ vmrs(pc);
4433 runtime.Branch(eq); // d0 reached infinity.
4434 __ vdiv(d0, d2, d0);
4435 __ b(&allocate_return);
4436
4437 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004438 // Special handling of raising to the power of -0.5 and 0.5. First check
4439 // that the value is a heap number and that the lower bits (which for both
4440 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004441 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004442 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4443 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4444 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4445 __ cmp(scratch1, heap_number_map);
4446 runtime.Branch(ne);
4447 __ tst(scratch2, scratch2);
4448 runtime.Branch(ne);
4449
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004450 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004451 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4452
4453 // Compare exponent with -0.5.
4454 __ cmp(scratch1, Operand(0xbfe00000));
4455 __ b(ne, &not_minus_half);
4456
4457 // Get the double value from the base into vfp register d0.
4458 __ ObjectToDoubleVFPRegister(base, d0,
4459 scratch1, scratch2, heap_number_map, s0,
4460 runtime.entry_label(),
4461 AVOID_NANS_AND_INFINITIES);
4462
4463 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004464 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004465
4466 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4467 __ vdiv(d0, d2, d0);
4468 __ vsqrt(d0, d0);
4469
4470 __ b(&allocate_return);
4471
4472 __ bind(&not_minus_half);
4473 // Compare exponent with 0.5.
4474 __ cmp(scratch1, Operand(0x3fe00000));
4475 runtime.Branch(ne);
4476
4477 // Get the double value from the base into vfp register d0.
4478 __ ObjectToDoubleVFPRegister(base, d0,
4479 scratch1, scratch2, heap_number_map, s0,
4480 runtime.entry_label(),
4481 AVOID_NANS_AND_INFINITIES);
4482 __ vsqrt(d0, d0);
4483
4484 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004485 Register scratch3 = r5;
4486 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4487 heap_number_map, runtime.entry_label());
4488 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004489 done.Jump();
4490
4491 runtime.Bind();
4492
4493 // Push back the arguments again for the runtime call.
4494 frame_->EmitPush(base);
4495 frame_->EmitPush(exponent);
4496 frame_->CallRuntime(Runtime::kMath_pow, 2);
4497 __ Move(base, r0);
4498
4499 done.Bind();
4500 frame_->EmitPush(base);
4501 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004502}
4503
4504
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004505// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004506void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4507 ASSERT(args->length() == 1);
4508 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004509
4510 if (!CpuFeatures::IsSupported(VFP3)) {
4511 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4512 frame_->EmitPush(r0);
4513 } else {
4514 CpuFeatures::Scope scope(VFP3);
4515 JumpTarget runtime, done;
4516
4517 Register scratch1 = VirtualFrame::scratch0();
4518 Register scratch2 = VirtualFrame::scratch1();
4519
4520 // Get the value from the frame.
4521 Register tos = frame_->PopToRegister();
4522
4523 // Set the frame for the runtime jump target. The code below jumps to the
4524 // jump target label so the frame needs to be established before that.
4525 ASSERT(runtime.entry_frame() == NULL);
4526 runtime.set_entry_frame(frame_);
4527
4528 Register heap_number_map = r6;
4529 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4530
4531 // Get the double value from the heap number into vfp register d0.
4532 __ ObjectToDoubleVFPRegister(tos, d0,
4533 scratch1, scratch2, heap_number_map, s0,
4534 runtime.entry_label());
4535
4536 // Calculate the square root of d0 and place result in a heap number object.
4537 __ vsqrt(d0, d0);
4538 __ AllocateHeapNumberWithValue(
4539 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4540 done.Jump();
4541
4542 runtime.Bind();
4543 // Push back the argument again for the runtime call.
4544 frame_->EmitPush(tos);
4545 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4546 __ Move(tos, r0);
4547
4548 done.Bind();
4549 frame_->EmitPush(tos);
4550 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004551}
4552
4553
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004554class DeferredStringCharCodeAt : public DeferredCode {
4555 public:
4556 DeferredStringCharCodeAt(Register object,
4557 Register index,
4558 Register scratch,
4559 Register result)
4560 : result_(result),
4561 char_code_at_generator_(object,
4562 index,
4563 scratch,
4564 result,
4565 &need_conversion_,
4566 &need_conversion_,
4567 &index_out_of_range_,
4568 STRING_INDEX_IS_NUMBER) {}
4569
4570 StringCharCodeAtGenerator* fast_case_generator() {
4571 return &char_code_at_generator_;
4572 }
4573
4574 virtual void Generate() {
4575 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4576 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4577
4578 __ bind(&need_conversion_);
4579 // Move the undefined value into the result register, which will
4580 // trigger conversion.
4581 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4582 __ jmp(exit_label());
4583
4584 __ bind(&index_out_of_range_);
4585 // When the index is out of range, the spec requires us to return
4586 // NaN.
4587 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4588 __ jmp(exit_label());
4589 }
4590
4591 private:
4592 Register result_;
4593
4594 Label need_conversion_;
4595 Label index_out_of_range_;
4596
4597 StringCharCodeAtGenerator char_code_at_generator_;
4598};
4599
4600
4601// This generates code that performs a String.prototype.charCodeAt() call
4602// or returns a smi in order to trigger conversion.
4603void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004604 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004605 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004606
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004607 Load(args->at(0));
4608 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004609
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004610 Register index = frame_->PopToRegister();
4611 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004612
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004613 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004614 Register scratch = VirtualFrame::scratch0();
4615 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004616
4617 DeferredStringCharCodeAt* deferred =
4618 new DeferredStringCharCodeAt(object,
4619 index,
4620 scratch,
4621 result);
4622 deferred->fast_case_generator()->GenerateFast(masm_);
4623 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004624 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004625}
4626
4627
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004628class DeferredStringCharFromCode : public DeferredCode {
4629 public:
4630 DeferredStringCharFromCode(Register code,
4631 Register result)
4632 : char_from_code_generator_(code, result) {}
4633
4634 StringCharFromCodeGenerator* fast_case_generator() {
4635 return &char_from_code_generator_;
4636 }
4637
4638 virtual void Generate() {
4639 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4640 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4641 }
4642
4643 private:
4644 StringCharFromCodeGenerator char_from_code_generator_;
4645};
4646
4647
4648// Generates code for creating a one-char string from a char code.
4649void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004650 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004651 ASSERT(args->length() == 1);
4652
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004653 Load(args->at(0));
4654
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004655 Register result = frame_->GetTOSRegister();
4656 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004657
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004658 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4659 code, result);
4660 deferred->fast_case_generator()->GenerateFast(masm_);
4661 deferred->BindExit();
4662 frame_->EmitPush(result);
4663}
4664
4665
4666class DeferredStringCharAt : public DeferredCode {
4667 public:
4668 DeferredStringCharAt(Register object,
4669 Register index,
4670 Register scratch1,
4671 Register scratch2,
4672 Register result)
4673 : result_(result),
4674 char_at_generator_(object,
4675 index,
4676 scratch1,
4677 scratch2,
4678 result,
4679 &need_conversion_,
4680 &need_conversion_,
4681 &index_out_of_range_,
4682 STRING_INDEX_IS_NUMBER) {}
4683
4684 StringCharAtGenerator* fast_case_generator() {
4685 return &char_at_generator_;
4686 }
4687
4688 virtual void Generate() {
4689 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4690 char_at_generator_.GenerateSlow(masm(), call_helper);
4691
4692 __ bind(&need_conversion_);
4693 // Move smi zero into the result register, which will trigger
4694 // conversion.
4695 __ mov(result_, Operand(Smi::FromInt(0)));
4696 __ jmp(exit_label());
4697
4698 __ bind(&index_out_of_range_);
4699 // When the index is out of range, the spec requires us to return
4700 // the empty string.
4701 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4702 __ jmp(exit_label());
4703 }
4704
4705 private:
4706 Register result_;
4707
4708 Label need_conversion_;
4709 Label index_out_of_range_;
4710
4711 StringCharAtGenerator char_at_generator_;
4712};
4713
4714
4715// This generates code that performs a String.prototype.charAt() call
4716// or returns a smi in order to trigger conversion.
4717void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004718 Comment(masm_, "[ GenerateStringCharAt");
4719 ASSERT(args->length() == 2);
4720
4721 Load(args->at(0));
4722 Load(args->at(1));
4723
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004724 Register index = frame_->PopToRegister();
4725 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004726
4727 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004728 Register scratch1 = VirtualFrame::scratch0();
4729 Register scratch2 = VirtualFrame::scratch1();
4730 // Use r6 without notifying the virtual frame.
4731 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004732
4733 DeferredStringCharAt* deferred =
4734 new DeferredStringCharAt(object,
4735 index,
4736 scratch1,
4737 scratch2,
4738 result);
4739 deferred->fast_case_generator()->GenerateFast(masm_);
4740 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004741 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004742}
4743
4744
ager@chromium.org7c537e22008-10-16 08:43:32 +00004745void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004746 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004747 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004748 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004749 // We need the CC bits to come out as not_equal in the case where the
4750 // object is a smi. This can't be done with the usual test opcode so
4751 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004752 Register possible_array = frame_->PopToRegister();
4753 Register scratch = VirtualFrame::scratch0();
4754 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4755 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004756 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004757 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004758 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004759 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004760 cc_reg_ = eq;
4761}
4762
4763
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004764void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004765 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004766 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004767 JumpTarget answer;
4768 // We need the CC bits to come out as not_equal in the case where the
4769 // object is a smi. This can't be done with the usual test opcode so
4770 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004771 Register possible_regexp = frame_->PopToRegister();
4772 Register scratch = VirtualFrame::scratch0();
4773 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4774 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004775 answer.Branch(ne);
4776 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004777 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004778 answer.Bind();
4779 cc_reg_ = eq;
4780}
4781
4782
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004783void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4784 // This generates a fast version of:
4785 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004786 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004787 Load(args->at(0));
4788 Register possible_object = frame_->PopToRegister();
4789 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004790 false_target()->Branch(eq);
4791
4792 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004793 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004794 true_target()->Branch(eq);
4795
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004796 Register map_reg = VirtualFrame::scratch0();
4797 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004798 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004799 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4800 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004801 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004802
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004803 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4804 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004805 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004806 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004807 cc_reg_ = le;
4808}
4809
4810
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004811void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4812 // This generates a fast version of:
4813 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4814 // typeof(arg) == function).
4815 // It includes undetectable objects (as opposed to IsObject).
4816 ASSERT(args->length() == 1);
4817 Load(args->at(0));
4818 Register value = frame_->PopToRegister();
4819 __ tst(value, Operand(kSmiTagMask));
4820 false_target()->Branch(eq);
4821 // Check that this is an object.
4822 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4823 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4824 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4825 cc_reg_ = ge;
4826}
4827
4828
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004829// Deferred code to check whether the String JavaScript object is safe for using
4830// default value of. This code is called after the bit caching this information
4831// in the map has been checked with the map for the object in the map_result_
4832// register. On return the register map_result_ contains 1 for true and 0 for
4833// false.
4834class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
4835 public:
4836 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
4837 Register map_result,
4838 Register scratch1,
4839 Register scratch2)
4840 : object_(object),
4841 map_result_(map_result),
4842 scratch1_(scratch1),
4843 scratch2_(scratch2) { }
4844
4845 virtual void Generate() {
4846 Label false_result;
4847
4848 // Check that map is loaded as expected.
4849 if (FLAG_debug_code) {
4850 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
4851 __ cmp(map_result_, ip);
4852 __ Assert(eq, "Map not in expected register");
4853 }
4854
4855 // Check for fast case object. Generate false result for slow case object.
4856 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
4857 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4858 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
4859 __ cmp(scratch1_, ip);
4860 __ b(eq, &false_result);
4861
4862 // Look for valueOf symbol in the descriptor array, and indicate false if
4863 // found. The type is not checked, so if it is a transition it is a false
4864 // negative.
4865 __ ldr(map_result_,
4866 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
4867 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
4868 // map_result_: descriptor array
4869 // scratch2_: length of descriptor array
4870 // Calculate the end of the descriptor array.
4871 STATIC_ASSERT(kSmiTag == 0);
4872 STATIC_ASSERT(kSmiTagSize == 1);
4873 STATIC_ASSERT(kPointerSize == 4);
4874 __ add(scratch1_,
4875 map_result_,
4876 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4877 __ add(scratch1_,
4878 scratch1_,
4879 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
4880
4881 // Calculate location of the first key name.
4882 __ add(map_result_,
4883 map_result_,
4884 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
4885 DescriptorArray::kFirstIndex * kPointerSize));
4886 // Loop through all the keys in the descriptor array. If one of these is the
4887 // symbol valueOf the result is false.
4888 Label entry, loop;
4889 // The use of ip to store the valueOf symbol asumes that it is not otherwise
4890 // used in the loop below.
4891 __ mov(ip, Operand(Factory::value_of_symbol()));
4892 __ jmp(&entry);
4893 __ bind(&loop);
4894 __ ldr(scratch2_, MemOperand(map_result_, 0));
4895 __ cmp(scratch2_, ip);
4896 __ b(eq, &false_result);
4897 __ add(map_result_, map_result_, Operand(kPointerSize));
4898 __ bind(&entry);
4899 __ cmp(map_result_, Operand(scratch1_));
4900 __ b(ne, &loop);
4901
4902 // Reload map as register map_result_ was used as temporary above.
4903 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4904
4905 // If a valueOf property is not found on the object check that it's
4906 // prototype is the un-modified String prototype. If not result is false.
4907 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
4908 __ tst(scratch1_, Operand(kSmiTagMask));
4909 __ b(eq, &false_result);
4910 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4911 __ ldr(scratch2_,
4912 CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
4913 __ ldr(scratch2_,
4914 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
4915 __ ldr(scratch2_,
4916 CodeGenerator::ContextOperand(
4917 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
4918 __ cmp(scratch1_, scratch2_);
4919 __ b(ne, &false_result);
4920
4921 // Set the bit in the map to indicate that it has been checked safe for
4922 // default valueOf and set true result.
4923 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4924 __ orr(scratch1_,
4925 scratch1_,
4926 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4927 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4928 __ mov(map_result_, Operand(1));
4929 __ jmp(exit_label());
4930 __ bind(&false_result);
4931 // Set false result.
4932 __ mov(map_result_, Operand(0));
4933 }
4934
4935 private:
4936 Register object_;
4937 Register map_result_;
4938 Register scratch1_;
4939 Register scratch2_;
4940};
4941
4942
4943void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
4944 ZoneList<Expression*>* args) {
4945 ASSERT(args->length() == 1);
4946 Load(args->at(0));
4947 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
4948 if (FLAG_debug_code) {
4949 __ AbortIfSmi(obj);
4950 }
4951
4952 // Check whether this map has already been checked to be safe for default
4953 // valueOf.
4954 Register map_result = VirtualFrame::scratch0();
4955 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
4956 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
4957 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4958 true_target()->Branch(ne);
4959
4960 // We need an additional two scratch registers for the deferred code.
4961 Register scratch1 = VirtualFrame::scratch1();
4962 // Use r6 without notifying the virtual frame.
4963 Register scratch2 = r6;
4964
4965 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
4966 new DeferredIsStringWrapperSafeForDefaultValueOf(
4967 obj, map_result, scratch1, scratch2);
4968 deferred->Branch(eq);
4969 deferred->BindExit();
4970 __ tst(map_result, Operand(map_result));
4971 cc_reg_ = ne;
4972}
4973
4974
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004975void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4976 // This generates a fast version of:
4977 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004978 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004979 Load(args->at(0));
4980 Register possible_function = frame_->PopToRegister();
4981 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004982 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004983 Register map_reg = VirtualFrame::scratch0();
4984 Register scratch = VirtualFrame::scratch1();
4985 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004986 cc_reg_ = eq;
4987}
4988
4989
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004990void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004991 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004992 Load(args->at(0));
4993 Register possible_undetectable = frame_->PopToRegister();
4994 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004995 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004996 Register scratch = VirtualFrame::scratch0();
4997 __ ldr(scratch,
4998 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4999 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
5000 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005001 cc_reg_ = ne;
5002}
5003
5004
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005005void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005006 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005007
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005008 Register scratch0 = VirtualFrame::scratch0();
5009 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005010 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005011 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005012
5013 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005014 __ ldr(scratch1,
5015 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5016 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5017 __ ldr(scratch0,
5018 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005019
5020 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005021 __ ldr(scratch1,
5022 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5023 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005024 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005025}
5026
5027
ager@chromium.org7c537e22008-10-16 08:43:32 +00005028void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005029 ASSERT(args->length() == 0);
5030
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005031 Register tos = frame_->GetTOSRegister();
5032 Register scratch0 = VirtualFrame::scratch0();
5033 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005034
lrn@chromium.org25156de2010-04-06 13:10:27 +00005035 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005036 __ ldr(scratch0,
5037 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5038 __ ldr(scratch1,
5039 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5040 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5041
5042 // Get the number of formal parameters.
5043 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005044
5045 // Arguments adaptor case: Read the arguments length from the
5046 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005047 __ ldr(tos,
5048 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5049 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005050
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005051 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052}
5053
5054
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005055void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005056 ASSERT(args->length() == 1);
5057
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005058 // Satisfy contract with ArgumentsAccessStub:
5059 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005060 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005061 frame_->PopToR1();
5062 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005063 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005064
5065 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005066 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005067 frame_->CallStub(&stub, 0);
5068 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005069}
5070
5071
ager@chromium.org357bf652010-04-12 11:30:10 +00005072void CodeGenerator::GenerateRandomHeapNumber(
5073 ZoneList<Expression*>* args) {
5074 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005075 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005076
5077 Label slow_allocate_heapnumber;
5078 Label heapnumber_allocated;
5079
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005080 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5081 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005082 __ jmp(&heapnumber_allocated);
5083
5084 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005085 // Allocate a heap number.
5086 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005087 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005088
5089 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005090
5091 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5092 // by computing:
5093 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5094 if (CpuFeatures::IsSupported(VFP3)) {
5095 __ PrepareCallCFunction(0, r1);
5096 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5097
5098 CpuFeatures::Scope scope(VFP3);
5099 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5100 // Create this constant using mov/orr to avoid PC relative load.
5101 __ mov(r1, Operand(0x41000000));
5102 __ orr(r1, r1, Operand(0x300000));
5103 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5104 __ vmov(d7, r0, r1);
5105 // Move 0x4130000000000000 to VFP.
5106 __ mov(r0, Operand(0));
5107 __ vmov(d8, r0, r1);
5108 // Subtract and store the result in the heap number.
5109 __ vsub(d7, d7, d8);
5110 __ sub(r0, r4, Operand(kHeapObjectTag));
5111 __ vstr(d7, r0, HeapNumber::kValueOffset);
5112 frame_->EmitPush(r4);
5113 } else {
5114 __ mov(r0, Operand(r4));
5115 __ PrepareCallCFunction(1, r1);
5116 __ CallCFunction(
5117 ExternalReference::fill_heap_number_with_random_function(), 1);
5118 frame_->EmitPush(r0);
5119 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005120}
5121
5122
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005123void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5124 ASSERT_EQ(2, args->length());
5125
5126 Load(args->at(0));
5127 Load(args->at(1));
5128
ager@chromium.org5c838252010-02-19 08:53:10 +00005129 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005130 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005131 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005132 frame_->EmitPush(r0);
5133}
5134
5135
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005136void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5137 ASSERT_EQ(3, args->length());
5138
5139 Load(args->at(0));
5140 Load(args->at(1));
5141 Load(args->at(2));
5142
ager@chromium.org5c838252010-02-19 08:53:10 +00005143 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005144 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005145 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005146 frame_->EmitPush(r0);
5147}
5148
5149
5150void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5151 ASSERT_EQ(2, args->length());
5152
5153 Load(args->at(0));
5154 Load(args->at(1));
5155
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005156 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005157 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005158 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005159 frame_->EmitPush(r0);
5160}
5161
5162
5163void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5164 ASSERT_EQ(4, args->length());
5165
5166 Load(args->at(0));
5167 Load(args->at(1));
5168 Load(args->at(2));
5169 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005170 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005171 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005172 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005173 frame_->EmitPush(r0);
5174}
5175
5176
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005177void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5178 // No stub. This code only occurs a few times in regexp.js.
5179 const int kMaxInlineLength = 100;
5180 ASSERT_EQ(3, args->length());
5181 Load(args->at(0)); // Size of array, smi.
5182 Load(args->at(1)); // "index" property value.
5183 Load(args->at(2)); // "input" property value.
5184 {
5185 VirtualFrame::SpilledScope spilled_scope(frame_);
5186 Label slowcase;
5187 Label done;
5188 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5189 STATIC_ASSERT(kSmiTag == 0);
5190 STATIC_ASSERT(kSmiTagSize == 1);
5191 __ tst(r1, Operand(kSmiTagMask));
5192 __ b(ne, &slowcase);
5193 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5194 __ b(hi, &slowcase);
5195 // Smi-tagging is equivalent to multiplying by 2.
5196 // Allocate RegExpResult followed by FixedArray with size in ebx.
5197 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5198 // Elements: [Map][Length][..elements..]
5199 // Size of JSArray with two in-object properties and the header of a
5200 // FixedArray.
5201 int objects_size =
5202 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5203 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5204 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005205 __ AllocateInNewSpace(
5206 r2, // In: Size, in words.
5207 r0, // Out: Start of allocation (tagged).
5208 r3, // Scratch register.
5209 r4, // Scratch register.
5210 &slowcase,
5211 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005212 // r0: Start of allocated area, object-tagged.
5213 // r1: Number of elements in array, as smi.
5214 // r5: Number of elements, untagged.
5215
5216 // Set JSArray map to global.regexp_result_map().
5217 // Set empty properties FixedArray.
5218 // Set elements to point to FixedArray allocated right after the JSArray.
5219 // Interleave operations for better latency.
5220 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5221 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5222 __ mov(r4, Operand(Factory::empty_fixed_array()));
5223 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5224 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5225 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5226 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5227 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5228
5229 // Set input, index and length fields from arguments.
5230 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5231 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5232 __ add(sp, sp, Operand(kPointerSize));
5233 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5234 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5235
5236 // Fill out the elements FixedArray.
5237 // r0: JSArray, tagged.
5238 // r3: FixedArray, tagged.
5239 // r5: Number of elements in array, untagged.
5240
5241 // Set map.
5242 __ mov(r2, Operand(Factory::fixed_array_map()));
5243 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5244 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005245 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5246 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005247 // Fill contents of fixed-array with the-hole.
5248 __ mov(r2, Operand(Factory::the_hole_value()));
5249 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5250 // Fill fixed array elements with hole.
5251 // r0: JSArray, tagged.
5252 // r2: the hole.
5253 // r3: Start of elements in FixedArray.
5254 // r5: Number of elements to fill.
5255 Label loop;
5256 __ tst(r5, Operand(r5));
5257 __ bind(&loop);
5258 __ b(le, &done); // Jump if r1 is negative or zero.
5259 __ sub(r5, r5, Operand(1), SetCC);
5260 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5261 __ jmp(&loop);
5262
5263 __ bind(&slowcase);
5264 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5265
5266 __ bind(&done);
5267 }
5268 frame_->Forget(3);
5269 frame_->EmitPush(r0);
5270}
5271
5272
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005273void CodeGenerator::GenerateRegExpCloneResult(ZoneList<Expression*>* args) {
5274 ASSERT_EQ(1, args->length());
5275
5276 Load(args->at(0));
5277 frame_->PopToR0();
5278 {
5279 VirtualFrame::SpilledScope spilled_scope(frame_);
5280
5281 Label done;
5282 Label call_runtime;
5283 __ BranchOnSmi(r0, &done);
5284
5285 // Load JSRegExp map into r1. Check that argument object has this map.
5286 // Arguments to this function should be results of calling RegExp exec,
5287 // which is either an unmodified JSRegExpResult or null. Anything not having
5288 // the unmodified JSRegExpResult map is returned unmodified.
5289 // This also ensures that elements are fast.
5290
5291 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
5292 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalContextOffset));
5293 __ ldr(r1, ContextOperand(r1, Context::REGEXP_RESULT_MAP_INDEX));
5294 __ ldr(ip, FieldMemOperand(r0, HeapObject::kMapOffset));
5295 __ cmp(r1, Operand(ip));
5296 __ b(ne, &done);
5297
5298 // All set, copy the contents to a new object.
5299 __ AllocateInNewSpace(JSRegExpResult::kSize,
5300 r2,
5301 r3,
5302 r4,
5303 &call_runtime,
5304 NO_ALLOCATION_FLAGS);
5305 // Store RegExpResult map as map of allocated object.
5306 ASSERT(JSRegExpResult::kSize == 6 * kPointerSize);
5307 // Copy all fields (map is already in r1) from (untagged) r0 to r2.
5308 // Change map of elements array (ends up in r4) to be a FixedCOWArray.
5309 __ bic(r0, r0, Operand(kHeapObjectTagMask));
5310 __ ldm(ib, r0, r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
5311 __ stm(ia, r2,
5312 r1.bit() | r3.bit() | r4.bit() | r5.bit() | r6.bit() | r7.bit());
5313 ASSERT(!Heap::InNewSpace(Heap::fixed_cow_array_map()));
5314 ASSERT(JSRegExp::kElementsOffset == 2 * kPointerSize);
5315 // Check whether elements array is empty fixed array, and otherwise make
5316 // it copy-on-write (it never should be empty unless someone is messing
5317 // with the arguments to the runtime function).
5318 __ LoadRoot(ip, Heap::kEmptyFixedArrayRootIndex);
5319 __ add(r0, r2, Operand(kHeapObjectTag)); // Tag result and move it to r0.
5320 __ cmp(r4, ip);
5321 __ b(eq, &done);
5322 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
5323 __ str(ip, FieldMemOperand(r4, HeapObject::kMapOffset));
5324 __ b(&done);
5325 __ bind(&call_runtime);
5326 __ push(r0);
5327 __ CallRuntime(Runtime::kRegExpCloneResult, 1);
5328 __ bind(&done);
5329 }
5330 frame_->EmitPush(r0);
5331}
5332
5333
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005334class DeferredSearchCache: public DeferredCode {
5335 public:
5336 DeferredSearchCache(Register dst, Register cache, Register key)
5337 : dst_(dst), cache_(cache), key_(key) {
5338 set_comment("[ DeferredSearchCache");
5339 }
5340
5341 virtual void Generate();
5342
5343 private:
5344 Register dst_, cache_, key_;
5345};
5346
5347
5348void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005349 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005350 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005351 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005352}
5353
5354
5355void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5356 ASSERT_EQ(2, args->length());
5357
5358 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5359 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5360
5361 Handle<FixedArray> jsfunction_result_caches(
5362 Top::global_context()->jsfunction_result_caches());
5363 if (jsfunction_result_caches->length() <= cache_id) {
5364 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005365 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005366 return;
5367 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005368
5369 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005370
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005371 frame_->PopToR1();
5372 frame_->SpillAll();
5373 Register key = r1; // Just poped to r1
5374 Register result = r0; // Free, as frame has just been spilled.
5375 Register scratch1 = VirtualFrame::scratch0();
5376 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005377
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005378 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5379 __ ldr(scratch1,
5380 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5381 __ ldr(scratch1,
5382 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5383 __ ldr(scratch1,
5384 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005385
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005386 DeferredSearchCache* deferred =
5387 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005388
5389 const int kFingerOffset =
5390 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005391 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005392 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5393 // result now holds finger offset as a smi.
5394 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5395 // scratch2 now points to the start of fixed array elements.
5396 __ ldr(result,
5397 MemOperand(
5398 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5399 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5400 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005401 deferred->Branch(ne);
5402
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005403 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005404
5405 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005406 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005407}
5408
5409
ager@chromium.org5c838252010-02-19 08:53:10 +00005410void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5411 ASSERT_EQ(args->length(), 1);
5412
5413 // Load the argument on the stack and jump to the runtime.
5414 Load(args->at(0));
5415
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005416 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005417 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005418 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005419 frame_->EmitPush(r0);
5420}
5421
5422
ager@chromium.orgac091b72010-05-05 07:34:42 +00005423class DeferredSwapElements: public DeferredCode {
5424 public:
5425 DeferredSwapElements(Register object, Register index1, Register index2)
5426 : object_(object), index1_(index1), index2_(index2) {
5427 set_comment("[ DeferredSwapElements");
5428 }
5429
5430 virtual void Generate();
5431
5432 private:
5433 Register object_, index1_, index2_;
5434};
5435
5436
5437void DeferredSwapElements::Generate() {
5438 __ push(object_);
5439 __ push(index1_);
5440 __ push(index2_);
5441 __ CallRuntime(Runtime::kSwapElements, 3);
5442}
5443
5444
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005445void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5446 Comment cmnt(masm_, "[ GenerateSwapElements");
5447
5448 ASSERT_EQ(3, args->length());
5449
5450 Load(args->at(0));
5451 Load(args->at(1));
5452 Load(args->at(2));
5453
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005454 VirtualFrame::SpilledScope spilled_scope(frame_);
5455
ager@chromium.orgac091b72010-05-05 07:34:42 +00005456 Register index2 = r2;
5457 Register index1 = r1;
5458 Register object = r0;
5459 Register tmp1 = r3;
5460 Register tmp2 = r4;
5461
5462 frame_->EmitPop(index2);
5463 frame_->EmitPop(index1);
5464 frame_->EmitPop(object);
5465
5466 DeferredSwapElements* deferred =
5467 new DeferredSwapElements(object, index1, index2);
5468
5469 // Fetch the map and check if array is in fast case.
5470 // Check that object doesn't require security checks and
5471 // has no indexed interceptor.
5472 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5473 deferred->Branch(lt);
5474 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5475 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5476 deferred->Branch(nz);
5477
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005478 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005479 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5480 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5481 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5482 __ cmp(tmp2, ip);
5483 deferred->Branch(ne);
5484
5485 // Smi-tagging is equivalent to multiplying by 2.
5486 STATIC_ASSERT(kSmiTag == 0);
5487 STATIC_ASSERT(kSmiTagSize == 1);
5488
5489 // Check that both indices are smis.
5490 __ mov(tmp2, index1);
5491 __ orr(tmp2, tmp2, index2);
5492 __ tst(tmp2, Operand(kSmiTagMask));
5493 deferred->Branch(nz);
5494
5495 // Bring the offsets into the fixed array in tmp1 into index1 and
5496 // index2.
5497 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5498 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5499 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5500
5501 // Swap elements.
5502 Register tmp3 = object;
5503 object = no_reg;
5504 __ ldr(tmp3, MemOperand(tmp1, index1));
5505 __ ldr(tmp2, MemOperand(tmp1, index2));
5506 __ str(tmp3, MemOperand(tmp1, index2));
5507 __ str(tmp2, MemOperand(tmp1, index1));
5508
5509 Label done;
5510 __ InNewSpace(tmp1, tmp2, eq, &done);
5511 // Possible optimization: do a check that both values are Smis
5512 // (or them and test against Smi mask.)
5513
5514 __ mov(tmp2, tmp1);
5515 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5516 __ CallStub(&recordWrite1);
5517
5518 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5519 __ CallStub(&recordWrite2);
5520
5521 __ bind(&done);
5522
5523 deferred->BindExit();
5524 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5525 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005526}
5527
5528
ager@chromium.org357bf652010-04-12 11:30:10 +00005529void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5530 Comment cmnt(masm_, "[ GenerateCallFunction");
5531
5532 ASSERT(args->length() >= 2);
5533
5534 int n_args = args->length() - 2; // for receiver and function.
5535 Load(args->at(0)); // receiver
5536 for (int i = 0; i < n_args; i++) {
5537 Load(args->at(i + 1));
5538 }
5539 Load(args->at(n_args + 1)); // function
5540 frame_->CallJSFunction(n_args);
5541 frame_->EmitPush(r0);
5542}
5543
5544
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005545void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5546 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005547 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005548 if (CpuFeatures::IsSupported(VFP3)) {
5549 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5550 frame_->SpillAllButCopyTOSToR0();
5551 frame_->CallStub(&stub, 1);
5552 } else {
5553 frame_->CallRuntime(Runtime::kMath_sin, 1);
5554 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005555 frame_->EmitPush(r0);
5556}
5557
5558
5559void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5560 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005561 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005562 if (CpuFeatures::IsSupported(VFP3)) {
5563 TranscendentalCacheStub stub(TranscendentalCache::COS);
5564 frame_->SpillAllButCopyTOSToR0();
5565 frame_->CallStub(&stub, 1);
5566 } else {
5567 frame_->CallRuntime(Runtime::kMath_cos, 1);
5568 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005569 frame_->EmitPush(r0);
5570}
5571
5572
ager@chromium.org7c537e22008-10-16 08:43:32 +00005573void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005574 ASSERT(args->length() == 2);
5575
5576 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005577 Load(args->at(0));
5578 Load(args->at(1));
5579 Register lhs = frame_->PopToRegister();
5580 Register rhs = frame_->PopToRegister(lhs);
5581 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005582 cc_reg_ = eq;
5583}
5584
5585
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005586void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5587 ASSERT(args->length() == 2);
5588
5589 // Load the two objects into registers and perform the comparison.
5590 Load(args->at(0));
5591 Load(args->at(1));
5592 Register right = frame_->PopToRegister();
5593 Register left = frame_->PopToRegister(right);
5594 Register tmp = frame_->scratch0();
5595 Register tmp2 = frame_->scratch1();
5596
5597 // Jumps to done must have the eq flag set if the test is successful
5598 // and clear if the test has failed.
5599 Label done;
5600
5601 // Fail if either is a non-HeapObject.
5602 __ cmp(left, Operand(right));
5603 __ b(eq, &done);
5604 __ and_(tmp, left, Operand(right));
5605 __ eor(tmp, tmp, Operand(kSmiTagMask));
5606 __ tst(tmp, Operand(kSmiTagMask));
5607 __ b(ne, &done);
5608 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5609 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5610 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5611 __ b(ne, &done);
5612 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5613 __ cmp(tmp, Operand(tmp2));
5614 __ b(ne, &done);
5615 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5616 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5617 __ cmp(tmp, tmp2);
5618 __ bind(&done);
5619 cc_reg_ = eq;
5620}
5621
5622
5623
ager@chromium.org7c537e22008-10-16 08:43:32 +00005624void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005625#ifdef DEBUG
5626 int original_height = frame_->height();
5627#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005628 if (CheckForInlineRuntimeCall(node)) {
5629 ASSERT((has_cc() && frame_->height() == original_height) ||
5630 (!has_cc() && frame_->height() == original_height + 1));
5631 return;
5632 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005633
5634 ZoneList<Expression*>* args = node->arguments();
5635 Comment cmnt(masm_, "[ CallRuntime");
5636 Runtime::Function* function = node->function();
5637
ager@chromium.org41826e72009-03-30 13:30:57 +00005638 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005639 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005640 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005641 Register scratch = VirtualFrame::scratch0();
5642 __ ldr(scratch, GlobalObject());
5643 Register builtins = frame_->GetTOSRegister();
5644 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5645 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005646 }
mads.s.ager31e71382008-08-13 09:32:07 +00005647
ager@chromium.org41826e72009-03-30 13:30:57 +00005648 // Push the arguments ("left-to-right").
5649 int arg_count = args->length();
5650 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005651 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005652 }
mads.s.ager31e71382008-08-13 09:32:07 +00005653
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005654 VirtualFrame::SpilledScope spilled_scope(frame_);
5655
ager@chromium.org41826e72009-03-30 13:30:57 +00005656 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005657 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005658 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005659 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5660 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005661 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005662 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005663 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005664 } else {
5665 // Call the C runtime function.
5666 frame_->CallRuntime(function, arg_count);
5667 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005668 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005669 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005670}
5671
5672
ager@chromium.org7c537e22008-10-16 08:43:32 +00005673void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005674#ifdef DEBUG
5675 int original_height = frame_->height();
5676#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005677 Comment cmnt(masm_, "[ UnaryOperation");
5678
5679 Token::Value op = node->op();
5680
5681 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005682 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005683 // LoadCondition may (and usually does) leave a test and branch to
5684 // be emitted by the caller. In that case, negate the condition.
5685 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686
5687 } else if (op == Token::DELETE) {
5688 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005689 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005690 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005691 Load(property->obj());
5692 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005693 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005694 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005695
mads.s.ager31e71382008-08-13 09:32:07 +00005696 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005697 Slot* slot = variable->slot();
5698 if (variable->is_global()) {
5699 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005700 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005701 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005702 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005703
5704 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5705 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005706 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005707 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005708 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005709 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005710 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005711 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005712 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005713 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005714
mads.s.ager31e71382008-08-13 09:32:07 +00005715 } else {
5716 // Default: Result of deleting non-global, not dynamically
5717 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005718 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005719 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005720
5721 } else {
5722 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005723 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005724 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005725 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005726 }
5727
5728 } else if (op == Token::TYPEOF) {
5729 // Special case for loading the typeof expression; see comment on
5730 // LoadTypeofExpression().
5731 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005732 frame_->CallRuntime(Runtime::kTypeof, 1);
5733 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005734
5735 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005736 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005737 (node->expression()->AsBinaryOperation() != NULL &&
5738 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005739 UnaryOverwriteMode overwrite =
5740 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5741
5742 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005743 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005744 switch (op) {
5745 case Token::NOT:
5746 case Token::DELETE:
5747 case Token::TYPEOF:
5748 UNREACHABLE(); // handled above
5749 break;
5750
5751 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005752 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005753 GenericUnaryOpStub stub(
5754 Token::SUB,
5755 overwrite,
5756 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005757 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005758 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005759 break;
5760 }
5761
5762 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005763 Register tos = frame_->PopToRegister();
5764 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005765 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005766 // Smi check.
5767 __ tst(tos, Operand(kSmiTagMask));
5768 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005769
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005770 __ mvn(tos, Operand(tos));
5771 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5772 frame_->EmitPush(tos);
5773 // The fast case is the first to jump to the continue label, so it gets
5774 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005775 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005776
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005777 not_smi_label.Bind();
5778 frame_->SpillAll();
5779 __ Move(r0, tos);
5780 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5781 frame_->CallStub(&stub, 0);
5782 frame_->EmitPush(r0);
5783
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005784 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005785 break;
5786 }
5787
5788 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005789 frame_->Drop();
5790 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005791 break;
5792
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005793 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005794 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005795 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005796 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005797 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005798 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005799
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005800 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005801 frame_->EmitPush(r0);
5802
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005803 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005804 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005805 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005806 default:
5807 UNREACHABLE();
5808 }
5809 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005810 ASSERT(!has_valid_frame() ||
5811 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005812 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005813}
5814
5815
ager@chromium.org7c537e22008-10-16 08:43:32 +00005816void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005817#ifdef DEBUG
5818 int original_height = frame_->height();
5819#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005821 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005822
5823 bool is_postfix = node->is_postfix();
5824 bool is_increment = node->op() == Token::INC;
5825
5826 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5827 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005828 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005829
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005830 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5831 // The type info declares that this variable is always a Smi. That
5832 // means it is a Smi both before and after the increment/decrement.
5833 // Lets make use of that to make a very minimal count.
5834 Reference target(this, node->expression(), !is_const);
5835 ASSERT(!target.is_illegal());
5836 target.GetValue(); // Pushes the value.
5837 Register value = frame_->PopToRegister();
5838 if (is_postfix) frame_->EmitPush(value);
5839 if (is_increment) {
5840 __ add(value, value, Operand(Smi::FromInt(1)));
5841 } else {
5842 __ sub(value, value, Operand(Smi::FromInt(1)));
5843 }
5844 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005845 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005846 if (is_postfix) frame_->Pop();
5847 ASSERT_EQ(original_height + 1, frame_->height());
5848 return;
5849 }
5850
5851 // If it's a postfix expression and its result is not ignored and the
5852 // reference is non-trivial, then push a placeholder on the stack now
5853 // to hold the result of the expression.
5854 bool placeholder_pushed = false;
5855 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005856 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005857 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005858 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005859
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005860 // A constant reference is not saved to, so a constant reference is not a
5861 // compound assignment reference.
5862 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005863 if (target.is_illegal()) {
5864 // Spoof the virtual frame to have the expected height (one higher
5865 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005866 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005867 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005868 return;
5869 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005870
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005871 // This pushes 0, 1 or 2 words on the object to be used later when updating
5872 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005873 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005874
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005875 JumpTarget slow;
5876 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005877
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005878 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005879
5880 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005881 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005882 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005883 } else if (is_postfix) {
5884 frame_->EmitPush(value);
5885 __ mov(VirtualFrame::scratch0(), value);
5886 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005887 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005888
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005889 // Check for smi operand.
5890 __ tst(value, Operand(kSmiTagMask));
5891 slow.Branch(ne);
5892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005893 // Perform optimistic increment/decrement.
5894 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005895 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005896 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005897 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005898 }
5899
5900 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005901 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005902
5903 // Revert optimistic increment/decrement.
5904 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005905 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005906 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005907 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005908 }
5909
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005910 // Slow case: Convert to number. At this point the
5911 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005912 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005913
5914 // Convert the operand to a number.
5915 frame_->EmitPush(value);
5916
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005917 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005918 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005919 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005920
5921 if (is_postfix) {
5922 // Postfix: store to result (on the stack).
5923 __ str(r0, frame_->ElementAt(target.size()));
5924 }
5925
5926 // Compute the new value.
5927 frame_->EmitPush(r0);
5928 frame_->EmitPush(Operand(Smi::FromInt(1)));
5929 if (is_increment) {
5930 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5931 } else {
5932 frame_->CallRuntime(Runtime::kNumberSub, 2);
5933 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005934 }
5935
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005936 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005937 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005938 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005939 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005940 frame_->EmitPush(value);
5941 // Set the target with the result, leaving the result on
5942 // top of the stack. Removes the target from the stack if
5943 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005944 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005945 }
5946
5947 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005948 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005949 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005950}
5951
5952
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005953void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005954 // According to ECMA-262 section 11.11, page 58, the binary logical
5955 // operators must yield the result of one of the two expressions
5956 // before any ToBoolean() conversions. This means that the value
5957 // produced by a && or || operator is not necessarily a boolean.
5958
5959 // NOTE: If the left hand side produces a materialized value (not in
5960 // the CC register), we force the right hand side to do the
5961 // same. This is necessary because we may have to branch to the exit
5962 // after evaluating the left hand side (due to the shortcut
5963 // semantics), but the compiler must (statically) know if the result
5964 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005965 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005966 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005967 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005968 if (has_valid_frame() && !has_cc()) {
5969 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005970 JumpTarget pop_and_continue;
5971 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005972
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005973 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005974 // Avoid popping the result if it converts to 'false' using the
5975 // standard ToBoolean() conversion as described in ECMA-262,
5976 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005977 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005978 Branch(false, &exit);
5979
5980 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005981 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005982 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005983
5984 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005985 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005986 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005987
5988 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005989 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005990 } else if (has_cc() || is_true.is_linked()) {
5991 // The left-hand side is either (a) partially compiled to
5992 // control flow with a final branch left to emit or (b) fully
5993 // compiled to control flow and possibly true.
5994 if (has_cc()) {
5995 Branch(false, false_target());
5996 }
5997 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005998 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005999 } else {
6000 // Nothing to do.
6001 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006002 }
6003
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006004 } else {
6005 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006006 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006007 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006008 if (has_valid_frame() && !has_cc()) {
6009 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006010 JumpTarget pop_and_continue;
6011 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006012
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006013 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006014 // Avoid popping the result if it converts to 'true' using the
6015 // standard ToBoolean() conversion as described in ECMA-262,
6016 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00006017 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006018 Branch(true, &exit);
6019
6020 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006021 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006022 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006023
6024 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006025 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006026 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006027
6028 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006029 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006030 } else if (has_cc() || is_false.is_linked()) {
6031 // The left-hand side is either (a) partially compiled to
6032 // control flow with a final branch left to emit or (b) fully
6033 // compiled to control flow and possibly false.
6034 if (has_cc()) {
6035 Branch(true, true_target());
6036 }
6037 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006038 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006039 } else {
6040 // Nothing to do.
6041 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006042 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006043 }
6044}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006045
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006046
6047void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
6048#ifdef DEBUG
6049 int original_height = frame_->height();
6050#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00006051 Comment cmnt(masm_, "[ BinaryOperation");
6052
6053 if (node->op() == Token::AND || node->op() == Token::OR) {
6054 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006055 } else {
6056 // Optimize for the case where (at least) one of the expressions
6057 // is a literal small integer.
6058 Literal* lliteral = node->left()->AsLiteral();
6059 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006060 // NOTE: The code below assumes that the slow cases (calls to runtime)
6061 // never return a constant/immutable object.
6062 bool overwrite_left =
6063 (node->left()->AsBinaryOperation() != NULL &&
6064 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
6065 bool overwrite_right =
6066 (node->right()->AsBinaryOperation() != NULL &&
6067 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006068
6069 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006070 VirtualFrame::RegisterAllocationScope scope(this);
6071 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006072 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006073 SmiOperation(node->op(),
6074 rliteral->handle(),
6075 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006076 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006077 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006078 VirtualFrame::RegisterAllocationScope scope(this);
6079 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006080 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006081 SmiOperation(node->op(),
6082 lliteral->handle(),
6083 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006084 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006085 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006086 GenerateInlineSmi inline_smi =
6087 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6088 if (lliteral != NULL) {
6089 ASSERT(!lliteral->handle()->IsSmi());
6090 inline_smi = DONT_GENERATE_INLINE_SMI;
6091 }
6092 if (rliteral != NULL) {
6093 ASSERT(!rliteral->handle()->IsSmi());
6094 inline_smi = DONT_GENERATE_INLINE_SMI;
6095 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006096 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006097 OverwriteMode overwrite_mode = NO_OVERWRITE;
6098 if (overwrite_left) {
6099 overwrite_mode = OVERWRITE_LEFT;
6100 } else if (overwrite_right) {
6101 overwrite_mode = OVERWRITE_RIGHT;
6102 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006103 Load(node->left());
6104 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006105 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006106 }
6107 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006108 ASSERT(!has_valid_frame() ||
6109 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006110 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006111}
6112
6113
ager@chromium.org7c537e22008-10-16 08:43:32 +00006114void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006115#ifdef DEBUG
6116 int original_height = frame_->height();
6117#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006118 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006119 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006120}
6121
6122
ager@chromium.org7c537e22008-10-16 08:43:32 +00006123void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006124#ifdef DEBUG
6125 int original_height = frame_->height();
6126#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006127 Comment cmnt(masm_, "[ CompareOperation");
6128
ager@chromium.org357bf652010-04-12 11:30:10 +00006129 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006131 // Get the expressions from the node.
6132 Expression* left = node->left();
6133 Expression* right = node->right();
6134 Token::Value op = node->op();
6135
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006136 // To make null checks efficient, we check if either left or right is the
6137 // literal 'null'. If so, we optimize the code by inlining a null check
6138 // instead of calling the (very) general runtime routine for checking
6139 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006140 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006141 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006142 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006143 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006144 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
6145 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006146 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006147 Load(left_is_null ? right : left);
6148 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006149 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006150 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006151
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006152 // The 'null' value is only equal to 'undefined' if using non-strict
6153 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006154 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006155 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006156
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006157 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006158 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006159 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006160
ager@chromium.org357bf652010-04-12 11:30:10 +00006161 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006162 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006163
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006164 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006165 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6166 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6167 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6168 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006169 }
6170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006171 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006172 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006173 return;
6174 }
6175 }
6176
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006177 // To make typeof testing for natives implemented in JavaScript really
6178 // efficient, we generate special code for expressions of the form:
6179 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006180 UnaryOperation* operation = left->AsUnaryOperation();
6181 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6182 (operation != NULL && operation->op() == Token::TYPEOF) &&
6183 (right->AsLiteral() != NULL &&
6184 right->AsLiteral()->handle()->IsString())) {
6185 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6186
ager@chromium.org357bf652010-04-12 11:30:10 +00006187 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006188 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006189 Register tos = frame_->PopToRegister();
6190
ager@chromium.org357bf652010-04-12 11:30:10 +00006191 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006192
6193 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006194 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006195 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006196 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006197 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006198 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006199 cc_reg_ = eq;
6200
6201 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006202 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006203 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006204
ager@chromium.org357bf652010-04-12 11:30:10 +00006205 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006206
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006207 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006208 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6209 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6210 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006211 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006212
ager@chromium.org357bf652010-04-12 11:30:10 +00006213 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6214 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006215 cc_reg_ = lt;
6216
6217 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006218 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006219 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006220 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006221 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006222 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006223 cc_reg_ = eq;
6224
6225 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006226 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006227 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006228 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006229
ager@chromium.org357bf652010-04-12 11:30:10 +00006230 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006231 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006232
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006233 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006234 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6235 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6236 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6237 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006238
6239 cc_reg_ = eq;
6240
6241 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006242 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006243 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006244 Register map_reg = scratch;
6245 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006246 true_target()->Branch(eq);
6247 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006248 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006249 cc_reg_ = eq;
6250
6251 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006252 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006253 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006254
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006255 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006256 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006257 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006258
ager@chromium.org357bf652010-04-12 11:30:10 +00006259 Register map_reg = scratch;
6260 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006261 false_target()->Branch(eq);
6262
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006263 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006264 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6265 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6266 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006267 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006268
ager@chromium.org357bf652010-04-12 11:30:10 +00006269 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6270 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006271 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006272 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006273 cc_reg_ = le;
6274
6275 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006276 // Uncommon case: typeof testing against a string literal that is
6277 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006278 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006279 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006280 ASSERT(!has_valid_frame() ||
6281 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006282 return;
6283 }
6284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006285 switch (op) {
6286 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006287 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006288 break;
6289
6290 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006291 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006292 break;
6293
6294 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006295 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006296 break;
6297
6298 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006299 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006300 break;
6301
6302 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006303 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006304 break;
6305
6306 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006307 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006308 break;
6309
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006310 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006311 Load(left);
6312 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006313 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006314 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006315 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006317
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006318 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006319 Load(left);
6320 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006321 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006322 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006323 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006324 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006325 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006326 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006328
6329 default:
6330 UNREACHABLE();
6331 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006332 ASSERT((has_cc() && frame_->height() == original_height) ||
6333 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006334}
6335
6336
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006337class DeferredReferenceGetNamedValue: public DeferredCode {
6338 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006339 explicit DeferredReferenceGetNamedValue(Register receiver,
6340 Handle<String> name)
6341 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006342 set_comment("[ DeferredReferenceGetNamedValue");
6343 }
6344
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006345 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006346
6347 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006348 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006349 Handle<String> name_;
6350};
6351
6352
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006353// Convention for this is that on entry the receiver is in a register that
6354// is not used by the stack. On exit the answer is found in that same
6355// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006356void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006357#ifdef DEBUG
6358 int expected_height = frame_state()->frame()->height();
6359#endif
6360 VirtualFrame copied_frame(*frame_state()->frame());
6361 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006362
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006363 Register scratch1 = VirtualFrame::scratch0();
6364 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006365 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006366 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6367 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6368
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006369 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6370 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006371 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006372
6373 // The rest of the instructions in the deferred code must be together.
6374 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6375 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6376 __ Call(ic, RelocInfo::CODE_TARGET);
6377 // The call must be followed by a nop(1) instruction to indicate that the
6378 // in-object has been inlined.
6379 __ nop(PROPERTY_ACCESS_INLINED);
6380
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006381 // At this point the answer is in r0. We move it to the expected register
6382 // if necessary.
6383 __ Move(receiver_, r0);
6384
6385 // Now go back to the frame that we entered with. This will not overwrite
6386 // the receiver register since that register was not in use when we came
6387 // in. The instructions emitted by this merge are skipped over by the
6388 // inline load patching mechanism when looking for the branch instruction
6389 // that tells it where the code to patch is.
6390 copied_frame.MergeTo(frame_state()->frame());
6391
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006392 // Block the constant pool for one more instruction after leaving this
6393 // constant pool block scope to include the branch instruction ending the
6394 // deferred code.
6395 __ BlockConstPoolFor(1);
6396 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006397 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006398}
6399
6400
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006401class DeferredReferenceGetKeyedValue: public DeferredCode {
6402 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006403 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6404 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006405 set_comment("[ DeferredReferenceGetKeyedValue");
6406 }
6407
6408 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006409
6410 private:
6411 Register key_;
6412 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006413};
6414
6415
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006416// Takes key and register in r0 and r1 or vice versa. Returns result
6417// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006418void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006419 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6420 (key_.is(r1) && receiver_.is(r0)));
6421
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006422 VirtualFrame copied_frame(*frame_state()->frame());
6423 copied_frame.SpillAll();
6424
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006425 Register scratch1 = VirtualFrame::scratch0();
6426 Register scratch2 = VirtualFrame::scratch1();
6427 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6428 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6429
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006430 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6431 // convention.
6432 if (key_.is(r1)) {
6433 __ Swap(r0, r1, ip);
6434 }
6435
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006436 // The rest of the instructions in the deferred code must be together.
6437 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006438 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006439 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6440 __ Call(ic, RelocInfo::CODE_TARGET);
6441 // The call must be followed by a nop instruction to indicate that the
6442 // keyed load has been inlined.
6443 __ nop(PROPERTY_ACCESS_INLINED);
6444
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006445 // Now go back to the frame that we entered with. This will not overwrite
6446 // the receiver or key registers since they were not in use when we came
6447 // in. The instructions emitted by this merge are skipped over by the
6448 // inline load patching mechanism when looking for the branch instruction
6449 // that tells it where the code to patch is.
6450 copied_frame.MergeTo(frame_state()->frame());
6451
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006452 // Block the constant pool for one more instruction after leaving this
6453 // constant pool block scope to include the branch instruction ending the
6454 // deferred code.
6455 __ BlockConstPoolFor(1);
6456 }
6457}
6458
6459
6460class DeferredReferenceSetKeyedValue: public DeferredCode {
6461 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006462 DeferredReferenceSetKeyedValue(Register value,
6463 Register key,
6464 Register receiver)
6465 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006466 set_comment("[ DeferredReferenceSetKeyedValue");
6467 }
6468
6469 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006470
6471 private:
6472 Register value_;
6473 Register key_;
6474 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006475};
6476
6477
6478void DeferredReferenceSetKeyedValue::Generate() {
6479 Register scratch1 = VirtualFrame::scratch0();
6480 Register scratch2 = VirtualFrame::scratch1();
6481 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6482 __ IncrementCounter(
6483 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6484
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006485 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6486 // calling convention.
6487 if (value_.is(r1)) {
6488 __ Swap(r0, r1, ip);
6489 }
6490 ASSERT(receiver_.is(r2));
6491
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006492 // The rest of the instructions in the deferred code must be together.
6493 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006494 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6495 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006496 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6497 __ Call(ic, RelocInfo::CODE_TARGET);
6498 // The call must be followed by a nop instruction to indicate that the
6499 // keyed store has been inlined.
6500 __ nop(PROPERTY_ACCESS_INLINED);
6501
6502 // Block the constant pool for one more instruction after leaving this
6503 // constant pool block scope to include the branch instruction ending the
6504 // deferred code.
6505 __ BlockConstPoolFor(1);
6506 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006507}
6508
6509
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006510class DeferredReferenceSetNamedValue: public DeferredCode {
6511 public:
6512 DeferredReferenceSetNamedValue(Register value,
6513 Register receiver,
6514 Handle<String> name)
6515 : value_(value), receiver_(receiver), name_(name) {
6516 set_comment("[ DeferredReferenceSetNamedValue");
6517 }
6518
6519 virtual void Generate();
6520
6521 private:
6522 Register value_;
6523 Register receiver_;
6524 Handle<String> name_;
6525};
6526
6527
6528// Takes value in r0, receiver in r1 and returns the result (the
6529// value) in r0.
6530void DeferredReferenceSetNamedValue::Generate() {
6531 // Record the entry frame and spill.
6532 VirtualFrame copied_frame(*frame_state()->frame());
6533 copied_frame.SpillAll();
6534
6535 // Ensure value in r0, receiver in r1 to match store ic calling
6536 // convention.
6537 ASSERT(value_.is(r0) && receiver_.is(r1));
6538 __ mov(r2, Operand(name_));
6539
6540 // The rest of the instructions in the deferred code must be together.
6541 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6542 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6543 // r1 and r2.
6544 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6545 __ Call(ic, RelocInfo::CODE_TARGET);
6546 // The call must be followed by a nop instruction to indicate that the
6547 // named store has been inlined.
6548 __ nop(PROPERTY_ACCESS_INLINED);
6549
6550 // Go back to the frame we entered with. The instructions
6551 // generated by this merge are skipped over by the inline store
6552 // patching mechanism when looking for the branch instruction that
6553 // tells it where the code to patch is.
6554 copied_frame.MergeTo(frame_state()->frame());
6555
6556 // Block the constant pool for one more instruction after leaving this
6557 // constant pool block scope to include the branch instruction ending the
6558 // deferred code.
6559 __ BlockConstPoolFor(1);
6560 }
6561}
6562
6563
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006564// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006565void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6566 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6567 Comment cmnt(masm(), "[ Load from named Property");
6568 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006569 frame_->CallLoadIC(name,
6570 is_contextual
6571 ? RelocInfo::CODE_TARGET_CONTEXT
6572 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006573 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006574 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006575 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006576 Comment cmnt(masm(), "[ Inlined named property load");
6577
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006578 // Counter will be decremented in the deferred code. Placed here to avoid
6579 // having it in the instruction stream below where patching will occur.
6580 __ IncrementCounter(&Counters::named_load_inline, 1,
6581 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006582
6583 // The following instructions are the inlined load of an in-object property.
6584 // Parts of this code is patched, so the exact instructions generated needs
6585 // to be fixed. Therefore the instruction pool is blocked when generating
6586 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006587
6588 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006589 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006590
6591 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006592 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006593
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006594#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006595 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006596 Label check_inlined_codesize;
6597 masm_->bind(&check_inlined_codesize);
6598#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006599
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006600 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006601 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006602 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006603 deferred->Branch(eq);
6604
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006605 Register scratch = VirtualFrame::scratch0();
6606 Register scratch2 = VirtualFrame::scratch1();
6607
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006608 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006609 // code. Therefore we can't use a LoadRoot call.
6610 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6611 __ mov(scratch2, Operand(Factory::null_value()));
6612 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006613 deferred->Branch(ne);
6614
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006615 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006616 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006617 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006618
6619 // Make sure that the expected number of instructions are generated.
6620 ASSERT_EQ(kInlinedNamedLoadInstructions,
6621 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006622 }
6623
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006624 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006625 // At this point the receiver register has the result, either from the
6626 // deferred code or from the inlined code.
6627 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006628 }
6629}
6630
6631
ager@chromium.orgac091b72010-05-05 07:34:42 +00006632void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6633#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006634 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006635#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006636
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006637 Result result;
6638 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6639 frame()->CallStoreIC(name, is_contextual);
6640 } else {
6641 // Inline the in-object property case.
6642 JumpTarget slow, done;
6643
6644 // Get the value and receiver from the stack.
6645 frame()->PopToR0();
6646 Register value = r0;
6647 frame()->PopToR1();
6648 Register receiver = r1;
6649
6650 DeferredReferenceSetNamedValue* deferred =
6651 new DeferredReferenceSetNamedValue(value, receiver, name);
6652
6653 // Check that the receiver is a heap object.
6654 __ tst(receiver, Operand(kSmiTagMask));
6655 deferred->Branch(eq);
6656
6657 // The following instructions are the part of the inlined
6658 // in-object property store code which can be patched. Therefore
6659 // the exact number of instructions generated must be fixed, so
6660 // the constant pool is blocked while generating this code.
6661 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6662 Register scratch0 = VirtualFrame::scratch0();
6663 Register scratch1 = VirtualFrame::scratch1();
6664
6665 // Check the map. Initially use an invalid map to force a
6666 // failure. The map check will be patched in the runtime system.
6667 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6668
6669#ifdef DEBUG
6670 Label check_inlined_codesize;
6671 masm_->bind(&check_inlined_codesize);
6672#endif
6673 __ mov(scratch0, Operand(Factory::null_value()));
6674 __ cmp(scratch0, scratch1);
6675 deferred->Branch(ne);
6676
6677 int offset = 0;
6678 __ str(value, MemOperand(receiver, offset));
6679
6680 // Update the write barrier and record its size. We do not use
6681 // the RecordWrite macro here because we want the offset
6682 // addition instruction first to make it easy to patch.
6683 Label record_write_start, record_write_done;
6684 __ bind(&record_write_start);
6685 // Add offset into the object.
6686 __ add(scratch0, receiver, Operand(offset));
6687 // Test that the object is not in the new space. We cannot set
6688 // region marks for new space pages.
6689 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6690 // Record the actual write.
6691 __ RecordWriteHelper(receiver, scratch0, scratch1);
6692 __ bind(&record_write_done);
6693 // Clobber all input registers when running with the debug-code flag
6694 // turned on to provoke errors.
6695 if (FLAG_debug_code) {
6696 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6697 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6698 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6699 }
6700 // Check that this is the first inlined write barrier or that
6701 // this inlined write barrier has the same size as all the other
6702 // inlined write barriers.
6703 ASSERT((inlined_write_barrier_size_ == -1) ||
6704 (inlined_write_barrier_size_ ==
6705 masm()->InstructionsGeneratedSince(&record_write_start)));
6706 inlined_write_barrier_size_ =
6707 masm()->InstructionsGeneratedSince(&record_write_start);
6708
6709 // Make sure that the expected number of instructions are generated.
6710 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6711 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6712 }
6713 deferred->BindExit();
6714 }
6715 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006716}
6717
6718
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006719void CodeGenerator::EmitKeyedLoad() {
6720 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006721 Comment cmnt(masm_, "[ Load from keyed property");
6722 frame_->CallKeyedLoadIC();
6723 } else {
6724 // Inline the keyed load.
6725 Comment cmnt(masm_, "[ Inlined load from keyed property");
6726
6727 // Counter will be decremented in the deferred code. Placed here to avoid
6728 // having it in the instruction stream below where patching will occur.
6729 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6730 frame_->scratch0(), frame_->scratch1());
6731
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006732 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006733 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006734 Register key = frame_->PopToRegister();
6735 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006736
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006737 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006738 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006739 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006740
6741 // Check that the receiver is a heap object.
6742 __ tst(receiver, Operand(kSmiTagMask));
6743 deferred->Branch(eq);
6744
6745 // The following instructions are the part of the inlined load keyed
6746 // property code which can be patched. Therefore the exact number of
6747 // instructions generated need to be fixed, so the constant pool is blocked
6748 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006749 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6750 Register scratch1 = VirtualFrame::scratch0();
6751 Register scratch2 = VirtualFrame::scratch1();
6752 // Check the map. The null map used below is patched by the inline cache
6753 // code.
6754 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006755
6756 // Check that the key is a smi.
6757 if (!key_is_known_smi) {
6758 __ tst(key, Operand(kSmiTagMask));
6759 deferred->Branch(ne);
6760 }
6761
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006762#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006763 Label check_inlined_codesize;
6764 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006765#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006766 __ mov(scratch2, Operand(Factory::null_value()));
6767 __ cmp(scratch1, scratch2);
6768 deferred->Branch(ne);
6769
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006770 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006771 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006772 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006773
6774 // Check that key is within bounds. Use unsigned comparison to handle
6775 // negative keys.
6776 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006777 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006778 deferred->Branch(ls); // Unsigned less equal.
6779
6780 // Load and check that the result is not the hole (key is a smi).
6781 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6782 __ add(scratch1,
6783 scratch1,
6784 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006785 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006786 MemOperand(scratch1, key, LSL,
6787 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006788 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006789 deferred->Branch(eq);
6790
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006791 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006792 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006793 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006794 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6795 }
6796
6797 deferred->BindExit();
6798 }
6799}
6800
6801
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006802void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6803 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006804 // Generate inlined version of the keyed store if the code is in a loop
6805 // and the key is likely to be a smi.
6806 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6807 // Inline the keyed store.
6808 Comment cmnt(masm_, "[ Inlined store to keyed property");
6809
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006810 Register scratch1 = VirtualFrame::scratch0();
6811 Register scratch2 = VirtualFrame::scratch1();
6812 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006813
6814 // Counter will be decremented in the deferred code. Placed here to avoid
6815 // having it in the instruction stream below where patching will occur.
6816 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006817 scratch1, scratch2);
6818
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006819
6820
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006821 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006822 bool value_is_harmless = frame_->KnownSmiAt(0);
6823 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6824 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006825 Register value = frame_->PopToRegister();
6826 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006827 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006828 Register receiver = r2;
6829 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006830
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006831#ifdef DEBUG
6832 bool we_remembered_the_write_barrier = value_is_harmless;
6833#endif
6834
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006835 // The deferred code expects value, key and receiver in registers.
6836 DeferredReferenceSetKeyedValue* deferred =
6837 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006838
6839 // Check that the value is a smi. As this inlined code does not set the
6840 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006841 if (!value_is_harmless) {
6842 // If the value is not likely to be a Smi then let's test the fixed array
6843 // for new space instead. See below.
6844 if (wb_info == LIKELY_SMI) {
6845 __ tst(value, Operand(kSmiTagMask));
6846 deferred->Branch(ne);
6847#ifdef DEBUG
6848 we_remembered_the_write_barrier = true;
6849#endif
6850 }
6851 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006852
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006853 if (!key_is_smi) {
6854 // Check that the key is a smi.
6855 __ tst(key, Operand(kSmiTagMask));
6856 deferred->Branch(ne);
6857 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006858
6859 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006860 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006861 deferred->Branch(eq);
6862
6863 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006864 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006865 deferred->Branch(ne);
6866
6867 // Check that the key is within bounds. Both the key and the length of
6868 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006869 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6870 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006871 deferred->Branch(ls); // Unsigned less equal.
6872
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006873 // Get the elements array from the receiver.
6874 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6875 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6876 Label ok;
6877 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6878 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6879 __ tst(value, Operand(kSmiTagMask), ne);
6880 deferred->Branch(ne);
6881#ifdef DEBUG
6882 we_remembered_the_write_barrier = true;
6883#endif
6884 }
6885 // Check that the elements array is not a dictionary.
6886 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006887 // The following instructions are the part of the inlined store keyed
6888 // property code which can be patched. Therefore the exact number of
6889 // instructions generated need to be fixed, so the constant pool is blocked
6890 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006891 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006892#ifdef DEBUG
6893 Label check_inlined_codesize;
6894 masm_->bind(&check_inlined_codesize);
6895#endif
6896
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006897 // Read the fixed array map from the constant pool (not from the root
6898 // array) so that the value can be patched. When debugging, we patch this
6899 // comparison to always fail so that we will hit the IC call in the
6900 // deferred code which will allow the debugger to break for fast case
6901 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006902 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6903 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006904 deferred->Branch(ne);
6905
6906 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006907 __ add(scratch1, scratch1,
6908 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6909 __ str(value,
6910 MemOperand(scratch1, key, LSL,
6911 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006912
6913 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006914 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006915 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6916 }
6917
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006918 ASSERT(we_remembered_the_write_barrier);
6919
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006920 deferred->BindExit();
6921 } else {
6922 frame()->CallKeyedStoreIC();
6923 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006924}
6925
6926
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006927#ifdef DEBUG
6928bool CodeGenerator::HasValidEntryRegisters() { return true; }
6929#endif
6930
6931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006932#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006933#define __ ACCESS_MASM(masm)
6934
ager@chromium.org7c537e22008-10-16 08:43:32 +00006935Handle<String> Reference::GetName() {
6936 ASSERT(type_ == NAMED);
6937 Property* property = expression_->AsProperty();
6938 if (property == NULL) {
6939 // Global variable reference treated as a named property reference.
6940 VariableProxy* proxy = expression_->AsVariableProxy();
6941 ASSERT(proxy->AsVariable() != NULL);
6942 ASSERT(proxy->AsVariable()->is_global());
6943 return proxy->name();
6944 } else {
6945 Literal* raw_name = property->key()->AsLiteral();
6946 ASSERT(raw_name != NULL);
6947 return Handle<String>(String::cast(*raw_name->handle()));
6948 }
6949}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006950
ager@chromium.org7c537e22008-10-16 08:43:32 +00006951
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006952void Reference::DupIfPersist() {
6953 if (persist_after_get_) {
6954 switch (type_) {
6955 case KEYED:
6956 cgen_->frame()->Dup2();
6957 break;
6958 case NAMED:
6959 cgen_->frame()->Dup();
6960 // Fall through.
6961 case UNLOADED:
6962 case ILLEGAL:
6963 case SLOT:
6964 // Do nothing.
6965 ;
6966 }
6967 } else {
6968 set_unloaded();
6969 }
6970}
6971
6972
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006973void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006974 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006975 ASSERT(!is_illegal());
6976 ASSERT(!cgen_->has_cc());
6977 MacroAssembler* masm = cgen_->masm();
6978 Property* property = expression_->AsProperty();
6979 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006980 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006981 }
6982
6983 switch (type_) {
6984 case SLOT: {
6985 Comment cmnt(masm, "[ Load from Slot");
6986 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6987 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006988 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006989 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006990 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006991 }
6992
ager@chromium.org7c537e22008-10-16 08:43:32 +00006993 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006994 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006995 bool is_global = var != NULL;
6996 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006997 Handle<String> name = GetName();
6998 DupIfPersist();
6999 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007000 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007001 }
7002
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007003 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007004 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007005 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007006 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007007 cgen_->frame()->EmitPush(r0);
7008 break;
7009 }
7010
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007011 default:
7012 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007013 }
7014}
7015
7016
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007017void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00007018 ASSERT(!is_illegal());
7019 ASSERT(!cgen_->has_cc());
7020 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007021 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007022 Property* property = expression_->AsProperty();
7023 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007024 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007025 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007026
ager@chromium.org7c537e22008-10-16 08:43:32 +00007027 switch (type_) {
7028 case SLOT: {
7029 Comment cmnt(masm, "[ Store to Slot");
7030 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007031 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007032 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007033 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007034 }
7035
ager@chromium.org7c537e22008-10-16 08:43:32 +00007036 case NAMED: {
7037 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00007038 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007039 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00007040 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007041 break;
7042 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007043
ager@chromium.org7c537e22008-10-16 08:43:32 +00007044 case KEYED: {
7045 Comment cmnt(masm, "[ Store to keyed Property");
7046 Property* property = expression_->AsProperty();
7047 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00007048 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007049 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00007050 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00007051 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00007052 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007053 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00007054
7055 default:
7056 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007057 }
7058}
7059
7060
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007061void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007062 // Create a new closure from the given function info in new
7063 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007064 Label gc;
7065
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007066 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007067 __ pop(r3);
7068
7069 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007070 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007071 r0,
7072 r1,
7073 r2,
7074 &gc,
7075 TAG_OBJECT);
7076
7077 // Compute the function map in the current global context and set that
7078 // as the map of the allocated object.
7079 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7080 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
7081 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
7082 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
7083
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007084 // Initialize the rest of the function. We don't have to update the
7085 // write barrier because the allocated object is in new space.
7086 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
7087 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
7088 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
7089 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
7090 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
7091 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
7092 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
7093 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007094
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00007095 // Initialize the code pointer in the function to be the one
7096 // found in the shared function info object.
7097 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00007098 __ add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
7099 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00007100
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007101 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007102 __ Ret();
7103
7104 // Create a new closure through the slower runtime call.
7105 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007106 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007107 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007108}
7109
7110
7111void FastNewContextStub::Generate(MacroAssembler* masm) {
7112 // Try to allocate the context in new space.
7113 Label gc;
7114 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
7115
7116 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007117 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007118 r0,
7119 r1,
7120 r2,
7121 &gc,
7122 TAG_OBJECT);
7123
7124 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00007125 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007126
7127 // Setup the object header.
7128 __ LoadRoot(r2, Heap::kContextMapRootIndex);
7129 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007130 __ mov(r2, Operand(Smi::FromInt(length)));
7131 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007132
7133 // Setup the fixed slots.
7134 __ mov(r1, Operand(Smi::FromInt(0)));
7135 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
7136 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
7137 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
7138 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
7139
7140 // Copy the global object from the surrounding context.
7141 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7142 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
7143
7144 // Initialize the rest of the slots to undefined.
7145 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
7146 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
7147 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
7148 }
7149
7150 // Remove the on-stack argument and return.
7151 __ mov(cp, r0);
7152 __ pop();
7153 __ Ret();
7154
7155 // Need to collect. Call into runtime system.
7156 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007157 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007158}
7159
7160
ager@chromium.org5c838252010-02-19 08:53:10 +00007161void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
7162 // Stack layout on entry:
7163 //
7164 // [sp]: constant elements.
7165 // [sp + kPointerSize]: literal index.
7166 // [sp + (2 * kPointerSize)]: literals array.
7167
7168 // All sizes here are multiples of kPointerSize.
7169 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
7170 int size = JSArray::kSize + elements_size;
7171
7172 // Load boilerplate object into r3 and check if we need to create a
7173 // boilerplate.
7174 Label slow_case;
7175 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
7176 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
7177 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7178 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
7179 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
7180 __ cmp(r3, ip);
7181 __ b(eq, &slow_case);
7182
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00007183 if (FLAG_debug_code) {
7184 const char* message;
7185 Heap::RootListIndex expected_map_index;
7186 if (mode_ == CLONE_ELEMENTS) {
7187 message = "Expected (writable) fixed array";
7188 expected_map_index = Heap::kFixedArrayMapRootIndex;
7189 } else {
7190 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
7191 message = "Expected copy-on-write fixed array";
7192 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
7193 }
7194 __ push(r3);
7195 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
7196 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
7197 __ LoadRoot(ip, expected_map_index);
7198 __ cmp(r3, ip);
7199 __ Assert(eq, message);
7200 __ pop(r3);
7201 }
7202
ager@chromium.org5c838252010-02-19 08:53:10 +00007203 // Allocate both the JS array and the elements array in one big
7204 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007205 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00007206 r0,
7207 r1,
7208 r2,
7209 &slow_case,
7210 TAG_OBJECT);
7211
7212 // Copy the JS array part.
7213 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
7214 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
7215 __ ldr(r1, FieldMemOperand(r3, i));
7216 __ str(r1, FieldMemOperand(r0, i));
7217 }
7218 }
7219
7220 if (length_ > 0) {
7221 // Get hold of the elements array of the boilerplate and setup the
7222 // elements pointer in the resulting object.
7223 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
7224 __ add(r2, r0, Operand(JSArray::kSize));
7225 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
7226
7227 // Copy the elements array.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00007228 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00007229 }
7230
7231 // Return and remove the on-stack parameters.
7232 __ add(sp, sp, Operand(3 * kPointerSize));
7233 __ Ret();
7234
7235 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007236 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00007237}
7238
7239
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007240// Takes a Smi and converts to an IEEE 64 bit floating point value in two
7241// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
7242// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
7243// scratch register. Destroys the source register. No GC occurs during this
7244// stub so you don't have to set up the frame.
7245class ConvertToDoubleStub : public CodeStub {
7246 public:
7247 ConvertToDoubleStub(Register result_reg_1,
7248 Register result_reg_2,
7249 Register source_reg,
7250 Register scratch_reg)
7251 : result1_(result_reg_1),
7252 result2_(result_reg_2),
7253 source_(source_reg),
7254 zeros_(scratch_reg) { }
7255
7256 private:
7257 Register result1_;
7258 Register result2_;
7259 Register source_;
7260 Register zeros_;
7261
7262 // Minor key encoding in 16 bits.
7263 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
7264 class OpBits: public BitField<Token::Value, 2, 14> {};
7265
7266 Major MajorKey() { return ConvertToDouble; }
7267 int MinorKey() {
7268 // Encode the parameters in a unique 16 bit value.
7269 return result1_.code() +
7270 (result2_.code() << 4) +
7271 (source_.code() << 8) +
7272 (zeros_.code() << 12);
7273 }
7274
7275 void Generate(MacroAssembler* masm);
7276
7277 const char* GetName() { return "ConvertToDoubleStub"; }
7278
7279#ifdef DEBUG
7280 void Print() { PrintF("ConvertToDoubleStub\n"); }
7281#endif
7282};
7283
7284
7285void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
7286#ifndef BIG_ENDIAN_FLOATING_POINT
7287 Register exponent = result1_;
7288 Register mantissa = result2_;
7289#else
7290 Register exponent = result2_;
7291 Register mantissa = result1_;
7292#endif
7293 Label not_special;
7294 // Convert from Smi to integer.
7295 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
7296 // Move sign bit from source to destination. This works because the sign bit
7297 // in the exponent word of the double has the same position and polarity as
7298 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007299 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007300 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
7301 // Subtract from 0 if source was negative.
7302 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007303
7304 // We have -1, 0 or 1, which we treat specially. Register source_ contains
7305 // absolute value: it is either equal to 1 (special case of -1 and 1),
7306 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007307 __ cmp(source_, Operand(1));
7308 __ b(gt, &not_special);
7309
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007310 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
7311 static const uint32_t exponent_word_for_1 =
7312 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007313 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007314 // 1, 0 and -1 all have 0 for the second word.
7315 __ mov(mantissa, Operand(0));
7316 __ Ret();
7317
7318 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007319 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007320 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007321 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007322 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007323 // We use mantissa as a scratch register here. Use a fudge factor to
7324 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7325 // that fit in the ARM's constant field.
7326 int fudge = 0x400;
7327 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7328 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007329 __ orr(exponent,
7330 exponent,
7331 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7332 // Shift up the source chopping the top bit off.
7333 __ add(zeros_, zeros_, Operand(1));
7334 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7335 __ mov(source_, Operand(source_, LSL, zeros_));
7336 // Compute lower part of fraction (last 12 bits).
7337 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7338 // And the top (top 20 bits).
7339 __ orr(exponent,
7340 exponent,
7341 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7342 __ Ret();
7343}
7344
7345
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007346// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007347void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007348 Label max_negative_int;
7349 // the_int_ has the answer which is a signed int32 but not a Smi.
7350 // We test for the special value that has a different exponent. This test
7351 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007352 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007353 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007354 __ b(eq, &max_negative_int);
7355 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7356 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7357 uint32_t non_smi_exponent =
7358 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7359 __ mov(scratch_, Operand(non_smi_exponent));
7360 // Set the sign bit in scratch_ if the value was negative.
7361 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7362 // Subtract from 0 if the value was negative.
7363 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7364 // We should be masking the implict first digit of the mantissa away here,
7365 // but it just ends up combining harmlessly with the last digit of the
7366 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7367 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7368 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7369 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7370 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7371 __ str(scratch_, FieldMemOperand(the_heap_number_,
7372 HeapNumber::kExponentOffset));
7373 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7374 __ str(scratch_, FieldMemOperand(the_heap_number_,
7375 HeapNumber::kMantissaOffset));
7376 __ Ret();
7377
7378 __ bind(&max_negative_int);
7379 // The max negative int32 is stored as a positive number in the mantissa of
7380 // a double because it uses a sign bit instead of using two's complement.
7381 // The actual mantissa bits stored are all 0 because the implicit most
7382 // significant 1 bit is not stored.
7383 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7384 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7385 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7386 __ mov(ip, Operand(0));
7387 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7388 __ Ret();
7389}
7390
7391
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007392// Handle the case where the lhs and rhs are the same object.
7393// Equality is almost reflexive (everything but NaN), so this is a test
7394// for "identity and not NaN".
7395static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7396 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007397 Condition cc,
7398 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007399 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007400 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007401 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007402 __ b(ne, &not_identical);
7403
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007404 // The two objects are identical. If we know that one of them isn't NaN then
7405 // we now know they test equal.
7406 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007407 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7408 // so we do the second best thing - test it ourselves.
7409 // They are both equal and they are not both Smis so both of them are not
7410 // Smis. If it's not a heap number, then return equal.
7411 if (cc == lt || cc == gt) {
7412 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007413 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007414 } else {
7415 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7416 __ b(eq, &heap_number);
7417 // Comparing JS objects with <=, >= is complicated.
7418 if (cc != eq) {
7419 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7420 __ b(ge, slow);
7421 // Normally here we fall through to return_equal, but undefined is
7422 // special: (undefined == undefined) == true, but
7423 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7424 if (cc == le || cc == ge) {
7425 __ cmp(r4, Operand(ODDBALL_TYPE));
7426 __ b(ne, &return_equal);
7427 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007428 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007429 __ b(ne, &return_equal);
7430 if (cc == le) {
7431 // undefined <= undefined should fail.
7432 __ mov(r0, Operand(GREATER));
7433 } else {
7434 // undefined >= undefined should fail.
7435 __ mov(r0, Operand(LESS));
7436 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007437 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007438 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007439 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007440 }
7441 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007442
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007443 __ bind(&return_equal);
7444 if (cc == lt) {
7445 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7446 } else if (cc == gt) {
7447 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7448 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007449 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007450 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007451 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007452
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007453 if (cc != eq || !never_nan_nan) {
7454 // For less and greater we don't have to check for NaN since the result of
7455 // x < x is false regardless. For the others here is some code to check
7456 // for NaN.
7457 if (cc != lt && cc != gt) {
7458 __ bind(&heap_number);
7459 // It is a heap number, so return non-equal if it's NaN and equal if it's
7460 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007461
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007462 // The representation of NaN values has all exponent bits (52..62) set,
7463 // and not all mantissa bits (0..51) clear.
7464 // Read top bits of double representation (second word of value).
7465 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7466 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007467 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7468 // NaNs have all-one exponents so they sign extend to -1.
7469 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007470 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007471
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007472 // Shift out flag and all exponent bits, retaining only mantissa.
7473 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7474 // Or with all low-bits of mantissa.
7475 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7476 __ orr(r0, r3, Operand(r2), SetCC);
7477 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007478 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7479 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7480 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007481 if (cc != eq) {
7482 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007483 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007484 if (cc == le) {
7485 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7486 } else {
7487 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7488 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007489 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007490 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007491 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007492 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007493 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007494
7495 __ bind(&not_identical);
7496}
7497
7498
7499// See comment at call site.
7500static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007501 Register lhs,
7502 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007503 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007504 Label* slow,
7505 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007506 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7507 (lhs.is(r1) && rhs.is(r0)));
7508
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007509 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007510 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007511 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007512
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007513 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007514 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007515 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007516 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007517 // succeed. Return non-equal
7518 // If rhs is r0 then there is already a non zero value in it.
7519 if (!rhs.is(r0)) {
7520 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7521 }
7522 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007523 } else {
7524 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7525 // the runtime.
7526 __ b(ne, slow);
7527 }
7528
ager@chromium.orgb5737492010-07-15 09:29:43 +00007529 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007530 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007531 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007532 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007533 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007534 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007535 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007536 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007537 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007538 __ push(lr);
7539 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007540 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007541 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7542 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007543 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007544 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007545 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007546 }
7547
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007548 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007549 // since it's a smi.
7550 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007551
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007552 __ bind(&rhs_is_smi);
7553 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007554 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007555 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007556 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007557 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007558 // If lhs is r0 then there is already a non zero value in it.
7559 if (!lhs.is(r0)) {
7560 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7561 }
7562 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007563 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007564 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007565 // the runtime.
7566 __ b(ne, slow);
7567 }
7568
ager@chromium.orgb5737492010-07-15 09:29:43 +00007569 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007570 if (CpuFeatures::IsSupported(VFP3)) {
7571 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007572 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007573 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007574 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007575 // Convert rhs to a double in d6 .
7576 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007577 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007578 __ push(lr);
7579 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007580 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007581 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007582 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007583 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7584 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007585 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007586 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007587 // Fall through to both_loaded_as_doubles.
7588}
7589
7590
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007591void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007592 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007593 Register rhs_exponent = exp_first ? r0 : r1;
7594 Register lhs_exponent = exp_first ? r2 : r3;
7595 Register rhs_mantissa = exp_first ? r1 : r0;
7596 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007597 Label one_is_nan, neither_is_nan;
7598
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007599 __ Sbfx(r4,
7600 lhs_exponent,
7601 HeapNumber::kExponentShift,
7602 HeapNumber::kExponentBits);
7603 // NaNs have all-one exponents so they sign extend to -1.
7604 __ cmp(r4, Operand(-1));
7605 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007606 __ mov(r4,
7607 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7608 SetCC);
7609 __ b(ne, &one_is_nan);
7610 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007611 __ b(ne, &one_is_nan);
7612
7613 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007614 __ Sbfx(r4,
7615 rhs_exponent,
7616 HeapNumber::kExponentShift,
7617 HeapNumber::kExponentBits);
7618 // NaNs have all-one exponents so they sign extend to -1.
7619 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007620 __ b(ne, &neither_is_nan);
7621 __ mov(r4,
7622 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7623 SetCC);
7624 __ b(ne, &one_is_nan);
7625 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007626 __ b(eq, &neither_is_nan);
7627
7628 __ bind(&one_is_nan);
7629 // NaN comparisons always fail.
7630 // Load whatever we need in r0 to make the comparison fail.
7631 if (cc == lt || cc == le) {
7632 __ mov(r0, Operand(GREATER));
7633 } else {
7634 __ mov(r0, Operand(LESS));
7635 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007636 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007637
7638 __ bind(&neither_is_nan);
7639}
7640
7641
7642// See comment at call site.
7643static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7644 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007645 Register rhs_exponent = exp_first ? r0 : r1;
7646 Register lhs_exponent = exp_first ? r2 : r3;
7647 Register rhs_mantissa = exp_first ? r1 : r0;
7648 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007649
7650 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7651 if (cc == eq) {
7652 // Doubles are not equal unless they have the same bit pattern.
7653 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007654 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7655 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007656 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007657 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007658
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007659 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007660 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007661 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007662
7663 // Exponents are unequal. The only way we can return that the numbers
7664 // are equal is if one is -0 and the other is 0. We already dealt
7665 // with the case where both are -0 or both are 0.
7666 // We start by seeing if the mantissas (that are equal) or the bottom
7667 // 31 bits of the rhs exponent are non-zero. If so we return not
7668 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007669 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007670 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007671 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007672 // Now they are equal if and only if the lhs exponent is zero in its
7673 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007674 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007675 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007676 } else {
7677 // Call a native function to do a comparison between two non-NaNs.
7678 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007679 __ push(lr);
7680 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7681 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7682 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007683 }
7684}
7685
7686
7687// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007688static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7689 Register lhs,
7690 Register rhs) {
7691 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7692 (lhs.is(r1) && rhs.is(r0)));
7693
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007694 // If either operand is a JSObject or an oddball value, then they are
7695 // not equal since their pointers are different.
7696 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007697 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007698 Label first_non_object;
7699 // Get the type of the first operand into r2 and compare it with
7700 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007701 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007702 __ b(lt, &first_non_object);
7703
7704 // Return non-zero (r0 is not zero)
7705 Label return_not_equal;
7706 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007707 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007708
7709 __ bind(&first_non_object);
7710 // Check for oddballs: true, false, null, undefined.
7711 __ cmp(r2, Operand(ODDBALL_TYPE));
7712 __ b(eq, &return_not_equal);
7713
ager@chromium.orgb5737492010-07-15 09:29:43 +00007714 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007715 __ b(ge, &return_not_equal);
7716
7717 // Check for oddballs: true, false, null, undefined.
7718 __ cmp(r3, Operand(ODDBALL_TYPE));
7719 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007720
7721 // Now that we have the types we might as well check for symbol-symbol.
7722 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007723 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7724 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007725 __ and_(r2, r2, Operand(r3));
7726 __ tst(r2, Operand(kIsSymbolMask));
7727 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007728}
7729
7730
7731// See comment at call site.
7732static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007733 Register lhs,
7734 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007735 Label* both_loaded_as_doubles,
7736 Label* not_heap_numbers,
7737 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007738 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7739 (lhs.is(r1) && rhs.is(r0)));
7740
7741 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007742 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007743 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007744 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007745 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7746
7747 // Both are heap numbers. Load them up then jump to the code we have
7748 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007749 if (CpuFeatures::IsSupported(VFP3)) {
7750 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007751 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007752 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007753 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007754 __ vldr(d7, r7, HeapNumber::kValueOffset);
7755 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007756 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7757 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007758 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007759 __ jmp(both_loaded_as_doubles);
7760}
7761
7762
7763// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007764static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007765 Register lhs,
7766 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007767 Label* possible_strings,
7768 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007769 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7770 (lhs.is(r1) && rhs.is(r0)));
7771
7772 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007773 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007774 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007775 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007776 __ tst(r2, Operand(kIsNotStringMask));
7777 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007778 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007779 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007780 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007781 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007782 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007783 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007784
7785 // Both are symbols. We already checked they weren't the same pointer
7786 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007787 __ mov(r0, Operand(NOT_EQUAL));
7788 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007789
7790 __ bind(&object_test);
7791 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7792 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007793 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007794 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007795 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007796 // are not equal, since they are different objects and an object is not
7797 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007798 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007799 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7800 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7801 __ and_(r0, r2, Operand(r3));
7802 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7803 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007804 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007805}
7806
7807
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007808void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7809 Register object,
7810 Register result,
7811 Register scratch1,
7812 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007813 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007814 bool object_is_smi,
7815 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007816 // Use of registers. Register result is used as a temporary.
7817 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007818 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007819
7820 // Load the number string cache.
7821 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7822
7823 // Make the hash mask from the length of the number string cache. It
7824 // contains two elements (number and string) for each cache entry.
7825 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007826 // Divide length by two (length is a smi).
7827 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007828 __ sub(mask, mask, Operand(1)); // Make mask.
7829
7830 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007831 // number string cache for smis is just the smi value, and the hash for
7832 // doubles is the xor of the upper and lower words. See
7833 // Heap::GetNumberStringCache.
7834 Label is_smi;
7835 Label load_result_from_cache;
7836 if (!object_is_smi) {
7837 __ BranchOnSmi(object, &is_smi);
7838 if (CpuFeatures::IsSupported(VFP3)) {
7839 CpuFeatures::Scope scope(VFP3);
7840 __ CheckMap(object,
7841 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007842 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007843 not_found,
7844 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007845
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007846 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007847 __ add(scratch1,
7848 object,
7849 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7850 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7851 __ eor(scratch1, scratch1, Operand(scratch2));
7852 __ and_(scratch1, scratch1, Operand(mask));
7853
7854 // Calculate address of entry in string cache: each entry consists
7855 // of two pointer sized fields.
7856 __ add(scratch1,
7857 number_string_cache,
7858 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7859
7860 Register probe = mask;
7861 __ ldr(probe,
7862 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7863 __ BranchOnSmi(probe, not_found);
7864 __ sub(scratch2, object, Operand(kHeapObjectTag));
7865 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7866 __ sub(probe, probe, Operand(kHeapObjectTag));
7867 __ vldr(d1, probe, HeapNumber::kValueOffset);
7868 __ vcmp(d0, d1);
7869 __ vmrs(pc);
7870 __ b(ne, not_found); // The cache did not contain this value.
7871 __ b(&load_result_from_cache);
7872 } else {
7873 __ b(not_found);
7874 }
7875 }
7876
7877 __ bind(&is_smi);
7878 Register scratch = scratch1;
7879 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007880 // Calculate address of entry in string cache: each entry consists
7881 // of two pointer sized fields.
7882 __ add(scratch,
7883 number_string_cache,
7884 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7885
7886 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007887 Register probe = mask;
7888 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7889 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007890 __ b(ne, not_found);
7891
7892 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007893 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007894 __ ldr(result,
7895 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007896 __ IncrementCounter(&Counters::number_to_string_native,
7897 1,
7898 scratch1,
7899 scratch2);
7900}
7901
7902
7903void NumberToStringStub::Generate(MacroAssembler* masm) {
7904 Label runtime;
7905
7906 __ ldr(r1, MemOperand(sp, 0));
7907
7908 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007909 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007910 __ add(sp, sp, Operand(1 * kPointerSize));
7911 __ Ret();
7912
7913 __ bind(&runtime);
7914 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007915 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007916}
7917
7918
ager@chromium.orgac091b72010-05-05 07:34:42 +00007919void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007920 __ add(offset_, object_, Operand(offset_));
7921 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007922 __ Ret();
7923}
7924
7925
ager@chromium.orgb5737492010-07-15 09:29:43 +00007926// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007927// On exit r0 is 0, positive or negative to indicate the result of
7928// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007929void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007930 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7931 (lhs_.is(r1) && rhs_.is(r0)));
7932
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007933 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007934 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007935
7936 // NOTICE! This code is only reached after a smi-fast-case check, so
7937 // it is certain that at least one operand isn't a smi.
7938
7939 // Handle the case where the objects are identical. Either returns the answer
7940 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007941 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007942
7943 // If either is a Smi (we know that not both are), then they can only
7944 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007945 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007946 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007947 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007948 __ tst(r2, Operand(kSmiTagMask));
7949 __ b(ne, &not_smis);
7950 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7951 // 1) Return the answer.
7952 // 2) Go to slow.
7953 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007954 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007955 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007956 // comparison. If VFP3 is supported the double values of the numbers have
7957 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7958 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007959 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007960
7961 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007962 // The arguments have been converted to doubles and stored in d6 and d7, if
7963 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007964 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007965 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007966 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007967 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007968 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007969 __ vcmp(d7, d6);
7970 __ vmrs(pc); // Move vector status bits to normal status bits.
7971 Label nan;
7972 __ b(vs, &nan);
7973 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7974 __ mov(r0, Operand(LESS), LeaveCC, lt);
7975 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007976 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007977
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007978 __ bind(&nan);
7979 // If one of the sides was a NaN then the v flag is set. Load r0 with
7980 // whatever it takes to make the comparison fail, since comparisons with NaN
7981 // always fail.
7982 if (cc_ == lt || cc_ == le) {
7983 __ mov(r0, Operand(GREATER));
7984 } else {
7985 __ mov(r0, Operand(LESS));
7986 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007987 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007988 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007989 // Checks for NaN in the doubles we have loaded. Can return the answer or
7990 // fall through if neither is a NaN. Also binds lhs_not_nan.
7991 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007992 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7993 // answer. Never falls through.
7994 EmitTwoNonNanDoubleComparison(masm, cc_);
7995 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007996
7997 __ bind(&not_smis);
7998 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007999 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008000 if (strict_) {
8001 // This returns non-equal for some object types, or falls through if it
8002 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00008003 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008004 }
8005
8006 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008007 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008008 // Check for heap-number-heap-number comparison. Can jump to slow case,
8009 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
8010 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00008011 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008012 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00008013 lhs_,
8014 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008015 &both_loaded_as_doubles,
8016 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008017 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008018
8019 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008020 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
8021 // symbols.
8022 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008023 // Returns an answer for two symbols or two detectable objects.
8024 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00008025 // Assumes that r2 is the type of rhs_ on entry.
8026 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008027 }
8028
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008029 // Check for both being sequential ASCII strings, and inline if that is the
8030 // case.
8031 __ bind(&flat_string_check);
8032
ager@chromium.orgb5737492010-07-15 09:29:43 +00008033 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008034
8035 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
8036 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00008037 lhs_,
8038 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008039 r2,
8040 r3,
8041 r4,
8042 r5);
8043 // Never falls through to here.
8044
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008045 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008046
ager@chromium.orgb5737492010-07-15 09:29:43 +00008047 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008048 // Figure out which native to call and setup the arguments.
8049 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008050 if (cc_ == eq) {
8051 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
8052 } else {
8053 native = Builtins::COMPARE;
8054 int ncr; // NaN compare result
8055 if (cc_ == lt || cc_ == le) {
8056 ncr = GREATER;
8057 } else {
8058 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
8059 ncr = LESS;
8060 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008061 __ mov(r0, Operand(Smi::FromInt(ncr)));
8062 __ push(r0);
8063 }
8064
8065 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
8066 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008067 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008068}
8069
8070
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00008071// This stub does not handle the inlined cases (Smis, Booleans, undefined).
8072// The stub returns zero for false, and a non-zero value for true.
8073void ToBooleanStub::Generate(MacroAssembler* masm) {
8074 Label false_result;
8075 Label not_heap_number;
8076 Register scratch0 = VirtualFrame::scratch0();
8077
8078 // HeapNumber => false iff +0, -0, or NaN.
8079 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8080 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
8081 __ cmp(scratch0, ip);
8082 __ b(&not_heap_number, ne);
8083
8084 __ sub(ip, tos_, Operand(kHeapObjectTag));
8085 __ vldr(d1, ip, HeapNumber::kValueOffset);
8086 __ vcmp(d1, 0.0);
8087 __ vmrs(pc);
8088 // "tos_" is a register, and contains a non zero value by default.
8089 // Hence we only need to overwrite "tos_" with zero to return false for
8090 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
8091 __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO
8092 __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN
8093 __ Ret();
8094
8095 __ bind(&not_heap_number);
8096
8097 // Check if the value is 'null'.
8098 // 'null' => false.
8099 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8100 __ cmp(tos_, ip);
8101 __ b(&false_result, eq);
8102
8103 // It can be an undetectable object.
8104 // Undetectable => false.
8105 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
8106 __ ldrb(scratch0, FieldMemOperand(ip, Map::kBitFieldOffset));
8107 __ and_(scratch0, scratch0, Operand(1 << Map::kIsUndetectable));
8108 __ cmp(scratch0, Operand(1 << Map::kIsUndetectable));
8109 __ b(&false_result, eq);
8110
8111 // JavaScript object => true.
8112 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8113 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8114 __ cmp(scratch0, Operand(FIRST_JS_OBJECT_TYPE));
8115 // "tos_" is a register and contains a non-zero value.
8116 // Hence we implicitly return true if the greater than
8117 // condition is satisfied.
8118 __ Ret(gt);
8119
8120 // Check for string
8121 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8122 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8123 __ cmp(scratch0, Operand(FIRST_NONSTRING_TYPE));
8124 // "tos_" is a register and contains a non-zero value.
8125 // Hence we implicitly return true if the greater than
8126 // condition is satisfied.
8127 __ Ret(gt);
8128
8129 // String value => false iff empty, i.e., length is zero
8130 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
8131 // If length is zero, "tos_" contains zero ==> false.
8132 // If length is not zero, "tos_" contains a non-zero value ==> true.
8133 __ Ret();
8134
8135 // Return 0 in "tos_" for false .
8136 __ bind(&false_result);
8137 __ mov(tos_, Operand(0));
8138 __ Ret();
8139}
8140
8141
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00008142// We fall into this code if the operands were Smis, but the result was
8143// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008144// the operands were not both Smi. The operands are in r0 and r1. In order
8145// to call the C-implemented binary fp operation routines we need to end up
8146// with the double precision floating point operands in r0 and r1 (for the
8147// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00008148void GenericBinaryOpStub::HandleBinaryOpSlowCases(
8149 MacroAssembler* masm,
8150 Label* not_smi,
8151 Register lhs,
8152 Register rhs,
8153 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008154 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00008155 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008156
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008157 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008158 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00008159
8160 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008161 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8162
ager@chromium.org357bf652010-04-12 11:30:10 +00008163 // Smi-smi case (overflow).
8164 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008165 // The new heap number is in r5. r3 and r7 are scratch.
8166 __ AllocateHeapNumber(
8167 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008168
8169 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
8170 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008171 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008172 CpuFeatures::Scope scope(VFP3);
8173 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
8174 __ vmov(s15, r7);
8175 __ vcvt_f64_s32(d7, s15);
8176 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
8177 __ vmov(s13, r7);
8178 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008179 if (!use_fp_registers) {
8180 __ vmov(r2, r3, d7);
8181 __ vmov(r0, r1, d6);
8182 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008183 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008184 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008185 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008186 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008187 __ push(lr);
8188 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008189 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008190 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008191 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008192 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
8193 __ pop(lr);
8194 }
8195 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008196 }
8197
ager@chromium.org357bf652010-04-12 11:30:10 +00008198 // We branch here if at least one of r0 and r1 is not a Smi.
8199 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008200 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008201
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008202 // After this point we have the left hand side in r1 and the right hand side
8203 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008204 if (lhs.is(r0)) {
8205 __ Swap(r0, r1, ip);
8206 }
8207
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008208 // The type transition also calculates the answer.
8209 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008210
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008211 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008212 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
8213 switch (op_) {
8214 case Token::ADD:
8215 case Token::SUB:
8216 case Token::MUL:
8217 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008218 GenerateTypeTransition(masm); // Tail call.
8219 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00008220 break;
8221
8222 default:
8223 break;
8224 }
8225 }
8226
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008227 if (generate_code_to_calculate_answer) {
8228 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
8229 if (mode_ == NO_OVERWRITE) {
8230 // In the case where there is no chance of an overwritable float we may
8231 // as well do the allocation immediately while r0 and r1 are untouched.
8232 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008233 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008234
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008235 // Move r0 to a double in r2-r3.
8236 __ tst(r0, Operand(kSmiTagMask));
8237 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
8238 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8239 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8240 __ cmp(r4, heap_number_map);
8241 __ b(ne, &slow);
8242 if (mode_ == OVERWRITE_RIGHT) {
8243 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008244 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008245 if (use_fp_registers) {
8246 CpuFeatures::Scope scope(VFP3);
8247 // Load the double from tagged HeapNumber r0 to d7.
8248 __ sub(r7, r0, Operand(kHeapObjectTag));
8249 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00008250 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008251 // Calling convention says that second double is in r2 and r3.
8252 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00008253 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008254 __ jmp(&finished_loading_r0);
8255 __ bind(&r0_is_smi);
8256 if (mode_ == OVERWRITE_RIGHT) {
8257 // We can't overwrite a Smi so get address of new heap number into r5.
8258 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8259 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008260
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008261 if (CpuFeatures::IsSupported(VFP3)) {
8262 CpuFeatures::Scope scope(VFP3);
8263 // Convert smi in r0 to double in d7.
8264 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
8265 __ vmov(s15, r7);
8266 __ vcvt_f64_s32(d7, s15);
8267 if (!use_fp_registers) {
8268 __ vmov(r2, r3, d7);
8269 }
8270 } else {
8271 // Write Smi from r0 to r3 and r2 in double format.
8272 __ mov(r7, Operand(r0));
8273 ConvertToDoubleStub stub3(r3, r2, r7, r4);
8274 __ push(lr);
8275 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
8276 __ pop(lr);
8277 }
8278
8279 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
8280 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
8281 Label r1_is_not_smi;
8282 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
8283 __ tst(r1, Operand(kSmiTagMask));
8284 __ b(ne, &r1_is_not_smi);
8285 GenerateTypeTransition(masm); // Tail call.
8286 }
8287
8288 __ bind(&finished_loading_r0);
8289
8290 // Move r1 to a double in r0-r1.
8291 __ tst(r1, Operand(kSmiTagMask));
8292 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
8293 __ bind(&r1_is_not_smi);
8294 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
8295 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8296 __ cmp(r4, heap_number_map);
8297 __ b(ne, &slow);
8298 if (mode_ == OVERWRITE_LEFT) {
8299 __ mov(r5, Operand(r1)); // Overwrite this heap number.
8300 }
8301 if (use_fp_registers) {
8302 CpuFeatures::Scope scope(VFP3);
8303 // Load the double from tagged HeapNumber r1 to d6.
8304 __ sub(r7, r1, Operand(kHeapObjectTag));
8305 __ vldr(d6, r7, HeapNumber::kValueOffset);
8306 } else {
8307 // Calling convention says that first double is in r0 and r1.
8308 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
8309 }
8310 __ jmp(&finished_loading_r1);
8311 __ bind(&r1_is_smi);
8312 if (mode_ == OVERWRITE_LEFT) {
8313 // We can't overwrite a Smi so get address of new heap number into r5.
8314 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8315 }
8316
8317 if (CpuFeatures::IsSupported(VFP3)) {
8318 CpuFeatures::Scope scope(VFP3);
8319 // Convert smi in r1 to double in d6.
8320 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
8321 __ vmov(s13, r7);
8322 __ vcvt_f64_s32(d6, s13);
8323 if (!use_fp_registers) {
8324 __ vmov(r0, r1, d6);
8325 }
8326 } else {
8327 // Write Smi from r1 to r1 and r0 in double format.
8328 __ mov(r7, Operand(r1));
8329 ConvertToDoubleStub stub4(r1, r0, r7, r9);
8330 __ push(lr);
8331 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
8332 __ pop(lr);
8333 }
8334
8335 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00008336 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008337
8338 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
8339 __ bind(&do_the_call);
8340 // If we are inlining the operation using VFP3 instructions for
8341 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
8342 if (use_fp_registers) {
8343 CpuFeatures::Scope scope(VFP3);
8344 // ARMv7 VFP3 instructions to implement
8345 // double precision, add, subtract, multiply, divide.
8346
8347 if (Token::MUL == op_) {
8348 __ vmul(d5, d6, d7);
8349 } else if (Token::DIV == op_) {
8350 __ vdiv(d5, d6, d7);
8351 } else if (Token::ADD == op_) {
8352 __ vadd(d5, d6, d7);
8353 } else if (Token::SUB == op_) {
8354 __ vsub(d5, d6, d7);
8355 } else {
8356 UNREACHABLE();
8357 }
8358 __ sub(r0, r5, Operand(kHeapObjectTag));
8359 __ vstr(d5, r0, HeapNumber::kValueOffset);
8360 __ add(r0, r0, Operand(kHeapObjectTag));
8361 __ mov(pc, lr);
8362 } else {
8363 // If we did not inline the operation, then the arguments are in:
8364 // r0: Left value (least significant part of mantissa).
8365 // r1: Left value (sign, exponent, top of mantissa).
8366 // r2: Right value (least significant part of mantissa).
8367 // r3: Right value (sign, exponent, top of mantissa).
8368 // r5: Address of heap number for result.
8369
8370 __ push(lr); // For later.
8371 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
8372 // Call C routine that may not cause GC or other trouble. r5 is callee
8373 // save.
8374 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
8375 // Store answer in the overwritable heap number.
8376 #if !defined(USE_ARM_EABI)
8377 // Double returned in fp coprocessor register 0 and 1, encoded as
8378 // register cr8. Offsets must be divisible by 4 for coprocessor so we
8379 // need to substract the tag from r5.
8380 __ sub(r4, r5, Operand(kHeapObjectTag));
8381 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
8382 #else
8383 // Double returned in registers 0 and 1.
8384 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8385 #endif
8386 __ mov(r0, Operand(r5));
8387 // And we are done.
8388 __ pop(pc);
8389 }
8390 }
8391 }
8392
8393 if (!generate_code_to_calculate_answer &&
8394 !slow_reverse.is_linked() &&
8395 !slow.is_linked()) {
8396 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008397 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008398
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008399 if (lhs.is(r0)) {
8400 __ b(&slow);
8401 __ bind(&slow_reverse);
8402 __ Swap(r0, r1, ip);
8403 }
8404
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008405 heap_number_map = no_reg; // Don't use this any more from here on.
8406
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008407 // We jump to here if something goes wrong (one param is not a number of any
8408 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008409 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008410
8411 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008412 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008413
ager@chromium.org357bf652010-04-12 11:30:10 +00008414 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008415 // Test for string arguments before calling runtime.
8416 // r1 : first argument
8417 // r0 : second argument
8418 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008419 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008420
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008421 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008422 __ tst(r1, Operand(kSmiTagMask));
8423 __ b(eq, &not_string1);
8424 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8425 __ b(ge, &not_string1);
8426
8427 // First argument is a a string, test second.
8428 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008429 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008430 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8431 __ b(ge, &string1);
8432
8433 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008434 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8435 __ TailCallStub(&string_add_stub);
8436
8437 __ bind(&string1_smi2);
8438 // First argument is a string, second is a smi. Try to lookup the number
8439 // string for the smi in the number string cache.
8440 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008441 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008442
8443 // Replace second argument on stack and tailcall string add stub to make
8444 // the result.
8445 __ str(r2, MemOperand(sp, 0));
8446 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008447
8448 // Only first argument is a string.
8449 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008450 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8451
8452 // First argument was not a string, test second.
8453 __ bind(&not_string1);
8454 __ tst(r0, Operand(kSmiTagMask));
8455 __ b(eq, &not_strings);
8456 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8457 __ b(ge, &not_strings);
8458
8459 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008460 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8461
8462 __ bind(&not_strings);
8463 }
8464
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008465 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008466}
8467
8468
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008469// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008470// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008471// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8472// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008473// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8474// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008475static void GetInt32(MacroAssembler* masm,
8476 Register source,
8477 Register dest,
8478 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008479 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008480 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008481 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008482 // Get exponent word.
8483 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8484 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008485 __ Ubfx(scratch2,
8486 scratch,
8487 HeapNumber::kExponentShift,
8488 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008489 // Load dest with zero. We use this either for the final shift or
8490 // for the answer.
8491 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008492 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008493 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8494 // the exponent that we are fastest at and also the highest exponent we can
8495 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008496 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8497 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8498 // split it up to avoid a constant pool entry. You can't do that in general
8499 // for cmp because of the overflow flag, but we know the exponent is in the
8500 // range 0-2047 so there is no overflow.
8501 int fudge_factor = 0x400;
8502 __ sub(scratch2, scratch2, Operand(fudge_factor));
8503 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008504 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8505 __ b(eq, &right_exponent);
8506 // If the exponent is higher than that then go to slow case. This catches
8507 // numbers that don't fit in a signed int32, infinities and NaNs.
8508 __ b(gt, slow);
8509
8510 // We know the exponent is smaller than 30 (biased). If it is less than
8511 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8512 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008513 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8514 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008515 // Dest already has a Smi zero.
8516 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008517 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008518 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8519 // get how much to shift down.
8520 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008521 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008522 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008523 if (CpuFeatures::IsSupported(VFP3)) {
8524 CpuFeatures::Scope scope(VFP3);
8525 // ARMv7 VFP3 instructions implementing double precision to integer
8526 // conversion using round to zero.
8527 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008528 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008529 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008530 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008531 } else {
8532 // Get the top bits of the mantissa.
8533 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8534 // Put back the implicit 1.
8535 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8536 // Shift up the mantissa bits to take up the space the exponent used to
8537 // take. We just orred in the implicit bit so that took care of one and
8538 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8539 // distance.
8540 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8541 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8542 // Put sign in zero flag.
8543 __ tst(scratch, Operand(HeapNumber::kSignMask));
8544 // Get the second half of the double. For some exponents we don't
8545 // actually need this because the bits get shifted out again, but
8546 // it's probably slower to test than just to do it.
8547 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8548 // Shift down 22 bits to get the last 10 bits.
8549 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8550 // Move down according to the exponent.
8551 __ mov(dest, Operand(scratch, LSR, dest));
8552 // Fix sign if sign bit was set.
8553 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8554 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008555 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008556}
8557
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008558// For bitwise ops where the inputs are not both Smis we here try to determine
8559// whether both inputs are either Smis or at least heap numbers that can be
8560// represented by a 32 bit signed value. We truncate towards zero as required
8561// by the ES spec. If this is the case we do the bitwise op and see if the
8562// result is a Smi. If so, great, otherwise we try to find a heap number to
8563// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008564// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8565void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8566 Register lhs,
8567 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008568 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008569 Label rhs_is_smi, lhs_is_smi;
8570 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008571
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008572 Register heap_number_map = r6;
8573 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8574
ager@chromium.org357bf652010-04-12 11:30:10 +00008575 __ tst(lhs, Operand(kSmiTagMask));
8576 __ 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 +00008577 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8578 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008579 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008580 GetInt32(masm, lhs, r3, r5, r4, &slow);
8581 __ jmp(&done_checking_lhs);
8582 __ bind(&lhs_is_smi);
8583 __ mov(r3, Operand(lhs, ASR, 1));
8584 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008585
ager@chromium.org357bf652010-04-12 11:30:10 +00008586 __ tst(rhs, Operand(kSmiTagMask));
8587 __ 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 +00008588 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8589 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008590 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008591 GetInt32(masm, rhs, r2, r5, r4, &slow);
8592 __ jmp(&done_checking_rhs);
8593 __ bind(&rhs_is_smi);
8594 __ mov(r2, Operand(rhs, ASR, 1));
8595 __ bind(&done_checking_rhs);
8596
8597 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008598
8599 // r0 and r1: Original operands (Smi or heap numbers).
8600 // r2 and r3: Signed int32 operands.
8601 switch (op_) {
8602 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8603 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8604 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8605 case Token::SAR:
8606 // Use only the 5 least significant bits of the shift count.
8607 __ and_(r2, r2, Operand(0x1f));
8608 __ mov(r2, Operand(r3, ASR, r2));
8609 break;
8610 case Token::SHR:
8611 // Use only the 5 least significant bits of the shift count.
8612 __ and_(r2, r2, Operand(0x1f));
8613 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8614 // SHR is special because it is required to produce a positive answer.
8615 // The code below for writing into heap numbers isn't capable of writing
8616 // the register as an unsigned int so we go to slow case if we hit this
8617 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008618 if (CpuFeatures::IsSupported(VFP3)) {
8619 __ b(mi, &result_not_a_smi);
8620 } else {
8621 __ b(mi, &slow);
8622 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008623 break;
8624 case Token::SHL:
8625 // Use only the 5 least significant bits of the shift count.
8626 __ and_(r2, r2, Operand(0x1f));
8627 __ mov(r2, Operand(r3, LSL, r2));
8628 break;
8629 default: UNREACHABLE();
8630 }
8631 // check that the *signed* result fits in a smi
8632 __ add(r3, r2, Operand(0x40000000), SetCC);
8633 __ b(mi, &result_not_a_smi);
8634 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8635 __ Ret();
8636
8637 Label have_to_allocate, got_a_heap_number;
8638 __ bind(&result_not_a_smi);
8639 switch (mode_) {
8640 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008641 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008642 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008643 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008644 break;
8645 }
8646 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008647 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008648 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008649 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008650 break;
8651 }
8652 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008653 // Get a new heap number in r5. r4 and r7 are scratch.
8654 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008655 }
8656 default: break;
8657 }
8658 __ bind(&got_a_heap_number);
8659 // r2: Answer as signed int32.
8660 // r5: Heap number to write answer into.
8661
8662 // Nothing can go wrong now, so move the heap number to r0, which is the
8663 // result.
8664 __ mov(r0, Operand(r5));
8665
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008666 if (CpuFeatures::IsSupported(VFP3)) {
8667 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8668 CpuFeatures::Scope scope(VFP3);
8669 __ vmov(s0, r2);
8670 if (op_ == Token::SHR) {
8671 __ vcvt_f64_u32(d0, s0);
8672 } else {
8673 __ vcvt_f64_s32(d0, s0);
8674 }
8675 __ sub(r3, r0, Operand(kHeapObjectTag));
8676 __ vstr(d0, r3, HeapNumber::kValueOffset);
8677 __ Ret();
8678 } else {
8679 // Tail call that writes the int32 in r2 to the heap number in r0, using
8680 // r3 as scratch. r0 is preserved and returned.
8681 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8682 __ TailCallStub(&stub);
8683 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008684
8685 if (mode_ != NO_OVERWRITE) {
8686 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008687 // Get a new heap number in r5. r4 and r7 are scratch.
8688 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008689 __ jmp(&got_a_heap_number);
8690 }
8691
8692 // If all else failed then we go to the runtime system.
8693 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008694 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008695 switch (op_) {
8696 case Token::BIT_OR:
8697 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8698 break;
8699 case Token::BIT_AND:
8700 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8701 break;
8702 case Token::BIT_XOR:
8703 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8704 break;
8705 case Token::SAR:
8706 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8707 break;
8708 case Token::SHR:
8709 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8710 break;
8711 case Token::SHL:
8712 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8713 break;
8714 default:
8715 UNREACHABLE();
8716 }
8717}
8718
8719
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008720// Can we multiply by x with max two shifts and an add.
8721// This answers yes to all integers from 2 to 10.
8722static bool IsEasyToMultiplyBy(int x) {
8723 if (x < 2) return false; // Avoid special cases.
8724 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8725 if (IsPowerOf2(x)) return true; // Simple shift.
8726 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8727 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8728 return false;
8729}
8730
8731
8732// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8733// Source and destination may be the same register. This routine does
8734// not set carry and overflow the way a mul instruction would.
8735static void MultiplyByKnownInt(MacroAssembler* masm,
8736 Register source,
8737 Register destination,
8738 int known_int) {
8739 if (IsPowerOf2(known_int)) {
8740 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8741 } else if (PopCountLessThanEqual2(known_int)) {
8742 int first_bit = BitPosition(known_int);
8743 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8744 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8745 if (first_bit != 0) {
8746 __ mov(destination, Operand(destination, LSL, first_bit));
8747 }
8748 } else {
8749 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8750 int the_bit = BitPosition(known_int + 1);
8751 __ rsb(destination, source, Operand(source, LSL, the_bit));
8752 }
8753}
8754
8755
8756// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8757// a register for the cases where it doesn't know a good trick, and may deliver
8758// a result that needs shifting.
8759static void MultiplyByKnownInt2(
8760 MacroAssembler* masm,
8761 Register result,
8762 Register source,
8763 Register known_int_register, // Smi tagged.
8764 int known_int,
8765 int* required_shift) { // Including Smi tag shift
8766 switch (known_int) {
8767 case 3:
8768 __ add(result, source, Operand(source, LSL, 1));
8769 *required_shift = 1;
8770 break;
8771 case 5:
8772 __ add(result, source, Operand(source, LSL, 2));
8773 *required_shift = 1;
8774 break;
8775 case 6:
8776 __ add(result, source, Operand(source, LSL, 1));
8777 *required_shift = 2;
8778 break;
8779 case 7:
8780 __ rsb(result, source, Operand(source, LSL, 3));
8781 *required_shift = 1;
8782 break;
8783 case 9:
8784 __ add(result, source, Operand(source, LSL, 3));
8785 *required_shift = 1;
8786 break;
8787 case 10:
8788 __ add(result, source, Operand(source, LSL, 2));
8789 *required_shift = 2;
8790 break;
8791 default:
8792 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8793 __ mul(result, source, known_int_register);
8794 *required_shift = 0;
8795 }
8796}
8797
8798
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008799// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8800// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8801// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8802// number from 0 to mask. On exit the 'eq' condition flags are set if the
8803// answer is exactly the mask.
8804void IntegerModStub::DigitSum(MacroAssembler* masm,
8805 Register lhs,
8806 int mask,
8807 int shift,
8808 Label* entry) {
8809 ASSERT(mask > 0);
8810 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8811 Label loop;
8812 __ bind(&loop);
8813 __ and_(ip, lhs, Operand(mask));
8814 __ add(lhs, ip, Operand(lhs, LSR, shift));
8815 __ bind(entry);
8816 __ cmp(lhs, Operand(mask));
8817 __ b(gt, &loop);
8818}
8819
8820
8821void IntegerModStub::DigitSum(MacroAssembler* masm,
8822 Register lhs,
8823 Register scratch,
8824 int mask,
8825 int shift1,
8826 int shift2,
8827 Label* entry) {
8828 ASSERT(mask > 0);
8829 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8830 Label loop;
8831 __ bind(&loop);
8832 __ bic(scratch, lhs, Operand(mask));
8833 __ and_(ip, lhs, Operand(mask));
8834 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8835 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8836 __ bind(entry);
8837 __ cmp(lhs, Operand(mask));
8838 __ b(gt, &loop);
8839}
8840
8841
8842// Splits the number into two halves (bottom half has shift bits). The top
8843// half is subtracted from the bottom half. If the result is negative then
8844// rhs is added.
8845void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8846 Register lhs,
8847 int shift,
8848 int rhs) {
8849 int mask = (1 << shift) - 1;
8850 __ and_(ip, lhs, Operand(mask));
8851 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8852 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8853}
8854
8855
8856void IntegerModStub::ModReduce(MacroAssembler* masm,
8857 Register lhs,
8858 int max,
8859 int denominator) {
8860 int limit = denominator;
8861 while (limit * 2 <= max) limit *= 2;
8862 while (limit >= denominator) {
8863 __ cmp(lhs, Operand(limit));
8864 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8865 limit >>= 1;
8866 }
8867}
8868
8869
8870void IntegerModStub::ModAnswer(MacroAssembler* masm,
8871 Register result,
8872 Register shift_distance,
8873 Register mask_bits,
8874 Register sum_of_digits) {
8875 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8876 __ Ret();
8877}
8878
8879
8880// See comment for class.
8881void IntegerModStub::Generate(MacroAssembler* masm) {
8882 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8883 __ bic(odd_number_, odd_number_, Operand(1));
8884 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8885 // We now have (odd_number_ - 1) * 2 in the register.
8886 // Build a switch out of branches instead of data because it avoids
8887 // having to teach the assembler about intra-code-object pointers
8888 // that are not in relative branch instructions.
8889 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8890 Label mod21, mod23, mod25;
8891 { Assembler::BlockConstPoolScope block_const_pool(masm);
8892 __ add(pc, pc, Operand(odd_number_));
8893 // When you read pc it is always 8 ahead, but when you write it you always
8894 // write the actual value. So we put in two nops to take up the slack.
8895 __ nop();
8896 __ nop();
8897 __ b(&mod3);
8898 __ b(&mod5);
8899 __ b(&mod7);
8900 __ b(&mod9);
8901 __ b(&mod11);
8902 __ b(&mod13);
8903 __ b(&mod15);
8904 __ b(&mod17);
8905 __ b(&mod19);
8906 __ b(&mod21);
8907 __ b(&mod23);
8908 __ b(&mod25);
8909 }
8910
8911 // For each denominator we find a multiple that is almost only ones
8912 // when expressed in binary. Then we do the sum-of-digits trick for
8913 // that number. If the multiple is not 1 then we have to do a little
8914 // more work afterwards to get the answer into the 0-denominator-1
8915 // range.
8916 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8917 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8918 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8919
8920 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8921 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8922 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8923
8924 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8925 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8926 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8927
8928 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8929 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8930 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8931
8932 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8933 ModReduce(masm, lhs_, 0x3f, 11);
8934 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8935
8936 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8937 ModReduce(masm, lhs_, 0xff, 13);
8938 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8939
8940 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8941 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8942 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8943
8944 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8945 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8946 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8947
8948 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8949 ModReduce(masm, lhs_, 0xff, 19);
8950 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8951
8952 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8953 ModReduce(masm, lhs_, 0x3f, 21);
8954 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8955
8956 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8957 ModReduce(masm, lhs_, 0xff, 23);
8958 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8959
8960 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8961 ModReduce(masm, lhs_, 0x7f, 25);
8962 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8963}
8964
8965
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008966const char* GenericBinaryOpStub::GetName() {
8967 if (name_ != NULL) return name_;
8968 const int len = 100;
8969 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8970 if (name_ == NULL) return "OOM";
8971 const char* op_name = Token::Name(op_);
8972 const char* overwrite_name;
8973 switch (mode_) {
8974 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8975 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8976 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8977 default: overwrite_name = "UnknownOverwrite"; break;
8978 }
8979
8980 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008981 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008982 op_name,
8983 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008984 specialized_on_rhs_ ? "_ConstantRhs" : "",
8985 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008986 return name_;
8987}
8988
8989
ager@chromium.org5c838252010-02-19 08:53:10 +00008990
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008991void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008992 // lhs_ : x
8993 // rhs_ : y
8994 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008995
ager@chromium.org357bf652010-04-12 11:30:10 +00008996 Register result = r0;
8997 Register lhs = lhs_;
8998 Register rhs = rhs_;
8999
9000 // This code can't cope with other register allocations yet.
9001 ASSERT(result.is(r0) &&
9002 ((lhs.is(r0) && rhs.is(r1)) ||
9003 (lhs.is(r1) && rhs.is(r0))));
9004
9005 Register smi_test_reg = VirtualFrame::scratch0();
9006 Register scratch = VirtualFrame::scratch1();
9007
9008 // All ops need to know whether we are dealing with two Smis. Set up
9009 // smi_test_reg to tell us that.
9010 if (ShouldGenerateSmiCode()) {
9011 __ orr(smi_test_reg, lhs, Operand(rhs));
9012 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009013
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009014 switch (op_) {
9015 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009016 Label not_smi;
9017 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00009018 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009019 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00009020 __ tst(smi_test_reg, Operand(kSmiTagMask));
9021 __ b(ne, &not_smi);
9022 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
9023 // Return if no overflow.
9024 __ Ret(vc);
9025 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
9026 }
9027 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009028 break;
9029 }
9030
9031 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009032 Label not_smi;
9033 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00009034 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009035 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00009036 __ tst(smi_test_reg, Operand(kSmiTagMask));
9037 __ b(ne, &not_smi);
9038 if (lhs.is(r1)) {
9039 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
9040 // Return if no overflow.
9041 __ Ret(vc);
9042 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
9043 } else {
9044 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
9045 // Return if no overflow.
9046 __ Ret(vc);
9047 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
9048 }
9049 }
9050 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009051 break;
9052 }
9053
9054 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009055 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00009056 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009057 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00009058 __ tst(smi_test_reg, Operand(kSmiTagMask));
9059 Register scratch2 = smi_test_reg;
9060 smi_test_reg = no_reg;
9061 __ b(ne, &not_smi);
9062 // Remove tag from one operand (but keep sign), so that result is Smi.
9063 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
9064 // Do multiplication
9065 // scratch = lower 32 bits of ip * lhs.
9066 __ smull(scratch, scratch2, lhs, ip);
9067 // Go slow on overflows (overflow bit is not set).
9068 __ mov(ip, Operand(scratch, ASR, 31));
9069 // No overflow if higher 33 bits are identical.
9070 __ cmp(ip, Operand(scratch2));
9071 __ b(ne, &slow);
9072 // Go slow on zero result to handle -0.
9073 __ tst(scratch, Operand(scratch));
9074 __ mov(result, Operand(scratch), LeaveCC, ne);
9075 __ Ret(ne);
9076 // We need -0 if we were multiplying a negative number with 0 to get 0.
9077 // We know one of them was zero.
9078 __ add(scratch2, rhs, Operand(lhs), SetCC);
9079 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
9080 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
9081 // Slow case. We fall through here if we multiplied a negative number
9082 // with 0, because that would mean we should produce -0.
9083 __ bind(&slow);
9084 }
9085 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009086 break;
9087 }
9088
9089 case Token::DIV:
9090 case Token::MOD: {
9091 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00009092 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009093 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00009094 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009095 if (IsPowerOf2(constant_rhs_)) {
9096 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009097 __ and_(rhs,
9098 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009099 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
9100 SetCC);
9101 // We now have the answer, but if the input was negative we also
9102 // have the sign bit. Our work is done if the result is
9103 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00009104 if (!rhs.is(r0)) {
9105 __ mov(r0, rhs, LeaveCC, pl);
9106 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009107 __ Ret(pl);
9108 // A mod of a negative left hand side must return a negative number.
9109 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00009110 // already optimistically trashed rhs so we may need to restore it.
9111 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009112 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00009113 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009114 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009115 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00009116 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009117 } else {
9118 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00009119 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009120 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009121 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009122 int shift = 0;
9123 int d = constant_rhs_;
9124 while ((d & 1) == 0) {
9125 d >>= 1;
9126 shift++;
9127 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009128 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009129 __ bic(r0, r0, Operand(kSmiTagMask));
9130 }
9131 } else {
9132 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00009133 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009134 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009135 // Find a fixed point reciprocal of the divisor so we can divide by
9136 // multiplying.
9137 double divisor = 1.0 / constant_rhs_;
9138 int shift = 32;
9139 double scale = 4294967296.0; // 1 << 32.
9140 uint32_t mul;
9141 // Maximise the precision of the fixed point reciprocal.
9142 while (true) {
9143 mul = static_cast<uint32_t>(scale * divisor);
9144 if (mul >= 0x7fffffff) break;
9145 scale *= 2.0;
9146 shift++;
9147 }
9148 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00009149 Register scratch2 = smi_test_reg;
9150 smi_test_reg = no_reg;
9151 __ mov(scratch2, Operand(mul));
9152 __ umull(scratch, scratch2, scratch2, lhs);
9153 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
9154 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
9155 // rhs is still the known rhs. rhs is Smi tagged.
9156 // lhs is still the unkown lhs. lhs is Smi tagged.
9157 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
9158 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009159 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00009160 scratch,
9161 scratch2,
9162 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009163 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00009164 &required_scratch_shift);
9165 // scratch << required_scratch_shift is now the Smi tagged rhs *
9166 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009167 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009168 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009169 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00009170 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009171 } else {
9172 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00009173 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009174 }
9175 }
9176 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009177 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009178 } else if (op_ == Token::MOD &&
9179 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
9180 runtime_operands_type_ != BinaryOpIC::STRINGS) {
9181 // Do generate a bit of smi code for modulus even though the default for
9182 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009183 // support for modulus checking for smis makes sense. We can handle
9184 // 1 to 25 times any power of 2. This covers over half the numbers from
9185 // 1 to 100 including all of the first 25. (Actually the constants < 10
9186 // are handled above by reciprocal multiplication. We only get here for
9187 // those cases if the right hand side is not a constant or for cases
9188 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
9189 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009190 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009191 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009192 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009193 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009194 // Check for two positive smis.
9195 __ orr(smi_test_reg, lhs, Operand(rhs));
9196 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
9197 __ b(ne, &slow);
9198 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009199 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009200 __ sub(scratch, rhs, Operand(1), SetCC);
9201 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009202 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
9203 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009204 // Calculate power of two modulus.
9205 __ and_(result, lhs, Operand(scratch));
9206 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009207
9208 __ bind(&not_power_of_2);
9209 __ eor(scratch, scratch, Operand(mask_bits));
9210 // At least two bits are set in the modulus. The high one(s) are in
9211 // mask_bits and the low one is scratch + 1.
9212 __ and_(mask_bits, scratch, Operand(lhs));
9213 Register shift_distance = scratch;
9214 scratch = no_reg;
9215
9216 // The rhs consists of a power of 2 multiplied by some odd number.
9217 // The power-of-2 part we handle by putting the corresponding bits
9218 // from the lhs in the mask_bits register, and the power in the
9219 // shift_distance register. Shift distance is never 0 due to Smi
9220 // tagging.
9221 __ CountLeadingZeros(r4, shift_distance, shift_distance);
9222 __ rsb(shift_distance, r4, Operand(32));
9223
9224 // Now we need to find out what the odd number is. The last bit is
9225 // always 1.
9226 Register odd_number = r4;
9227 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
9228 __ cmp(odd_number, Operand(25));
9229 __ b(gt, &slow);
9230
9231 IntegerModStub stub(
9232 result, shift_distance, odd_number, mask_bits, lhs, r5);
9233 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
9234
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009235 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009236 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009237 HandleBinaryOpSlowCases(
9238 masm,
9239 &not_smi,
9240 lhs,
9241 rhs,
9242 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009243 break;
9244 }
9245
9246 case Token::BIT_OR:
9247 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009248 case Token::BIT_XOR:
9249 case Token::SAR:
9250 case Token::SHR:
9251 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009252 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009253 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00009254 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009255 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009256 Register scratch2 = smi_test_reg;
9257 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009258 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009259 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
9260 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
9261 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009262 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009263 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00009264 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9265 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009266 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009267 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009268 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009269 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009270 // Remove tags from operands. We can't do this on a 31 bit number
9271 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00009272 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9273 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9274 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009275 // Unsigned shift is not allowed to produce a negative number, so
9276 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00009277 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009278 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009279 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009280 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009281 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009282 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009283 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00009284 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9285 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9286 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009287 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00009288 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009289 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009290 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009291 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009292 default: UNREACHABLE();
9293 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009294 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009295 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009296 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009297 break;
9298 }
9299
9300 default: UNREACHABLE();
9301 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009302 // This code should be unreachable.
9303 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00009304
9305 // Generate an unreachable reference to the DEFAULT stub so that it can be
9306 // found at the end of this stub when clearing ICs at GC.
9307 // TODO(kaznacheev): Check performance impact and get rid of this.
9308 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
9309 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
9310 __ CallStub(&uninit);
9311 }
9312}
9313
9314
9315void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
9316 Label get_result;
9317
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009318 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009319
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009320 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
9321 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00009322 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009323 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009324
9325 __ TailCallExternalReference(
9326 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009327 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00009328 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009329}
9330
9331
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009332Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009333 GenericBinaryOpStub stub(key, type_info);
9334 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009335}
9336
9337
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009338void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9339 // Argument is a number and is on stack and in r0.
9340 Label runtime_call;
9341 Label input_not_smi;
9342 Label loaded;
9343
9344 if (CpuFeatures::IsSupported(VFP3)) {
9345 // Load argument and check if it is a smi.
9346 __ BranchOnNotSmi(r0, &input_not_smi);
9347
9348 CpuFeatures::Scope scope(VFP3);
9349 // Input is a smi. Convert to double and load the low and high words
9350 // of the double into r2, r3.
9351 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
9352 __ b(&loaded);
9353
9354 __ bind(&input_not_smi);
9355 // Check if input is a HeapNumber.
9356 __ CheckMap(r0,
9357 r1,
9358 Heap::kHeapNumberMapRootIndex,
9359 &runtime_call,
9360 true);
9361 // Input is a HeapNumber. Load it to a double register and store the
9362 // low and high words into r2, r3.
9363 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
9364
9365 __ bind(&loaded);
9366 // r2 = low 32 bits of double value
9367 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009368 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009369 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
9370 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009371 __ eor(r1, r1, Operand(r1, ASR, 16));
9372 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009373 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009374 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009375
9376 // r2 = low 32 bits of double value.
9377 // r3 = high 32 bits of double value.
9378 // r1 = TranscendentalCache::hash(double value).
9379 __ mov(r0,
9380 Operand(ExternalReference::transcendental_cache_array_address()));
9381 // r0 points to cache array.
9382 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
9383 // r0 points to the cache for the type type_.
9384 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9385 __ cmp(r0, Operand(0));
9386 __ b(eq, &runtime_call);
9387
9388#ifdef DEBUG
9389 // Check that the layout of cache elements match expectations.
9390 { TranscendentalCache::Element test_elem[2];
9391 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9392 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9393 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9394 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9395 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9396 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9397 CHECK_EQ(0, elem_in0 - elem_start);
9398 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9399 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9400 }
9401#endif
9402
9403 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9404 __ add(r1, r1, Operand(r1, LSL, 1));
9405 __ add(r0, r0, Operand(r1, LSL, 2));
9406 // Check if cache matches: Double value is stored in uint32_t[2] array.
9407 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9408 __ cmp(r2, r4);
9409 __ b(ne, &runtime_call);
9410 __ cmp(r3, r5);
9411 __ b(ne, &runtime_call);
9412 // Cache hit. Load result, pop argument and return.
9413 __ mov(r0, Operand(r6));
9414 __ pop();
9415 __ Ret();
9416 }
9417
9418 __ bind(&runtime_call);
9419 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9420}
9421
9422
9423Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9424 switch (type_) {
9425 // Add more cases when necessary.
9426 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9427 case TranscendentalCache::COS: return Runtime::kMath_cos;
9428 default:
9429 UNIMPLEMENTED();
9430 return Runtime::kAbort;
9431 }
9432}
9433
9434
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009435void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009436 // Do tail-call to runtime routine. Runtime routines expect at least one
9437 // argument, so give it a Smi.
9438 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009439 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009440 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009441
9442 __ StubReturn(1);
9443}
9444
9445
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009446void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009447 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009448
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009449 Register heap_number_map = r6;
9450 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9451
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009452 if (op_ == Token::SUB) {
9453 // Check whether the value is a smi.
9454 Label try_float;
9455 __ tst(r0, Operand(kSmiTagMask));
9456 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009457
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009458 // Go slow case if the value of the expression is zero
9459 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009460 if (negative_zero_ == kStrictNegativeZero) {
9461 // If we have to check for zero, then we can check for the max negative
9462 // smi while we are at it.
9463 __ bic(ip, r0, Operand(0x80000000), SetCC);
9464 __ b(eq, &slow);
9465 __ rsb(r0, r0, Operand(0));
9466 __ StubReturn(1);
9467 } else {
9468 // The value of the expression is a smi and 0 is OK for -0. Try
9469 // optimistic subtraction '0 - value'.
9470 __ rsb(r0, r0, Operand(0), SetCC);
9471 __ StubReturn(1, vc);
9472 // We don't have to reverse the optimistic neg since the only case
9473 // where we fall through is the minimum negative Smi, which is the case
9474 // where the neg leaves the register unchanged.
9475 __ jmp(&slow); // Go slow on max negative Smi.
9476 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009477
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009478 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009479 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9480 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9481 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009482 __ b(ne, &slow);
9483 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009484 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009485 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9486 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9487 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9488 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009489 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009490 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9491 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9492 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9493 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9494 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9495 __ mov(r0, Operand(r1));
9496 }
9497 } else if (op_ == Token::BIT_NOT) {
9498 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009499 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9500 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9501 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009502 __ b(ne, &slow);
9503
9504 // Convert the heap number is r0 to an untagged integer in r1.
9505 GetInt32(masm, r0, r1, r2, r3, &slow);
9506
9507 // Do the bitwise operation (move negated) and check if the result
9508 // fits in a smi.
9509 Label try_float;
9510 __ mvn(r1, Operand(r1));
9511 __ add(r2, r1, Operand(0x40000000), SetCC);
9512 __ b(mi, &try_float);
9513 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9514 __ b(&done);
9515
9516 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009517 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009518 // Allocate a fresh heap number, but don't overwrite r0 until
9519 // we're sure we can do it without going through the slow case
9520 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009521 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009522 __ mov(r0, Operand(r2));
9523 }
9524
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009525 if (CpuFeatures::IsSupported(VFP3)) {
9526 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9527 CpuFeatures::Scope scope(VFP3);
9528 __ vmov(s0, r1);
9529 __ vcvt_f64_s32(d0, s0);
9530 __ sub(r2, r0, Operand(kHeapObjectTag));
9531 __ vstr(d0, r2, HeapNumber::kValueOffset);
9532 } else {
9533 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9534 // have to set up a frame.
9535 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9536 __ push(lr);
9537 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9538 __ pop(lr);
9539 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009540 } else {
9541 UNIMPLEMENTED();
9542 }
9543
9544 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009545 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009546
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009547 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009548 __ bind(&slow);
9549 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009550 switch (op_) {
9551 case Token::SUB:
9552 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9553 break;
9554 case Token::BIT_NOT:
9555 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9556 break;
9557 default:
9558 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009559 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009560}
9561
9562
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009563void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009564 // r0 holds the exception.
9565
9566 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009567 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009568
9569 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009570 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9571 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009572
9573 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009574 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009575 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009576 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009577 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009578 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9579
9580 // Before returning we restore the context from the frame pointer if
9581 // not NULL. The frame pointer is NULL in the exception handler of a
9582 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009583 __ cmp(fp, Operand(0));
9584 // Set cp to NULL if fp is NULL.
9585 __ mov(cp, Operand(0), LeaveCC, eq);
9586 // Restore cp otherwise.
9587 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009588#ifdef DEBUG
9589 if (FLAG_debug_code) {
9590 __ mov(lr, Operand(pc));
9591 }
9592#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009593 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009594 __ pop(pc);
9595}
9596
9597
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009598void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9599 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009600 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009601 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009602
9603 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009604 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009605 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009606
9607 // Unwind the handlers until the ENTRY handler is found.
9608 Label loop, done;
9609 __ bind(&loop);
9610 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009611 const int kStateOffset = StackHandlerConstants::kStateOffset;
9612 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009613 __ cmp(r2, Operand(StackHandler::ENTRY));
9614 __ b(eq, &done);
9615 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009616 const int kNextOffset = StackHandlerConstants::kNextOffset;
9617 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009618 __ jmp(&loop);
9619 __ bind(&done);
9620
9621 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009622 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009623 __ pop(r2);
9624 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009625
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009626 if (type == OUT_OF_MEMORY) {
9627 // Set external caught exception to false.
9628 ExternalReference external_caught(Top::k_external_caught_exception_address);
9629 __ mov(r0, Operand(false));
9630 __ mov(r2, Operand(external_caught));
9631 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009632
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009633 // Set pending exception and r0 to out of memory exception.
9634 Failure* out_of_memory = Failure::OutOfMemoryException();
9635 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9636 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9637 __ str(r0, MemOperand(r2));
9638 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009639
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009640 // Stack layout at this point. See also StackHandlerConstants.
9641 // sp -> state (ENTRY)
9642 // fp
9643 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009644
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009645 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009646 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009647 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
9648 // Before returning we restore the context from the frame pointer if
9649 // not NULL. The frame pointer is NULL in the exception handler of a
9650 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009651 __ cmp(fp, Operand(0));
9652 // Set cp to NULL if fp is NULL.
9653 __ mov(cp, Operand(0), LeaveCC, eq);
9654 // Restore cp otherwise.
9655 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009656#ifdef DEBUG
9657 if (FLAG_debug_code) {
9658 __ mov(lr, Operand(pc));
9659 }
9660#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009661 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009662 __ pop(pc);
9663}
9664
9665
9666void CEntryStub::GenerateCore(MacroAssembler* masm,
9667 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009668 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009669 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009670 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009671 bool always_allocate,
9672 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009673 // r0: result parameter for PerformGC, if any
9674 // r4: number of arguments including receiver (C callee-saved)
9675 // r5: pointer to builtin function (C callee-saved)
9676 // r6: pointer to the first argument (C callee-saved)
9677
9678 if (do_gc) {
9679 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009680 __ PrepareCallCFunction(1, r1);
9681 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009682 }
9683
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009684 ExternalReference scope_depth =
9685 ExternalReference::heap_always_allocate_scope_depth();
9686 if (always_allocate) {
9687 __ mov(r0, Operand(scope_depth));
9688 __ ldr(r1, MemOperand(r0));
9689 __ add(r1, r1, Operand(1));
9690 __ str(r1, MemOperand(r0));
9691 }
9692
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009693 // Call C built-in.
9694 // r0 = argc, r1 = argv
9695 __ mov(r0, Operand(r4));
9696 __ mov(r1, Operand(r6));
9697
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009698 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9699 int frame_alignment_mask = frame_alignment - 1;
9700#if defined(V8_HOST_ARCH_ARM)
9701 if (FLAG_debug_code) {
9702 if (frame_alignment > kPointerSize) {
9703 Label alignment_as_expected;
9704 ASSERT(IsPowerOf2(frame_alignment));
9705 __ sub(r2, sp, Operand(frame_alignment_skew));
9706 __ tst(r2, Operand(frame_alignment_mask));
9707 __ b(eq, &alignment_as_expected);
9708 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9709 __ stop("Unexpected alignment");
9710 __ bind(&alignment_as_expected);
9711 }
9712 }
9713#endif
9714
9715 // Just before the call (jump) below lr is pushed, so the actual alignment is
9716 // adding one to the current skew.
9717 int alignment_before_call =
9718 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9719 if (alignment_before_call > 0) {
9720 // Push until the alignment before the call is met.
9721 __ mov(r2, Operand(0));
9722 for (int i = alignment_before_call;
9723 (i & frame_alignment_mask) != 0;
9724 i += kPointerSize) {
9725 __ push(r2);
9726 }
9727 }
9728
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009729 // TODO(1242173): To let the GC traverse the return address of the exit
9730 // frames, we need to know where the return address is. Right now,
9731 // we push it on the stack to be able to find it again, but we never
9732 // restore from it in case of changes, which makes it impossible to
9733 // support moving the C entry code stub. This should be fixed, but currently
9734 // this is OK because the CEntryStub gets generated so early in the V8 boot
9735 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009736 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009737 masm->push(lr);
9738 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009739
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009740 // Restore sp back to before aligning the stack.
9741 if (alignment_before_call > 0) {
9742 __ add(sp, sp, Operand(alignment_before_call));
9743 }
9744
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009745 if (always_allocate) {
9746 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9747 // though (contain the result).
9748 __ mov(r2, Operand(scope_depth));
9749 __ ldr(r3, MemOperand(r2));
9750 __ sub(r3, r3, Operand(1));
9751 __ str(r3, MemOperand(r2));
9752 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009753
9754 // check for failure result
9755 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009756 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009757 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9758 __ add(r2, r0, Operand(1));
9759 __ tst(r2, Operand(kFailureTagMask));
9760 __ b(eq, &failure_returned);
9761
9762 // Exit C frame and return.
9763 // r0:r1: result
9764 // sp: stack pointer
9765 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009766 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009767
9768 // check if we should retry or throw exception
9769 Label retry;
9770 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009771 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009772 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9773 __ b(eq, &retry);
9774
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009775 // Special handling of out of memory exceptions.
9776 Failure* out_of_memory = Failure::OutOfMemoryException();
9777 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9778 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009779
9780 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009781 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009782 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009783 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009784 __ ldr(r0, MemOperand(ip));
9785 __ str(r3, MemOperand(ip));
9786
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009787 // Special handling of termination exceptions which are uncatchable
9788 // by javascript code.
9789 __ cmp(r0, Operand(Factory::termination_exception()));
9790 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009791
9792 // Handle normal exception.
9793 __ jmp(throw_normal_exception);
9794
9795 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9796}
9797
9798
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009799void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009800 // Called from JavaScript; parameters are on stack as if calling JS function
9801 // r0: number of arguments including receiver
9802 // r1: pointer to builtin function
9803 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009804 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009805 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009806
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009807 // Result returned in r0 or r0+r1 by default.
9808
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009809 // NOTE: Invocations of builtins may return failure objects
9810 // instead of a proper result. The builtin entry handles
9811 // this by performing a garbage collection and retrying the
9812 // builtin once.
9813
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009814 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009815 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009816
9817 // r4: number of arguments (C callee-saved)
9818 // r5: pointer to builtin function (C callee-saved)
9819 // r6: pointer to first argument (C callee-saved)
9820
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009821 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009822 Label throw_termination_exception;
9823 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009824
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009825 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009826 GenerateCore(masm,
9827 &throw_normal_exception,
9828 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009829 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009830 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009831 false,
9832 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009833
9834 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009835 GenerateCore(masm,
9836 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009837 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009838 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009839 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009840 false,
9841 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009842
9843 // Do full GC and retry runtime call one final time.
9844 Failure* failure = Failure::InternalError();
9845 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9846 GenerateCore(masm,
9847 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009848 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009849 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009850 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009851 true,
9852 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009853
9854 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009855 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9856
9857 __ bind(&throw_termination_exception);
9858 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009859
9860 __ bind(&throw_normal_exception);
9861 GenerateThrowTOS(masm);
9862}
9863
9864
9865void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9866 // r0: code entry
9867 // r1: function
9868 // r2: receiver
9869 // r3: argc
9870 // [sp+0]: argv
9871
9872 Label invoke, exit;
9873
9874 // Called from C, so do not pop argc and args on exit (preserve sp)
9875 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009876 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009877 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9878
9879 // Get address of argv, see stm above.
9880 // r0: code entry
9881 // r1: function
9882 // r2: receiver
9883 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009884 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009885
9886 // Push a frame with special values setup to mark it as an entry frame.
9887 // r0: code entry
9888 // r1: function
9889 // r2: receiver
9890 // r3: argc
9891 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009892 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009893 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9894 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009895 __ mov(r6, Operand(Smi::FromInt(marker)));
9896 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9897 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009898 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009899
9900 // Setup frame pointer for the frame to be pushed.
9901 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9902
9903 // Call a faked try-block that does the invoke.
9904 __ bl(&invoke);
9905
9906 // Caught exception: Store result (exception) in the pending
9907 // exception field in the JSEnv and return a failure sentinel.
9908 // Coming in here the fp will be invalid because the PushTryHandler below
9909 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009910 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009911 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009912 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009913 __ b(&exit);
9914
9915 // Invoke: Link this frame into the handler chain.
9916 __ bind(&invoke);
9917 // Must preserve r0-r4, r5-r7 are available.
9918 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009919 // If an exception not caught by another handler occurs, this handler
9920 // returns control to the code after the bl(&invoke) above, which
9921 // restores all kCalleeSaved registers (including cp and fp) to their
9922 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009923
9924 // Clear any pending exceptions.
9925 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9926 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009927 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009928 __ str(r5, MemOperand(ip));
9929
9930 // Invoke the function by calling through JS entry trampoline builtin.
9931 // Notice that we cannot store a reference to the trampoline code directly in
9932 // this stub, because runtime stubs are not traversed when doing GC.
9933
9934 // Expected registers by Builtins::JSEntryTrampoline
9935 // r0: code entry
9936 // r1: function
9937 // r2: receiver
9938 // r3: argc
9939 // r4: argv
9940 if (is_construct) {
9941 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9942 __ mov(ip, Operand(construct_entry));
9943 } else {
9944 ExternalReference entry(Builtins::JSEntryTrampoline);
9945 __ mov(ip, Operand(entry));
9946 }
9947 __ ldr(ip, MemOperand(ip)); // deref address
9948
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009949 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9950 // macro for the add instruction because we don't want the coverage tool
9951 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009952 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009953 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009954
9955 // Unlink this frame from the handler chain. When reading the
9956 // address of the next handler, there is no need to use the address
9957 // displacement since the current stack pointer (sp) points directly
9958 // to the stack handler.
9959 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9960 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9961 __ str(r3, MemOperand(ip));
9962 // No need to restore registers
9963 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9964
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009965
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009966 __ bind(&exit); // r0 holds result
9967 // Restore the top frame descriptors from the stack.
9968 __ pop(r3);
9969 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9970 __ str(r3, MemOperand(ip));
9971
9972 // Reset the stack to the callee saved registers.
9973 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9974
9975 // Restore callee-saved registers and return.
9976#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009977 if (FLAG_debug_code) {
9978 __ mov(lr, Operand(pc));
9979 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009980#endif
9981 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9982}
9983
9984
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009985// This stub performs an instanceof, calling the builtin function if
9986// necessary. Uses r1 for the object, r0 for the function that it may
9987// be an instance of (these are fetched from the stack).
9988void InstanceofStub::Generate(MacroAssembler* masm) {
9989 // Get the object - slow case for smis (we may need to throw an exception
9990 // depending on the rhs).
9991 Label slow, loop, is_instance, is_not_instance;
9992 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9993 __ BranchOnSmi(r0, &slow);
9994
9995 // Check that the left hand is a JS object and put map in r3.
9996 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9997 __ b(lt, &slow);
9998 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9999 __ b(gt, &slow);
10000
10001 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +000010002 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010003 // r1 is function, r3 is map.
10004
10005 // Look up the function and the map in the instanceof cache.
10006 Label miss;
10007 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
10008 __ cmp(r1, ip);
10009 __ b(ne, &miss);
10010 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
10011 __ cmp(r3, ip);
10012 __ b(ne, &miss);
10013 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
10014 __ pop();
10015 __ pop();
10016 __ mov(pc, Operand(lr));
10017
10018 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010019 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
10020
10021 // Check that the function prototype is a JS object.
10022 __ BranchOnSmi(r4, &slow);
10023 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
10024 __ b(lt, &slow);
10025 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
10026 __ b(gt, &slow);
10027
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010028 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
10029 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
10030
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010031 // Register mapping: r3 is object map and r4 is function prototype.
10032 // Get prototype of object into r2.
10033 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
10034
10035 // Loop through the prototype chain looking for the function prototype.
10036 __ bind(&loop);
10037 __ cmp(r2, Operand(r4));
10038 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +000010039 __ LoadRoot(ip, Heap::kNullValueRootIndex);
10040 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010041 __ b(eq, &is_not_instance);
10042 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
10043 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
10044 __ jmp(&loop);
10045
10046 __ bind(&is_instance);
10047 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010048 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010049 __ pop();
10050 __ pop();
10051 __ mov(pc, Operand(lr)); // Return.
10052
10053 __ bind(&is_not_instance);
10054 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010055 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010056 __ pop();
10057 __ pop();
10058 __ mov(pc, Operand(lr)); // Return.
10059
10060 // Slow-case. Tail call builtin.
10061 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010062 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
10063}
10064
10065
ager@chromium.org7c537e22008-10-16 08:43:32 +000010066void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
10067 // The displacement is the offset of the last parameter (if any)
10068 // relative to the frame pointer.
10069 static const int kDisplacement =
10070 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010071
ager@chromium.org7c537e22008-10-16 08:43:32 +000010072 // Check that the key is a smi.
10073 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010074 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010075
ager@chromium.org7c537e22008-10-16 08:43:32 +000010076 // Check if the calling frame is an arguments adaptor frame.
10077 Label adaptor;
10078 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
10079 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +000010080 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +000010081 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010082
ager@chromium.org7c537e22008-10-16 08:43:32 +000010083 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000010084 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +000010085 // check for free.
10086 __ cmp(r1, r0);
10087 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010088
ager@chromium.org7c537e22008-10-16 08:43:32 +000010089 // Read the argument from the stack and return it.
10090 __ sub(r3, r0, r1);
10091 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10092 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010093 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010094
10095 // Arguments adaptor case: Check index against actual arguments
10096 // limit found in the arguments adaptor frame. Use unsigned
10097 // comparison to get negative check for free.
10098 __ bind(&adaptor);
10099 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10100 __ cmp(r1, r0);
10101 __ b(cs, &slow);
10102
10103 // Read the argument from the adaptor frame and return it.
10104 __ sub(r3, r0, r1);
10105 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10106 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010107 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010108
10109 // Slow-case: Handle non-smi or out-of-bounds access to arguments
10110 // by calling the runtime system.
10111 __ bind(&slow);
10112 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010113 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010114}
10115
10116
10117void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010118 // sp[0] : number of parameters
10119 // sp[4] : receiver displacement
10120 // sp[8] : function
10121
ager@chromium.org7c537e22008-10-16 08:43:32 +000010122 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +000010123 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +000010124 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
10125 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +000010126 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +000010127 __ b(eq, &adaptor_frame);
10128
10129 // Get the length from the frame.
10130 __ ldr(r1, MemOperand(sp, 0));
10131 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010132
10133 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +000010134 __ bind(&adaptor_frame);
10135 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10136 __ str(r1, MemOperand(sp, 0));
10137 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +000010138 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
10139 __ str(r3, MemOperand(sp, 1 * kPointerSize));
10140
ager@chromium.org5c838252010-02-19 08:53:10 +000010141 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010142 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +000010143 Label add_arguments_object;
10144 __ bind(&try_allocate);
10145 __ cmp(r1, Operand(0));
10146 __ b(eq, &add_arguments_object);
10147 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
10148 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
10149 __ bind(&add_arguments_object);
10150 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
10151
10152 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010153 __ AllocateInNewSpace(
10154 r1,
10155 r0,
10156 r2,
10157 r3,
10158 &runtime,
10159 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +000010160
10161 // Get the arguments boilerplate from the current (global) context.
10162 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
10163 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
10164 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
10165 __ ldr(r4, MemOperand(r4, offset));
10166
10167 // Copy the JS object part.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +000010168 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +000010169
10170 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010171 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010172 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
10173 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
10174
10175 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010176 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010177 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
10178 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
10179
10180 // If there are no actual arguments, we're done.
10181 Label done;
10182 __ cmp(r1, Operand(0));
10183 __ b(eq, &done);
10184
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010185 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +000010186 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000010187
10188 // Setup the elements pointer in the allocated arguments object and
10189 // initialize the header in the elements fixed array.
10190 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
10191 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
10192 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
10193 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
10194 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010195 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +000010196
10197 // Copy the fixed array slots.
10198 Label loop;
10199 // Setup r4 to point to the first array slot.
10200 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
10201 __ bind(&loop);
10202 // Pre-decrement r2 with kPointerSize on each iteration.
10203 // Pre-decrement in order to skip receiver.
10204 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
10205 // Post-increment r4 with kPointerSize on each iteration.
10206 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
10207 __ sub(r1, r1, Operand(1));
10208 __ cmp(r1, Operand(0));
10209 __ b(ne, &loop);
10210
10211 // Return and remove the on-stack parameters.
10212 __ bind(&done);
10213 __ add(sp, sp, Operand(3 * kPointerSize));
10214 __ Ret();
10215
ager@chromium.org7c537e22008-10-16 08:43:32 +000010216 // Do the runtime call to allocate the arguments object.
10217 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010218 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010219}
10220
10221
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010222void RegExpExecStub::Generate(MacroAssembler* masm) {
10223 // Just jump directly to runtime if native RegExp is not selected at compile
10224 // time or if regexp entry in generated code is turned off runtime switch or
10225 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010226#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010227 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010228#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010229 if (!FLAG_regexp_entry_native) {
10230 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10231 return;
10232 }
10233
10234 // Stack frame on entry.
10235 // sp[0]: last_match_info (expected JSArray)
10236 // sp[4]: previous index
10237 // sp[8]: subject string
10238 // sp[12]: JSRegExp object
10239
10240 static const int kLastMatchInfoOffset = 0 * kPointerSize;
10241 static const int kPreviousIndexOffset = 1 * kPointerSize;
10242 static const int kSubjectOffset = 2 * kPointerSize;
10243 static const int kJSRegExpOffset = 3 * kPointerSize;
10244
10245 Label runtime, invoke_regexp;
10246
10247 // Allocation of registers for this function. These are in callee save
10248 // registers and will be preserved by the call to the native RegExp code, as
10249 // this code is called using the normal C calling convention. When calling
10250 // directly from generated code the native RegExp code will not do a GC and
10251 // therefore the content of these registers are safe to use after the call.
10252 Register subject = r4;
10253 Register regexp_data = r5;
10254 Register last_match_info_elements = r6;
10255
10256 // Ensure that a RegExp stack is allocated.
10257 ExternalReference address_of_regexp_stack_memory_address =
10258 ExternalReference::address_of_regexp_stack_memory_address();
10259 ExternalReference address_of_regexp_stack_memory_size =
10260 ExternalReference::address_of_regexp_stack_memory_size();
10261 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
10262 __ ldr(r0, MemOperand(r0, 0));
10263 __ tst(r0, Operand(r0));
10264 __ b(eq, &runtime);
10265
10266 // Check that the first argument is a JSRegExp object.
10267 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010268 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010269 __ tst(r0, Operand(kSmiTagMask));
10270 __ b(eq, &runtime);
10271 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
10272 __ b(ne, &runtime);
10273
10274 // Check that the RegExp has been compiled (data contains a fixed array).
10275 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
10276 if (FLAG_debug_code) {
10277 __ tst(regexp_data, Operand(kSmiTagMask));
10278 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
10279 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
10280 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
10281 }
10282
10283 // regexp_data: RegExp data (FixedArray)
10284 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
10285 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
10286 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
10287 __ b(ne, &runtime);
10288
10289 // regexp_data: RegExp data (FixedArray)
10290 // Check that the number of captures fit in the static offsets vector buffer.
10291 __ ldr(r2,
10292 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10293 // Calculate number of capture registers (number_of_captures + 1) * 2. This
10294 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010295 STATIC_ASSERT(kSmiTag == 0);
10296 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010297 __ add(r2, r2, Operand(2)); // r2 was a smi.
10298 // Check that the static offsets vector buffer is large enough.
10299 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
10300 __ b(hi, &runtime);
10301
10302 // r2: Number of capture registers
10303 // regexp_data: RegExp data (FixedArray)
10304 // Check that the second argument is a string.
10305 __ ldr(subject, MemOperand(sp, kSubjectOffset));
10306 __ tst(subject, Operand(kSmiTagMask));
10307 __ b(eq, &runtime);
10308 Condition is_string = masm->IsObjectStringType(subject, r0);
10309 __ b(NegateCondition(is_string), &runtime);
10310 // Get the length of the string to r3.
10311 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
10312
10313 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +000010314 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010315 // subject: Subject string
10316 // regexp_data: RegExp data (FixedArray)
10317 // Check that the third argument is a positive smi less than the subject
10318 // string length. A negative value will be greater (unsigned comparison).
10319 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010320 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010321 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010322 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010323 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010324
10325 // r2: Number of capture registers
10326 // subject: Subject string
10327 // regexp_data: RegExp data (FixedArray)
10328 // Check that the fourth object is a JSArray object.
10329 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10330 __ tst(r0, Operand(kSmiTagMask));
10331 __ b(eq, &runtime);
10332 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
10333 __ b(ne, &runtime);
10334 // Check that the JSArray is in fast case.
10335 __ ldr(last_match_info_elements,
10336 FieldMemOperand(r0, JSArray::kElementsOffset));
10337 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010338 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000010339 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010340 __ b(ne, &runtime);
10341 // Check that the last match info has space for the capture registers and the
10342 // additional information.
10343 __ ldr(r0,
10344 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
10345 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010346 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010347 __ b(gt, &runtime);
10348
10349 // subject: Subject string
10350 // regexp_data: RegExp data (FixedArray)
10351 // Check the representation and encoding of the subject string.
10352 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010353 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10354 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010355 // First check for flat string.
10356 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010357 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010358 __ b(eq, &seq_string);
10359
10360 // subject: Subject string
10361 // regexp_data: RegExp data (FixedArray)
10362 // Check for flat cons string.
10363 // A flat cons string is a cons string where the second part is the empty
10364 // string. In that case the subject string is just the first part of the cons
10365 // string. Also in this case the first part of the cons string is known to be
10366 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010367 STATIC_ASSERT(kExternalStringTag !=0);
10368 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010369 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010370 __ b(ne, &runtime);
10371 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
10372 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
10373 __ cmp(r0, r1);
10374 __ b(ne, &runtime);
10375 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
10376 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10377 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010378 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010379 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010380 __ tst(r0, Operand(kStringRepresentationMask));
10381 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010382
10383 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010384 // subject: Subject string
10385 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010386 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010387 STATIC_ASSERT(4 == kAsciiStringTag);
10388 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010389 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010390 __ and_(r0, r0, Operand(kStringEncodingMask));
10391 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010392 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10393 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10394
10395 // Check that the irregexp code has been generated for the actual string
10396 // encoding. If it has, the field contains a code object otherwise it contains
10397 // the hole.
10398 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10399 __ b(ne, &runtime);
10400
10401 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10402 // r7: code
10403 // subject: Subject string
10404 // regexp_data: RegExp data (FixedArray)
10405 // Load used arguments before starting to push arguments for call to native
10406 // RegExp code to avoid handling changing stack height.
10407 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10408 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10409
10410 // r1: previous index
10411 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10412 // r7: code
10413 // subject: Subject string
10414 // regexp_data: RegExp data (FixedArray)
10415 // All checks done. Now push arguments for native regexp code.
10416 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10417
10418 static const int kRegExpExecuteArguments = 7;
10419 __ push(lr);
10420 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10421
10422 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10423 __ mov(r0, Operand(1));
10424 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10425
10426 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10427 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10428 __ ldr(r0, MemOperand(r0, 0));
10429 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10430 __ ldr(r2, MemOperand(r2, 0));
10431 __ add(r0, r0, Operand(r2));
10432 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10433
10434 // Argument 5 (sp[0]): static offsets vector buffer.
10435 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10436 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10437
10438 // For arguments 4 and 3 get string length, calculate start of string data and
10439 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10440 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010441 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010442 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010443 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10444 __ eor(r3, r3, Operand(1));
10445 // Argument 4 (r3): End of string data
10446 // Argument 3 (r2): Start of string data
10447 __ add(r2, r9, Operand(r1, LSL, r3));
10448 __ add(r3, r9, Operand(r0, LSL, r3));
10449
10450 // Argument 2 (r1): Previous index.
10451 // Already there
10452
10453 // Argument 1 (r0): Subject string.
10454 __ mov(r0, subject);
10455
10456 // Locate the code entry and call it.
10457 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10458 __ CallCFunction(r7, kRegExpExecuteArguments);
10459 __ pop(lr);
10460
10461 // r0: result
10462 // subject: subject string (callee saved)
10463 // regexp_data: RegExp data (callee saved)
10464 // last_match_info_elements: Last match info elements (callee saved)
10465
10466 // Check the result.
10467 Label success;
10468 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10469 __ b(eq, &success);
10470 Label failure;
10471 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10472 __ b(eq, &failure);
10473 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10474 // If not exception it can only be retry. Handle that in the runtime system.
10475 __ b(ne, &runtime);
10476 // Result must now be exception. If there is no pending exception already a
10477 // stack overflow (on the backtrack stack) was detected in RegExp code but
10478 // haven't created the exception yet. Handle that in the runtime system.
10479 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10480 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10481 __ ldr(r0, MemOperand(r0, 0));
10482 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10483 __ ldr(r1, MemOperand(r1, 0));
10484 __ cmp(r0, r1);
10485 __ b(eq, &runtime);
10486 __ bind(&failure);
10487 // For failure and exception return null.
10488 __ mov(r0, Operand(Factory::null_value()));
10489 __ add(sp, sp, Operand(4 * kPointerSize));
10490 __ Ret();
10491
10492 // Process the result from the native regexp code.
10493 __ bind(&success);
10494 __ ldr(r1,
10495 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10496 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010497 STATIC_ASSERT(kSmiTag == 0);
10498 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010499 __ add(r1, r1, Operand(2)); // r1 was a smi.
10500
10501 // r1: number of capture registers
10502 // r4: subject string
10503 // Store the capture count.
10504 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10505 __ str(r2, FieldMemOperand(last_match_info_elements,
10506 RegExpImpl::kLastCaptureCountOffset));
10507 // Store last subject and last input.
10508 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010509 __ str(subject,
10510 FieldMemOperand(last_match_info_elements,
10511 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010512 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010513 __ str(subject,
10514 FieldMemOperand(last_match_info_elements,
10515 RegExpImpl::kLastInputOffset));
10516 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010517 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010518
10519 // Get the static offsets vector filled by the native regexp code.
10520 ExternalReference address_of_static_offsets_vector =
10521 ExternalReference::address_of_static_offsets_vector();
10522 __ mov(r2, Operand(address_of_static_offsets_vector));
10523
10524 // r1: number of capture registers
10525 // r2: offsets vector
10526 Label next_capture, done;
10527 // Capture register counter starts from number of capture registers and
10528 // counts down until wraping after zero.
10529 __ add(r0,
10530 last_match_info_elements,
10531 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10532 __ bind(&next_capture);
10533 __ sub(r1, r1, Operand(1), SetCC);
10534 __ b(mi, &done);
10535 // Read the value from the static offsets vector buffer.
10536 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10537 // Store the smi value in the last match info.
10538 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10539 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10540 __ jmp(&next_capture);
10541 __ bind(&done);
10542
10543 // Return last match info.
10544 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10545 __ add(sp, sp, Operand(4 * kPointerSize));
10546 __ Ret();
10547
10548 // Do the runtime call to execute the regexp.
10549 __ bind(&runtime);
10550 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010551#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010552}
10553
10554
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010555void CallFunctionStub::Generate(MacroAssembler* masm) {
10556 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010557
10558 // If the receiver might be a value (string, number or boolean) check for this
10559 // and box it if it is.
10560 if (ReceiverMightBeValue()) {
10561 // Get the receiver from the stack.
10562 // function, receiver [, arguments]
10563 Label receiver_is_value, receiver_is_js_object;
10564 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10565
10566 // Check if receiver is a smi (which is a number value).
10567 __ BranchOnSmi(r1, &receiver_is_value);
10568
10569 // Check if the receiver is a valid JS object.
10570 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10571 __ b(ge, &receiver_is_js_object);
10572
10573 // Call the runtime to box the value.
10574 __ bind(&receiver_is_value);
10575 __ EnterInternalFrame();
10576 __ push(r1);
10577 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10578 __ LeaveInternalFrame();
10579 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10580
10581 __ bind(&receiver_is_js_object);
10582 }
10583
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010584 // Get the function to call from the stack.
10585 // function, receiver [, arguments]
10586 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10587
10588 // Check that the function is really a JavaScript function.
10589 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010590 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010591 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010592 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010593 __ b(ne, &slow);
10594
10595 // Fast-case: Invoke the function now.
10596 // r1: pushed function
10597 ParameterCount actual(argc_);
10598 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10599
10600 // Slow-case: Non-function called.
10601 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010602 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10603 // of the original receiver from the call site).
10604 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010605 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010606 __ mov(r2, Operand(0));
10607 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10608 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10609 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010610}
10611
10612
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010613// Unfortunately you have to run without snapshots to see most of these
10614// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010615const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010616 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10617 (lhs_.is(r1) && rhs_.is(r0)));
10618
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010619 if (name_ != NULL) return name_;
10620 const int kMaxNameLength = 100;
10621 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10622 if (name_ == NULL) return "OOM";
10623
10624 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010625 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010626 case lt: cc_name = "LT"; break;
10627 case gt: cc_name = "GT"; break;
10628 case le: cc_name = "LE"; break;
10629 case ge: cc_name = "GE"; break;
10630 case eq: cc_name = "EQ"; break;
10631 case ne: cc_name = "NE"; break;
10632 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010633 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010634
ager@chromium.orgb5737492010-07-15 09:29:43 +000010635 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10636 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10637
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010638 const char* strict_name = "";
10639 if (strict_ && (cc_ == eq || cc_ == ne)) {
10640 strict_name = "_STRICT";
10641 }
10642
10643 const char* never_nan_nan_name = "";
10644 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10645 never_nan_nan_name = "_NO_NAN";
10646 }
10647
10648 const char* include_number_compare_name = "";
10649 if (!include_number_compare_) {
10650 include_number_compare_name = "_NO_NUMBER";
10651 }
10652
10653 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010654 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010655 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010656 lhs_name,
10657 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010658 strict_name,
10659 never_nan_nan_name,
10660 include_number_compare_name);
10661 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010662}
10663
10664
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010665int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010666 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10667 // stubs the never NaN NaN condition is only taken into account if the
10668 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010669 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10670 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10671 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010672 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010673 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010674 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010675 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10676 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010677}
10678
10679
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010680// StringCharCodeAtGenerator
10681
10682void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10683 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010684 Label ascii_string;
10685 Label got_char_code;
10686
10687 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010688 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010689
10690 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010691 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10692 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010693 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010694 __ tst(result_, Operand(kIsNotStringMask));
10695 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010696
10697 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010698 __ BranchOnNotSmi(index_, &index_not_smi_);
10699
10700 // Put smi-tagged index into scratch register.
10701 __ mov(scratch_, index_);
10702 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010703
10704 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010705 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10706 __ cmp(ip, Operand(scratch_));
10707 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010708
10709 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010710 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010711 __ tst(result_, Operand(kStringRepresentationMask));
10712 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010713
10714 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010715 __ tst(result_, Operand(kIsConsStringMask));
10716 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010717
10718 // ConsString.
10719 // Check whether the right hand side is the empty string (i.e. if
10720 // this is really a flat string in a cons string). If that is not
10721 // the case we would rather go to the runtime system now to flatten
10722 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010723 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10724 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10725 __ cmp(result_, Operand(ip));
10726 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010727 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010728 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10729 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10730 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10731 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010732 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010733 __ tst(result_, Operand(kStringRepresentationMask));
10734 __ b(nz, &call_runtime_);
10735
10736 // Check for 1-byte or 2-byte string.
10737 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010738 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010739 __ tst(result_, Operand(kStringEncodingMask));
10740 __ b(nz, &ascii_string);
10741
10742 // 2-byte string.
10743 // Load the 2-byte character code into the result register. We can
10744 // add without shifting since the smi tag size is the log2 of the
10745 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010746 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010747 __ add(scratch_, object_, Operand(scratch_));
10748 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10749 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010750
10751 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010752 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010753 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010754 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10755 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010756
10757 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010758 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10759 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010760}
10761
10762
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010763void StringCharCodeAtGenerator::GenerateSlow(
10764 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10765 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010766
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010767 // Index is not a smi.
10768 __ bind(&index_not_smi_);
10769 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010770 __ CheckMap(index_,
10771 scratch_,
10772 Heap::kHeapNumberMapRootIndex,
10773 index_not_number_,
10774 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010775 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010776 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010777 __ push(index_); // Consumed by runtime conversion function.
10778 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10779 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10780 } else {
10781 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10782 // NumberToSmi discards numbers that are not exact integers.
10783 __ CallRuntime(Runtime::kNumberToSmi, 1);
10784 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010785 // Save the conversion result before the pop instructions below
10786 // have a chance to overwrite it.
10787 __ Move(scratch_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010788 __ pop(index_);
10789 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010790 // Reload the instance type.
10791 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10792 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010793 call_helper.AfterCall(masm);
10794 // If index is still not a smi, it must be out of range.
10795 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10796 // Otherwise, return to the fast path.
10797 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010798
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010799 // Call runtime. We get here when the receiver is a string and the
10800 // index is a number, but the code of getting the actual character
10801 // is too complex (e.g., when the string needs to be flattened).
10802 __ bind(&call_runtime_);
10803 call_helper.BeforeCall(masm);
10804 __ Push(object_, index_);
10805 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010806 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010807 call_helper.AfterCall(masm);
10808 __ jmp(&exit_);
10809
10810 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10811}
10812
10813
10814// -------------------------------------------------------------------------
10815// StringCharFromCodeGenerator
10816
10817void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010818 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010819 STATIC_ASSERT(kSmiTag == 0);
10820 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010821 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010822 __ tst(code_,
10823 Operand(kSmiTagMask |
10824 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10825 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010826
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010827 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10828 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010829 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010830 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10831 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10832 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10833 __ cmp(result_, Operand(ip));
10834 __ b(eq, &slow_case_);
10835 __ bind(&exit_);
10836}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010837
ager@chromium.orgac091b72010-05-05 07:34:42 +000010838
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010839void StringCharFromCodeGenerator::GenerateSlow(
10840 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10841 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10842
10843 __ bind(&slow_case_);
10844 call_helper.BeforeCall(masm);
10845 __ push(code_);
10846 __ CallRuntime(Runtime::kCharFromCode, 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010847 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010848 call_helper.AfterCall(masm);
10849 __ jmp(&exit_);
10850
10851 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10852}
10853
10854
10855// -------------------------------------------------------------------------
10856// StringCharAtGenerator
10857
10858void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10859 char_code_at_generator_.GenerateFast(masm);
10860 char_from_code_generator_.GenerateFast(masm);
10861}
10862
10863
10864void StringCharAtGenerator::GenerateSlow(
10865 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10866 char_code_at_generator_.GenerateSlow(masm, call_helper);
10867 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010868}
10869
10870
10871void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10872 Register dest,
10873 Register src,
10874 Register count,
10875 Register scratch,
10876 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010877 Label loop;
10878 Label done;
10879 // This loop just copies one character at a time, as it is only used for very
10880 // short strings.
10881 if (!ascii) {
10882 __ add(count, count, Operand(count), SetCC);
10883 } else {
10884 __ cmp(count, Operand(0));
10885 }
10886 __ b(eq, &done);
10887
10888 __ bind(&loop);
10889 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10890 // Perform sub between load and dependent store to get the load time to
10891 // complete.
10892 __ sub(count, count, Operand(1), SetCC);
10893 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10894 // last iteration.
10895 __ b(gt, &loop);
10896
10897 __ bind(&done);
10898}
10899
10900
10901enum CopyCharactersFlags {
10902 COPY_ASCII = 1,
10903 DEST_ALWAYS_ALIGNED = 2
10904};
10905
10906
ager@chromium.orgac091b72010-05-05 07:34:42 +000010907void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10908 Register dest,
10909 Register src,
10910 Register count,
10911 Register scratch1,
10912 Register scratch2,
10913 Register scratch3,
10914 Register scratch4,
10915 Register scratch5,
10916 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010917 bool ascii = (flags & COPY_ASCII) != 0;
10918 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10919
10920 if (dest_always_aligned && FLAG_debug_code) {
10921 // Check that destination is actually word aligned if the flag says
10922 // that it is.
10923 __ tst(dest, Operand(kPointerAlignmentMask));
10924 __ Check(eq, "Destination of copy not aligned.");
10925 }
10926
10927 const int kReadAlignment = 4;
10928 const int kReadAlignmentMask = kReadAlignment - 1;
10929 // Ensure that reading an entire aligned word containing the last character
10930 // of a string will not read outside the allocated area (because we pad up
10931 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010932 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010933 // Assumes word reads and writes are little endian.
10934 // Nothing to do for zero characters.
10935 Label done;
10936 if (!ascii) {
10937 __ add(count, count, Operand(count), SetCC);
10938 } else {
10939 __ cmp(count, Operand(0));
10940 }
10941 __ b(eq, &done);
10942
10943 // Assume that you cannot read (or write) unaligned.
10944 Label byte_loop;
10945 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10946 __ cmp(count, Operand(8));
10947 __ add(count, dest, Operand(count));
10948 Register limit = count; // Read until src equals this.
10949 __ b(lt, &byte_loop);
10950
10951 if (!dest_always_aligned) {
10952 // Align dest by byte copying. Copies between zero and three bytes.
10953 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10954 Label dest_aligned;
10955 __ b(eq, &dest_aligned);
10956 __ cmp(scratch4, Operand(2));
10957 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10958 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10959 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10960 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10961 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10962 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10963 __ bind(&dest_aligned);
10964 }
10965
10966 Label simple_loop;
10967
10968 __ sub(scratch4, dest, Operand(src));
10969 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10970 __ b(eq, &simple_loop);
10971 // Shift register is number of bits in a source word that
10972 // must be combined with bits in the next source word in order
10973 // to create a destination word.
10974
10975 // Complex loop for src/dst that are not aligned the same way.
10976 {
10977 Label loop;
10978 __ mov(scratch4, Operand(scratch4, LSL, 3));
10979 Register left_shift = scratch4;
10980 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10981 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10982 // Store the "shift" most significant bits of scratch in the least
10983 // signficant bits (i.e., shift down by (32-shift)).
10984 __ rsb(scratch2, left_shift, Operand(32));
10985 Register right_shift = scratch2;
10986 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10987
10988 __ bind(&loop);
10989 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10990 __ sub(scratch5, limit, Operand(dest));
10991 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10992 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10993 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10994 // Loop if four or more bytes left to copy.
10995 // Compare to eight, because we did the subtract before increasing dst.
10996 __ sub(scratch5, scratch5, Operand(8), SetCC);
10997 __ b(ge, &loop);
10998 }
10999 // There is now between zero and three bytes left to copy (negative that
11000 // number is in scratch5), and between one and three bytes already read into
11001 // scratch1 (eight times that number in scratch4). We may have read past
11002 // the end of the string, but because objects are aligned, we have not read
11003 // past the end of the object.
11004 // Find the minimum of remaining characters to move and preloaded characters
11005 // and write those as bytes.
11006 __ add(scratch5, scratch5, Operand(4), SetCC);
11007 __ b(eq, &done);
11008 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
11009 // Move minimum of bytes read and bytes left to copy to scratch4.
11010 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
11011 // Between one and three (value in scratch5) characters already read into
11012 // scratch ready to write.
11013 __ cmp(scratch5, Operand(2));
11014 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
11015 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
11016 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
11017 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
11018 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
11019 // Copy any remaining bytes.
11020 __ b(&byte_loop);
11021
11022 // Simple loop.
11023 // Copy words from src to dst, until less than four bytes left.
11024 // Both src and dest are word aligned.
11025 __ bind(&simple_loop);
11026 {
11027 Label loop;
11028 __ bind(&loop);
11029 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
11030 __ sub(scratch3, limit, Operand(dest));
11031 __ str(scratch1, MemOperand(dest, 4, PostIndex));
11032 // Compare to 8, not 4, because we do the substraction before increasing
11033 // dest.
11034 __ cmp(scratch3, Operand(8));
11035 __ b(ge, &loop);
11036 }
11037
11038 // Copy bytes from src to dst until dst hits limit.
11039 __ bind(&byte_loop);
11040 __ cmp(dest, Operand(limit));
11041 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
11042 __ b(ge, &done);
11043 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
11044 __ b(&byte_loop);
11045
11046 __ bind(&done);
11047}
11048
11049
ager@chromium.orgac091b72010-05-05 07:34:42 +000011050void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
11051 Register c1,
11052 Register c2,
11053 Register scratch1,
11054 Register scratch2,
11055 Register scratch3,
11056 Register scratch4,
11057 Register scratch5,
11058 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011059 // Register scratch3 is the general scratch register in this function.
11060 Register scratch = scratch3;
11061
11062 // Make sure that both characters are not digits as such strings has a
11063 // different hash algorithm. Don't try to look for these in the symbol table.
11064 Label not_array_index;
11065 __ sub(scratch, c1, Operand(static_cast<int>('0')));
11066 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
11067 __ b(hi, &not_array_index);
11068 __ sub(scratch, c2, Operand(static_cast<int>('0')));
11069 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
11070
11071 // If check failed combine both characters into single halfword.
11072 // This is required by the contract of the method: code at the
11073 // not_found branch expects this combination in c1 register
11074 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
11075 __ b(ls, not_found);
11076
11077 __ bind(&not_array_index);
11078 // Calculate the two character string hash.
11079 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011080 StringHelper::GenerateHashInit(masm, hash, c1);
11081 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
11082 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011083
11084 // Collect the two characters in a register.
11085 Register chars = c1;
11086 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
11087
11088 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11089 // hash: hash of two character string.
11090
11091 // Load symbol table
11092 // Load address of first element of the symbol table.
11093 Register symbol_table = c2;
11094 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
11095
11096 // Load undefined value
11097 Register undefined = scratch4;
11098 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
11099
11100 // Calculate capacity mask from the symbol table capacity.
11101 Register mask = scratch2;
11102 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
11103 __ mov(mask, Operand(mask, ASR, 1));
11104 __ sub(mask, mask, Operand(1));
11105
11106 // Calculate untagged address of the first element of the symbol table.
11107 Register first_symbol_table_element = symbol_table;
11108 __ add(first_symbol_table_element, symbol_table,
11109 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
11110
11111 // Registers
11112 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11113 // hash: hash of two character string
11114 // mask: capacity mask
11115 // first_symbol_table_element: address of the first element of
11116 // the symbol table
11117 // scratch: -
11118
11119 // Perform a number of probes in the symbol table.
11120 static const int kProbes = 4;
11121 Label found_in_symbol_table;
11122 Label next_probe[kProbes];
11123 for (int i = 0; i < kProbes; i++) {
11124 Register candidate = scratch5; // Scratch register contains candidate.
11125
11126 // Calculate entry in symbol table.
11127 if (i > 0) {
11128 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
11129 } else {
11130 __ mov(candidate, hash);
11131 }
11132
11133 __ and_(candidate, candidate, Operand(mask));
11134
11135 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011136 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011137 __ ldr(candidate,
11138 MemOperand(first_symbol_table_element,
11139 candidate,
11140 LSL,
11141 kPointerSizeLog2));
11142
11143 // If entry is undefined no string with this hash can be found.
11144 __ cmp(candidate, undefined);
11145 __ b(eq, not_found);
11146
11147 // If length is not 2 the string is not a candidate.
11148 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011149 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011150 __ b(ne, &next_probe[i]);
11151
11152 // Check that the candidate is a non-external ascii string.
11153 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
11154 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
11155 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
11156 &next_probe[i]);
11157
11158 // Check if the two characters match.
11159 // Assumes that word load is little endian.
11160 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
11161 __ cmp(chars, scratch);
11162 __ b(eq, &found_in_symbol_table);
11163 __ bind(&next_probe[i]);
11164 }
11165
11166 // No matching 2 character string found by probing.
11167 __ jmp(not_found);
11168
11169 // Scratch register contains result when we fall through to here.
11170 Register result = scratch;
11171 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000011172 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011173}
11174
11175
ager@chromium.orgac091b72010-05-05 07:34:42 +000011176void StringHelper::GenerateHashInit(MacroAssembler* masm,
11177 Register hash,
11178 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011179 // hash = character + (character << 10);
11180 __ add(hash, character, Operand(character, LSL, 10));
11181 // hash ^= hash >> 6;
11182 __ eor(hash, hash, Operand(hash, ASR, 6));
11183}
11184
11185
ager@chromium.orgac091b72010-05-05 07:34:42 +000011186void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
11187 Register hash,
11188 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011189 // hash += character;
11190 __ add(hash, hash, Operand(character));
11191 // hash += hash << 10;
11192 __ add(hash, hash, Operand(hash, LSL, 10));
11193 // hash ^= hash >> 6;
11194 __ eor(hash, hash, Operand(hash, ASR, 6));
11195}
11196
11197
ager@chromium.orgac091b72010-05-05 07:34:42 +000011198void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
11199 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011200 // hash += hash << 3;
11201 __ add(hash, hash, Operand(hash, LSL, 3));
11202 // hash ^= hash >> 11;
11203 __ eor(hash, hash, Operand(hash, ASR, 11));
11204 // hash += hash << 15;
11205 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
11206
11207 // if (hash == 0) hash = 27;
11208 __ mov(hash, Operand(27), LeaveCC, nz);
11209}
11210
11211
ager@chromium.org5c838252010-02-19 08:53:10 +000011212void SubStringStub::Generate(MacroAssembler* masm) {
11213 Label runtime;
11214
11215 // Stack frame on entry.
11216 // lr: return address
11217 // sp[0]: to
11218 // sp[4]: from
11219 // sp[8]: string
11220
11221 // This stub is called from the native-call %_SubString(...), so
11222 // nothing can be assumed about the arguments. It is tested that:
11223 // "string" is a sequential string,
11224 // both "from" and "to" are smis, and
11225 // 0 <= from <= to <= string.length.
11226 // If any of these assumptions fail, we call the runtime system.
11227
11228 static const int kToOffset = 0 * kPointerSize;
11229 static const int kFromOffset = 1 * kPointerSize;
11230 static const int kStringOffset = 2 * kPointerSize;
11231
11232
11233 // Check bounds and smi-ness.
11234 __ ldr(r7, MemOperand(sp, kToOffset));
11235 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011236 STATIC_ASSERT(kSmiTag == 0);
11237 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011238 // I.e., arithmetic shift right by one un-smi-tags.
11239 __ mov(r2, Operand(r7, ASR, 1), SetCC);
11240 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
11241 // If either r2 or r6 had the smi tag bit set, then carry is set now.
11242 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
11243 __ b(mi, &runtime); // From is negative.
11244
11245 __ sub(r2, r2, Operand(r3), SetCC);
11246 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011247 // Special handling of sub-strings of length 1 and 2. One character strings
11248 // are handled in the runtime system (looked up in the single character
11249 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000011250 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011251 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000011252
11253 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011254 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011255 // r6: from (smi)
11256 // r7: to (smi)
11257
11258 // Make sure first argument is a sequential (or flat) string.
11259 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011260 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011261 __ tst(r5, Operand(kSmiTagMask));
11262 __ b(eq, &runtime);
11263 Condition is_string = masm->IsObjectStringType(r5, r1);
11264 __ b(NegateCondition(is_string), &runtime);
11265
11266 // r1: instance type
11267 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011268 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011269 // r5: string
11270 // r6: from (smi)
11271 // r7: to (smi)
11272 Label seq_string;
11273 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011274 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
11275 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000011276 __ cmp(r4, Operand(kConsStringTag));
11277 __ b(gt, &runtime); // External strings go to runtime.
11278 __ b(lt, &seq_string); // Sequential strings are handled directly.
11279
11280 // Cons string. Try to recurse (once) on the first substring.
11281 // (This adds a little more generality than necessary to handle flattened
11282 // cons strings, but not much).
11283 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
11284 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
11285 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11286 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011287 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011288 __ b(ne, &runtime); // Cons and External strings go to runtime.
11289
11290 // Definitly a sequential string.
11291 __ bind(&seq_string);
11292
11293 // r1: instance type.
11294 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011295 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011296 // r5: string
11297 // r6: from (smi)
11298 // r7: to (smi)
11299 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011300 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000011301 __ b(lt, &runtime); // Fail if to > length.
11302
11303 // r1: instance type.
11304 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011305 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011306 // r5: string.
11307 // r6: from offset (smi)
11308 // Check for flat ascii string.
11309 Label non_ascii_flat;
11310 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011311 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011312 __ b(eq, &non_ascii_flat);
11313
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011314 Label result_longer_than_two;
11315 __ cmp(r2, Operand(2));
11316 __ b(gt, &result_longer_than_two);
11317
11318 // Sub string of length 2 requested.
11319 // Get the two characters forming the sub string.
11320 __ add(r5, r5, Operand(r3));
11321 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
11322 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
11323
11324 // Try to lookup two character string in symbol table.
11325 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011326 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11327 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011328 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11329 __ add(sp, sp, Operand(3 * kPointerSize));
11330 __ Ret();
11331
11332 // r2: result string length.
11333 // r3: two characters combined into halfword in little endian byte order.
11334 __ bind(&make_two_character_string);
11335 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
11336 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11337 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11338 __ add(sp, sp, Operand(3 * kPointerSize));
11339 __ Ret();
11340
11341 __ bind(&result_longer_than_two);
11342
ager@chromium.org5c838252010-02-19 08:53:10 +000011343 // Allocate the result.
11344 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
11345
11346 // r0: result string.
11347 // r2: result string length.
11348 // r5: string.
11349 // r6: from offset (smi)
11350 // Locate first character of result.
11351 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11352 // Locate 'from' character of string.
11353 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11354 __ add(r5, r5, Operand(r6, ASR, 1));
11355
11356 // r0: result string.
11357 // r1: first character of result string.
11358 // r2: result string length.
11359 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011360 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011361 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11362 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011363 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11364 __ add(sp, sp, Operand(3 * kPointerSize));
11365 __ Ret();
11366
11367 __ bind(&non_ascii_flat);
11368 // r2: result string length.
11369 // r5: string.
11370 // r6: from offset (smi)
11371 // Check for flat two byte string.
11372
11373 // Allocate the result.
11374 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
11375
11376 // r0: result string.
11377 // r2: result string length.
11378 // r5: string.
11379 // Locate first character of result.
11380 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11381 // Locate 'from' character of string.
11382 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11383 // As "from" is a smi it is 2 times the value which matches the size of a two
11384 // byte character.
11385 __ add(r5, r5, Operand(r6));
11386
11387 // r0: result string.
11388 // r1: first character of result.
11389 // r2: result length.
11390 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011391 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011392 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11393 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011394 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11395 __ add(sp, sp, Operand(3 * kPointerSize));
11396 __ Ret();
11397
11398 // Just jump to runtime to create the sub string.
11399 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011400 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011401}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011402
11403
11404void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11405 Register left,
11406 Register right,
11407 Register scratch1,
11408 Register scratch2,
11409 Register scratch3,
11410 Register scratch4) {
11411 Label compare_lengths;
11412 // Find minimum length and length difference.
11413 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11414 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11415 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11416 Register length_delta = scratch3;
11417 __ mov(scratch1, scratch2, LeaveCC, gt);
11418 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011419 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011420 __ tst(min_length, Operand(min_length));
11421 __ b(eq, &compare_lengths);
11422
ager@chromium.orgac091b72010-05-05 07:34:42 +000011423 // Untag smi.
11424 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11425
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011426 // Setup registers so that we only need to increment one register
11427 // in the loop.
11428 __ add(scratch2, min_length,
11429 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11430 __ add(left, left, Operand(scratch2));
11431 __ add(right, right, Operand(scratch2));
11432 // Registers left and right points to the min_length character of strings.
11433 __ rsb(min_length, min_length, Operand(-1));
11434 Register index = min_length;
11435 // Index starts at -min_length.
11436
11437 {
11438 // Compare loop.
11439 Label loop;
11440 __ bind(&loop);
11441 // Compare characters.
11442 __ add(index, index, Operand(1), SetCC);
11443 __ ldrb(scratch2, MemOperand(left, index), ne);
11444 __ ldrb(scratch4, MemOperand(right, index), ne);
11445 // Skip to compare lengths with eq condition true.
11446 __ b(eq, &compare_lengths);
11447 __ cmp(scratch2, scratch4);
11448 __ b(eq, &loop);
11449 // Fallthrough with eq condition false.
11450 }
11451 // Compare lengths - strings up to min-length are equal.
11452 __ bind(&compare_lengths);
11453 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11454 // Use zero length_delta as result.
11455 __ mov(r0, Operand(length_delta), SetCC, eq);
11456 // Fall through to here if characters compare not-equal.
11457 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11458 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11459 __ Ret();
11460}
11461
11462
11463void StringCompareStub::Generate(MacroAssembler* masm) {
11464 Label runtime;
11465
11466 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011467 // sp[0]: right string
11468 // sp[4]: left string
11469 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11470 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011471
11472 Label not_same;
11473 __ cmp(r0, r1);
11474 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011475 STATIC_ASSERT(EQUAL == 0);
11476 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011477 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11478 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11479 __ add(sp, sp, Operand(2 * kPointerSize));
11480 __ Ret();
11481
11482 __ bind(&not_same);
11483
11484 // Check that both objects are sequential ascii strings.
11485 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11486
11487 // Compare flat ascii strings natively. Remove arguments from stack first.
11488 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11489 __ add(sp, sp, Operand(2 * kPointerSize));
11490 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11491
11492 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11493 // tagged as a small integer.
11494 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011495 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011496}
11497
11498
ager@chromium.org5c838252010-02-19 08:53:10 +000011499void StringAddStub::Generate(MacroAssembler* masm) {
11500 Label string_add_runtime;
11501 // Stack on entry:
11502 // sp[0]: second argument.
11503 // sp[4]: first argument.
11504
11505 // Load the two arguments.
11506 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11507 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11508
11509 // Make sure that both arguments are strings if not known in advance.
11510 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011511 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011512 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11513 // Load instance types.
11514 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11515 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11516 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11517 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011518 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011519 // If either is not a string, go to runtime.
11520 __ tst(r4, Operand(kIsNotStringMask));
11521 __ tst(r5, Operand(kIsNotStringMask), eq);
11522 __ b(ne, &string_add_runtime);
11523 }
11524
11525 // Both arguments are strings.
11526 // r0: first string
11527 // r1: second string
11528 // r4: first string instance type (if string_check_)
11529 // r5: second string instance type (if string_check_)
11530 {
11531 Label strings_not_empty;
11532 // Check if either of the strings are empty. In that case return the other.
11533 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11534 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011535 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011536 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011537 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011538 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011539 // Else test if second string is empty.
11540 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011541 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11542
11543 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11544 __ add(sp, sp, Operand(2 * kPointerSize));
11545 __ Ret();
11546
11547 __ bind(&strings_not_empty);
11548 }
11549
ager@chromium.orgac091b72010-05-05 07:34:42 +000011550 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11551 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011552 // Both strings are non-empty.
11553 // r0: first string
11554 // r1: second string
11555 // r2: length of first string
11556 // r3: length of second string
11557 // r4: first string instance type (if string_check_)
11558 // r5: second string instance type (if string_check_)
11559 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011560 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011561 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011562 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011563 __ add(r6, r2, Operand(r3));
11564 // Use the runtime system when adding two one character strings, as it
11565 // contains optimizations for this specific case using the symbol table.
11566 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011567 __ b(ne, &longer_than_two);
11568
11569 // Check that both strings are non-external ascii strings.
11570 if (!string_check_) {
11571 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11572 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11573 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11574 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11575 }
11576 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11577 &string_add_runtime);
11578
11579 // Get the two characters forming the sub string.
11580 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11581 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11582
11583 // Try to lookup two character string in symbol table. If it is not found
11584 // just allocate a new one.
11585 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011586 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11587 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011588 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11589 __ add(sp, sp, Operand(2 * kPointerSize));
11590 __ Ret();
11591
11592 __ bind(&make_two_character_string);
11593 // Resulting string has length 2 and first chars of two strings
11594 // are combined into single halfword in r2 register.
11595 // So we can fill resulting string without two loops by a single
11596 // halfword store instruction (which assumes that processor is
11597 // in a little endian mode)
11598 __ mov(r6, Operand(2));
11599 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11600 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11601 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11602 __ add(sp, sp, Operand(2 * kPointerSize));
11603 __ Ret();
11604
11605 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011606 // Check if resulting string will be flat.
11607 __ cmp(r6, Operand(String::kMinNonFlatLength));
11608 __ b(lt, &string_add_flat_result);
11609 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011610 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011611 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11612 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11613 __ cmp(r6, Operand(String::kMaxLength + 1));
11614 __ b(hs, &string_add_runtime);
11615
11616 // If result is not supposed to be flat, allocate a cons string object.
11617 // If both strings are ascii the result is an ascii cons string.
11618 if (!string_check_) {
11619 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11620 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11621 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11622 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11623 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011624 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011625 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011626 __ tst(r4, Operand(kStringEncodingMask));
11627 __ tst(r5, Operand(kStringEncodingMask), ne);
11628 __ b(eq, &non_ascii);
11629
11630 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011631 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011632 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11633 __ bind(&allocated);
11634 // Fill the fields of the cons string.
11635 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11636 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11637 __ mov(r0, Operand(r7));
11638 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11639 __ add(sp, sp, Operand(2 * kPointerSize));
11640 __ Ret();
11641
11642 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011643 // At least one of the strings is two-byte. Check whether it happens
11644 // to contain only ascii characters.
11645 // r4: first instance type.
11646 // r5: second instance type.
11647 __ tst(r4, Operand(kAsciiDataHintMask));
11648 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11649 __ b(ne, &ascii_data);
11650 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011651 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011652 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11653 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11654 __ b(eq, &ascii_data);
11655
ager@chromium.org5c838252010-02-19 08:53:10 +000011656 // Allocate a two byte cons string.
11657 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11658 __ jmp(&allocated);
11659
11660 // Handle creating a flat result. First check that both strings are
11661 // sequential and that they have the same encoding.
11662 // r0: first string
11663 // r1: second string
11664 // r2: length of first string
11665 // r3: length of second string
11666 // r4: first string instance type (if string_check_)
11667 // r5: second string instance type (if string_check_)
11668 // r6: sum of lengths.
11669 __ bind(&string_add_flat_result);
11670 if (!string_check_) {
11671 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11672 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11673 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11674 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11675 }
11676 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011677 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011678 __ tst(r4, Operand(kStringRepresentationMask));
11679 __ tst(r5, Operand(kStringRepresentationMask), eq);
11680 __ b(ne, &string_add_runtime);
11681 // Now check if both strings have the same encoding (ASCII/Two-byte).
11682 // r0: first string.
11683 // r1: second string.
11684 // r2: length of first string.
11685 // r3: length of second string.
11686 // r6: sum of lengths..
11687 Label non_ascii_string_add_flat_result;
11688 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11689 __ eor(r7, r4, Operand(r5));
11690 __ tst(r7, Operand(kStringEncodingMask));
11691 __ b(ne, &string_add_runtime);
11692 // And see if it's ASCII or two-byte.
11693 __ tst(r4, Operand(kStringEncodingMask));
11694 __ b(eq, &non_ascii_string_add_flat_result);
11695
11696 // Both strings are sequential ASCII strings. We also know that they are
11697 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011698 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011699 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11700 // Locate first character of result.
11701 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11702 // Locate first character of first argument.
11703 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11704 // r0: first character of first string.
11705 // r1: second string.
11706 // r2: length of first string.
11707 // r3: length of second string.
11708 // r6: first character of result.
11709 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011710 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011711
11712 // Load second argument and locate first character.
11713 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11714 // r1: first character of second string.
11715 // r3: length of second string.
11716 // r6: next character of result.
11717 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011718 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011719 __ mov(r0, Operand(r7));
11720 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11721 __ add(sp, sp, Operand(2 * kPointerSize));
11722 __ Ret();
11723
11724 __ bind(&non_ascii_string_add_flat_result);
11725 // Both strings are sequential two byte strings.
11726 // r0: first string.
11727 // r1: second string.
11728 // r2: length of first string.
11729 // r3: length of second string.
11730 // r6: sum of length of strings.
11731 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11732 // r0: first string.
11733 // r1: second string.
11734 // r2: length of first string.
11735 // r3: length of second string.
11736 // r7: result string.
11737
11738 // Locate first character of result.
11739 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11740 // Locate first character of first argument.
11741 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11742
11743 // r0: first character of first string.
11744 // r1: second string.
11745 // r2: length of first string.
11746 // r3: length of second string.
11747 // r6: first character of result.
11748 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011749 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011750
11751 // Locate first character of second argument.
11752 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11753
11754 // r1: first character of second string.
11755 // r3: length of second string.
11756 // r6: next character of result (after copy of first string).
11757 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011758 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011759
11760 __ mov(r0, Operand(r7));
11761 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11762 __ add(sp, sp, Operand(2 * kPointerSize));
11763 __ Ret();
11764
11765 // Just jump to runtime to add the two strings.
11766 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011767 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011768}
11769
11770
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011771#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011772
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011773} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011774
11775#endif // V8_TARGET_ARCH_ARM