blob: 15f34b59448eff84d7154b80abc11dbae6f6ea4e [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);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001225 TypeInfo result =
1226 (op == Token::SAR) ? TypeInfo::Integer32() : TypeInfo::Number();
1227 if (!reversed) {
1228 if (op == Token::SHR) {
1229 if (int_value >= 2) {
1230 result = TypeInfo::Smi();
1231 } else if (int_value >= 1) {
1232 result = TypeInfo::Integer32();
1233 }
1234 } else {
1235 if (int_value >= 1) {
1236 result = TypeInfo::Smi();
1237 }
1238 }
1239 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001240 Register scratch = VirtualFrame::scratch0();
1241 Register scratch2 = VirtualFrame::scratch1();
1242 int shift_value = int_value & 0x1f; // least significant 5 bits
1243 DeferredCode* deferred =
1244 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001245 uint32_t problematic_mask = kSmiTagMask;
1246 // For unsigned shift by zero all negative smis are problematic.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001247 bool skip_smi_test = both_sides_are_smi;
1248 if (shift_value == 0 && op == Token::SHR) {
1249 problematic_mask |= 0x80000000;
1250 skip_smi_test = false;
1251 }
1252 if (!skip_smi_test) {
1253 __ tst(tos, Operand(problematic_mask));
1254 deferred->Branch(ne); // Go slow for problematic input.
1255 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001256 switch (op) {
1257 case Token::SHL: {
1258 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001259 int adjusted_shift = shift_value - kSmiTagSize;
1260 ASSERT(adjusted_shift >= 0);
1261 if (adjusted_shift != 0) {
1262 __ mov(scratch, Operand(tos, LSL, adjusted_shift));
1263 // Check that the *signed* result fits in a smi.
1264 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1265 deferred->Branch(mi);
1266 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1267 } else {
1268 // Check that the *signed* result fits in a smi.
1269 __ add(scratch2, tos, Operand(0x40000000), SetCC);
1270 deferred->Branch(mi);
1271 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
1272 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001273 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001274 break;
1275 }
1276 case Token::SHR: {
ager@chromium.org357bf652010-04-12 11:30:10 +00001277 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001278 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // Remove tag.
1279 // LSR by immediate 0 means shifting 32 bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001280 __ mov(scratch, Operand(scratch, LSR, shift_value));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001281 if (shift_value == 1) {
1282 // check that the *unsigned* result fits in a smi
1283 // neither of the two high-order bits can be set:
1284 // - 0x80000000: high bit would be lost when smi tagging
1285 // - 0x40000000: this number would convert to negative when
1286 // smi tagging these two cases can only happen with shifts
1287 // by 0 or 1 when handed a valid smi
1288 __ tst(scratch, Operand(0xc0000000));
1289 deferred->Branch(ne);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001290 } else {
1291 ASSERT(shift_value >= 2);
1292 result = TypeInfo::Smi(); // SHR by at least 2 gives a Smi.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001293 }
1294 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
ager@chromium.org357bf652010-04-12 11:30:10 +00001295 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001296 break;
1297 }
1298 case Token::SAR: {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001299 // In the ARM instructions set, ASR by immediate 0 means shifting 32
1300 // bits.
ager@chromium.org357bf652010-04-12 11:30:10 +00001301 if (shift_value != 0) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001302 // Do the shift and the tag removal in one operation. If the shift
1303 // is 31 bits (the highest possible value) then we emit the
1304 // instruction as a shift by 0 which means shift arithmetically by
1305 // 32.
1306 __ mov(tos, Operand(tos, ASR, (kSmiTagSize + shift_value) & 0x1f));
1307 // Put tag back.
1308 __ mov(tos, Operand(tos, LSL, kSmiTagSize));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001309 // SAR by at least 1 gives a Smi.
1310 result = TypeInfo::Smi();
ager@chromium.org357bf652010-04-12 11:30:10 +00001311 }
1312 break;
1313 }
1314 default: UNREACHABLE();
1315 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001316 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001317 frame_->EmitPush(tos, result);
ager@chromium.org357bf652010-04-12 11:30:10 +00001318 break;
1319 }
1320
1321 case Token::MOD: {
1322 ASSERT(!reversed);
1323 ASSERT(int_value >= 2);
1324 ASSERT(IsPowerOf2(int_value));
1325 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001326 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001327 unsigned mask = (0x80000000u | kSmiTagMask);
1328 __ tst(tos, Operand(mask));
1329 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1330 mask = (int_value << kSmiTagSize) - 1;
1331 __ and_(tos, tos, Operand(mask));
1332 deferred->BindExit();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001333 // Mod of positive power of 2 Smi gives a Smi if the lhs is an integer.
1334 frame_->EmitPush(
1335 tos,
1336 both_sides_are_smi ? TypeInfo::Smi() : TypeInfo::Number());
ager@chromium.org357bf652010-04-12 11:30:10 +00001337 break;
1338 }
1339
1340 case Token::MUL: {
1341 ASSERT(IsEasyToMultiplyBy(int_value));
1342 DeferredCode* deferred =
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001343 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
ager@chromium.org357bf652010-04-12 11:30:10 +00001344 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1345 max_smi_that_wont_overflow <<= kSmiTagSize;
1346 unsigned mask = 0x80000000u;
1347 while ((mask & max_smi_that_wont_overflow) == 0) {
1348 mask |= mask >> 1;
1349 }
1350 mask |= kSmiTagMask;
1351 // This does a single mask that checks for a too high value in a
1352 // conservative way and for a non-Smi. It also filters out negative
1353 // numbers, unfortunately, but since this code is inline we prefer
1354 // brevity to comprehensiveness.
1355 __ tst(tos, Operand(mask));
1356 deferred->Branch(ne);
1357 MultiplyByKnownInt(masm_, tos, tos, int_value);
1358 deferred->BindExit();
1359 frame_->EmitPush(tos);
1360 break;
1361 }
1362
1363 default:
1364 UNREACHABLE();
1365 break;
1366 }
1367}
1368
1369
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001370void CodeGenerator::Comparison(Condition cc,
1371 Expression* left,
1372 Expression* right,
1373 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001374 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001375
ager@chromium.org357bf652010-04-12 11:30:10 +00001376 if (left != NULL) Load(left);
1377 if (right != NULL) Load(right);
1378
mads.s.ager31e71382008-08-13 09:32:07 +00001379 // sp[0] : y
1380 // sp[1] : x
1381 // result : cc register
1382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 // Strict only makes sense for equality comparisons.
1384 ASSERT(!strict || cc == eq);
1385
ager@chromium.org357bf652010-04-12 11:30:10 +00001386 Register lhs;
1387 Register rhs;
1388
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001389 bool lhs_is_smi;
1390 bool rhs_is_smi;
1391
ager@chromium.org357bf652010-04-12 11:30:10 +00001392 // We load the top two stack positions into registers chosen by the virtual
1393 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001394 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1395 if (cc == gt || cc == le) {
1396 cc = ReverseCondition(cc);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001397 lhs_is_smi = frame_->KnownSmiAt(0);
1398 rhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001399 lhs = frame_->PopToRegister();
1400 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001401 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001402 rhs_is_smi = frame_->KnownSmiAt(0);
1403 lhs_is_smi = frame_->KnownSmiAt(1);
ager@chromium.org357bf652010-04-12 11:30:10 +00001404 rhs = frame_->PopToRegister();
1405 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001406 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001407
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001408 bool both_sides_are_smi = (lhs_is_smi && rhs_is_smi);
1409
ager@chromium.org357bf652010-04-12 11:30:10 +00001410 ASSERT(rhs.is(r0) || rhs.is(r1));
1411 ASSERT(lhs.is(r0) || lhs.is(r1));
1412
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001413 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001415 if (!both_sides_are_smi) {
1416 // Now we have the two sides in r0 and r1. We flush any other registers
1417 // because the stub doesn't know about register allocation.
1418 frame_->SpillAll();
1419 Register scratch = VirtualFrame::scratch0();
1420 Register smi_test_reg;
1421 if (lhs_is_smi) {
1422 smi_test_reg = rhs;
1423 } else if (rhs_is_smi) {
1424 smi_test_reg = lhs;
1425 } else {
1426 __ orr(scratch, lhs, Operand(rhs));
1427 smi_test_reg = scratch;
1428 }
1429 __ tst(smi_test_reg, Operand(kSmiTagMask));
1430 JumpTarget smi;
1431 smi.Branch(eq);
1432
1433 // Perform non-smi comparison by stub.
1434 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1435 // We call with 0 args because there are 0 on the stack.
ager@chromium.orgb5737492010-07-15 09:29:43 +00001436 CompareStub stub(cc, strict, kBothCouldBeNaN, true, lhs, rhs);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001437 frame_->CallStub(&stub, 0);
1438 __ cmp(r0, Operand(0));
1439 exit.Jump();
1440
1441 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001442 }
1443
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001444 // Do smi comparisons by pointer comparison.
ager@chromium.org357bf652010-04-12 11:30:10 +00001445 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001447 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 cc_reg_ = cc;
1449}
1450
1451
mads.s.ager31e71382008-08-13 09:32:07 +00001452// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001453void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001454 CallFunctionFlags flags,
1455 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001457 int arg_count = args->length();
1458 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001459 Load(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001460 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461
kasper.lund7276f142008-07-30 08:49:36 +00001462 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001463 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464
kasper.lund7276f142008-07-30 08:49:36 +00001465 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001466 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001467 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001468 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001471 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001472 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473}
1474
1475
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001476void CodeGenerator::CallApplyLazy(Expression* applicand,
1477 Expression* receiver,
1478 VariableProxy* arguments,
1479 int position) {
1480 // An optimized implementation of expressions of the form
1481 // x.apply(y, arguments).
1482 // If the arguments object of the scope has not been allocated,
1483 // and x.apply is Function.prototype.apply, this optimization
1484 // just copies y and the arguments of the current function on the
1485 // stack, as receiver and arguments, and calls x.
1486 // In the implementation comments, we call x the applicand
1487 // and y the receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001488
1489 ASSERT(ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION);
1490 ASSERT(arguments->IsArguments());
1491
1492 // Load applicand.apply onto the stack. This will usually
1493 // give us a megamorphic load site. Not super, but it works.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001494 Load(applicand);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001495 Handle<String> name = Factory::LookupAsciiSymbol("apply");
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001496 frame_->Dup();
ager@chromium.orgac091b72010-05-05 07:34:42 +00001497 frame_->CallLoadIC(name, RelocInfo::CODE_TARGET);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001498 frame_->EmitPush(r0);
1499
1500 // Load the receiver and the existing arguments object onto the
1501 // expression stack. Avoid allocating the arguments object here.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001502 Load(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001503 LoadFromSlot(scope()->arguments()->var()->slot(), NOT_INSIDE_TYPEOF);
1504
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001505 // At this point the top two stack elements are probably in registers
1506 // since they were just loaded. Ensure they are in regs and get the
1507 // regs.
1508 Register receiver_reg = frame_->Peek2();
1509 Register arguments_reg = frame_->Peek();
1510
1511 // From now on the frame is spilled.
1512 frame_->SpillAll();
1513
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001514 // Emit the source position information after having loaded the
1515 // receiver and the arguments.
1516 CodeForSourcePosition(position);
1517 // Contents of the stack at this point:
1518 // sp[0]: arguments object of the current function or the hole.
1519 // sp[1]: receiver
1520 // sp[2]: applicand.apply
1521 // sp[3]: applicand.
1522
1523 // Check if the arguments object has been lazily allocated
1524 // already. If so, just use that instead of copying the arguments
1525 // from the stack. This also deals with cases where a local variable
1526 // named 'arguments' has been introduced.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001527 JumpTarget slow;
1528 Label done;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001529 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001530 __ cmp(ip, arguments_reg);
1531 slow.Branch(ne);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001532
1533 Label build_args;
1534 // Get rid of the arguments object probe.
1535 frame_->Drop();
1536 // Stack now has 3 elements on it.
1537 // Contents of stack at this point:
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001538 // sp[0]: receiver - in the receiver_reg register.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001539 // sp[1]: applicand.apply
1540 // sp[2]: applicand.
1541
1542 // Check that the receiver really is a JavaScript object.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001543 __ BranchOnSmi(receiver_reg, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001544 // We allow all JSObjects including JSFunctions. As long as
1545 // JS_FUNCTION_TYPE is the last instance type and it is right
1546 // after LAST_JS_OBJECT_TYPE, we do not have to check the upper
1547 // bound.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00001548 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1549 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001550 __ CompareObjectType(receiver_reg, r2, r3, FIRST_JS_OBJECT_TYPE);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001551 __ b(lt, &build_args);
1552
1553 // Check that applicand.apply is Function.prototype.apply.
1554 __ ldr(r0, MemOperand(sp, kPointerSize));
1555 __ BranchOnSmi(r0, &build_args);
1556 __ CompareObjectType(r0, r1, r2, JS_FUNCTION_TYPE);
1557 __ b(ne, &build_args);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001558 Handle<Code> apply_code(Builtins::builtin(Builtins::FunctionApply));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001559 __ ldr(r1, FieldMemOperand(r0, JSFunction::kCodeOffset));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001560 __ cmp(r1, Operand(apply_code));
1561 __ b(ne, &build_args);
1562
1563 // Check that applicand is a function.
1564 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1565 __ BranchOnSmi(r1, &build_args);
1566 __ CompareObjectType(r1, r2, r3, JS_FUNCTION_TYPE);
1567 __ b(ne, &build_args);
1568
1569 // Copy the arguments to this function possibly from the
1570 // adaptor frame below it.
1571 Label invoke, adapted;
1572 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1573 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
1574 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1575 __ b(eq, &adapted);
1576
1577 // No arguments adaptor frame. Copy fixed number of arguments.
1578 __ mov(r0, Operand(scope()->num_parameters()));
1579 for (int i = 0; i < scope()->num_parameters(); i++) {
1580 __ ldr(r2, frame_->ParameterAt(i));
1581 __ push(r2);
1582 }
1583 __ jmp(&invoke);
1584
1585 // Arguments adaptor frame present. Copy arguments from there, but
1586 // avoid copying too many arguments to avoid stack overflows.
1587 __ bind(&adapted);
1588 static const uint32_t kArgumentsLimit = 1 * KB;
1589 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1590 __ mov(r0, Operand(r0, LSR, kSmiTagSize));
1591 __ mov(r3, r0);
1592 __ cmp(r0, Operand(kArgumentsLimit));
1593 __ b(gt, &build_args);
1594
1595 // Loop through the arguments pushing them onto the execution
1596 // stack. We don't inform the virtual frame of the push, so we don't
1597 // have to worry about getting rid of the elements from the virtual
1598 // frame.
1599 Label loop;
1600 // r3 is a small non-negative integer, due to the test above.
1601 __ cmp(r3, Operand(0));
1602 __ b(eq, &invoke);
1603 // Compute the address of the first argument.
1604 __ add(r2, r2, Operand(r3, LSL, kPointerSizeLog2));
1605 __ add(r2, r2, Operand(kPointerSize));
1606 __ bind(&loop);
1607 // Post-decrement argument address by kPointerSize on each iteration.
1608 __ ldr(r4, MemOperand(r2, kPointerSize, NegPostIndex));
1609 __ push(r4);
1610 __ sub(r3, r3, Operand(1), SetCC);
1611 __ b(gt, &loop);
1612
1613 // Invoke the function.
1614 __ bind(&invoke);
1615 ParameterCount actual(r0);
1616 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1617 // Drop applicand.apply and applicand from the stack, and push
1618 // the result of the function call, but leave the spilled frame
1619 // unchanged, with 3 elements, so it is correct when we compile the
1620 // slow-case code.
1621 __ add(sp, sp, Operand(2 * kPointerSize));
1622 __ push(r0);
1623 // Stack now has 1 element:
1624 // sp[0]: result
1625 __ jmp(&done);
1626
1627 // Slow-case: Allocate the arguments object since we know it isn't
1628 // there, and fall-through to the slow-case where we call
1629 // applicand.apply.
1630 __ bind(&build_args);
1631 // Stack now has 3 elements, because we have jumped from where:
1632 // sp[0]: receiver
1633 // sp[1]: applicand.apply
1634 // sp[2]: applicand.
1635 StoreArgumentsObject(false);
1636
1637 // Stack and frame now have 4 elements.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001638 slow.Bind();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001639
1640 // Generic computation of x.apply(y, args) with no special optimization.
1641 // Flip applicand.apply and applicand on the stack, so
1642 // applicand looks like the receiver of the applicand.apply call.
1643 // Then process it as a normal function call.
1644 __ ldr(r0, MemOperand(sp, 3 * kPointerSize));
1645 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001646 __ Strd(r0, r1, MemOperand(sp, 2 * kPointerSize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001647
1648 CallFunctionStub call_function(2, NOT_IN_LOOP, NO_CALL_FUNCTION_FLAGS);
1649 frame_->CallStub(&call_function, 3);
1650 // The function and its two arguments have been dropped.
1651 frame_->Drop(); // Drop the receiver as well.
1652 frame_->EmitPush(r0);
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001653 frame_->SpillAll(); // A spilled frame is also jumping to label done.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00001654 // Stack now has 1 element:
1655 // sp[0]: result
1656 __ bind(&done);
1657
1658 // Restore the context register after a call.
1659 __ ldr(cp, frame_->Context());
1660}
1661
1662
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001663void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 ASSERT(has_cc());
1665 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001666 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 cc_reg_ = al;
1668}
1669
1670
ager@chromium.org7c537e22008-10-16 08:43:32 +00001671void CodeGenerator::CheckStack() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001672 frame_->SpillAll();
ager@chromium.org3811b432009-10-28 14:53:37 +00001673 Comment cmnt(masm_, "[ check stack");
1674 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1675 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1676 // the implicit 8 byte offset that always applies to operations with pc and
1677 // gives a return address 12 bytes down.
1678 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1679 masm_->cmp(sp, Operand(ip));
1680 StackCheckStub stub;
1681 // Call the stub if lower.
1682 masm_->mov(pc,
1683 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1684 RelocInfo::CODE_TARGET),
1685 LeaveCC,
1686 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687}
1688
1689
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1691#ifdef DEBUG
1692 int original_height = frame_->height();
1693#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001694 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001695 Visit(statements->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001696 }
1697 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1698}
1699
1700
ager@chromium.org7c537e22008-10-16 08:43:32 +00001701void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001702#ifdef DEBUG
1703 int original_height = frame_->height();
1704#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001706 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001707 node->break_target()->SetExpectedHeight();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001708 VisitStatements(node->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001709 if (node->break_target()->is_linked()) {
1710 node->break_target()->Bind();
1711 }
1712 node->break_target()->Unuse();
1713 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714}
1715
1716
ager@chromium.org7c537e22008-10-16 08:43:32 +00001717void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org3811b432009-10-28 14:53:37 +00001718 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001719 frame_->EmitPush(Operand(pairs));
1720 frame_->EmitPush(Operand(Smi::FromInt(is_eval() ? 1 : 0)));
1721
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001722 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001723 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724}
1725
1726
ager@chromium.org7c537e22008-10-16 08:43:32 +00001727void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001728#ifdef DEBUG
1729 int original_height = frame_->height();
1730#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 Comment cmnt(masm_, "[ Declaration");
1732 Variable* var = node->proxy()->var();
1733 ASSERT(var != NULL); // must have been resolved
1734 Slot* slot = var->slot();
1735
1736 // If it was not possible to allocate the variable at compile time,
1737 // we need to "declare" it at runtime to make sure it actually
1738 // exists in the local context.
1739 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1740 // Variables with a "LOOKUP" slot were introduced as non-locals
1741 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001742 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001744 frame_->EmitPush(cp);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001745 frame_->EmitPush(Operand(var->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 // Declaration nodes are always declared in only two modes.
1747 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1748 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001749 frame_->EmitPush(Operand(Smi::FromInt(attr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 // Push initial value, if any.
1751 // Note: For variables we must not push an initial value (such as
1752 // 'undefined') because we may have a (legal) redeclaration and we
1753 // must not destroy the current value.
1754 if (node->mode() == Variable::CONST) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001755 frame_->EmitPushRoot(Heap::kTheHoleValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 } else if (node->fun() != NULL) {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001757 Load(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 } else {
ager@chromium.orgac091b72010-05-05 07:34:42 +00001759 frame_->EmitPush(Operand(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00001761
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001762 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001763 // Ignore the return value (declarations are statements).
ager@chromium.orgac091b72010-05-05 07:34:42 +00001764
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001765 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 return;
1767 }
1768
1769 ASSERT(!var->is_global());
1770
1771 // If we have a function or a constant, we need to initialize the variable.
1772 Expression* val = NULL;
1773 if (node->mode() == Variable::CONST) {
1774 val = new Literal(Factory::the_hole_value());
1775 } else {
1776 val = node->fun(); // NULL if we don't have a function
1777 }
1778
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 if (val != NULL) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001781 WriteBarrierCharacter wb_info =
1782 val->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
1783 if (val->AsLiteral() != NULL) wb_info = NEVER_NEWSPACE;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001784 // Set initial value.
1785 Reference target(this, node->proxy());
1786 Load(val);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001787 target.SetValue(NOT_CONST_INIT, wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001788
iposva@chromium.org245aa852009-02-10 00:49:54 +00001789 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001790 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793}
1794
1795
ager@chromium.org7c537e22008-10-16 08:43:32 +00001796void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797#ifdef DEBUG
1798 int original_height = frame_->height();
1799#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 Expression* expression = node->expression();
1803 expression->MarkAsStatement();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001804 Load(expression);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001805 frame_->Drop();
1806 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807}
1808
1809
ager@chromium.org7c537e22008-10-16 08:43:32 +00001810void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001811#ifdef DEBUG
1812 int original_height = frame_->height();
1813#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001815 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818}
1819
1820
ager@chromium.org7c537e22008-10-16 08:43:32 +00001821void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822#ifdef DEBUG
1823 int original_height = frame_->height();
1824#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001826 // Generate different code depending on which parts of the if statement
1827 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 bool has_then_stm = node->HasThenStatement();
1829 bool has_else_stm = node->HasElseStatement();
1830
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001831 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001833 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001835 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001836 JumpTarget then;
1837 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001839 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001840 if (frame_ != NULL) {
1841 Branch(false, &else_);
1842 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 if (frame_ != NULL || then.is_linked()) {
1845 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001846 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 }
1848 if (frame_ != NULL) {
1849 exit.Jump();
1850 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 if (else_.is_linked()) {
1853 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001854 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856
1857 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001858 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001860 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001862 LoadCondition(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 if (frame_ != NULL) {
1864 Branch(false, &exit);
1865 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 if (frame_ != NULL || then.is_linked()) {
1868 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001869 Visit(node->then_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871
1872 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001873 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001875 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 // if (!cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001877 LoadCondition(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 if (frame_ != NULL) {
1879 Branch(true, &exit);
1880 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 if (frame_ != NULL || else_.is_linked()) {
1883 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001884 Visit(node->else_statement());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001885 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886
1887 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001888 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 ASSERT(!has_then_stm && !has_else_stm);
1890 // if (cond)
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001891 LoadCondition(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001892 if (frame_ != NULL) {
1893 if (has_cc()) {
1894 cc_reg_ = al;
1895 } else {
1896 frame_->Drop();
1897 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 }
1899 }
1900
1901 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 if (exit.is_linked()) {
1903 exit.Bind();
1904 }
1905 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906}
1907
1908
ager@chromium.org7c537e22008-10-16 08:43:32 +00001909void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 CodeForStatementPosition(node);
1912 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913}
1914
1915
ager@chromium.org7c537e22008-10-16 08:43:32 +00001916void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001918 CodeForStatementPosition(node);
1919 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920}
1921
1922
ager@chromium.org7c537e22008-10-16 08:43:32 +00001923void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001925
ager@chromium.org4af710e2009-09-15 12:20:11 +00001926 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001927 Load(node->expression());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001928 frame_->PopToR0();
1929 frame_->PrepareForReturn();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001930 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001931 function_return_.Jump();
1932 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001933 // Pop the result from the frame and prepare the frame for
1934 // returning thus making it easier to merge.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001935 if (function_return_.is_bound()) {
1936 // If the function return label is already bound we reuse the
1937 // code by jumping to the return site.
1938 function_return_.Jump();
1939 } else {
1940 function_return_.Bind();
1941 GenerateReturnSequence();
1942 }
1943 }
1944}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001945
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001946
1947void CodeGenerator::GenerateReturnSequence() {
1948 if (FLAG_trace) {
1949 // Push the return value on the stack as the parameter.
1950 // Runtime::TraceExit returns the parameter as it is.
1951 frame_->EmitPush(r0);
1952 frame_->CallRuntime(Runtime::kTraceExit, 1);
1953 }
1954
1955#ifdef DEBUG
1956 // Add a label for checking the size of the code used for returning.
1957 Label check_exit_codesize;
1958 masm_->bind(&check_exit_codesize);
1959#endif
1960 // Make sure that the constant pool is not emitted inside of the return
1961 // sequence.
1962 { Assembler::BlockConstPoolScope block_const_pool(masm_);
1963 // Tear down the frame which will restore the caller's frame pointer and
1964 // the link register.
1965 frame_->Exit();
1966
1967 // Here we use masm_-> instead of the __ macro to avoid the code coverage
1968 // tool from instrumenting as we rely on the code size here.
1969 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
1970 masm_->add(sp, sp, Operand(sp_delta));
1971 masm_->Jump(lr);
1972 DeleteFrame();
1973
1974#ifdef DEBUG
1975 // Check that the size of the code used for returning matches what is
1976 // expected by the debugger. If the sp_delts above cannot be encoded in
1977 // the add instruction the add will generate two instructions.
1978 int return_sequence_length =
1979 masm_->InstructionsGeneratedSince(&check_exit_codesize);
1980 CHECK(return_sequence_length ==
1981 Assembler::kJSReturnSequenceInstructions ||
1982 return_sequence_length ==
1983 Assembler::kJSReturnSequenceInstructions + 1);
1984#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986}
1987
1988
ager@chromium.org7c537e22008-10-16 08:43:32 +00001989void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990#ifdef DEBUG
1991 int original_height = frame_->height();
1992#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001995 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001996 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001998 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002000 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002001#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002002 JumpTarget verified_true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002003 __ cmp(r0, cp);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002004 verified_true.Branch(eq);
2005 __ stop("PushContext: r0 is expected to be the same as cp");
2006 verified_true.Bind();
2007#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002009 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002010 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011}
2012
2013
ager@chromium.org7c537e22008-10-16 08:43:32 +00002014void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015#ifdef DEBUG
2016 int original_height = frame_->height();
2017#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002019 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 // Pop context.
2021 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
2022 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002023 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025}
2026
2027
ager@chromium.org7c537e22008-10-16 08:43:32 +00002028void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002029#ifdef DEBUG
2030 int original_height = frame_->height();
2031#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002033 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002034 node->break_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002036 Load(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002037
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002038 JumpTarget next_test;
2039 JumpTarget fall_through;
2040 JumpTarget default_entry;
2041 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 ZoneList<CaseClause*>* cases = node->cases();
2043 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045
2046 for (int i = 0; i < length; i++) {
2047 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002049 // Remember the default clause and compile it at the end.
2050 default_clause = clause;
2051 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 }
2053
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054 Comment cmnt(masm_, "[ Case clause");
2055 // Compile the test.
2056 next_test.Bind();
2057 next_test.Unuse();
2058 // Duplicate TOS.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002059 frame_->Dup();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002060 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002063 // Before entering the body from the test, remove the switch value from
2064 // the stack.
2065 frame_->Drop();
2066
2067 // Label the body so that fall through is enabled.
2068 if (i > 0 && cases->at(i - 1)->is_default()) {
2069 default_exit.Bind();
2070 } else {
2071 fall_through.Bind();
2072 fall_through.Unuse();
2073 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002074 VisitStatements(clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075
2076 // If control flow can fall through from the body, jump to the next body
2077 // or the end of the statement.
2078 if (frame_ != NULL) {
2079 if (i < length - 1 && cases->at(i + 1)->is_default()) {
2080 default_entry.Jump();
2081 } else {
2082 fall_through.Jump();
2083 }
2084 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 }
2086
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 // The final "test" removes the switch value.
2088 next_test.Bind();
2089 frame_->Drop();
2090
2091 // If there is a default clause, compile it.
2092 if (default_clause != NULL) {
2093 Comment cmnt(masm_, "[ Default clause");
2094 default_entry.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002095 VisitStatements(default_clause->statements());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096 // If control flow can fall out of the default and there is a case after
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002097 // it, jump to that case's body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 if (frame_ != NULL && default_exit.is_bound()) {
2099 default_exit.Jump();
2100 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002101 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002103 if (fall_through.is_linked()) {
2104 fall_through.Bind();
2105 }
2106
2107 if (node->break_target()->is_linked()) {
2108 node->break_target()->Bind();
2109 }
2110 node->break_target()->Unuse();
2111 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112}
2113
2114
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002115void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116#ifdef DEBUG
2117 int original_height = frame_->height();
2118#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002119 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002120 CodeForStatementPosition(node);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002121 node->break_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002122 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002123 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002125 // Label the top of the loop for the backward CFG edge. If the test
2126 // is always true we can use the continue target, and if the test is
2127 // always false there is no need.
2128 ConditionAnalysis info = AnalyzeCondition(node->cond());
2129 switch (info) {
2130 case ALWAYS_TRUE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002131 node->continue_target()->SetExpectedHeight();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002133 break;
2134 case ALWAYS_FALSE:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002135 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002136 break;
2137 case DONT_KNOW:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002138 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002139 body.Bind();
2140 break;
2141 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002143 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002144 Visit(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002146 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002147 switch (info) {
2148 case ALWAYS_TRUE:
2149 // If control can fall off the end of the body, jump back to the
2150 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002152 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002153 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002155 case ALWAYS_FALSE:
2156 // If we have a continue in the body, we only have to bind its
2157 // jump target.
2158 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002161 break;
2162 case DONT_KNOW:
2163 // We have to compile the test expression if it can be reached by
2164 // control flow falling out of the body or via continue.
2165 if (node->continue_target()->is_linked()) {
2166 node->continue_target()->Bind();
2167 }
2168 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002169 Comment cmnt(masm_, "[ DoWhileCondition");
2170 CodeForDoWhileConditionPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002171 LoadCondition(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002173 // A invalid frame here indicates that control did not
2174 // fall out of the test expression.
2175 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 }
2177 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 break;
2179 }
2180
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002181 if (node->break_target()->is_linked()) {
2182 node->break_target()->Bind();
2183 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002184 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002185 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2186}
2187
2188
2189void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
2190#ifdef DEBUG
2191 int original_height = frame_->height();
2192#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002193 Comment cmnt(masm_, "[ WhileStatement");
2194 CodeForStatementPosition(node);
2195
2196 // If the test is never true and has no side effects there is no need
2197 // to compile the test or body.
2198 ConditionAnalysis info = AnalyzeCondition(node->cond());
2199 if (info == ALWAYS_FALSE) return;
2200
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002201 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002202 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002203
2204 // Label the top of the loop with the continue target for the backward
2205 // CFG edge.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002206 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002207 node->continue_target()->Bind();
2208
2209 if (info == DONT_KNOW) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002210 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002211 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002212 if (has_valid_frame()) {
2213 // A NULL frame indicates that control did not fall out of the
2214 // test expression.
2215 Branch(false, node->break_target());
2216 }
2217 if (has_valid_frame() || body.is_linked()) {
2218 body.Bind();
2219 }
2220 }
2221
2222 if (has_valid_frame()) {
2223 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002224 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002225
2226 // If control flow can fall out of the body, jump back to the top.
2227 if (has_valid_frame()) {
2228 node->continue_target()->Jump();
2229 }
2230 }
2231 if (node->break_target()->is_linked()) {
2232 node->break_target()->Bind();
2233 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002234 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002235 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2236}
2237
2238
2239void CodeGenerator::VisitForStatement(ForStatement* node) {
2240#ifdef DEBUG
2241 int original_height = frame_->height();
2242#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002243 Comment cmnt(masm_, "[ ForStatement");
2244 CodeForStatementPosition(node);
2245 if (node->init() != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002246 Visit(node->init());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002247 }
2248
2249 // If the test is never true there is no need to compile the test or
2250 // body.
2251 ConditionAnalysis info = AnalyzeCondition(node->cond());
2252 if (info == ALWAYS_FALSE) return;
2253
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002254 node->break_target()->SetExpectedHeight();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002255 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002256
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002257 // We know that the loop index is a smi if it is not modified in the
2258 // loop body and it is checked against a constant limit in the loop
2259 // condition. In this case, we reset the static type information of the
2260 // loop index to smi before compiling the body, the update expression, and
2261 // the bottom check of the loop condition.
2262 TypeInfoCodeGenState type_info_scope(this,
2263 node->is_fast_smi_loop() ?
2264 node->loop_variable()->slot() :
2265 NULL,
2266 TypeInfo::Smi());
2267
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002268 // If there is no update statement, label the top of the loop with the
2269 // continue target, otherwise with the loop target.
2270 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2271 if (node->next() == NULL) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002272 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002273 node->continue_target()->Bind();
2274 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002275 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002276 loop.Bind();
2277 }
2278
2279 // If the test is always true, there is no need to compile it.
2280 if (info == DONT_KNOW) {
2281 JumpTarget body;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002282 LoadCondition(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002283 if (has_valid_frame()) {
2284 Branch(false, node->break_target());
2285 }
2286 if (has_valid_frame() || body.is_linked()) {
2287 body.Bind();
2288 }
2289 }
2290
2291 if (has_valid_frame()) {
2292 CheckStack(); // TODO(1222600): ignore if body contains calls.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002293 Visit(node->body());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002294
2295 if (node->next() == NULL) {
2296 // If there is no update statement and control flow can fall out
2297 // of the loop, jump directly to the continue label.
2298 if (has_valid_frame()) {
2299 node->continue_target()->Jump();
2300 }
2301 } else {
2302 // If there is an update statement and control flow can reach it
2303 // via falling out of the body of the loop or continuing, we
2304 // compile the update statement.
2305 if (node->continue_target()->is_linked()) {
2306 node->continue_target()->Bind();
2307 }
2308 if (has_valid_frame()) {
2309 // Record source position of the statement as this code which is
2310 // after the code for the body actually belongs to the loop
2311 // statement and not the body.
2312 CodeForStatementPosition(node);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002313 Visit(node->next());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002314 loop.Jump();
2315 }
2316 }
2317 }
2318 if (node->break_target()->is_linked()) {
2319 node->break_target()->Bind();
2320 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002321 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002322 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323}
2324
2325
ager@chromium.org7c537e22008-10-16 08:43:32 +00002326void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002327#ifdef DEBUG
2328 int original_height = frame_->height();
2329#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002331 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002333 JumpTarget primitive;
2334 JumpTarget jsobject;
2335 JumpTarget fixed_array;
2336 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2337 JumpTarget end_del_check;
2338 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339
2340 // Get the object to enumerate over (converted to JSObject).
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002341 Load(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002343 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2345 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002347 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2348 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002350 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2351 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002352 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
2354 // Stack layout in body:
2355 // [iteration counter (Smi)]
2356 // [length of array]
2357 // [FixedArray]
2358 // [Map or 0]
2359 // [Object]
2360
2361 // Check if enumerable is already a JSObject
2362 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002364 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002367 primitive.Bind();
2368 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002369 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002373 // r0: value to be iterated over
2374 frame_->EmitPush(r0); // Push the object being iterated over.
2375
2376 // Check cache validity in generated code. This is a fast case for
2377 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2378 // guarantee cache validity, call the runtime system to check cache
2379 // validity or get the property names in a fixed array.
2380 JumpTarget call_runtime;
2381 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2382 JumpTarget check_prototype;
2383 JumpTarget use_cache;
2384 __ mov(r1, Operand(r0));
2385 loop.Bind();
2386 // Check that there are no elements.
2387 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2388 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2389 __ cmp(r2, r4);
2390 call_runtime.Branch(ne);
2391 // Check that instance descriptors are not empty so that we can
2392 // check for an enum cache. Leave the map in r3 for the subsequent
2393 // prototype load.
2394 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2395 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2396 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2397 __ cmp(r2, ip);
2398 call_runtime.Branch(eq);
2399 // Check that there in an enum cache in the non-empty instance
2400 // descriptors. This is the case if the next enumeration index
2401 // field does not contain a smi.
2402 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2403 __ tst(r2, Operand(kSmiTagMask));
2404 call_runtime.Branch(eq);
2405 // For all objects but the receiver, check that the cache is empty.
2406 // r4: empty fixed array root.
2407 __ cmp(r1, r0);
2408 check_prototype.Branch(eq);
2409 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2410 __ cmp(r2, r4);
2411 call_runtime.Branch(ne);
2412 check_prototype.Bind();
2413 // Load the prototype from the map and loop if non-null.
2414 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2415 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2416 __ cmp(r1, ip);
2417 loop.Branch(ne);
2418 // The enum cache is valid. Load the map of the object being
2419 // iterated over and use the cache for the iteration.
2420 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2421 use_cache.Jump();
2422
2423 call_runtime.Bind();
2424 // Call the runtime to get the property names for the object.
2425 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002426 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002428 // If we got a map from the runtime call, we can do a fast
2429 // modification check. Otherwise, we got a fixed array, and we have
2430 // to do a slow check.
2431 // r0: map or fixed array (result from call to
2432 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 __ mov(r2, Operand(r0));
2434 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002435 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2436 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002437 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002439 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002440 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002441 // r0: map (either the result from a call to
2442 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2443 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 __ mov(r1, Operand(r0));
2445 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2446 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2447 __ ldr(r2,
2448 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2449
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 frame_->EmitPush(r0); // map
2451 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002452 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002453 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002454 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 frame_->EmitPush(r0);
2456 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 frame_->EmitPush(r1); // insert 0 in place of Map
2461 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462
2463 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002464 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002466 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
2469 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002471 // sp[0] : index
2472 // sp[1] : array/enum cache length
2473 // sp[2] : array or enum cache
2474 // sp[3] : 0 or map
2475 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 // Grab the current frame's height for the break and continue
2477 // targets only after all the state is pushed on the frame.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002478 node->break_target()->SetExpectedHeight();
2479 node->continue_target()->SetExpectedHeight();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002481 // Load the current count to r0, load the length to r1.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002482 __ Ldrd(r0, r1, frame_->ElementAt(0));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00002483 __ cmp(r0, r1); // compare to the array length
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484 node->break_target()->Branch(hs);
2485
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2489 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2490
2491 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002492 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493 // Check if this (still) matches the map of the enumerable.
2494 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002495 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2497 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499
2500 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002501 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2502 frame_->EmitPush(r0);
2503 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002504 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002505 __ mov(r3, Operand(r0), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506 // If the property has been removed while iterating, we just skip it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002507 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 end_del_check.Bind();
2510 // Store the entry in the 'each' expression and take another spin in the
2511 // loop. r3: i'th entry of the enum cache (or string there of)
2512 frame_->EmitPush(r3); // push entry
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002513 { VirtualFrame::RegisterAllocationScope scope(this);
2514 Reference each(this, node->each());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002516 if (each.size() > 0) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002517 // Loading a reference may leave the frame in an unspilled state.
2518 frame_->SpillAll(); // Sync stack to memory.
2519 // Get the value (under the reference on the stack) from memory.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002520 __ ldr(r0, frame_->ElementAt(each.size()));
2521 frame_->EmitPush(r0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002522 each.SetValue(NOT_CONST_INIT, UNLIKELY_SMI);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002523 frame_->Drop(2); // The result of the set and the extra pushed value.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002524 } else {
2525 // If the reference was to a slot we rely on the convenient property
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002526 // that it doesn't matter whether a value (eg, ebx pushed above) is
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002527 // right on top of or right underneath a zero-sized reference.
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(1); // Drop the result of the set operation.
mads.s.ager31e71382008-08-13 09:32:07 +00002530 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 }
2532 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 CheckStack(); // TODO(1222600): ignore if body contains calls.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002535 { VirtualFrame::RegisterAllocationScope scope(this);
2536 Visit(node->body());
2537 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002538
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002539 // Next. Reestablish a spilled frame in case we are coming here via
2540 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002541 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002542 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 frame_->EmitPop(r0);
2544 __ add(r0, r0, Operand(Smi::FromInt(1)));
2545 frame_->EmitPush(r0);
2546 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002548 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2549 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002551 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552
2553 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 exit.Bind();
2555 node->continue_target()->Unuse();
2556 node->break_target()->Unuse();
2557 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558}
2559
2560
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002561void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562#ifdef DEBUG
2563 int original_height = frame_->height();
2564#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002565 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002566 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002569 JumpTarget try_block;
2570 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
2576 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002577 Variable* catch_var = node->catch_var()->var();
2578 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2579 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580
2581 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002584 { VirtualFrame::RegisterAllocationScope scope(this);
2585 VisitStatements(node->catch_block()->statements());
2586 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002587 if (frame_ != NULL) {
2588 exit.Jump();
2589 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590
2591
2592 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002593 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2596 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002598 // Shadow the labels for all escapes from the try block, including
2599 // returns. During shadowing, the original label is hidden as the
2600 // LabelShadow and operations on the original actually affect the
2601 // shadowing label.
2602 //
2603 // We should probably try to unify the escaping labels and the return
2604 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002605 int nof_escapes = node->escaping_targets()->length();
2606 List<ShadowTarget*> shadows(1 + nof_escapes);
2607
2608 // Add the shadow target for the function return.
2609 static const int kReturnShadowIndex = 0;
2610 shadows.Add(new ShadowTarget(&function_return_));
2611 bool function_return_was_shadowed = function_return_is_shadowed_;
2612 function_return_is_shadowed_ = true;
2613 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2614
2615 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 }
2619
2620 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002621 { VirtualFrame::RegisterAllocationScope scope(this);
2622 VisitStatements(node->try_block()->statements());
2623 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
2625 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002626 // After shadowing stops, the original labels are unshadowed and the
2627 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002628 bool has_unlinks = false;
2629 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002631 has_unlinks = has_unlinks || shadows[i]->is_linked();
2632 }
2633 function_return_is_shadowed_ = function_return_was_shadowed;
2634
2635 // Get an external reference to the handler address.
2636 ExternalReference handler_address(Top::k_handler_address);
2637
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002638 // If we can fall off the end of the try block, unlink from try chain.
2639 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002640 // The next handler address is on top of the frame. Unlink from
2641 // the handler list and drop the rest of this handler from the
2642 // frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002643 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002644 frame_->EmitPop(r1); // r0 can contain the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 __ mov(r3, Operand(handler_address));
2646 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002647 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002648 if (has_unlinks) {
2649 exit.Jump();
2650 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 }
2652
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002653 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654 // jumped to. Deallocate each shadow target.
2655 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002657 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002658 shadows[i]->Bind();
2659 // Because we can be jumping here (to spilled code) from unspilled
2660 // code, we need to reestablish a spilled frame at this block.
2661 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 // Reload sp from the top handler, because some statements that we
2664 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002667 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002669 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002670 frame_->EmitPop(r1); // r0 can contain the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002672 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002674 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2675 frame_->PrepareForReturn();
2676 }
2677 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 }
2679 }
2680
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 exit.Bind();
2682 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683}
2684
2685
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002686void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687#ifdef DEBUG
2688 int original_height = frame_->height();
2689#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002690 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002691 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693
2694 // State: Used to keep track of reason for entering the finally
2695 // block. Should probably be extended to hold information for
2696 // break/continue from within the try block.
2697 enum { FALLING, THROWING, JUMPING };
2698
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002699 JumpTarget try_block;
2700 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002702 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002704 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 // In case of thrown exceptions, this is where we continue.
2706 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002707 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708
2709 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002710 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002712 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2713 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002715 // Shadow the labels for all escapes from the try block, including
2716 // returns. Shadowing hides the original label as the LabelShadow and
2717 // operations on the original actually affect the shadowing label.
2718 //
2719 // We should probably try to unify the escaping labels and the return
2720 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002721 int nof_escapes = node->escaping_targets()->length();
2722 List<ShadowTarget*> shadows(1 + nof_escapes);
2723
2724 // Add the shadow target for the function return.
2725 static const int kReturnShadowIndex = 0;
2726 shadows.Add(new ShadowTarget(&function_return_));
2727 bool function_return_was_shadowed = function_return_is_shadowed_;
2728 function_return_is_shadowed_ = true;
2729 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2730
2731 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002733 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734 }
2735
2736 // Generate code for the statements in the try block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002737 { VirtualFrame::RegisterAllocationScope scope(this);
2738 VisitStatements(node->try_block()->statements());
2739 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002741 // Stop the introduced shadowing and count the number of required unlinks.
2742 // After shadowing stops, the original labels are unshadowed and the
2743 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746 shadows[i]->StopShadowing();
2747 if (shadows[i]->is_linked()) nof_unlinks++;
2748 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002749 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002751 // Get an external reference to the handler address.
2752 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 // If we can fall off the end of the try block, unlink from the try
2755 // chain and set the state on the frame to FALLING.
2756 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002757 // The next handler address is on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002758 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002759 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 __ mov(r3, Operand(handler_address));
2761 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002762 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002763
2764 // Fake a top of stack value (unneeded when FALLING) and set the
2765 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002766 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2769 if (nof_unlinks > 0) {
2770 finally_block.Jump();
2771 }
2772 }
2773
2774 // Generate code to unlink and set the state for the (formerly)
2775 // shadowing targets that have been jumped to.
2776 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002778 // If we have come from the shadowed return, the return value is
2779 // in (a non-refcounted reference to) r0. We must preserve it
2780 // until it is pushed.
2781 //
2782 // Because we can be jumping here (to spilled code) from
2783 // unspilled code, we need to reestablish a spilled frame at
2784 // this block.
2785 shadows[i]->Bind();
2786 frame_->SpillAll();
2787
2788 // Reload sp from the top handler, because some statements that
2789 // we break from (eg, for...in) may have left stuff on the
2790 // stack.
2791 __ mov(r3, Operand(handler_address));
2792 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002793 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002794
2795 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002796 // handler address is currently on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00002797 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002798 frame_->EmitPop(r1);
2799 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002800 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002801
2802 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002803 // If this label shadowed the function return, materialize the
2804 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002805 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002806 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002807 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002808 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002809 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 }
2811 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002812 if (--nof_unlinks > 0) {
2813 // If this is not the last unlink block, jump around the next.
2814 finally_block.Jump();
2815 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 }
2817 }
2818
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002820 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002822 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002823 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002824
2825 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002826 // and the state - while evaluating the finally block.
2827 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 // Generate code for the statements in the finally block.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002829 { VirtualFrame::RegisterAllocationScope scope(this);
2830 VisitStatements(node->finally_block()->statements());
2831 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833 if (has_valid_frame()) {
2834 // Restore state and return value or faked TOS.
2835 frame_->EmitPop(r2);
2836 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 }
2838
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 // Generate code to jump to the right destination for all used
2840 // formerly shadowing targets. Deallocate each shadow target.
2841 for (int i = 0; i < shadows.length(); i++) {
2842 if (has_valid_frame() && shadows[i]->is_bound()) {
2843 JumpTarget* original = shadows[i]->other_target();
2844 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2845 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002846 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 skip.Branch(ne);
2848 frame_->PrepareForReturn();
2849 original->Jump();
2850 skip.Bind();
2851 } else {
2852 original->Branch(eq);
2853 }
2854 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002855 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002856
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857 if (has_valid_frame()) {
2858 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002859 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002860 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2861 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002863 // Rethrow exception.
2864 frame_->EmitPush(r0);
2865 frame_->CallRuntime(Runtime::kReThrow, 1);
2866
2867 // Done.
2868 exit.Bind();
2869 }
2870 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871}
2872
2873
ager@chromium.org7c537e22008-10-16 08:43:32 +00002874void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002875#ifdef DEBUG
2876 int original_height = frame_->height();
2877#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002879 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002880#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002881 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002882#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002883 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885}
2886
2887
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002888void CodeGenerator::InstantiateFunction(
2889 Handle<SharedFunctionInfo> function_info) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002890 // Use the fast case closure allocation code that allocates in new
2891 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002892 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002893 FastNewClosureStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002894 frame_->EmitPush(Operand(function_info));
2895 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002896 frame_->CallStub(&stub, 1);
2897 frame_->EmitPush(r0);
2898 } else {
2899 // Create a new closure.
2900 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002901 frame_->EmitPush(Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002902 frame_->CallRuntime(Runtime::kNewClosure, 2);
2903 frame_->EmitPush(r0);
2904 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905}
2906
2907
ager@chromium.org7c537e22008-10-16 08:43:32 +00002908void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002909#ifdef DEBUG
2910 int original_height = frame_->height();
2911#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 Comment cmnt(masm_, "[ FunctionLiteral");
2913
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002914 // Build the function info and instantiate it.
2915 Handle<SharedFunctionInfo> function_info =
2916 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002917 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002918 if (HasStackOverflow()) {
2919 ASSERT(frame_->height() == original_height);
2920 return;
2921 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002922 InstantiateFunction(function_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00002923 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924}
2925
2926
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002927void CodeGenerator::VisitSharedFunctionInfoLiteral(
2928 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002929#ifdef DEBUG
2930 int original_height = frame_->height();
2931#endif
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002932 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2933 InstantiateFunction(node->shared_function_info());
ager@chromium.orgac091b72010-05-05 07:34:42 +00002934 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935}
2936
2937
ager@chromium.org7c537e22008-10-16 08:43:32 +00002938void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002939#ifdef DEBUG
2940 int original_height = frame_->height();
2941#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002943 JumpTarget then;
2944 JumpTarget else_;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002945 LoadCondition(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002946 if (has_valid_frame()) {
2947 Branch(false, &else_);
2948 }
2949 if (has_valid_frame() || then.is_linked()) {
2950 then.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002951 Load(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002952 }
2953 if (else_.is_linked()) {
2954 JumpTarget exit;
2955 if (has_valid_frame()) exit.Jump();
2956 else_.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002957 Load(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002958 if (exit.is_linked()) exit.Bind();
2959 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00002960 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961}
2962
2963
ager@chromium.org7c537e22008-10-16 08:43:32 +00002964void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2965 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002966 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00002968 // JumpTargets do not yet support merging frames so the frame must be
2969 // spilled when jumping to these targets.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002970 JumpTarget slow;
2971 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002972
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002973 // Generate fast case for loading from slots that correspond to
2974 // local/global variables or arguments unless they are shadowed by
2975 // eval-introduced bindings.
2976 EmitDynamicLoadFromSlotFastCase(slot,
2977 typeof_state,
2978 &slow,
2979 &done);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002980
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002981 slow.Bind();
2982 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002983 frame_->EmitPush(Operand(slot->var()->name()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002984
ager@chromium.org7c537e22008-10-16 08:43:32 +00002985 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002986 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002987 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002988 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002990
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002991 done.Bind();
2992 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993
2994 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002995 Register scratch = VirtualFrame::scratch0();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002996 TypeInfo info = type_info(slot);
2997 frame_->EmitPush(SlotOperand(slot, scratch), info);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002998
ager@chromium.org7c537e22008-10-16 08:43:32 +00002999 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003000 // Const slots may contain 'the hole' value (the constant hasn't been
3001 // initialized yet) which needs to be converted into the 'undefined'
3002 // value.
3003 Comment cmnt(masm_, "[ Unhole const");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003004 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003005 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003006 __ cmp(tos, ip);
3007 __ LoadRoot(tos, Heap::kUndefinedValueRootIndex, eq);
3008 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009 }
3010 }
3011}
3012
3013
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003014void CodeGenerator::LoadFromSlotCheckForArguments(Slot* slot,
3015 TypeofState state) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003016 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003017 LoadFromSlot(slot, state);
3018
3019 // Bail out quickly if we're not using lazy arguments allocation.
3020 if (ArgumentsMode() != LAZY_ARGUMENTS_ALLOCATION) return;
3021
3022 // ... or if the slot isn't a non-parameter arguments slot.
3023 if (slot->type() == Slot::PARAMETER || !slot->is_arguments()) return;
3024
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003025 // Load the loaded value from the stack into a register but leave it on the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003026 // stack.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003027 Register tos = frame_->Peek();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003028
3029 // If the loaded value is the sentinel that indicates that we
3030 // haven't loaded the arguments object yet, we need to do it now.
3031 JumpTarget exit;
3032 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003033 __ cmp(tos, ip);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003034 exit.Branch(ne);
3035 frame_->Drop();
3036 StoreArgumentsObject(false);
3037 exit.Bind();
3038}
3039
3040
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003041void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
3042 ASSERT(slot != NULL);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003043 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003044 if (slot->type() == Slot::LOOKUP) {
3045 ASSERT(slot->var()->is_dynamic());
3046
3047 // For now, just do a runtime call.
3048 frame_->EmitPush(cp);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003049 frame_->EmitPush(Operand(slot->var()->name()));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003050
3051 if (init_state == CONST_INIT) {
3052 // Same as the case for a normal store, but ignores attribute
3053 // (e.g. READ_ONLY) of context slot so that we can initialize
3054 // const properties (introduced via eval("const foo = (some
3055 // expr);")). Also, uses the current function context instead of
3056 // the top context.
3057 //
3058 // Note that we must declare the foo upon entry of eval(), via a
3059 // context slot declaration, but we cannot initialize it at the
3060 // same time, because the const declaration may be at the end of
3061 // the eval code (sigh...) and the const variable may have been
3062 // used before (where its value is 'undefined'). Thus, we can only
3063 // do the initialization when we actually encounter the expression
3064 // and when the expression operands are defined and valid, and
3065 // thus we need the split into 2 operations: declaration of the
3066 // context slot followed by initialization.
3067 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3068 } else {
3069 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
3070 }
3071 // Storing a variable must keep the (new) value on the expression
3072 // stack. This is necessary for compiling assignment expressions.
3073 frame_->EmitPush(r0);
3074
3075 } else {
3076 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00003077 Register scratch = VirtualFrame::scratch0();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003078 Register scratch2 = VirtualFrame::scratch1();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003079
ager@chromium.org357bf652010-04-12 11:30:10 +00003080 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003081 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00003082
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003083 if (init_state == CONST_INIT) {
3084 ASSERT(slot->var()->mode() == Variable::CONST);
3085 // Only the first const initialization must be executed (the slot
3086 // still contains 'the hole' value). When the assignment is
3087 // executed, the code is identical to a normal store (see below).
3088 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00003089 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003090 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00003091 __ cmp(scratch, ip);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003092 exit.Branch(ne);
3093 }
3094
3095 // We must execute the store. Storing a variable must keep the
3096 // (new) value on the stack. This is necessary for compiling
3097 // assignment expressions.
3098 //
3099 // Note: We will reach here even with slot->var()->mode() ==
3100 // Variable::CONST because of const declarations which will
3101 // initialize consts to 'the hole' value and by doing so, end up
3102 // calling this code. r2 may be loaded with context; used below in
3103 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00003104 Register tos = frame_->Peek();
3105 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003106 if (slot->type() == Slot::CONTEXT) {
3107 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00003108 __ tst(tos, Operand(kSmiTagMask));
3109 // We don't use tos any more after here.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003110 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00003111 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003112 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003113 // We need an extra register. Until we have a way to do that in the
3114 // virtual frame we will cheat and ask for a free TOS register.
3115 Register scratch3 = frame_->GetTOSRegister();
3116 __ RecordWrite(scratch, Operand(offset), scratch2, scratch3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003117 }
3118 // If we definitely did not jump over the assignment, we do not need
3119 // to bind the exit label. Doing so can defeat peephole
3120 // optimization.
3121 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3122 exit.Bind();
3123 }
3124 }
3125}
3126
3127
ager@chromium.org381abbb2009-02-25 13:23:22 +00003128void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
3129 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003130 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00003131 // Check that no extension objects have been created by calls to
3132 // eval from the current scope to the global scope.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003133 Register tmp = frame_->scratch0();
3134 Register tmp2 = frame_->scratch1();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003135 Register context = cp;
3136 Scope* s = scope();
3137 while (s != NULL) {
3138 if (s->num_heap_slots() > 0) {
3139 if (s->calls_eval()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003140 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003141 // Check that extension is NULL.
3142 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
3143 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003144 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003145 }
3146 // Load next context in chain.
3147 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
3148 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3149 context = tmp;
3150 }
3151 // If no outer scope calls eval, we do not need to check more
3152 // context extensions.
3153 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
3154 s = s->outer_scope();
3155 }
3156
3157 if (s->is_eval_scope()) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00003158 frame_->SpillAll();
ager@chromium.org381abbb2009-02-25 13:23:22 +00003159 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00003160 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003161 __ bind(&next);
3162 // Terminate at global context.
3163 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003164 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
3165 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003166 __ b(eq, &fast);
3167 // Check that extension is NULL.
3168 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
3169 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003170 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003171 // Load next context in chain.
3172 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
3173 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
3174 __ b(&next);
3175 __ bind(&fast);
3176 }
3177
ager@chromium.org381abbb2009-02-25 13:23:22 +00003178 // Load the global object.
3179 LoadGlobal();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003180 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00003181 frame_->CallLoadIC(slot->var()->name(),
3182 typeof_state == INSIDE_TYPEOF
3183 ? RelocInfo::CODE_TARGET
3184 : RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003185}
3186
3187
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00003188void CodeGenerator::EmitDynamicLoadFromSlotFastCase(Slot* slot,
3189 TypeofState typeof_state,
3190 JumpTarget* slow,
3191 JumpTarget* done) {
3192 // Generate fast-case code for variables that might be shadowed by
3193 // eval-introduced variables. Eval is used a lot without
3194 // introducing variables. In those cases, we do not want to
3195 // perform a runtime call for all variables in the scope
3196 // containing the eval.
3197 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
3198 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, slow);
3199 frame_->SpillAll();
3200 done->Jump();
3201
3202 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
3203 frame_->SpillAll();
3204 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
3205 Expression* rewrite = slot->var()->local_if_not_shadowed()->rewrite();
3206 if (potential_slot != NULL) {
3207 // Generate fast case for locals that rewrite to slots.
3208 __ ldr(r0,
3209 ContextSlotOperandCheckExtensions(potential_slot,
3210 r1,
3211 r2,
3212 slow));
3213 if (potential_slot->var()->mode() == Variable::CONST) {
3214 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
3215 __ cmp(r0, ip);
3216 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
3217 }
3218 done->Jump();
3219 } else if (rewrite != NULL) {
3220 // Generate fast case for argument loads.
3221 Property* property = rewrite->AsProperty();
3222 if (property != NULL) {
3223 VariableProxy* obj_proxy = property->obj()->AsVariableProxy();
3224 Literal* key_literal = property->key()->AsLiteral();
3225 if (obj_proxy != NULL &&
3226 key_literal != NULL &&
3227 obj_proxy->IsArguments() &&
3228 key_literal->handle()->IsSmi()) {
3229 // Load arguments object if there are no eval-introduced
3230 // variables. Then load the argument from the arguments
3231 // object using keyed load.
3232 __ ldr(r0,
3233 ContextSlotOperandCheckExtensions(obj_proxy->var()->slot(),
3234 r1,
3235 r2,
3236 slow));
3237 frame_->EmitPush(r0);
3238 __ mov(r1, Operand(key_literal->handle()));
3239 frame_->EmitPush(r1);
3240 EmitKeyedLoad();
3241 done->Jump();
3242 }
3243 }
3244 }
3245 }
3246}
3247
3248
ager@chromium.org7c537e22008-10-16 08:43:32 +00003249void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003250#ifdef DEBUG
3251 int original_height = frame_->height();
3252#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003253 Comment cmnt(masm_, "[ Slot");
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00003254 LoadFromSlotCheckForArguments(node, NOT_INSIDE_TYPEOF);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003255 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003256}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257
ager@chromium.org7c537e22008-10-16 08:43:32 +00003258
3259void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003260#ifdef DEBUG
3261 int original_height = frame_->height();
3262#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00003263 Comment cmnt(masm_, "[ VariableProxy");
3264
3265 Variable* var = node->var();
3266 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003267 if (expr != NULL) {
3268 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003269 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00003270 ASSERT(var->is_global());
3271 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003272 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003274 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003275}
3276
3277
ager@chromium.org7c537e22008-10-16 08:43:32 +00003278void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279#ifdef DEBUG
3280 int original_height = frame_->height();
3281#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00003283 Register reg = frame_->GetTOSRegister();
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003284 bool is_smi = node->handle()->IsSmi();
ager@chromium.org357bf652010-04-12 11:30:10 +00003285 __ mov(reg, Operand(node->handle()));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003286 frame_->EmitPush(reg, is_smi ? TypeInfo::Smi() : TypeInfo::Unknown());
ager@chromium.orgac091b72010-05-05 07:34:42 +00003287 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288}
3289
3290
ager@chromium.org7c537e22008-10-16 08:43:32 +00003291void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292#ifdef DEBUG
3293 int original_height = frame_->height();
3294#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003295 Comment cmnt(masm_, "[ RexExp Literal");
3296
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003297 Register tmp = VirtualFrame::scratch0();
3298 // Free up a TOS register that can be used to push the literal.
3299 Register literal = frame_->GetTOSRegister();
3300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301 // Retrieve the literal array and check the allocated entry.
3302
3303 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003304 __ ldr(tmp, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305
3306 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003307 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kLiteralsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003308
3309 // Load the literal at the ast saved index.
3310 int literal_offset =
3311 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003312 __ ldr(literal, FieldMemOperand(tmp, literal_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003314 JumpTarget materialized;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003315 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003316 __ cmp(literal, ip);
3317 // This branch locks the virtual frame at the done label to match the
3318 // one we have here, where the literal register is not on the stack and
3319 // nothing is spilled.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003320 materialized.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003322 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323 // the literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003324 // literal array (0)
3325 frame_->EmitPush(tmp);
3326 // literal index (1)
3327 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3328 // RegExp pattern (2)
3329 frame_->EmitPush(Operand(node->pattern()));
3330 // RegExp flags (3)
3331 frame_->EmitPush(Operand(node->flags()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003332 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003333 __ Move(literal, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003334
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003335 materialized.Bind();
3336
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003337 frame_->EmitPush(literal);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00003338 int size = JSRegExp::kSize + JSRegExp::kInObjectFieldCount * kPointerSize;
3339 frame_->EmitPush(Operand(Smi::FromInt(size)));
3340 frame_->CallRuntime(Runtime::kAllocateInNewSpace, 1);
3341 // TODO(lrn): Use AllocateInNewSpace macro with fallback to runtime.
3342 // r0 is newly allocated space.
3343
3344 // Reuse literal variable with (possibly) a new register, still holding
3345 // the materialized boilerplate.
3346 literal = frame_->PopToRegister(r0);
3347
3348 __ CopyFields(r0, literal, tmp.bit(), size / kPointerSize);
3349
3350 // Push the clone.
3351 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003352 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003353}
3354
3355
ager@chromium.org7c537e22008-10-16 08:43:32 +00003356void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003357#ifdef DEBUG
3358 int original_height = frame_->height();
3359#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360 Comment cmnt(masm_, "[ ObjectLiteral");
3361
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003362 Register literal = frame_->GetTOSRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003364 __ ldr(literal, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003365 // Literal array.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003366 __ ldr(literal, FieldMemOperand(literal, JSFunction::kLiteralsOffset));
3367 frame_->EmitPush(literal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003368 // Literal index.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003369 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003370 // Constant properties.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003371 frame_->EmitPush(Operand(node->constant_properties()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003372 // Should the object literal have fast elements?
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003373 frame_->EmitPush(Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003374 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003375 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003376 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003377 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003378 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003379 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003380 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003381 // At the start of each iteration, the top of stack contains
3382 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383 ObjectLiteral::Property* property = node->properties()->at(i);
3384 Literal* key = property->key();
3385 Expression* value = property->value();
3386 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003387 case ObjectLiteral::Property::CONSTANT:
3388 break;
3389 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3390 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3391 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003392 case ObjectLiteral::Property::COMPUTED:
3393 if (key->handle()->IsSymbol()) {
3394 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003395 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003396 frame_->PopToR0();
3397 // Fetch the object literal.
3398 frame_->SpillAllButCopyTOSToR1();
ager@chromium.org5c838252010-02-19 08:53:10 +00003399 __ mov(r2, Operand(key->handle()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003400 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3401 break;
3402 }
3403 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003404 case ObjectLiteral::Property::PROTOTYPE: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003405 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003406 Load(key);
3407 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003408 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003409 break;
3410 }
3411 case ObjectLiteral::Property::SETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003412 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003413 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003414 frame_->EmitPush(Operand(Smi::FromInt(1)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003415 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003416 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003417 break;
3418 }
3419 case ObjectLiteral::Property::GETTER: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003420 frame_->Dup();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003421 Load(key);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003422 frame_->EmitPush(Operand(Smi::FromInt(0)));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003423 Load(value);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003424 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 break;
3426 }
3427 }
3428 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003429 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430}
3431
3432
ager@chromium.org7c537e22008-10-16 08:43:32 +00003433void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003434#ifdef DEBUG
3435 int original_height = frame_->height();
3436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003438
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003439 Register tos = frame_->GetTOSRegister();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003440 // Load the function of this activation.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003441 __ ldr(tos, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003442 // Load the literals array of the function.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003443 __ ldr(tos, FieldMemOperand(tos, JSFunction::kLiteralsOffset));
3444 frame_->EmitPush(tos);
3445 frame_->EmitPush(Operand(Smi::FromInt(node->literal_index())));
3446 frame_->EmitPush(Operand(node->constant_elements()));
ager@chromium.org5c838252010-02-19 08:53:10 +00003447 int length = node->values()->length();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003448 if (node->constant_elements()->map() == Heap::fixed_cow_array_map()) {
3449 FastCloneShallowArrayStub stub(
3450 FastCloneShallowArrayStub::COPY_ON_WRITE_ELEMENTS, length);
3451 frame_->CallStub(&stub, 3);
3452 __ IncrementCounter(&Counters::cow_arrays_created_stub, 1, r1, r2);
3453 } else if (node->depth() > 1) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003454 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003455 } else if (length > FastCloneShallowArrayStub::kMaximumClonedLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003456 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003457 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003458 FastCloneShallowArrayStub stub(
3459 FastCloneShallowArrayStub::CLONE_ELEMENTS, length);
ager@chromium.org5c838252010-02-19 08:53:10 +00003460 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003461 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003462 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003463 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003464
3465 // Generate code to set the elements in the array that are not
3466 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467 for (int i = 0; i < node->values()->length(); i++) {
3468 Expression* value = node->values()->at(i);
3469
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003470 // If value is a literal the property value is already set in the
3471 // boilerplate object.
3472 if (value->AsLiteral() != NULL) continue;
3473 // If value is a materialized literal the property value is already set
3474 // in the boilerplate object if it is simple.
3475 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003476
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003477 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003478 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003479 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003480 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003481 frame_->SpillAllButCopyTOSToR1();
3482
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003483 // Get the elements array.
3484 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003486 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003487 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003488 __ str(r0, FieldMemOperand(r1, offset));
3489
3490 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003491 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003493 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494}
3495
3496
ager@chromium.org32912102009-01-16 10:38:43 +00003497void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003498#ifdef DEBUG
3499 int original_height = frame_->height();
3500#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003501 // Call runtime routine to allocate the catch extension object and
3502 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003503 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003504 Load(node->key());
3505 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003506 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3507 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003508 ASSERT_EQ(original_height + 1, frame_->height());
3509}
3510
3511
3512void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3513#ifdef DEBUG
3514 int original_height = frame_->height();
3515#endif
3516 Comment cmnt(masm(), "[ Variable Assignment");
3517 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3518 ASSERT(var != NULL);
3519 Slot* slot = var->slot();
3520 ASSERT(slot != NULL);
3521
3522 // Evaluate the right-hand side.
3523 if (node->is_compound()) {
3524 // For a compound assignment the right-hand side is a binary operation
3525 // between the current property value and the actual right-hand side.
3526 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3527
3528 // Perform the binary operation.
3529 Literal* literal = node->value()->AsLiteral();
3530 bool overwrite_value =
3531 (node->value()->AsBinaryOperation() != NULL &&
3532 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3533 if (literal != NULL && literal->handle()->IsSmi()) {
3534 SmiOperation(node->binary_op(),
3535 literal->handle(),
3536 false,
3537 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3538 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003539 GenerateInlineSmi inline_smi =
3540 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3541 if (literal != NULL) {
3542 ASSERT(!literal->handle()->IsSmi());
3543 inline_smi = DONT_GENERATE_INLINE_SMI;
3544 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003545 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003546 GenericBinaryOperation(node->binary_op(),
3547 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3548 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003549 }
3550 } else {
3551 Load(node->value());
3552 }
3553
3554 // Perform the assignment.
3555 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3556 CodeForSourcePosition(node->position());
3557 StoreToSlot(slot,
3558 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3559 }
3560 ASSERT_EQ(original_height + 1, frame_->height());
3561}
3562
3563
3564void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3565#ifdef DEBUG
3566 int original_height = frame_->height();
3567#endif
3568 Comment cmnt(masm(), "[ Named Property Assignment");
3569 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3570 Property* prop = node->target()->AsProperty();
3571 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3572
3573 // Initialize name and evaluate the receiver sub-expression if necessary. If
3574 // the receiver is trivial it is not placed on the stack at this point, but
3575 // loaded whenever actually needed.
3576 Handle<String> name;
3577 bool is_trivial_receiver = false;
3578 if (var != NULL) {
3579 name = var->name();
3580 } else {
3581 Literal* lit = prop->key()->AsLiteral();
3582 ASSERT_NOT_NULL(lit);
3583 name = Handle<String>::cast(lit->handle());
3584 // Do not materialize the receiver on the frame if it is trivial.
3585 is_trivial_receiver = prop->obj()->IsTrivial();
3586 if (!is_trivial_receiver) Load(prop->obj());
3587 }
3588
3589 // Change to slow case in the beginning of an initialization block to
3590 // avoid the quadratic behavior of repeatedly adding fast properties.
3591 if (node->starts_initialization_block()) {
3592 // Initialization block consists of assignments of the form expr.x = ..., so
3593 // this will never be an assignment to a variable, so there must be a
3594 // receiver object.
3595 ASSERT_EQ(NULL, var);
3596 if (is_trivial_receiver) {
3597 Load(prop->obj());
3598 } else {
3599 frame_->Dup();
3600 }
3601 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3602 }
3603
3604 // Change to fast case at the end of an initialization block. To prepare for
3605 // that add an extra copy of the receiver to the frame, so that it can be
3606 // converted back to fast case after the assignment.
3607 if (node->ends_initialization_block() && !is_trivial_receiver) {
3608 frame_->Dup();
3609 }
3610
3611 // Stack layout:
3612 // [tos] : receiver (only materialized if non-trivial)
3613 // [tos+1] : receiver if at the end of an initialization block
3614
3615 // Evaluate the right-hand side.
3616 if (node->is_compound()) {
3617 // For a compound assignment the right-hand side is a binary operation
3618 // between the current property value and the actual right-hand side.
3619 if (is_trivial_receiver) {
3620 Load(prop->obj());
3621 } else if (var != NULL) {
3622 LoadGlobal();
3623 } else {
3624 frame_->Dup();
3625 }
3626 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003627
3628 // Perform the binary operation.
3629 Literal* literal = node->value()->AsLiteral();
3630 bool overwrite_value =
3631 (node->value()->AsBinaryOperation() != NULL &&
3632 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3633 if (literal != NULL && literal->handle()->IsSmi()) {
3634 SmiOperation(node->binary_op(),
3635 literal->handle(),
3636 false,
3637 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3638 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003639 GenerateInlineSmi inline_smi =
3640 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3641 if (literal != NULL) {
3642 ASSERT(!literal->handle()->IsSmi());
3643 inline_smi = DONT_GENERATE_INLINE_SMI;
3644 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003645 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003646 GenericBinaryOperation(node->binary_op(),
3647 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3648 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003649 }
3650 } else {
3651 // For non-compound assignment just load the right-hand side.
3652 Load(node->value());
3653 }
3654
3655 // Stack layout:
3656 // [tos] : value
3657 // [tos+1] : receiver (only materialized if non-trivial)
3658 // [tos+2] : receiver if at the end of an initialization block
3659
3660 // Perform the assignment. It is safe to ignore constants here.
3661 ASSERT(var == NULL || var->mode() != Variable::CONST);
3662 ASSERT_NE(Token::INIT_CONST, node->op());
3663 if (is_trivial_receiver) {
3664 // Load the receiver and swap with the value.
3665 Load(prop->obj());
3666 Register t0 = frame_->PopToRegister();
3667 Register t1 = frame_->PopToRegister(t0);
3668 frame_->EmitPush(t0);
3669 frame_->EmitPush(t1);
3670 }
3671 CodeForSourcePosition(node->position());
3672 bool is_contextual = (var != NULL);
3673 EmitNamedStore(name, is_contextual);
3674 frame_->EmitPush(r0);
3675
3676 // Change to fast case at the end of an initialization block.
3677 if (node->ends_initialization_block()) {
3678 ASSERT_EQ(NULL, var);
3679 // The argument to the runtime call is the receiver.
3680 if (is_trivial_receiver) {
3681 Load(prop->obj());
3682 } else {
3683 // A copy of the receiver is below the value of the assignment. Swap
3684 // the receiver and the value of the assignment expression.
3685 Register t0 = frame_->PopToRegister();
3686 Register t1 = frame_->PopToRegister(t0);
3687 frame_->EmitPush(t0);
3688 frame_->EmitPush(t1);
3689 }
3690 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3691 }
3692
3693 // Stack layout:
3694 // [tos] : result
3695
3696 ASSERT_EQ(original_height + 1, frame_->height());
3697}
3698
3699
3700void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3701#ifdef DEBUG
3702 int original_height = frame_->height();
3703#endif
3704 Comment cmnt(masm_, "[ Keyed Property Assignment");
3705 Property* prop = node->target()->AsProperty();
3706 ASSERT_NOT_NULL(prop);
3707
3708 // Evaluate the receiver subexpression.
3709 Load(prop->obj());
3710
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003711 WriteBarrierCharacter wb_info;
3712
ager@chromium.orgac091b72010-05-05 07:34:42 +00003713 // Change to slow case in the beginning of an initialization block to
3714 // avoid the quadratic behavior of repeatedly adding fast properties.
3715 if (node->starts_initialization_block()) {
3716 frame_->Dup();
3717 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3718 }
3719
3720 // Change to fast case at the end of an initialization block. To prepare for
3721 // that add an extra copy of the receiver to the frame, so that it can be
3722 // converted back to fast case after the assignment.
3723 if (node->ends_initialization_block()) {
3724 frame_->Dup();
3725 }
3726
3727 // Evaluate the key subexpression.
3728 Load(prop->key());
3729
3730 // Stack layout:
3731 // [tos] : key
3732 // [tos+1] : receiver
3733 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003734 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003735 // Evaluate the right-hand side.
3736 if (node->is_compound()) {
3737 // For a compound assignment the right-hand side is a binary operation
3738 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003739 // Duplicate receiver and key for loading the current property value.
3740 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003741 EmitKeyedLoad();
3742 frame_->EmitPush(r0);
3743
3744 // Perform the binary operation.
3745 Literal* literal = node->value()->AsLiteral();
3746 bool overwrite_value =
3747 (node->value()->AsBinaryOperation() != NULL &&
3748 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3749 if (literal != NULL && literal->handle()->IsSmi()) {
3750 SmiOperation(node->binary_op(),
3751 literal->handle(),
3752 false,
3753 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3754 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003755 GenerateInlineSmi inline_smi =
3756 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3757 if (literal != NULL) {
3758 ASSERT(!literal->handle()->IsSmi());
3759 inline_smi = DONT_GENERATE_INLINE_SMI;
3760 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003761 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003762 GenericBinaryOperation(node->binary_op(),
3763 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3764 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003765 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003766 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003767 } else {
3768 // For non-compound assignment just load the right-hand side.
3769 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003770 wb_info = node->value()->AsLiteral() != NULL ?
3771 NEVER_NEWSPACE :
3772 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003773 }
3774
3775 // Stack layout:
3776 // [tos] : value
3777 // [tos+1] : key
3778 // [tos+2] : receiver
3779 // [tos+3] : receiver if at the end of an initialization block
3780
3781 // Perform the assignment. It is safe to ignore constants here.
3782 ASSERT(node->op() != Token::INIT_CONST);
3783 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003784 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003785 frame_->EmitPush(r0);
3786
3787 // Stack layout:
3788 // [tos] : result
3789 // [tos+1] : receiver if at the end of an initialization block
3790
3791 // Change to fast case at the end of an initialization block.
3792 if (node->ends_initialization_block()) {
3793 // The argument to the runtime call is the extra copy of the receiver,
3794 // which is below the value of the assignment. Swap the receiver and
3795 // the value of the assignment expression.
3796 Register t0 = frame_->PopToRegister();
3797 Register t1 = frame_->PopToRegister(t0);
3798 frame_->EmitPush(t1);
3799 frame_->EmitPush(t0);
3800 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3801 }
3802
3803 // Stack layout:
3804 // [tos] : result
3805
3806 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003807}
3808
3809
ager@chromium.org7c537e22008-10-16 08:43:32 +00003810void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003811 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003812#ifdef DEBUG
3813 int original_height = frame_->height();
3814#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003816
ager@chromium.orgac091b72010-05-05 07:34:42 +00003817 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3818 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003819
ager@chromium.orgac091b72010-05-05 07:34:42 +00003820 if (var != NULL && !var->is_global()) {
3821 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003822
ager@chromium.orgac091b72010-05-05 07:34:42 +00003823 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3824 (var != NULL && var->is_global())) {
3825 // Properties whose keys are property names and global variables are
3826 // treated as named property references. We do not need to consider
3827 // global 'this' because it is not a valid left-hand side.
3828 EmitNamedPropertyAssignment(node);
3829
3830 } else if (prop != NULL) {
3831 // Other properties (including rewritten parameters for a function that
3832 // uses arguments) are keyed property assignments.
3833 EmitKeyedPropertyAssignment(node);
3834
3835 } else {
3836 // Invalid left-hand side.
3837 Load(node->target());
3838 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3839 // The runtime call doesn't actually return but the code generator will
3840 // still generate code and expects a certain frame height.
3841 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003843 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844}
3845
3846
ager@chromium.org7c537e22008-10-16 08:43:32 +00003847void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003848#ifdef DEBUG
3849 int original_height = frame_->height();
3850#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003851 Comment cmnt(masm_, "[ Throw");
3852
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003853 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003854 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003855 frame_->CallRuntime(Runtime::kThrow, 1);
3856 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003857 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858}
3859
3860
ager@chromium.org7c537e22008-10-16 08:43:32 +00003861void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003862#ifdef DEBUG
3863 int original_height = frame_->height();
3864#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003866
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003867 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003868 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003869 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003870 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871}
3872
3873
ager@chromium.org7c537e22008-10-16 08:43:32 +00003874void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003875#ifdef DEBUG
3876 int original_height = frame_->height();
3877#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878 Comment cmnt(masm_, "[ Call");
3879
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003880 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881 ZoneList<Expression*>* args = node->arguments();
3882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003885 Variable* var = function->AsVariableProxy()->AsVariable();
3886 Property* property = function->AsProperty();
3887
3888 // ------------------------------------------------------------------------
3889 // Fast-case: Use inline caching.
3890 // ---
3891 // According to ECMA-262, section 11.2.3, page 44, the function to call
3892 // must be resolved after the arguments have been evaluated. The IC code
3893 // automatically handles this by loading the arguments before the function
3894 // is resolved in cache misses (this also holds for megamorphic calls).
3895 // ------------------------------------------------------------------------
3896
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003897 if (var != NULL && var->is_possibly_eval()) {
3898 // ----------------------------------
3899 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3900 // ----------------------------------
3901
3902 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3903 // resolve the function we need to call and the receiver of the
3904 // call. Then we call the resolved function using the given
3905 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003906
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003907 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003908 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003909
3910 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003911 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003912
3913 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003914 int arg_count = args->length();
3915 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003916 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003917 }
3918
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003919 VirtualFrame::SpilledScope spilled_scope(frame_);
3920
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003921 // If we know that eval can only be shadowed by eval-introduced
3922 // variables we attempt to load the global eval function directly
3923 // in generated code. If we succeed, there is no need to perform a
3924 // context lookup in the runtime system.
3925 JumpTarget done;
3926 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3927 ASSERT(var->slot()->type() == Slot::LOOKUP);
3928 JumpTarget slow;
3929 // Prepare the stack for the call to
3930 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3931 // function, the first argument to the eval call and the
3932 // receiver.
3933 LoadFromGlobalSlotCheckExtensions(var->slot(),
3934 NOT_INSIDE_TYPEOF,
3935 &slow);
3936 frame_->EmitPush(r0);
3937 if (arg_count > 0) {
3938 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3939 frame_->EmitPush(r1);
3940 } else {
3941 frame_->EmitPush(r2);
3942 }
3943 __ ldr(r1, frame_->Receiver());
3944 frame_->EmitPush(r1);
3945
3946 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3947
3948 done.Jump();
3949 slow.Bind();
3950 }
3951
3952 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3953 // pushing the loaded function, the first argument to the eval
3954 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003955 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3956 frame_->EmitPush(r1);
3957 if (arg_count > 0) {
3958 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3959 frame_->EmitPush(r1);
3960 } else {
3961 frame_->EmitPush(r2);
3962 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003963 __ ldr(r1, frame_->Receiver());
3964 frame_->EmitPush(r1);
3965
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003966 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003967 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003968
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003969 // If we generated fast-case code bind the jump-target where fast
3970 // and slow case merge.
3971 if (done.is_linked()) done.Bind();
3972
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003973 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003974 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003975 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3976
3977 // Call the function.
3978 CodeForSourcePosition(node->position());
3979
3980 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003981 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003982 frame_->CallStub(&call_function, arg_count + 1);
3983
3984 __ ldr(cp, frame_->Context());
3985 // Remove the function from the stack.
3986 frame_->Drop();
3987 frame_->EmitPush(r0);
3988
3989 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990 // ----------------------------------
3991 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3992 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003993 // Pass the global object as the receiver and let the IC stub
3994 // patch the stack to use the global proxy as 'this' in the
3995 // invoked function.
3996 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003997
3998 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003999 int arg_count = args->length();
4000 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004001 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004002 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004004 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00004005 // Setup the name register and call the IC initialization code.
4006 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004007 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4008 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004009 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4011 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004012 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004013 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014
4015 } else if (var != NULL && var->slot() != NULL &&
4016 var->slot()->type() == Slot::LOOKUP) {
4017 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004018 // JavaScript examples:
4019 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004020 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004021 //
4022 // function f() {};
4023 // function g() {
4024 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004025 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004026 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004027 // ----------------------------------
4028
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004029 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004030
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004031 // Generate fast case for loading functions from slots that
4032 // correspond to local/global variables or arguments unless they
4033 // are shadowed by eval-introduced bindings.
4034 EmitDynamicLoadFromSlotFastCase(var->slot(),
4035 NOT_INSIDE_TYPEOF,
4036 &slow,
4037 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004038
4039 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004041 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004042 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004043 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 // r0: slot value; r1: receiver
4045
4046 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004047 frame_->EmitPush(r0); // function
4048 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004049
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004050 // If fast case code has been generated, emit code to push the
4051 // function and receiver and have the slow path jump around this
4052 // code.
4053 if (done.is_linked()) {
4054 JumpTarget call;
4055 call.Jump();
4056 done.Bind();
4057 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004058 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004059 call.Bind();
4060 }
4061
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004062 // Call the function. At this point, everything is spilled but the
4063 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004064 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004065 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066
4067 } else if (property != NULL) {
4068 // Check if the key is a literal string.
4069 Literal* literal = property->key()->AsLiteral();
4070
4071 if (literal != NULL && literal->handle()->IsSymbol()) {
4072 // ------------------------------------------------------------------
4073 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4074 // ------------------------------------------------------------------
4075
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004076 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004078 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4079 name->IsEqualTo(CStrVector("apply")) &&
4080 args->length() == 2 &&
4081 args->at(1)->AsVariableProxy() != NULL &&
4082 args->at(1)->AsVariableProxy()->IsArguments()) {
4083 // Use the optimized Function.prototype.apply that avoids
4084 // allocating lazily allocated arguments objects.
4085 CallApplyLazy(property->obj(),
4086 args->at(0),
4087 args->at(1)->AsVariableProxy(),
4088 node->position());
4089
4090 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004091 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004092 // Load the arguments.
4093 int arg_count = args->length();
4094 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004095 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004096 }
4097
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004098 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004099 // Set the name register and call the IC initialization code.
4100 __ mov(r2, Operand(name));
4101 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4102 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4103 CodeForSourcePosition(node->position());
4104 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4105 __ ldr(cp, frame_->Context());
4106 frame_->EmitPush(r0);
4107 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108
4109 } else {
4110 // -------------------------------------------
4111 // JavaScript example: 'array[index](1, 2, 3)'
4112 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004113 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004114 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004115 Load(property->key());
4116 EmitKeyedLoad();
4117 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004118 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004119 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004120 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004121 // Call the function.
4122 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4123 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004124 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004125 // Load the arguments.
4126 int arg_count = args->length();
4127 for (int i = 0; i < arg_count; i++) {
4128 Load(args->at(i));
4129 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004130
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004131 // Set the name register and call the IC initialization code.
4132 Load(property->key());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004133 frame_->SpillAll();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004134 frame_->EmitPop(r2); // Function name.
4135
4136 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4137 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4138 CodeForSourcePosition(node->position());
4139 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4140 __ ldr(cp, frame_->Context());
4141 frame_->EmitPush(r0);
4142 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004143 }
4144
4145 } else {
4146 // ----------------------------------
4147 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4148 // ----------------------------------
4149
4150 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004151 Load(function);
4152
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004153 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004154 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004157 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004158 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004159 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004160 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004161}
4162
4163
ager@chromium.org7c537e22008-10-16 08:43:32 +00004164void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004165#ifdef DEBUG
4166 int original_height = frame_->height();
4167#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004168 Comment cmnt(masm_, "[ CallNew");
4169
4170 // According to ECMA-262, section 11.2.2, page 44, the function
4171 // expression in new calls must be evaluated before the
4172 // arguments. This is different from ordinary calls, where the
4173 // actual function to call is resolved after the arguments have been
4174 // evaluated.
4175
4176 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004177 // receiver. There is no need to use the global proxy here because
4178 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004179 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004180 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004181
4182 // Push the arguments ("left-to-right") on the stack.
4183 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 int arg_count = args->length();
4185 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004186 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004187 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004189 VirtualFrame::SpilledScope spilled_scope(frame_);
4190
mads.s.ager31e71382008-08-13 09:32:07 +00004191 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004192 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004193 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004194 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196 // Call the construct call builtin that handles allocation and
4197 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004198 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004199 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004200 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004201
4202 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004203 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004204 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205}
4206
4207
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004208void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004209 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004210 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004211
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004212 // Load the object into register.
4213 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004214 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004215 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004216
4217 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004218 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004219 null.Branch(eq);
4220
4221 // Check that the object is a JS object but take special care of JS
4222 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004223 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004224 null.Branch(lt);
4225
4226 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4227 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4228 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004229 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4230 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004231 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004232 function.Branch(eq);
4233
4234 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004235 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4236 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004237 non_function_constructor.Branch(ne);
4238
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004239 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004240 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004241 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4242 __ ldr(tos,
4243 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4244 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004245 leave.Jump();
4246
4247 // Functions have class 'Function'.
4248 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004249 __ mov(tos, Operand(Factory::function_class_symbol()));
4250 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004251 leave.Jump();
4252
4253 // Objects with a non-function constructor have class 'Object'.
4254 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004255 __ mov(tos, Operand(Factory::Object_symbol()));
4256 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004257 leave.Jump();
4258
4259 // Non-JS objects have class null.
4260 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004261 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4262 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004263
4264 // All done.
4265 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004266}
4267
4268
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004270 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004271 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004272
4273 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004274 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004275 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004276 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004277 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004278 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004279 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004280 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004281 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004282 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004283 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004284 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004285 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004286}
4287
4288
ager@chromium.org7c537e22008-10-16 08:43:32 +00004289void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004290 Register scratch1 = VirtualFrame::scratch0();
4291 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004292 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004293
4294 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004295 Load(args->at(0)); // Load the object.
4296 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004297 Register value = frame_->PopToRegister();
4298 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004300 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004302 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004303 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004304 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004305 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004306 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004307 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004308 __ RecordWrite(object,
4309 Operand(JSValue::kValueOffset - kHeapObjectTag),
4310 scratch1,
4311 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004312 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004313 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004314 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004315}
4316
4317
ager@chromium.org7c537e22008-10-16 08:43:32 +00004318void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004319 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004320 Load(args->at(0));
4321 Register reg = frame_->PopToRegister();
4322 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004323 cc_reg_ = eq;
4324}
4325
4326
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004327void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4328 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4329 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004330#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004331 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004332 Load(args->at(1));
4333 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004334 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004335 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004336#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004337 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004338}
4339
4340
ager@chromium.org7c537e22008-10-16 08:43:32 +00004341void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004342 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004343 Load(args->at(0));
4344 Register reg = frame_->PopToRegister();
4345 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004346 cc_reg_ = eq;
4347}
4348
4349
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004350// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004351void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4352 ASSERT(args->length() == 2);
4353 Load(args->at(0));
4354 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004355
4356 if (!CpuFeatures::IsSupported(VFP3)) {
4357 frame_->CallRuntime(Runtime::kMath_pow, 2);
4358 frame_->EmitPush(r0);
4359 } else {
4360 CpuFeatures::Scope scope(VFP3);
4361 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004362 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004363
4364 Register scratch1 = VirtualFrame::scratch0();
4365 Register scratch2 = VirtualFrame::scratch1();
4366
4367 // Get base and exponent to registers.
4368 Register exponent = frame_->PopToRegister();
4369 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004370 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004371
4372 // Set the frame for the runtime jump target. The code below jumps to the
4373 // jump target label so the frame needs to be established before that.
4374 ASSERT(runtime.entry_frame() == NULL);
4375 runtime.set_entry_frame(frame_);
4376
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004377 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4378 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004379
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004380 heap_number_map = r6;
4381 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4382
4383 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4384 // d1.
4385 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4386 __ b(&powi);
4387
4388 __ bind(&base_nonsmi);
4389 // Exponent is smi and base is non smi. Get the double value from the base
4390 // into vfp register d1.
4391 __ ObjectToDoubleVFPRegister(base, d1,
4392 scratch1, scratch2, heap_number_map, s0,
4393 runtime.entry_label());
4394
4395 __ bind(&powi);
4396
4397 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004398 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004399
4400 // Get the absolute untagged value of the exponent and use that for the
4401 // calculation.
4402 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4403 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4404 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4405
4406 // Run through all the bits in the exponent. The result is calculated in d0
4407 // and d1 holds base^(bit^2).
4408 Label more_bits;
4409 __ bind(&more_bits);
4410 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4411 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4412 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4413 __ b(ne, &more_bits);
4414
4415 // If exponent is positive we are done.
4416 __ cmp(exponent, Operand(0));
4417 __ b(ge, &allocate_return);
4418
4419 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4420 // case). However if d0 has reached infinity this will not provide the
4421 // correct result, so call runtime if that is the case.
4422 __ mov(scratch2, Operand(0x7FF00000));
4423 __ mov(scratch1, Operand(0));
4424 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4425 __ vcmp(d0, d1);
4426 __ vmrs(pc);
4427 runtime.Branch(eq); // d0 reached infinity.
4428 __ vdiv(d0, d2, d0);
4429 __ b(&allocate_return);
4430
4431 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004432 // Special handling of raising to the power of -0.5 and 0.5. First check
4433 // that the value is a heap number and that the lower bits (which for both
4434 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004435 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004436 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4437 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4438 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4439 __ cmp(scratch1, heap_number_map);
4440 runtime.Branch(ne);
4441 __ tst(scratch2, scratch2);
4442 runtime.Branch(ne);
4443
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004444 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004445 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4446
4447 // Compare exponent with -0.5.
4448 __ cmp(scratch1, Operand(0xbfe00000));
4449 __ b(ne, &not_minus_half);
4450
4451 // Get the double value from the base into vfp register d0.
4452 __ ObjectToDoubleVFPRegister(base, d0,
4453 scratch1, scratch2, heap_number_map, s0,
4454 runtime.entry_label(),
4455 AVOID_NANS_AND_INFINITIES);
4456
4457 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004458 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004459
4460 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4461 __ vdiv(d0, d2, d0);
4462 __ vsqrt(d0, d0);
4463
4464 __ b(&allocate_return);
4465
4466 __ bind(&not_minus_half);
4467 // Compare exponent with 0.5.
4468 __ cmp(scratch1, Operand(0x3fe00000));
4469 runtime.Branch(ne);
4470
4471 // Get the double value from the base into vfp register d0.
4472 __ ObjectToDoubleVFPRegister(base, d0,
4473 scratch1, scratch2, heap_number_map, s0,
4474 runtime.entry_label(),
4475 AVOID_NANS_AND_INFINITIES);
4476 __ vsqrt(d0, d0);
4477
4478 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004479 Register scratch3 = r5;
4480 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4481 heap_number_map, runtime.entry_label());
4482 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004483 done.Jump();
4484
4485 runtime.Bind();
4486
4487 // Push back the arguments again for the runtime call.
4488 frame_->EmitPush(base);
4489 frame_->EmitPush(exponent);
4490 frame_->CallRuntime(Runtime::kMath_pow, 2);
4491 __ Move(base, r0);
4492
4493 done.Bind();
4494 frame_->EmitPush(base);
4495 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004496}
4497
4498
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004499// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004500void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4501 ASSERT(args->length() == 1);
4502 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004503
4504 if (!CpuFeatures::IsSupported(VFP3)) {
4505 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4506 frame_->EmitPush(r0);
4507 } else {
4508 CpuFeatures::Scope scope(VFP3);
4509 JumpTarget runtime, done;
4510
4511 Register scratch1 = VirtualFrame::scratch0();
4512 Register scratch2 = VirtualFrame::scratch1();
4513
4514 // Get the value from the frame.
4515 Register tos = frame_->PopToRegister();
4516
4517 // Set the frame for the runtime jump target. The code below jumps to the
4518 // jump target label so the frame needs to be established before that.
4519 ASSERT(runtime.entry_frame() == NULL);
4520 runtime.set_entry_frame(frame_);
4521
4522 Register heap_number_map = r6;
4523 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4524
4525 // Get the double value from the heap number into vfp register d0.
4526 __ ObjectToDoubleVFPRegister(tos, d0,
4527 scratch1, scratch2, heap_number_map, s0,
4528 runtime.entry_label());
4529
4530 // Calculate the square root of d0 and place result in a heap number object.
4531 __ vsqrt(d0, d0);
4532 __ AllocateHeapNumberWithValue(
4533 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4534 done.Jump();
4535
4536 runtime.Bind();
4537 // Push back the argument again for the runtime call.
4538 frame_->EmitPush(tos);
4539 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4540 __ Move(tos, r0);
4541
4542 done.Bind();
4543 frame_->EmitPush(tos);
4544 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004545}
4546
4547
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004548class DeferredStringCharCodeAt : public DeferredCode {
4549 public:
4550 DeferredStringCharCodeAt(Register object,
4551 Register index,
4552 Register scratch,
4553 Register result)
4554 : result_(result),
4555 char_code_at_generator_(object,
4556 index,
4557 scratch,
4558 result,
4559 &need_conversion_,
4560 &need_conversion_,
4561 &index_out_of_range_,
4562 STRING_INDEX_IS_NUMBER) {}
4563
4564 StringCharCodeAtGenerator* fast_case_generator() {
4565 return &char_code_at_generator_;
4566 }
4567
4568 virtual void Generate() {
4569 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4570 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4571
4572 __ bind(&need_conversion_);
4573 // Move the undefined value into the result register, which will
4574 // trigger conversion.
4575 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4576 __ jmp(exit_label());
4577
4578 __ bind(&index_out_of_range_);
4579 // When the index is out of range, the spec requires us to return
4580 // NaN.
4581 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4582 __ jmp(exit_label());
4583 }
4584
4585 private:
4586 Register result_;
4587
4588 Label need_conversion_;
4589 Label index_out_of_range_;
4590
4591 StringCharCodeAtGenerator char_code_at_generator_;
4592};
4593
4594
4595// This generates code that performs a String.prototype.charCodeAt() call
4596// or returns a smi in order to trigger conversion.
4597void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004598 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004599 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004600
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004601 Load(args->at(0));
4602 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004603
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004604 Register index = frame_->PopToRegister();
4605 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004606
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004607 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004608 Register scratch = VirtualFrame::scratch0();
4609 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004610
4611 DeferredStringCharCodeAt* deferred =
4612 new DeferredStringCharCodeAt(object,
4613 index,
4614 scratch,
4615 result);
4616 deferred->fast_case_generator()->GenerateFast(masm_);
4617 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004618 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004619}
4620
4621
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004622class DeferredStringCharFromCode : public DeferredCode {
4623 public:
4624 DeferredStringCharFromCode(Register code,
4625 Register result)
4626 : char_from_code_generator_(code, result) {}
4627
4628 StringCharFromCodeGenerator* fast_case_generator() {
4629 return &char_from_code_generator_;
4630 }
4631
4632 virtual void Generate() {
4633 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4634 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4635 }
4636
4637 private:
4638 StringCharFromCodeGenerator char_from_code_generator_;
4639};
4640
4641
4642// Generates code for creating a one-char string from a char code.
4643void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004644 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004645 ASSERT(args->length() == 1);
4646
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004647 Load(args->at(0));
4648
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004649 Register result = frame_->GetTOSRegister();
4650 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004651
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004652 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4653 code, result);
4654 deferred->fast_case_generator()->GenerateFast(masm_);
4655 deferred->BindExit();
4656 frame_->EmitPush(result);
4657}
4658
4659
4660class DeferredStringCharAt : public DeferredCode {
4661 public:
4662 DeferredStringCharAt(Register object,
4663 Register index,
4664 Register scratch1,
4665 Register scratch2,
4666 Register result)
4667 : result_(result),
4668 char_at_generator_(object,
4669 index,
4670 scratch1,
4671 scratch2,
4672 result,
4673 &need_conversion_,
4674 &need_conversion_,
4675 &index_out_of_range_,
4676 STRING_INDEX_IS_NUMBER) {}
4677
4678 StringCharAtGenerator* fast_case_generator() {
4679 return &char_at_generator_;
4680 }
4681
4682 virtual void Generate() {
4683 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4684 char_at_generator_.GenerateSlow(masm(), call_helper);
4685
4686 __ bind(&need_conversion_);
4687 // Move smi zero into the result register, which will trigger
4688 // conversion.
4689 __ mov(result_, Operand(Smi::FromInt(0)));
4690 __ jmp(exit_label());
4691
4692 __ bind(&index_out_of_range_);
4693 // When the index is out of range, the spec requires us to return
4694 // the empty string.
4695 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4696 __ jmp(exit_label());
4697 }
4698
4699 private:
4700 Register result_;
4701
4702 Label need_conversion_;
4703 Label index_out_of_range_;
4704
4705 StringCharAtGenerator char_at_generator_;
4706};
4707
4708
4709// This generates code that performs a String.prototype.charAt() call
4710// or returns a smi in order to trigger conversion.
4711void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004712 Comment(masm_, "[ GenerateStringCharAt");
4713 ASSERT(args->length() == 2);
4714
4715 Load(args->at(0));
4716 Load(args->at(1));
4717
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004718 Register index = frame_->PopToRegister();
4719 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004720
4721 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004722 Register scratch1 = VirtualFrame::scratch0();
4723 Register scratch2 = VirtualFrame::scratch1();
4724 // Use r6 without notifying the virtual frame.
4725 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004726
4727 DeferredStringCharAt* deferred =
4728 new DeferredStringCharAt(object,
4729 index,
4730 scratch1,
4731 scratch2,
4732 result);
4733 deferred->fast_case_generator()->GenerateFast(masm_);
4734 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004735 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004736}
4737
4738
ager@chromium.org7c537e22008-10-16 08:43:32 +00004739void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004740 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004741 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004742 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004743 // We need the CC bits to come out as not_equal in the case where the
4744 // object is a smi. This can't be done with the usual test opcode so
4745 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004746 Register possible_array = frame_->PopToRegister();
4747 Register scratch = VirtualFrame::scratch0();
4748 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4749 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004750 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004751 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004752 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004753 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004754 cc_reg_ = eq;
4755}
4756
4757
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004758void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004759 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004760 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004761 JumpTarget answer;
4762 // We need the CC bits to come out as not_equal in the case where the
4763 // object is a smi. This can't be done with the usual test opcode so
4764 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004765 Register possible_regexp = frame_->PopToRegister();
4766 Register scratch = VirtualFrame::scratch0();
4767 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4768 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004769 answer.Branch(ne);
4770 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004771 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004772 answer.Bind();
4773 cc_reg_ = eq;
4774}
4775
4776
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004777void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4778 // This generates a fast version of:
4779 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004780 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004781 Load(args->at(0));
4782 Register possible_object = frame_->PopToRegister();
4783 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004784 false_target()->Branch(eq);
4785
4786 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004787 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004788 true_target()->Branch(eq);
4789
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004790 Register map_reg = VirtualFrame::scratch0();
4791 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004792 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004793 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4794 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004795 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004796
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004797 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4798 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004799 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004800 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004801 cc_reg_ = le;
4802}
4803
4804
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004805void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4806 // This generates a fast version of:
4807 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4808 // typeof(arg) == function).
4809 // It includes undetectable objects (as opposed to IsObject).
4810 ASSERT(args->length() == 1);
4811 Load(args->at(0));
4812 Register value = frame_->PopToRegister();
4813 __ tst(value, Operand(kSmiTagMask));
4814 false_target()->Branch(eq);
4815 // Check that this is an object.
4816 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4817 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4818 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4819 cc_reg_ = ge;
4820}
4821
4822
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004823// Deferred code to check whether the String JavaScript object is safe for using
4824// default value of. This code is called after the bit caching this information
4825// in the map has been checked with the map for the object in the map_result_
4826// register. On return the register map_result_ contains 1 for true and 0 for
4827// false.
4828class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
4829 public:
4830 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
4831 Register map_result,
4832 Register scratch1,
4833 Register scratch2)
4834 : object_(object),
4835 map_result_(map_result),
4836 scratch1_(scratch1),
4837 scratch2_(scratch2) { }
4838
4839 virtual void Generate() {
4840 Label false_result;
4841
4842 // Check that map is loaded as expected.
4843 if (FLAG_debug_code) {
4844 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
4845 __ cmp(map_result_, ip);
4846 __ Assert(eq, "Map not in expected register");
4847 }
4848
4849 // Check for fast case object. Generate false result for slow case object.
4850 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
4851 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4852 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
4853 __ cmp(scratch1_, ip);
4854 __ b(eq, &false_result);
4855
4856 // Look for valueOf symbol in the descriptor array, and indicate false if
4857 // found. The type is not checked, so if it is a transition it is a false
4858 // negative.
4859 __ ldr(map_result_,
4860 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
4861 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
4862 // map_result_: descriptor array
4863 // scratch2_: length of descriptor array
4864 // Calculate the end of the descriptor array.
4865 STATIC_ASSERT(kSmiTag == 0);
4866 STATIC_ASSERT(kSmiTagSize == 1);
4867 STATIC_ASSERT(kPointerSize == 4);
4868 __ add(scratch1_,
4869 map_result_,
4870 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4871 __ add(scratch1_,
4872 scratch1_,
4873 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
4874
4875 // Calculate location of the first key name.
4876 __ add(map_result_,
4877 map_result_,
4878 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
4879 DescriptorArray::kFirstIndex * kPointerSize));
4880 // Loop through all the keys in the descriptor array. If one of these is the
4881 // symbol valueOf the result is false.
4882 Label entry, loop;
4883 // The use of ip to store the valueOf symbol asumes that it is not otherwise
4884 // used in the loop below.
4885 __ mov(ip, Operand(Factory::value_of_symbol()));
4886 __ jmp(&entry);
4887 __ bind(&loop);
4888 __ ldr(scratch2_, MemOperand(map_result_, 0));
4889 __ cmp(scratch2_, ip);
4890 __ b(eq, &false_result);
4891 __ add(map_result_, map_result_, Operand(kPointerSize));
4892 __ bind(&entry);
4893 __ cmp(map_result_, Operand(scratch1_));
4894 __ b(ne, &loop);
4895
4896 // Reload map as register map_result_ was used as temporary above.
4897 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4898
4899 // If a valueOf property is not found on the object check that it's
4900 // prototype is the un-modified String prototype. If not result is false.
4901 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
4902 __ tst(scratch1_, Operand(kSmiTagMask));
4903 __ b(eq, &false_result);
4904 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4905 __ ldr(scratch2_,
4906 CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
4907 __ ldr(scratch2_,
4908 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
4909 __ ldr(scratch2_,
4910 CodeGenerator::ContextOperand(
4911 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
4912 __ cmp(scratch1_, scratch2_);
4913 __ b(ne, &false_result);
4914
4915 // Set the bit in the map to indicate that it has been checked safe for
4916 // default valueOf and set true result.
4917 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4918 __ orr(scratch1_,
4919 scratch1_,
4920 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4921 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4922 __ mov(map_result_, Operand(1));
4923 __ jmp(exit_label());
4924 __ bind(&false_result);
4925 // Set false result.
4926 __ mov(map_result_, Operand(0));
4927 }
4928
4929 private:
4930 Register object_;
4931 Register map_result_;
4932 Register scratch1_;
4933 Register scratch2_;
4934};
4935
4936
4937void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
4938 ZoneList<Expression*>* args) {
4939 ASSERT(args->length() == 1);
4940 Load(args->at(0));
4941 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
4942 if (FLAG_debug_code) {
4943 __ AbortIfSmi(obj);
4944 }
4945
4946 // Check whether this map has already been checked to be safe for default
4947 // valueOf.
4948 Register map_result = VirtualFrame::scratch0();
4949 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
4950 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
4951 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4952 true_target()->Branch(ne);
4953
4954 // We need an additional two scratch registers for the deferred code.
4955 Register scratch1 = VirtualFrame::scratch1();
4956 // Use r6 without notifying the virtual frame.
4957 Register scratch2 = r6;
4958
4959 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
4960 new DeferredIsStringWrapperSafeForDefaultValueOf(
4961 obj, map_result, scratch1, scratch2);
4962 deferred->Branch(eq);
4963 deferred->BindExit();
4964 __ tst(map_result, Operand(map_result));
4965 cc_reg_ = ne;
4966}
4967
4968
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004969void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4970 // This generates a fast version of:
4971 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004972 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004973 Load(args->at(0));
4974 Register possible_function = frame_->PopToRegister();
4975 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004976 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004977 Register map_reg = VirtualFrame::scratch0();
4978 Register scratch = VirtualFrame::scratch1();
4979 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004980 cc_reg_ = eq;
4981}
4982
4983
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004984void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004985 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004986 Load(args->at(0));
4987 Register possible_undetectable = frame_->PopToRegister();
4988 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004989 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004990 Register scratch = VirtualFrame::scratch0();
4991 __ ldr(scratch,
4992 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4993 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4994 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004995 cc_reg_ = ne;
4996}
4997
4998
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004999void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005000 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005001
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005002 Register scratch0 = VirtualFrame::scratch0();
5003 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005004 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005005 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005006
5007 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005008 __ ldr(scratch1,
5009 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5010 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5011 __ ldr(scratch0,
5012 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005013
5014 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005015 __ ldr(scratch1,
5016 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5017 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005018 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005019}
5020
5021
ager@chromium.org7c537e22008-10-16 08:43:32 +00005022void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005023 ASSERT(args->length() == 0);
5024
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005025 Register tos = frame_->GetTOSRegister();
5026 Register scratch0 = VirtualFrame::scratch0();
5027 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005028
lrn@chromium.org25156de2010-04-06 13:10:27 +00005029 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005030 __ ldr(scratch0,
5031 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5032 __ ldr(scratch1,
5033 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5034 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5035
5036 // Get the number of formal parameters.
5037 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005038
5039 // Arguments adaptor case: Read the arguments length from the
5040 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005041 __ ldr(tos,
5042 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5043 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005044
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005045 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005046}
5047
5048
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005049void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005050 ASSERT(args->length() == 1);
5051
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005052 // Satisfy contract with ArgumentsAccessStub:
5053 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005054 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005055 frame_->PopToR1();
5056 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005057 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005058
5059 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005060 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005061 frame_->CallStub(&stub, 0);
5062 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005063}
5064
5065
ager@chromium.org357bf652010-04-12 11:30:10 +00005066void CodeGenerator::GenerateRandomHeapNumber(
5067 ZoneList<Expression*>* args) {
5068 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005069 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005070
5071 Label slow_allocate_heapnumber;
5072 Label heapnumber_allocated;
5073
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005074 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5075 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005076 __ jmp(&heapnumber_allocated);
5077
5078 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005079 // Allocate a heap number.
5080 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005081 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005082
5083 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005084
5085 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5086 // by computing:
5087 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5088 if (CpuFeatures::IsSupported(VFP3)) {
5089 __ PrepareCallCFunction(0, r1);
5090 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5091
5092 CpuFeatures::Scope scope(VFP3);
5093 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5094 // Create this constant using mov/orr to avoid PC relative load.
5095 __ mov(r1, Operand(0x41000000));
5096 __ orr(r1, r1, Operand(0x300000));
5097 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5098 __ vmov(d7, r0, r1);
5099 // Move 0x4130000000000000 to VFP.
5100 __ mov(r0, Operand(0));
5101 __ vmov(d8, r0, r1);
5102 // Subtract and store the result in the heap number.
5103 __ vsub(d7, d7, d8);
5104 __ sub(r0, r4, Operand(kHeapObjectTag));
5105 __ vstr(d7, r0, HeapNumber::kValueOffset);
5106 frame_->EmitPush(r4);
5107 } else {
5108 __ mov(r0, Operand(r4));
5109 __ PrepareCallCFunction(1, r1);
5110 __ CallCFunction(
5111 ExternalReference::fill_heap_number_with_random_function(), 1);
5112 frame_->EmitPush(r0);
5113 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005114}
5115
5116
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005117void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5118 ASSERT_EQ(2, args->length());
5119
5120 Load(args->at(0));
5121 Load(args->at(1));
5122
ager@chromium.org5c838252010-02-19 08:53:10 +00005123 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005124 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005125 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005126 frame_->EmitPush(r0);
5127}
5128
5129
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005130void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5131 ASSERT_EQ(3, args->length());
5132
5133 Load(args->at(0));
5134 Load(args->at(1));
5135 Load(args->at(2));
5136
ager@chromium.org5c838252010-02-19 08:53:10 +00005137 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005138 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005139 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005140 frame_->EmitPush(r0);
5141}
5142
5143
5144void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5145 ASSERT_EQ(2, args->length());
5146
5147 Load(args->at(0));
5148 Load(args->at(1));
5149
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005150 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005151 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005152 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005153 frame_->EmitPush(r0);
5154}
5155
5156
5157void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5158 ASSERT_EQ(4, args->length());
5159
5160 Load(args->at(0));
5161 Load(args->at(1));
5162 Load(args->at(2));
5163 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005164 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005165 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005166 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005167 frame_->EmitPush(r0);
5168}
5169
5170
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005171void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5172 // No stub. This code only occurs a few times in regexp.js.
5173 const int kMaxInlineLength = 100;
5174 ASSERT_EQ(3, args->length());
5175 Load(args->at(0)); // Size of array, smi.
5176 Load(args->at(1)); // "index" property value.
5177 Load(args->at(2)); // "input" property value.
5178 {
5179 VirtualFrame::SpilledScope spilled_scope(frame_);
5180 Label slowcase;
5181 Label done;
5182 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5183 STATIC_ASSERT(kSmiTag == 0);
5184 STATIC_ASSERT(kSmiTagSize == 1);
5185 __ tst(r1, Operand(kSmiTagMask));
5186 __ b(ne, &slowcase);
5187 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5188 __ b(hi, &slowcase);
5189 // Smi-tagging is equivalent to multiplying by 2.
5190 // Allocate RegExpResult followed by FixedArray with size in ebx.
5191 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5192 // Elements: [Map][Length][..elements..]
5193 // Size of JSArray with two in-object properties and the header of a
5194 // FixedArray.
5195 int objects_size =
5196 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5197 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5198 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005199 __ AllocateInNewSpace(
5200 r2, // In: Size, in words.
5201 r0, // Out: Start of allocation (tagged).
5202 r3, // Scratch register.
5203 r4, // Scratch register.
5204 &slowcase,
5205 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005206 // r0: Start of allocated area, object-tagged.
5207 // r1: Number of elements in array, as smi.
5208 // r5: Number of elements, untagged.
5209
5210 // Set JSArray map to global.regexp_result_map().
5211 // Set empty properties FixedArray.
5212 // Set elements to point to FixedArray allocated right after the JSArray.
5213 // Interleave operations for better latency.
5214 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5215 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5216 __ mov(r4, Operand(Factory::empty_fixed_array()));
5217 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5218 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5219 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5220 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5221 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5222
5223 // Set input, index and length fields from arguments.
5224 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5225 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5226 __ add(sp, sp, Operand(kPointerSize));
5227 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5228 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5229
5230 // Fill out the elements FixedArray.
5231 // r0: JSArray, tagged.
5232 // r3: FixedArray, tagged.
5233 // r5: Number of elements in array, untagged.
5234
5235 // Set map.
5236 __ mov(r2, Operand(Factory::fixed_array_map()));
5237 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5238 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005239 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5240 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005241 // Fill contents of fixed-array with the-hole.
5242 __ mov(r2, Operand(Factory::the_hole_value()));
5243 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5244 // Fill fixed array elements with hole.
5245 // r0: JSArray, tagged.
5246 // r2: the hole.
5247 // r3: Start of elements in FixedArray.
5248 // r5: Number of elements to fill.
5249 Label loop;
5250 __ tst(r5, Operand(r5));
5251 __ bind(&loop);
5252 __ b(le, &done); // Jump if r1 is negative or zero.
5253 __ sub(r5, r5, Operand(1), SetCC);
5254 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5255 __ jmp(&loop);
5256
5257 __ bind(&slowcase);
5258 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5259
5260 __ bind(&done);
5261 }
5262 frame_->Forget(3);
5263 frame_->EmitPush(r0);
5264}
5265
5266
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005267class DeferredSearchCache: public DeferredCode {
5268 public:
5269 DeferredSearchCache(Register dst, Register cache, Register key)
5270 : dst_(dst), cache_(cache), key_(key) {
5271 set_comment("[ DeferredSearchCache");
5272 }
5273
5274 virtual void Generate();
5275
5276 private:
5277 Register dst_, cache_, key_;
5278};
5279
5280
5281void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005282 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005283 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005284 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005285}
5286
5287
5288void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5289 ASSERT_EQ(2, args->length());
5290
5291 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5292 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5293
5294 Handle<FixedArray> jsfunction_result_caches(
5295 Top::global_context()->jsfunction_result_caches());
5296 if (jsfunction_result_caches->length() <= cache_id) {
5297 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005298 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005299 return;
5300 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005301
5302 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005303
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005304 frame_->PopToR1();
5305 frame_->SpillAll();
5306 Register key = r1; // Just poped to r1
5307 Register result = r0; // Free, as frame has just been spilled.
5308 Register scratch1 = VirtualFrame::scratch0();
5309 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005310
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005311 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5312 __ ldr(scratch1,
5313 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5314 __ ldr(scratch1,
5315 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5316 __ ldr(scratch1,
5317 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005318
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005319 DeferredSearchCache* deferred =
5320 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005321
5322 const int kFingerOffset =
5323 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005324 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005325 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5326 // result now holds finger offset as a smi.
5327 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5328 // scratch2 now points to the start of fixed array elements.
5329 __ ldr(result,
5330 MemOperand(
5331 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5332 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5333 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005334 deferred->Branch(ne);
5335
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005336 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005337
5338 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005339 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005340}
5341
5342
ager@chromium.org5c838252010-02-19 08:53:10 +00005343void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5344 ASSERT_EQ(args->length(), 1);
5345
5346 // Load the argument on the stack and jump to the runtime.
5347 Load(args->at(0));
5348
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005349 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005350 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005351 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005352 frame_->EmitPush(r0);
5353}
5354
5355
ager@chromium.orgac091b72010-05-05 07:34:42 +00005356class DeferredSwapElements: public DeferredCode {
5357 public:
5358 DeferredSwapElements(Register object, Register index1, Register index2)
5359 : object_(object), index1_(index1), index2_(index2) {
5360 set_comment("[ DeferredSwapElements");
5361 }
5362
5363 virtual void Generate();
5364
5365 private:
5366 Register object_, index1_, index2_;
5367};
5368
5369
5370void DeferredSwapElements::Generate() {
5371 __ push(object_);
5372 __ push(index1_);
5373 __ push(index2_);
5374 __ CallRuntime(Runtime::kSwapElements, 3);
5375}
5376
5377
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005378void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5379 Comment cmnt(masm_, "[ GenerateSwapElements");
5380
5381 ASSERT_EQ(3, args->length());
5382
5383 Load(args->at(0));
5384 Load(args->at(1));
5385 Load(args->at(2));
5386
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005387 VirtualFrame::SpilledScope spilled_scope(frame_);
5388
ager@chromium.orgac091b72010-05-05 07:34:42 +00005389 Register index2 = r2;
5390 Register index1 = r1;
5391 Register object = r0;
5392 Register tmp1 = r3;
5393 Register tmp2 = r4;
5394
5395 frame_->EmitPop(index2);
5396 frame_->EmitPop(index1);
5397 frame_->EmitPop(object);
5398
5399 DeferredSwapElements* deferred =
5400 new DeferredSwapElements(object, index1, index2);
5401
5402 // Fetch the map and check if array is in fast case.
5403 // Check that object doesn't require security checks and
5404 // has no indexed interceptor.
5405 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5406 deferred->Branch(lt);
5407 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5408 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5409 deferred->Branch(nz);
5410
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005411 // Check the object's elements are in fast case and writable.
ager@chromium.orgac091b72010-05-05 07:34:42 +00005412 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5413 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5414 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5415 __ cmp(tmp2, ip);
5416 deferred->Branch(ne);
5417
5418 // Smi-tagging is equivalent to multiplying by 2.
5419 STATIC_ASSERT(kSmiTag == 0);
5420 STATIC_ASSERT(kSmiTagSize == 1);
5421
5422 // Check that both indices are smis.
5423 __ mov(tmp2, index1);
5424 __ orr(tmp2, tmp2, index2);
5425 __ tst(tmp2, Operand(kSmiTagMask));
5426 deferred->Branch(nz);
5427
5428 // Bring the offsets into the fixed array in tmp1 into index1 and
5429 // index2.
5430 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5431 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5432 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5433
5434 // Swap elements.
5435 Register tmp3 = object;
5436 object = no_reg;
5437 __ ldr(tmp3, MemOperand(tmp1, index1));
5438 __ ldr(tmp2, MemOperand(tmp1, index2));
5439 __ str(tmp3, MemOperand(tmp1, index2));
5440 __ str(tmp2, MemOperand(tmp1, index1));
5441
5442 Label done;
5443 __ InNewSpace(tmp1, tmp2, eq, &done);
5444 // Possible optimization: do a check that both values are Smis
5445 // (or them and test against Smi mask.)
5446
5447 __ mov(tmp2, tmp1);
5448 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5449 __ CallStub(&recordWrite1);
5450
5451 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5452 __ CallStub(&recordWrite2);
5453
5454 __ bind(&done);
5455
5456 deferred->BindExit();
5457 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5458 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005459}
5460
5461
ager@chromium.org357bf652010-04-12 11:30:10 +00005462void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5463 Comment cmnt(masm_, "[ GenerateCallFunction");
5464
5465 ASSERT(args->length() >= 2);
5466
5467 int n_args = args->length() - 2; // for receiver and function.
5468 Load(args->at(0)); // receiver
5469 for (int i = 0; i < n_args; i++) {
5470 Load(args->at(i + 1));
5471 }
5472 Load(args->at(n_args + 1)); // function
5473 frame_->CallJSFunction(n_args);
5474 frame_->EmitPush(r0);
5475}
5476
5477
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005478void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5479 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005480 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005481 if (CpuFeatures::IsSupported(VFP3)) {
5482 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5483 frame_->SpillAllButCopyTOSToR0();
5484 frame_->CallStub(&stub, 1);
5485 } else {
5486 frame_->CallRuntime(Runtime::kMath_sin, 1);
5487 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005488 frame_->EmitPush(r0);
5489}
5490
5491
5492void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5493 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005494 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005495 if (CpuFeatures::IsSupported(VFP3)) {
5496 TranscendentalCacheStub stub(TranscendentalCache::COS);
5497 frame_->SpillAllButCopyTOSToR0();
5498 frame_->CallStub(&stub, 1);
5499 } else {
5500 frame_->CallRuntime(Runtime::kMath_cos, 1);
5501 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005502 frame_->EmitPush(r0);
5503}
5504
5505
ager@chromium.org7c537e22008-10-16 08:43:32 +00005506void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005507 ASSERT(args->length() == 2);
5508
5509 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005510 Load(args->at(0));
5511 Load(args->at(1));
5512 Register lhs = frame_->PopToRegister();
5513 Register rhs = frame_->PopToRegister(lhs);
5514 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005515 cc_reg_ = eq;
5516}
5517
5518
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005519void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5520 ASSERT(args->length() == 2);
5521
5522 // Load the two objects into registers and perform the comparison.
5523 Load(args->at(0));
5524 Load(args->at(1));
5525 Register right = frame_->PopToRegister();
5526 Register left = frame_->PopToRegister(right);
5527 Register tmp = frame_->scratch0();
5528 Register tmp2 = frame_->scratch1();
5529
5530 // Jumps to done must have the eq flag set if the test is successful
5531 // and clear if the test has failed.
5532 Label done;
5533
5534 // Fail if either is a non-HeapObject.
5535 __ cmp(left, Operand(right));
5536 __ b(eq, &done);
5537 __ and_(tmp, left, Operand(right));
5538 __ eor(tmp, tmp, Operand(kSmiTagMask));
5539 __ tst(tmp, Operand(kSmiTagMask));
5540 __ b(ne, &done);
5541 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5542 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5543 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5544 __ b(ne, &done);
5545 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5546 __ cmp(tmp, Operand(tmp2));
5547 __ b(ne, &done);
5548 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5549 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5550 __ cmp(tmp, tmp2);
5551 __ bind(&done);
5552 cc_reg_ = eq;
5553}
5554
5555
5556
ager@chromium.org7c537e22008-10-16 08:43:32 +00005557void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005558#ifdef DEBUG
5559 int original_height = frame_->height();
5560#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005561 if (CheckForInlineRuntimeCall(node)) {
5562 ASSERT((has_cc() && frame_->height() == original_height) ||
5563 (!has_cc() && frame_->height() == original_height + 1));
5564 return;
5565 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005566
5567 ZoneList<Expression*>* args = node->arguments();
5568 Comment cmnt(masm_, "[ CallRuntime");
5569 Runtime::Function* function = node->function();
5570
ager@chromium.org41826e72009-03-30 13:30:57 +00005571 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005572 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005573 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005574 Register scratch = VirtualFrame::scratch0();
5575 __ ldr(scratch, GlobalObject());
5576 Register builtins = frame_->GetTOSRegister();
5577 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5578 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005579 }
mads.s.ager31e71382008-08-13 09:32:07 +00005580
ager@chromium.org41826e72009-03-30 13:30:57 +00005581 // Push the arguments ("left-to-right").
5582 int arg_count = args->length();
5583 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005584 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005585 }
mads.s.ager31e71382008-08-13 09:32:07 +00005586
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005587 VirtualFrame::SpilledScope spilled_scope(frame_);
5588
ager@chromium.org41826e72009-03-30 13:30:57 +00005589 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005590 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005591 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005592 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5593 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005594 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005595 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005596 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005597 } else {
5598 // Call the C runtime function.
5599 frame_->CallRuntime(function, arg_count);
5600 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005601 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005602 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005603}
5604
5605
ager@chromium.org7c537e22008-10-16 08:43:32 +00005606void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005607#ifdef DEBUG
5608 int original_height = frame_->height();
5609#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005610 Comment cmnt(masm_, "[ UnaryOperation");
5611
5612 Token::Value op = node->op();
5613
5614 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005615 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005616 // LoadCondition may (and usually does) leave a test and branch to
5617 // be emitted by the caller. In that case, negate the condition.
5618 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005619
5620 } else if (op == Token::DELETE) {
5621 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005622 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005623 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005624 Load(property->obj());
5625 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005626 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005627 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005628
mads.s.ager31e71382008-08-13 09:32:07 +00005629 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005630 Slot* slot = variable->slot();
5631 if (variable->is_global()) {
5632 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005633 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005634 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005635 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005636
5637 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5638 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005639 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005640 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005641 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005642 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005643 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005644 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005645 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005646 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005647
mads.s.ager31e71382008-08-13 09:32:07 +00005648 } else {
5649 // Default: Result of deleting non-global, not dynamically
5650 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005651 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005652 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005653
5654 } else {
5655 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005656 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005657 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005658 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005659 }
5660
5661 } else if (op == Token::TYPEOF) {
5662 // Special case for loading the typeof expression; see comment on
5663 // LoadTypeofExpression().
5664 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005665 frame_->CallRuntime(Runtime::kTypeof, 1);
5666 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005667
5668 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005669 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005670 (node->expression()->AsBinaryOperation() != NULL &&
5671 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005672 UnaryOverwriteMode overwrite =
5673 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5674
5675 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005676 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005677 switch (op) {
5678 case Token::NOT:
5679 case Token::DELETE:
5680 case Token::TYPEOF:
5681 UNREACHABLE(); // handled above
5682 break;
5683
5684 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005685 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005686 GenericUnaryOpStub stub(
5687 Token::SUB,
5688 overwrite,
5689 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005690 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005691 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005692 break;
5693 }
5694
5695 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005696 Register tos = frame_->PopToRegister();
5697 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005698 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005699 // Smi check.
5700 __ tst(tos, Operand(kSmiTagMask));
5701 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005702
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005703 __ mvn(tos, Operand(tos));
5704 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5705 frame_->EmitPush(tos);
5706 // The fast case is the first to jump to the continue label, so it gets
5707 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005708 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005709
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005710 not_smi_label.Bind();
5711 frame_->SpillAll();
5712 __ Move(r0, tos);
5713 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5714 frame_->CallStub(&stub, 0);
5715 frame_->EmitPush(r0);
5716
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005717 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005718 break;
5719 }
5720
5721 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005722 frame_->Drop();
5723 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005724 break;
5725
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005726 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005727 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005728 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005729 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005730 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005731 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005732
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005733 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005734 frame_->EmitPush(r0);
5735
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005736 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005737 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005738 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005739 default:
5740 UNREACHABLE();
5741 }
5742 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005743 ASSERT(!has_valid_frame() ||
5744 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005745 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005746}
5747
5748
ager@chromium.org7c537e22008-10-16 08:43:32 +00005749void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005750#ifdef DEBUG
5751 int original_height = frame_->height();
5752#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005753 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005754 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005755
5756 bool is_postfix = node->is_postfix();
5757 bool is_increment = node->op() == Token::INC;
5758
5759 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5760 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005761 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005762
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005763 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5764 // The type info declares that this variable is always a Smi. That
5765 // means it is a Smi both before and after the increment/decrement.
5766 // Lets make use of that to make a very minimal count.
5767 Reference target(this, node->expression(), !is_const);
5768 ASSERT(!target.is_illegal());
5769 target.GetValue(); // Pushes the value.
5770 Register value = frame_->PopToRegister();
5771 if (is_postfix) frame_->EmitPush(value);
5772 if (is_increment) {
5773 __ add(value, value, Operand(Smi::FromInt(1)));
5774 } else {
5775 __ sub(value, value, Operand(Smi::FromInt(1)));
5776 }
5777 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005778 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005779 if (is_postfix) frame_->Pop();
5780 ASSERT_EQ(original_height + 1, frame_->height());
5781 return;
5782 }
5783
5784 // If it's a postfix expression and its result is not ignored and the
5785 // reference is non-trivial, then push a placeholder on the stack now
5786 // to hold the result of the expression.
5787 bool placeholder_pushed = false;
5788 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005789 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005790 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005791 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005792
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005793 // A constant reference is not saved to, so a constant reference is not a
5794 // compound assignment reference.
5795 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005796 if (target.is_illegal()) {
5797 // Spoof the virtual frame to have the expected height (one higher
5798 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005799 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005800 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005801 return;
5802 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005803
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005804 // This pushes 0, 1 or 2 words on the object to be used later when updating
5805 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005806 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005807
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005808 JumpTarget slow;
5809 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005810
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005811 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005812
5813 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005814 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005815 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005816 } else if (is_postfix) {
5817 frame_->EmitPush(value);
5818 __ mov(VirtualFrame::scratch0(), value);
5819 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005820 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005821
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005822 // Check for smi operand.
5823 __ tst(value, Operand(kSmiTagMask));
5824 slow.Branch(ne);
5825
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005826 // Perform optimistic increment/decrement.
5827 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005828 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005829 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005830 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005831 }
5832
5833 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005834 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005835
5836 // Revert optimistic increment/decrement.
5837 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005838 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005839 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005840 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005841 }
5842
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005843 // Slow case: Convert to number. At this point the
5844 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005845 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005846
5847 // Convert the operand to a number.
5848 frame_->EmitPush(value);
5849
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005850 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005851 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005852 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005853
5854 if (is_postfix) {
5855 // Postfix: store to result (on the stack).
5856 __ str(r0, frame_->ElementAt(target.size()));
5857 }
5858
5859 // Compute the new value.
5860 frame_->EmitPush(r0);
5861 frame_->EmitPush(Operand(Smi::FromInt(1)));
5862 if (is_increment) {
5863 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5864 } else {
5865 frame_->CallRuntime(Runtime::kNumberSub, 2);
5866 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005867 }
5868
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005869 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005870 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005871 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005872 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005873 frame_->EmitPush(value);
5874 // Set the target with the result, leaving the result on
5875 // top of the stack. Removes the target from the stack if
5876 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005877 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005878 }
5879
5880 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005881 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005882 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005883}
5884
5885
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005886void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005887 // According to ECMA-262 section 11.11, page 58, the binary logical
5888 // operators must yield the result of one of the two expressions
5889 // before any ToBoolean() conversions. This means that the value
5890 // produced by a && or || operator is not necessarily a boolean.
5891
5892 // NOTE: If the left hand side produces a materialized value (not in
5893 // the CC register), we force the right hand side to do the
5894 // same. This is necessary because we may have to branch to the exit
5895 // after evaluating the left hand side (due to the shortcut
5896 // semantics), but the compiler must (statically) know if the result
5897 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005898 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005899 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005900 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005901 if (has_valid_frame() && !has_cc()) {
5902 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005903 JumpTarget pop_and_continue;
5904 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005905
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005906 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005907 // Avoid popping the result if it converts to 'false' using the
5908 // standard ToBoolean() conversion as described in ECMA-262,
5909 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005910 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005911 Branch(false, &exit);
5912
5913 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005914 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005915 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005916
5917 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005918 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005919 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005920
5921 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005922 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005923 } else if (has_cc() || is_true.is_linked()) {
5924 // The left-hand side is either (a) partially compiled to
5925 // control flow with a final branch left to emit or (b) fully
5926 // compiled to control flow and possibly true.
5927 if (has_cc()) {
5928 Branch(false, false_target());
5929 }
5930 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005931 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005932 } else {
5933 // Nothing to do.
5934 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005935 }
5936
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005937 } else {
5938 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005939 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005940 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005941 if (has_valid_frame() && !has_cc()) {
5942 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005943 JumpTarget pop_and_continue;
5944 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005945
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005946 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005947 // Avoid popping the result if it converts to 'true' using the
5948 // standard ToBoolean() conversion as described in ECMA-262,
5949 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005950 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005951 Branch(true, &exit);
5952
5953 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005954 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005955 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005956
5957 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005958 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005959 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005960
5961 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005962 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005963 } else if (has_cc() || is_false.is_linked()) {
5964 // The left-hand side is either (a) partially compiled to
5965 // control flow with a final branch left to emit or (b) fully
5966 // compiled to control flow and possibly false.
5967 if (has_cc()) {
5968 Branch(true, true_target());
5969 }
5970 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005971 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005972 } else {
5973 // Nothing to do.
5974 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005975 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005976 }
5977}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005978
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005979
5980void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5981#ifdef DEBUG
5982 int original_height = frame_->height();
5983#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005984 Comment cmnt(masm_, "[ BinaryOperation");
5985
5986 if (node->op() == Token::AND || node->op() == Token::OR) {
5987 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005988 } else {
5989 // Optimize for the case where (at least) one of the expressions
5990 // is a literal small integer.
5991 Literal* lliteral = node->left()->AsLiteral();
5992 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005993 // NOTE: The code below assumes that the slow cases (calls to runtime)
5994 // never return a constant/immutable object.
5995 bool overwrite_left =
5996 (node->left()->AsBinaryOperation() != NULL &&
5997 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5998 bool overwrite_right =
5999 (node->right()->AsBinaryOperation() != NULL &&
6000 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006001
6002 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006003 VirtualFrame::RegisterAllocationScope scope(this);
6004 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006005 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006006 SmiOperation(node->op(),
6007 rliteral->handle(),
6008 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006009 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006010 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006011 VirtualFrame::RegisterAllocationScope scope(this);
6012 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006013 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006014 SmiOperation(node->op(),
6015 lliteral->handle(),
6016 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006017 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006018 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006019 GenerateInlineSmi inline_smi =
6020 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6021 if (lliteral != NULL) {
6022 ASSERT(!lliteral->handle()->IsSmi());
6023 inline_smi = DONT_GENERATE_INLINE_SMI;
6024 }
6025 if (rliteral != NULL) {
6026 ASSERT(!rliteral->handle()->IsSmi());
6027 inline_smi = DONT_GENERATE_INLINE_SMI;
6028 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006029 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006030 OverwriteMode overwrite_mode = NO_OVERWRITE;
6031 if (overwrite_left) {
6032 overwrite_mode = OVERWRITE_LEFT;
6033 } else if (overwrite_right) {
6034 overwrite_mode = OVERWRITE_RIGHT;
6035 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006036 Load(node->left());
6037 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006038 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006039 }
6040 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006041 ASSERT(!has_valid_frame() ||
6042 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006043 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006044}
6045
6046
ager@chromium.org7c537e22008-10-16 08:43:32 +00006047void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006048#ifdef DEBUG
6049 int original_height = frame_->height();
6050#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006051 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006052 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006053}
6054
6055
ager@chromium.org7c537e22008-10-16 08:43:32 +00006056void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006057#ifdef DEBUG
6058 int original_height = frame_->height();
6059#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006060 Comment cmnt(masm_, "[ CompareOperation");
6061
ager@chromium.org357bf652010-04-12 11:30:10 +00006062 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006064 // Get the expressions from the node.
6065 Expression* left = node->left();
6066 Expression* right = node->right();
6067 Token::Value op = node->op();
6068
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006069 // To make null checks efficient, we check if either left or right is the
6070 // literal 'null'. If so, we optimize the code by inlining a null check
6071 // instead of calling the (very) general runtime routine for checking
6072 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006073 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006074 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006075 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006076 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006077 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
6078 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006079 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006080 Load(left_is_null ? right : left);
6081 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006082 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006083 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006084
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006085 // The 'null' value is only equal to 'undefined' if using non-strict
6086 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006088 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006089
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006090 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006091 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006092 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006093
ager@chromium.org357bf652010-04-12 11:30:10 +00006094 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006095 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006096
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006097 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006098 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6099 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6100 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6101 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006102 }
6103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006104 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006105 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006106 return;
6107 }
6108 }
6109
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006110 // To make typeof testing for natives implemented in JavaScript really
6111 // efficient, we generate special code for expressions of the form:
6112 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006113 UnaryOperation* operation = left->AsUnaryOperation();
6114 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6115 (operation != NULL && operation->op() == Token::TYPEOF) &&
6116 (right->AsLiteral() != NULL &&
6117 right->AsLiteral()->handle()->IsString())) {
6118 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6119
ager@chromium.org357bf652010-04-12 11:30:10 +00006120 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006121 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006122 Register tos = frame_->PopToRegister();
6123
ager@chromium.org357bf652010-04-12 11:30:10 +00006124 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006125
6126 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006127 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006128 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006129 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006130 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006131 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006132 cc_reg_ = eq;
6133
6134 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006135 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006136 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006137
ager@chromium.org357bf652010-04-12 11:30:10 +00006138 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006139
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006140 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006141 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6142 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6143 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006144 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006145
ager@chromium.org357bf652010-04-12 11:30:10 +00006146 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6147 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006148 cc_reg_ = lt;
6149
6150 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006151 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006152 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006153 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006154 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006155 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006156 cc_reg_ = eq;
6157
6158 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006159 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006160 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006161 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006162
ager@chromium.org357bf652010-04-12 11:30:10 +00006163 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006164 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006165
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006166 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006167 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6168 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6169 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6170 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006171
6172 cc_reg_ = eq;
6173
6174 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006175 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006176 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006177 Register map_reg = scratch;
6178 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006179 true_target()->Branch(eq);
6180 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006181 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006182 cc_reg_ = eq;
6183
6184 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006185 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006186 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006187
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006188 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006189 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006190 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006191
ager@chromium.org357bf652010-04-12 11:30:10 +00006192 Register map_reg = scratch;
6193 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006194 false_target()->Branch(eq);
6195
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006196 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006197 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6198 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6199 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006200 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006201
ager@chromium.org357bf652010-04-12 11:30:10 +00006202 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6203 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006204 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006205 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006206 cc_reg_ = le;
6207
6208 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006209 // Uncommon case: typeof testing against a string literal that is
6210 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006211 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006212 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006213 ASSERT(!has_valid_frame() ||
6214 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006215 return;
6216 }
6217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006218 switch (op) {
6219 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006220 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006221 break;
6222
6223 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006224 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006225 break;
6226
6227 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006228 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006229 break;
6230
6231 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006232 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006233 break;
6234
6235 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006236 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006237 break;
6238
6239 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006240 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006241 break;
6242
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006243 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006244 Load(left);
6245 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006246 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006247 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006248 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006249 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006250
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006251 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006252 Load(left);
6253 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006254 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006255 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006256 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006257 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006258 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006259 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006260 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006261
6262 default:
6263 UNREACHABLE();
6264 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006265 ASSERT((has_cc() && frame_->height() == original_height) ||
6266 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006267}
6268
6269
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006270class DeferredReferenceGetNamedValue: public DeferredCode {
6271 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006272 explicit DeferredReferenceGetNamedValue(Register receiver,
6273 Handle<String> name)
6274 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006275 set_comment("[ DeferredReferenceGetNamedValue");
6276 }
6277
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006278 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006279
6280 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006281 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006282 Handle<String> name_;
6283};
6284
6285
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006286// Convention for this is that on entry the receiver is in a register that
6287// is not used by the stack. On exit the answer is found in that same
6288// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006289void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006290#ifdef DEBUG
6291 int expected_height = frame_state()->frame()->height();
6292#endif
6293 VirtualFrame copied_frame(*frame_state()->frame());
6294 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006295
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006296 Register scratch1 = VirtualFrame::scratch0();
6297 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006298 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006299 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6300 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6301
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006302 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6303 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006304 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006305
6306 // The rest of the instructions in the deferred code must be together.
6307 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6308 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6309 __ Call(ic, RelocInfo::CODE_TARGET);
6310 // The call must be followed by a nop(1) instruction to indicate that the
6311 // in-object has been inlined.
6312 __ nop(PROPERTY_ACCESS_INLINED);
6313
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006314 // At this point the answer is in r0. We move it to the expected register
6315 // if necessary.
6316 __ Move(receiver_, r0);
6317
6318 // Now go back to the frame that we entered with. This will not overwrite
6319 // the receiver register since that register was not in use when we came
6320 // in. The instructions emitted by this merge are skipped over by the
6321 // inline load patching mechanism when looking for the branch instruction
6322 // that tells it where the code to patch is.
6323 copied_frame.MergeTo(frame_state()->frame());
6324
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006325 // Block the constant pool for one more instruction after leaving this
6326 // constant pool block scope to include the branch instruction ending the
6327 // deferred code.
6328 __ BlockConstPoolFor(1);
6329 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006330 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006331}
6332
6333
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006334class DeferredReferenceGetKeyedValue: public DeferredCode {
6335 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006336 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6337 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006338 set_comment("[ DeferredReferenceGetKeyedValue");
6339 }
6340
6341 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006342
6343 private:
6344 Register key_;
6345 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006346};
6347
6348
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006349// Takes key and register in r0 and r1 or vice versa. Returns result
6350// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006351void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006352 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6353 (key_.is(r1) && receiver_.is(r0)));
6354
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006355 VirtualFrame copied_frame(*frame_state()->frame());
6356 copied_frame.SpillAll();
6357
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006358 Register scratch1 = VirtualFrame::scratch0();
6359 Register scratch2 = VirtualFrame::scratch1();
6360 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6361 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6362
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006363 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6364 // convention.
6365 if (key_.is(r1)) {
6366 __ Swap(r0, r1, ip);
6367 }
6368
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006369 // The rest of the instructions in the deferred code must be together.
6370 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006371 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006372 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6373 __ Call(ic, RelocInfo::CODE_TARGET);
6374 // The call must be followed by a nop instruction to indicate that the
6375 // keyed load has been inlined.
6376 __ nop(PROPERTY_ACCESS_INLINED);
6377
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006378 // Now go back to the frame that we entered with. This will not overwrite
6379 // the receiver or key registers since they were not in use when we came
6380 // in. The instructions emitted by this merge are skipped over by the
6381 // inline load patching mechanism when looking for the branch instruction
6382 // that tells it where the code to patch is.
6383 copied_frame.MergeTo(frame_state()->frame());
6384
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006385 // Block the constant pool for one more instruction after leaving this
6386 // constant pool block scope to include the branch instruction ending the
6387 // deferred code.
6388 __ BlockConstPoolFor(1);
6389 }
6390}
6391
6392
6393class DeferredReferenceSetKeyedValue: public DeferredCode {
6394 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006395 DeferredReferenceSetKeyedValue(Register value,
6396 Register key,
6397 Register receiver)
6398 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006399 set_comment("[ DeferredReferenceSetKeyedValue");
6400 }
6401
6402 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006403
6404 private:
6405 Register value_;
6406 Register key_;
6407 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006408};
6409
6410
6411void DeferredReferenceSetKeyedValue::Generate() {
6412 Register scratch1 = VirtualFrame::scratch0();
6413 Register scratch2 = VirtualFrame::scratch1();
6414 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6415 __ IncrementCounter(
6416 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6417
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006418 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6419 // calling convention.
6420 if (value_.is(r1)) {
6421 __ Swap(r0, r1, ip);
6422 }
6423 ASSERT(receiver_.is(r2));
6424
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006425 // The rest of the instructions in the deferred code must be together.
6426 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006427 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6428 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006429 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6430 __ Call(ic, RelocInfo::CODE_TARGET);
6431 // The call must be followed by a nop instruction to indicate that the
6432 // keyed store has been inlined.
6433 __ nop(PROPERTY_ACCESS_INLINED);
6434
6435 // Block the constant pool for one more instruction after leaving this
6436 // constant pool block scope to include the branch instruction ending the
6437 // deferred code.
6438 __ BlockConstPoolFor(1);
6439 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006440}
6441
6442
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006443class DeferredReferenceSetNamedValue: public DeferredCode {
6444 public:
6445 DeferredReferenceSetNamedValue(Register value,
6446 Register receiver,
6447 Handle<String> name)
6448 : value_(value), receiver_(receiver), name_(name) {
6449 set_comment("[ DeferredReferenceSetNamedValue");
6450 }
6451
6452 virtual void Generate();
6453
6454 private:
6455 Register value_;
6456 Register receiver_;
6457 Handle<String> name_;
6458};
6459
6460
6461// Takes value in r0, receiver in r1 and returns the result (the
6462// value) in r0.
6463void DeferredReferenceSetNamedValue::Generate() {
6464 // Record the entry frame and spill.
6465 VirtualFrame copied_frame(*frame_state()->frame());
6466 copied_frame.SpillAll();
6467
6468 // Ensure value in r0, receiver in r1 to match store ic calling
6469 // convention.
6470 ASSERT(value_.is(r0) && receiver_.is(r1));
6471 __ mov(r2, Operand(name_));
6472
6473 // The rest of the instructions in the deferred code must be together.
6474 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6475 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6476 // r1 and r2.
6477 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6478 __ Call(ic, RelocInfo::CODE_TARGET);
6479 // The call must be followed by a nop instruction to indicate that the
6480 // named store has been inlined.
6481 __ nop(PROPERTY_ACCESS_INLINED);
6482
6483 // Go back to the frame we entered with. The instructions
6484 // generated by this merge are skipped over by the inline store
6485 // patching mechanism when looking for the branch instruction that
6486 // tells it where the code to patch is.
6487 copied_frame.MergeTo(frame_state()->frame());
6488
6489 // Block the constant pool for one more instruction after leaving this
6490 // constant pool block scope to include the branch instruction ending the
6491 // deferred code.
6492 __ BlockConstPoolFor(1);
6493 }
6494}
6495
6496
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006497// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006498void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6499 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6500 Comment cmnt(masm(), "[ Load from named Property");
6501 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006502 frame_->CallLoadIC(name,
6503 is_contextual
6504 ? RelocInfo::CODE_TARGET_CONTEXT
6505 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006506 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006507 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006508 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006509 Comment cmnt(masm(), "[ Inlined named property load");
6510
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006511 // Counter will be decremented in the deferred code. Placed here to avoid
6512 // having it in the instruction stream below where patching will occur.
6513 __ IncrementCounter(&Counters::named_load_inline, 1,
6514 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006515
6516 // The following instructions are the inlined load of an in-object property.
6517 // Parts of this code is patched, so the exact instructions generated needs
6518 // to be fixed. Therefore the instruction pool is blocked when generating
6519 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006520
6521 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006522 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006523
6524 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006525 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006526
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006527#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006528 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006529 Label check_inlined_codesize;
6530 masm_->bind(&check_inlined_codesize);
6531#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006532
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006533 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006534 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006535 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006536 deferred->Branch(eq);
6537
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006538 Register scratch = VirtualFrame::scratch0();
6539 Register scratch2 = VirtualFrame::scratch1();
6540
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006541 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006542 // code. Therefore we can't use a LoadRoot call.
6543 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6544 __ mov(scratch2, Operand(Factory::null_value()));
6545 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006546 deferred->Branch(ne);
6547
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006548 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006549 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006550 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006551
6552 // Make sure that the expected number of instructions are generated.
6553 ASSERT_EQ(kInlinedNamedLoadInstructions,
6554 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006555 }
6556
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006557 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006558 // At this point the receiver register has the result, either from the
6559 // deferred code or from the inlined code.
6560 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006561 }
6562}
6563
6564
ager@chromium.orgac091b72010-05-05 07:34:42 +00006565void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6566#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006567 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006568#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006569
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006570 Result result;
6571 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6572 frame()->CallStoreIC(name, is_contextual);
6573 } else {
6574 // Inline the in-object property case.
6575 JumpTarget slow, done;
6576
6577 // Get the value and receiver from the stack.
6578 frame()->PopToR0();
6579 Register value = r0;
6580 frame()->PopToR1();
6581 Register receiver = r1;
6582
6583 DeferredReferenceSetNamedValue* deferred =
6584 new DeferredReferenceSetNamedValue(value, receiver, name);
6585
6586 // Check that the receiver is a heap object.
6587 __ tst(receiver, Operand(kSmiTagMask));
6588 deferred->Branch(eq);
6589
6590 // The following instructions are the part of the inlined
6591 // in-object property store code which can be patched. Therefore
6592 // the exact number of instructions generated must be fixed, so
6593 // the constant pool is blocked while generating this code.
6594 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6595 Register scratch0 = VirtualFrame::scratch0();
6596 Register scratch1 = VirtualFrame::scratch1();
6597
6598 // Check the map. Initially use an invalid map to force a
6599 // failure. The map check will be patched in the runtime system.
6600 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6601
6602#ifdef DEBUG
6603 Label check_inlined_codesize;
6604 masm_->bind(&check_inlined_codesize);
6605#endif
6606 __ mov(scratch0, Operand(Factory::null_value()));
6607 __ cmp(scratch0, scratch1);
6608 deferred->Branch(ne);
6609
6610 int offset = 0;
6611 __ str(value, MemOperand(receiver, offset));
6612
6613 // Update the write barrier and record its size. We do not use
6614 // the RecordWrite macro here because we want the offset
6615 // addition instruction first to make it easy to patch.
6616 Label record_write_start, record_write_done;
6617 __ bind(&record_write_start);
6618 // Add offset into the object.
6619 __ add(scratch0, receiver, Operand(offset));
6620 // Test that the object is not in the new space. We cannot set
6621 // region marks for new space pages.
6622 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6623 // Record the actual write.
6624 __ RecordWriteHelper(receiver, scratch0, scratch1);
6625 __ bind(&record_write_done);
6626 // Clobber all input registers when running with the debug-code flag
6627 // turned on to provoke errors.
6628 if (FLAG_debug_code) {
6629 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6630 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6631 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6632 }
6633 // Check that this is the first inlined write barrier or that
6634 // this inlined write barrier has the same size as all the other
6635 // inlined write barriers.
6636 ASSERT((inlined_write_barrier_size_ == -1) ||
6637 (inlined_write_barrier_size_ ==
6638 masm()->InstructionsGeneratedSince(&record_write_start)));
6639 inlined_write_barrier_size_ =
6640 masm()->InstructionsGeneratedSince(&record_write_start);
6641
6642 // Make sure that the expected number of instructions are generated.
6643 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6644 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6645 }
6646 deferred->BindExit();
6647 }
6648 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006649}
6650
6651
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006652void CodeGenerator::EmitKeyedLoad() {
6653 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006654 Comment cmnt(masm_, "[ Load from keyed property");
6655 frame_->CallKeyedLoadIC();
6656 } else {
6657 // Inline the keyed load.
6658 Comment cmnt(masm_, "[ Inlined load from keyed property");
6659
6660 // Counter will be decremented in the deferred code. Placed here to avoid
6661 // having it in the instruction stream below where patching will occur.
6662 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6663 frame_->scratch0(), frame_->scratch1());
6664
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006665 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006666 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006667 Register key = frame_->PopToRegister();
6668 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006669
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006670 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006671 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006672 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006673
6674 // Check that the receiver is a heap object.
6675 __ tst(receiver, Operand(kSmiTagMask));
6676 deferred->Branch(eq);
6677
6678 // The following instructions are the part of the inlined load keyed
6679 // property code which can be patched. Therefore the exact number of
6680 // instructions generated need to be fixed, so the constant pool is blocked
6681 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006682 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6683 Register scratch1 = VirtualFrame::scratch0();
6684 Register scratch2 = VirtualFrame::scratch1();
6685 // Check the map. The null map used below is patched by the inline cache
6686 // code.
6687 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006688
6689 // Check that the key is a smi.
6690 if (!key_is_known_smi) {
6691 __ tst(key, Operand(kSmiTagMask));
6692 deferred->Branch(ne);
6693 }
6694
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006695#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006696 Label check_inlined_codesize;
6697 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006698#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006699 __ mov(scratch2, Operand(Factory::null_value()));
6700 __ cmp(scratch1, scratch2);
6701 deferred->Branch(ne);
6702
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006703 // Get the elements array from the receiver.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006704 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00006705 __ AssertFastElements(scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006706
6707 // Check that key is within bounds. Use unsigned comparison to handle
6708 // negative keys.
6709 __ ldr(scratch2, FieldMemOperand(scratch1, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006710 __ cmp(scratch2, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006711 deferred->Branch(ls); // Unsigned less equal.
6712
6713 // Load and check that the result is not the hole (key is a smi).
6714 __ LoadRoot(scratch2, Heap::kTheHoleValueRootIndex);
6715 __ add(scratch1,
6716 scratch1,
6717 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006718 __ ldr(scratch1,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006719 MemOperand(scratch1, key, LSL,
6720 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006721 __ cmp(scratch1, scratch2);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006722 deferred->Branch(eq);
6723
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006724 __ mov(r0, scratch1);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006725 // Make sure that the expected number of instructions are generated.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006726 ASSERT_EQ(GetInlinedKeyedLoadInstructionsAfterPatch(),
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006727 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6728 }
6729
6730 deferred->BindExit();
6731 }
6732}
6733
6734
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006735void CodeGenerator::EmitKeyedStore(StaticType* key_type,
6736 WriteBarrierCharacter wb_info) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006737 // Generate inlined version of the keyed store if the code is in a loop
6738 // and the key is likely to be a smi.
6739 if (loop_nesting() > 0 && key_type->IsLikelySmi()) {
6740 // Inline the keyed store.
6741 Comment cmnt(masm_, "[ Inlined store to keyed property");
6742
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006743 Register scratch1 = VirtualFrame::scratch0();
6744 Register scratch2 = VirtualFrame::scratch1();
6745 Register scratch3 = r3;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006746
6747 // Counter will be decremented in the deferred code. Placed here to avoid
6748 // having it in the instruction stream below where patching will occur.
6749 __ IncrementCounter(&Counters::keyed_store_inline, 1,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006750 scratch1, scratch2);
6751
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006752
6753
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006754 // Load the value, key and receiver from the stack.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006755 bool value_is_harmless = frame_->KnownSmiAt(0);
6756 if (wb_info == NEVER_NEWSPACE) value_is_harmless = true;
6757 bool key_is_smi = frame_->KnownSmiAt(1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006758 Register value = frame_->PopToRegister();
6759 Register key = frame_->PopToRegister(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006760 VirtualFrame::SpilledScope spilled(frame_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006761 Register receiver = r2;
6762 frame_->EmitPop(receiver);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006763
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006764#ifdef DEBUG
6765 bool we_remembered_the_write_barrier = value_is_harmless;
6766#endif
6767
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006768 // The deferred code expects value, key and receiver in registers.
6769 DeferredReferenceSetKeyedValue* deferred =
6770 new DeferredReferenceSetKeyedValue(value, key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006771
6772 // Check that the value is a smi. As this inlined code does not set the
6773 // write barrier it is only possible to store smi values.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006774 if (!value_is_harmless) {
6775 // If the value is not likely to be a Smi then let's test the fixed array
6776 // for new space instead. See below.
6777 if (wb_info == LIKELY_SMI) {
6778 __ tst(value, Operand(kSmiTagMask));
6779 deferred->Branch(ne);
6780#ifdef DEBUG
6781 we_remembered_the_write_barrier = true;
6782#endif
6783 }
6784 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006785
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006786 if (!key_is_smi) {
6787 // Check that the key is a smi.
6788 __ tst(key, Operand(kSmiTagMask));
6789 deferred->Branch(ne);
6790 }
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006791
6792 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006793 __ tst(receiver, Operand(kSmiTagMask));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006794 deferred->Branch(eq);
6795
6796 // Check that the receiver is a JSArray.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006797 __ CompareObjectType(receiver, scratch1, scratch1, JS_ARRAY_TYPE);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006798 deferred->Branch(ne);
6799
6800 // Check that the key is within bounds. Both the key and the length of
6801 // the JSArray are smis. Use unsigned comparison to handle negative keys.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006802 __ ldr(scratch1, FieldMemOperand(receiver, JSArray::kLengthOffset));
6803 __ cmp(scratch1, key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006804 deferred->Branch(ls); // Unsigned less equal.
6805
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006806 // Get the elements array from the receiver.
6807 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
6808 if (!value_is_harmless && wb_info != LIKELY_SMI) {
6809 Label ok;
6810 __ and_(scratch2, scratch1, Operand(ExternalReference::new_space_mask()));
6811 __ cmp(scratch2, Operand(ExternalReference::new_space_start()));
6812 __ tst(value, Operand(kSmiTagMask), ne);
6813 deferred->Branch(ne);
6814#ifdef DEBUG
6815 we_remembered_the_write_barrier = true;
6816#endif
6817 }
6818 // Check that the elements array is not a dictionary.
6819 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006820 // The following instructions are the part of the inlined store keyed
6821 // property code which can be patched. Therefore the exact number of
6822 // instructions generated need to be fixed, so the constant pool is blocked
6823 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006824 { Assembler::BlockConstPoolScope block_const_pool(masm_);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006825#ifdef DEBUG
6826 Label check_inlined_codesize;
6827 masm_->bind(&check_inlined_codesize);
6828#endif
6829
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006830 // Read the fixed array map from the constant pool (not from the root
6831 // array) so that the value can be patched. When debugging, we patch this
6832 // comparison to always fail so that we will hit the IC call in the
6833 // deferred code which will allow the debugger to break for fast case
6834 // stores.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006835 __ mov(scratch3, Operand(Factory::fixed_array_map()));
6836 __ cmp(scratch2, scratch3);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006837 deferred->Branch(ne);
6838
6839 // Store the value.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006840 __ add(scratch1, scratch1,
6841 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6842 __ str(value,
6843 MemOperand(scratch1, key, LSL,
6844 kPointerSizeLog2 - (kSmiTagSize + kSmiShiftSize)));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006845
6846 // Make sure that the expected number of instructions are generated.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006847 ASSERT_EQ(kInlinedKeyedStoreInstructionsAfterPatch,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006848 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
6849 }
6850
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006851 ASSERT(we_remembered_the_write_barrier);
6852
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006853 deferred->BindExit();
6854 } else {
6855 frame()->CallKeyedStoreIC();
6856 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006857}
6858
6859
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006860#ifdef DEBUG
6861bool CodeGenerator::HasValidEntryRegisters() { return true; }
6862#endif
6863
6864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006865#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006866#define __ ACCESS_MASM(masm)
6867
ager@chromium.org7c537e22008-10-16 08:43:32 +00006868Handle<String> Reference::GetName() {
6869 ASSERT(type_ == NAMED);
6870 Property* property = expression_->AsProperty();
6871 if (property == NULL) {
6872 // Global variable reference treated as a named property reference.
6873 VariableProxy* proxy = expression_->AsVariableProxy();
6874 ASSERT(proxy->AsVariable() != NULL);
6875 ASSERT(proxy->AsVariable()->is_global());
6876 return proxy->name();
6877 } else {
6878 Literal* raw_name = property->key()->AsLiteral();
6879 ASSERT(raw_name != NULL);
6880 return Handle<String>(String::cast(*raw_name->handle()));
6881 }
6882}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006883
ager@chromium.org7c537e22008-10-16 08:43:32 +00006884
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006885void Reference::DupIfPersist() {
6886 if (persist_after_get_) {
6887 switch (type_) {
6888 case KEYED:
6889 cgen_->frame()->Dup2();
6890 break;
6891 case NAMED:
6892 cgen_->frame()->Dup();
6893 // Fall through.
6894 case UNLOADED:
6895 case ILLEGAL:
6896 case SLOT:
6897 // Do nothing.
6898 ;
6899 }
6900 } else {
6901 set_unloaded();
6902 }
6903}
6904
6905
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006906void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006907 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006908 ASSERT(!is_illegal());
6909 ASSERT(!cgen_->has_cc());
6910 MacroAssembler* masm = cgen_->masm();
6911 Property* property = expression_->AsProperty();
6912 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006913 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00006914 }
6915
6916 switch (type_) {
6917 case SLOT: {
6918 Comment cmnt(masm, "[ Load from Slot");
6919 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
6920 ASSERT(slot != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006921 DupIfPersist();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006922 cgen_->LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006923 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006924 }
6925
ager@chromium.org7c537e22008-10-16 08:43:32 +00006926 case NAMED: {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006927 Variable* var = expression_->AsVariableProxy()->AsVariable();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006928 bool is_global = var != NULL;
6929 ASSERT(!is_global || var->is_global());
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006930 Handle<String> name = GetName();
6931 DupIfPersist();
6932 cgen_->EmitNamedLoad(name, is_global);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006933 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006934 }
6935
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006936 case KEYED: {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006937 ASSERT(property != NULL);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006938 DupIfPersist();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006939 cgen_->EmitKeyedLoad();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006940 cgen_->frame()->EmitPush(r0);
6941 break;
6942 }
6943
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006944 default:
6945 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006946 }
6947}
6948
6949
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006950void Reference::SetValue(InitState init_state, WriteBarrierCharacter wb_info) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00006951 ASSERT(!is_illegal());
6952 ASSERT(!cgen_->has_cc());
6953 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006954 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006955 Property* property = expression_->AsProperty();
6956 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006957 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006958 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006959
ager@chromium.org7c537e22008-10-16 08:43:32 +00006960 switch (type_) {
6961 case SLOT: {
6962 Comment cmnt(masm, "[ Store to Slot");
6963 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006964 cgen_->StoreToSlot(slot, init_state);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006965 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006966 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006967 }
6968
ager@chromium.org7c537e22008-10-16 08:43:32 +00006969 case NAMED: {
6970 Comment cmnt(masm, "[ Store to named Property");
ager@chromium.orgac091b72010-05-05 07:34:42 +00006971 cgen_->EmitNamedStore(GetName(), false);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006972 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00006973 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006974 break;
6975 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006976
ager@chromium.org7c537e22008-10-16 08:43:32 +00006977 case KEYED: {
6978 Comment cmnt(masm, "[ Store to keyed Property");
6979 Property* property = expression_->AsProperty();
6980 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00006981 cgen_->CodeForSourcePosition(property->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00006982 cgen_->EmitKeyedStore(property->key()->type(), wb_info);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006983 frame->EmitPush(r0);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006984 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00006985 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006986 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00006987
6988 default:
6989 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006990 }
6991}
6992
6993
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006994void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006995 // Create a new closure from the given function info in new
6996 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006997 Label gc;
6998
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006999 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007000 __ pop(r3);
7001
7002 // Attempt to allocate new JSFunction in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007003 __ AllocateInNewSpace(JSFunction::kSize,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007004 r0,
7005 r1,
7006 r2,
7007 &gc,
7008 TAG_OBJECT);
7009
7010 // Compute the function map in the current global context and set that
7011 // as the map of the allocated object.
7012 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7013 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
7014 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
7015 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
7016
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007017 // Initialize the rest of the function. We don't have to update the
7018 // write barrier because the allocated object is in new space.
7019 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
7020 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
7021 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
7022 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
7023 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
7024 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
7025 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
7026 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007027
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00007028 // Initialize the code pointer in the function to be the one
7029 // found in the shared function info object.
7030 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
7031 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeOffset));
7032
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007033 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007034 __ Ret();
7035
7036 // Create a new closure through the slower runtime call.
7037 __ bind(&gc);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007038 __ Push(cp, r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007039 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007040}
7041
7042
7043void FastNewContextStub::Generate(MacroAssembler* masm) {
7044 // Try to allocate the context in new space.
7045 Label gc;
7046 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
7047
7048 // Attempt to allocate the context in new space.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007049 __ AllocateInNewSpace(FixedArray::SizeFor(length),
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007050 r0,
7051 r1,
7052 r2,
7053 &gc,
7054 TAG_OBJECT);
7055
7056 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00007057 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007058
7059 // Setup the object header.
7060 __ LoadRoot(r2, Heap::kContextMapRootIndex);
7061 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007062 __ mov(r2, Operand(Smi::FromInt(length)));
7063 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007064
7065 // Setup the fixed slots.
7066 __ mov(r1, Operand(Smi::FromInt(0)));
7067 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
7068 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
7069 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
7070 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
7071
7072 // Copy the global object from the surrounding context.
7073 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7074 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
7075
7076 // Initialize the rest of the slots to undefined.
7077 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
7078 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
7079 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
7080 }
7081
7082 // Remove the on-stack argument and return.
7083 __ mov(cp, r0);
7084 __ pop();
7085 __ Ret();
7086
7087 // Need to collect. Call into runtime system.
7088 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007089 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007090}
7091
7092
ager@chromium.org5c838252010-02-19 08:53:10 +00007093void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
7094 // Stack layout on entry:
7095 //
7096 // [sp]: constant elements.
7097 // [sp + kPointerSize]: literal index.
7098 // [sp + (2 * kPointerSize)]: literals array.
7099
7100 // All sizes here are multiples of kPointerSize.
7101 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
7102 int size = JSArray::kSize + elements_size;
7103
7104 // Load boilerplate object into r3 and check if we need to create a
7105 // boilerplate.
7106 Label slow_case;
7107 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
7108 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
7109 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7110 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
7111 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
7112 __ cmp(r3, ip);
7113 __ b(eq, &slow_case);
7114
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00007115 if (FLAG_debug_code) {
7116 const char* message;
7117 Heap::RootListIndex expected_map_index;
7118 if (mode_ == CLONE_ELEMENTS) {
7119 message = "Expected (writable) fixed array";
7120 expected_map_index = Heap::kFixedArrayMapRootIndex;
7121 } else {
7122 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
7123 message = "Expected copy-on-write fixed array";
7124 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
7125 }
7126 __ push(r3);
7127 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
7128 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
7129 __ LoadRoot(ip, expected_map_index);
7130 __ cmp(r3, ip);
7131 __ Assert(eq, message);
7132 __ pop(r3);
7133 }
7134
ager@chromium.org5c838252010-02-19 08:53:10 +00007135 // Allocate both the JS array and the elements array in one big
7136 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007137 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00007138 r0,
7139 r1,
7140 r2,
7141 &slow_case,
7142 TAG_OBJECT);
7143
7144 // Copy the JS array part.
7145 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
7146 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
7147 __ ldr(r1, FieldMemOperand(r3, i));
7148 __ str(r1, FieldMemOperand(r0, i));
7149 }
7150 }
7151
7152 if (length_ > 0) {
7153 // Get hold of the elements array of the boilerplate and setup the
7154 // elements pointer in the resulting object.
7155 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
7156 __ add(r2, r0, Operand(JSArray::kSize));
7157 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
7158
7159 // Copy the elements array.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00007160 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00007161 }
7162
7163 // Return and remove the on-stack parameters.
7164 __ add(sp, sp, Operand(3 * kPointerSize));
7165 __ Ret();
7166
7167 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007168 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00007169}
7170
7171
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007172// Takes a Smi and converts to an IEEE 64 bit floating point value in two
7173// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
7174// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
7175// scratch register. Destroys the source register. No GC occurs during this
7176// stub so you don't have to set up the frame.
7177class ConvertToDoubleStub : public CodeStub {
7178 public:
7179 ConvertToDoubleStub(Register result_reg_1,
7180 Register result_reg_2,
7181 Register source_reg,
7182 Register scratch_reg)
7183 : result1_(result_reg_1),
7184 result2_(result_reg_2),
7185 source_(source_reg),
7186 zeros_(scratch_reg) { }
7187
7188 private:
7189 Register result1_;
7190 Register result2_;
7191 Register source_;
7192 Register zeros_;
7193
7194 // Minor key encoding in 16 bits.
7195 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
7196 class OpBits: public BitField<Token::Value, 2, 14> {};
7197
7198 Major MajorKey() { return ConvertToDouble; }
7199 int MinorKey() {
7200 // Encode the parameters in a unique 16 bit value.
7201 return result1_.code() +
7202 (result2_.code() << 4) +
7203 (source_.code() << 8) +
7204 (zeros_.code() << 12);
7205 }
7206
7207 void Generate(MacroAssembler* masm);
7208
7209 const char* GetName() { return "ConvertToDoubleStub"; }
7210
7211#ifdef DEBUG
7212 void Print() { PrintF("ConvertToDoubleStub\n"); }
7213#endif
7214};
7215
7216
7217void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
7218#ifndef BIG_ENDIAN_FLOATING_POINT
7219 Register exponent = result1_;
7220 Register mantissa = result2_;
7221#else
7222 Register exponent = result2_;
7223 Register mantissa = result1_;
7224#endif
7225 Label not_special;
7226 // Convert from Smi to integer.
7227 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
7228 // Move sign bit from source to destination. This works because the sign bit
7229 // in the exponent word of the double has the same position and polarity as
7230 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007231 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007232 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
7233 // Subtract from 0 if source was negative.
7234 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007235
7236 // We have -1, 0 or 1, which we treat specially. Register source_ contains
7237 // absolute value: it is either equal to 1 (special case of -1 and 1),
7238 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007239 __ cmp(source_, Operand(1));
7240 __ b(gt, &not_special);
7241
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007242 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
7243 static const uint32_t exponent_word_for_1 =
7244 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007245 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007246 // 1, 0 and -1 all have 0 for the second word.
7247 __ mov(mantissa, Operand(0));
7248 __ Ret();
7249
7250 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007251 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007252 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007253 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007254 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007255 // We use mantissa as a scratch register here. Use a fudge factor to
7256 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7257 // that fit in the ARM's constant field.
7258 int fudge = 0x400;
7259 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7260 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007261 __ orr(exponent,
7262 exponent,
7263 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7264 // Shift up the source chopping the top bit off.
7265 __ add(zeros_, zeros_, Operand(1));
7266 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7267 __ mov(source_, Operand(source_, LSL, zeros_));
7268 // Compute lower part of fraction (last 12 bits).
7269 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7270 // And the top (top 20 bits).
7271 __ orr(exponent,
7272 exponent,
7273 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7274 __ Ret();
7275}
7276
7277
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007278// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007279void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007280 Label max_negative_int;
7281 // the_int_ has the answer which is a signed int32 but not a Smi.
7282 // We test for the special value that has a different exponent. This test
7283 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007284 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007285 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007286 __ b(eq, &max_negative_int);
7287 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7288 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7289 uint32_t non_smi_exponent =
7290 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7291 __ mov(scratch_, Operand(non_smi_exponent));
7292 // Set the sign bit in scratch_ if the value was negative.
7293 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7294 // Subtract from 0 if the value was negative.
7295 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7296 // We should be masking the implict first digit of the mantissa away here,
7297 // but it just ends up combining harmlessly with the last digit of the
7298 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7299 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7300 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7301 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7302 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7303 __ str(scratch_, FieldMemOperand(the_heap_number_,
7304 HeapNumber::kExponentOffset));
7305 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7306 __ str(scratch_, FieldMemOperand(the_heap_number_,
7307 HeapNumber::kMantissaOffset));
7308 __ Ret();
7309
7310 __ bind(&max_negative_int);
7311 // The max negative int32 is stored as a positive number in the mantissa of
7312 // a double because it uses a sign bit instead of using two's complement.
7313 // The actual mantissa bits stored are all 0 because the implicit most
7314 // significant 1 bit is not stored.
7315 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7316 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7317 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7318 __ mov(ip, Operand(0));
7319 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7320 __ Ret();
7321}
7322
7323
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007324// Handle the case where the lhs and rhs are the same object.
7325// Equality is almost reflexive (everything but NaN), so this is a test
7326// for "identity and not NaN".
7327static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7328 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007329 Condition cc,
7330 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007331 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007332 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007333 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007334 __ b(ne, &not_identical);
7335
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007336 // The two objects are identical. If we know that one of them isn't NaN then
7337 // we now know they test equal.
7338 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007339 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7340 // so we do the second best thing - test it ourselves.
7341 // They are both equal and they are not both Smis so both of them are not
7342 // Smis. If it's not a heap number, then return equal.
7343 if (cc == lt || cc == gt) {
7344 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007345 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007346 } else {
7347 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7348 __ b(eq, &heap_number);
7349 // Comparing JS objects with <=, >= is complicated.
7350 if (cc != eq) {
7351 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7352 __ b(ge, slow);
7353 // Normally here we fall through to return_equal, but undefined is
7354 // special: (undefined == undefined) == true, but
7355 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7356 if (cc == le || cc == ge) {
7357 __ cmp(r4, Operand(ODDBALL_TYPE));
7358 __ b(ne, &return_equal);
7359 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007360 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007361 __ b(ne, &return_equal);
7362 if (cc == le) {
7363 // undefined <= undefined should fail.
7364 __ mov(r0, Operand(GREATER));
7365 } else {
7366 // undefined >= undefined should fail.
7367 __ mov(r0, Operand(LESS));
7368 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007369 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007370 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007371 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007372 }
7373 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007374
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007375 __ bind(&return_equal);
7376 if (cc == lt) {
7377 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7378 } else if (cc == gt) {
7379 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7380 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007381 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007382 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007383 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007384
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007385 if (cc != eq || !never_nan_nan) {
7386 // For less and greater we don't have to check for NaN since the result of
7387 // x < x is false regardless. For the others here is some code to check
7388 // for NaN.
7389 if (cc != lt && cc != gt) {
7390 __ bind(&heap_number);
7391 // It is a heap number, so return non-equal if it's NaN and equal if it's
7392 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007393
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007394 // The representation of NaN values has all exponent bits (52..62) set,
7395 // and not all mantissa bits (0..51) clear.
7396 // Read top bits of double representation (second word of value).
7397 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7398 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007399 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7400 // NaNs have all-one exponents so they sign extend to -1.
7401 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007402 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007403
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007404 // Shift out flag and all exponent bits, retaining only mantissa.
7405 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7406 // Or with all low-bits of mantissa.
7407 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7408 __ orr(r0, r3, Operand(r2), SetCC);
7409 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007410 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7411 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7412 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007413 if (cc != eq) {
7414 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007415 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007416 if (cc == le) {
7417 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7418 } else {
7419 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7420 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007421 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007422 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007423 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007424 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007425 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007426
7427 __ bind(&not_identical);
7428}
7429
7430
7431// See comment at call site.
7432static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007433 Register lhs,
7434 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007435 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007436 Label* slow,
7437 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007438 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7439 (lhs.is(r1) && rhs.is(r0)));
7440
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007441 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007442 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007443 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007444
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007445 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007446 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007447 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007448 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007449 // succeed. Return non-equal
7450 // If rhs is r0 then there is already a non zero value in it.
7451 if (!rhs.is(r0)) {
7452 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7453 }
7454 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007455 } else {
7456 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7457 // the runtime.
7458 __ b(ne, slow);
7459 }
7460
ager@chromium.orgb5737492010-07-15 09:29:43 +00007461 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007462 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007463 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007464 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007465 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007466 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007467 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007468 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007469 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007470 __ push(lr);
7471 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007472 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007473 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7474 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007475 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007476 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007477 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007478 }
7479
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007480 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007481 // since it's a smi.
7482 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007483
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007484 __ bind(&rhs_is_smi);
7485 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007486 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007487 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007488 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007489 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007490 // If lhs is r0 then there is already a non zero value in it.
7491 if (!lhs.is(r0)) {
7492 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7493 }
7494 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007495 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007496 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007497 // the runtime.
7498 __ b(ne, slow);
7499 }
7500
ager@chromium.orgb5737492010-07-15 09:29:43 +00007501 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007502 if (CpuFeatures::IsSupported(VFP3)) {
7503 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007504 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007505 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007506 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007507 // Convert rhs to a double in d6 .
7508 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007509 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007510 __ push(lr);
7511 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007512 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007513 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007514 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007515 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7516 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007517 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007518 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007519 // Fall through to both_loaded_as_doubles.
7520}
7521
7522
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007523void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007524 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007525 Register rhs_exponent = exp_first ? r0 : r1;
7526 Register lhs_exponent = exp_first ? r2 : r3;
7527 Register rhs_mantissa = exp_first ? r1 : r0;
7528 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007529 Label one_is_nan, neither_is_nan;
7530
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007531 __ Sbfx(r4,
7532 lhs_exponent,
7533 HeapNumber::kExponentShift,
7534 HeapNumber::kExponentBits);
7535 // NaNs have all-one exponents so they sign extend to -1.
7536 __ cmp(r4, Operand(-1));
7537 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007538 __ mov(r4,
7539 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7540 SetCC);
7541 __ b(ne, &one_is_nan);
7542 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007543 __ b(ne, &one_is_nan);
7544
7545 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007546 __ Sbfx(r4,
7547 rhs_exponent,
7548 HeapNumber::kExponentShift,
7549 HeapNumber::kExponentBits);
7550 // NaNs have all-one exponents so they sign extend to -1.
7551 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007552 __ b(ne, &neither_is_nan);
7553 __ mov(r4,
7554 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7555 SetCC);
7556 __ b(ne, &one_is_nan);
7557 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007558 __ b(eq, &neither_is_nan);
7559
7560 __ bind(&one_is_nan);
7561 // NaN comparisons always fail.
7562 // Load whatever we need in r0 to make the comparison fail.
7563 if (cc == lt || cc == le) {
7564 __ mov(r0, Operand(GREATER));
7565 } else {
7566 __ mov(r0, Operand(LESS));
7567 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007568 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007569
7570 __ bind(&neither_is_nan);
7571}
7572
7573
7574// See comment at call site.
7575static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7576 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007577 Register rhs_exponent = exp_first ? r0 : r1;
7578 Register lhs_exponent = exp_first ? r2 : r3;
7579 Register rhs_mantissa = exp_first ? r1 : r0;
7580 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007581
7582 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7583 if (cc == eq) {
7584 // Doubles are not equal unless they have the same bit pattern.
7585 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007586 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7587 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007588 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007589 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007590
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007591 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007592 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007593 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007594
7595 // Exponents are unequal. The only way we can return that the numbers
7596 // are equal is if one is -0 and the other is 0. We already dealt
7597 // with the case where both are -0 or both are 0.
7598 // We start by seeing if the mantissas (that are equal) or the bottom
7599 // 31 bits of the rhs exponent are non-zero. If so we return not
7600 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007601 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007602 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007603 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007604 // Now they are equal if and only if the lhs exponent is zero in its
7605 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007606 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007607 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007608 } else {
7609 // Call a native function to do a comparison between two non-NaNs.
7610 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007611 __ push(lr);
7612 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7613 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7614 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007615 }
7616}
7617
7618
7619// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007620static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7621 Register lhs,
7622 Register rhs) {
7623 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7624 (lhs.is(r1) && rhs.is(r0)));
7625
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007626 // If either operand is a JSObject or an oddball value, then they are
7627 // not equal since their pointers are different.
7628 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007629 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007630 Label first_non_object;
7631 // Get the type of the first operand into r2 and compare it with
7632 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007633 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007634 __ b(lt, &first_non_object);
7635
7636 // Return non-zero (r0 is not zero)
7637 Label return_not_equal;
7638 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007639 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007640
7641 __ bind(&first_non_object);
7642 // Check for oddballs: true, false, null, undefined.
7643 __ cmp(r2, Operand(ODDBALL_TYPE));
7644 __ b(eq, &return_not_equal);
7645
ager@chromium.orgb5737492010-07-15 09:29:43 +00007646 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007647 __ b(ge, &return_not_equal);
7648
7649 // Check for oddballs: true, false, null, undefined.
7650 __ cmp(r3, Operand(ODDBALL_TYPE));
7651 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007652
7653 // Now that we have the types we might as well check for symbol-symbol.
7654 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007655 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7656 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007657 __ and_(r2, r2, Operand(r3));
7658 __ tst(r2, Operand(kIsSymbolMask));
7659 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007660}
7661
7662
7663// See comment at call site.
7664static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007665 Register lhs,
7666 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007667 Label* both_loaded_as_doubles,
7668 Label* not_heap_numbers,
7669 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007670 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7671 (lhs.is(r1) && rhs.is(r0)));
7672
7673 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007674 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007675 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007676 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007677 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7678
7679 // Both are heap numbers. Load them up then jump to the code we have
7680 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007681 if (CpuFeatures::IsSupported(VFP3)) {
7682 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007683 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007684 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007685 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007686 __ vldr(d7, r7, HeapNumber::kValueOffset);
7687 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007688 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7689 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007690 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007691 __ jmp(both_loaded_as_doubles);
7692}
7693
7694
7695// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007696static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007697 Register lhs,
7698 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007699 Label* possible_strings,
7700 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007701 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7702 (lhs.is(r1) && rhs.is(r0)));
7703
7704 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007705 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007706 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007707 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007708 __ tst(r2, Operand(kIsNotStringMask));
7709 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007710 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007711 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007712 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007713 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007714 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007715 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007716
7717 // Both are symbols. We already checked they weren't the same pointer
7718 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007719 __ mov(r0, Operand(NOT_EQUAL));
7720 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007721
7722 __ bind(&object_test);
7723 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7724 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007725 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007726 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007727 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007728 // are not equal, since they are different objects and an object is not
7729 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007730 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007731 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7732 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7733 __ and_(r0, r2, Operand(r3));
7734 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7735 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007736 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007737}
7738
7739
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007740void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7741 Register object,
7742 Register result,
7743 Register scratch1,
7744 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007745 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007746 bool object_is_smi,
7747 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007748 // Use of registers. Register result is used as a temporary.
7749 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007750 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007751
7752 // Load the number string cache.
7753 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7754
7755 // Make the hash mask from the length of the number string cache. It
7756 // contains two elements (number and string) for each cache entry.
7757 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007758 // Divide length by two (length is a smi).
7759 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007760 __ sub(mask, mask, Operand(1)); // Make mask.
7761
7762 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007763 // number string cache for smis is just the smi value, and the hash for
7764 // doubles is the xor of the upper and lower words. See
7765 // Heap::GetNumberStringCache.
7766 Label is_smi;
7767 Label load_result_from_cache;
7768 if (!object_is_smi) {
7769 __ BranchOnSmi(object, &is_smi);
7770 if (CpuFeatures::IsSupported(VFP3)) {
7771 CpuFeatures::Scope scope(VFP3);
7772 __ CheckMap(object,
7773 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007774 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007775 not_found,
7776 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007777
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007778 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007779 __ add(scratch1,
7780 object,
7781 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7782 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7783 __ eor(scratch1, scratch1, Operand(scratch2));
7784 __ and_(scratch1, scratch1, Operand(mask));
7785
7786 // Calculate address of entry in string cache: each entry consists
7787 // of two pointer sized fields.
7788 __ add(scratch1,
7789 number_string_cache,
7790 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7791
7792 Register probe = mask;
7793 __ ldr(probe,
7794 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7795 __ BranchOnSmi(probe, not_found);
7796 __ sub(scratch2, object, Operand(kHeapObjectTag));
7797 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7798 __ sub(probe, probe, Operand(kHeapObjectTag));
7799 __ vldr(d1, probe, HeapNumber::kValueOffset);
7800 __ vcmp(d0, d1);
7801 __ vmrs(pc);
7802 __ b(ne, not_found); // The cache did not contain this value.
7803 __ b(&load_result_from_cache);
7804 } else {
7805 __ b(not_found);
7806 }
7807 }
7808
7809 __ bind(&is_smi);
7810 Register scratch = scratch1;
7811 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007812 // Calculate address of entry in string cache: each entry consists
7813 // of two pointer sized fields.
7814 __ add(scratch,
7815 number_string_cache,
7816 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7817
7818 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007819 Register probe = mask;
7820 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7821 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007822 __ b(ne, not_found);
7823
7824 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007825 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007826 __ ldr(result,
7827 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007828 __ IncrementCounter(&Counters::number_to_string_native,
7829 1,
7830 scratch1,
7831 scratch2);
7832}
7833
7834
7835void NumberToStringStub::Generate(MacroAssembler* masm) {
7836 Label runtime;
7837
7838 __ ldr(r1, MemOperand(sp, 0));
7839
7840 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007841 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007842 __ add(sp, sp, Operand(1 * kPointerSize));
7843 __ Ret();
7844
7845 __ bind(&runtime);
7846 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007847 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007848}
7849
7850
ager@chromium.orgac091b72010-05-05 07:34:42 +00007851void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007852 __ add(offset_, object_, Operand(offset_));
7853 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007854 __ Ret();
7855}
7856
7857
ager@chromium.orgb5737492010-07-15 09:29:43 +00007858// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007859// On exit r0 is 0, positive or negative to indicate the result of
7860// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007861void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007862 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7863 (lhs_.is(r1) && rhs_.is(r0)));
7864
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007865 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007866 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007867
7868 // NOTICE! This code is only reached after a smi-fast-case check, so
7869 // it is certain that at least one operand isn't a smi.
7870
7871 // Handle the case where the objects are identical. Either returns the answer
7872 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007873 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007874
7875 // If either is a Smi (we know that not both are), then they can only
7876 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007877 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007878 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007879 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007880 __ tst(r2, Operand(kSmiTagMask));
7881 __ b(ne, &not_smis);
7882 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7883 // 1) Return the answer.
7884 // 2) Go to slow.
7885 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007886 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007887 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007888 // comparison. If VFP3 is supported the double values of the numbers have
7889 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7890 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007891 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007892
7893 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007894 // The arguments have been converted to doubles and stored in d6 and d7, if
7895 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007896 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007897 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007898 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007899 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007900 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007901 __ vcmp(d7, d6);
7902 __ vmrs(pc); // Move vector status bits to normal status bits.
7903 Label nan;
7904 __ b(vs, &nan);
7905 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7906 __ mov(r0, Operand(LESS), LeaveCC, lt);
7907 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007908 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007909
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007910 __ bind(&nan);
7911 // If one of the sides was a NaN then the v flag is set. Load r0 with
7912 // whatever it takes to make the comparison fail, since comparisons with NaN
7913 // always fail.
7914 if (cc_ == lt || cc_ == le) {
7915 __ mov(r0, Operand(GREATER));
7916 } else {
7917 __ mov(r0, Operand(LESS));
7918 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007919 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007920 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007921 // Checks for NaN in the doubles we have loaded. Can return the answer or
7922 // fall through if neither is a NaN. Also binds lhs_not_nan.
7923 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007924 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7925 // answer. Never falls through.
7926 EmitTwoNonNanDoubleComparison(masm, cc_);
7927 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007928
7929 __ bind(&not_smis);
7930 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007931 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007932 if (strict_) {
7933 // This returns non-equal for some object types, or falls through if it
7934 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007935 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007936 }
7937
7938 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007939 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007940 // Check for heap-number-heap-number comparison. Can jump to slow case,
7941 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7942 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007943 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007944 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007945 lhs_,
7946 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007947 &both_loaded_as_doubles,
7948 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007949 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007950
7951 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007952 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7953 // symbols.
7954 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007955 // Returns an answer for two symbols or two detectable objects.
7956 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007957 // Assumes that r2 is the type of rhs_ on entry.
7958 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007959 }
7960
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007961 // Check for both being sequential ASCII strings, and inline if that is the
7962 // case.
7963 __ bind(&flat_string_check);
7964
ager@chromium.orgb5737492010-07-15 09:29:43 +00007965 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007966
7967 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7968 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007969 lhs_,
7970 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007971 r2,
7972 r3,
7973 r4,
7974 r5);
7975 // Never falls through to here.
7976
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007977 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007978
ager@chromium.orgb5737492010-07-15 09:29:43 +00007979 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007980 // Figure out which native to call and setup the arguments.
7981 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007982 if (cc_ == eq) {
7983 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7984 } else {
7985 native = Builtins::COMPARE;
7986 int ncr; // NaN compare result
7987 if (cc_ == lt || cc_ == le) {
7988 ncr = GREATER;
7989 } else {
7990 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7991 ncr = LESS;
7992 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007993 __ mov(r0, Operand(Smi::FromInt(ncr)));
7994 __ push(r0);
7995 }
7996
7997 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7998 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007999 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008000}
8001
8002
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00008003// This stub does not handle the inlined cases (Smis, Booleans, undefined).
8004// The stub returns zero for false, and a non-zero value for true.
8005void ToBooleanStub::Generate(MacroAssembler* masm) {
8006 Label false_result;
8007 Label not_heap_number;
8008 Register scratch0 = VirtualFrame::scratch0();
8009
8010 // HeapNumber => false iff +0, -0, or NaN.
8011 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8012 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
8013 __ cmp(scratch0, ip);
8014 __ b(&not_heap_number, ne);
8015
8016 __ sub(ip, tos_, Operand(kHeapObjectTag));
8017 __ vldr(d1, ip, HeapNumber::kValueOffset);
8018 __ vcmp(d1, 0.0);
8019 __ vmrs(pc);
8020 // "tos_" is a register, and contains a non zero value by default.
8021 // Hence we only need to overwrite "tos_" with zero to return false for
8022 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
8023 __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO
8024 __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN
8025 __ Ret();
8026
8027 __ bind(&not_heap_number);
8028
8029 // Check if the value is 'null'.
8030 // 'null' => false.
8031 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8032 __ cmp(tos_, ip);
8033 __ b(&false_result, eq);
8034
8035 // It can be an undetectable object.
8036 // Undetectable => false.
8037 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
8038 __ ldrb(scratch0, FieldMemOperand(ip, Map::kBitFieldOffset));
8039 __ and_(scratch0, scratch0, Operand(1 << Map::kIsUndetectable));
8040 __ cmp(scratch0, Operand(1 << Map::kIsUndetectable));
8041 __ b(&false_result, eq);
8042
8043 // JavaScript object => true.
8044 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8045 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8046 __ cmp(scratch0, Operand(FIRST_JS_OBJECT_TYPE));
8047 // "tos_" is a register and contains a non-zero value.
8048 // Hence we implicitly return true if the greater than
8049 // condition is satisfied.
8050 __ Ret(gt);
8051
8052 // Check for string
8053 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8054 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8055 __ cmp(scratch0, Operand(FIRST_NONSTRING_TYPE));
8056 // "tos_" is a register and contains a non-zero value.
8057 // Hence we implicitly return true if the greater than
8058 // condition is satisfied.
8059 __ Ret(gt);
8060
8061 // String value => false iff empty, i.e., length is zero
8062 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
8063 // If length is zero, "tos_" contains zero ==> false.
8064 // If length is not zero, "tos_" contains a non-zero value ==> true.
8065 __ Ret();
8066
8067 // Return 0 in "tos_" for false .
8068 __ bind(&false_result);
8069 __ mov(tos_, Operand(0));
8070 __ Ret();
8071}
8072
8073
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00008074// We fall into this code if the operands were Smis, but the result was
8075// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008076// the operands were not both Smi. The operands are in r0 and r1. In order
8077// to call the C-implemented binary fp operation routines we need to end up
8078// with the double precision floating point operands in r0 and r1 (for the
8079// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00008080void GenericBinaryOpStub::HandleBinaryOpSlowCases(
8081 MacroAssembler* masm,
8082 Label* not_smi,
8083 Register lhs,
8084 Register rhs,
8085 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008086 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00008087 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008088
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008089 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008090 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00008091
8092 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008093 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8094
ager@chromium.org357bf652010-04-12 11:30:10 +00008095 // Smi-smi case (overflow).
8096 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008097 // The new heap number is in r5. r3 and r7 are scratch.
8098 __ AllocateHeapNumber(
8099 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008100
8101 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
8102 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008103 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008104 CpuFeatures::Scope scope(VFP3);
8105 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
8106 __ vmov(s15, r7);
8107 __ vcvt_f64_s32(d7, s15);
8108 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
8109 __ vmov(s13, r7);
8110 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008111 if (!use_fp_registers) {
8112 __ vmov(r2, r3, d7);
8113 __ vmov(r0, r1, d6);
8114 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008115 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008116 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008117 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008118 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008119 __ push(lr);
8120 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008121 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008122 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008123 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008124 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
8125 __ pop(lr);
8126 }
8127 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008128 }
8129
ager@chromium.org357bf652010-04-12 11:30:10 +00008130 // We branch here if at least one of r0 and r1 is not a Smi.
8131 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008132 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008133
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008134 // After this point we have the left hand side in r1 and the right hand side
8135 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008136 if (lhs.is(r0)) {
8137 __ Swap(r0, r1, ip);
8138 }
8139
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008140 // The type transition also calculates the answer.
8141 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008142
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008143 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008144 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
8145 switch (op_) {
8146 case Token::ADD:
8147 case Token::SUB:
8148 case Token::MUL:
8149 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008150 GenerateTypeTransition(masm); // Tail call.
8151 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00008152 break;
8153
8154 default:
8155 break;
8156 }
8157 }
8158
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008159 if (generate_code_to_calculate_answer) {
8160 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
8161 if (mode_ == NO_OVERWRITE) {
8162 // In the case where there is no chance of an overwritable float we may
8163 // as well do the allocation immediately while r0 and r1 are untouched.
8164 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008165 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008166
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008167 // Move r0 to a double in r2-r3.
8168 __ tst(r0, Operand(kSmiTagMask));
8169 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
8170 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8171 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8172 __ cmp(r4, heap_number_map);
8173 __ b(ne, &slow);
8174 if (mode_ == OVERWRITE_RIGHT) {
8175 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008176 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008177 if (use_fp_registers) {
8178 CpuFeatures::Scope scope(VFP3);
8179 // Load the double from tagged HeapNumber r0 to d7.
8180 __ sub(r7, r0, Operand(kHeapObjectTag));
8181 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00008182 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008183 // Calling convention says that second double is in r2 and r3.
8184 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00008185 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008186 __ jmp(&finished_loading_r0);
8187 __ bind(&r0_is_smi);
8188 if (mode_ == OVERWRITE_RIGHT) {
8189 // We can't overwrite a Smi so get address of new heap number into r5.
8190 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8191 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008192
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008193 if (CpuFeatures::IsSupported(VFP3)) {
8194 CpuFeatures::Scope scope(VFP3);
8195 // Convert smi in r0 to double in d7.
8196 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
8197 __ vmov(s15, r7);
8198 __ vcvt_f64_s32(d7, s15);
8199 if (!use_fp_registers) {
8200 __ vmov(r2, r3, d7);
8201 }
8202 } else {
8203 // Write Smi from r0 to r3 and r2 in double format.
8204 __ mov(r7, Operand(r0));
8205 ConvertToDoubleStub stub3(r3, r2, r7, r4);
8206 __ push(lr);
8207 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
8208 __ pop(lr);
8209 }
8210
8211 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
8212 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
8213 Label r1_is_not_smi;
8214 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
8215 __ tst(r1, Operand(kSmiTagMask));
8216 __ b(ne, &r1_is_not_smi);
8217 GenerateTypeTransition(masm); // Tail call.
8218 }
8219
8220 __ bind(&finished_loading_r0);
8221
8222 // Move r1 to a double in r0-r1.
8223 __ tst(r1, Operand(kSmiTagMask));
8224 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
8225 __ bind(&r1_is_not_smi);
8226 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
8227 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8228 __ cmp(r4, heap_number_map);
8229 __ b(ne, &slow);
8230 if (mode_ == OVERWRITE_LEFT) {
8231 __ mov(r5, Operand(r1)); // Overwrite this heap number.
8232 }
8233 if (use_fp_registers) {
8234 CpuFeatures::Scope scope(VFP3);
8235 // Load the double from tagged HeapNumber r1 to d6.
8236 __ sub(r7, r1, Operand(kHeapObjectTag));
8237 __ vldr(d6, r7, HeapNumber::kValueOffset);
8238 } else {
8239 // Calling convention says that first double is in r0 and r1.
8240 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
8241 }
8242 __ jmp(&finished_loading_r1);
8243 __ bind(&r1_is_smi);
8244 if (mode_ == OVERWRITE_LEFT) {
8245 // We can't overwrite a Smi so get address of new heap number into r5.
8246 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8247 }
8248
8249 if (CpuFeatures::IsSupported(VFP3)) {
8250 CpuFeatures::Scope scope(VFP3);
8251 // Convert smi in r1 to double in d6.
8252 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
8253 __ vmov(s13, r7);
8254 __ vcvt_f64_s32(d6, s13);
8255 if (!use_fp_registers) {
8256 __ vmov(r0, r1, d6);
8257 }
8258 } else {
8259 // Write Smi from r1 to r1 and r0 in double format.
8260 __ mov(r7, Operand(r1));
8261 ConvertToDoubleStub stub4(r1, r0, r7, r9);
8262 __ push(lr);
8263 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
8264 __ pop(lr);
8265 }
8266
8267 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00008268 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008269
8270 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
8271 __ bind(&do_the_call);
8272 // If we are inlining the operation using VFP3 instructions for
8273 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
8274 if (use_fp_registers) {
8275 CpuFeatures::Scope scope(VFP3);
8276 // ARMv7 VFP3 instructions to implement
8277 // double precision, add, subtract, multiply, divide.
8278
8279 if (Token::MUL == op_) {
8280 __ vmul(d5, d6, d7);
8281 } else if (Token::DIV == op_) {
8282 __ vdiv(d5, d6, d7);
8283 } else if (Token::ADD == op_) {
8284 __ vadd(d5, d6, d7);
8285 } else if (Token::SUB == op_) {
8286 __ vsub(d5, d6, d7);
8287 } else {
8288 UNREACHABLE();
8289 }
8290 __ sub(r0, r5, Operand(kHeapObjectTag));
8291 __ vstr(d5, r0, HeapNumber::kValueOffset);
8292 __ add(r0, r0, Operand(kHeapObjectTag));
8293 __ mov(pc, lr);
8294 } else {
8295 // If we did not inline the operation, then the arguments are in:
8296 // r0: Left value (least significant part of mantissa).
8297 // r1: Left value (sign, exponent, top of mantissa).
8298 // r2: Right value (least significant part of mantissa).
8299 // r3: Right value (sign, exponent, top of mantissa).
8300 // r5: Address of heap number for result.
8301
8302 __ push(lr); // For later.
8303 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
8304 // Call C routine that may not cause GC or other trouble. r5 is callee
8305 // save.
8306 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
8307 // Store answer in the overwritable heap number.
8308 #if !defined(USE_ARM_EABI)
8309 // Double returned in fp coprocessor register 0 and 1, encoded as
8310 // register cr8. Offsets must be divisible by 4 for coprocessor so we
8311 // need to substract the tag from r5.
8312 __ sub(r4, r5, Operand(kHeapObjectTag));
8313 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
8314 #else
8315 // Double returned in registers 0 and 1.
8316 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8317 #endif
8318 __ mov(r0, Operand(r5));
8319 // And we are done.
8320 __ pop(pc);
8321 }
8322 }
8323 }
8324
8325 if (!generate_code_to_calculate_answer &&
8326 !slow_reverse.is_linked() &&
8327 !slow.is_linked()) {
8328 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008329 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008330
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008331 if (lhs.is(r0)) {
8332 __ b(&slow);
8333 __ bind(&slow_reverse);
8334 __ Swap(r0, r1, ip);
8335 }
8336
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008337 heap_number_map = no_reg; // Don't use this any more from here on.
8338
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008339 // We jump to here if something goes wrong (one param is not a number of any
8340 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008341 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008342
8343 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008344 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008345
ager@chromium.org357bf652010-04-12 11:30:10 +00008346 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008347 // Test for string arguments before calling runtime.
8348 // r1 : first argument
8349 // r0 : second argument
8350 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008351 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008352
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008353 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008354 __ tst(r1, Operand(kSmiTagMask));
8355 __ b(eq, &not_string1);
8356 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8357 __ b(ge, &not_string1);
8358
8359 // First argument is a a string, test second.
8360 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008361 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008362 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8363 __ b(ge, &string1);
8364
8365 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008366 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8367 __ TailCallStub(&string_add_stub);
8368
8369 __ bind(&string1_smi2);
8370 // First argument is a string, second is a smi. Try to lookup the number
8371 // string for the smi in the number string cache.
8372 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008373 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008374
8375 // Replace second argument on stack and tailcall string add stub to make
8376 // the result.
8377 __ str(r2, MemOperand(sp, 0));
8378 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008379
8380 // Only first argument is a string.
8381 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008382 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8383
8384 // First argument was not a string, test second.
8385 __ bind(&not_string1);
8386 __ tst(r0, Operand(kSmiTagMask));
8387 __ b(eq, &not_strings);
8388 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8389 __ b(ge, &not_strings);
8390
8391 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008392 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8393
8394 __ bind(&not_strings);
8395 }
8396
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008397 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008398}
8399
8400
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008401// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008402// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008403// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8404// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008405// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8406// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008407static void GetInt32(MacroAssembler* masm,
8408 Register source,
8409 Register dest,
8410 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008411 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008412 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008413 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008414 // Get exponent word.
8415 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8416 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008417 __ Ubfx(scratch2,
8418 scratch,
8419 HeapNumber::kExponentShift,
8420 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008421 // Load dest with zero. We use this either for the final shift or
8422 // for the answer.
8423 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008424 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008425 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8426 // the exponent that we are fastest at and also the highest exponent we can
8427 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008428 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8429 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8430 // split it up to avoid a constant pool entry. You can't do that in general
8431 // for cmp because of the overflow flag, but we know the exponent is in the
8432 // range 0-2047 so there is no overflow.
8433 int fudge_factor = 0x400;
8434 __ sub(scratch2, scratch2, Operand(fudge_factor));
8435 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008436 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8437 __ b(eq, &right_exponent);
8438 // If the exponent is higher than that then go to slow case. This catches
8439 // numbers that don't fit in a signed int32, infinities and NaNs.
8440 __ b(gt, slow);
8441
8442 // We know the exponent is smaller than 30 (biased). If it is less than
8443 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8444 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008445 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8446 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008447 // Dest already has a Smi zero.
8448 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008449 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008450 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8451 // get how much to shift down.
8452 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008453 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008454 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008455 if (CpuFeatures::IsSupported(VFP3)) {
8456 CpuFeatures::Scope scope(VFP3);
8457 // ARMv7 VFP3 instructions implementing double precision to integer
8458 // conversion using round to zero.
8459 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008460 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008461 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008462 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008463 } else {
8464 // Get the top bits of the mantissa.
8465 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8466 // Put back the implicit 1.
8467 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8468 // Shift up the mantissa bits to take up the space the exponent used to
8469 // take. We just orred in the implicit bit so that took care of one and
8470 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8471 // distance.
8472 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8473 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8474 // Put sign in zero flag.
8475 __ tst(scratch, Operand(HeapNumber::kSignMask));
8476 // Get the second half of the double. For some exponents we don't
8477 // actually need this because the bits get shifted out again, but
8478 // it's probably slower to test than just to do it.
8479 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8480 // Shift down 22 bits to get the last 10 bits.
8481 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8482 // Move down according to the exponent.
8483 __ mov(dest, Operand(scratch, LSR, dest));
8484 // Fix sign if sign bit was set.
8485 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8486 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008487 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008488}
8489
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008490// For bitwise ops where the inputs are not both Smis we here try to determine
8491// whether both inputs are either Smis or at least heap numbers that can be
8492// represented by a 32 bit signed value. We truncate towards zero as required
8493// by the ES spec. If this is the case we do the bitwise op and see if the
8494// result is a Smi. If so, great, otherwise we try to find a heap number to
8495// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008496// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8497void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8498 Register lhs,
8499 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008500 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008501 Label rhs_is_smi, lhs_is_smi;
8502 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008503
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008504 Register heap_number_map = r6;
8505 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8506
ager@chromium.org357bf652010-04-12 11:30:10 +00008507 __ tst(lhs, Operand(kSmiTagMask));
8508 __ 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 +00008509 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8510 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008511 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008512 GetInt32(masm, lhs, r3, r5, r4, &slow);
8513 __ jmp(&done_checking_lhs);
8514 __ bind(&lhs_is_smi);
8515 __ mov(r3, Operand(lhs, ASR, 1));
8516 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008517
ager@chromium.org357bf652010-04-12 11:30:10 +00008518 __ tst(rhs, Operand(kSmiTagMask));
8519 __ 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 +00008520 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8521 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008522 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008523 GetInt32(masm, rhs, r2, r5, r4, &slow);
8524 __ jmp(&done_checking_rhs);
8525 __ bind(&rhs_is_smi);
8526 __ mov(r2, Operand(rhs, ASR, 1));
8527 __ bind(&done_checking_rhs);
8528
8529 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008530
8531 // r0 and r1: Original operands (Smi or heap numbers).
8532 // r2 and r3: Signed int32 operands.
8533 switch (op_) {
8534 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8535 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8536 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8537 case Token::SAR:
8538 // Use only the 5 least significant bits of the shift count.
8539 __ and_(r2, r2, Operand(0x1f));
8540 __ mov(r2, Operand(r3, ASR, r2));
8541 break;
8542 case Token::SHR:
8543 // Use only the 5 least significant bits of the shift count.
8544 __ and_(r2, r2, Operand(0x1f));
8545 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8546 // SHR is special because it is required to produce a positive answer.
8547 // The code below for writing into heap numbers isn't capable of writing
8548 // the register as an unsigned int so we go to slow case if we hit this
8549 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008550 if (CpuFeatures::IsSupported(VFP3)) {
8551 __ b(mi, &result_not_a_smi);
8552 } else {
8553 __ b(mi, &slow);
8554 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008555 break;
8556 case Token::SHL:
8557 // Use only the 5 least significant bits of the shift count.
8558 __ and_(r2, r2, Operand(0x1f));
8559 __ mov(r2, Operand(r3, LSL, r2));
8560 break;
8561 default: UNREACHABLE();
8562 }
8563 // check that the *signed* result fits in a smi
8564 __ add(r3, r2, Operand(0x40000000), SetCC);
8565 __ b(mi, &result_not_a_smi);
8566 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8567 __ Ret();
8568
8569 Label have_to_allocate, got_a_heap_number;
8570 __ bind(&result_not_a_smi);
8571 switch (mode_) {
8572 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008573 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008574 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008575 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008576 break;
8577 }
8578 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008579 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008580 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008581 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008582 break;
8583 }
8584 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008585 // Get a new heap number in r5. r4 and r7 are scratch.
8586 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008587 }
8588 default: break;
8589 }
8590 __ bind(&got_a_heap_number);
8591 // r2: Answer as signed int32.
8592 // r5: Heap number to write answer into.
8593
8594 // Nothing can go wrong now, so move the heap number to r0, which is the
8595 // result.
8596 __ mov(r0, Operand(r5));
8597
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008598 if (CpuFeatures::IsSupported(VFP3)) {
8599 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8600 CpuFeatures::Scope scope(VFP3);
8601 __ vmov(s0, r2);
8602 if (op_ == Token::SHR) {
8603 __ vcvt_f64_u32(d0, s0);
8604 } else {
8605 __ vcvt_f64_s32(d0, s0);
8606 }
8607 __ sub(r3, r0, Operand(kHeapObjectTag));
8608 __ vstr(d0, r3, HeapNumber::kValueOffset);
8609 __ Ret();
8610 } else {
8611 // Tail call that writes the int32 in r2 to the heap number in r0, using
8612 // r3 as scratch. r0 is preserved and returned.
8613 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8614 __ TailCallStub(&stub);
8615 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008616
8617 if (mode_ != NO_OVERWRITE) {
8618 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008619 // Get a new heap number in r5. r4 and r7 are scratch.
8620 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008621 __ jmp(&got_a_heap_number);
8622 }
8623
8624 // If all else failed then we go to the runtime system.
8625 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008626 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008627 switch (op_) {
8628 case Token::BIT_OR:
8629 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8630 break;
8631 case Token::BIT_AND:
8632 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8633 break;
8634 case Token::BIT_XOR:
8635 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8636 break;
8637 case Token::SAR:
8638 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8639 break;
8640 case Token::SHR:
8641 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8642 break;
8643 case Token::SHL:
8644 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8645 break;
8646 default:
8647 UNREACHABLE();
8648 }
8649}
8650
8651
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008652// Can we multiply by x with max two shifts and an add.
8653// This answers yes to all integers from 2 to 10.
8654static bool IsEasyToMultiplyBy(int x) {
8655 if (x < 2) return false; // Avoid special cases.
8656 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8657 if (IsPowerOf2(x)) return true; // Simple shift.
8658 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8659 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8660 return false;
8661}
8662
8663
8664// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8665// Source and destination may be the same register. This routine does
8666// not set carry and overflow the way a mul instruction would.
8667static void MultiplyByKnownInt(MacroAssembler* masm,
8668 Register source,
8669 Register destination,
8670 int known_int) {
8671 if (IsPowerOf2(known_int)) {
8672 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8673 } else if (PopCountLessThanEqual2(known_int)) {
8674 int first_bit = BitPosition(known_int);
8675 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8676 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8677 if (first_bit != 0) {
8678 __ mov(destination, Operand(destination, LSL, first_bit));
8679 }
8680 } else {
8681 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8682 int the_bit = BitPosition(known_int + 1);
8683 __ rsb(destination, source, Operand(source, LSL, the_bit));
8684 }
8685}
8686
8687
8688// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8689// a register for the cases where it doesn't know a good trick, and may deliver
8690// a result that needs shifting.
8691static void MultiplyByKnownInt2(
8692 MacroAssembler* masm,
8693 Register result,
8694 Register source,
8695 Register known_int_register, // Smi tagged.
8696 int known_int,
8697 int* required_shift) { // Including Smi tag shift
8698 switch (known_int) {
8699 case 3:
8700 __ add(result, source, Operand(source, LSL, 1));
8701 *required_shift = 1;
8702 break;
8703 case 5:
8704 __ add(result, source, Operand(source, LSL, 2));
8705 *required_shift = 1;
8706 break;
8707 case 6:
8708 __ add(result, source, Operand(source, LSL, 1));
8709 *required_shift = 2;
8710 break;
8711 case 7:
8712 __ rsb(result, source, Operand(source, LSL, 3));
8713 *required_shift = 1;
8714 break;
8715 case 9:
8716 __ add(result, source, Operand(source, LSL, 3));
8717 *required_shift = 1;
8718 break;
8719 case 10:
8720 __ add(result, source, Operand(source, LSL, 2));
8721 *required_shift = 2;
8722 break;
8723 default:
8724 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8725 __ mul(result, source, known_int_register);
8726 *required_shift = 0;
8727 }
8728}
8729
8730
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008731// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8732// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8733// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8734// number from 0 to mask. On exit the 'eq' condition flags are set if the
8735// answer is exactly the mask.
8736void IntegerModStub::DigitSum(MacroAssembler* masm,
8737 Register lhs,
8738 int mask,
8739 int shift,
8740 Label* entry) {
8741 ASSERT(mask > 0);
8742 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8743 Label loop;
8744 __ bind(&loop);
8745 __ and_(ip, lhs, Operand(mask));
8746 __ add(lhs, ip, Operand(lhs, LSR, shift));
8747 __ bind(entry);
8748 __ cmp(lhs, Operand(mask));
8749 __ b(gt, &loop);
8750}
8751
8752
8753void IntegerModStub::DigitSum(MacroAssembler* masm,
8754 Register lhs,
8755 Register scratch,
8756 int mask,
8757 int shift1,
8758 int shift2,
8759 Label* entry) {
8760 ASSERT(mask > 0);
8761 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8762 Label loop;
8763 __ bind(&loop);
8764 __ bic(scratch, lhs, Operand(mask));
8765 __ and_(ip, lhs, Operand(mask));
8766 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8767 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8768 __ bind(entry);
8769 __ cmp(lhs, Operand(mask));
8770 __ b(gt, &loop);
8771}
8772
8773
8774// Splits the number into two halves (bottom half has shift bits). The top
8775// half is subtracted from the bottom half. If the result is negative then
8776// rhs is added.
8777void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8778 Register lhs,
8779 int shift,
8780 int rhs) {
8781 int mask = (1 << shift) - 1;
8782 __ and_(ip, lhs, Operand(mask));
8783 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8784 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8785}
8786
8787
8788void IntegerModStub::ModReduce(MacroAssembler* masm,
8789 Register lhs,
8790 int max,
8791 int denominator) {
8792 int limit = denominator;
8793 while (limit * 2 <= max) limit *= 2;
8794 while (limit >= denominator) {
8795 __ cmp(lhs, Operand(limit));
8796 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8797 limit >>= 1;
8798 }
8799}
8800
8801
8802void IntegerModStub::ModAnswer(MacroAssembler* masm,
8803 Register result,
8804 Register shift_distance,
8805 Register mask_bits,
8806 Register sum_of_digits) {
8807 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8808 __ Ret();
8809}
8810
8811
8812// See comment for class.
8813void IntegerModStub::Generate(MacroAssembler* masm) {
8814 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8815 __ bic(odd_number_, odd_number_, Operand(1));
8816 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8817 // We now have (odd_number_ - 1) * 2 in the register.
8818 // Build a switch out of branches instead of data because it avoids
8819 // having to teach the assembler about intra-code-object pointers
8820 // that are not in relative branch instructions.
8821 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8822 Label mod21, mod23, mod25;
8823 { Assembler::BlockConstPoolScope block_const_pool(masm);
8824 __ add(pc, pc, Operand(odd_number_));
8825 // When you read pc it is always 8 ahead, but when you write it you always
8826 // write the actual value. So we put in two nops to take up the slack.
8827 __ nop();
8828 __ nop();
8829 __ b(&mod3);
8830 __ b(&mod5);
8831 __ b(&mod7);
8832 __ b(&mod9);
8833 __ b(&mod11);
8834 __ b(&mod13);
8835 __ b(&mod15);
8836 __ b(&mod17);
8837 __ b(&mod19);
8838 __ b(&mod21);
8839 __ b(&mod23);
8840 __ b(&mod25);
8841 }
8842
8843 // For each denominator we find a multiple that is almost only ones
8844 // when expressed in binary. Then we do the sum-of-digits trick for
8845 // that number. If the multiple is not 1 then we have to do a little
8846 // more work afterwards to get the answer into the 0-denominator-1
8847 // range.
8848 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8849 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8850 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8851
8852 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8853 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8854 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8855
8856 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8857 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8858 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8859
8860 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8861 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8862 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8863
8864 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8865 ModReduce(masm, lhs_, 0x3f, 11);
8866 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8867
8868 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8869 ModReduce(masm, lhs_, 0xff, 13);
8870 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8871
8872 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8873 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8874 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8875
8876 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8877 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8878 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8879
8880 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8881 ModReduce(masm, lhs_, 0xff, 19);
8882 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8883
8884 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8885 ModReduce(masm, lhs_, 0x3f, 21);
8886 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8887
8888 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8889 ModReduce(masm, lhs_, 0xff, 23);
8890 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8891
8892 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8893 ModReduce(masm, lhs_, 0x7f, 25);
8894 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8895}
8896
8897
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008898const char* GenericBinaryOpStub::GetName() {
8899 if (name_ != NULL) return name_;
8900 const int len = 100;
8901 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8902 if (name_ == NULL) return "OOM";
8903 const char* op_name = Token::Name(op_);
8904 const char* overwrite_name;
8905 switch (mode_) {
8906 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8907 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8908 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8909 default: overwrite_name = "UnknownOverwrite"; break;
8910 }
8911
8912 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008913 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008914 op_name,
8915 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008916 specialized_on_rhs_ ? "_ConstantRhs" : "",
8917 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008918 return name_;
8919}
8920
8921
ager@chromium.org5c838252010-02-19 08:53:10 +00008922
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008923void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008924 // lhs_ : x
8925 // rhs_ : y
8926 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008927
ager@chromium.org357bf652010-04-12 11:30:10 +00008928 Register result = r0;
8929 Register lhs = lhs_;
8930 Register rhs = rhs_;
8931
8932 // This code can't cope with other register allocations yet.
8933 ASSERT(result.is(r0) &&
8934 ((lhs.is(r0) && rhs.is(r1)) ||
8935 (lhs.is(r1) && rhs.is(r0))));
8936
8937 Register smi_test_reg = VirtualFrame::scratch0();
8938 Register scratch = VirtualFrame::scratch1();
8939
8940 // All ops need to know whether we are dealing with two Smis. Set up
8941 // smi_test_reg to tell us that.
8942 if (ShouldGenerateSmiCode()) {
8943 __ orr(smi_test_reg, lhs, Operand(rhs));
8944 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008945
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008946 switch (op_) {
8947 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008948 Label not_smi;
8949 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008950 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008951 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008952 __ tst(smi_test_reg, Operand(kSmiTagMask));
8953 __ b(ne, &not_smi);
8954 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8955 // Return if no overflow.
8956 __ Ret(vc);
8957 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8958 }
8959 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008960 break;
8961 }
8962
8963 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008964 Label not_smi;
8965 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008966 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008967 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008968 __ tst(smi_test_reg, Operand(kSmiTagMask));
8969 __ b(ne, &not_smi);
8970 if (lhs.is(r1)) {
8971 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8972 // Return if no overflow.
8973 __ Ret(vc);
8974 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8975 } else {
8976 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8977 // Return if no overflow.
8978 __ Ret(vc);
8979 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8980 }
8981 }
8982 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008983 break;
8984 }
8985
8986 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008987 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008988 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008989 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008990 __ tst(smi_test_reg, Operand(kSmiTagMask));
8991 Register scratch2 = smi_test_reg;
8992 smi_test_reg = no_reg;
8993 __ b(ne, &not_smi);
8994 // Remove tag from one operand (but keep sign), so that result is Smi.
8995 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8996 // Do multiplication
8997 // scratch = lower 32 bits of ip * lhs.
8998 __ smull(scratch, scratch2, lhs, ip);
8999 // Go slow on overflows (overflow bit is not set).
9000 __ mov(ip, Operand(scratch, ASR, 31));
9001 // No overflow if higher 33 bits are identical.
9002 __ cmp(ip, Operand(scratch2));
9003 __ b(ne, &slow);
9004 // Go slow on zero result to handle -0.
9005 __ tst(scratch, Operand(scratch));
9006 __ mov(result, Operand(scratch), LeaveCC, ne);
9007 __ Ret(ne);
9008 // We need -0 if we were multiplying a negative number with 0 to get 0.
9009 // We know one of them was zero.
9010 __ add(scratch2, rhs, Operand(lhs), SetCC);
9011 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
9012 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
9013 // Slow case. We fall through here if we multiplied a negative number
9014 // with 0, because that would mean we should produce -0.
9015 __ bind(&slow);
9016 }
9017 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009018 break;
9019 }
9020
9021 case Token::DIV:
9022 case Token::MOD: {
9023 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00009024 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009025 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00009026 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009027 if (IsPowerOf2(constant_rhs_)) {
9028 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009029 __ and_(rhs,
9030 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009031 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
9032 SetCC);
9033 // We now have the answer, but if the input was negative we also
9034 // have the sign bit. Our work is done if the result is
9035 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00009036 if (!rhs.is(r0)) {
9037 __ mov(r0, rhs, LeaveCC, pl);
9038 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009039 __ Ret(pl);
9040 // A mod of a negative left hand side must return a negative number.
9041 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00009042 // already optimistically trashed rhs so we may need to restore it.
9043 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009044 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00009045 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009046 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009047 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00009048 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009049 } else {
9050 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00009051 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009052 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009053 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009054 int shift = 0;
9055 int d = constant_rhs_;
9056 while ((d & 1) == 0) {
9057 d >>= 1;
9058 shift++;
9059 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009060 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009061 __ bic(r0, r0, Operand(kSmiTagMask));
9062 }
9063 } else {
9064 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00009065 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009066 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009067 // Find a fixed point reciprocal of the divisor so we can divide by
9068 // multiplying.
9069 double divisor = 1.0 / constant_rhs_;
9070 int shift = 32;
9071 double scale = 4294967296.0; // 1 << 32.
9072 uint32_t mul;
9073 // Maximise the precision of the fixed point reciprocal.
9074 while (true) {
9075 mul = static_cast<uint32_t>(scale * divisor);
9076 if (mul >= 0x7fffffff) break;
9077 scale *= 2.0;
9078 shift++;
9079 }
9080 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00009081 Register scratch2 = smi_test_reg;
9082 smi_test_reg = no_reg;
9083 __ mov(scratch2, Operand(mul));
9084 __ umull(scratch, scratch2, scratch2, lhs);
9085 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
9086 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
9087 // rhs is still the known rhs. rhs is Smi tagged.
9088 // lhs is still the unkown lhs. lhs is Smi tagged.
9089 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
9090 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009091 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00009092 scratch,
9093 scratch2,
9094 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009095 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00009096 &required_scratch_shift);
9097 // scratch << required_scratch_shift is now the Smi tagged rhs *
9098 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009099 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009100 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009101 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00009102 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009103 } else {
9104 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00009105 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009106 }
9107 }
9108 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009109 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009110 } else if (op_ == Token::MOD &&
9111 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
9112 runtime_operands_type_ != BinaryOpIC::STRINGS) {
9113 // Do generate a bit of smi code for modulus even though the default for
9114 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009115 // support for modulus checking for smis makes sense. We can handle
9116 // 1 to 25 times any power of 2. This covers over half the numbers from
9117 // 1 to 100 including all of the first 25. (Actually the constants < 10
9118 // are handled above by reciprocal multiplication. We only get here for
9119 // those cases if the right hand side is not a constant or for cases
9120 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
9121 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009122 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009123 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009124 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009125 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009126 // Check for two positive smis.
9127 __ orr(smi_test_reg, lhs, Operand(rhs));
9128 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
9129 __ b(ne, &slow);
9130 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009131 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009132 __ sub(scratch, rhs, Operand(1), SetCC);
9133 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009134 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
9135 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009136 // Calculate power of two modulus.
9137 __ and_(result, lhs, Operand(scratch));
9138 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009139
9140 __ bind(&not_power_of_2);
9141 __ eor(scratch, scratch, Operand(mask_bits));
9142 // At least two bits are set in the modulus. The high one(s) are in
9143 // mask_bits and the low one is scratch + 1.
9144 __ and_(mask_bits, scratch, Operand(lhs));
9145 Register shift_distance = scratch;
9146 scratch = no_reg;
9147
9148 // The rhs consists of a power of 2 multiplied by some odd number.
9149 // The power-of-2 part we handle by putting the corresponding bits
9150 // from the lhs in the mask_bits register, and the power in the
9151 // shift_distance register. Shift distance is never 0 due to Smi
9152 // tagging.
9153 __ CountLeadingZeros(r4, shift_distance, shift_distance);
9154 __ rsb(shift_distance, r4, Operand(32));
9155
9156 // Now we need to find out what the odd number is. The last bit is
9157 // always 1.
9158 Register odd_number = r4;
9159 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
9160 __ cmp(odd_number, Operand(25));
9161 __ b(gt, &slow);
9162
9163 IntegerModStub stub(
9164 result, shift_distance, odd_number, mask_bits, lhs, r5);
9165 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
9166
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009167 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009168 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009169 HandleBinaryOpSlowCases(
9170 masm,
9171 &not_smi,
9172 lhs,
9173 rhs,
9174 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009175 break;
9176 }
9177
9178 case Token::BIT_OR:
9179 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009180 case Token::BIT_XOR:
9181 case Token::SAR:
9182 case Token::SHR:
9183 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009184 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009185 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00009186 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009187 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009188 Register scratch2 = smi_test_reg;
9189 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009190 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009191 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
9192 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
9193 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009194 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009195 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00009196 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9197 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009198 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009199 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009200 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009201 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009202 // Remove tags from operands. We can't do this on a 31 bit number
9203 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00009204 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9205 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9206 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009207 // Unsigned shift is not allowed to produce a negative number, so
9208 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00009209 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009210 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009211 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009212 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009213 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009214 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009215 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00009216 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9217 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9218 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009219 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00009220 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009221 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009222 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009223 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009224 default: UNREACHABLE();
9225 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009226 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009227 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009228 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009229 break;
9230 }
9231
9232 default: UNREACHABLE();
9233 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009234 // This code should be unreachable.
9235 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00009236
9237 // Generate an unreachable reference to the DEFAULT stub so that it can be
9238 // found at the end of this stub when clearing ICs at GC.
9239 // TODO(kaznacheev): Check performance impact and get rid of this.
9240 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
9241 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
9242 __ CallStub(&uninit);
9243 }
9244}
9245
9246
9247void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
9248 Label get_result;
9249
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009250 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009251
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009252 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
9253 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00009254 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009255 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009256
9257 __ TailCallExternalReference(
9258 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009259 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00009260 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009261}
9262
9263
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009264Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009265 GenericBinaryOpStub stub(key, type_info);
9266 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009267}
9268
9269
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009270void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9271 // Argument is a number and is on stack and in r0.
9272 Label runtime_call;
9273 Label input_not_smi;
9274 Label loaded;
9275
9276 if (CpuFeatures::IsSupported(VFP3)) {
9277 // Load argument and check if it is a smi.
9278 __ BranchOnNotSmi(r0, &input_not_smi);
9279
9280 CpuFeatures::Scope scope(VFP3);
9281 // Input is a smi. Convert to double and load the low and high words
9282 // of the double into r2, r3.
9283 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
9284 __ b(&loaded);
9285
9286 __ bind(&input_not_smi);
9287 // Check if input is a HeapNumber.
9288 __ CheckMap(r0,
9289 r1,
9290 Heap::kHeapNumberMapRootIndex,
9291 &runtime_call,
9292 true);
9293 // Input is a HeapNumber. Load it to a double register and store the
9294 // low and high words into r2, r3.
9295 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
9296
9297 __ bind(&loaded);
9298 // r2 = low 32 bits of double value
9299 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009300 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009301 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
9302 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009303 __ eor(r1, r1, Operand(r1, ASR, 16));
9304 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009305 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009306 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009307
9308 // r2 = low 32 bits of double value.
9309 // r3 = high 32 bits of double value.
9310 // r1 = TranscendentalCache::hash(double value).
9311 __ mov(r0,
9312 Operand(ExternalReference::transcendental_cache_array_address()));
9313 // r0 points to cache array.
9314 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
9315 // r0 points to the cache for the type type_.
9316 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9317 __ cmp(r0, Operand(0));
9318 __ b(eq, &runtime_call);
9319
9320#ifdef DEBUG
9321 // Check that the layout of cache elements match expectations.
9322 { TranscendentalCache::Element test_elem[2];
9323 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9324 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9325 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9326 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9327 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9328 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9329 CHECK_EQ(0, elem_in0 - elem_start);
9330 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9331 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9332 }
9333#endif
9334
9335 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9336 __ add(r1, r1, Operand(r1, LSL, 1));
9337 __ add(r0, r0, Operand(r1, LSL, 2));
9338 // Check if cache matches: Double value is stored in uint32_t[2] array.
9339 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9340 __ cmp(r2, r4);
9341 __ b(ne, &runtime_call);
9342 __ cmp(r3, r5);
9343 __ b(ne, &runtime_call);
9344 // Cache hit. Load result, pop argument and return.
9345 __ mov(r0, Operand(r6));
9346 __ pop();
9347 __ Ret();
9348 }
9349
9350 __ bind(&runtime_call);
9351 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9352}
9353
9354
9355Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9356 switch (type_) {
9357 // Add more cases when necessary.
9358 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9359 case TranscendentalCache::COS: return Runtime::kMath_cos;
9360 default:
9361 UNIMPLEMENTED();
9362 return Runtime::kAbort;
9363 }
9364}
9365
9366
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009367void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009368 // Do tail-call to runtime routine. Runtime routines expect at least one
9369 // argument, so give it a Smi.
9370 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009371 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009372 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009373
9374 __ StubReturn(1);
9375}
9376
9377
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009378void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009379 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009380
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009381 Register heap_number_map = r6;
9382 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9383
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009384 if (op_ == Token::SUB) {
9385 // Check whether the value is a smi.
9386 Label try_float;
9387 __ tst(r0, Operand(kSmiTagMask));
9388 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009389
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009390 // Go slow case if the value of the expression is zero
9391 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009392 if (negative_zero_ == kStrictNegativeZero) {
9393 // If we have to check for zero, then we can check for the max negative
9394 // smi while we are at it.
9395 __ bic(ip, r0, Operand(0x80000000), SetCC);
9396 __ b(eq, &slow);
9397 __ rsb(r0, r0, Operand(0));
9398 __ StubReturn(1);
9399 } else {
9400 // The value of the expression is a smi and 0 is OK for -0. Try
9401 // optimistic subtraction '0 - value'.
9402 __ rsb(r0, r0, Operand(0), SetCC);
9403 __ StubReturn(1, vc);
9404 // We don't have to reverse the optimistic neg since the only case
9405 // where we fall through is the minimum negative Smi, which is the case
9406 // where the neg leaves the register unchanged.
9407 __ jmp(&slow); // Go slow on max negative Smi.
9408 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009409
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009410 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009411 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9412 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9413 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009414 __ b(ne, &slow);
9415 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009416 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009417 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9418 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9419 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9420 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009421 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009422 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9423 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9424 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9425 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9426 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9427 __ mov(r0, Operand(r1));
9428 }
9429 } else if (op_ == Token::BIT_NOT) {
9430 // Check if the operand is a heap number.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009431 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9432 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9433 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009434 __ b(ne, &slow);
9435
9436 // Convert the heap number is r0 to an untagged integer in r1.
9437 GetInt32(masm, r0, r1, r2, r3, &slow);
9438
9439 // Do the bitwise operation (move negated) and check if the result
9440 // fits in a smi.
9441 Label try_float;
9442 __ mvn(r1, Operand(r1));
9443 __ add(r2, r1, Operand(0x40000000), SetCC);
9444 __ b(mi, &try_float);
9445 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9446 __ b(&done);
9447
9448 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009449 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009450 // Allocate a fresh heap number, but don't overwrite r0 until
9451 // we're sure we can do it without going through the slow case
9452 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009453 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009454 __ mov(r0, Operand(r2));
9455 }
9456
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009457 if (CpuFeatures::IsSupported(VFP3)) {
9458 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9459 CpuFeatures::Scope scope(VFP3);
9460 __ vmov(s0, r1);
9461 __ vcvt_f64_s32(d0, s0);
9462 __ sub(r2, r0, Operand(kHeapObjectTag));
9463 __ vstr(d0, r2, HeapNumber::kValueOffset);
9464 } else {
9465 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9466 // have to set up a frame.
9467 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9468 __ push(lr);
9469 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9470 __ pop(lr);
9471 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009472 } else {
9473 UNIMPLEMENTED();
9474 }
9475
9476 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009477 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009478
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009479 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009480 __ bind(&slow);
9481 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009482 switch (op_) {
9483 case Token::SUB:
9484 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9485 break;
9486 case Token::BIT_NOT:
9487 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9488 break;
9489 default:
9490 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009491 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009492}
9493
9494
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009495void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009496 // r0 holds the exception.
9497
9498 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009499 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009500
9501 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009502 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9503 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009504
9505 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009506 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009507 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009508 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009509 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009510 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9511
9512 // Before returning we restore the context from the frame pointer if
9513 // not NULL. The frame pointer is NULL in the exception handler of a
9514 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009515 __ cmp(fp, Operand(0));
9516 // Set cp to NULL if fp is NULL.
9517 __ mov(cp, Operand(0), LeaveCC, eq);
9518 // Restore cp otherwise.
9519 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009520#ifdef DEBUG
9521 if (FLAG_debug_code) {
9522 __ mov(lr, Operand(pc));
9523 }
9524#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009525 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009526 __ pop(pc);
9527}
9528
9529
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009530void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9531 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009532 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009533 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009534
9535 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009536 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009537 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009538
9539 // Unwind the handlers until the ENTRY handler is found.
9540 Label loop, done;
9541 __ bind(&loop);
9542 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009543 const int kStateOffset = StackHandlerConstants::kStateOffset;
9544 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009545 __ cmp(r2, Operand(StackHandler::ENTRY));
9546 __ b(eq, &done);
9547 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009548 const int kNextOffset = StackHandlerConstants::kNextOffset;
9549 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009550 __ jmp(&loop);
9551 __ bind(&done);
9552
9553 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009554 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009555 __ pop(r2);
9556 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009557
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009558 if (type == OUT_OF_MEMORY) {
9559 // Set external caught exception to false.
9560 ExternalReference external_caught(Top::k_external_caught_exception_address);
9561 __ mov(r0, Operand(false));
9562 __ mov(r2, Operand(external_caught));
9563 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009564
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009565 // Set pending exception and r0 to out of memory exception.
9566 Failure* out_of_memory = Failure::OutOfMemoryException();
9567 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9568 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9569 __ str(r0, MemOperand(r2));
9570 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009571
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009572 // Stack layout at this point. See also StackHandlerConstants.
9573 // sp -> state (ENTRY)
9574 // fp
9575 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009576
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009577 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009578 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009579 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
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
9598void CEntryStub::GenerateCore(MacroAssembler* masm,
9599 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009600 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009601 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009602 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009603 bool always_allocate,
9604 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009605 // r0: result parameter for PerformGC, if any
9606 // r4: number of arguments including receiver (C callee-saved)
9607 // r5: pointer to builtin function (C callee-saved)
9608 // r6: pointer to the first argument (C callee-saved)
9609
9610 if (do_gc) {
9611 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009612 __ PrepareCallCFunction(1, r1);
9613 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009614 }
9615
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009616 ExternalReference scope_depth =
9617 ExternalReference::heap_always_allocate_scope_depth();
9618 if (always_allocate) {
9619 __ mov(r0, Operand(scope_depth));
9620 __ ldr(r1, MemOperand(r0));
9621 __ add(r1, r1, Operand(1));
9622 __ str(r1, MemOperand(r0));
9623 }
9624
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009625 // Call C built-in.
9626 // r0 = argc, r1 = argv
9627 __ mov(r0, Operand(r4));
9628 __ mov(r1, Operand(r6));
9629
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009630 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9631 int frame_alignment_mask = frame_alignment - 1;
9632#if defined(V8_HOST_ARCH_ARM)
9633 if (FLAG_debug_code) {
9634 if (frame_alignment > kPointerSize) {
9635 Label alignment_as_expected;
9636 ASSERT(IsPowerOf2(frame_alignment));
9637 __ sub(r2, sp, Operand(frame_alignment_skew));
9638 __ tst(r2, Operand(frame_alignment_mask));
9639 __ b(eq, &alignment_as_expected);
9640 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9641 __ stop("Unexpected alignment");
9642 __ bind(&alignment_as_expected);
9643 }
9644 }
9645#endif
9646
9647 // Just before the call (jump) below lr is pushed, so the actual alignment is
9648 // adding one to the current skew.
9649 int alignment_before_call =
9650 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9651 if (alignment_before_call > 0) {
9652 // Push until the alignment before the call is met.
9653 __ mov(r2, Operand(0));
9654 for (int i = alignment_before_call;
9655 (i & frame_alignment_mask) != 0;
9656 i += kPointerSize) {
9657 __ push(r2);
9658 }
9659 }
9660
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009661 // TODO(1242173): To let the GC traverse the return address of the exit
9662 // frames, we need to know where the return address is. Right now,
9663 // we push it on the stack to be able to find it again, but we never
9664 // restore from it in case of changes, which makes it impossible to
9665 // support moving the C entry code stub. This should be fixed, but currently
9666 // this is OK because the CEntryStub gets generated so early in the V8 boot
9667 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009668 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009669 masm->push(lr);
9670 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009671
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009672 // Restore sp back to before aligning the stack.
9673 if (alignment_before_call > 0) {
9674 __ add(sp, sp, Operand(alignment_before_call));
9675 }
9676
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009677 if (always_allocate) {
9678 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9679 // though (contain the result).
9680 __ mov(r2, Operand(scope_depth));
9681 __ ldr(r3, MemOperand(r2));
9682 __ sub(r3, r3, Operand(1));
9683 __ str(r3, MemOperand(r2));
9684 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009685
9686 // check for failure result
9687 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009688 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009689 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9690 __ add(r2, r0, Operand(1));
9691 __ tst(r2, Operand(kFailureTagMask));
9692 __ b(eq, &failure_returned);
9693
9694 // Exit C frame and return.
9695 // r0:r1: result
9696 // sp: stack pointer
9697 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009698 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009699
9700 // check if we should retry or throw exception
9701 Label retry;
9702 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009703 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009704 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9705 __ b(eq, &retry);
9706
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009707 // Special handling of out of memory exceptions.
9708 Failure* out_of_memory = Failure::OutOfMemoryException();
9709 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9710 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009711
9712 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009713 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009714 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009715 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009716 __ ldr(r0, MemOperand(ip));
9717 __ str(r3, MemOperand(ip));
9718
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009719 // Special handling of termination exceptions which are uncatchable
9720 // by javascript code.
9721 __ cmp(r0, Operand(Factory::termination_exception()));
9722 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009723
9724 // Handle normal exception.
9725 __ jmp(throw_normal_exception);
9726
9727 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9728}
9729
9730
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009731void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009732 // Called from JavaScript; parameters are on stack as if calling JS function
9733 // r0: number of arguments including receiver
9734 // r1: pointer to builtin function
9735 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009736 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009737 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009738
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009739 // Result returned in r0 or r0+r1 by default.
9740
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009741 // NOTE: Invocations of builtins may return failure objects
9742 // instead of a proper result. The builtin entry handles
9743 // this by performing a garbage collection and retrying the
9744 // builtin once.
9745
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009746 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009747 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009748
9749 // r4: number of arguments (C callee-saved)
9750 // r5: pointer to builtin function (C callee-saved)
9751 // r6: pointer to first argument (C callee-saved)
9752
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009753 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009754 Label throw_termination_exception;
9755 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009756
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009757 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009758 GenerateCore(masm,
9759 &throw_normal_exception,
9760 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009761 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009762 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009763 false,
9764 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009765
9766 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009767 GenerateCore(masm,
9768 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009769 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009770 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009771 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009772 false,
9773 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009774
9775 // Do full GC and retry runtime call one final time.
9776 Failure* failure = Failure::InternalError();
9777 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9778 GenerateCore(masm,
9779 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009780 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009781 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009782 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009783 true,
9784 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009785
9786 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009787 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9788
9789 __ bind(&throw_termination_exception);
9790 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009791
9792 __ bind(&throw_normal_exception);
9793 GenerateThrowTOS(masm);
9794}
9795
9796
9797void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9798 // r0: code entry
9799 // r1: function
9800 // r2: receiver
9801 // r3: argc
9802 // [sp+0]: argv
9803
9804 Label invoke, exit;
9805
9806 // Called from C, so do not pop argc and args on exit (preserve sp)
9807 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009808 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009809 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9810
9811 // Get address of argv, see stm above.
9812 // r0: code entry
9813 // r1: function
9814 // r2: receiver
9815 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009816 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009817
9818 // Push a frame with special values setup to mark it as an entry frame.
9819 // r0: code entry
9820 // r1: function
9821 // r2: receiver
9822 // r3: argc
9823 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009824 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009825 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9826 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009827 __ mov(r6, Operand(Smi::FromInt(marker)));
9828 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9829 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009830 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009831
9832 // Setup frame pointer for the frame to be pushed.
9833 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9834
9835 // Call a faked try-block that does the invoke.
9836 __ bl(&invoke);
9837
9838 // Caught exception: Store result (exception) in the pending
9839 // exception field in the JSEnv and return a failure sentinel.
9840 // Coming in here the fp will be invalid because the PushTryHandler below
9841 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009842 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009843 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009844 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009845 __ b(&exit);
9846
9847 // Invoke: Link this frame into the handler chain.
9848 __ bind(&invoke);
9849 // Must preserve r0-r4, r5-r7 are available.
9850 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009851 // If an exception not caught by another handler occurs, this handler
9852 // returns control to the code after the bl(&invoke) above, which
9853 // restores all kCalleeSaved registers (including cp and fp) to their
9854 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009855
9856 // Clear any pending exceptions.
9857 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9858 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009859 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009860 __ str(r5, MemOperand(ip));
9861
9862 // Invoke the function by calling through JS entry trampoline builtin.
9863 // Notice that we cannot store a reference to the trampoline code directly in
9864 // this stub, because runtime stubs are not traversed when doing GC.
9865
9866 // Expected registers by Builtins::JSEntryTrampoline
9867 // r0: code entry
9868 // r1: function
9869 // r2: receiver
9870 // r3: argc
9871 // r4: argv
9872 if (is_construct) {
9873 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9874 __ mov(ip, Operand(construct_entry));
9875 } else {
9876 ExternalReference entry(Builtins::JSEntryTrampoline);
9877 __ mov(ip, Operand(entry));
9878 }
9879 __ ldr(ip, MemOperand(ip)); // deref address
9880
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009881 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9882 // macro for the add instruction because we don't want the coverage tool
9883 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009884 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009885 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009886
9887 // Unlink this frame from the handler chain. When reading the
9888 // address of the next handler, there is no need to use the address
9889 // displacement since the current stack pointer (sp) points directly
9890 // to the stack handler.
9891 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9892 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9893 __ str(r3, MemOperand(ip));
9894 // No need to restore registers
9895 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9896
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009897
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009898 __ bind(&exit); // r0 holds result
9899 // Restore the top frame descriptors from the stack.
9900 __ pop(r3);
9901 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9902 __ str(r3, MemOperand(ip));
9903
9904 // Reset the stack to the callee saved registers.
9905 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9906
9907 // Restore callee-saved registers and return.
9908#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009909 if (FLAG_debug_code) {
9910 __ mov(lr, Operand(pc));
9911 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009912#endif
9913 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9914}
9915
9916
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009917// This stub performs an instanceof, calling the builtin function if
9918// necessary. Uses r1 for the object, r0 for the function that it may
9919// be an instance of (these are fetched from the stack).
9920void InstanceofStub::Generate(MacroAssembler* masm) {
9921 // Get the object - slow case for smis (we may need to throw an exception
9922 // depending on the rhs).
9923 Label slow, loop, is_instance, is_not_instance;
9924 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9925 __ BranchOnSmi(r0, &slow);
9926
9927 // Check that the left hand is a JS object and put map in r3.
9928 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9929 __ b(lt, &slow);
9930 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9931 __ b(gt, &slow);
9932
9933 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00009934 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009935 // r1 is function, r3 is map.
9936
9937 // Look up the function and the map in the instanceof cache.
9938 Label miss;
9939 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
9940 __ cmp(r1, ip);
9941 __ b(ne, &miss);
9942 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
9943 __ cmp(r3, ip);
9944 __ b(ne, &miss);
9945 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
9946 __ pop();
9947 __ pop();
9948 __ mov(pc, Operand(lr));
9949
9950 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009951 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
9952
9953 // Check that the function prototype is a JS object.
9954 __ BranchOnSmi(r4, &slow);
9955 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
9956 __ b(lt, &slow);
9957 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
9958 __ b(gt, &slow);
9959
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009960 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
9961 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
9962
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009963 // Register mapping: r3 is object map and r4 is function prototype.
9964 // Get prototype of object into r2.
9965 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9966
9967 // Loop through the prototype chain looking for the function prototype.
9968 __ bind(&loop);
9969 __ cmp(r2, Operand(r4));
9970 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009971 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9972 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009973 __ b(eq, &is_not_instance);
9974 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9975 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9976 __ jmp(&loop);
9977
9978 __ bind(&is_instance);
9979 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009980 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009981 __ pop();
9982 __ pop();
9983 __ mov(pc, Operand(lr)); // Return.
9984
9985 __ bind(&is_not_instance);
9986 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009987 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009988 __ pop();
9989 __ pop();
9990 __ mov(pc, Operand(lr)); // Return.
9991
9992 // Slow-case. Tail call builtin.
9993 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009994 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9995}
9996
9997
ager@chromium.org7c537e22008-10-16 08:43:32 +00009998void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9999 // The displacement is the offset of the last parameter (if any)
10000 // relative to the frame pointer.
10001 static const int kDisplacement =
10002 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010003
ager@chromium.org7c537e22008-10-16 08:43:32 +000010004 // Check that the key is a smi.
10005 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010006 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010007
ager@chromium.org7c537e22008-10-16 08:43:32 +000010008 // Check if the calling frame is an arguments adaptor frame.
10009 Label adaptor;
10010 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
10011 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +000010012 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +000010013 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010014
ager@chromium.org7c537e22008-10-16 08:43:32 +000010015 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000010016 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +000010017 // check for free.
10018 __ cmp(r1, r0);
10019 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010020
ager@chromium.org7c537e22008-10-16 08:43:32 +000010021 // Read the argument from the stack and return it.
10022 __ sub(r3, r0, r1);
10023 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10024 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010025 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010026
10027 // Arguments adaptor case: Check index against actual arguments
10028 // limit found in the arguments adaptor frame. Use unsigned
10029 // comparison to get negative check for free.
10030 __ bind(&adaptor);
10031 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10032 __ cmp(r1, r0);
10033 __ b(cs, &slow);
10034
10035 // Read the argument from the adaptor frame and return it.
10036 __ sub(r3, r0, r1);
10037 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10038 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010039 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010040
10041 // Slow-case: Handle non-smi or out-of-bounds access to arguments
10042 // by calling the runtime system.
10043 __ bind(&slow);
10044 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010045 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010046}
10047
10048
10049void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010050 // sp[0] : number of parameters
10051 // sp[4] : receiver displacement
10052 // sp[8] : function
10053
ager@chromium.org7c537e22008-10-16 08:43:32 +000010054 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +000010055 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +000010056 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
10057 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +000010058 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +000010059 __ b(eq, &adaptor_frame);
10060
10061 // Get the length from the frame.
10062 __ ldr(r1, MemOperand(sp, 0));
10063 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010064
10065 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +000010066 __ bind(&adaptor_frame);
10067 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10068 __ str(r1, MemOperand(sp, 0));
10069 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +000010070 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
10071 __ str(r3, MemOperand(sp, 1 * kPointerSize));
10072
ager@chromium.org5c838252010-02-19 08:53:10 +000010073 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010074 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +000010075 Label add_arguments_object;
10076 __ bind(&try_allocate);
10077 __ cmp(r1, Operand(0));
10078 __ b(eq, &add_arguments_object);
10079 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
10080 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
10081 __ bind(&add_arguments_object);
10082 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
10083
10084 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010085 __ AllocateInNewSpace(
10086 r1,
10087 r0,
10088 r2,
10089 r3,
10090 &runtime,
10091 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +000010092
10093 // Get the arguments boilerplate from the current (global) context.
10094 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
10095 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
10096 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
10097 __ ldr(r4, MemOperand(r4, offset));
10098
10099 // Copy the JS object part.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +000010100 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +000010101
10102 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010103 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010104 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
10105 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
10106
10107 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010108 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010109 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
10110 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
10111
10112 // If there are no actual arguments, we're done.
10113 Label done;
10114 __ cmp(r1, Operand(0));
10115 __ b(eq, &done);
10116
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010117 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +000010118 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000010119
10120 // Setup the elements pointer in the allocated arguments object and
10121 // initialize the header in the elements fixed array.
10122 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
10123 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
10124 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
10125 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
10126 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010127 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +000010128
10129 // Copy the fixed array slots.
10130 Label loop;
10131 // Setup r4 to point to the first array slot.
10132 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
10133 __ bind(&loop);
10134 // Pre-decrement r2 with kPointerSize on each iteration.
10135 // Pre-decrement in order to skip receiver.
10136 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
10137 // Post-increment r4 with kPointerSize on each iteration.
10138 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
10139 __ sub(r1, r1, Operand(1));
10140 __ cmp(r1, Operand(0));
10141 __ b(ne, &loop);
10142
10143 // Return and remove the on-stack parameters.
10144 __ bind(&done);
10145 __ add(sp, sp, Operand(3 * kPointerSize));
10146 __ Ret();
10147
ager@chromium.org7c537e22008-10-16 08:43:32 +000010148 // Do the runtime call to allocate the arguments object.
10149 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010150 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010151}
10152
10153
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010154void RegExpExecStub::Generate(MacroAssembler* masm) {
10155 // Just jump directly to runtime if native RegExp is not selected at compile
10156 // time or if regexp entry in generated code is turned off runtime switch or
10157 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010158#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010159 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010160#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010161 if (!FLAG_regexp_entry_native) {
10162 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10163 return;
10164 }
10165
10166 // Stack frame on entry.
10167 // sp[0]: last_match_info (expected JSArray)
10168 // sp[4]: previous index
10169 // sp[8]: subject string
10170 // sp[12]: JSRegExp object
10171
10172 static const int kLastMatchInfoOffset = 0 * kPointerSize;
10173 static const int kPreviousIndexOffset = 1 * kPointerSize;
10174 static const int kSubjectOffset = 2 * kPointerSize;
10175 static const int kJSRegExpOffset = 3 * kPointerSize;
10176
10177 Label runtime, invoke_regexp;
10178
10179 // Allocation of registers for this function. These are in callee save
10180 // registers and will be preserved by the call to the native RegExp code, as
10181 // this code is called using the normal C calling convention. When calling
10182 // directly from generated code the native RegExp code will not do a GC and
10183 // therefore the content of these registers are safe to use after the call.
10184 Register subject = r4;
10185 Register regexp_data = r5;
10186 Register last_match_info_elements = r6;
10187
10188 // Ensure that a RegExp stack is allocated.
10189 ExternalReference address_of_regexp_stack_memory_address =
10190 ExternalReference::address_of_regexp_stack_memory_address();
10191 ExternalReference address_of_regexp_stack_memory_size =
10192 ExternalReference::address_of_regexp_stack_memory_size();
10193 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
10194 __ ldr(r0, MemOperand(r0, 0));
10195 __ tst(r0, Operand(r0));
10196 __ b(eq, &runtime);
10197
10198 // Check that the first argument is a JSRegExp object.
10199 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010200 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010201 __ tst(r0, Operand(kSmiTagMask));
10202 __ b(eq, &runtime);
10203 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
10204 __ b(ne, &runtime);
10205
10206 // Check that the RegExp has been compiled (data contains a fixed array).
10207 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
10208 if (FLAG_debug_code) {
10209 __ tst(regexp_data, Operand(kSmiTagMask));
10210 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
10211 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
10212 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
10213 }
10214
10215 // regexp_data: RegExp data (FixedArray)
10216 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
10217 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
10218 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
10219 __ b(ne, &runtime);
10220
10221 // regexp_data: RegExp data (FixedArray)
10222 // Check that the number of captures fit in the static offsets vector buffer.
10223 __ ldr(r2,
10224 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10225 // Calculate number of capture registers (number_of_captures + 1) * 2. This
10226 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010227 STATIC_ASSERT(kSmiTag == 0);
10228 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010229 __ add(r2, r2, Operand(2)); // r2 was a smi.
10230 // Check that the static offsets vector buffer is large enough.
10231 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
10232 __ b(hi, &runtime);
10233
10234 // r2: Number of capture registers
10235 // regexp_data: RegExp data (FixedArray)
10236 // Check that the second argument is a string.
10237 __ ldr(subject, MemOperand(sp, kSubjectOffset));
10238 __ tst(subject, Operand(kSmiTagMask));
10239 __ b(eq, &runtime);
10240 Condition is_string = masm->IsObjectStringType(subject, r0);
10241 __ b(NegateCondition(is_string), &runtime);
10242 // Get the length of the string to r3.
10243 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
10244
10245 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +000010246 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010247 // subject: Subject string
10248 // regexp_data: RegExp data (FixedArray)
10249 // Check that the third argument is a positive smi less than the subject
10250 // string length. A negative value will be greater (unsigned comparison).
10251 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010252 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010253 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010254 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010255 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010256
10257 // r2: Number of capture registers
10258 // subject: Subject string
10259 // regexp_data: RegExp data (FixedArray)
10260 // Check that the fourth object is a JSArray object.
10261 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10262 __ tst(r0, Operand(kSmiTagMask));
10263 __ b(eq, &runtime);
10264 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
10265 __ b(ne, &runtime);
10266 // Check that the JSArray is in fast case.
10267 __ ldr(last_match_info_elements,
10268 FieldMemOperand(r0, JSArray::kElementsOffset));
10269 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010270 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000010271 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010272 __ b(ne, &runtime);
10273 // Check that the last match info has space for the capture registers and the
10274 // additional information.
10275 __ ldr(r0,
10276 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
10277 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010278 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010279 __ b(gt, &runtime);
10280
10281 // subject: Subject string
10282 // regexp_data: RegExp data (FixedArray)
10283 // Check the representation and encoding of the subject string.
10284 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010285 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10286 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010287 // First check for flat string.
10288 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010289 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010290 __ b(eq, &seq_string);
10291
10292 // subject: Subject string
10293 // regexp_data: RegExp data (FixedArray)
10294 // Check for flat cons string.
10295 // A flat cons string is a cons string where the second part is the empty
10296 // string. In that case the subject string is just the first part of the cons
10297 // string. Also in this case the first part of the cons string is known to be
10298 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010299 STATIC_ASSERT(kExternalStringTag !=0);
10300 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010301 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010302 __ b(ne, &runtime);
10303 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
10304 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
10305 __ cmp(r0, r1);
10306 __ b(ne, &runtime);
10307 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
10308 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10309 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010310 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010311 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010312 __ tst(r0, Operand(kStringRepresentationMask));
10313 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010314
10315 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010316 // subject: Subject string
10317 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010318 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010319 STATIC_ASSERT(4 == kAsciiStringTag);
10320 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010321 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010322 __ and_(r0, r0, Operand(kStringEncodingMask));
10323 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010324 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10325 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10326
10327 // Check that the irregexp code has been generated for the actual string
10328 // encoding. If it has, the field contains a code object otherwise it contains
10329 // the hole.
10330 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10331 __ b(ne, &runtime);
10332
10333 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10334 // r7: code
10335 // subject: Subject string
10336 // regexp_data: RegExp data (FixedArray)
10337 // Load used arguments before starting to push arguments for call to native
10338 // RegExp code to avoid handling changing stack height.
10339 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10340 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10341
10342 // r1: previous index
10343 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10344 // r7: code
10345 // subject: Subject string
10346 // regexp_data: RegExp data (FixedArray)
10347 // All checks done. Now push arguments for native regexp code.
10348 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10349
10350 static const int kRegExpExecuteArguments = 7;
10351 __ push(lr);
10352 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10353
10354 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10355 __ mov(r0, Operand(1));
10356 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10357
10358 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10359 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10360 __ ldr(r0, MemOperand(r0, 0));
10361 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10362 __ ldr(r2, MemOperand(r2, 0));
10363 __ add(r0, r0, Operand(r2));
10364 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10365
10366 // Argument 5 (sp[0]): static offsets vector buffer.
10367 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10368 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10369
10370 // For arguments 4 and 3 get string length, calculate start of string data and
10371 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10372 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010373 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010374 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010375 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10376 __ eor(r3, r3, Operand(1));
10377 // Argument 4 (r3): End of string data
10378 // Argument 3 (r2): Start of string data
10379 __ add(r2, r9, Operand(r1, LSL, r3));
10380 __ add(r3, r9, Operand(r0, LSL, r3));
10381
10382 // Argument 2 (r1): Previous index.
10383 // Already there
10384
10385 // Argument 1 (r0): Subject string.
10386 __ mov(r0, subject);
10387
10388 // Locate the code entry and call it.
10389 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10390 __ CallCFunction(r7, kRegExpExecuteArguments);
10391 __ pop(lr);
10392
10393 // r0: result
10394 // subject: subject string (callee saved)
10395 // regexp_data: RegExp data (callee saved)
10396 // last_match_info_elements: Last match info elements (callee saved)
10397
10398 // Check the result.
10399 Label success;
10400 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10401 __ b(eq, &success);
10402 Label failure;
10403 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10404 __ b(eq, &failure);
10405 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10406 // If not exception it can only be retry. Handle that in the runtime system.
10407 __ b(ne, &runtime);
10408 // Result must now be exception. If there is no pending exception already a
10409 // stack overflow (on the backtrack stack) was detected in RegExp code but
10410 // haven't created the exception yet. Handle that in the runtime system.
10411 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10412 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10413 __ ldr(r0, MemOperand(r0, 0));
10414 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10415 __ ldr(r1, MemOperand(r1, 0));
10416 __ cmp(r0, r1);
10417 __ b(eq, &runtime);
10418 __ bind(&failure);
10419 // For failure and exception return null.
10420 __ mov(r0, Operand(Factory::null_value()));
10421 __ add(sp, sp, Operand(4 * kPointerSize));
10422 __ Ret();
10423
10424 // Process the result from the native regexp code.
10425 __ bind(&success);
10426 __ ldr(r1,
10427 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10428 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010429 STATIC_ASSERT(kSmiTag == 0);
10430 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010431 __ add(r1, r1, Operand(2)); // r1 was a smi.
10432
10433 // r1: number of capture registers
10434 // r4: subject string
10435 // Store the capture count.
10436 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10437 __ str(r2, FieldMemOperand(last_match_info_elements,
10438 RegExpImpl::kLastCaptureCountOffset));
10439 // Store last subject and last input.
10440 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010441 __ str(subject,
10442 FieldMemOperand(last_match_info_elements,
10443 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010444 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010445 __ str(subject,
10446 FieldMemOperand(last_match_info_elements,
10447 RegExpImpl::kLastInputOffset));
10448 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010449 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010450
10451 // Get the static offsets vector filled by the native regexp code.
10452 ExternalReference address_of_static_offsets_vector =
10453 ExternalReference::address_of_static_offsets_vector();
10454 __ mov(r2, Operand(address_of_static_offsets_vector));
10455
10456 // r1: number of capture registers
10457 // r2: offsets vector
10458 Label next_capture, done;
10459 // Capture register counter starts from number of capture registers and
10460 // counts down until wraping after zero.
10461 __ add(r0,
10462 last_match_info_elements,
10463 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10464 __ bind(&next_capture);
10465 __ sub(r1, r1, Operand(1), SetCC);
10466 __ b(mi, &done);
10467 // Read the value from the static offsets vector buffer.
10468 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10469 // Store the smi value in the last match info.
10470 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10471 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10472 __ jmp(&next_capture);
10473 __ bind(&done);
10474
10475 // Return last match info.
10476 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10477 __ add(sp, sp, Operand(4 * kPointerSize));
10478 __ Ret();
10479
10480 // Do the runtime call to execute the regexp.
10481 __ bind(&runtime);
10482 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010483#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010484}
10485
10486
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010487void CallFunctionStub::Generate(MacroAssembler* masm) {
10488 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010489
10490 // If the receiver might be a value (string, number or boolean) check for this
10491 // and box it if it is.
10492 if (ReceiverMightBeValue()) {
10493 // Get the receiver from the stack.
10494 // function, receiver [, arguments]
10495 Label receiver_is_value, receiver_is_js_object;
10496 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10497
10498 // Check if receiver is a smi (which is a number value).
10499 __ BranchOnSmi(r1, &receiver_is_value);
10500
10501 // Check if the receiver is a valid JS object.
10502 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10503 __ b(ge, &receiver_is_js_object);
10504
10505 // Call the runtime to box the value.
10506 __ bind(&receiver_is_value);
10507 __ EnterInternalFrame();
10508 __ push(r1);
10509 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10510 __ LeaveInternalFrame();
10511 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10512
10513 __ bind(&receiver_is_js_object);
10514 }
10515
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010516 // Get the function to call from the stack.
10517 // function, receiver [, arguments]
10518 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10519
10520 // Check that the function is really a JavaScript function.
10521 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010522 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010523 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010524 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010525 __ b(ne, &slow);
10526
10527 // Fast-case: Invoke the function now.
10528 // r1: pushed function
10529 ParameterCount actual(argc_);
10530 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10531
10532 // Slow-case: Non-function called.
10533 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010534 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10535 // of the original receiver from the call site).
10536 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010537 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010538 __ mov(r2, Operand(0));
10539 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10540 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10541 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010542}
10543
10544
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010545// Unfortunately you have to run without snapshots to see most of these
10546// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010547const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010548 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10549 (lhs_.is(r1) && rhs_.is(r0)));
10550
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010551 if (name_ != NULL) return name_;
10552 const int kMaxNameLength = 100;
10553 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10554 if (name_ == NULL) return "OOM";
10555
10556 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010557 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010558 case lt: cc_name = "LT"; break;
10559 case gt: cc_name = "GT"; break;
10560 case le: cc_name = "LE"; break;
10561 case ge: cc_name = "GE"; break;
10562 case eq: cc_name = "EQ"; break;
10563 case ne: cc_name = "NE"; break;
10564 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010565 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010566
ager@chromium.orgb5737492010-07-15 09:29:43 +000010567 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10568 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10569
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010570 const char* strict_name = "";
10571 if (strict_ && (cc_ == eq || cc_ == ne)) {
10572 strict_name = "_STRICT";
10573 }
10574
10575 const char* never_nan_nan_name = "";
10576 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10577 never_nan_nan_name = "_NO_NAN";
10578 }
10579
10580 const char* include_number_compare_name = "";
10581 if (!include_number_compare_) {
10582 include_number_compare_name = "_NO_NUMBER";
10583 }
10584
10585 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010586 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010587 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010588 lhs_name,
10589 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010590 strict_name,
10591 never_nan_nan_name,
10592 include_number_compare_name);
10593 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010594}
10595
10596
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010597int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010598 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10599 // stubs the never NaN NaN condition is only taken into account if the
10600 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010601 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10602 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10603 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010604 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010605 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010606 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010607 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10608 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010609}
10610
10611
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010612// StringCharCodeAtGenerator
10613
10614void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10615 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010616 Label ascii_string;
10617 Label got_char_code;
10618
10619 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010620 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010621
10622 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010623 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10624 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010625 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010626 __ tst(result_, Operand(kIsNotStringMask));
10627 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010628
10629 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010630 __ BranchOnNotSmi(index_, &index_not_smi_);
10631
10632 // Put smi-tagged index into scratch register.
10633 __ mov(scratch_, index_);
10634 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010635
10636 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010637 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10638 __ cmp(ip, Operand(scratch_));
10639 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010640
10641 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010642 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010643 __ tst(result_, Operand(kStringRepresentationMask));
10644 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010645
10646 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010647 __ tst(result_, Operand(kIsConsStringMask));
10648 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010649
10650 // ConsString.
10651 // Check whether the right hand side is the empty string (i.e. if
10652 // this is really a flat string in a cons string). If that is not
10653 // the case we would rather go to the runtime system now to flatten
10654 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010655 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10656 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10657 __ cmp(result_, Operand(ip));
10658 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010659 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010660 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10661 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10662 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10663 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010664 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010665 __ tst(result_, Operand(kStringRepresentationMask));
10666 __ b(nz, &call_runtime_);
10667
10668 // Check for 1-byte or 2-byte string.
10669 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010670 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010671 __ tst(result_, Operand(kStringEncodingMask));
10672 __ b(nz, &ascii_string);
10673
10674 // 2-byte string.
10675 // Load the 2-byte character code into the result register. We can
10676 // add without shifting since the smi tag size is the log2 of the
10677 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010678 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010679 __ add(scratch_, object_, Operand(scratch_));
10680 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10681 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010682
10683 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010684 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010685 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010686 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10687 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010688
10689 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010690 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10691 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010692}
10693
10694
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010695void StringCharCodeAtGenerator::GenerateSlow(
10696 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10697 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010698
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010699 // Index is not a smi.
10700 __ bind(&index_not_smi_);
10701 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010702 __ CheckMap(index_,
10703 scratch_,
10704 Heap::kHeapNumberMapRootIndex,
10705 index_not_number_,
10706 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010707 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010708 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010709 __ push(index_); // Consumed by runtime conversion function.
10710 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10711 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10712 } else {
10713 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10714 // NumberToSmi discards numbers that are not exact integers.
10715 __ CallRuntime(Runtime::kNumberToSmi, 1);
10716 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010717 // Save the conversion result before the pop instructions below
10718 // have a chance to overwrite it.
10719 __ Move(scratch_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010720 __ pop(index_);
10721 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010722 // Reload the instance type.
10723 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10724 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010725 call_helper.AfterCall(masm);
10726 // If index is still not a smi, it must be out of range.
10727 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10728 // Otherwise, return to the fast path.
10729 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010730
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010731 // Call runtime. We get here when the receiver is a string and the
10732 // index is a number, but the code of getting the actual character
10733 // is too complex (e.g., when the string needs to be flattened).
10734 __ bind(&call_runtime_);
10735 call_helper.BeforeCall(masm);
10736 __ Push(object_, index_);
10737 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010738 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010739 call_helper.AfterCall(masm);
10740 __ jmp(&exit_);
10741
10742 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10743}
10744
10745
10746// -------------------------------------------------------------------------
10747// StringCharFromCodeGenerator
10748
10749void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010750 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010751 STATIC_ASSERT(kSmiTag == 0);
10752 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010753 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010754 __ tst(code_,
10755 Operand(kSmiTagMask |
10756 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10757 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010758
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010759 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10760 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010761 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010762 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10763 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10764 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10765 __ cmp(result_, Operand(ip));
10766 __ b(eq, &slow_case_);
10767 __ bind(&exit_);
10768}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010769
ager@chromium.orgac091b72010-05-05 07:34:42 +000010770
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010771void StringCharFromCodeGenerator::GenerateSlow(
10772 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10773 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10774
10775 __ bind(&slow_case_);
10776 call_helper.BeforeCall(masm);
10777 __ push(code_);
10778 __ CallRuntime(Runtime::kCharFromCode, 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010779 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010780 call_helper.AfterCall(masm);
10781 __ jmp(&exit_);
10782
10783 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10784}
10785
10786
10787// -------------------------------------------------------------------------
10788// StringCharAtGenerator
10789
10790void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10791 char_code_at_generator_.GenerateFast(masm);
10792 char_from_code_generator_.GenerateFast(masm);
10793}
10794
10795
10796void StringCharAtGenerator::GenerateSlow(
10797 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10798 char_code_at_generator_.GenerateSlow(masm, call_helper);
10799 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010800}
10801
10802
10803void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10804 Register dest,
10805 Register src,
10806 Register count,
10807 Register scratch,
10808 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010809 Label loop;
10810 Label done;
10811 // This loop just copies one character at a time, as it is only used for very
10812 // short strings.
10813 if (!ascii) {
10814 __ add(count, count, Operand(count), SetCC);
10815 } else {
10816 __ cmp(count, Operand(0));
10817 }
10818 __ b(eq, &done);
10819
10820 __ bind(&loop);
10821 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10822 // Perform sub between load and dependent store to get the load time to
10823 // complete.
10824 __ sub(count, count, Operand(1), SetCC);
10825 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10826 // last iteration.
10827 __ b(gt, &loop);
10828
10829 __ bind(&done);
10830}
10831
10832
10833enum CopyCharactersFlags {
10834 COPY_ASCII = 1,
10835 DEST_ALWAYS_ALIGNED = 2
10836};
10837
10838
ager@chromium.orgac091b72010-05-05 07:34:42 +000010839void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10840 Register dest,
10841 Register src,
10842 Register count,
10843 Register scratch1,
10844 Register scratch2,
10845 Register scratch3,
10846 Register scratch4,
10847 Register scratch5,
10848 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010849 bool ascii = (flags & COPY_ASCII) != 0;
10850 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10851
10852 if (dest_always_aligned && FLAG_debug_code) {
10853 // Check that destination is actually word aligned if the flag says
10854 // that it is.
10855 __ tst(dest, Operand(kPointerAlignmentMask));
10856 __ Check(eq, "Destination of copy not aligned.");
10857 }
10858
10859 const int kReadAlignment = 4;
10860 const int kReadAlignmentMask = kReadAlignment - 1;
10861 // Ensure that reading an entire aligned word containing the last character
10862 // of a string will not read outside the allocated area (because we pad up
10863 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010864 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010865 // Assumes word reads and writes are little endian.
10866 // Nothing to do for zero characters.
10867 Label done;
10868 if (!ascii) {
10869 __ add(count, count, Operand(count), SetCC);
10870 } else {
10871 __ cmp(count, Operand(0));
10872 }
10873 __ b(eq, &done);
10874
10875 // Assume that you cannot read (or write) unaligned.
10876 Label byte_loop;
10877 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10878 __ cmp(count, Operand(8));
10879 __ add(count, dest, Operand(count));
10880 Register limit = count; // Read until src equals this.
10881 __ b(lt, &byte_loop);
10882
10883 if (!dest_always_aligned) {
10884 // Align dest by byte copying. Copies between zero and three bytes.
10885 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10886 Label dest_aligned;
10887 __ b(eq, &dest_aligned);
10888 __ cmp(scratch4, Operand(2));
10889 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10890 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10891 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10892 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10893 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10894 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10895 __ bind(&dest_aligned);
10896 }
10897
10898 Label simple_loop;
10899
10900 __ sub(scratch4, dest, Operand(src));
10901 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10902 __ b(eq, &simple_loop);
10903 // Shift register is number of bits in a source word that
10904 // must be combined with bits in the next source word in order
10905 // to create a destination word.
10906
10907 // Complex loop for src/dst that are not aligned the same way.
10908 {
10909 Label loop;
10910 __ mov(scratch4, Operand(scratch4, LSL, 3));
10911 Register left_shift = scratch4;
10912 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10913 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10914 // Store the "shift" most significant bits of scratch in the least
10915 // signficant bits (i.e., shift down by (32-shift)).
10916 __ rsb(scratch2, left_shift, Operand(32));
10917 Register right_shift = scratch2;
10918 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10919
10920 __ bind(&loop);
10921 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10922 __ sub(scratch5, limit, Operand(dest));
10923 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10924 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10925 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10926 // Loop if four or more bytes left to copy.
10927 // Compare to eight, because we did the subtract before increasing dst.
10928 __ sub(scratch5, scratch5, Operand(8), SetCC);
10929 __ b(ge, &loop);
10930 }
10931 // There is now between zero and three bytes left to copy (negative that
10932 // number is in scratch5), and between one and three bytes already read into
10933 // scratch1 (eight times that number in scratch4). We may have read past
10934 // the end of the string, but because objects are aligned, we have not read
10935 // past the end of the object.
10936 // Find the minimum of remaining characters to move and preloaded characters
10937 // and write those as bytes.
10938 __ add(scratch5, scratch5, Operand(4), SetCC);
10939 __ b(eq, &done);
10940 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
10941 // Move minimum of bytes read and bytes left to copy to scratch4.
10942 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
10943 // Between one and three (value in scratch5) characters already read into
10944 // scratch ready to write.
10945 __ cmp(scratch5, Operand(2));
10946 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10947 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
10948 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
10949 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
10950 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
10951 // Copy any remaining bytes.
10952 __ b(&byte_loop);
10953
10954 // Simple loop.
10955 // Copy words from src to dst, until less than four bytes left.
10956 // Both src and dest are word aligned.
10957 __ bind(&simple_loop);
10958 {
10959 Label loop;
10960 __ bind(&loop);
10961 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10962 __ sub(scratch3, limit, Operand(dest));
10963 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10964 // Compare to 8, not 4, because we do the substraction before increasing
10965 // dest.
10966 __ cmp(scratch3, Operand(8));
10967 __ b(ge, &loop);
10968 }
10969
10970 // Copy bytes from src to dst until dst hits limit.
10971 __ bind(&byte_loop);
10972 __ cmp(dest, Operand(limit));
10973 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10974 __ b(ge, &done);
10975 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10976 __ b(&byte_loop);
10977
10978 __ bind(&done);
10979}
10980
10981
ager@chromium.orgac091b72010-05-05 07:34:42 +000010982void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10983 Register c1,
10984 Register c2,
10985 Register scratch1,
10986 Register scratch2,
10987 Register scratch3,
10988 Register scratch4,
10989 Register scratch5,
10990 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010991 // Register scratch3 is the general scratch register in this function.
10992 Register scratch = scratch3;
10993
10994 // Make sure that both characters are not digits as such strings has a
10995 // different hash algorithm. Don't try to look for these in the symbol table.
10996 Label not_array_index;
10997 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10998 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10999 __ b(hi, &not_array_index);
11000 __ sub(scratch, c2, Operand(static_cast<int>('0')));
11001 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
11002
11003 // If check failed combine both characters into single halfword.
11004 // This is required by the contract of the method: code at the
11005 // not_found branch expects this combination in c1 register
11006 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
11007 __ b(ls, not_found);
11008
11009 __ bind(&not_array_index);
11010 // Calculate the two character string hash.
11011 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011012 StringHelper::GenerateHashInit(masm, hash, c1);
11013 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
11014 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011015
11016 // Collect the two characters in a register.
11017 Register chars = c1;
11018 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
11019
11020 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11021 // hash: hash of two character string.
11022
11023 // Load symbol table
11024 // Load address of first element of the symbol table.
11025 Register symbol_table = c2;
11026 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
11027
11028 // Load undefined value
11029 Register undefined = scratch4;
11030 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
11031
11032 // Calculate capacity mask from the symbol table capacity.
11033 Register mask = scratch2;
11034 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
11035 __ mov(mask, Operand(mask, ASR, 1));
11036 __ sub(mask, mask, Operand(1));
11037
11038 // Calculate untagged address of the first element of the symbol table.
11039 Register first_symbol_table_element = symbol_table;
11040 __ add(first_symbol_table_element, symbol_table,
11041 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
11042
11043 // Registers
11044 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11045 // hash: hash of two character string
11046 // mask: capacity mask
11047 // first_symbol_table_element: address of the first element of
11048 // the symbol table
11049 // scratch: -
11050
11051 // Perform a number of probes in the symbol table.
11052 static const int kProbes = 4;
11053 Label found_in_symbol_table;
11054 Label next_probe[kProbes];
11055 for (int i = 0; i < kProbes; i++) {
11056 Register candidate = scratch5; // Scratch register contains candidate.
11057
11058 // Calculate entry in symbol table.
11059 if (i > 0) {
11060 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
11061 } else {
11062 __ mov(candidate, hash);
11063 }
11064
11065 __ and_(candidate, candidate, Operand(mask));
11066
11067 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011068 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011069 __ ldr(candidate,
11070 MemOperand(first_symbol_table_element,
11071 candidate,
11072 LSL,
11073 kPointerSizeLog2));
11074
11075 // If entry is undefined no string with this hash can be found.
11076 __ cmp(candidate, undefined);
11077 __ b(eq, not_found);
11078
11079 // If length is not 2 the string is not a candidate.
11080 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011081 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011082 __ b(ne, &next_probe[i]);
11083
11084 // Check that the candidate is a non-external ascii string.
11085 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
11086 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
11087 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
11088 &next_probe[i]);
11089
11090 // Check if the two characters match.
11091 // Assumes that word load is little endian.
11092 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
11093 __ cmp(chars, scratch);
11094 __ b(eq, &found_in_symbol_table);
11095 __ bind(&next_probe[i]);
11096 }
11097
11098 // No matching 2 character string found by probing.
11099 __ jmp(not_found);
11100
11101 // Scratch register contains result when we fall through to here.
11102 Register result = scratch;
11103 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000011104 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011105}
11106
11107
ager@chromium.orgac091b72010-05-05 07:34:42 +000011108void StringHelper::GenerateHashInit(MacroAssembler* masm,
11109 Register hash,
11110 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011111 // hash = character + (character << 10);
11112 __ add(hash, character, Operand(character, LSL, 10));
11113 // hash ^= hash >> 6;
11114 __ eor(hash, hash, Operand(hash, ASR, 6));
11115}
11116
11117
ager@chromium.orgac091b72010-05-05 07:34:42 +000011118void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
11119 Register hash,
11120 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011121 // hash += character;
11122 __ add(hash, hash, Operand(character));
11123 // hash += hash << 10;
11124 __ add(hash, hash, Operand(hash, LSL, 10));
11125 // hash ^= hash >> 6;
11126 __ eor(hash, hash, Operand(hash, ASR, 6));
11127}
11128
11129
ager@chromium.orgac091b72010-05-05 07:34:42 +000011130void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
11131 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011132 // hash += hash << 3;
11133 __ add(hash, hash, Operand(hash, LSL, 3));
11134 // hash ^= hash >> 11;
11135 __ eor(hash, hash, Operand(hash, ASR, 11));
11136 // hash += hash << 15;
11137 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
11138
11139 // if (hash == 0) hash = 27;
11140 __ mov(hash, Operand(27), LeaveCC, nz);
11141}
11142
11143
ager@chromium.org5c838252010-02-19 08:53:10 +000011144void SubStringStub::Generate(MacroAssembler* masm) {
11145 Label runtime;
11146
11147 // Stack frame on entry.
11148 // lr: return address
11149 // sp[0]: to
11150 // sp[4]: from
11151 // sp[8]: string
11152
11153 // This stub is called from the native-call %_SubString(...), so
11154 // nothing can be assumed about the arguments. It is tested that:
11155 // "string" is a sequential string,
11156 // both "from" and "to" are smis, and
11157 // 0 <= from <= to <= string.length.
11158 // If any of these assumptions fail, we call the runtime system.
11159
11160 static const int kToOffset = 0 * kPointerSize;
11161 static const int kFromOffset = 1 * kPointerSize;
11162 static const int kStringOffset = 2 * kPointerSize;
11163
11164
11165 // Check bounds and smi-ness.
11166 __ ldr(r7, MemOperand(sp, kToOffset));
11167 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011168 STATIC_ASSERT(kSmiTag == 0);
11169 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011170 // I.e., arithmetic shift right by one un-smi-tags.
11171 __ mov(r2, Operand(r7, ASR, 1), SetCC);
11172 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
11173 // If either r2 or r6 had the smi tag bit set, then carry is set now.
11174 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
11175 __ b(mi, &runtime); // From is negative.
11176
11177 __ sub(r2, r2, Operand(r3), SetCC);
11178 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011179 // Special handling of sub-strings of length 1 and 2. One character strings
11180 // are handled in the runtime system (looked up in the single character
11181 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000011182 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011183 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000011184
11185 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011186 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011187 // r6: from (smi)
11188 // r7: to (smi)
11189
11190 // Make sure first argument is a sequential (or flat) string.
11191 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011192 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011193 __ tst(r5, Operand(kSmiTagMask));
11194 __ b(eq, &runtime);
11195 Condition is_string = masm->IsObjectStringType(r5, r1);
11196 __ b(NegateCondition(is_string), &runtime);
11197
11198 // r1: instance type
11199 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011200 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011201 // r5: string
11202 // r6: from (smi)
11203 // r7: to (smi)
11204 Label seq_string;
11205 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011206 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
11207 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000011208 __ cmp(r4, Operand(kConsStringTag));
11209 __ b(gt, &runtime); // External strings go to runtime.
11210 __ b(lt, &seq_string); // Sequential strings are handled directly.
11211
11212 // Cons string. Try to recurse (once) on the first substring.
11213 // (This adds a little more generality than necessary to handle flattened
11214 // cons strings, but not much).
11215 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
11216 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
11217 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11218 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011219 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011220 __ b(ne, &runtime); // Cons and External strings go to runtime.
11221
11222 // Definitly a sequential string.
11223 __ bind(&seq_string);
11224
11225 // r1: instance type.
11226 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011227 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011228 // r5: string
11229 // r6: from (smi)
11230 // r7: to (smi)
11231 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011232 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000011233 __ b(lt, &runtime); // Fail if to > length.
11234
11235 // r1: instance type.
11236 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011237 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011238 // r5: string.
11239 // r6: from offset (smi)
11240 // Check for flat ascii string.
11241 Label non_ascii_flat;
11242 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011243 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011244 __ b(eq, &non_ascii_flat);
11245
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011246 Label result_longer_than_two;
11247 __ cmp(r2, Operand(2));
11248 __ b(gt, &result_longer_than_two);
11249
11250 // Sub string of length 2 requested.
11251 // Get the two characters forming the sub string.
11252 __ add(r5, r5, Operand(r3));
11253 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
11254 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
11255
11256 // Try to lookup two character string in symbol table.
11257 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011258 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11259 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011260 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11261 __ add(sp, sp, Operand(3 * kPointerSize));
11262 __ Ret();
11263
11264 // r2: result string length.
11265 // r3: two characters combined into halfword in little endian byte order.
11266 __ bind(&make_two_character_string);
11267 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
11268 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11269 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11270 __ add(sp, sp, Operand(3 * kPointerSize));
11271 __ Ret();
11272
11273 __ bind(&result_longer_than_two);
11274
ager@chromium.org5c838252010-02-19 08:53:10 +000011275 // Allocate the result.
11276 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
11277
11278 // r0: result string.
11279 // r2: result string length.
11280 // r5: string.
11281 // r6: from offset (smi)
11282 // Locate first character of result.
11283 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11284 // Locate 'from' character of string.
11285 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11286 __ add(r5, r5, Operand(r6, ASR, 1));
11287
11288 // r0: result string.
11289 // r1: first character of result string.
11290 // r2: result string length.
11291 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011292 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011293 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11294 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011295 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11296 __ add(sp, sp, Operand(3 * kPointerSize));
11297 __ Ret();
11298
11299 __ bind(&non_ascii_flat);
11300 // r2: result string length.
11301 // r5: string.
11302 // r6: from offset (smi)
11303 // Check for flat two byte string.
11304
11305 // Allocate the result.
11306 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
11307
11308 // r0: result string.
11309 // r2: result string length.
11310 // r5: string.
11311 // Locate first character of result.
11312 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11313 // Locate 'from' character of string.
11314 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11315 // As "from" is a smi it is 2 times the value which matches the size of a two
11316 // byte character.
11317 __ add(r5, r5, Operand(r6));
11318
11319 // r0: result string.
11320 // r1: first character of result.
11321 // r2: result length.
11322 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011323 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011324 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11325 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011326 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11327 __ add(sp, sp, Operand(3 * kPointerSize));
11328 __ Ret();
11329
11330 // Just jump to runtime to create the sub string.
11331 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011332 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011333}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011334
11335
11336void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11337 Register left,
11338 Register right,
11339 Register scratch1,
11340 Register scratch2,
11341 Register scratch3,
11342 Register scratch4) {
11343 Label compare_lengths;
11344 // Find minimum length and length difference.
11345 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11346 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11347 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11348 Register length_delta = scratch3;
11349 __ mov(scratch1, scratch2, LeaveCC, gt);
11350 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011351 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011352 __ tst(min_length, Operand(min_length));
11353 __ b(eq, &compare_lengths);
11354
ager@chromium.orgac091b72010-05-05 07:34:42 +000011355 // Untag smi.
11356 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11357
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011358 // Setup registers so that we only need to increment one register
11359 // in the loop.
11360 __ add(scratch2, min_length,
11361 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11362 __ add(left, left, Operand(scratch2));
11363 __ add(right, right, Operand(scratch2));
11364 // Registers left and right points to the min_length character of strings.
11365 __ rsb(min_length, min_length, Operand(-1));
11366 Register index = min_length;
11367 // Index starts at -min_length.
11368
11369 {
11370 // Compare loop.
11371 Label loop;
11372 __ bind(&loop);
11373 // Compare characters.
11374 __ add(index, index, Operand(1), SetCC);
11375 __ ldrb(scratch2, MemOperand(left, index), ne);
11376 __ ldrb(scratch4, MemOperand(right, index), ne);
11377 // Skip to compare lengths with eq condition true.
11378 __ b(eq, &compare_lengths);
11379 __ cmp(scratch2, scratch4);
11380 __ b(eq, &loop);
11381 // Fallthrough with eq condition false.
11382 }
11383 // Compare lengths - strings up to min-length are equal.
11384 __ bind(&compare_lengths);
11385 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11386 // Use zero length_delta as result.
11387 __ mov(r0, Operand(length_delta), SetCC, eq);
11388 // Fall through to here if characters compare not-equal.
11389 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11390 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11391 __ Ret();
11392}
11393
11394
11395void StringCompareStub::Generate(MacroAssembler* masm) {
11396 Label runtime;
11397
11398 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011399 // sp[0]: right string
11400 // sp[4]: left string
11401 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11402 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011403
11404 Label not_same;
11405 __ cmp(r0, r1);
11406 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011407 STATIC_ASSERT(EQUAL == 0);
11408 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011409 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11410 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11411 __ add(sp, sp, Operand(2 * kPointerSize));
11412 __ Ret();
11413
11414 __ bind(&not_same);
11415
11416 // Check that both objects are sequential ascii strings.
11417 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11418
11419 // Compare flat ascii strings natively. Remove arguments from stack first.
11420 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11421 __ add(sp, sp, Operand(2 * kPointerSize));
11422 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11423
11424 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11425 // tagged as a small integer.
11426 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011427 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011428}
11429
11430
ager@chromium.org5c838252010-02-19 08:53:10 +000011431void StringAddStub::Generate(MacroAssembler* masm) {
11432 Label string_add_runtime;
11433 // Stack on entry:
11434 // sp[0]: second argument.
11435 // sp[4]: first argument.
11436
11437 // Load the two arguments.
11438 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11439 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11440
11441 // Make sure that both arguments are strings if not known in advance.
11442 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011443 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011444 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11445 // Load instance types.
11446 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11447 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11448 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11449 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011450 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011451 // If either is not a string, go to runtime.
11452 __ tst(r4, Operand(kIsNotStringMask));
11453 __ tst(r5, Operand(kIsNotStringMask), eq);
11454 __ b(ne, &string_add_runtime);
11455 }
11456
11457 // Both arguments are strings.
11458 // r0: first string
11459 // r1: second string
11460 // r4: first string instance type (if string_check_)
11461 // r5: second string instance type (if string_check_)
11462 {
11463 Label strings_not_empty;
11464 // Check if either of the strings are empty. In that case return the other.
11465 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11466 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011467 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011468 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011469 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011470 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011471 // Else test if second string is empty.
11472 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011473 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11474
11475 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11476 __ add(sp, sp, Operand(2 * kPointerSize));
11477 __ Ret();
11478
11479 __ bind(&strings_not_empty);
11480 }
11481
ager@chromium.orgac091b72010-05-05 07:34:42 +000011482 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11483 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011484 // Both strings are non-empty.
11485 // r0: first string
11486 // r1: second string
11487 // r2: length of first string
11488 // r3: length of second string
11489 // r4: first string instance type (if string_check_)
11490 // r5: second string instance type (if string_check_)
11491 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011492 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011493 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011494 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011495 __ add(r6, r2, Operand(r3));
11496 // Use the runtime system when adding two one character strings, as it
11497 // contains optimizations for this specific case using the symbol table.
11498 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011499 __ b(ne, &longer_than_two);
11500
11501 // Check that both strings are non-external ascii strings.
11502 if (!string_check_) {
11503 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11504 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11505 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11506 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11507 }
11508 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11509 &string_add_runtime);
11510
11511 // Get the two characters forming the sub string.
11512 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11513 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11514
11515 // Try to lookup two character string in symbol table. If it is not found
11516 // just allocate a new one.
11517 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011518 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11519 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011520 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11521 __ add(sp, sp, Operand(2 * kPointerSize));
11522 __ Ret();
11523
11524 __ bind(&make_two_character_string);
11525 // Resulting string has length 2 and first chars of two strings
11526 // are combined into single halfword in r2 register.
11527 // So we can fill resulting string without two loops by a single
11528 // halfword store instruction (which assumes that processor is
11529 // in a little endian mode)
11530 __ mov(r6, Operand(2));
11531 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11532 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11533 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11534 __ add(sp, sp, Operand(2 * kPointerSize));
11535 __ Ret();
11536
11537 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011538 // Check if resulting string will be flat.
11539 __ cmp(r6, Operand(String::kMinNonFlatLength));
11540 __ b(lt, &string_add_flat_result);
11541 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011542 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011543 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11544 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11545 __ cmp(r6, Operand(String::kMaxLength + 1));
11546 __ b(hs, &string_add_runtime);
11547
11548 // If result is not supposed to be flat, allocate a cons string object.
11549 // If both strings are ascii the result is an ascii cons string.
11550 if (!string_check_) {
11551 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11552 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11553 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11554 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11555 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011556 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011557 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011558 __ tst(r4, Operand(kStringEncodingMask));
11559 __ tst(r5, Operand(kStringEncodingMask), ne);
11560 __ b(eq, &non_ascii);
11561
11562 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011563 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011564 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11565 __ bind(&allocated);
11566 // Fill the fields of the cons string.
11567 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11568 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11569 __ mov(r0, Operand(r7));
11570 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11571 __ add(sp, sp, Operand(2 * kPointerSize));
11572 __ Ret();
11573
11574 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011575 // At least one of the strings is two-byte. Check whether it happens
11576 // to contain only ascii characters.
11577 // r4: first instance type.
11578 // r5: second instance type.
11579 __ tst(r4, Operand(kAsciiDataHintMask));
11580 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11581 __ b(ne, &ascii_data);
11582 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011583 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011584 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11585 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11586 __ b(eq, &ascii_data);
11587
ager@chromium.org5c838252010-02-19 08:53:10 +000011588 // Allocate a two byte cons string.
11589 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11590 __ jmp(&allocated);
11591
11592 // Handle creating a flat result. First check that both strings are
11593 // sequential and that they have the same encoding.
11594 // r0: first string
11595 // r1: second string
11596 // r2: length of first string
11597 // r3: length of second string
11598 // r4: first string instance type (if string_check_)
11599 // r5: second string instance type (if string_check_)
11600 // r6: sum of lengths.
11601 __ bind(&string_add_flat_result);
11602 if (!string_check_) {
11603 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11604 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11605 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11606 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11607 }
11608 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011609 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011610 __ tst(r4, Operand(kStringRepresentationMask));
11611 __ tst(r5, Operand(kStringRepresentationMask), eq);
11612 __ b(ne, &string_add_runtime);
11613 // Now check if both strings have the same encoding (ASCII/Two-byte).
11614 // r0: first string.
11615 // r1: second string.
11616 // r2: length of first string.
11617 // r3: length of second string.
11618 // r6: sum of lengths..
11619 Label non_ascii_string_add_flat_result;
11620 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11621 __ eor(r7, r4, Operand(r5));
11622 __ tst(r7, Operand(kStringEncodingMask));
11623 __ b(ne, &string_add_runtime);
11624 // And see if it's ASCII or two-byte.
11625 __ tst(r4, Operand(kStringEncodingMask));
11626 __ b(eq, &non_ascii_string_add_flat_result);
11627
11628 // Both strings are sequential ASCII strings. We also know that they are
11629 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011630 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011631 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11632 // Locate first character of result.
11633 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11634 // Locate first character of first argument.
11635 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11636 // r0: first character of first string.
11637 // r1: second string.
11638 // r2: length of first string.
11639 // r3: length of second string.
11640 // r6: first character of result.
11641 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011642 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011643
11644 // Load second argument and locate first character.
11645 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11646 // r1: first character of second string.
11647 // r3: length of second string.
11648 // r6: next character of result.
11649 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011650 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011651 __ mov(r0, Operand(r7));
11652 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11653 __ add(sp, sp, Operand(2 * kPointerSize));
11654 __ Ret();
11655
11656 __ bind(&non_ascii_string_add_flat_result);
11657 // Both strings are sequential two byte strings.
11658 // r0: first string.
11659 // r1: second string.
11660 // r2: length of first string.
11661 // r3: length of second string.
11662 // r6: sum of length of strings.
11663 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11664 // r0: first string.
11665 // r1: second string.
11666 // r2: length of first string.
11667 // r3: length of second string.
11668 // r7: result string.
11669
11670 // Locate first character of result.
11671 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11672 // Locate first character of first argument.
11673 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11674
11675 // r0: first character of first string.
11676 // r1: second string.
11677 // r2: length of first string.
11678 // r3: length of second string.
11679 // r6: first character of result.
11680 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011681 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011682
11683 // Locate first character of second argument.
11684 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11685
11686 // r1: first character of second string.
11687 // r3: length of second string.
11688 // r6: next character of result (after copy of first string).
11689 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011690 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011691
11692 __ mov(r0, Operand(r7));
11693 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11694 __ add(sp, sp, Operand(2 * kPointerSize));
11695 __ Ret();
11696
11697 // Just jump to runtime to add the two strings.
11698 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011699 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011700}
11701
11702
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011703#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011705} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011706
11707#endif // V8_TARGET_ARCH_ARM