blob: df17b6f8644c95c87ad7146a02e0ee22e0f2f59d [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();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003448 if (node->depth() > 1) {
3449 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003450 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003451 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003452 } else {
3453 FastCloneShallowArrayStub stub(length);
3454 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003455 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003456 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003457 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003458
3459 // Generate code to set the elements in the array that are not
3460 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461 for (int i = 0; i < node->values()->length(); i++) {
3462 Expression* value = node->values()->at(i);
3463
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003464 // If value is a literal the property value is already set in the
3465 // boilerplate object.
3466 if (value->AsLiteral() != NULL) continue;
3467 // If value is a materialized literal the property value is already set
3468 // in the boilerplate object if it is simple.
3469 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003471 // The property must be set by generated code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003472 Load(value);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003473 frame_->PopToR0();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003474 // Fetch the object literal.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003475 frame_->SpillAllButCopyTOSToR1();
3476
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003477 // Get the elements array.
3478 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003479
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003480 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003481 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003482 __ str(r0, FieldMemOperand(r1, offset));
3483
3484 // Update the write barrier for the array address.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003485 __ RecordWrite(r1, Operand(offset), r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003487 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488}
3489
3490
ager@chromium.org32912102009-01-16 10:38:43 +00003491void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492#ifdef DEBUG
3493 int original_height = frame_->height();
3494#endif
ager@chromium.org32912102009-01-16 10:38:43 +00003495 // Call runtime routine to allocate the catch extension object and
3496 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003497 Comment cmnt(masm_, "[ CatchExtensionObject");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003498 Load(node->key());
3499 Load(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003500 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3501 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003502 ASSERT_EQ(original_height + 1, frame_->height());
3503}
3504
3505
3506void CodeGenerator::EmitSlotAssignment(Assignment* node) {
3507#ifdef DEBUG
3508 int original_height = frame_->height();
3509#endif
3510 Comment cmnt(masm(), "[ Variable Assignment");
3511 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3512 ASSERT(var != NULL);
3513 Slot* slot = var->slot();
3514 ASSERT(slot != NULL);
3515
3516 // Evaluate the right-hand side.
3517 if (node->is_compound()) {
3518 // For a compound assignment the right-hand side is a binary operation
3519 // between the current property value and the actual right-hand side.
3520 LoadFromSlotCheckForArguments(slot, NOT_INSIDE_TYPEOF);
3521
3522 // Perform the binary operation.
3523 Literal* literal = node->value()->AsLiteral();
3524 bool overwrite_value =
3525 (node->value()->AsBinaryOperation() != NULL &&
3526 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3527 if (literal != NULL && literal->handle()->IsSmi()) {
3528 SmiOperation(node->binary_op(),
3529 literal->handle(),
3530 false,
3531 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3532 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003533 GenerateInlineSmi inline_smi =
3534 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3535 if (literal != NULL) {
3536 ASSERT(!literal->handle()->IsSmi());
3537 inline_smi = DONT_GENERATE_INLINE_SMI;
3538 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003539 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003540 GenericBinaryOperation(node->binary_op(),
3541 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3542 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003543 }
3544 } else {
3545 Load(node->value());
3546 }
3547
3548 // Perform the assignment.
3549 if (var->mode() != Variable::CONST || node->op() == Token::INIT_CONST) {
3550 CodeForSourcePosition(node->position());
3551 StoreToSlot(slot,
3552 node->op() == Token::INIT_CONST ? CONST_INIT : NOT_CONST_INIT);
3553 }
3554 ASSERT_EQ(original_height + 1, frame_->height());
3555}
3556
3557
3558void CodeGenerator::EmitNamedPropertyAssignment(Assignment* node) {
3559#ifdef DEBUG
3560 int original_height = frame_->height();
3561#endif
3562 Comment cmnt(masm(), "[ Named Property Assignment");
3563 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3564 Property* prop = node->target()->AsProperty();
3565 ASSERT(var == NULL || (prop == NULL && var->is_global()));
3566
3567 // Initialize name and evaluate the receiver sub-expression if necessary. If
3568 // the receiver is trivial it is not placed on the stack at this point, but
3569 // loaded whenever actually needed.
3570 Handle<String> name;
3571 bool is_trivial_receiver = false;
3572 if (var != NULL) {
3573 name = var->name();
3574 } else {
3575 Literal* lit = prop->key()->AsLiteral();
3576 ASSERT_NOT_NULL(lit);
3577 name = Handle<String>::cast(lit->handle());
3578 // Do not materialize the receiver on the frame if it is trivial.
3579 is_trivial_receiver = prop->obj()->IsTrivial();
3580 if (!is_trivial_receiver) Load(prop->obj());
3581 }
3582
3583 // Change to slow case in the beginning of an initialization block to
3584 // avoid the quadratic behavior of repeatedly adding fast properties.
3585 if (node->starts_initialization_block()) {
3586 // Initialization block consists of assignments of the form expr.x = ..., so
3587 // this will never be an assignment to a variable, so there must be a
3588 // receiver object.
3589 ASSERT_EQ(NULL, var);
3590 if (is_trivial_receiver) {
3591 Load(prop->obj());
3592 } else {
3593 frame_->Dup();
3594 }
3595 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3596 }
3597
3598 // Change to fast case at the end of an initialization block. To prepare for
3599 // that add an extra copy of the receiver to the frame, so that it can be
3600 // converted back to fast case after the assignment.
3601 if (node->ends_initialization_block() && !is_trivial_receiver) {
3602 frame_->Dup();
3603 }
3604
3605 // Stack layout:
3606 // [tos] : receiver (only materialized if non-trivial)
3607 // [tos+1] : receiver if at the end of an initialization block
3608
3609 // Evaluate the right-hand side.
3610 if (node->is_compound()) {
3611 // For a compound assignment the right-hand side is a binary operation
3612 // between the current property value and the actual right-hand side.
3613 if (is_trivial_receiver) {
3614 Load(prop->obj());
3615 } else if (var != NULL) {
3616 LoadGlobal();
3617 } else {
3618 frame_->Dup();
3619 }
3620 EmitNamedLoad(name, var != NULL);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003621
3622 // Perform the binary operation.
3623 Literal* literal = node->value()->AsLiteral();
3624 bool overwrite_value =
3625 (node->value()->AsBinaryOperation() != NULL &&
3626 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3627 if (literal != NULL && literal->handle()->IsSmi()) {
3628 SmiOperation(node->binary_op(),
3629 literal->handle(),
3630 false,
3631 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3632 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003633 GenerateInlineSmi inline_smi =
3634 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3635 if (literal != NULL) {
3636 ASSERT(!literal->handle()->IsSmi());
3637 inline_smi = DONT_GENERATE_INLINE_SMI;
3638 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003639 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003640 GenericBinaryOperation(node->binary_op(),
3641 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3642 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003643 }
3644 } else {
3645 // For non-compound assignment just load the right-hand side.
3646 Load(node->value());
3647 }
3648
3649 // Stack layout:
3650 // [tos] : value
3651 // [tos+1] : receiver (only materialized if non-trivial)
3652 // [tos+2] : receiver if at the end of an initialization block
3653
3654 // Perform the assignment. It is safe to ignore constants here.
3655 ASSERT(var == NULL || var->mode() != Variable::CONST);
3656 ASSERT_NE(Token::INIT_CONST, node->op());
3657 if (is_trivial_receiver) {
3658 // Load the receiver and swap with the value.
3659 Load(prop->obj());
3660 Register t0 = frame_->PopToRegister();
3661 Register t1 = frame_->PopToRegister(t0);
3662 frame_->EmitPush(t0);
3663 frame_->EmitPush(t1);
3664 }
3665 CodeForSourcePosition(node->position());
3666 bool is_contextual = (var != NULL);
3667 EmitNamedStore(name, is_contextual);
3668 frame_->EmitPush(r0);
3669
3670 // Change to fast case at the end of an initialization block.
3671 if (node->ends_initialization_block()) {
3672 ASSERT_EQ(NULL, var);
3673 // The argument to the runtime call is the receiver.
3674 if (is_trivial_receiver) {
3675 Load(prop->obj());
3676 } else {
3677 // A copy of the receiver is below the value of the assignment. Swap
3678 // the receiver and the value of the assignment expression.
3679 Register t0 = frame_->PopToRegister();
3680 Register t1 = frame_->PopToRegister(t0);
3681 frame_->EmitPush(t0);
3682 frame_->EmitPush(t1);
3683 }
3684 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3685 }
3686
3687 // Stack layout:
3688 // [tos] : result
3689
3690 ASSERT_EQ(original_height + 1, frame_->height());
3691}
3692
3693
3694void CodeGenerator::EmitKeyedPropertyAssignment(Assignment* node) {
3695#ifdef DEBUG
3696 int original_height = frame_->height();
3697#endif
3698 Comment cmnt(masm_, "[ Keyed Property Assignment");
3699 Property* prop = node->target()->AsProperty();
3700 ASSERT_NOT_NULL(prop);
3701
3702 // Evaluate the receiver subexpression.
3703 Load(prop->obj());
3704
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003705 WriteBarrierCharacter wb_info;
3706
ager@chromium.orgac091b72010-05-05 07:34:42 +00003707 // Change to slow case in the beginning of an initialization block to
3708 // avoid the quadratic behavior of repeatedly adding fast properties.
3709 if (node->starts_initialization_block()) {
3710 frame_->Dup();
3711 frame_->CallRuntime(Runtime::kToSlowProperties, 1);
3712 }
3713
3714 // Change to fast case at the end of an initialization block. To prepare for
3715 // that add an extra copy of the receiver to the frame, so that it can be
3716 // converted back to fast case after the assignment.
3717 if (node->ends_initialization_block()) {
3718 frame_->Dup();
3719 }
3720
3721 // Evaluate the key subexpression.
3722 Load(prop->key());
3723
3724 // Stack layout:
3725 // [tos] : key
3726 // [tos+1] : receiver
3727 // [tos+2] : receiver if at the end of an initialization block
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003728 //
ager@chromium.orgac091b72010-05-05 07:34:42 +00003729 // Evaluate the right-hand side.
3730 if (node->is_compound()) {
3731 // For a compound assignment the right-hand side is a binary operation
3732 // between the current property value and the actual right-hand side.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003733 // Duplicate receiver and key for loading the current property value.
3734 frame_->Dup2();
ager@chromium.orgac091b72010-05-05 07:34:42 +00003735 EmitKeyedLoad();
3736 frame_->EmitPush(r0);
3737
3738 // Perform the binary operation.
3739 Literal* literal = node->value()->AsLiteral();
3740 bool overwrite_value =
3741 (node->value()->AsBinaryOperation() != NULL &&
3742 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
3743 if (literal != NULL && literal->handle()->IsSmi()) {
3744 SmiOperation(node->binary_op(),
3745 literal->handle(),
3746 false,
3747 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE);
3748 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003749 GenerateInlineSmi inline_smi =
3750 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
3751 if (literal != NULL) {
3752 ASSERT(!literal->handle()->IsSmi());
3753 inline_smi = DONT_GENERATE_INLINE_SMI;
3754 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003755 Load(node->value());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003756 GenericBinaryOperation(node->binary_op(),
3757 overwrite_value ? OVERWRITE_RIGHT : NO_OVERWRITE,
3758 inline_smi);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003759 }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003760 wb_info = node->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI;
ager@chromium.orgac091b72010-05-05 07:34:42 +00003761 } else {
3762 // For non-compound assignment just load the right-hand side.
3763 Load(node->value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003764 wb_info = node->value()->AsLiteral() != NULL ?
3765 NEVER_NEWSPACE :
3766 (node->value()->type()->IsLikelySmi() ? LIKELY_SMI : UNLIKELY_SMI);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003767 }
3768
3769 // Stack layout:
3770 // [tos] : value
3771 // [tos+1] : key
3772 // [tos+2] : receiver
3773 // [tos+3] : receiver if at the end of an initialization block
3774
3775 // Perform the assignment. It is safe to ignore constants here.
3776 ASSERT(node->op() != Token::INIT_CONST);
3777 CodeForSourcePosition(node->position());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003778 EmitKeyedStore(prop->key()->type(), wb_info);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003779 frame_->EmitPush(r0);
3780
3781 // Stack layout:
3782 // [tos] : result
3783 // [tos+1] : receiver if at the end of an initialization block
3784
3785 // Change to fast case at the end of an initialization block.
3786 if (node->ends_initialization_block()) {
3787 // The argument to the runtime call is the extra copy of the receiver,
3788 // which is below the value of the assignment. Swap the receiver and
3789 // the value of the assignment expression.
3790 Register t0 = frame_->PopToRegister();
3791 Register t1 = frame_->PopToRegister(t0);
3792 frame_->EmitPush(t1);
3793 frame_->EmitPush(t0);
3794 frame_->CallRuntime(Runtime::kToFastProperties, 1);
3795 }
3796
3797 // Stack layout:
3798 // [tos] : result
3799
3800 ASSERT_EQ(original_height + 1, frame_->height());
ager@chromium.org32912102009-01-16 10:38:43 +00003801}
3802
3803
ager@chromium.org7c537e22008-10-16 08:43:32 +00003804void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003805 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806#ifdef DEBUG
3807 int original_height = frame_->height();
3808#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003810
ager@chromium.orgac091b72010-05-05 07:34:42 +00003811 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3812 Property* prop = node->target()->AsProperty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813
ager@chromium.orgac091b72010-05-05 07:34:42 +00003814 if (var != NULL && !var->is_global()) {
3815 EmitSlotAssignment(node);
mads.s.ager31e71382008-08-13 09:32:07 +00003816
ager@chromium.orgac091b72010-05-05 07:34:42 +00003817 } else if ((prop != NULL && prop->key()->IsPropertyName()) ||
3818 (var != NULL && var->is_global())) {
3819 // Properties whose keys are property names and global variables are
3820 // treated as named property references. We do not need to consider
3821 // global 'this' because it is not a valid left-hand side.
3822 EmitNamedPropertyAssignment(node);
3823
3824 } else if (prop != NULL) {
3825 // Other properties (including rewritten parameters for a function that
3826 // uses arguments) are keyed property assignments.
3827 EmitKeyedPropertyAssignment(node);
3828
3829 } else {
3830 // Invalid left-hand side.
3831 Load(node->target());
3832 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
3833 // The runtime call doesn't actually return but the code generator will
3834 // still generate code and expects a certain frame height.
3835 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003836 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003837 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003838}
3839
3840
ager@chromium.org7c537e22008-10-16 08:43:32 +00003841void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003842#ifdef DEBUG
3843 int original_height = frame_->height();
3844#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003845 Comment cmnt(masm_, "[ Throw");
3846
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003847 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003848 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 frame_->CallRuntime(Runtime::kThrow, 1);
3850 frame_->EmitPush(r0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00003851 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852}
3853
3854
ager@chromium.org7c537e22008-10-16 08:43:32 +00003855void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003856#ifdef DEBUG
3857 int original_height = frame_->height();
3858#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003860
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003861 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003862 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003863 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00003864 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865}
3866
3867
ager@chromium.org7c537e22008-10-16 08:43:32 +00003868void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003869#ifdef DEBUG
3870 int original_height = frame_->height();
3871#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003872 Comment cmnt(masm_, "[ Call");
3873
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003874 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 ZoneList<Expression*>* args = node->arguments();
3876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879 Variable* var = function->AsVariableProxy()->AsVariable();
3880 Property* property = function->AsProperty();
3881
3882 // ------------------------------------------------------------------------
3883 // Fast-case: Use inline caching.
3884 // ---
3885 // According to ECMA-262, section 11.2.3, page 44, the function to call
3886 // must be resolved after the arguments have been evaluated. The IC code
3887 // automatically handles this by loading the arguments before the function
3888 // is resolved in cache misses (this also holds for megamorphic calls).
3889 // ------------------------------------------------------------------------
3890
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003891 if (var != NULL && var->is_possibly_eval()) {
3892 // ----------------------------------
3893 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3894 // ----------------------------------
3895
3896 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3897 // resolve the function we need to call and the receiver of the
3898 // call. Then we call the resolved function using the given
3899 // arguments.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003900
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003901 // Prepare stack for call to resolved function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003902 Load(function);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003903
3904 // Allocate a frame slot for the receiver.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003905 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003906
3907 // Load the arguments.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003908 int arg_count = args->length();
3909 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003910 Load(args->at(i));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003911 }
3912
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003913 VirtualFrame::SpilledScope spilled_scope(frame_);
3914
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003915 // If we know that eval can only be shadowed by eval-introduced
3916 // variables we attempt to load the global eval function directly
3917 // in generated code. If we succeed, there is no need to perform a
3918 // context lookup in the runtime system.
3919 JumpTarget done;
3920 if (var->slot() != NULL && var->mode() == Variable::DYNAMIC_GLOBAL) {
3921 ASSERT(var->slot()->type() == Slot::LOOKUP);
3922 JumpTarget slow;
3923 // Prepare the stack for the call to
3924 // ResolvePossiblyDirectEvalNoLookup by pushing the loaded
3925 // function, the first argument to the eval call and the
3926 // receiver.
3927 LoadFromGlobalSlotCheckExtensions(var->slot(),
3928 NOT_INSIDE_TYPEOF,
3929 &slow);
3930 frame_->EmitPush(r0);
3931 if (arg_count > 0) {
3932 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3933 frame_->EmitPush(r1);
3934 } else {
3935 frame_->EmitPush(r2);
3936 }
3937 __ ldr(r1, frame_->Receiver());
3938 frame_->EmitPush(r1);
3939
3940 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEvalNoLookup, 3);
3941
3942 done.Jump();
3943 slow.Bind();
3944 }
3945
3946 // Prepare the stack for the call to ResolvePossiblyDirectEval by
3947 // pushing the loaded function, the first argument to the eval
3948 // call and the receiver.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003949 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3950 frame_->EmitPush(r1);
3951 if (arg_count > 0) {
3952 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3953 frame_->EmitPush(r1);
3954 } else {
3955 frame_->EmitPush(r2);
3956 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003957 __ ldr(r1, frame_->Receiver());
3958 frame_->EmitPush(r1);
3959
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003960 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003961 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003962
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003963 // If we generated fast-case code bind the jump-target where fast
3964 // and slow case merge.
3965 if (done.is_linked()) done.Bind();
3966
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003967 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003968 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003969 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3970
3971 // Call the function.
3972 CodeForSourcePosition(node->position());
3973
3974 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003975 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003976 frame_->CallStub(&call_function, arg_count + 1);
3977
3978 __ ldr(cp, frame_->Context());
3979 // Remove the function from the stack.
3980 frame_->Drop();
3981 frame_->EmitPush(r0);
3982
3983 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 // ----------------------------------
3985 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3986 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003987 // Pass the global object as the receiver and let the IC stub
3988 // patch the stack to use the global proxy as 'this' in the
3989 // invoked function.
3990 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991
3992 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003993 int arg_count = args->length();
3994 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003995 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003997
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003998 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +00003999 // Setup the name register and call the IC initialization code.
4000 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004001 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4002 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004003 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004004 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
4005 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004006 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004007 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008
4009 } else if (var != NULL && var->slot() != NULL &&
4010 var->slot()->type() == Slot::LOOKUP) {
4011 // ----------------------------------
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004012 // JavaScript examples:
4013 //
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004014 // with (obj) foo(1, 2, 3) // foo may be in obj.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004015 //
4016 // function f() {};
4017 // function g() {
4018 // eval(...);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004019 // f(); // f could be in extension object.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004020 // }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021 // ----------------------------------
4022
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004023 JumpTarget slow, done;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004024
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004025 // Generate fast case for loading functions from slots that
4026 // correspond to local/global variables or arguments unless they
4027 // are shadowed by eval-introduced bindings.
4028 EmitDynamicLoadFromSlotFastCase(var->slot(),
4029 NOT_INSIDE_TYPEOF,
4030 &slow,
4031 &done);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004032
4033 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004034 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004035 frame_->EmitPush(cp);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004036 frame_->EmitPush(Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004037 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 // r0: slot value; r1: receiver
4039
4040 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004041 frame_->EmitPush(r0); // function
4042 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004044 // If fast case code has been generated, emit code to push the
4045 // function and receiver and have the slow path jump around this
4046 // code.
4047 if (done.is_linked()) {
4048 JumpTarget call;
4049 call.Jump();
4050 done.Bind();
4051 frame_->EmitPush(r0); // function
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004052 LoadGlobalReceiver(VirtualFrame::scratch0()); // receiver
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00004053 call.Bind();
4054 }
4055
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004056 // Call the function. At this point, everything is spilled but the
4057 // function and receiver are in r0 and r1.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004058 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004059 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004060
4061 } else if (property != NULL) {
4062 // Check if the key is a literal string.
4063 Literal* literal = property->key()->AsLiteral();
4064
4065 if (literal != NULL && literal->handle()->IsSymbol()) {
4066 // ------------------------------------------------------------------
4067 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
4068 // ------------------------------------------------------------------
4069
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004070 Handle<String> name = Handle<String>::cast(literal->handle());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004072 if (ArgumentsMode() == LAZY_ARGUMENTS_ALLOCATION &&
4073 name->IsEqualTo(CStrVector("apply")) &&
4074 args->length() == 2 &&
4075 args->at(1)->AsVariableProxy() != NULL &&
4076 args->at(1)->AsVariableProxy()->IsArguments()) {
4077 // Use the optimized Function.prototype.apply that avoids
4078 // allocating lazily allocated arguments objects.
4079 CallApplyLazy(property->obj(),
4080 args->at(0),
4081 args->at(1)->AsVariableProxy(),
4082 node->position());
4083
4084 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004085 Load(property->obj()); // Receiver.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004086 // Load the arguments.
4087 int arg_count = args->length();
4088 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004089 Load(args->at(i));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004090 }
4091
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004092 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00004093 // Set the name register and call the IC initialization code.
4094 __ mov(r2, Operand(name));
4095 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4096 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
4097 CodeForSourcePosition(node->position());
4098 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4099 __ ldr(cp, frame_->Context());
4100 frame_->EmitPush(r0);
4101 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004102
4103 } else {
4104 // -------------------------------------------
4105 // JavaScript example: 'array[index](1, 2, 3)'
4106 // -------------------------------------------
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004107 Load(property->obj());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004108 if (property->is_synthetic()) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004109 Load(property->key());
4110 EmitKeyedLoad();
4111 // Put the function below the receiver.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004112 // Use the global receiver.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00004113 frame_->EmitPush(r0); // Function.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004114 LoadGlobalReceiver(VirtualFrame::scratch0());
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004115 // Call the function.
4116 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
4117 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004118 } else {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004119 // Load the arguments.
4120 int arg_count = args->length();
4121 for (int i = 0; i < arg_count; i++) {
4122 Load(args->at(i));
4123 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004124
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004125 // Set the name register and call the IC initialization code.
4126 Load(property->key());
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004127 frame_->SpillAll();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00004128 frame_->EmitPop(r2); // Function name.
4129
4130 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4131 Handle<Code> stub = ComputeKeyedCallInitialize(arg_count, in_loop);
4132 CodeForSourcePosition(node->position());
4133 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
4134 __ ldr(cp, frame_->Context());
4135 frame_->EmitPush(r0);
4136 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 }
4138
4139 } else {
4140 // ----------------------------------
4141 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
4142 // ----------------------------------
4143
4144 // Load the function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004145 Load(function);
4146
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004147 // Pass the global proxy as the receiver.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004148 LoadGlobalReceiver(VirtualFrame::scratch0());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004151 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004152 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004153 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00004154 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004155}
4156
4157
ager@chromium.org7c537e22008-10-16 08:43:32 +00004158void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159#ifdef DEBUG
4160 int original_height = frame_->height();
4161#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004162 Comment cmnt(masm_, "[ CallNew");
4163
4164 // According to ECMA-262, section 11.2.2, page 44, the function
4165 // expression in new calls must be evaluated before the
4166 // arguments. This is different from ordinary calls, where the
4167 // actual function to call is resolved after the arguments have been
4168 // evaluated.
4169
4170 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004171 // receiver. There is no need to use the global proxy here because
4172 // it will always be replaced with a newly allocated object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004173 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004174 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004175
4176 // Push the arguments ("left-to-right") on the stack.
4177 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004178 int arg_count = args->length();
4179 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004180 Load(args->at(i));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004181 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004182
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004183 VirtualFrame::SpilledScope spilled_scope(frame_);
4184
mads.s.ager31e71382008-08-13 09:32:07 +00004185 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004186 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004187 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004188 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004190 // Call the construct call builtin that handles allocation and
4191 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004192 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004193 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004194 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004195
4196 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004197 __ str(r0, frame_->Top());
ager@chromium.orgac091b72010-05-05 07:34:42 +00004198 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004199}
4200
4201
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004202void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004203 Register scratch = VirtualFrame::scratch0();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004204 JumpTarget null, function, leave, non_function_constructor;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004205
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004206 // Load the object into register.
4207 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004208 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004209 Register tos = frame_->PopToRegister();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004210
4211 // If the object is a smi, we return null.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004212 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004213 null.Branch(eq);
4214
4215 // Check that the object is a JS object but take special care of JS
4216 // functions to make sure they have 'Function' as their class.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004217 __ CompareObjectType(tos, tos, scratch, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004218 null.Branch(lt);
4219
4220 // As long as JS_FUNCTION_TYPE is the last instance type and it is
4221 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
4222 // LAST_JS_OBJECT_TYPE.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00004223 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4224 STATIC_ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004225 __ cmp(scratch, Operand(JS_FUNCTION_TYPE));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004226 function.Branch(eq);
4227
4228 // Check if the constructor in the map is a function.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004229 __ ldr(tos, FieldMemOperand(tos, Map::kConstructorOffset));
4230 __ CompareObjectType(tos, scratch, scratch, JS_FUNCTION_TYPE);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004231 non_function_constructor.Branch(ne);
4232
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004233 // The tos register now contains the constructor function. Grab the
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004234 // instance class name from there.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004235 __ ldr(tos, FieldMemOperand(tos, JSFunction::kSharedFunctionInfoOffset));
4236 __ ldr(tos,
4237 FieldMemOperand(tos, SharedFunctionInfo::kInstanceClassNameOffset));
4238 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004239 leave.Jump();
4240
4241 // Functions have class 'Function'.
4242 function.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004243 __ mov(tos, Operand(Factory::function_class_symbol()));
4244 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004245 leave.Jump();
4246
4247 // Objects with a non-function constructor have class 'Object'.
4248 non_function_constructor.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004249 __ mov(tos, Operand(Factory::Object_symbol()));
4250 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004251 leave.Jump();
4252
4253 // Non-JS objects have class null.
4254 null.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004255 __ LoadRoot(tos, Heap::kNullValueRootIndex);
4256 frame_->EmitPush(tos);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004257
4258 // All done.
4259 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004260}
4261
4262
ager@chromium.org7c537e22008-10-16 08:43:32 +00004263void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004264 Register scratch = VirtualFrame::scratch0();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004265 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004266
4267 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004268 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004269 Register tos = frame_->PopToRegister(); // tos contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00004270 // if (object->IsSmi()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004271 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004272 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004273 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004274 __ CompareObjectType(tos, scratch, scratch, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276 // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004277 __ ldr(tos, FieldMemOperand(tos, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004278 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004279 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004280}
4281
4282
ager@chromium.org7c537e22008-10-16 08:43:32 +00004283void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004284 Register scratch1 = VirtualFrame::scratch0();
4285 Register scratch2 = VirtualFrame::scratch1();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004286 JumpTarget leave;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004287
4288 ASSERT(args->length() == 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004289 Load(args->at(0)); // Load the object.
4290 Load(args->at(1)); // Load the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004291 Register value = frame_->PopToRegister();
4292 Register object = frame_->PopToRegister(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004293 // if (object->IsSmi()) return object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004294 __ tst(object, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004296 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004297 __ CompareObjectType(object, scratch1, scratch1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004298 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299 // Store the value.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004300 __ str(value, FieldMemOperand(object, JSValue::kValueOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301 // Update the write barrier.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004302 __ RecordWrite(object,
4303 Operand(JSValue::kValueOffset - kHeapObjectTag),
4304 scratch1,
4305 scratch2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004307 leave.Bind();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004308 frame_->EmitPush(value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004309}
4310
4311
ager@chromium.org7c537e22008-10-16 08:43:32 +00004312void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004314 Load(args->at(0));
4315 Register reg = frame_->PopToRegister();
4316 __ tst(reg, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004317 cc_reg_ = eq;
4318}
4319
4320
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004321void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
4322 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
4323 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004324#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004325 if (ShouldGenerateLog(args->at(0))) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004326 Load(args->at(1));
4327 Load(args->at(2));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004328 frame_->CallRuntime(Runtime::kLog, 2);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004329 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00004330#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004331 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004332}
4333
4334
ager@chromium.org7c537e22008-10-16 08:43:32 +00004335void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004336 ASSERT(args->length() == 1);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004337 Load(args->at(0));
4338 Register reg = frame_->PopToRegister();
4339 __ tst(reg, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00004340 cc_reg_ = eq;
4341}
4342
4343
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004344// Generates the Math.pow method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004345void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
4346 ASSERT(args->length() == 2);
4347 Load(args->at(0));
4348 Load(args->at(1));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004349
4350 if (!CpuFeatures::IsSupported(VFP3)) {
4351 frame_->CallRuntime(Runtime::kMath_pow, 2);
4352 frame_->EmitPush(r0);
4353 } else {
4354 CpuFeatures::Scope scope(VFP3);
4355 JumpTarget runtime, done;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004356 Label exponent_nonsmi, base_nonsmi, powi, not_minus_half, allocate_return;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004357
4358 Register scratch1 = VirtualFrame::scratch0();
4359 Register scratch2 = VirtualFrame::scratch1();
4360
4361 // Get base and exponent to registers.
4362 Register exponent = frame_->PopToRegister();
4363 Register base = frame_->PopToRegister(exponent);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004364 Register heap_number_map = no_reg;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004365
4366 // Set the frame for the runtime jump target. The code below jumps to the
4367 // jump target label so the frame needs to be established before that.
4368 ASSERT(runtime.entry_frame() == NULL);
4369 runtime.set_entry_frame(frame_);
4370
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004371 __ BranchOnNotSmi(exponent, &exponent_nonsmi);
4372 __ BranchOnNotSmi(base, &base_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004373
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004374 heap_number_map = r6;
4375 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4376
4377 // Exponent is a smi and base is a smi. Get the smi value into vfp register
4378 // d1.
4379 __ SmiToDoubleVFPRegister(base, d1, scratch1, s0);
4380 __ b(&powi);
4381
4382 __ bind(&base_nonsmi);
4383 // Exponent is smi and base is non smi. Get the double value from the base
4384 // into vfp register d1.
4385 __ ObjectToDoubleVFPRegister(base, d1,
4386 scratch1, scratch2, heap_number_map, s0,
4387 runtime.entry_label());
4388
4389 __ bind(&powi);
4390
4391 // Load 1.0 into d0.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004392 __ vmov(d0, 1.0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004393
4394 // Get the absolute untagged value of the exponent and use that for the
4395 // calculation.
4396 __ mov(scratch1, Operand(exponent, ASR, kSmiTagSize), SetCC);
4397 __ rsb(scratch1, scratch1, Operand(0), LeaveCC, mi); // Negate if negative.
4398 __ vmov(d2, d0, mi); // 1.0 needed in d2 later if exponent is negative.
4399
4400 // Run through all the bits in the exponent. The result is calculated in d0
4401 // and d1 holds base^(bit^2).
4402 Label more_bits;
4403 __ bind(&more_bits);
4404 __ mov(scratch1, Operand(scratch1, LSR, 1), SetCC);
4405 __ vmul(d0, d0, d1, cs); // Multiply with base^(bit^2) if bit is set.
4406 __ vmul(d1, d1, d1, ne); // Don't bother calculating next d1 if done.
4407 __ b(ne, &more_bits);
4408
4409 // If exponent is positive we are done.
4410 __ cmp(exponent, Operand(0));
4411 __ b(ge, &allocate_return);
4412
4413 // If exponent is negative result is 1/result (d2 already holds 1.0 in that
4414 // case). However if d0 has reached infinity this will not provide the
4415 // correct result, so call runtime if that is the case.
4416 __ mov(scratch2, Operand(0x7FF00000));
4417 __ mov(scratch1, Operand(0));
4418 __ vmov(d1, scratch1, scratch2); // Load infinity into d1.
4419 __ vcmp(d0, d1);
4420 __ vmrs(pc);
4421 runtime.Branch(eq); // d0 reached infinity.
4422 __ vdiv(d0, d2, d0);
4423 __ b(&allocate_return);
4424
4425 __ bind(&exponent_nonsmi);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004426 // Special handling of raising to the power of -0.5 and 0.5. First check
4427 // that the value is a heap number and that the lower bits (which for both
4428 // values are zero).
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004429 heap_number_map = r6;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004430 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4431 __ ldr(scratch1, FieldMemOperand(exponent, HeapObject::kMapOffset));
4432 __ ldr(scratch2, FieldMemOperand(exponent, HeapNumber::kMantissaOffset));
4433 __ cmp(scratch1, heap_number_map);
4434 runtime.Branch(ne);
4435 __ tst(scratch2, scratch2);
4436 runtime.Branch(ne);
4437
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004438 // Load the higher bits (which contains the floating point exponent).
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004439 __ ldr(scratch1, FieldMemOperand(exponent, HeapNumber::kExponentOffset));
4440
4441 // Compare exponent with -0.5.
4442 __ cmp(scratch1, Operand(0xbfe00000));
4443 __ b(ne, &not_minus_half);
4444
4445 // Get the double value from the base into vfp register d0.
4446 __ ObjectToDoubleVFPRegister(base, d0,
4447 scratch1, scratch2, heap_number_map, s0,
4448 runtime.entry_label(),
4449 AVOID_NANS_AND_INFINITIES);
4450
4451 // Load 1.0 into d2.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004452 __ vmov(d2, 1.0);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004453
4454 // Calculate the reciprocal of the square root. 1/sqrt(x) = sqrt(1/x).
4455 __ vdiv(d0, d2, d0);
4456 __ vsqrt(d0, d0);
4457
4458 __ b(&allocate_return);
4459
4460 __ bind(&not_minus_half);
4461 // Compare exponent with 0.5.
4462 __ cmp(scratch1, Operand(0x3fe00000));
4463 runtime.Branch(ne);
4464
4465 // Get the double value from the base into vfp register d0.
4466 __ ObjectToDoubleVFPRegister(base, d0,
4467 scratch1, scratch2, heap_number_map, s0,
4468 runtime.entry_label(),
4469 AVOID_NANS_AND_INFINITIES);
4470 __ vsqrt(d0, d0);
4471
4472 __ bind(&allocate_return);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004473 Register scratch3 = r5;
4474 __ AllocateHeapNumberWithValue(scratch3, d0, scratch1, scratch2,
4475 heap_number_map, runtime.entry_label());
4476 __ mov(base, scratch3);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004477 done.Jump();
4478
4479 runtime.Bind();
4480
4481 // Push back the arguments again for the runtime call.
4482 frame_->EmitPush(base);
4483 frame_->EmitPush(exponent);
4484 frame_->CallRuntime(Runtime::kMath_pow, 2);
4485 __ Move(base, r0);
4486
4487 done.Bind();
4488 frame_->EmitPush(base);
4489 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004490}
4491
4492
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004493// Generates the Math.sqrt method.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004494void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
4495 ASSERT(args->length() == 1);
4496 Load(args->at(0));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00004497
4498 if (!CpuFeatures::IsSupported(VFP3)) {
4499 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4500 frame_->EmitPush(r0);
4501 } else {
4502 CpuFeatures::Scope scope(VFP3);
4503 JumpTarget runtime, done;
4504
4505 Register scratch1 = VirtualFrame::scratch0();
4506 Register scratch2 = VirtualFrame::scratch1();
4507
4508 // Get the value from the frame.
4509 Register tos = frame_->PopToRegister();
4510
4511 // Set the frame for the runtime jump target. The code below jumps to the
4512 // jump target label so the frame needs to be established before that.
4513 ASSERT(runtime.entry_frame() == NULL);
4514 runtime.set_entry_frame(frame_);
4515
4516 Register heap_number_map = r6;
4517 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4518
4519 // Get the double value from the heap number into vfp register d0.
4520 __ ObjectToDoubleVFPRegister(tos, d0,
4521 scratch1, scratch2, heap_number_map, s0,
4522 runtime.entry_label());
4523
4524 // Calculate the square root of d0 and place result in a heap number object.
4525 __ vsqrt(d0, d0);
4526 __ AllocateHeapNumberWithValue(
4527 tos, d0, scratch1, scratch2, heap_number_map, runtime.entry_label());
4528 done.Jump();
4529
4530 runtime.Bind();
4531 // Push back the argument again for the runtime call.
4532 frame_->EmitPush(tos);
4533 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
4534 __ Move(tos, r0);
4535
4536 done.Bind();
4537 frame_->EmitPush(tos);
4538 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004539}
4540
4541
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004542class DeferredStringCharCodeAt : public DeferredCode {
4543 public:
4544 DeferredStringCharCodeAt(Register object,
4545 Register index,
4546 Register scratch,
4547 Register result)
4548 : result_(result),
4549 char_code_at_generator_(object,
4550 index,
4551 scratch,
4552 result,
4553 &need_conversion_,
4554 &need_conversion_,
4555 &index_out_of_range_,
4556 STRING_INDEX_IS_NUMBER) {}
4557
4558 StringCharCodeAtGenerator* fast_case_generator() {
4559 return &char_code_at_generator_;
4560 }
4561
4562 virtual void Generate() {
4563 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4564 char_code_at_generator_.GenerateSlow(masm(), call_helper);
4565
4566 __ bind(&need_conversion_);
4567 // Move the undefined value into the result register, which will
4568 // trigger conversion.
4569 __ LoadRoot(result_, Heap::kUndefinedValueRootIndex);
4570 __ jmp(exit_label());
4571
4572 __ bind(&index_out_of_range_);
4573 // When the index is out of range, the spec requires us to return
4574 // NaN.
4575 __ LoadRoot(result_, Heap::kNanValueRootIndex);
4576 __ jmp(exit_label());
4577 }
4578
4579 private:
4580 Register result_;
4581
4582 Label need_conversion_;
4583 Label index_out_of_range_;
4584
4585 StringCharCodeAtGenerator char_code_at_generator_;
4586};
4587
4588
4589// This generates code that performs a String.prototype.charCodeAt() call
4590// or returns a smi in order to trigger conversion.
4591void CodeGenerator::GenerateStringCharCodeAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004592 Comment(masm_, "[ GenerateStringCharCodeAt");
kasper.lund7276f142008-07-30 08:49:36 +00004593 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004594
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004595 Load(args->at(0));
4596 Load(args->at(1));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004597
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004598 Register index = frame_->PopToRegister();
4599 Register object = frame_->PopToRegister(index);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004600
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004601 // We need two extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004602 Register scratch = VirtualFrame::scratch0();
4603 Register result = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004604
4605 DeferredStringCharCodeAt* deferred =
4606 new DeferredStringCharCodeAt(object,
4607 index,
4608 scratch,
4609 result);
4610 deferred->fast_case_generator()->GenerateFast(masm_);
4611 deferred->BindExit();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004612 frame_->EmitPush(result);
kasper.lund7276f142008-07-30 08:49:36 +00004613}
4614
4615
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004616class DeferredStringCharFromCode : public DeferredCode {
4617 public:
4618 DeferredStringCharFromCode(Register code,
4619 Register result)
4620 : char_from_code_generator_(code, result) {}
4621
4622 StringCharFromCodeGenerator* fast_case_generator() {
4623 return &char_from_code_generator_;
4624 }
4625
4626 virtual void Generate() {
4627 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4628 char_from_code_generator_.GenerateSlow(masm(), call_helper);
4629 }
4630
4631 private:
4632 StringCharFromCodeGenerator char_from_code_generator_;
4633};
4634
4635
4636// Generates code for creating a one-char string from a char code.
4637void CodeGenerator::GenerateStringCharFromCode(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004638 Comment(masm_, "[ GenerateStringCharFromCode");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004639 ASSERT(args->length() == 1);
4640
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004641 Load(args->at(0));
4642
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004643 Register result = frame_->GetTOSRegister();
4644 Register code = frame_->PopToRegister(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004645
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004646 DeferredStringCharFromCode* deferred = new DeferredStringCharFromCode(
4647 code, result);
4648 deferred->fast_case_generator()->GenerateFast(masm_);
4649 deferred->BindExit();
4650 frame_->EmitPush(result);
4651}
4652
4653
4654class DeferredStringCharAt : public DeferredCode {
4655 public:
4656 DeferredStringCharAt(Register object,
4657 Register index,
4658 Register scratch1,
4659 Register scratch2,
4660 Register result)
4661 : result_(result),
4662 char_at_generator_(object,
4663 index,
4664 scratch1,
4665 scratch2,
4666 result,
4667 &need_conversion_,
4668 &need_conversion_,
4669 &index_out_of_range_,
4670 STRING_INDEX_IS_NUMBER) {}
4671
4672 StringCharAtGenerator* fast_case_generator() {
4673 return &char_at_generator_;
4674 }
4675
4676 virtual void Generate() {
4677 VirtualFrameRuntimeCallHelper call_helper(frame_state());
4678 char_at_generator_.GenerateSlow(masm(), call_helper);
4679
4680 __ bind(&need_conversion_);
4681 // Move smi zero into the result register, which will trigger
4682 // conversion.
4683 __ mov(result_, Operand(Smi::FromInt(0)));
4684 __ jmp(exit_label());
4685
4686 __ bind(&index_out_of_range_);
4687 // When the index is out of range, the spec requires us to return
4688 // the empty string.
4689 __ LoadRoot(result_, Heap::kEmptyStringRootIndex);
4690 __ jmp(exit_label());
4691 }
4692
4693 private:
4694 Register result_;
4695
4696 Label need_conversion_;
4697 Label index_out_of_range_;
4698
4699 StringCharAtGenerator char_at_generator_;
4700};
4701
4702
4703// This generates code that performs a String.prototype.charAt() call
4704// or returns a smi in order to trigger conversion.
4705void CodeGenerator::GenerateStringCharAt(ZoneList<Expression*>* args) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004706 Comment(masm_, "[ GenerateStringCharAt");
4707 ASSERT(args->length() == 2);
4708
4709 Load(args->at(0));
4710 Load(args->at(1));
4711
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004712 Register index = frame_->PopToRegister();
4713 Register object = frame_->PopToRegister(index);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004714
4715 // We need three extra registers.
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004716 Register scratch1 = VirtualFrame::scratch0();
4717 Register scratch2 = VirtualFrame::scratch1();
4718 // Use r6 without notifying the virtual frame.
4719 Register result = r6;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004720
4721 DeferredStringCharAt* deferred =
4722 new DeferredStringCharAt(object,
4723 index,
4724 scratch1,
4725 scratch2,
4726 result);
4727 deferred->fast_case_generator()->GenerateFast(masm_);
4728 deferred->BindExit();
ager@chromium.orgac091b72010-05-05 07:34:42 +00004729 frame_->EmitPush(result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004730}
4731
4732
ager@chromium.org7c537e22008-10-16 08:43:32 +00004733void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004734 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004735 Load(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004736 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004737 // We need the CC bits to come out as not_equal in the case where the
4738 // object is a smi. This can't be done with the usual test opcode so
4739 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004740 Register possible_array = frame_->PopToRegister();
4741 Register scratch = VirtualFrame::scratch0();
4742 __ and_(scratch, possible_array, Operand(kSmiTagMask));
4743 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004744 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004745 // It is a heap object - get the map. Check if the object is a JS array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004746 __ CompareObjectType(possible_array, scratch, scratch, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004747 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004748 cc_reg_ = eq;
4749}
4750
4751
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004752void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004753 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004754 Load(args->at(0));
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004755 JumpTarget answer;
4756 // We need the CC bits to come out as not_equal in the case where the
4757 // object is a smi. This can't be done with the usual test opcode so
4758 // we use XOR to get the right CC bits.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004759 Register possible_regexp = frame_->PopToRegister();
4760 Register scratch = VirtualFrame::scratch0();
4761 __ and_(scratch, possible_regexp, Operand(kSmiTagMask));
4762 __ eor(scratch, scratch, Operand(kSmiTagMask), SetCC);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004763 answer.Branch(ne);
4764 // It is a heap object - get the map. Check if the object is a regexp.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004765 __ CompareObjectType(possible_regexp, scratch, scratch, JS_REGEXP_TYPE);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004766 answer.Bind();
4767 cc_reg_ = eq;
4768}
4769
4770
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004771void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
4772 // This generates a fast version of:
4773 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004774 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004775 Load(args->at(0));
4776 Register possible_object = frame_->PopToRegister();
4777 __ tst(possible_object, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004778 false_target()->Branch(eq);
4779
4780 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004781 __ cmp(possible_object, ip);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004782 true_target()->Branch(eq);
4783
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004784 Register map_reg = VirtualFrame::scratch0();
4785 __ ldr(map_reg, FieldMemOperand(possible_object, HeapObject::kMapOffset));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004786 // Undetectable objects behave like undefined when tested with typeof.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004787 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4788 __ tst(possible_object, Operand(1 << Map::kIsUndetectable));
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004789 false_target()->Branch(ne);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004790
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004791 __ ldrb(possible_object, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4792 __ cmp(possible_object, Operand(FIRST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004793 false_target()->Branch(lt);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004794 __ cmp(possible_object, Operand(LAST_JS_OBJECT_TYPE));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004795 cc_reg_ = le;
4796}
4797
4798
ricow@chromium.org4980dff2010-07-19 08:33:45 +00004799void CodeGenerator::GenerateIsSpecObject(ZoneList<Expression*>* args) {
4800 // This generates a fast version of:
4801 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp' ||
4802 // typeof(arg) == function).
4803 // It includes undetectable objects (as opposed to IsObject).
4804 ASSERT(args->length() == 1);
4805 Load(args->at(0));
4806 Register value = frame_->PopToRegister();
4807 __ tst(value, Operand(kSmiTagMask));
4808 false_target()->Branch(eq);
4809 // Check that this is an object.
4810 __ ldr(value, FieldMemOperand(value, HeapObject::kMapOffset));
4811 __ ldrb(value, FieldMemOperand(value, Map::kInstanceTypeOffset));
4812 __ cmp(value, Operand(FIRST_JS_OBJECT_TYPE));
4813 cc_reg_ = ge;
4814}
4815
4816
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004817// Deferred code to check whether the String JavaScript object is safe for using
4818// default value of. This code is called after the bit caching this information
4819// in the map has been checked with the map for the object in the map_result_
4820// register. On return the register map_result_ contains 1 for true and 0 for
4821// false.
4822class DeferredIsStringWrapperSafeForDefaultValueOf : public DeferredCode {
4823 public:
4824 DeferredIsStringWrapperSafeForDefaultValueOf(Register object,
4825 Register map_result,
4826 Register scratch1,
4827 Register scratch2)
4828 : object_(object),
4829 map_result_(map_result),
4830 scratch1_(scratch1),
4831 scratch2_(scratch2) { }
4832
4833 virtual void Generate() {
4834 Label false_result;
4835
4836 // Check that map is loaded as expected.
4837 if (FLAG_debug_code) {
4838 __ ldr(ip, FieldMemOperand(object_, HeapObject::kMapOffset));
4839 __ cmp(map_result_, ip);
4840 __ Assert(eq, "Map not in expected register");
4841 }
4842
4843 // Check for fast case object. Generate false result for slow case object.
4844 __ ldr(scratch1_, FieldMemOperand(object_, JSObject::kPropertiesOffset));
4845 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4846 __ LoadRoot(ip, Heap::kHashTableMapRootIndex);
4847 __ cmp(scratch1_, ip);
4848 __ b(eq, &false_result);
4849
4850 // Look for valueOf symbol in the descriptor array, and indicate false if
4851 // found. The type is not checked, so if it is a transition it is a false
4852 // negative.
4853 __ ldr(map_result_,
4854 FieldMemOperand(map_result_, Map::kInstanceDescriptorsOffset));
4855 __ ldr(scratch2_, FieldMemOperand(map_result_, FixedArray::kLengthOffset));
4856 // map_result_: descriptor array
4857 // scratch2_: length of descriptor array
4858 // Calculate the end of the descriptor array.
4859 STATIC_ASSERT(kSmiTag == 0);
4860 STATIC_ASSERT(kSmiTagSize == 1);
4861 STATIC_ASSERT(kPointerSize == 4);
4862 __ add(scratch1_,
4863 map_result_,
4864 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4865 __ add(scratch1_,
4866 scratch1_,
4867 Operand(scratch2_, LSL, kPointerSizeLog2 - kSmiTagSize));
4868
4869 // Calculate location of the first key name.
4870 __ add(map_result_,
4871 map_result_,
4872 Operand(FixedArray::kHeaderSize - kHeapObjectTag +
4873 DescriptorArray::kFirstIndex * kPointerSize));
4874 // Loop through all the keys in the descriptor array. If one of these is the
4875 // symbol valueOf the result is false.
4876 Label entry, loop;
4877 // The use of ip to store the valueOf symbol asumes that it is not otherwise
4878 // used in the loop below.
4879 __ mov(ip, Operand(Factory::value_of_symbol()));
4880 __ jmp(&entry);
4881 __ bind(&loop);
4882 __ ldr(scratch2_, MemOperand(map_result_, 0));
4883 __ cmp(scratch2_, ip);
4884 __ b(eq, &false_result);
4885 __ add(map_result_, map_result_, Operand(kPointerSize));
4886 __ bind(&entry);
4887 __ cmp(map_result_, Operand(scratch1_));
4888 __ b(ne, &loop);
4889
4890 // Reload map as register map_result_ was used as temporary above.
4891 __ ldr(map_result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4892
4893 // If a valueOf property is not found on the object check that it's
4894 // prototype is the un-modified String prototype. If not result is false.
4895 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kPrototypeOffset));
4896 __ tst(scratch1_, Operand(kSmiTagMask));
4897 __ b(eq, &false_result);
4898 __ ldr(scratch1_, FieldMemOperand(scratch1_, HeapObject::kMapOffset));
4899 __ ldr(scratch2_,
4900 CodeGenerator::ContextOperand(cp, Context::GLOBAL_INDEX));
4901 __ ldr(scratch2_,
4902 FieldMemOperand(scratch2_, GlobalObject::kGlobalContextOffset));
4903 __ ldr(scratch2_,
4904 CodeGenerator::ContextOperand(
4905 scratch2_, Context::STRING_FUNCTION_PROTOTYPE_MAP_INDEX));
4906 __ cmp(scratch1_, scratch2_);
4907 __ b(ne, &false_result);
4908
4909 // Set the bit in the map to indicate that it has been checked safe for
4910 // default valueOf and set true result.
4911 __ ldr(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4912 __ orr(scratch1_,
4913 scratch1_,
4914 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4915 __ str(scratch1_, FieldMemOperand(map_result_, Map::kBitField2Offset));
4916 __ mov(map_result_, Operand(1));
4917 __ jmp(exit_label());
4918 __ bind(&false_result);
4919 // Set false result.
4920 __ mov(map_result_, Operand(0));
4921 }
4922
4923 private:
4924 Register object_;
4925 Register map_result_;
4926 Register scratch1_;
4927 Register scratch2_;
4928};
4929
4930
4931void CodeGenerator::GenerateIsStringWrapperSafeForDefaultValueOf(
4932 ZoneList<Expression*>* args) {
4933 ASSERT(args->length() == 1);
4934 Load(args->at(0));
4935 Register obj = frame_->PopToRegister(); // Pop the string wrapper.
4936 if (FLAG_debug_code) {
4937 __ AbortIfSmi(obj);
4938 }
4939
4940 // Check whether this map has already been checked to be safe for default
4941 // valueOf.
4942 Register map_result = VirtualFrame::scratch0();
4943 __ ldr(map_result, FieldMemOperand(obj, HeapObject::kMapOffset));
4944 __ ldrb(ip, FieldMemOperand(map_result, Map::kBitField2Offset));
4945 __ tst(ip, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
4946 true_target()->Branch(ne);
4947
4948 // We need an additional two scratch registers for the deferred code.
4949 Register scratch1 = VirtualFrame::scratch1();
4950 // Use r6 without notifying the virtual frame.
4951 Register scratch2 = r6;
4952
4953 DeferredIsStringWrapperSafeForDefaultValueOf* deferred =
4954 new DeferredIsStringWrapperSafeForDefaultValueOf(
4955 obj, map_result, scratch1, scratch2);
4956 deferred->Branch(eq);
4957 deferred->BindExit();
4958 __ tst(map_result, Operand(map_result));
4959 cc_reg_ = ne;
4960}
4961
4962
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004963void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
4964 // This generates a fast version of:
4965 // (%_ClassOf(arg) === 'Function')
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004966 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004967 Load(args->at(0));
4968 Register possible_function = frame_->PopToRegister();
4969 __ tst(possible_function, Operand(kSmiTagMask));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004970 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004971 Register map_reg = VirtualFrame::scratch0();
4972 Register scratch = VirtualFrame::scratch1();
4973 __ CompareObjectType(possible_function, map_reg, scratch, JS_FUNCTION_TYPE);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00004974 cc_reg_ = eq;
4975}
4976
4977
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004978void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004979 ASSERT(args->length() == 1);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004980 Load(args->at(0));
4981 Register possible_undetectable = frame_->PopToRegister();
4982 __ tst(possible_undetectable, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004983 false_target()->Branch(eq);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004984 Register scratch = VirtualFrame::scratch0();
4985 __ ldr(scratch,
4986 FieldMemOperand(possible_undetectable, HeapObject::kMapOffset));
4987 __ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
4988 __ tst(scratch, Operand(1 << Map::kIsUndetectable));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004989 cc_reg_ = ne;
4990}
4991
4992
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004993void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004994 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004995
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004996 Register scratch0 = VirtualFrame::scratch0();
4997 Register scratch1 = VirtualFrame::scratch1();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004998 // Get the frame pointer for the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00004999 __ ldr(scratch0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005000
5001 // Skip the arguments adaptor frame if it exists.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005002 __ ldr(scratch1,
5003 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5004 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5005 __ ldr(scratch0,
5006 MemOperand(scratch0, StandardFrameConstants::kCallerFPOffset), eq);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005007
5008 // Check the marker in the calling frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005009 __ ldr(scratch1,
5010 MemOperand(scratch0, StandardFrameConstants::kMarkerOffset));
5011 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005012 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005013}
5014
5015
ager@chromium.org7c537e22008-10-16 08:43:32 +00005016void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005017 ASSERT(args->length() == 0);
5018
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005019 Register tos = frame_->GetTOSRegister();
5020 Register scratch0 = VirtualFrame::scratch0();
5021 Register scratch1 = VirtualFrame::scratch1();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005022
lrn@chromium.org25156de2010-04-06 13:10:27 +00005023 // Check if the calling frame is an arguments adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005024 __ ldr(scratch0,
5025 MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5026 __ ldr(scratch1,
5027 MemOperand(scratch0, StandardFrameConstants::kContextOffset));
5028 __ cmp(scratch1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5029
5030 // Get the number of formal parameters.
5031 __ mov(tos, Operand(Smi::FromInt(scope()->num_parameters())), LeaveCC, ne);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005032
5033 // Arguments adaptor case: Read the arguments length from the
5034 // adaptor frame.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005035 __ ldr(tos,
5036 MemOperand(scratch0, ArgumentsAdaptorFrameConstants::kLengthOffset),
5037 eq);
lrn@chromium.org25156de2010-04-06 13:10:27 +00005038
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005039 frame_->EmitPush(tos);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005040}
5041
5042
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00005043void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005044 ASSERT(args->length() == 1);
5045
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005046 // Satisfy contract with ArgumentsAccessStub:
5047 // Load the key into r1 and the formal parameters count into r0.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005048 Load(args->at(0));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005049 frame_->PopToR1();
5050 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005051 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005052
5053 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005054 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005055 frame_->CallStub(&stub, 0);
5056 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005057}
5058
5059
ager@chromium.org357bf652010-04-12 11:30:10 +00005060void CodeGenerator::GenerateRandomHeapNumber(
5061 ZoneList<Expression*>* args) {
5062 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005063 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00005064
5065 Label slow_allocate_heapnumber;
5066 Label heapnumber_allocated;
5067
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005068 __ LoadRoot(r6, Heap::kHeapNumberMapRootIndex);
5069 __ AllocateHeapNumber(r4, r1, r2, r6, &slow_allocate_heapnumber);
ager@chromium.org357bf652010-04-12 11:30:10 +00005070 __ jmp(&heapnumber_allocated);
5071
5072 __ bind(&slow_allocate_heapnumber);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00005073 // Allocate a heap number.
5074 __ CallRuntime(Runtime::kNumberAlloc, 0);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005075 __ mov(r4, Operand(r0));
ager@chromium.org357bf652010-04-12 11:30:10 +00005076
5077 __ bind(&heapnumber_allocated);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00005078
5079 // Convert 32 random bits in r0 to 0.(32 random bits) in a double
5080 // by computing:
5081 // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
5082 if (CpuFeatures::IsSupported(VFP3)) {
5083 __ PrepareCallCFunction(0, r1);
5084 __ CallCFunction(ExternalReference::random_uint32_function(), 0);
5085
5086 CpuFeatures::Scope scope(VFP3);
5087 // 0x41300000 is the top half of 1.0 x 2^20 as a double.
5088 // Create this constant using mov/orr to avoid PC relative load.
5089 __ mov(r1, Operand(0x41000000));
5090 __ orr(r1, r1, Operand(0x300000));
5091 // Move 0x41300000xxxxxxxx (x = random bits) to VFP.
5092 __ vmov(d7, r0, r1);
5093 // Move 0x4130000000000000 to VFP.
5094 __ mov(r0, Operand(0));
5095 __ vmov(d8, r0, r1);
5096 // Subtract and store the result in the heap number.
5097 __ vsub(d7, d7, d8);
5098 __ sub(r0, r4, Operand(kHeapObjectTag));
5099 __ vstr(d7, r0, HeapNumber::kValueOffset);
5100 frame_->EmitPush(r4);
5101 } else {
5102 __ mov(r0, Operand(r4));
5103 __ PrepareCallCFunction(1, r1);
5104 __ CallCFunction(
5105 ExternalReference::fill_heap_number_with_random_function(), 1);
5106 frame_->EmitPush(r0);
5107 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005108}
5109
5110
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005111void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
5112 ASSERT_EQ(2, args->length());
5113
5114 Load(args->at(0));
5115 Load(args->at(1));
5116
ager@chromium.org5c838252010-02-19 08:53:10 +00005117 StringAddStub stub(NO_STRING_ADD_FLAGS);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005118 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005119 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005120 frame_->EmitPush(r0);
5121}
5122
5123
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005124void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
5125 ASSERT_EQ(3, args->length());
5126
5127 Load(args->at(0));
5128 Load(args->at(1));
5129 Load(args->at(2));
5130
ager@chromium.org5c838252010-02-19 08:53:10 +00005131 SubStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005132 frame_->SpillAll();
ager@chromium.org5c838252010-02-19 08:53:10 +00005133 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005134 frame_->EmitPush(r0);
5135}
5136
5137
5138void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
5139 ASSERT_EQ(2, args->length());
5140
5141 Load(args->at(0));
5142 Load(args->at(1));
5143
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005144 StringCompareStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005145 frame_->SpillAll();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005146 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005147 frame_->EmitPush(r0);
5148}
5149
5150
5151void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
5152 ASSERT_EQ(4, args->length());
5153
5154 Load(args->at(0));
5155 Load(args->at(1));
5156 Load(args->at(2));
5157 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005158 RegExpExecStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005159 frame_->SpillAll();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005160 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005161 frame_->EmitPush(r0);
5162}
5163
5164
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005165void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
5166 // No stub. This code only occurs a few times in regexp.js.
5167 const int kMaxInlineLength = 100;
5168 ASSERT_EQ(3, args->length());
5169 Load(args->at(0)); // Size of array, smi.
5170 Load(args->at(1)); // "index" property value.
5171 Load(args->at(2)); // "input" property value.
5172 {
5173 VirtualFrame::SpilledScope spilled_scope(frame_);
5174 Label slowcase;
5175 Label done;
5176 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5177 STATIC_ASSERT(kSmiTag == 0);
5178 STATIC_ASSERT(kSmiTagSize == 1);
5179 __ tst(r1, Operand(kSmiTagMask));
5180 __ b(ne, &slowcase);
5181 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5182 __ b(hi, &slowcase);
5183 // Smi-tagging is equivalent to multiplying by 2.
5184 // Allocate RegExpResult followed by FixedArray with size in ebx.
5185 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5186 // Elements: [Map][Length][..elements..]
5187 // Size of JSArray with two in-object properties and the header of a
5188 // FixedArray.
5189 int objects_size =
5190 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5191 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5192 __ add(r2, r5, Operand(objects_size));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00005193 __ AllocateInNewSpace(
5194 r2, // In: Size, in words.
5195 r0, // Out: Start of allocation (tagged).
5196 r3, // Scratch register.
5197 r4, // Scratch register.
5198 &slowcase,
5199 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005200 // r0: Start of allocated area, object-tagged.
5201 // r1: Number of elements in array, as smi.
5202 // r5: Number of elements, untagged.
5203
5204 // Set JSArray map to global.regexp_result_map().
5205 // Set empty properties FixedArray.
5206 // Set elements to point to FixedArray allocated right after the JSArray.
5207 // Interleave operations for better latency.
5208 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5209 __ add(r3, r0, Operand(JSRegExpResult::kSize));
5210 __ mov(r4, Operand(Factory::empty_fixed_array()));
5211 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5212 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5213 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5214 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5215 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5216
5217 // Set input, index and length fields from arguments.
5218 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
5219 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5220 __ add(sp, sp, Operand(kPointerSize));
5221 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5222 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5223
5224 // Fill out the elements FixedArray.
5225 // r0: JSArray, tagged.
5226 // r3: FixedArray, tagged.
5227 // r5: Number of elements in array, untagged.
5228
5229 // Set map.
5230 __ mov(r2, Operand(Factory::fixed_array_map()));
5231 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5232 // Set FixedArray length.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005233 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5234 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00005235 // Fill contents of fixed-array with the-hole.
5236 __ mov(r2, Operand(Factory::the_hole_value()));
5237 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5238 // Fill fixed array elements with hole.
5239 // r0: JSArray, tagged.
5240 // r2: the hole.
5241 // r3: Start of elements in FixedArray.
5242 // r5: Number of elements to fill.
5243 Label loop;
5244 __ tst(r5, Operand(r5));
5245 __ bind(&loop);
5246 __ b(le, &done); // Jump if r1 is negative or zero.
5247 __ sub(r5, r5, Operand(1), SetCC);
5248 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5249 __ jmp(&loop);
5250
5251 __ bind(&slowcase);
5252 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
5253
5254 __ bind(&done);
5255 }
5256 frame_->Forget(3);
5257 frame_->EmitPush(r0);
5258}
5259
5260
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005261class DeferredSearchCache: public DeferredCode {
5262 public:
5263 DeferredSearchCache(Register dst, Register cache, Register key)
5264 : dst_(dst), cache_(cache), key_(key) {
5265 set_comment("[ DeferredSearchCache");
5266 }
5267
5268 virtual void Generate();
5269
5270 private:
5271 Register dst_, cache_, key_;
5272};
5273
5274
5275void DeferredSearchCache::Generate() {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00005276 __ Push(cache_, key_);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005277 __ CallRuntime(Runtime::kGetFromCache, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005278 __ Move(dst_, r0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005279}
5280
5281
5282void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
5283 ASSERT_EQ(2, args->length());
5284
5285 ASSERT_NE(NULL, args->at(0)->AsLiteral());
5286 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
5287
5288 Handle<FixedArray> jsfunction_result_caches(
5289 Top::global_context()->jsfunction_result_caches());
5290 if (jsfunction_result_caches->length() <= cache_id) {
5291 __ Abort("Attempt to use undefined cache.");
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005292 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005293 return;
5294 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005295
5296 Load(args->at(1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005297
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005298 frame_->PopToR1();
5299 frame_->SpillAll();
5300 Register key = r1; // Just poped to r1
5301 Register result = r0; // Free, as frame has just been spilled.
5302 Register scratch1 = VirtualFrame::scratch0();
5303 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005304
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005305 __ ldr(scratch1, ContextOperand(cp, Context::GLOBAL_INDEX));
5306 __ ldr(scratch1,
5307 FieldMemOperand(scratch1, GlobalObject::kGlobalContextOffset));
5308 __ ldr(scratch1,
5309 ContextOperand(scratch1, Context::JSFUNCTION_RESULT_CACHES_INDEX));
5310 __ ldr(scratch1,
5311 FieldMemOperand(scratch1, FixedArray::OffsetOfElementAt(cache_id)));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005312
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005313 DeferredSearchCache* deferred =
5314 new DeferredSearchCache(result, scratch1, key);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005315
5316 const int kFingerOffset =
5317 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00005318 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005319 __ ldr(result, FieldMemOperand(scratch1, kFingerOffset));
5320 // result now holds finger offset as a smi.
5321 __ add(scratch2, scratch1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5322 // scratch2 now points to the start of fixed array elements.
5323 __ ldr(result,
5324 MemOperand(
5325 scratch2, result, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
5326 // Note side effect of PreIndex: scratch2 now points to the key of the pair.
5327 __ cmp(key, result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005328 deferred->Branch(ne);
5329
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005330 __ ldr(result, MemOperand(scratch2, kPointerSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005331
5332 deferred->BindExit();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005333 frame_->EmitPush(result);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005334}
5335
5336
ager@chromium.org5c838252010-02-19 08:53:10 +00005337void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
5338 ASSERT_EQ(args->length(), 1);
5339
5340 // Load the argument on the stack and jump to the runtime.
5341 Load(args->at(0));
5342
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005343 NumberToStringStub stub;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005344 frame_->SpillAll();
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005345 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005346 frame_->EmitPush(r0);
5347}
5348
5349
ager@chromium.orgac091b72010-05-05 07:34:42 +00005350class DeferredSwapElements: public DeferredCode {
5351 public:
5352 DeferredSwapElements(Register object, Register index1, Register index2)
5353 : object_(object), index1_(index1), index2_(index2) {
5354 set_comment("[ DeferredSwapElements");
5355 }
5356
5357 virtual void Generate();
5358
5359 private:
5360 Register object_, index1_, index2_;
5361};
5362
5363
5364void DeferredSwapElements::Generate() {
5365 __ push(object_);
5366 __ push(index1_);
5367 __ push(index2_);
5368 __ CallRuntime(Runtime::kSwapElements, 3);
5369}
5370
5371
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005372void CodeGenerator::GenerateSwapElements(ZoneList<Expression*>* args) {
5373 Comment cmnt(masm_, "[ GenerateSwapElements");
5374
5375 ASSERT_EQ(3, args->length());
5376
5377 Load(args->at(0));
5378 Load(args->at(1));
5379 Load(args->at(2));
5380
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005381 VirtualFrame::SpilledScope spilled_scope(frame_);
5382
ager@chromium.orgac091b72010-05-05 07:34:42 +00005383 Register index2 = r2;
5384 Register index1 = r1;
5385 Register object = r0;
5386 Register tmp1 = r3;
5387 Register tmp2 = r4;
5388
5389 frame_->EmitPop(index2);
5390 frame_->EmitPop(index1);
5391 frame_->EmitPop(object);
5392
5393 DeferredSwapElements* deferred =
5394 new DeferredSwapElements(object, index1, index2);
5395
5396 // Fetch the map and check if array is in fast case.
5397 // Check that object doesn't require security checks and
5398 // has no indexed interceptor.
5399 __ CompareObjectType(object, tmp1, tmp2, FIRST_JS_OBJECT_TYPE);
5400 deferred->Branch(lt);
5401 __ ldrb(tmp2, FieldMemOperand(tmp1, Map::kBitFieldOffset));
5402 __ tst(tmp2, Operand(KeyedLoadIC::kSlowCaseBitFieldMask));
5403 deferred->Branch(nz);
5404
5405 // Check the object's elements are in fast case.
5406 __ ldr(tmp1, FieldMemOperand(object, JSObject::kElementsOffset));
5407 __ ldr(tmp2, FieldMemOperand(tmp1, HeapObject::kMapOffset));
5408 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5409 __ cmp(tmp2, ip);
5410 deferred->Branch(ne);
5411
5412 // Smi-tagging is equivalent to multiplying by 2.
5413 STATIC_ASSERT(kSmiTag == 0);
5414 STATIC_ASSERT(kSmiTagSize == 1);
5415
5416 // Check that both indices are smis.
5417 __ mov(tmp2, index1);
5418 __ orr(tmp2, tmp2, index2);
5419 __ tst(tmp2, Operand(kSmiTagMask));
5420 deferred->Branch(nz);
5421
5422 // Bring the offsets into the fixed array in tmp1 into index1 and
5423 // index2.
5424 __ mov(tmp2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5425 __ add(index1, tmp2, Operand(index1, LSL, kPointerSizeLog2 - kSmiTagSize));
5426 __ add(index2, tmp2, Operand(index2, LSL, kPointerSizeLog2 - kSmiTagSize));
5427
5428 // Swap elements.
5429 Register tmp3 = object;
5430 object = no_reg;
5431 __ ldr(tmp3, MemOperand(tmp1, index1));
5432 __ ldr(tmp2, MemOperand(tmp1, index2));
5433 __ str(tmp3, MemOperand(tmp1, index2));
5434 __ str(tmp2, MemOperand(tmp1, index1));
5435
5436 Label done;
5437 __ InNewSpace(tmp1, tmp2, eq, &done);
5438 // Possible optimization: do a check that both values are Smis
5439 // (or them and test against Smi mask.)
5440
5441 __ mov(tmp2, tmp1);
5442 RecordWriteStub recordWrite1(tmp1, index1, tmp3);
5443 __ CallStub(&recordWrite1);
5444
5445 RecordWriteStub recordWrite2(tmp2, index2, tmp3);
5446 __ CallStub(&recordWrite2);
5447
5448 __ bind(&done);
5449
5450 deferred->BindExit();
5451 __ LoadRoot(tmp1, Heap::kUndefinedValueRootIndex);
5452 frame_->EmitPush(tmp1);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005453}
5454
5455
ager@chromium.org357bf652010-04-12 11:30:10 +00005456void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
5457 Comment cmnt(masm_, "[ GenerateCallFunction");
5458
5459 ASSERT(args->length() >= 2);
5460
5461 int n_args = args->length() - 2; // for receiver and function.
5462 Load(args->at(0)); // receiver
5463 for (int i = 0; i < n_args; i++) {
5464 Load(args->at(i + 1));
5465 }
5466 Load(args->at(n_args + 1)); // function
5467 frame_->CallJSFunction(n_args);
5468 frame_->EmitPush(r0);
5469}
5470
5471
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005472void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
5473 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005474 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005475 if (CpuFeatures::IsSupported(VFP3)) {
5476 TranscendentalCacheStub stub(TranscendentalCache::SIN);
5477 frame_->SpillAllButCopyTOSToR0();
5478 frame_->CallStub(&stub, 1);
5479 } else {
5480 frame_->CallRuntime(Runtime::kMath_sin, 1);
5481 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005482 frame_->EmitPush(r0);
5483}
5484
5485
5486void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
5487 ASSERT_EQ(args->length(), 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005488 Load(args->at(0));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00005489 if (CpuFeatures::IsSupported(VFP3)) {
5490 TranscendentalCacheStub stub(TranscendentalCache::COS);
5491 frame_->SpillAllButCopyTOSToR0();
5492 frame_->CallStub(&stub, 1);
5493 } else {
5494 frame_->CallRuntime(Runtime::kMath_cos, 1);
5495 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005496 frame_->EmitPush(r0);
5497}
5498
5499
ager@chromium.org7c537e22008-10-16 08:43:32 +00005500void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005501 ASSERT(args->length() == 2);
5502
5503 // Load the two objects into registers and perform the comparison.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005504 Load(args->at(0));
5505 Load(args->at(1));
5506 Register lhs = frame_->PopToRegister();
5507 Register rhs = frame_->PopToRegister(lhs);
5508 __ cmp(lhs, rhs);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005509 cc_reg_ = eq;
5510}
5511
5512
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005513void CodeGenerator::GenerateIsRegExpEquivalent(ZoneList<Expression*>* args) {
5514 ASSERT(args->length() == 2);
5515
5516 // Load the two objects into registers and perform the comparison.
5517 Load(args->at(0));
5518 Load(args->at(1));
5519 Register right = frame_->PopToRegister();
5520 Register left = frame_->PopToRegister(right);
5521 Register tmp = frame_->scratch0();
5522 Register tmp2 = frame_->scratch1();
5523
5524 // Jumps to done must have the eq flag set if the test is successful
5525 // and clear if the test has failed.
5526 Label done;
5527
5528 // Fail if either is a non-HeapObject.
5529 __ cmp(left, Operand(right));
5530 __ b(eq, &done);
5531 __ and_(tmp, left, Operand(right));
5532 __ eor(tmp, tmp, Operand(kSmiTagMask));
5533 __ tst(tmp, Operand(kSmiTagMask));
5534 __ b(ne, &done);
5535 __ ldr(tmp, FieldMemOperand(left, HeapObject::kMapOffset));
5536 __ ldrb(tmp2, FieldMemOperand(tmp, Map::kInstanceTypeOffset));
5537 __ cmp(tmp2, Operand(JS_REGEXP_TYPE));
5538 __ b(ne, &done);
5539 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
5540 __ cmp(tmp, Operand(tmp2));
5541 __ b(ne, &done);
5542 __ ldr(tmp, FieldMemOperand(left, JSRegExp::kDataOffset));
5543 __ ldr(tmp2, FieldMemOperand(right, JSRegExp::kDataOffset));
5544 __ cmp(tmp, tmp2);
5545 __ bind(&done);
5546 cc_reg_ = eq;
5547}
5548
5549
5550
ager@chromium.org7c537e22008-10-16 08:43:32 +00005551void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005552#ifdef DEBUG
5553 int original_height = frame_->height();
5554#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005555 if (CheckForInlineRuntimeCall(node)) {
5556 ASSERT((has_cc() && frame_->height() == original_height) ||
5557 (!has_cc() && frame_->height() == original_height + 1));
5558 return;
5559 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005560
5561 ZoneList<Expression*>* args = node->arguments();
5562 Comment cmnt(masm_, "[ CallRuntime");
5563 Runtime::Function* function = node->function();
5564
ager@chromium.org41826e72009-03-30 13:30:57 +00005565 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00005566 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00005567 // Push the builtins object found in the current global object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005568 Register scratch = VirtualFrame::scratch0();
5569 __ ldr(scratch, GlobalObject());
5570 Register builtins = frame_->GetTOSRegister();
5571 __ ldr(builtins, FieldMemOperand(scratch, GlobalObject::kBuiltinsOffset));
5572 frame_->EmitPush(builtins);
ager@chromium.org41826e72009-03-30 13:30:57 +00005573 }
mads.s.ager31e71382008-08-13 09:32:07 +00005574
ager@chromium.org41826e72009-03-30 13:30:57 +00005575 // Push the arguments ("left-to-right").
5576 int arg_count = args->length();
5577 for (int i = 0; i < arg_count; i++) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005578 Load(args->at(i));
ager@chromium.org41826e72009-03-30 13:30:57 +00005579 }
mads.s.ager31e71382008-08-13 09:32:07 +00005580
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005581 VirtualFrame::SpilledScope spilled_scope(frame_);
5582
ager@chromium.org41826e72009-03-30 13:30:57 +00005583 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005584 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00005585 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005586 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
5587 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005588 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005589 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005590 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00005591 } else {
5592 // Call the C runtime function.
5593 frame_->CallRuntime(function, arg_count);
5594 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005595 }
ager@chromium.orgac091b72010-05-05 07:34:42 +00005596 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005597}
5598
5599
ager@chromium.org7c537e22008-10-16 08:43:32 +00005600void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005601#ifdef DEBUG
5602 int original_height = frame_->height();
5603#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005604 Comment cmnt(masm_, "[ UnaryOperation");
5605
5606 Token::Value op = node->op();
5607
5608 if (op == Token::NOT) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005609 LoadCondition(node->expression(), false_target(), true_target(), true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005610 // LoadCondition may (and usually does) leave a test and branch to
5611 // be emitted by the caller. In that case, negate the condition.
5612 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005613
5614 } else if (op == Token::DELETE) {
5615 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00005616 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005617 if (property != NULL) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005618 Load(property->obj());
5619 Load(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005620 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005621 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005622
mads.s.ager31e71382008-08-13 09:32:07 +00005623 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005624 Slot* slot = variable->slot();
5625 if (variable->is_global()) {
5626 LoadGlobal();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005627 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005628 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005629 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005630
5631 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
5632 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005633 frame_->EmitPush(cp);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005634 frame_->EmitPush(Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005635 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005636 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005637 frame_->EmitPush(r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005638 frame_->EmitPush(Operand(variable->name()));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005639 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005640 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005641
mads.s.ager31e71382008-08-13 09:32:07 +00005642 } else {
5643 // Default: Result of deleting non-global, not dynamically
5644 // introduced variables is false.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005645 frame_->EmitPushRoot(Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00005646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005647
5648 } else {
5649 // Default: Result of deleting expressions is true.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005650 Load(node->expression()); // may have side-effects
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005651 frame_->Drop();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005652 frame_->EmitPushRoot(Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005653 }
5654
5655 } else if (op == Token::TYPEOF) {
5656 // Special case for loading the typeof expression; see comment on
5657 // LoadTypeofExpression().
5658 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005659 frame_->CallRuntime(Runtime::kTypeof, 1);
5660 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005661
5662 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005663 bool can_overwrite =
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005664 (node->expression()->AsBinaryOperation() != NULL &&
5665 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005666 UnaryOverwriteMode overwrite =
5667 can_overwrite ? UNARY_OVERWRITE : UNARY_NO_OVERWRITE;
5668
5669 bool no_negative_zero = node->expression()->no_negative_zero();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005670 Load(node->expression());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005671 switch (op) {
5672 case Token::NOT:
5673 case Token::DELETE:
5674 case Token::TYPEOF:
5675 UNREACHABLE(); // handled above
5676 break;
5677
5678 case Token::SUB: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005679 frame_->PopToR0();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005680 GenericUnaryOpStub stub(
5681 Token::SUB,
5682 overwrite,
5683 no_negative_zero ? kIgnoreNegativeZero : kStrictNegativeZero);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005684 frame_->CallStub(&stub, 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005685 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686 break;
5687 }
5688
5689 case Token::BIT_NOT: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005690 Register tos = frame_->PopToRegister();
5691 JumpTarget not_smi_label;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005692 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005693 // Smi check.
5694 __ tst(tos, Operand(kSmiTagMask));
5695 not_smi_label.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005696
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005697 __ mvn(tos, Operand(tos));
5698 __ bic(tos, tos, Operand(kSmiTagMask)); // Bit-clear inverted smi-tag.
5699 frame_->EmitPush(tos);
5700 // The fast case is the first to jump to the continue label, so it gets
5701 // to decide the virtual frame layout.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005702 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005703
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005704 not_smi_label.Bind();
5705 frame_->SpillAll();
5706 __ Move(r0, tos);
5707 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
5708 frame_->CallStub(&stub, 0);
5709 frame_->EmitPush(r0);
5710
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005711 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005712 break;
5713 }
5714
5715 case Token::VOID:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005716 frame_->Drop();
5717 frame_->EmitPushRoot(Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005718 break;
5719
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005720 case Token::ADD: {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005721 Register tos = frame_->Peek();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005722 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005723 JumpTarget continue_label;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005724 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005725 continue_label.Branch(eq);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005726
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005727 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005728 frame_->EmitPush(r0);
5729
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005730 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005731 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005732 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005733 default:
5734 UNREACHABLE();
5735 }
5736 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005737 ASSERT(!has_valid_frame() ||
5738 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005739 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005740}
5741
5742
ager@chromium.org7c537e22008-10-16 08:43:32 +00005743void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005744#ifdef DEBUG
5745 int original_height = frame_->height();
5746#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005747 Comment cmnt(masm_, "[ CountOperation");
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00005748 VirtualFrame::RegisterAllocationScope scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005749
5750 bool is_postfix = node->is_postfix();
5751 bool is_increment = node->op() == Token::INC;
5752
5753 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
5754 bool is_const = (var != NULL && var->mode() == Variable::CONST);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005755 bool is_slot = (var != NULL && var->mode() == Variable::VAR);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005756
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005757 if (!is_const && is_slot && type_info(var->slot()).IsSmi()) {
5758 // The type info declares that this variable is always a Smi. That
5759 // means it is a Smi both before and after the increment/decrement.
5760 // Lets make use of that to make a very minimal count.
5761 Reference target(this, node->expression(), !is_const);
5762 ASSERT(!target.is_illegal());
5763 target.GetValue(); // Pushes the value.
5764 Register value = frame_->PopToRegister();
5765 if (is_postfix) frame_->EmitPush(value);
5766 if (is_increment) {
5767 __ add(value, value, Operand(Smi::FromInt(1)));
5768 } else {
5769 __ sub(value, value, Operand(Smi::FromInt(1)));
5770 }
5771 frame_->EmitPush(value);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005772 target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005773 if (is_postfix) frame_->Pop();
5774 ASSERT_EQ(original_height + 1, frame_->height());
5775 return;
5776 }
5777
5778 // If it's a postfix expression and its result is not ignored and the
5779 // reference is non-trivial, then push a placeholder on the stack now
5780 // to hold the result of the expression.
5781 bool placeholder_pushed = false;
5782 if (!is_slot && is_postfix) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005783 frame_->EmitPush(Operand(Smi::FromInt(0)));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005784 placeholder_pushed = true;
mads.s.ager31e71382008-08-13 09:32:07 +00005785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005786
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005787 // A constant reference is not saved to, so a constant reference is not a
5788 // compound assignment reference.
5789 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005790 if (target.is_illegal()) {
5791 // Spoof the virtual frame to have the expected height (one higher
5792 // than on entry).
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005793 if (!placeholder_pushed) frame_->EmitPush(Operand(Smi::FromInt(0)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00005794 ASSERT_EQ(original_height + 1, frame_->height());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005795 return;
5796 }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005797
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005798 // This pushes 0, 1 or 2 words on the object to be used later when updating
5799 // the target. It also pushes the current value of the target.
ager@chromium.org357bf652010-04-12 11:30:10 +00005800 target.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005801
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005802 JumpTarget slow;
5803 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005804
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005805 Register value = frame_->PopToRegister();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005806
5807 // Postfix: Store the old value as the result.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005808 if (placeholder_pushed) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005809 frame_->SetElementAt(value, target.size());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005810 } else if (is_postfix) {
5811 frame_->EmitPush(value);
5812 __ mov(VirtualFrame::scratch0(), value);
5813 value = VirtualFrame::scratch0();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005814 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005815
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005816 // Check for smi operand.
5817 __ tst(value, Operand(kSmiTagMask));
5818 slow.Branch(ne);
5819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820 // Perform optimistic increment/decrement.
5821 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005822 __ add(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005823 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005824 __ sub(value, value, Operand(Smi::FromInt(1)), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005825 }
5826
5827 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005828 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005829
5830 // Revert optimistic increment/decrement.
5831 if (is_increment) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005832 __ sub(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005833 } else {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005834 __ add(value, value, Operand(Smi::FromInt(1)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005835 }
5836
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005837 // Slow case: Convert to number. At this point the
5838 // value to be incremented is in the value register..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005839 slow.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005840
5841 // Convert the operand to a number.
5842 frame_->EmitPush(value);
5843
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00005844 {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005845 VirtualFrame::SpilledScope spilled(frame_);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005846 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005847
5848 if (is_postfix) {
5849 // Postfix: store to result (on the stack).
5850 __ str(r0, frame_->ElementAt(target.size()));
5851 }
5852
5853 // Compute the new value.
5854 frame_->EmitPush(r0);
5855 frame_->EmitPush(Operand(Smi::FromInt(1)));
5856 if (is_increment) {
5857 frame_->CallRuntime(Runtime::kNumberAdd, 2);
5858 } else {
5859 frame_->CallRuntime(Runtime::kNumberSub, 2);
5860 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005861 }
5862
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005863 __ Move(value, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005864 // Store the new value in the target if not const.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005865 // At this point the answer is in the value register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005866 exit.Bind();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005867 frame_->EmitPush(value);
5868 // Set the target with the result, leaving the result on
5869 // top of the stack. Removes the target from the stack if
5870 // it has a non-zero size.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00005871 if (!is_const) target.SetValue(NOT_CONST_INIT, LIKELY_SMI);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005872 }
5873
5874 // Postfix: Discard the new value and use the old.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00005875 if (is_postfix) frame_->Pop();
ager@chromium.orgac091b72010-05-05 07:34:42 +00005876 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005877}
5878
5879
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005880void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005881 // According to ECMA-262 section 11.11, page 58, the binary logical
5882 // operators must yield the result of one of the two expressions
5883 // before any ToBoolean() conversions. This means that the value
5884 // produced by a && or || operator is not necessarily a boolean.
5885
5886 // NOTE: If the left hand side produces a materialized value (not in
5887 // the CC register), we force the right hand side to do the
5888 // same. This is necessary because we may have to branch to the exit
5889 // after evaluating the left hand side (due to the shortcut
5890 // semantics), but the compiler must (statically) know if the result
5891 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005892 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005893 JumpTarget is_true;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005894 LoadCondition(node->left(), &is_true, false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005895 if (has_valid_frame() && !has_cc()) {
5896 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005897 JumpTarget pop_and_continue;
5898 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005899
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005900 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005901 // Avoid popping the result if it converts to 'false' using the
5902 // standard ToBoolean() conversion as described in ECMA-262,
5903 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005904 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005905 Branch(false, &exit);
5906
5907 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005908 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005909 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005910
5911 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005912 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005913 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005914
5915 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005916 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005917 } else if (has_cc() || is_true.is_linked()) {
5918 // The left-hand side is either (a) partially compiled to
5919 // control flow with a final branch left to emit or (b) fully
5920 // compiled to control flow and possibly true.
5921 if (has_cc()) {
5922 Branch(false, false_target());
5923 }
5924 is_true.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005925 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005926 } else {
5927 // Nothing to do.
5928 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005929 }
5930
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005931 } else {
5932 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005933 JumpTarget is_false;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005934 LoadCondition(node->left(), true_target(), &is_false, false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005935 if (has_valid_frame() && !has_cc()) {
5936 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005937 JumpTarget pop_and_continue;
5938 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005939
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005940 frame_->Dup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005941 // Avoid popping the result if it converts to 'true' using the
5942 // standard ToBoolean() conversion as described in ECMA-262,
5943 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00005944 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005945 Branch(true, &exit);
5946
5947 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005948 pop_and_continue.Bind();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00005949 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005950
5951 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005952 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005953 Load(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005954
5955 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005956 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005957 } else if (has_cc() || is_false.is_linked()) {
5958 // The left-hand side is either (a) partially compiled to
5959 // control flow with a final branch left to emit or (b) fully
5960 // compiled to control flow and possibly false.
5961 if (has_cc()) {
5962 Branch(true, true_target());
5963 }
5964 is_false.Bind();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005965 LoadCondition(node->right(), true_target(), false_target(), false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00005966 } else {
5967 // Nothing to do.
5968 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005969 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005970 }
5971}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005972
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005973
5974void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
5975#ifdef DEBUG
5976 int original_height = frame_->height();
5977#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00005978 Comment cmnt(masm_, "[ BinaryOperation");
5979
5980 if (node->op() == Token::AND || node->op() == Token::OR) {
5981 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005982 } else {
5983 // Optimize for the case where (at least) one of the expressions
5984 // is a literal small integer.
5985 Literal* lliteral = node->left()->AsLiteral();
5986 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005987 // NOTE: The code below assumes that the slow cases (calls to runtime)
5988 // never return a constant/immutable object.
5989 bool overwrite_left =
5990 (node->left()->AsBinaryOperation() != NULL &&
5991 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
5992 bool overwrite_right =
5993 (node->right()->AsBinaryOperation() != NULL &&
5994 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005995
5996 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00005997 VirtualFrame::RegisterAllocationScope scope(this);
5998 Load(node->left());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005999 if (frame_->KnownSmiAt(0)) overwrite_left = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006000 SmiOperation(node->op(),
6001 rliteral->handle(),
6002 false,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006003 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006004 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006005 VirtualFrame::RegisterAllocationScope scope(this);
6006 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006007 if (frame_->KnownSmiAt(0)) overwrite_right = false;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00006008 SmiOperation(node->op(),
6009 lliteral->handle(),
6010 true,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006011 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006012 } else {
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006013 GenerateInlineSmi inline_smi =
6014 loop_nesting() > 0 ? GENERATE_INLINE_SMI : DONT_GENERATE_INLINE_SMI;
6015 if (lliteral != NULL) {
6016 ASSERT(!lliteral->handle()->IsSmi());
6017 inline_smi = DONT_GENERATE_INLINE_SMI;
6018 }
6019 if (rliteral != NULL) {
6020 ASSERT(!rliteral->handle()->IsSmi());
6021 inline_smi = DONT_GENERATE_INLINE_SMI;
6022 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006023 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006024 OverwriteMode overwrite_mode = NO_OVERWRITE;
6025 if (overwrite_left) {
6026 overwrite_mode = OVERWRITE_LEFT;
6027 } else if (overwrite_right) {
6028 overwrite_mode = OVERWRITE_RIGHT;
6029 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006030 Load(node->left());
6031 Load(node->right());
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006032 GenericBinaryOperation(node->op(), overwrite_mode, inline_smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006033 }
6034 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00006035 ASSERT(!has_valid_frame() ||
6036 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006037 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006038}
6039
6040
ager@chromium.org7c537e22008-10-16 08:43:32 +00006041void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006042#ifdef DEBUG
6043 int original_height = frame_->height();
6044#endif
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00006045 frame_->EmitPush(MemOperand(frame_->Function()));
ager@chromium.orgac091b72010-05-05 07:34:42 +00006046 ASSERT_EQ(original_height + 1, frame_->height());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006047}
6048
6049
ager@chromium.org7c537e22008-10-16 08:43:32 +00006050void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006051#ifdef DEBUG
6052 int original_height = frame_->height();
6053#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006054 Comment cmnt(masm_, "[ CompareOperation");
6055
ager@chromium.org357bf652010-04-12 11:30:10 +00006056 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
6057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006058 // Get the expressions from the node.
6059 Expression* left = node->left();
6060 Expression* right = node->right();
6061 Token::Value op = node->op();
6062
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006063 // To make null checks efficient, we check if either left or right is the
6064 // literal 'null'. If so, we optimize the code by inlining a null check
6065 // instead of calling the (very) general runtime routine for checking
6066 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006067 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006068 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006069 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006070 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006071 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
6072 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006073 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006074 Load(left_is_null ? right : left);
6075 Register tos = frame_->PopToRegister();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006076 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006077 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006078
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006079 // The 'null' value is only equal to 'undefined' if using non-strict
6080 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006081 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006082 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006083
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006084 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006085 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006086 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087
ager@chromium.org357bf652010-04-12 11:30:10 +00006088 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006089 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006090
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006091 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006092 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6093 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
6094 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6095 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006096 }
6097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006098 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006099 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006100 return;
6101 }
6102 }
6103
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006104 // To make typeof testing for natives implemented in JavaScript really
6105 // efficient, we generate special code for expressions of the form:
6106 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006107 UnaryOperation* operation = left->AsUnaryOperation();
6108 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
6109 (operation != NULL && operation->op() == Token::TYPEOF) &&
6110 (right->AsLiteral() != NULL &&
6111 right->AsLiteral()->handle()->IsString())) {
6112 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
6113
ager@chromium.org357bf652010-04-12 11:30:10 +00006114 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006115 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00006116 Register tos = frame_->PopToRegister();
6117
ager@chromium.org357bf652010-04-12 11:30:10 +00006118 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006119
6120 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006121 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006122 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006123 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006124 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006125 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006126 cc_reg_ = eq;
6127
6128 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006129 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006130 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006131
ager@chromium.org357bf652010-04-12 11:30:10 +00006132 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006133
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006134 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006135 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6136 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6137 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006138 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006139
ager@chromium.org357bf652010-04-12 11:30:10 +00006140 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
6141 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006142 cc_reg_ = lt;
6143
6144 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006145 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006146 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006147 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006148 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006149 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006150 cc_reg_ = eq;
6151
6152 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006153 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006154 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006155 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006156
ager@chromium.org357bf652010-04-12 11:30:10 +00006157 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006158 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006159
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006160 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006161 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
6162 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
6163 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
6164 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006165
6166 cc_reg_ = eq;
6167
6168 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006169 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006170 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00006171 Register map_reg = scratch;
6172 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006173 true_target()->Branch(eq);
6174 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00006175 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006176 cc_reg_ = eq;
6177
6178 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006179 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006180 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006181
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006182 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00006183 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006184 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006185
ager@chromium.org357bf652010-04-12 11:30:10 +00006186 Register map_reg = scratch;
6187 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006188 false_target()->Branch(eq);
6189
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006190 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00006191 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
6192 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
6193 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006194 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006195
ager@chromium.org357bf652010-04-12 11:30:10 +00006196 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
6197 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006198 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00006199 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006200 cc_reg_ = le;
6201
6202 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006203 // Uncommon case: typeof testing against a string literal that is
6204 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006205 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006206 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006207 ASSERT(!has_valid_frame() ||
6208 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006209 return;
6210 }
6211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006212 switch (op) {
6213 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006214 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006215 break;
6216
6217 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006218 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006219 break;
6220
6221 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006222 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006223 break;
6224
6225 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006226 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006227 break;
6228
6229 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006230 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006231 break;
6232
6233 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006234 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006235 break;
6236
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006237 case Token::IN: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006238 Load(left);
6239 Load(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006240 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006241 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006242 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006243 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006244
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006245 case Token::INSTANCEOF: {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006246 Load(left);
6247 Load(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006248 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006249 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006250 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00006251 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006252 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006253 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006254 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006255
6256 default:
6257 UNREACHABLE();
6258 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006259 ASSERT((has_cc() && frame_->height() == original_height) ||
6260 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006261}
6262
6263
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006264class DeferredReferenceGetNamedValue: public DeferredCode {
6265 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006266 explicit DeferredReferenceGetNamedValue(Register receiver,
6267 Handle<String> name)
6268 : receiver_(receiver), name_(name) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006269 set_comment("[ DeferredReferenceGetNamedValue");
6270 }
6271
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006272 virtual void Generate();
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006273
6274 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006275 Register receiver_;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006276 Handle<String> name_;
6277};
6278
6279
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006280// Convention for this is that on entry the receiver is in a register that
6281// is not used by the stack. On exit the answer is found in that same
6282// register and the stack has the same height.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006283void DeferredReferenceGetNamedValue::Generate() {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006284#ifdef DEBUG
6285 int expected_height = frame_state()->frame()->height();
6286#endif
6287 VirtualFrame copied_frame(*frame_state()->frame());
6288 copied_frame.SpillAll();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006289
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006290 Register scratch1 = VirtualFrame::scratch0();
6291 Register scratch2 = VirtualFrame::scratch1();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006292 ASSERT(!receiver_.is(scratch1) && !receiver_.is(scratch2));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006293 __ DecrementCounter(&Counters::named_load_inline, 1, scratch1, scratch2);
6294 __ IncrementCounter(&Counters::named_load_inline_miss, 1, scratch1, scratch2);
6295
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006296 // Ensure receiver in r0 and name in r2 to match load ic calling convention.
6297 __ Move(r0, receiver_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006298 __ mov(r2, Operand(name_));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006299
6300 // The rest of the instructions in the deferred code must be together.
6301 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6302 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
6303 __ Call(ic, RelocInfo::CODE_TARGET);
6304 // The call must be followed by a nop(1) instruction to indicate that the
6305 // in-object has been inlined.
6306 __ nop(PROPERTY_ACCESS_INLINED);
6307
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006308 // At this point the answer is in r0. We move it to the expected register
6309 // if necessary.
6310 __ Move(receiver_, r0);
6311
6312 // Now go back to the frame that we entered with. This will not overwrite
6313 // the receiver register since that register was not in use when we came
6314 // in. The instructions emitted by this merge are skipped over by the
6315 // inline load patching mechanism when looking for the branch instruction
6316 // that tells it where the code to patch is.
6317 copied_frame.MergeTo(frame_state()->frame());
6318
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006319 // Block the constant pool for one more instruction after leaving this
6320 // constant pool block scope to include the branch instruction ending the
6321 // deferred code.
6322 __ BlockConstPoolFor(1);
6323 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006324 ASSERT_EQ(expected_height, frame_state()->frame()->height());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006325}
6326
6327
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006328class DeferredReferenceGetKeyedValue: public DeferredCode {
6329 public:
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006330 DeferredReferenceGetKeyedValue(Register key, Register receiver)
6331 : key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006332 set_comment("[ DeferredReferenceGetKeyedValue");
6333 }
6334
6335 virtual void Generate();
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006336
6337 private:
6338 Register key_;
6339 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006340};
6341
6342
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006343// Takes key and register in r0 and r1 or vice versa. Returns result
6344// in r0.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006345void DeferredReferenceGetKeyedValue::Generate() {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006346 ASSERT((key_.is(r0) && receiver_.is(r1)) ||
6347 (key_.is(r1) && receiver_.is(r0)));
6348
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006349 VirtualFrame copied_frame(*frame_state()->frame());
6350 copied_frame.SpillAll();
6351
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006352 Register scratch1 = VirtualFrame::scratch0();
6353 Register scratch2 = VirtualFrame::scratch1();
6354 __ DecrementCounter(&Counters::keyed_load_inline, 1, scratch1, scratch2);
6355 __ IncrementCounter(&Counters::keyed_load_inline_miss, 1, scratch1, scratch2);
6356
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006357 // Ensure key in r0 and receiver in r1 to match keyed load ic calling
6358 // convention.
6359 if (key_.is(r1)) {
6360 __ Swap(r0, r1, ip);
6361 }
6362
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006363 // The rest of the instructions in the deferred code must be together.
6364 { Assembler::BlockConstPoolScope block_const_pool(masm_);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00006365 // Call keyed load IC. It has the arguments key and receiver in r0 and r1.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006366 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
6367 __ Call(ic, RelocInfo::CODE_TARGET);
6368 // The call must be followed by a nop instruction to indicate that the
6369 // keyed load has been inlined.
6370 __ nop(PROPERTY_ACCESS_INLINED);
6371
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006372 // Now go back to the frame that we entered with. This will not overwrite
6373 // the receiver or key registers since they were not in use when we came
6374 // in. The instructions emitted by this merge are skipped over by the
6375 // inline load patching mechanism when looking for the branch instruction
6376 // that tells it where the code to patch is.
6377 copied_frame.MergeTo(frame_state()->frame());
6378
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006379 // Block the constant pool for one more instruction after leaving this
6380 // constant pool block scope to include the branch instruction ending the
6381 // deferred code.
6382 __ BlockConstPoolFor(1);
6383 }
6384}
6385
6386
6387class DeferredReferenceSetKeyedValue: public DeferredCode {
6388 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006389 DeferredReferenceSetKeyedValue(Register value,
6390 Register key,
6391 Register receiver)
6392 : value_(value), key_(key), receiver_(receiver) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006393 set_comment("[ DeferredReferenceSetKeyedValue");
6394 }
6395
6396 virtual void Generate();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006397
6398 private:
6399 Register value_;
6400 Register key_;
6401 Register receiver_;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006402};
6403
6404
6405void DeferredReferenceSetKeyedValue::Generate() {
6406 Register scratch1 = VirtualFrame::scratch0();
6407 Register scratch2 = VirtualFrame::scratch1();
6408 __ DecrementCounter(&Counters::keyed_store_inline, 1, scratch1, scratch2);
6409 __ IncrementCounter(
6410 &Counters::keyed_store_inline_miss, 1, scratch1, scratch2);
6411
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006412 // Ensure value in r0, key in r1 and receiver in r2 to match keyed store ic
6413 // calling convention.
6414 if (value_.is(r1)) {
6415 __ Swap(r0, r1, ip);
6416 }
6417 ASSERT(receiver_.is(r2));
6418
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006419 // The rest of the instructions in the deferred code must be together.
6420 { Assembler::BlockConstPoolScope block_const_pool(masm_);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006421 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6422 // r1 and r2.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006423 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
6424 __ Call(ic, RelocInfo::CODE_TARGET);
6425 // The call must be followed by a nop instruction to indicate that the
6426 // keyed store has been inlined.
6427 __ nop(PROPERTY_ACCESS_INLINED);
6428
6429 // Block the constant pool for one more instruction after leaving this
6430 // constant pool block scope to include the branch instruction ending the
6431 // deferred code.
6432 __ BlockConstPoolFor(1);
6433 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006434}
6435
6436
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006437class DeferredReferenceSetNamedValue: public DeferredCode {
6438 public:
6439 DeferredReferenceSetNamedValue(Register value,
6440 Register receiver,
6441 Handle<String> name)
6442 : value_(value), receiver_(receiver), name_(name) {
6443 set_comment("[ DeferredReferenceSetNamedValue");
6444 }
6445
6446 virtual void Generate();
6447
6448 private:
6449 Register value_;
6450 Register receiver_;
6451 Handle<String> name_;
6452};
6453
6454
6455// Takes value in r0, receiver in r1 and returns the result (the
6456// value) in r0.
6457void DeferredReferenceSetNamedValue::Generate() {
6458 // Record the entry frame and spill.
6459 VirtualFrame copied_frame(*frame_state()->frame());
6460 copied_frame.SpillAll();
6461
6462 // Ensure value in r0, receiver in r1 to match store ic calling
6463 // convention.
6464 ASSERT(value_.is(r0) && receiver_.is(r1));
6465 __ mov(r2, Operand(name_));
6466
6467 // The rest of the instructions in the deferred code must be together.
6468 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6469 // Call keyed store IC. It has the arguments value, key and receiver in r0,
6470 // r1 and r2.
6471 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
6472 __ Call(ic, RelocInfo::CODE_TARGET);
6473 // The call must be followed by a nop instruction to indicate that the
6474 // named store has been inlined.
6475 __ nop(PROPERTY_ACCESS_INLINED);
6476
6477 // Go back to the frame we entered with. The instructions
6478 // generated by this merge are skipped over by the inline store
6479 // patching mechanism when looking for the branch instruction that
6480 // tells it where the code to patch is.
6481 copied_frame.MergeTo(frame_state()->frame());
6482
6483 // Block the constant pool for one more instruction after leaving this
6484 // constant pool block scope to include the branch instruction ending the
6485 // deferred code.
6486 __ BlockConstPoolFor(1);
6487 }
6488}
6489
6490
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006491// Consumes the top of stack (the receiver) and pushes the result instead.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006492void CodeGenerator::EmitNamedLoad(Handle<String> name, bool is_contextual) {
6493 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6494 Comment cmnt(masm(), "[ Load from named Property");
6495 // Setup the name register and call load IC.
ager@chromium.orgac091b72010-05-05 07:34:42 +00006496 frame_->CallLoadIC(name,
6497 is_contextual
6498 ? RelocInfo::CODE_TARGET_CONTEXT
6499 : RelocInfo::CODE_TARGET);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006500 frame_->EmitPush(r0); // Push answer.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006501 } else {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006502 // Inline the in-object property case.
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006503 Comment cmnt(masm(), "[ Inlined named property load");
6504
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006505 // Counter will be decremented in the deferred code. Placed here to avoid
6506 // having it in the instruction stream below where patching will occur.
6507 __ IncrementCounter(&Counters::named_load_inline, 1,
6508 frame_->scratch0(), frame_->scratch1());
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006509
6510 // The following instructions are the inlined load of an in-object property.
6511 // Parts of this code is patched, so the exact instructions generated needs
6512 // to be fixed. Therefore the instruction pool is blocked when generating
6513 // this code
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006514
6515 // Load the receiver from the stack.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006516 Register receiver = frame_->PopToRegister();
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006517
6518 DeferredReferenceGetNamedValue* deferred =
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006519 new DeferredReferenceGetNamedValue(receiver, name);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006520
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006521#ifdef DEBUG
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006522 int kInlinedNamedLoadInstructions = 7;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006523 Label check_inlined_codesize;
6524 masm_->bind(&check_inlined_codesize);
6525#endif
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006526
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006527 { Assembler::BlockConstPoolScope block_const_pool(masm_);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006528 // Check that the receiver is a heap object.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006529 __ tst(receiver, Operand(kSmiTagMask));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006530 deferred->Branch(eq);
6531
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006532 Register scratch = VirtualFrame::scratch0();
6533 Register scratch2 = VirtualFrame::scratch1();
6534
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006535 // Check the map. The null map used below is patched by the inline cache
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006536 // code. Therefore we can't use a LoadRoot call.
6537 __ ldr(scratch, FieldMemOperand(receiver, HeapObject::kMapOffset));
6538 __ mov(scratch2, Operand(Factory::null_value()));
6539 __ cmp(scratch, scratch2);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006540 deferred->Branch(ne);
6541
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006542 // Initially use an invalid index. The index will be patched by the
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006543 // inline cache code.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006544 __ ldr(receiver, MemOperand(receiver, 0));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006545
6546 // Make sure that the expected number of instructions are generated.
6547 ASSERT_EQ(kInlinedNamedLoadInstructions,
6548 masm_->InstructionsGeneratedSince(&check_inlined_codesize));
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006549 }
6550
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006551 deferred->BindExit();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006552 // At this point the receiver register has the result, either from the
6553 // deferred code or from the inlined code.
6554 frame_->EmitPush(receiver);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00006555 }
6556}
6557
6558
ager@chromium.orgac091b72010-05-05 07:34:42 +00006559void CodeGenerator::EmitNamedStore(Handle<String> name, bool is_contextual) {
6560#ifdef DEBUG
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006561 int expected_height = frame()->height() - (is_contextual ? 1 : 2);
ager@chromium.orgac091b72010-05-05 07:34:42 +00006562#endif
ager@chromium.orgac091b72010-05-05 07:34:42 +00006563
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00006564 Result result;
6565 if (is_contextual || scope()->is_global_scope() || loop_nesting() == 0) {
6566 frame()->CallStoreIC(name, is_contextual);
6567 } else {
6568 // Inline the in-object property case.
6569 JumpTarget slow, done;
6570
6571 // Get the value and receiver from the stack.
6572 frame()->PopToR0();
6573 Register value = r0;
6574 frame()->PopToR1();
6575 Register receiver = r1;
6576
6577 DeferredReferenceSetNamedValue* deferred =
6578 new DeferredReferenceSetNamedValue(value, receiver, name);
6579
6580 // Check that the receiver is a heap object.
6581 __ tst(receiver, Operand(kSmiTagMask));
6582 deferred->Branch(eq);
6583
6584 // The following instructions are the part of the inlined
6585 // in-object property store code which can be patched. Therefore
6586 // the exact number of instructions generated must be fixed, so
6587 // the constant pool is blocked while generating this code.
6588 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6589 Register scratch0 = VirtualFrame::scratch0();
6590 Register scratch1 = VirtualFrame::scratch1();
6591
6592 // Check the map. Initially use an invalid map to force a
6593 // failure. The map check will be patched in the runtime system.
6594 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
6595
6596#ifdef DEBUG
6597 Label check_inlined_codesize;
6598 masm_->bind(&check_inlined_codesize);
6599#endif
6600 __ mov(scratch0, Operand(Factory::null_value()));
6601 __ cmp(scratch0, scratch1);
6602 deferred->Branch(ne);
6603
6604 int offset = 0;
6605 __ str(value, MemOperand(receiver, offset));
6606
6607 // Update the write barrier and record its size. We do not use
6608 // the RecordWrite macro here because we want the offset
6609 // addition instruction first to make it easy to patch.
6610 Label record_write_start, record_write_done;
6611 __ bind(&record_write_start);
6612 // Add offset into the object.
6613 __ add(scratch0, receiver, Operand(offset));
6614 // Test that the object is not in the new space. We cannot set
6615 // region marks for new space pages.
6616 __ InNewSpace(receiver, scratch1, eq, &record_write_done);
6617 // Record the actual write.
6618 __ RecordWriteHelper(receiver, scratch0, scratch1);
6619 __ bind(&record_write_done);
6620 // Clobber all input registers when running with the debug-code flag
6621 // turned on to provoke errors.
6622 if (FLAG_debug_code) {
6623 __ mov(receiver, Operand(BitCast<int32_t>(kZapValue)));
6624 __ mov(scratch0, Operand(BitCast<int32_t>(kZapValue)));
6625 __ mov(scratch1, Operand(BitCast<int32_t>(kZapValue)));
6626 }
6627 // Check that this is the first inlined write barrier or that
6628 // this inlined write barrier has the same size as all the other
6629 // inlined write barriers.
6630 ASSERT((inlined_write_barrier_size_ == -1) ||
6631 (inlined_write_barrier_size_ ==
6632 masm()->InstructionsGeneratedSince(&record_write_start)));
6633 inlined_write_barrier_size_ =
6634 masm()->InstructionsGeneratedSince(&record_write_start);
6635
6636 // Make sure that the expected number of instructions are generated.
6637 ASSERT_EQ(GetInlinedNamedStoreInstructionsAfterPatch(),
6638 masm()->InstructionsGeneratedSince(&check_inlined_codesize));
6639 }
6640 deferred->BindExit();
6641 }
6642 ASSERT_EQ(expected_height, frame()->height());
ager@chromium.orgac091b72010-05-05 07:34:42 +00006643}
6644
6645
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006646void CodeGenerator::EmitKeyedLoad() {
6647 if (loop_nesting() == 0) {
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006648 Comment cmnt(masm_, "[ Load from keyed property");
6649 frame_->CallKeyedLoadIC();
6650 } else {
6651 // Inline the keyed load.
6652 Comment cmnt(masm_, "[ Inlined load from keyed property");
6653
6654 // Counter will be decremented in the deferred code. Placed here to avoid
6655 // having it in the instruction stream below where patching will occur.
6656 __ IncrementCounter(&Counters::keyed_load_inline, 1,
6657 frame_->scratch0(), frame_->scratch1());
6658
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006659 // Load the key and receiver from the stack.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006660 bool key_is_known_smi = frame_->KnownSmiAt(0);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006661 Register key = frame_->PopToRegister();
6662 Register receiver = frame_->PopToRegister(key);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006663
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006664 // The deferred code expects key and receiver in registers.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006665 DeferredReferenceGetKeyedValue* deferred =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006666 new DeferredReferenceGetKeyedValue(key, receiver);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006667
6668 // Check that the receiver is a heap object.
6669 __ tst(receiver, Operand(kSmiTagMask));
6670 deferred->Branch(eq);
6671
6672 // The following instructions are the part of the inlined load keyed
6673 // property code which can be patched. Therefore the exact number of
6674 // instructions generated need to be fixed, so the constant pool is blocked
6675 // while generating this code.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006676 { Assembler::BlockConstPoolScope block_const_pool(masm_);
6677 Register scratch1 = VirtualFrame::scratch0();
6678 Register scratch2 = VirtualFrame::scratch1();
6679 // Check the map. The null map used below is patched by the inline cache
6680 // code.
6681 __ ldr(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006682
6683 // Check that the key is a smi.
6684 if (!key_is_known_smi) {
6685 __ tst(key, Operand(kSmiTagMask));
6686 deferred->Branch(ne);
6687 }
6688
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006689#ifdef DEBUG
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006690 Label check_inlined_codesize;
6691 masm_->bind(&check_inlined_codesize);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00006692#endif
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006693 __ mov(scratch2, Operand(Factory::null_value()));
6694 __ cmp(scratch1, scratch2);
6695 deferred->Branch(ne);
6696
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00006697 // Get the elements array from the receiver and check that it
6698 // is not a dictionary.
6699 __ ldr(scratch1, FieldMemOperand(receiver, JSObject::kElementsOffset));
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00006700 if (FLAG_debug_code) {
6701 __ ldr(scratch2, FieldMemOperand(scratch1, JSObject::kMapOffset));
6702 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
6703 __ cmp(scratch2, ip);
6704 __ Assert(eq, "JSObject with fast elements map has slow elements");
6705 }
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
7115 // Allocate both the JS array and the elements array in one big
7116 // allocation. This avoids multiple limit checks.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00007117 __ AllocateInNewSpace(size,
ager@chromium.org5c838252010-02-19 08:53:10 +00007118 r0,
7119 r1,
7120 r2,
7121 &slow_case,
7122 TAG_OBJECT);
7123
7124 // Copy the JS array part.
7125 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
7126 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
7127 __ ldr(r1, FieldMemOperand(r3, i));
7128 __ str(r1, FieldMemOperand(r0, i));
7129 }
7130 }
7131
7132 if (length_ > 0) {
7133 // Get hold of the elements array of the boilerplate and setup the
7134 // elements pointer in the resulting object.
7135 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
7136 __ add(r2, r0, Operand(JSArray::kSize));
7137 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
7138
7139 // Copy the elements array.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00007140 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +00007141 }
7142
7143 // Return and remove the on-stack parameters.
7144 __ add(sp, sp, Operand(3 * kPointerSize));
7145 __ Ret();
7146
7147 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007148 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00007149}
7150
7151
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007152// Takes a Smi and converts to an IEEE 64 bit floating point value in two
7153// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
7154// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
7155// scratch register. Destroys the source register. No GC occurs during this
7156// stub so you don't have to set up the frame.
7157class ConvertToDoubleStub : public CodeStub {
7158 public:
7159 ConvertToDoubleStub(Register result_reg_1,
7160 Register result_reg_2,
7161 Register source_reg,
7162 Register scratch_reg)
7163 : result1_(result_reg_1),
7164 result2_(result_reg_2),
7165 source_(source_reg),
7166 zeros_(scratch_reg) { }
7167
7168 private:
7169 Register result1_;
7170 Register result2_;
7171 Register source_;
7172 Register zeros_;
7173
7174 // Minor key encoding in 16 bits.
7175 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
7176 class OpBits: public BitField<Token::Value, 2, 14> {};
7177
7178 Major MajorKey() { return ConvertToDouble; }
7179 int MinorKey() {
7180 // Encode the parameters in a unique 16 bit value.
7181 return result1_.code() +
7182 (result2_.code() << 4) +
7183 (source_.code() << 8) +
7184 (zeros_.code() << 12);
7185 }
7186
7187 void Generate(MacroAssembler* masm);
7188
7189 const char* GetName() { return "ConvertToDoubleStub"; }
7190
7191#ifdef DEBUG
7192 void Print() { PrintF("ConvertToDoubleStub\n"); }
7193#endif
7194};
7195
7196
7197void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
7198#ifndef BIG_ENDIAN_FLOATING_POINT
7199 Register exponent = result1_;
7200 Register mantissa = result2_;
7201#else
7202 Register exponent = result2_;
7203 Register mantissa = result1_;
7204#endif
7205 Label not_special;
7206 // Convert from Smi to integer.
7207 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
7208 // Move sign bit from source to destination. This works because the sign bit
7209 // in the exponent word of the double has the same position and polarity as
7210 // the 2's complement sign bit in a Smi.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007211 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007212 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
7213 // Subtract from 0 if source was negative.
7214 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007215
7216 // We have -1, 0 or 1, which we treat specially. Register source_ contains
7217 // absolute value: it is either equal to 1 (special case of -1 and 1),
7218 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007219 __ cmp(source_, Operand(1));
7220 __ b(gt, &not_special);
7221
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007222 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
7223 static const uint32_t exponent_word_for_1 =
7224 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007225 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007226 // 1, 0 and -1 all have 0 for the second word.
7227 __ mov(mantissa, Operand(0));
7228 __ Ret();
7229
7230 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00007231 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007232 // Gets the wrong answer for 0, but we already checked for that case above.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00007233 __ CountLeadingZeros(zeros_, source_, mantissa);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007234 // Compute exponent and or it into the exponent register.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007235 // We use mantissa as a scratch register here. Use a fudge factor to
7236 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
7237 // that fit in the ARM's constant field.
7238 int fudge = 0x400;
7239 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
7240 __ add(mantissa, mantissa, Operand(fudge));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007241 __ orr(exponent,
7242 exponent,
7243 Operand(mantissa, LSL, HeapNumber::kExponentShift));
7244 // Shift up the source chopping the top bit off.
7245 __ add(zeros_, zeros_, Operand(1));
7246 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
7247 __ mov(source_, Operand(source_, LSL, zeros_));
7248 // Compute lower part of fraction (last 12 bits).
7249 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
7250 // And the top (top 20 bits).
7251 __ orr(exponent,
7252 exponent,
7253 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
7254 __ Ret();
7255}
7256
7257
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007258// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007259void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007260 Label max_negative_int;
7261 // the_int_ has the answer which is a signed int32 but not a Smi.
7262 // We test for the special value that has a different exponent. This test
7263 // has the neat side effect of setting the flags according to the sign.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007264 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00007265 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007266 __ b(eq, &max_negative_int);
7267 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
7268 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
7269 uint32_t non_smi_exponent =
7270 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
7271 __ mov(scratch_, Operand(non_smi_exponent));
7272 // Set the sign bit in scratch_ if the value was negative.
7273 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
7274 // Subtract from 0 if the value was negative.
7275 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
7276 // We should be masking the implict first digit of the mantissa away here,
7277 // but it just ends up combining harmlessly with the last digit of the
7278 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
7279 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
7280 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
7281 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
7282 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
7283 __ str(scratch_, FieldMemOperand(the_heap_number_,
7284 HeapNumber::kExponentOffset));
7285 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
7286 __ str(scratch_, FieldMemOperand(the_heap_number_,
7287 HeapNumber::kMantissaOffset));
7288 __ Ret();
7289
7290 __ bind(&max_negative_int);
7291 // The max negative int32 is stored as a positive number in the mantissa of
7292 // a double because it uses a sign bit instead of using two's complement.
7293 // The actual mantissa bits stored are all 0 because the implicit most
7294 // significant 1 bit is not stored.
7295 non_smi_exponent += 1 << HeapNumber::kExponentShift;
7296 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
7297 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
7298 __ mov(ip, Operand(0));
7299 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
7300 __ Ret();
7301}
7302
7303
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007304// Handle the case where the lhs and rhs are the same object.
7305// Equality is almost reflexive (everything but NaN), so this is a test
7306// for "identity and not NaN".
7307static void EmitIdenticalObjectComparison(MacroAssembler* masm,
7308 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007309 Condition cc,
7310 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007311 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007312 Label heap_number, return_equal;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007313 __ cmp(r0, r1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007314 __ b(ne, &not_identical);
7315
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007316 // The two objects are identical. If we know that one of them isn't NaN then
7317 // we now know they test equal.
7318 if (cc != eq || !never_nan_nan) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007319 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
7320 // so we do the second best thing - test it ourselves.
7321 // They are both equal and they are not both Smis so both of them are not
7322 // Smis. If it's not a heap number, then return equal.
7323 if (cc == lt || cc == gt) {
7324 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007325 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007326 } else {
7327 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
7328 __ b(eq, &heap_number);
7329 // Comparing JS objects with <=, >= is complicated.
7330 if (cc != eq) {
7331 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
7332 __ b(ge, slow);
7333 // Normally here we fall through to return_equal, but undefined is
7334 // special: (undefined == undefined) == true, but
7335 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
7336 if (cc == le || cc == ge) {
7337 __ cmp(r4, Operand(ODDBALL_TYPE));
7338 __ b(ne, &return_equal);
7339 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00007340 __ cmp(r0, r2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007341 __ b(ne, &return_equal);
7342 if (cc == le) {
7343 // undefined <= undefined should fail.
7344 __ mov(r0, Operand(GREATER));
7345 } else {
7346 // undefined >= undefined should fail.
7347 __ mov(r0, Operand(LESS));
7348 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007349 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007350 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007351 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007352 }
7353 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007354
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007355 __ bind(&return_equal);
7356 if (cc == lt) {
7357 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
7358 } else if (cc == gt) {
7359 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
7360 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007361 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007362 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007363 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007364
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007365 if (cc != eq || !never_nan_nan) {
7366 // For less and greater we don't have to check for NaN since the result of
7367 // x < x is false regardless. For the others here is some code to check
7368 // for NaN.
7369 if (cc != lt && cc != gt) {
7370 __ bind(&heap_number);
7371 // It is a heap number, so return non-equal if it's NaN and equal if it's
7372 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007373
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007374 // The representation of NaN values has all exponent bits (52..62) set,
7375 // and not all mantissa bits (0..51) clear.
7376 // Read top bits of double representation (second word of value).
7377 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
7378 // Test that exponent bits are all set.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007379 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
7380 // NaNs have all-one exponents so they sign extend to -1.
7381 __ cmp(r3, Operand(-1));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007382 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007383
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007384 // Shift out flag and all exponent bits, retaining only mantissa.
7385 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
7386 // Or with all low-bits of mantissa.
7387 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
7388 __ orr(r0, r3, Operand(r2), SetCC);
7389 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007390 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
7391 // not (it's a NaN). For <= and >= we need to load r0 with the failing
7392 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007393 if (cc != eq) {
7394 // All-zero means Infinity means equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007395 __ Ret(eq);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007396 if (cc == le) {
7397 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
7398 } else {
7399 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
7400 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007401 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007402 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007403 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007404 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007405 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007406
7407 __ bind(&not_identical);
7408}
7409
7410
7411// See comment at call site.
7412static void EmitSmiNonsmiComparison(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007413 Register lhs,
7414 Register rhs,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007415 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007416 Label* slow,
7417 bool strict) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007418 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7419 (lhs.is(r1) && rhs.is(r0)));
7420
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007421 Label rhs_is_smi;
ager@chromium.orgb5737492010-07-15 09:29:43 +00007422 __ tst(rhs, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007423 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007424
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007425 // Lhs is a Smi. Check whether the rhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007426 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007427 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007428 // If rhs is not a number and lhs is a Smi then strict equality cannot
ager@chromium.orgb5737492010-07-15 09:29:43 +00007429 // succeed. Return non-equal
7430 // If rhs is r0 then there is already a non zero value in it.
7431 if (!rhs.is(r0)) {
7432 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7433 }
7434 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007435 } else {
7436 // Smi compared non-strictly with a non-Smi non-heap-number. Call
7437 // the runtime.
7438 __ b(ne, slow);
7439 }
7440
ager@chromium.orgb5737492010-07-15 09:29:43 +00007441 // Lhs is a smi, rhs is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007442 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007443 // Convert lhs to a double in d7.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007444 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007445 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007446 // Load the double from rhs, tagged HeapNumber r0, to d6.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007447 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007448 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007449 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007450 __ push(lr);
7451 // Convert lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007452 __ mov(r7, Operand(lhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007453 ConvertToDoubleStub stub1(r3, r2, r7, r6);
7454 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007455 // Load rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007456 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007457 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007458 }
7459
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007460 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007461 // since it's a smi.
7462 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007463
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007464 __ bind(&rhs_is_smi);
7465 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007466 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007467 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007468 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007469 // succeed. Return non-equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007470 // If lhs is r0 then there is already a non zero value in it.
7471 if (!lhs.is(r0)) {
7472 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
7473 }
7474 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007475 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007476 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007477 // the runtime.
7478 __ b(ne, slow);
7479 }
7480
ager@chromium.orgb5737492010-07-15 09:29:43 +00007481 // Rhs is a smi, lhs is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007482 if (CpuFeatures::IsSupported(VFP3)) {
7483 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007484 // Load the double from lhs, tagged HeapNumber r1, to d7.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007485 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007486 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007487 // Convert rhs to a double in d6 .
7488 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007489 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007490 __ push(lr);
7491 // Load lhs to a double in r2, r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007492 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007493 // Convert rhs to a double in r0, r1.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007494 __ mov(r7, Operand(rhs));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007495 ConvertToDoubleStub stub2(r1, r0, r7, r6);
7496 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007497 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007498 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007499 // Fall through to both_loaded_as_doubles.
7500}
7501
7502
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007503void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007504 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007505 Register rhs_exponent = exp_first ? r0 : r1;
7506 Register lhs_exponent = exp_first ? r2 : r3;
7507 Register rhs_mantissa = exp_first ? r1 : r0;
7508 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007509 Label one_is_nan, neither_is_nan;
7510
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007511 __ Sbfx(r4,
7512 lhs_exponent,
7513 HeapNumber::kExponentShift,
7514 HeapNumber::kExponentBits);
7515 // NaNs have all-one exponents so they sign extend to -1.
7516 __ cmp(r4, Operand(-1));
7517 __ b(ne, lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007518 __ mov(r4,
7519 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7520 SetCC);
7521 __ b(ne, &one_is_nan);
7522 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007523 __ b(ne, &one_is_nan);
7524
7525 __ bind(lhs_not_nan);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00007526 __ Sbfx(r4,
7527 rhs_exponent,
7528 HeapNumber::kExponentShift,
7529 HeapNumber::kExponentBits);
7530 // NaNs have all-one exponents so they sign extend to -1.
7531 __ cmp(r4, Operand(-1));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007532 __ b(ne, &neither_is_nan);
7533 __ mov(r4,
7534 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
7535 SetCC);
7536 __ b(ne, &one_is_nan);
7537 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007538 __ b(eq, &neither_is_nan);
7539
7540 __ bind(&one_is_nan);
7541 // NaN comparisons always fail.
7542 // Load whatever we need in r0 to make the comparison fail.
7543 if (cc == lt || cc == le) {
7544 __ mov(r0, Operand(GREATER));
7545 } else {
7546 __ mov(r0, Operand(LESS));
7547 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007548 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007549
7550 __ bind(&neither_is_nan);
7551}
7552
7553
7554// See comment at call site.
7555static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
7556 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007557 Register rhs_exponent = exp_first ? r0 : r1;
7558 Register lhs_exponent = exp_first ? r2 : r3;
7559 Register rhs_mantissa = exp_first ? r1 : r0;
7560 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007561
7562 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
7563 if (cc == eq) {
7564 // Doubles are not equal unless they have the same bit pattern.
7565 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007566 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
7567 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007568 // Return non-zero if the numbers are unequal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007569 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007570
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007571 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007572 // If exponents are equal then return 0.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007573 __ Ret(eq);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007574
7575 // Exponents are unequal. The only way we can return that the numbers
7576 // are equal is if one is -0 and the other is 0. We already dealt
7577 // with the case where both are -0 or both are 0.
7578 // We start by seeing if the mantissas (that are equal) or the bottom
7579 // 31 bits of the rhs exponent are non-zero. If so we return not
7580 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007581 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007582 __ mov(r0, Operand(r4), LeaveCC, ne);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007583 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007584 // Now they are equal if and only if the lhs exponent is zero in its
7585 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007586 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007587 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007588 } else {
7589 // Call a native function to do a comparison between two non-NaNs.
7590 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007591 __ push(lr);
7592 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
7593 __ CallCFunction(ExternalReference::compare_doubles(), 4);
7594 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007595 }
7596}
7597
7598
7599// See comment at call site.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007600static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
7601 Register lhs,
7602 Register rhs) {
7603 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7604 (lhs.is(r1) && rhs.is(r0)));
7605
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007606 // If either operand is a JSObject or an oddball value, then they are
7607 // not equal since their pointers are different.
7608 // There is no test for undetectability in strict equality.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007609 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007610 Label first_non_object;
7611 // Get the type of the first operand into r2 and compare it with
7612 // FIRST_JS_OBJECT_TYPE.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007613 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007614 __ b(lt, &first_non_object);
7615
7616 // Return non-zero (r0 is not zero)
7617 Label return_not_equal;
7618 __ bind(&return_not_equal);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007619 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007620
7621 __ bind(&first_non_object);
7622 // Check for oddballs: true, false, null, undefined.
7623 __ cmp(r2, Operand(ODDBALL_TYPE));
7624 __ b(eq, &return_not_equal);
7625
ager@chromium.orgb5737492010-07-15 09:29:43 +00007626 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007627 __ b(ge, &return_not_equal);
7628
7629 // Check for oddballs: true, false, null, undefined.
7630 __ cmp(r3, Operand(ODDBALL_TYPE));
7631 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007632
7633 // Now that we have the types we might as well check for symbol-symbol.
7634 // Ensure that no non-strings have the symbol bit set.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007635 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
7636 STATIC_ASSERT(kSymbolTag != 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007637 __ and_(r2, r2, Operand(r3));
7638 __ tst(r2, Operand(kIsSymbolMask));
7639 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007640}
7641
7642
7643// See comment at call site.
7644static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007645 Register lhs,
7646 Register rhs,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007647 Label* both_loaded_as_doubles,
7648 Label* not_heap_numbers,
7649 Label* slow) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007650 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7651 (lhs.is(r1) && rhs.is(r0)));
7652
7653 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007654 __ b(ne, not_heap_numbers);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007655 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007656 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007657 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
7658
7659 // Both are heap numbers. Load them up then jump to the code we have
7660 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007661 if (CpuFeatures::IsSupported(VFP3)) {
7662 CpuFeatures::Scope scope(VFP3);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007663 __ sub(r7, rhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007664 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007665 __ sub(r7, lhs, Operand(kHeapObjectTag));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007666 __ vldr(d7, r7, HeapNumber::kValueOffset);
7667 } else {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007668 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
7669 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007670 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007671 __ jmp(both_loaded_as_doubles);
7672}
7673
7674
7675// Fast negative check for symbol-to-symbol equality.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007676static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007677 Register lhs,
7678 Register rhs,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007679 Label* possible_strings,
7680 Label* not_both_strings) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007681 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
7682 (lhs.is(r1) && rhs.is(r0)));
7683
7684 // r2 is object type of rhs.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007685 // Ensure that no non-strings have the symbol bit set.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007686 Label object_test;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007687 STATIC_ASSERT(kSymbolTag != 0);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007688 __ tst(r2, Operand(kIsNotStringMask));
7689 __ b(ne, &object_test);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007690 __ tst(r2, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007691 __ b(eq, possible_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007692 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007693 __ b(ge, not_both_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007694 __ tst(r3, Operand(kIsSymbolMask));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007695 __ b(eq, possible_strings);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007696
7697 // Both are symbols. We already checked they weren't the same pointer
7698 // so they are not equal.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007699 __ mov(r0, Operand(NOT_EQUAL));
7700 __ Ret();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007701
7702 __ bind(&object_test);
7703 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
7704 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007705 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007706 __ b(lt, not_both_strings);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007707 // If both objects are undetectable, they are equal. Otherwise, they
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007708 // are not equal, since they are different objects and an object is not
7709 // equal to undefined.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007710 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007711 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
7712 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
7713 __ and_(r0, r2, Operand(r3));
7714 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
7715 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007716 __ Ret();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007717}
7718
7719
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007720void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
7721 Register object,
7722 Register result,
7723 Register scratch1,
7724 Register scratch2,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007725 Register scratch3,
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007726 bool object_is_smi,
7727 Label* not_found) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007728 // Use of registers. Register result is used as a temporary.
7729 Register number_string_cache = result;
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007730 Register mask = scratch3;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007731
7732 // Load the number string cache.
7733 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
7734
7735 // Make the hash mask from the length of the number string cache. It
7736 // contains two elements (number and string) for each cache entry.
7737 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00007738 // Divide length by two (length is a smi).
7739 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007740 __ sub(mask, mask, Operand(1)); // Make mask.
7741
7742 // Calculate the entry in the number string cache. The hash value in the
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007743 // number string cache for smis is just the smi value, and the hash for
7744 // doubles is the xor of the upper and lower words. See
7745 // Heap::GetNumberStringCache.
7746 Label is_smi;
7747 Label load_result_from_cache;
7748 if (!object_is_smi) {
7749 __ BranchOnSmi(object, &is_smi);
7750 if (CpuFeatures::IsSupported(VFP3)) {
7751 CpuFeatures::Scope scope(VFP3);
7752 __ CheckMap(object,
7753 scratch1,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00007754 Heap::kHeapNumberMapRootIndex,
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007755 not_found,
7756 true);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007757
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007758 STATIC_ASSERT(8 == kDoubleSize);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007759 __ add(scratch1,
7760 object,
7761 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
7762 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
7763 __ eor(scratch1, scratch1, Operand(scratch2));
7764 __ and_(scratch1, scratch1, Operand(mask));
7765
7766 // Calculate address of entry in string cache: each entry consists
7767 // of two pointer sized fields.
7768 __ add(scratch1,
7769 number_string_cache,
7770 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
7771
7772 Register probe = mask;
7773 __ ldr(probe,
7774 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
7775 __ BranchOnSmi(probe, not_found);
7776 __ sub(scratch2, object, Operand(kHeapObjectTag));
7777 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
7778 __ sub(probe, probe, Operand(kHeapObjectTag));
7779 __ vldr(d1, probe, HeapNumber::kValueOffset);
7780 __ vcmp(d0, d1);
7781 __ vmrs(pc);
7782 __ b(ne, not_found); // The cache did not contain this value.
7783 __ b(&load_result_from_cache);
7784 } else {
7785 __ b(not_found);
7786 }
7787 }
7788
7789 __ bind(&is_smi);
7790 Register scratch = scratch1;
7791 __ and_(scratch, mask, Operand(object, ASR, 1));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007792 // Calculate address of entry in string cache: each entry consists
7793 // of two pointer sized fields.
7794 __ add(scratch,
7795 number_string_cache,
7796 Operand(scratch, LSL, kPointerSizeLog2 + 1));
7797
7798 // Check if the entry is the smi we are looking for.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007799 Register probe = mask;
7800 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
7801 __ cmp(object, probe);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007802 __ b(ne, not_found);
7803
7804 // Get the result from the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007805 __ bind(&load_result_from_cache);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007806 __ ldr(result,
7807 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007808 __ IncrementCounter(&Counters::number_to_string_native,
7809 1,
7810 scratch1,
7811 scratch2);
7812}
7813
7814
7815void NumberToStringStub::Generate(MacroAssembler* masm) {
7816 Label runtime;
7817
7818 __ ldr(r1, MemOperand(sp, 0));
7819
7820 // Generate code to lookup number in the number string cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007821 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007822 __ add(sp, sp, Operand(1 * kPointerSize));
7823 __ Ret();
7824
7825 __ bind(&runtime);
7826 // Handle number to string in the runtime system if not found in the cache.
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00007827 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00007828}
7829
7830
ager@chromium.orgac091b72010-05-05 07:34:42 +00007831void RecordWriteStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007832 __ add(offset_, object_, Operand(offset_));
7833 __ RecordWriteHelper(object_, offset_, scratch_);
ager@chromium.orgac091b72010-05-05 07:34:42 +00007834 __ Ret();
7835}
7836
7837
ager@chromium.orgb5737492010-07-15 09:29:43 +00007838// On entry lhs_ and rhs_ are the values to be compared.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007839// On exit r0 is 0, positive or negative to indicate the result of
7840// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007841void CompareStub::Generate(MacroAssembler* masm) {
ager@chromium.orgb5737492010-07-15 09:29:43 +00007842 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
7843 (lhs_.is(r1) && rhs_.is(r0)));
7844
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007845 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007846 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007847
7848 // NOTICE! This code is only reached after a smi-fast-case check, so
7849 // it is certain that at least one operand isn't a smi.
7850
7851 // Handle the case where the objects are identical. Either returns the answer
7852 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007853 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007854
7855 // If either is a Smi (we know that not both are), then they can only
7856 // be strictly equal if the other is a HeapNumber.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00007857 STATIC_ASSERT(kSmiTag == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007858 ASSERT_EQ(0, Smi::FromInt(0));
ager@chromium.orgb5737492010-07-15 09:29:43 +00007859 __ and_(r2, lhs_, Operand(rhs_));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007860 __ tst(r2, Operand(kSmiTagMask));
7861 __ b(ne, &not_smis);
7862 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
7863 // 1) Return the answer.
7864 // 2) Go to slow.
7865 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007866 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007867 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007868 // comparison. If VFP3 is supported the double values of the numbers have
7869 // been loaded into d7 and d6. Otherwise, the double values have been loaded
7870 // into r0, r1, r2, and r3.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007871 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007872
7873 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007874 // The arguments have been converted to doubles and stored in d6 and d7, if
7875 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007876 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007877 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007878 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007879 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007880 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007881 __ vcmp(d7, d6);
7882 __ vmrs(pc); // Move vector status bits to normal status bits.
7883 Label nan;
7884 __ b(vs, &nan);
7885 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
7886 __ mov(r0, Operand(LESS), LeaveCC, lt);
7887 __ mov(r0, Operand(GREATER), LeaveCC, gt);
ager@chromium.orgb5737492010-07-15 09:29:43 +00007888 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007889
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007890 __ bind(&nan);
7891 // If one of the sides was a NaN then the v flag is set. Load r0 with
7892 // whatever it takes to make the comparison fail, since comparisons with NaN
7893 // always fail.
7894 if (cc_ == lt || cc_ == le) {
7895 __ mov(r0, Operand(GREATER));
7896 } else {
7897 __ mov(r0, Operand(LESS));
7898 }
ager@chromium.orgb5737492010-07-15 09:29:43 +00007899 __ Ret();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007900 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007901 // Checks for NaN in the doubles we have loaded. Can return the answer or
7902 // fall through if neither is a NaN. Also binds lhs_not_nan.
7903 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007904 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
7905 // answer. Never falls through.
7906 EmitTwoNonNanDoubleComparison(masm, cc_);
7907 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007908
7909 __ bind(&not_smis);
7910 // At this point we know we are dealing with two different objects,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007911 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007912 if (strict_) {
7913 // This returns non-equal for some object types, or falls through if it
7914 // was not lucky.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007915 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007916 }
7917
7918 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007919 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007920 // Check for heap-number-heap-number comparison. Can jump to slow case,
7921 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
7922 // that case. If the inputs are not doubles then jumps to check_for_symbols.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007923 // In this case r2 will contain the type of rhs_. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007924 EmitCheckForTwoHeapNumbers(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007925 lhs_,
7926 rhs_,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007927 &both_loaded_as_doubles,
7928 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007929 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007930
7931 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007932 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
7933 // symbols.
7934 if (cc_ == eq && !strict_) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00007935 // Returns an answer for two symbols or two detectable objects.
7936 // Otherwise jumps to string case or not both strings case.
ager@chromium.orgb5737492010-07-15 09:29:43 +00007937 // Assumes that r2 is the type of rhs_ on entry.
7938 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007939 }
7940
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007941 // Check for both being sequential ASCII strings, and inline if that is the
7942 // case.
7943 __ bind(&flat_string_check);
7944
ager@chromium.orgb5737492010-07-15 09:29:43 +00007945 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007946
7947 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7948 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
ager@chromium.orgb5737492010-07-15 09:29:43 +00007949 lhs_,
7950 rhs_,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007951 r2,
7952 r3,
7953 r4,
7954 r5);
7955 // Never falls through to here.
7956
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007957 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007958
ager@chromium.orgb5737492010-07-15 09:29:43 +00007959 __ Push(lhs_, rhs_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007960 // Figure out which native to call and setup the arguments.
7961 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007962 if (cc_ == eq) {
7963 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
7964 } else {
7965 native = Builtins::COMPARE;
7966 int ncr; // NaN compare result
7967 if (cc_ == lt || cc_ == le) {
7968 ncr = GREATER;
7969 } else {
7970 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
7971 ncr = LESS;
7972 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007973 __ mov(r0, Operand(Smi::FromInt(ncr)));
7974 __ push(r0);
7975 }
7976
7977 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
7978 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00007979 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007980}
7981
7982
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00007983// This stub does not handle the inlined cases (Smis, Booleans, undefined).
7984// The stub returns zero for false, and a non-zero value for true.
7985void ToBooleanStub::Generate(MacroAssembler* masm) {
7986 Label false_result;
7987 Label not_heap_number;
7988 Register scratch0 = VirtualFrame::scratch0();
7989
7990 // HeapNumber => false iff +0, -0, or NaN.
7991 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
7992 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
7993 __ cmp(scratch0, ip);
7994 __ b(&not_heap_number, ne);
7995
7996 __ sub(ip, tos_, Operand(kHeapObjectTag));
7997 __ vldr(d1, ip, HeapNumber::kValueOffset);
7998 __ vcmp(d1, 0.0);
7999 __ vmrs(pc);
8000 // "tos_" is a register, and contains a non zero value by default.
8001 // Hence we only need to overwrite "tos_" with zero to return false for
8002 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
8003 __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO
8004 __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN
8005 __ Ret();
8006
8007 __ bind(&not_heap_number);
8008
8009 // Check if the value is 'null'.
8010 // 'null' => false.
8011 __ LoadRoot(ip, Heap::kNullValueRootIndex);
8012 __ cmp(tos_, ip);
8013 __ b(&false_result, eq);
8014
8015 // It can be an undetectable object.
8016 // Undetectable => false.
8017 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
8018 __ ldrb(scratch0, FieldMemOperand(ip, Map::kBitFieldOffset));
8019 __ and_(scratch0, scratch0, Operand(1 << Map::kIsUndetectable));
8020 __ cmp(scratch0, Operand(1 << Map::kIsUndetectable));
8021 __ b(&false_result, eq);
8022
8023 // JavaScript object => true.
8024 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8025 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8026 __ cmp(scratch0, Operand(FIRST_JS_OBJECT_TYPE));
8027 // "tos_" is a register and contains a non-zero value.
8028 // Hence we implicitly return true if the greater than
8029 // condition is satisfied.
8030 __ Ret(gt);
8031
8032 // Check for string
8033 __ ldr(scratch0, FieldMemOperand(tos_, HeapObject::kMapOffset));
8034 __ ldrb(scratch0, FieldMemOperand(scratch0, Map::kInstanceTypeOffset));
8035 __ cmp(scratch0, Operand(FIRST_NONSTRING_TYPE));
8036 // "tos_" is a register and contains a non-zero value.
8037 // Hence we implicitly return true if the greater than
8038 // condition is satisfied.
8039 __ Ret(gt);
8040
8041 // String value => false iff empty, i.e., length is zero
8042 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
8043 // If length is zero, "tos_" contains zero ==> false.
8044 // If length is not zero, "tos_" contains a non-zero value ==> true.
8045 __ Ret();
8046
8047 // Return 0 in "tos_" for false .
8048 __ bind(&false_result);
8049 __ mov(tos_, Operand(0));
8050 __ Ret();
8051}
8052
8053
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00008054// We fall into this code if the operands were Smis, but the result was
8055// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008056// the operands were not both Smi. The operands are in r0 and r1. In order
8057// to call the C-implemented binary fp operation routines we need to end up
8058// with the double precision floating point operands in r0 and r1 (for the
8059// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00008060void GenericBinaryOpStub::HandleBinaryOpSlowCases(
8061 MacroAssembler* masm,
8062 Label* not_smi,
8063 Register lhs,
8064 Register rhs,
8065 const Builtins::JavaScript& builtin) {
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008066 Label slow, slow_reverse, do_the_call;
ager@chromium.org357bf652010-04-12 11:30:10 +00008067 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008068
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00008069 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008070 Register heap_number_map = r6;
ager@chromium.org357bf652010-04-12 11:30:10 +00008071
8072 if (ShouldGenerateSmiCode()) {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008073 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8074
ager@chromium.org357bf652010-04-12 11:30:10 +00008075 // Smi-smi case (overflow).
8076 // Since both are Smis there is no heap number to overwrite, so allocate.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008077 // The new heap number is in r5. r3 and r7 are scratch.
8078 __ AllocateHeapNumber(
8079 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008080
8081 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
8082 // using registers d7 and d6 for the double values.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008083 if (CpuFeatures::IsSupported(VFP3)) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008084 CpuFeatures::Scope scope(VFP3);
8085 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
8086 __ vmov(s15, r7);
8087 __ vcvt_f64_s32(d7, s15);
8088 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
8089 __ vmov(s13, r7);
8090 __ vcvt_f64_s32(d6, s13);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008091 if (!use_fp_registers) {
8092 __ vmov(r2, r3, d7);
8093 __ vmov(r0, r1, d6);
8094 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008095 } else {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008096 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008097 __ mov(r7, Operand(rhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008098 ConvertToDoubleStub stub1(r3, r2, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008099 __ push(lr);
8100 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008101 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
ager@chromium.org357bf652010-04-12 11:30:10 +00008102 __ mov(r7, Operand(lhs));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008103 ConvertToDoubleStub stub2(r1, r0, r7, r9);
ager@chromium.org357bf652010-04-12 11:30:10 +00008104 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
8105 __ pop(lr);
8106 }
8107 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008108 }
8109
ager@chromium.org357bf652010-04-12 11:30:10 +00008110 // We branch here if at least one of r0 and r1 is not a Smi.
8111 __ bind(not_smi);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008112 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008113
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008114 // After this point we have the left hand side in r1 and the right hand side
8115 // in r0.
ager@chromium.org357bf652010-04-12 11:30:10 +00008116 if (lhs.is(r0)) {
8117 __ Swap(r0, r1, ip);
8118 }
8119
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008120 // The type transition also calculates the answer.
8121 bool generate_code_to_calculate_answer = true;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008122
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008123 if (ShouldGenerateFPCode()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008124 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
8125 switch (op_) {
8126 case Token::ADD:
8127 case Token::SUB:
8128 case Token::MUL:
8129 case Token::DIV:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008130 GenerateTypeTransition(masm); // Tail call.
8131 generate_code_to_calculate_answer = false;
ager@chromium.org357bf652010-04-12 11:30:10 +00008132 break;
8133
8134 default:
8135 break;
8136 }
8137 }
8138
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008139 if (generate_code_to_calculate_answer) {
8140 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
8141 if (mode_ == NO_OVERWRITE) {
8142 // In the case where there is no chance of an overwritable float we may
8143 // as well do the allocation immediately while r0 and r1 are untouched.
8144 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008145 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008146
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008147 // Move r0 to a double in r2-r3.
8148 __ tst(r0, Operand(kSmiTagMask));
8149 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
8150 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8151 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8152 __ cmp(r4, heap_number_map);
8153 __ b(ne, &slow);
8154 if (mode_ == OVERWRITE_RIGHT) {
8155 __ mov(r5, Operand(r0)); // Overwrite this heap number.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008156 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008157 if (use_fp_registers) {
8158 CpuFeatures::Scope scope(VFP3);
8159 // Load the double from tagged HeapNumber r0 to d7.
8160 __ sub(r7, r0, Operand(kHeapObjectTag));
8161 __ vldr(d7, r7, HeapNumber::kValueOffset);
ager@chromium.org357bf652010-04-12 11:30:10 +00008162 } else {
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008163 // Calling convention says that second double is in r2 and r3.
8164 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.org357bf652010-04-12 11:30:10 +00008165 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008166 __ jmp(&finished_loading_r0);
8167 __ bind(&r0_is_smi);
8168 if (mode_ == OVERWRITE_RIGHT) {
8169 // We can't overwrite a Smi so get address of new heap number into r5.
8170 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8171 }
ager@chromium.org357bf652010-04-12 11:30:10 +00008172
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008173 if (CpuFeatures::IsSupported(VFP3)) {
8174 CpuFeatures::Scope scope(VFP3);
8175 // Convert smi in r0 to double in d7.
8176 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
8177 __ vmov(s15, r7);
8178 __ vcvt_f64_s32(d7, s15);
8179 if (!use_fp_registers) {
8180 __ vmov(r2, r3, d7);
8181 }
8182 } else {
8183 // Write Smi from r0 to r3 and r2 in double format.
8184 __ mov(r7, Operand(r0));
8185 ConvertToDoubleStub stub3(r3, r2, r7, r4);
8186 __ push(lr);
8187 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
8188 __ pop(lr);
8189 }
8190
8191 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
8192 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
8193 Label r1_is_not_smi;
8194 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
8195 __ tst(r1, Operand(kSmiTagMask));
8196 __ b(ne, &r1_is_not_smi);
8197 GenerateTypeTransition(masm); // Tail call.
8198 }
8199
8200 __ bind(&finished_loading_r0);
8201
8202 // Move r1 to a double in r0-r1.
8203 __ tst(r1, Operand(kSmiTagMask));
8204 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
8205 __ bind(&r1_is_not_smi);
8206 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
8207 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8208 __ cmp(r4, heap_number_map);
8209 __ b(ne, &slow);
8210 if (mode_ == OVERWRITE_LEFT) {
8211 __ mov(r5, Operand(r1)); // Overwrite this heap number.
8212 }
8213 if (use_fp_registers) {
8214 CpuFeatures::Scope scope(VFP3);
8215 // Load the double from tagged HeapNumber r1 to d6.
8216 __ sub(r7, r1, Operand(kHeapObjectTag));
8217 __ vldr(d6, r7, HeapNumber::kValueOffset);
8218 } else {
8219 // Calling convention says that first double is in r0 and r1.
8220 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
8221 }
8222 __ jmp(&finished_loading_r1);
8223 __ bind(&r1_is_smi);
8224 if (mode_ == OVERWRITE_LEFT) {
8225 // We can't overwrite a Smi so get address of new heap number into r5.
8226 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
8227 }
8228
8229 if (CpuFeatures::IsSupported(VFP3)) {
8230 CpuFeatures::Scope scope(VFP3);
8231 // Convert smi in r1 to double in d6.
8232 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
8233 __ vmov(s13, r7);
8234 __ vcvt_f64_s32(d6, s13);
8235 if (!use_fp_registers) {
8236 __ vmov(r0, r1, d6);
8237 }
8238 } else {
8239 // Write Smi from r1 to r1 and r0 in double format.
8240 __ mov(r7, Operand(r1));
8241 ConvertToDoubleStub stub4(r1, r0, r7, r9);
8242 __ push(lr);
8243 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
8244 __ pop(lr);
8245 }
8246
8247 __ bind(&finished_loading_r1);
ager@chromium.org357bf652010-04-12 11:30:10 +00008248 }
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00008249
8250 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
8251 __ bind(&do_the_call);
8252 // If we are inlining the operation using VFP3 instructions for
8253 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
8254 if (use_fp_registers) {
8255 CpuFeatures::Scope scope(VFP3);
8256 // ARMv7 VFP3 instructions to implement
8257 // double precision, add, subtract, multiply, divide.
8258
8259 if (Token::MUL == op_) {
8260 __ vmul(d5, d6, d7);
8261 } else if (Token::DIV == op_) {
8262 __ vdiv(d5, d6, d7);
8263 } else if (Token::ADD == op_) {
8264 __ vadd(d5, d6, d7);
8265 } else if (Token::SUB == op_) {
8266 __ vsub(d5, d6, d7);
8267 } else {
8268 UNREACHABLE();
8269 }
8270 __ sub(r0, r5, Operand(kHeapObjectTag));
8271 __ vstr(d5, r0, HeapNumber::kValueOffset);
8272 __ add(r0, r0, Operand(kHeapObjectTag));
8273 __ mov(pc, lr);
8274 } else {
8275 // If we did not inline the operation, then the arguments are in:
8276 // r0: Left value (least significant part of mantissa).
8277 // r1: Left value (sign, exponent, top of mantissa).
8278 // r2: Right value (least significant part of mantissa).
8279 // r3: Right value (sign, exponent, top of mantissa).
8280 // r5: Address of heap number for result.
8281
8282 __ push(lr); // For later.
8283 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
8284 // Call C routine that may not cause GC or other trouble. r5 is callee
8285 // save.
8286 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
8287 // Store answer in the overwritable heap number.
8288 #if !defined(USE_ARM_EABI)
8289 // Double returned in fp coprocessor register 0 and 1, encoded as
8290 // register cr8. Offsets must be divisible by 4 for coprocessor so we
8291 // need to substract the tag from r5.
8292 __ sub(r4, r5, Operand(kHeapObjectTag));
8293 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
8294 #else
8295 // Double returned in registers 0 and 1.
8296 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
8297 #endif
8298 __ mov(r0, Operand(r5));
8299 // And we are done.
8300 __ pop(pc);
8301 }
8302 }
8303 }
8304
8305 if (!generate_code_to_calculate_answer &&
8306 !slow_reverse.is_linked() &&
8307 !slow.is_linked()) {
8308 return;
ager@chromium.org357bf652010-04-12 11:30:10 +00008309 }
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008310
fschneider@chromium.org013f3e12010-04-26 13:27:52 +00008311 if (lhs.is(r0)) {
8312 __ b(&slow);
8313 __ bind(&slow_reverse);
8314 __ Swap(r0, r1, ip);
8315 }
8316
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008317 heap_number_map = no_reg; // Don't use this any more from here on.
8318
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008319 // We jump to here if something goes wrong (one param is not a number of any
8320 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008321 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008322
8323 // Push arguments to the stack
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008324 __ Push(r1, r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008325
ager@chromium.org357bf652010-04-12 11:30:10 +00008326 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008327 // Test for string arguments before calling runtime.
8328 // r1 : first argument
8329 // r0 : second argument
8330 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00008331 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008332
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008333 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008334 __ tst(r1, Operand(kSmiTagMask));
8335 __ b(eq, &not_string1);
8336 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
8337 __ b(ge, &not_string1);
8338
8339 // First argument is a a string, test second.
8340 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008341 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008342 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8343 __ b(ge, &string1);
8344
8345 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008346 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
8347 __ TailCallStub(&string_add_stub);
8348
8349 __ bind(&string1_smi2);
8350 // First argument is a string, second is a smi. Try to lookup the number
8351 // string for the smi in the number string cache.
8352 NumberToStringStub::GenerateLookupNumberStringCache(
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008353 masm, r0, r2, r4, r5, r6, true, &string1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00008354
8355 // Replace second argument on stack and tailcall string add stub to make
8356 // the result.
8357 __ str(r2, MemOperand(sp, 0));
8358 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008359
8360 // Only first argument is a string.
8361 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008362 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
8363
8364 // First argument was not a string, test second.
8365 __ bind(&not_string1);
8366 __ tst(r0, Operand(kSmiTagMask));
8367 __ b(eq, &not_strings);
8368 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
8369 __ b(ge, &not_strings);
8370
8371 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00008372 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
8373
8374 __ bind(&not_strings);
8375 }
8376
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008377 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008378}
8379
8380
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008381// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008382// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008383// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
8384// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008385// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
8386// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008387static void GetInt32(MacroAssembler* masm,
8388 Register source,
8389 Register dest,
8390 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008391 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008392 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008393 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008394 // Get exponent word.
8395 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
8396 // Get exponent alone in scratch2.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008397 __ Ubfx(scratch2,
8398 scratch,
8399 HeapNumber::kExponentShift,
8400 HeapNumber::kExponentBits);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008401 // Load dest with zero. We use this either for the final shift or
8402 // for the answer.
8403 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008404 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008405 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
8406 // the exponent that we are fastest at and also the highest exponent we can
8407 // handle here.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008408 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
8409 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
8410 // split it up to avoid a constant pool entry. You can't do that in general
8411 // for cmp because of the overflow flag, but we know the exponent is in the
8412 // range 0-2047 so there is no overflow.
8413 int fudge_factor = 0x400;
8414 __ sub(scratch2, scratch2, Operand(fudge_factor));
8415 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008416 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
8417 __ b(eq, &right_exponent);
8418 // If the exponent is higher than that then go to slow case. This catches
8419 // numbers that don't fit in a signed int32, infinities and NaNs.
8420 __ b(gt, slow);
8421
8422 // We know the exponent is smaller than 30 (biased). If it is less than
8423 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
8424 // it rounds to zero.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008425 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
8426 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008427 // Dest already has a Smi zero.
8428 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008429 if (!CpuFeatures::IsSupported(VFP3)) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00008430 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
8431 // get how much to shift down.
8432 __ rsb(dest, scratch2, Operand(30));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008433 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008434 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008435 if (CpuFeatures::IsSupported(VFP3)) {
8436 CpuFeatures::Scope scope(VFP3);
8437 // ARMv7 VFP3 instructions implementing double precision to integer
8438 // conversion using round to zero.
8439 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008440 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008441 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008442 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00008443 } else {
8444 // Get the top bits of the mantissa.
8445 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
8446 // Put back the implicit 1.
8447 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
8448 // Shift up the mantissa bits to take up the space the exponent used to
8449 // take. We just orred in the implicit bit so that took care of one and
8450 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
8451 // distance.
8452 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
8453 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
8454 // Put sign in zero flag.
8455 __ tst(scratch, Operand(HeapNumber::kSignMask));
8456 // Get the second half of the double. For some exponents we don't
8457 // actually need this because the bits get shifted out again, but
8458 // it's probably slower to test than just to do it.
8459 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
8460 // Shift down 22 bits to get the last 10 bits.
8461 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
8462 // Move down according to the exponent.
8463 __ mov(dest, Operand(scratch, LSR, dest));
8464 // Fix sign if sign bit was set.
8465 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
8466 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00008467 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008468}
8469
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008470// For bitwise ops where the inputs are not both Smis we here try to determine
8471// whether both inputs are either Smis or at least heap numbers that can be
8472// represented by a 32 bit signed value. We truncate towards zero as required
8473// by the ES spec. If this is the case we do the bitwise op and see if the
8474// result is a Smi. If so, great, otherwise we try to find a heap number to
8475// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00008476// On entry the operands are in lhs and rhs. On exit the answer is in r0.
8477void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
8478 Register lhs,
8479 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008480 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00008481 Label rhs_is_smi, lhs_is_smi;
8482 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008483
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008484 Register heap_number_map = r6;
8485 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
8486
ager@chromium.org357bf652010-04-12 11:30:10 +00008487 __ tst(lhs, Operand(kSmiTagMask));
8488 __ 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 +00008489 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
8490 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008491 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008492 GetInt32(masm, lhs, r3, r5, r4, &slow);
8493 __ jmp(&done_checking_lhs);
8494 __ bind(&lhs_is_smi);
8495 __ mov(r3, Operand(lhs, ASR, 1));
8496 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008497
ager@chromium.org357bf652010-04-12 11:30:10 +00008498 __ tst(rhs, Operand(kSmiTagMask));
8499 __ 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 +00008500 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
8501 __ cmp(r4, heap_number_map);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008502 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00008503 GetInt32(masm, rhs, r2, r5, r4, &slow);
8504 __ jmp(&done_checking_rhs);
8505 __ bind(&rhs_is_smi);
8506 __ mov(r2, Operand(rhs, ASR, 1));
8507 __ bind(&done_checking_rhs);
8508
8509 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008510
8511 // r0 and r1: Original operands (Smi or heap numbers).
8512 // r2 and r3: Signed int32 operands.
8513 switch (op_) {
8514 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
8515 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
8516 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
8517 case Token::SAR:
8518 // Use only the 5 least significant bits of the shift count.
8519 __ and_(r2, r2, Operand(0x1f));
8520 __ mov(r2, Operand(r3, ASR, r2));
8521 break;
8522 case Token::SHR:
8523 // Use only the 5 least significant bits of the shift count.
8524 __ and_(r2, r2, Operand(0x1f));
8525 __ mov(r2, Operand(r3, LSR, r2), SetCC);
8526 // SHR is special because it is required to produce a positive answer.
8527 // The code below for writing into heap numbers isn't capable of writing
8528 // the register as an unsigned int so we go to slow case if we hit this
8529 // case.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008530 if (CpuFeatures::IsSupported(VFP3)) {
8531 __ b(mi, &result_not_a_smi);
8532 } else {
8533 __ b(mi, &slow);
8534 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008535 break;
8536 case Token::SHL:
8537 // Use only the 5 least significant bits of the shift count.
8538 __ and_(r2, r2, Operand(0x1f));
8539 __ mov(r2, Operand(r3, LSL, r2));
8540 break;
8541 default: UNREACHABLE();
8542 }
8543 // check that the *signed* result fits in a smi
8544 __ add(r3, r2, Operand(0x40000000), SetCC);
8545 __ b(mi, &result_not_a_smi);
8546 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
8547 __ Ret();
8548
8549 Label have_to_allocate, got_a_heap_number;
8550 __ bind(&result_not_a_smi);
8551 switch (mode_) {
8552 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008553 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008554 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008555 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008556 break;
8557 }
8558 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00008559 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008560 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00008561 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008562 break;
8563 }
8564 case NO_OVERWRITE: {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008565 // Get a new heap number in r5. r4 and r7 are scratch.
8566 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008567 }
8568 default: break;
8569 }
8570 __ bind(&got_a_heap_number);
8571 // r2: Answer as signed int32.
8572 // r5: Heap number to write answer into.
8573
8574 // Nothing can go wrong now, so move the heap number to r0, which is the
8575 // result.
8576 __ mov(r0, Operand(r5));
8577
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00008578 if (CpuFeatures::IsSupported(VFP3)) {
8579 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
8580 CpuFeatures::Scope scope(VFP3);
8581 __ vmov(s0, r2);
8582 if (op_ == Token::SHR) {
8583 __ vcvt_f64_u32(d0, s0);
8584 } else {
8585 __ vcvt_f64_s32(d0, s0);
8586 }
8587 __ sub(r3, r0, Operand(kHeapObjectTag));
8588 __ vstr(d0, r3, HeapNumber::kValueOffset);
8589 __ Ret();
8590 } else {
8591 // Tail call that writes the int32 in r2 to the heap number in r0, using
8592 // r3 as scratch. r0 is preserved and returned.
8593 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
8594 __ TailCallStub(&stub);
8595 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008596
8597 if (mode_ != NO_OVERWRITE) {
8598 __ bind(&have_to_allocate);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008599 // Get a new heap number in r5. r4 and r7 are scratch.
8600 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008601 __ jmp(&got_a_heap_number);
8602 }
8603
8604 // If all else failed then we go to the runtime system.
8605 __ bind(&slow);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00008606 __ Push(lhs, rhs); // Restore stack.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008607 switch (op_) {
8608 case Token::BIT_OR:
8609 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
8610 break;
8611 case Token::BIT_AND:
8612 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
8613 break;
8614 case Token::BIT_XOR:
8615 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
8616 break;
8617 case Token::SAR:
8618 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
8619 break;
8620 case Token::SHR:
8621 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
8622 break;
8623 case Token::SHL:
8624 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
8625 break;
8626 default:
8627 UNREACHABLE();
8628 }
8629}
8630
8631
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008632// Can we multiply by x with max two shifts and an add.
8633// This answers yes to all integers from 2 to 10.
8634static bool IsEasyToMultiplyBy(int x) {
8635 if (x < 2) return false; // Avoid special cases.
8636 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
8637 if (IsPowerOf2(x)) return true; // Simple shift.
8638 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
8639 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
8640 return false;
8641}
8642
8643
8644// Can multiply by anything that IsEasyToMultiplyBy returns true for.
8645// Source and destination may be the same register. This routine does
8646// not set carry and overflow the way a mul instruction would.
8647static void MultiplyByKnownInt(MacroAssembler* masm,
8648 Register source,
8649 Register destination,
8650 int known_int) {
8651 if (IsPowerOf2(known_int)) {
8652 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
8653 } else if (PopCountLessThanEqual2(known_int)) {
8654 int first_bit = BitPosition(known_int);
8655 int second_bit = BitPosition(known_int ^ (1 << first_bit));
8656 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
8657 if (first_bit != 0) {
8658 __ mov(destination, Operand(destination, LSL, first_bit));
8659 }
8660 } else {
8661 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
8662 int the_bit = BitPosition(known_int + 1);
8663 __ rsb(destination, source, Operand(source, LSL, the_bit));
8664 }
8665}
8666
8667
8668// This function (as opposed to MultiplyByKnownInt) takes the known int in a
8669// a register for the cases where it doesn't know a good trick, and may deliver
8670// a result that needs shifting.
8671static void MultiplyByKnownInt2(
8672 MacroAssembler* masm,
8673 Register result,
8674 Register source,
8675 Register known_int_register, // Smi tagged.
8676 int known_int,
8677 int* required_shift) { // Including Smi tag shift
8678 switch (known_int) {
8679 case 3:
8680 __ add(result, source, Operand(source, LSL, 1));
8681 *required_shift = 1;
8682 break;
8683 case 5:
8684 __ add(result, source, Operand(source, LSL, 2));
8685 *required_shift = 1;
8686 break;
8687 case 6:
8688 __ add(result, source, Operand(source, LSL, 1));
8689 *required_shift = 2;
8690 break;
8691 case 7:
8692 __ rsb(result, source, Operand(source, LSL, 3));
8693 *required_shift = 1;
8694 break;
8695 case 9:
8696 __ add(result, source, Operand(source, LSL, 3));
8697 *required_shift = 1;
8698 break;
8699 case 10:
8700 __ add(result, source, Operand(source, LSL, 2));
8701 *required_shift = 2;
8702 break;
8703 default:
8704 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
8705 __ mul(result, source, known_int_register);
8706 *required_shift = 0;
8707 }
8708}
8709
8710
lrn@chromium.org32d961d2010-06-30 09:09:34 +00008711// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
8712// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
8713// Takes the sum of the digits base (mask + 1) repeatedly until we have a
8714// number from 0 to mask. On exit the 'eq' condition flags are set if the
8715// answer is exactly the mask.
8716void IntegerModStub::DigitSum(MacroAssembler* masm,
8717 Register lhs,
8718 int mask,
8719 int shift,
8720 Label* entry) {
8721 ASSERT(mask > 0);
8722 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8723 Label loop;
8724 __ bind(&loop);
8725 __ and_(ip, lhs, Operand(mask));
8726 __ add(lhs, ip, Operand(lhs, LSR, shift));
8727 __ bind(entry);
8728 __ cmp(lhs, Operand(mask));
8729 __ b(gt, &loop);
8730}
8731
8732
8733void IntegerModStub::DigitSum(MacroAssembler* masm,
8734 Register lhs,
8735 Register scratch,
8736 int mask,
8737 int shift1,
8738 int shift2,
8739 Label* entry) {
8740 ASSERT(mask > 0);
8741 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
8742 Label loop;
8743 __ bind(&loop);
8744 __ bic(scratch, lhs, Operand(mask));
8745 __ and_(ip, lhs, Operand(mask));
8746 __ add(lhs, ip, Operand(lhs, LSR, shift1));
8747 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
8748 __ bind(entry);
8749 __ cmp(lhs, Operand(mask));
8750 __ b(gt, &loop);
8751}
8752
8753
8754// Splits the number into two halves (bottom half has shift bits). The top
8755// half is subtracted from the bottom half. If the result is negative then
8756// rhs is added.
8757void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
8758 Register lhs,
8759 int shift,
8760 int rhs) {
8761 int mask = (1 << shift) - 1;
8762 __ and_(ip, lhs, Operand(mask));
8763 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
8764 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
8765}
8766
8767
8768void IntegerModStub::ModReduce(MacroAssembler* masm,
8769 Register lhs,
8770 int max,
8771 int denominator) {
8772 int limit = denominator;
8773 while (limit * 2 <= max) limit *= 2;
8774 while (limit >= denominator) {
8775 __ cmp(lhs, Operand(limit));
8776 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
8777 limit >>= 1;
8778 }
8779}
8780
8781
8782void IntegerModStub::ModAnswer(MacroAssembler* masm,
8783 Register result,
8784 Register shift_distance,
8785 Register mask_bits,
8786 Register sum_of_digits) {
8787 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
8788 __ Ret();
8789}
8790
8791
8792// See comment for class.
8793void IntegerModStub::Generate(MacroAssembler* masm) {
8794 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
8795 __ bic(odd_number_, odd_number_, Operand(1));
8796 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
8797 // We now have (odd_number_ - 1) * 2 in the register.
8798 // Build a switch out of branches instead of data because it avoids
8799 // having to teach the assembler about intra-code-object pointers
8800 // that are not in relative branch instructions.
8801 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
8802 Label mod21, mod23, mod25;
8803 { Assembler::BlockConstPoolScope block_const_pool(masm);
8804 __ add(pc, pc, Operand(odd_number_));
8805 // When you read pc it is always 8 ahead, but when you write it you always
8806 // write the actual value. So we put in two nops to take up the slack.
8807 __ nop();
8808 __ nop();
8809 __ b(&mod3);
8810 __ b(&mod5);
8811 __ b(&mod7);
8812 __ b(&mod9);
8813 __ b(&mod11);
8814 __ b(&mod13);
8815 __ b(&mod15);
8816 __ b(&mod17);
8817 __ b(&mod19);
8818 __ b(&mod21);
8819 __ b(&mod23);
8820 __ b(&mod25);
8821 }
8822
8823 // For each denominator we find a multiple that is almost only ones
8824 // when expressed in binary. Then we do the sum-of-digits trick for
8825 // that number. If the multiple is not 1 then we have to do a little
8826 // more work afterwards to get the answer into the 0-denominator-1
8827 // range.
8828 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
8829 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
8830 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8831
8832 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
8833 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
8834 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8835
8836 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
8837 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
8838 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8839
8840 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
8841 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
8842 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8843
8844 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
8845 ModReduce(masm, lhs_, 0x3f, 11);
8846 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8847
8848 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
8849 ModReduce(masm, lhs_, 0xff, 13);
8850 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8851
8852 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
8853 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
8854 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8855
8856 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
8857 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
8858 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8859
8860 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
8861 ModReduce(masm, lhs_, 0xff, 19);
8862 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8863
8864 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
8865 ModReduce(masm, lhs_, 0x3f, 21);
8866 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8867
8868 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
8869 ModReduce(masm, lhs_, 0xff, 23);
8870 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8871
8872 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
8873 ModReduce(masm, lhs_, 0x7f, 25);
8874 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
8875}
8876
8877
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008878const char* GenericBinaryOpStub::GetName() {
8879 if (name_ != NULL) return name_;
8880 const int len = 100;
8881 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
8882 if (name_ == NULL) return "OOM";
8883 const char* op_name = Token::Name(op_);
8884 const char* overwrite_name;
8885 switch (mode_) {
8886 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
8887 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
8888 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
8889 default: overwrite_name = "UnknownOverwrite"; break;
8890 }
8891
8892 OS::SNPrintF(Vector<char>(name_, len),
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008893 "GenericBinaryOpStub_%s_%s%s_%s",
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008894 op_name,
8895 overwrite_name,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00008896 specialized_on_rhs_ ? "_ConstantRhs" : "",
8897 BinaryOpIC::GetName(runtime_operands_type_));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00008898 return name_;
8899}
8900
8901
ager@chromium.org5c838252010-02-19 08:53:10 +00008902
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008903void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00008904 // lhs_ : x
8905 // rhs_ : y
8906 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008907
ager@chromium.org357bf652010-04-12 11:30:10 +00008908 Register result = r0;
8909 Register lhs = lhs_;
8910 Register rhs = rhs_;
8911
8912 // This code can't cope with other register allocations yet.
8913 ASSERT(result.is(r0) &&
8914 ((lhs.is(r0) && rhs.is(r1)) ||
8915 (lhs.is(r1) && rhs.is(r0))));
8916
8917 Register smi_test_reg = VirtualFrame::scratch0();
8918 Register scratch = VirtualFrame::scratch1();
8919
8920 // All ops need to know whether we are dealing with two Smis. Set up
8921 // smi_test_reg to tell us that.
8922 if (ShouldGenerateSmiCode()) {
8923 __ orr(smi_test_reg, lhs, Operand(rhs));
8924 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008925
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008926 switch (op_) {
8927 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008928 Label not_smi;
8929 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008930 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008931 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008932 __ tst(smi_test_reg, Operand(kSmiTagMask));
8933 __ b(ne, &not_smi);
8934 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
8935 // Return if no overflow.
8936 __ Ret(vc);
8937 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
8938 }
8939 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008940 break;
8941 }
8942
8943 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008944 Label not_smi;
8945 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00008946 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008947 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ager@chromium.org357bf652010-04-12 11:30:10 +00008948 __ tst(smi_test_reg, Operand(kSmiTagMask));
8949 __ b(ne, &not_smi);
8950 if (lhs.is(r1)) {
8951 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
8952 // Return if no overflow.
8953 __ Ret(vc);
8954 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
8955 } else {
8956 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
8957 // Return if no overflow.
8958 __ Ret(vc);
8959 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
8960 }
8961 }
8962 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008963 break;
8964 }
8965
8966 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00008967 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00008968 if (ShouldGenerateSmiCode()) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00008969 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00008970 __ tst(smi_test_reg, Operand(kSmiTagMask));
8971 Register scratch2 = smi_test_reg;
8972 smi_test_reg = no_reg;
8973 __ b(ne, &not_smi);
8974 // Remove tag from one operand (but keep sign), so that result is Smi.
8975 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
8976 // Do multiplication
8977 // scratch = lower 32 bits of ip * lhs.
8978 __ smull(scratch, scratch2, lhs, ip);
8979 // Go slow on overflows (overflow bit is not set).
8980 __ mov(ip, Operand(scratch, ASR, 31));
8981 // No overflow if higher 33 bits are identical.
8982 __ cmp(ip, Operand(scratch2));
8983 __ b(ne, &slow);
8984 // Go slow on zero result to handle -0.
8985 __ tst(scratch, Operand(scratch));
8986 __ mov(result, Operand(scratch), LeaveCC, ne);
8987 __ Ret(ne);
8988 // We need -0 if we were multiplying a negative number with 0 to get 0.
8989 // We know one of them was zero.
8990 __ add(scratch2, rhs, Operand(lhs), SetCC);
8991 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
8992 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
8993 // Slow case. We fall through here if we multiplied a negative number
8994 // with 0, because that would mean we should produce -0.
8995 __ bind(&slow);
8996 }
8997 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00008998 break;
8999 }
9000
9001 case Token::DIV:
9002 case Token::MOD: {
9003 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00009004 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009005 Label lhs_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00009006 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009007 if (IsPowerOf2(constant_rhs_)) {
9008 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009009 __ and_(rhs,
9010 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009011 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
9012 SetCC);
9013 // We now have the answer, but if the input was negative we also
9014 // have the sign bit. Our work is done if the result is
9015 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00009016 if (!rhs.is(r0)) {
9017 __ mov(r0, rhs, LeaveCC, pl);
9018 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009019 __ Ret(pl);
9020 // A mod of a negative left hand side must return a negative number.
9021 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00009022 // already optimistically trashed rhs so we may need to restore it.
9023 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009024 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00009025 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009026 __ b(eq, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009027 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00009028 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009029 } else {
9030 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00009031 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009032 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009033 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009034 int shift = 0;
9035 int d = constant_rhs_;
9036 while ((d & 1) == 0) {
9037 d >>= 1;
9038 shift++;
9039 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009040 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009041 __ bic(r0, r0, Operand(kSmiTagMask));
9042 }
9043 } else {
9044 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00009045 __ tst(lhs, Operand(0x80000000u));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009046 __ b(ne, &lhs_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009047 // Find a fixed point reciprocal of the divisor so we can divide by
9048 // multiplying.
9049 double divisor = 1.0 / constant_rhs_;
9050 int shift = 32;
9051 double scale = 4294967296.0; // 1 << 32.
9052 uint32_t mul;
9053 // Maximise the precision of the fixed point reciprocal.
9054 while (true) {
9055 mul = static_cast<uint32_t>(scale * divisor);
9056 if (mul >= 0x7fffffff) break;
9057 scale *= 2.0;
9058 shift++;
9059 }
9060 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00009061 Register scratch2 = smi_test_reg;
9062 smi_test_reg = no_reg;
9063 __ mov(scratch2, Operand(mul));
9064 __ umull(scratch, scratch2, scratch2, lhs);
9065 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
9066 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
9067 // rhs is still the known rhs. rhs is Smi tagged.
9068 // lhs is still the unkown lhs. lhs is Smi tagged.
9069 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
9070 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009071 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00009072 scratch,
9073 scratch2,
9074 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009075 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00009076 &required_scratch_shift);
9077 // scratch << required_scratch_shift is now the Smi tagged rhs *
9078 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009079 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009080 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009081 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00009082 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009083 } else {
9084 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00009085 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009086 }
9087 }
9088 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009089 __ bind(&lhs_is_unsuitable);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009090 } else if (op_ == Token::MOD &&
9091 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
9092 runtime_operands_type_ != BinaryOpIC::STRINGS) {
9093 // Do generate a bit of smi code for modulus even though the default for
9094 // modulus is not to do it, but as the ARM processor has no coprocessor
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009095 // support for modulus checking for smis makes sense. We can handle
9096 // 1 to 25 times any power of 2. This covers over half the numbers from
9097 // 1 to 100 including all of the first 25. (Actually the constants < 10
9098 // are handled above by reciprocal multiplication. We only get here for
9099 // those cases if the right hand side is not a constant or for cases
9100 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
9101 // stub.)
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009102 Label slow;
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009103 Label not_power_of_2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009104 ASSERT(!ShouldGenerateSmiCode());
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009105 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009106 // Check for two positive smis.
9107 __ orr(smi_test_reg, lhs, Operand(rhs));
9108 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
9109 __ b(ne, &slow);
9110 // Check that rhs is a power of two and not zero.
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009111 Register mask_bits = r3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009112 __ sub(scratch, rhs, Operand(1), SetCC);
9113 __ b(mi, &slow);
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009114 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
9115 __ b(ne, &not_power_of_2);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009116 // Calculate power of two modulus.
9117 __ and_(result, lhs, Operand(scratch));
9118 __ Ret();
lrn@chromium.org32d961d2010-06-30 09:09:34 +00009119
9120 __ bind(&not_power_of_2);
9121 __ eor(scratch, scratch, Operand(mask_bits));
9122 // At least two bits are set in the modulus. The high one(s) are in
9123 // mask_bits and the low one is scratch + 1.
9124 __ and_(mask_bits, scratch, Operand(lhs));
9125 Register shift_distance = scratch;
9126 scratch = no_reg;
9127
9128 // The rhs consists of a power of 2 multiplied by some odd number.
9129 // The power-of-2 part we handle by putting the corresponding bits
9130 // from the lhs in the mask_bits register, and the power in the
9131 // shift_distance register. Shift distance is never 0 due to Smi
9132 // tagging.
9133 __ CountLeadingZeros(r4, shift_distance, shift_distance);
9134 __ rsb(shift_distance, r4, Operand(32));
9135
9136 // Now we need to find out what the odd number is. The last bit is
9137 // always 1.
9138 Register odd_number = r4;
9139 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
9140 __ cmp(odd_number, Operand(25));
9141 __ b(gt, &slow);
9142
9143 IntegerModStub stub(
9144 result, shift_distance, odd_number, mask_bits, lhs, r5);
9145 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
9146
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009147 __ bind(&slow);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00009148 }
ager@chromium.org357bf652010-04-12 11:30:10 +00009149 HandleBinaryOpSlowCases(
9150 masm,
9151 &not_smi,
9152 lhs,
9153 rhs,
9154 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009155 break;
9156 }
9157
9158 case Token::BIT_OR:
9159 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009160 case Token::BIT_XOR:
9161 case Token::SAR:
9162 case Token::SHR:
9163 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009164 Label slow;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009165 STATIC_ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00009166 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009167 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009168 Register scratch2 = smi_test_reg;
9169 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009170 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009171 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
9172 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
9173 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009174 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009175 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00009176 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9177 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009178 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009179 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009180 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009181 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009182 // Remove tags from operands. We can't do this on a 31 bit number
9183 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00009184 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9185 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9186 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009187 // Unsigned shift is not allowed to produce a negative number, so
9188 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00009189 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009190 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009191 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00009192 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009193 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009194 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009195 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00009196 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
9197 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
9198 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009199 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00009200 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009201 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009202 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009203 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009204 default: UNREACHABLE();
9205 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009206 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009207 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00009208 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009209 break;
9210 }
9211
9212 default: UNREACHABLE();
9213 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009214 // This code should be unreachable.
9215 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00009216
9217 // Generate an unreachable reference to the DEFAULT stub so that it can be
9218 // found at the end of this stub when clearing ICs at GC.
9219 // TODO(kaznacheev): Check performance impact and get rid of this.
9220 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
9221 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
9222 __ CallStub(&uninit);
9223 }
9224}
9225
9226
9227void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
9228 Label get_result;
9229
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009230 __ Push(r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009231
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009232 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
9233 __ mov(r1, Operand(Smi::FromInt(op_)));
ager@chromium.org357bf652010-04-12 11:30:10 +00009234 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009235 __ Push(r2, r1, r0);
ager@chromium.org357bf652010-04-12 11:30:10 +00009236
9237 __ TailCallExternalReference(
9238 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009239 5,
ager@chromium.org357bf652010-04-12 11:30:10 +00009240 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009241}
9242
9243
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009244Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00009245 GenericBinaryOpStub stub(key, type_info);
9246 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009247}
9248
9249
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009250void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
9251 // Argument is a number and is on stack and in r0.
9252 Label runtime_call;
9253 Label input_not_smi;
9254 Label loaded;
9255
9256 if (CpuFeatures::IsSupported(VFP3)) {
9257 // Load argument and check if it is a smi.
9258 __ BranchOnNotSmi(r0, &input_not_smi);
9259
9260 CpuFeatures::Scope scope(VFP3);
9261 // Input is a smi. Convert to double and load the low and high words
9262 // of the double into r2, r3.
9263 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
9264 __ b(&loaded);
9265
9266 __ bind(&input_not_smi);
9267 // Check if input is a HeapNumber.
9268 __ CheckMap(r0,
9269 r1,
9270 Heap::kHeapNumberMapRootIndex,
9271 &runtime_call,
9272 true);
9273 // Input is a HeapNumber. Load it to a double register and store the
9274 // low and high words into r2, r3.
9275 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
9276
9277 __ bind(&loaded);
9278 // r2 = low 32 bits of double value
9279 // r3 = high 32 bits of double value
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009280 // Compute hash (the shifts are arithmetic):
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009281 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
9282 __ eor(r1, r2, Operand(r3));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009283 __ eor(r1, r1, Operand(r1, ASR, 16));
9284 __ eor(r1, r1, Operand(r1, ASR, 8));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009285 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00009286 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00009287
9288 // r2 = low 32 bits of double value.
9289 // r3 = high 32 bits of double value.
9290 // r1 = TranscendentalCache::hash(double value).
9291 __ mov(r0,
9292 Operand(ExternalReference::transcendental_cache_array_address()));
9293 // r0 points to cache array.
9294 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
9295 // r0 points to the cache for the type type_.
9296 // If NULL, the cache hasn't been initialized yet, so go through runtime.
9297 __ cmp(r0, Operand(0));
9298 __ b(eq, &runtime_call);
9299
9300#ifdef DEBUG
9301 // Check that the layout of cache elements match expectations.
9302 { TranscendentalCache::Element test_elem[2];
9303 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
9304 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
9305 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
9306 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
9307 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
9308 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
9309 CHECK_EQ(0, elem_in0 - elem_start);
9310 CHECK_EQ(kIntSize, elem_in1 - elem_start);
9311 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
9312 }
9313#endif
9314
9315 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
9316 __ add(r1, r1, Operand(r1, LSL, 1));
9317 __ add(r0, r0, Operand(r1, LSL, 2));
9318 // Check if cache matches: Double value is stored in uint32_t[2] array.
9319 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
9320 __ cmp(r2, r4);
9321 __ b(ne, &runtime_call);
9322 __ cmp(r3, r5);
9323 __ b(ne, &runtime_call);
9324 // Cache hit. Load result, pop argument and return.
9325 __ mov(r0, Operand(r6));
9326 __ pop();
9327 __ Ret();
9328 }
9329
9330 __ bind(&runtime_call);
9331 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
9332}
9333
9334
9335Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
9336 switch (type_) {
9337 // Add more cases when necessary.
9338 case TranscendentalCache::SIN: return Runtime::kMath_sin;
9339 case TranscendentalCache::COS: return Runtime::kMath_cos;
9340 default:
9341 UNIMPLEMENTED();
9342 return Runtime::kAbort;
9343 }
9344}
9345
9346
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009347void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00009348 // Do tail-call to runtime routine. Runtime routines expect at least one
9349 // argument, so give it a Smi.
9350 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009351 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00009352 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009353
9354 __ StubReturn(1);
9355}
9356
9357
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009358void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009359 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00009360
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009361 Register heap_number_map = r6;
9362 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9363
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009364 if (op_ == Token::SUB) {
9365 // Check whether the value is a smi.
9366 Label try_float;
9367 __ tst(r0, Operand(kSmiTagMask));
9368 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009369
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009370 // Go slow case if the value of the expression is zero
9371 // to make sure that we switch between 0 and -0.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009372 if (negative_zero_ == kStrictNegativeZero) {
9373 // If we have to check for zero, then we can check for the max negative
9374 // smi while we are at it.
9375 __ bic(ip, r0, Operand(0x80000000), SetCC);
9376 __ b(eq, &slow);
9377 __ rsb(r0, r0, Operand(0));
9378 __ StubReturn(1);
9379 } else {
9380 // The value of the expression is a smi and 0 is OK for -0. Try
9381 // optimistic subtraction '0 - value'.
9382 __ rsb(r0, r0, Operand(0), SetCC);
9383 __ StubReturn(1, vc);
9384 // We don't have to reverse the optimistic neg since the only case
9385 // where we fall through is the minimum negative Smi, which is the case
9386 // where the neg leaves the register unchanged.
9387 __ jmp(&slow); // Go slow on max negative Smi.
9388 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009389
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009390 __ bind(&try_float);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009391 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
9392 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
9393 __ cmp(r1, heap_number_map);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009394 __ b(ne, &slow);
9395 // r0 is a heap number. Get a new heap number in r1.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009396 if (overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009397 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9398 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9399 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9400 } else {
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009401 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009402 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
9403 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
9404 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
9405 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
9406 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
9407 __ mov(r0, Operand(r1));
9408 }
9409 } else if (op_ == Token::BIT_NOT) {
9410 // Check if the operand is a heap number.
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
9416 // Convert the heap number is r0 to an untagged integer in r1.
9417 GetInt32(masm, r0, r1, r2, r3, &slow);
9418
9419 // Do the bitwise operation (move negated) and check if the result
9420 // fits in a smi.
9421 Label try_float;
9422 __ mvn(r1, Operand(r1));
9423 __ add(r2, r1, Operand(0x40000000), SetCC);
9424 __ b(mi, &try_float);
9425 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
9426 __ b(&done);
9427
9428 __ bind(&try_float);
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00009429 if (!overwrite_ == UNARY_OVERWRITE) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009430 // Allocate a fresh heap number, but don't overwrite r0 until
9431 // we're sure we can do it without going through the slow case
9432 // that needs the value in r0.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00009433 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009434 __ mov(r0, Operand(r2));
9435 }
9436
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00009437 if (CpuFeatures::IsSupported(VFP3)) {
9438 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
9439 CpuFeatures::Scope scope(VFP3);
9440 __ vmov(s0, r1);
9441 __ vcvt_f64_s32(d0, s0);
9442 __ sub(r2, r0, Operand(kHeapObjectTag));
9443 __ vstr(d0, r2, HeapNumber::kValueOffset);
9444 } else {
9445 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
9446 // have to set up a frame.
9447 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
9448 __ push(lr);
9449 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
9450 __ pop(lr);
9451 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009452 } else {
9453 UNIMPLEMENTED();
9454 }
9455
9456 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009457 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009458
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009459 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009460 __ bind(&slow);
9461 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009462 switch (op_) {
9463 case Token::SUB:
9464 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
9465 break;
9466 case Token::BIT_NOT:
9467 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
9468 break;
9469 default:
9470 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009471 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00009472}
9473
9474
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009475void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009476 // r0 holds the exception.
9477
9478 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009479 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009480
9481 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009482 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
9483 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009484
9485 // Restore the next handler and frame pointer, discard handler state.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009486 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009487 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009488 __ str(r2, MemOperand(r3));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009489 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009490 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
9491
9492 // Before returning we restore the context from the frame pointer if
9493 // not NULL. The frame pointer is NULL in the exception handler of a
9494 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009495 __ cmp(fp, Operand(0));
9496 // Set cp to NULL if fp is NULL.
9497 __ mov(cp, Operand(0), LeaveCC, eq);
9498 // Restore cp otherwise.
9499 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009500#ifdef DEBUG
9501 if (FLAG_debug_code) {
9502 __ mov(lr, Operand(pc));
9503 }
9504#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009505 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009506 __ pop(pc);
9507}
9508
9509
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009510void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
9511 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009512 // Adjust this code if not the case.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009513 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009514
9515 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009516 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009517 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009518
9519 // Unwind the handlers until the ENTRY handler is found.
9520 Label loop, done;
9521 __ bind(&loop);
9522 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009523 const int kStateOffset = StackHandlerConstants::kStateOffset;
9524 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009525 __ cmp(r2, Operand(StackHandler::ENTRY));
9526 __ b(eq, &done);
9527 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009528 const int kNextOffset = StackHandlerConstants::kNextOffset;
9529 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009530 __ jmp(&loop);
9531 __ bind(&done);
9532
9533 // Set the top handler address to next handler past the current ENTRY handler.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009534 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009535 __ pop(r2);
9536 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009537
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009538 if (type == OUT_OF_MEMORY) {
9539 // Set external caught exception to false.
9540 ExternalReference external_caught(Top::k_external_caught_exception_address);
9541 __ mov(r0, Operand(false));
9542 __ mov(r2, Operand(external_caught));
9543 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009544
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009545 // Set pending exception and r0 to out of memory exception.
9546 Failure* out_of_memory = Failure::OutOfMemoryException();
9547 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9548 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
9549 __ str(r0, MemOperand(r2));
9550 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009551
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009552 // Stack layout at this point. See also StackHandlerConstants.
9553 // sp -> state (ENTRY)
9554 // fp
9555 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009556
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009557 // Discard handler state (r2 is not used) and restore frame pointer.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009558 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009559 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
9560 // Before returning we restore the context from the frame pointer if
9561 // not NULL. The frame pointer is NULL in the exception handler of a
9562 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009563 __ cmp(fp, Operand(0));
9564 // Set cp to NULL if fp is NULL.
9565 __ mov(cp, Operand(0), LeaveCC, eq);
9566 // Restore cp otherwise.
9567 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009568#ifdef DEBUG
9569 if (FLAG_debug_code) {
9570 __ mov(lr, Operand(pc));
9571 }
9572#endif
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009573 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009574 __ pop(pc);
9575}
9576
9577
9578void CEntryStub::GenerateCore(MacroAssembler* masm,
9579 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009580 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009581 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009582 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009583 bool always_allocate,
9584 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009585 // r0: result parameter for PerformGC, if any
9586 // r4: number of arguments including receiver (C callee-saved)
9587 // r5: pointer to builtin function (C callee-saved)
9588 // r6: pointer to the first argument (C callee-saved)
9589
9590 if (do_gc) {
9591 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009592 __ PrepareCallCFunction(1, r1);
9593 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009594 }
9595
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009596 ExternalReference scope_depth =
9597 ExternalReference::heap_always_allocate_scope_depth();
9598 if (always_allocate) {
9599 __ mov(r0, Operand(scope_depth));
9600 __ ldr(r1, MemOperand(r0));
9601 __ add(r1, r1, Operand(1));
9602 __ str(r1, MemOperand(r0));
9603 }
9604
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009605 // Call C built-in.
9606 // r0 = argc, r1 = argv
9607 __ mov(r0, Operand(r4));
9608 __ mov(r1, Operand(r6));
9609
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009610 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
9611 int frame_alignment_mask = frame_alignment - 1;
9612#if defined(V8_HOST_ARCH_ARM)
9613 if (FLAG_debug_code) {
9614 if (frame_alignment > kPointerSize) {
9615 Label alignment_as_expected;
9616 ASSERT(IsPowerOf2(frame_alignment));
9617 __ sub(r2, sp, Operand(frame_alignment_skew));
9618 __ tst(r2, Operand(frame_alignment_mask));
9619 __ b(eq, &alignment_as_expected);
9620 // Don't use Check here, as it will call Runtime_Abort re-entering here.
9621 __ stop("Unexpected alignment");
9622 __ bind(&alignment_as_expected);
9623 }
9624 }
9625#endif
9626
9627 // Just before the call (jump) below lr is pushed, so the actual alignment is
9628 // adding one to the current skew.
9629 int alignment_before_call =
9630 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
9631 if (alignment_before_call > 0) {
9632 // Push until the alignment before the call is met.
9633 __ mov(r2, Operand(0));
9634 for (int i = alignment_before_call;
9635 (i & frame_alignment_mask) != 0;
9636 i += kPointerSize) {
9637 __ push(r2);
9638 }
9639 }
9640
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009641 // TODO(1242173): To let the GC traverse the return address of the exit
9642 // frames, we need to know where the return address is. Right now,
9643 // we push it on the stack to be able to find it again, but we never
9644 // restore from it in case of changes, which makes it impossible to
9645 // support moving the C entry code stub. This should be fixed, but currently
9646 // this is OK because the CEntryStub gets generated so early in the V8 boot
9647 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009648 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00009649 masm->push(lr);
9650 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009651
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009652 // Restore sp back to before aligning the stack.
9653 if (alignment_before_call > 0) {
9654 __ add(sp, sp, Operand(alignment_before_call));
9655 }
9656
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009657 if (always_allocate) {
9658 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
9659 // though (contain the result).
9660 __ mov(r2, Operand(scope_depth));
9661 __ ldr(r3, MemOperand(r2));
9662 __ sub(r3, r3, Operand(1));
9663 __ str(r3, MemOperand(r2));
9664 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009665
9666 // check for failure result
9667 Label failure_returned;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009668 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009669 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
9670 __ add(r2, r0, Operand(1));
9671 __ tst(r2, Operand(kFailureTagMask));
9672 __ b(eq, &failure_returned);
9673
9674 // Exit C frame and return.
9675 // r0:r1: result
9676 // sp: stack pointer
9677 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009678 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009679
9680 // check if we should retry or throw exception
9681 Label retry;
9682 __ bind(&failure_returned);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +00009683 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009684 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
9685 __ b(eq, &retry);
9686
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009687 // Special handling of out of memory exceptions.
9688 Failure* out_of_memory = Failure::OutOfMemoryException();
9689 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
9690 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009691
9692 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00009693 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009694 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009695 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009696 __ ldr(r0, MemOperand(ip));
9697 __ str(r3, MemOperand(ip));
9698
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009699 // Special handling of termination exceptions which are uncatchable
9700 // by javascript code.
9701 __ cmp(r0, Operand(Factory::termination_exception()));
9702 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009703
9704 // Handle normal exception.
9705 __ jmp(throw_normal_exception);
9706
9707 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
9708}
9709
9710
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009711void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009712 // Called from JavaScript; parameters are on stack as if calling JS function
9713 // r0: number of arguments including receiver
9714 // r1: pointer to builtin function
9715 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009716 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009717 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009718
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009719 // Result returned in r0 or r0+r1 by default.
9720
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009721 // NOTE: Invocations of builtins may return failure objects
9722 // instead of a proper result. The builtin entry handles
9723 // this by performing a garbage collection and retrying the
9724 // builtin once.
9725
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009726 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00009727 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009728
9729 // r4: number of arguments (C callee-saved)
9730 // r5: pointer to builtin function (C callee-saved)
9731 // r6: pointer to first argument (C callee-saved)
9732
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009733 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009734 Label throw_termination_exception;
9735 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009736
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009737 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009738 GenerateCore(masm,
9739 &throw_normal_exception,
9740 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009741 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00009742 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009743 false,
9744 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009745
9746 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009747 GenerateCore(masm,
9748 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009749 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009750 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009751 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009752 false,
9753 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009754
9755 // Do full GC and retry runtime call one final time.
9756 Failure* failure = Failure::InternalError();
9757 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
9758 GenerateCore(masm,
9759 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009760 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009761 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00009762 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00009763 true,
9764 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009765
9766 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00009767 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
9768
9769 __ bind(&throw_termination_exception);
9770 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009771
9772 __ bind(&throw_normal_exception);
9773 GenerateThrowTOS(masm);
9774}
9775
9776
9777void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
9778 // r0: code entry
9779 // r1: function
9780 // r2: receiver
9781 // r3: argc
9782 // [sp+0]: argv
9783
9784 Label invoke, exit;
9785
9786 // Called from C, so do not pop argc and args on exit (preserve sp)
9787 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009788 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009789 __ stm(db_w, sp, kCalleeSaved | lr.bit());
9790
9791 // Get address of argv, see stm above.
9792 // r0: code entry
9793 // r1: function
9794 // r2: receiver
9795 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00009796 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009797
9798 // Push a frame with special values setup to mark it as an entry frame.
9799 // r0: code entry
9800 // r1: function
9801 // r2: receiver
9802 // r3: argc
9803 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009804 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009805 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
9806 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009807 __ mov(r6, Operand(Smi::FromInt(marker)));
9808 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9809 __ ldr(r5, MemOperand(r5));
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00009810 __ Push(r8, r7, r6, r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009811
9812 // Setup frame pointer for the frame to be pushed.
9813 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9814
9815 // Call a faked try-block that does the invoke.
9816 __ bl(&invoke);
9817
9818 // Caught exception: Store result (exception) in the pending
9819 // exception field in the JSEnv and return a failure sentinel.
9820 // Coming in here the fp will be invalid because the PushTryHandler below
9821 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00009822 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009823 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00009824 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009825 __ b(&exit);
9826
9827 // Invoke: Link this frame into the handler chain.
9828 __ bind(&invoke);
9829 // Must preserve r0-r4, r5-r7 are available.
9830 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009831 // If an exception not caught by another handler occurs, this handler
9832 // returns control to the code after the bl(&invoke) above, which
9833 // restores all kCalleeSaved registers (including cp and fp) to their
9834 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009835
9836 // Clear any pending exceptions.
9837 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
9838 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00009839 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009840 __ str(r5, MemOperand(ip));
9841
9842 // Invoke the function by calling through JS entry trampoline builtin.
9843 // Notice that we cannot store a reference to the trampoline code directly in
9844 // this stub, because runtime stubs are not traversed when doing GC.
9845
9846 // Expected registers by Builtins::JSEntryTrampoline
9847 // r0: code entry
9848 // r1: function
9849 // r2: receiver
9850 // r3: argc
9851 // r4: argv
9852 if (is_construct) {
9853 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
9854 __ mov(ip, Operand(construct_entry));
9855 } else {
9856 ExternalReference entry(Builtins::JSEntryTrampoline);
9857 __ mov(ip, Operand(entry));
9858 }
9859 __ ldr(ip, MemOperand(ip)); // deref address
9860
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009861 // Branch and link to JSEntryTrampoline. We don't use the double underscore
9862 // macro for the add instruction because we don't want the coverage tool
9863 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009864 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009865 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009866
9867 // Unlink this frame from the handler chain. When reading the
9868 // address of the next handler, there is no need to use the address
9869 // displacement since the current stack pointer (sp) points directly
9870 // to the stack handler.
9871 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
9872 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
9873 __ str(r3, MemOperand(ip));
9874 // No need to restore registers
9875 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
9876
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009877
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009878 __ bind(&exit); // r0 holds result
9879 // Restore the top frame descriptors from the stack.
9880 __ pop(r3);
9881 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
9882 __ str(r3, MemOperand(ip));
9883
9884 // Reset the stack to the callee saved registers.
9885 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
9886
9887 // Restore callee-saved registers and return.
9888#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00009889 if (FLAG_debug_code) {
9890 __ mov(lr, Operand(pc));
9891 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009892#endif
9893 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
9894}
9895
9896
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009897// This stub performs an instanceof, calling the builtin function if
9898// necessary. Uses r1 for the object, r0 for the function that it may
9899// be an instance of (these are fetched from the stack).
9900void InstanceofStub::Generate(MacroAssembler* masm) {
9901 // Get the object - slow case for smis (we may need to throw an exception
9902 // depending on the rhs).
9903 Label slow, loop, is_instance, is_not_instance;
9904 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
9905 __ BranchOnSmi(r0, &slow);
9906
9907 // Check that the left hand is a JS object and put map in r3.
9908 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
9909 __ b(lt, &slow);
9910 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
9911 __ b(gt, &slow);
9912
9913 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00009914 __ ldr(r1, MemOperand(sp, 0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009915 // r1 is function, r3 is map.
9916
9917 // Look up the function and the map in the instanceof cache.
9918 Label miss;
9919 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
9920 __ cmp(r1, ip);
9921 __ b(ne, &miss);
9922 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
9923 __ cmp(r3, ip);
9924 __ b(ne, &miss);
9925 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
9926 __ pop();
9927 __ pop();
9928 __ mov(pc, Operand(lr));
9929
9930 __ bind(&miss);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009931 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
9932
9933 // Check that the function prototype is a JS object.
9934 __ BranchOnSmi(r4, &slow);
9935 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
9936 __ b(lt, &slow);
9937 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
9938 __ b(gt, &slow);
9939
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009940 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
9941 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
9942
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009943 // Register mapping: r3 is object map and r4 is function prototype.
9944 // Get prototype of object into r2.
9945 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
9946
9947 // Loop through the prototype chain looking for the function prototype.
9948 __ bind(&loop);
9949 __ cmp(r2, Operand(r4));
9950 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00009951 __ LoadRoot(ip, Heap::kNullValueRootIndex);
9952 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009953 __ b(eq, &is_not_instance);
9954 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
9955 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
9956 __ jmp(&loop);
9957
9958 __ bind(&is_instance);
9959 __ mov(r0, Operand(Smi::FromInt(0)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009960 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009961 __ pop();
9962 __ pop();
9963 __ mov(pc, Operand(lr)); // Return.
9964
9965 __ bind(&is_not_instance);
9966 __ mov(r0, Operand(Smi::FromInt(1)));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00009967 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009968 __ pop();
9969 __ pop();
9970 __ mov(pc, Operand(lr)); // Return.
9971
9972 // Slow-case. Tail call builtin.
9973 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009974 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
9975}
9976
9977
ager@chromium.org7c537e22008-10-16 08:43:32 +00009978void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
9979 // The displacement is the offset of the last parameter (if any)
9980 // relative to the frame pointer.
9981 static const int kDisplacement =
9982 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009983
ager@chromium.org7c537e22008-10-16 08:43:32 +00009984 // Check that the key is a smi.
9985 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00009986 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009987
ager@chromium.org7c537e22008-10-16 08:43:32 +00009988 // Check if the calling frame is an arguments adaptor frame.
9989 Label adaptor;
9990 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
9991 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00009992 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00009993 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00009994
ager@chromium.org7c537e22008-10-16 08:43:32 +00009995 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00009996 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00009997 // check for free.
9998 __ cmp(r1, r0);
9999 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010000
ager@chromium.org7c537e22008-10-16 08:43:32 +000010001 // Read the argument from the stack and return it.
10002 __ sub(r3, r0, r1);
10003 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10004 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010005 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010006
10007 // Arguments adaptor case: Check index against actual arguments
10008 // limit found in the arguments adaptor frame. Use unsigned
10009 // comparison to get negative check for free.
10010 __ bind(&adaptor);
10011 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10012 __ cmp(r1, r0);
10013 __ b(cs, &slow);
10014
10015 // Read the argument from the adaptor frame and return it.
10016 __ sub(r3, r0, r1);
10017 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
10018 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +000010019 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010020
10021 // Slow-case: Handle non-smi or out-of-bounds access to arguments
10022 // by calling the runtime system.
10023 __ bind(&slow);
10024 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010025 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010026}
10027
10028
10029void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010030 // sp[0] : number of parameters
10031 // sp[4] : receiver displacement
10032 // sp[8] : function
10033
ager@chromium.org7c537e22008-10-16 08:43:32 +000010034 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +000010035 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +000010036 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
10037 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +000010038 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +000010039 __ b(eq, &adaptor_frame);
10040
10041 // Get the length from the frame.
10042 __ ldr(r1, MemOperand(sp, 0));
10043 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +000010044
10045 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +000010046 __ bind(&adaptor_frame);
10047 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
10048 __ str(r1, MemOperand(sp, 0));
10049 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +000010050 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
10051 __ str(r3, MemOperand(sp, 1 * kPointerSize));
10052
ager@chromium.org5c838252010-02-19 08:53:10 +000010053 // Try the new space allocation. Start out with computing the size
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010054 // of the arguments object and the elements array in words.
ager@chromium.org5c838252010-02-19 08:53:10 +000010055 Label add_arguments_object;
10056 __ bind(&try_allocate);
10057 __ cmp(r1, Operand(0));
10058 __ b(eq, &add_arguments_object);
10059 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
10060 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
10061 __ bind(&add_arguments_object);
10062 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
10063
10064 // Do the allocation of both objects in one go.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010065 __ AllocateInNewSpace(
10066 r1,
10067 r0,
10068 r2,
10069 r3,
10070 &runtime,
10071 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
ager@chromium.org5c838252010-02-19 08:53:10 +000010072
10073 // Get the arguments boilerplate from the current (global) context.
10074 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
10075 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
10076 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
10077 __ ldr(r4, MemOperand(r4, offset));
10078
10079 // Copy the JS object part.
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +000010080 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
ager@chromium.org5c838252010-02-19 08:53:10 +000010081
10082 // Setup the callee in-object property.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010083 STATIC_ASSERT(Heap::arguments_callee_index == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000010084 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
10085 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
10086
10087 // Get the length (smi tagged) and set that as an in-object property too.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010088 STATIC_ASSERT(Heap::arguments_length_index == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000010089 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
10090 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
10091
10092 // If there are no actual arguments, we're done.
10093 Label done;
10094 __ cmp(r1, Operand(0));
10095 __ b(eq, &done);
10096
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010097 // Get the parameters pointer from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +000010098 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000010099
10100 // Setup the elements pointer in the allocated arguments object and
10101 // initialize the header in the elements fixed array.
10102 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
10103 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
10104 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
10105 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
10106 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010107 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
ager@chromium.org5c838252010-02-19 08:53:10 +000010108
10109 // Copy the fixed array slots.
10110 Label loop;
10111 // Setup r4 to point to the first array slot.
10112 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
10113 __ bind(&loop);
10114 // Pre-decrement r2 with kPointerSize on each iteration.
10115 // Pre-decrement in order to skip receiver.
10116 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
10117 // Post-increment r4 with kPointerSize on each iteration.
10118 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
10119 __ sub(r1, r1, Operand(1));
10120 __ cmp(r1, Operand(0));
10121 __ b(ne, &loop);
10122
10123 // Return and remove the on-stack parameters.
10124 __ bind(&done);
10125 __ add(sp, sp, Operand(3 * kPointerSize));
10126 __ Ret();
10127
ager@chromium.org7c537e22008-10-16 08:43:32 +000010128 // Do the runtime call to allocate the arguments object.
10129 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010130 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010131}
10132
10133
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010134void RegExpExecStub::Generate(MacroAssembler* masm) {
10135 // Just jump directly to runtime if native RegExp is not selected at compile
10136 // time or if regexp entry in generated code is turned off runtime switch or
10137 // at compilation.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010138#ifdef V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010139 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010140#else // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010141 if (!FLAG_regexp_entry_native) {
10142 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
10143 return;
10144 }
10145
10146 // Stack frame on entry.
10147 // sp[0]: last_match_info (expected JSArray)
10148 // sp[4]: previous index
10149 // sp[8]: subject string
10150 // sp[12]: JSRegExp object
10151
10152 static const int kLastMatchInfoOffset = 0 * kPointerSize;
10153 static const int kPreviousIndexOffset = 1 * kPointerSize;
10154 static const int kSubjectOffset = 2 * kPointerSize;
10155 static const int kJSRegExpOffset = 3 * kPointerSize;
10156
10157 Label runtime, invoke_regexp;
10158
10159 // Allocation of registers for this function. These are in callee save
10160 // registers and will be preserved by the call to the native RegExp code, as
10161 // this code is called using the normal C calling convention. When calling
10162 // directly from generated code the native RegExp code will not do a GC and
10163 // therefore the content of these registers are safe to use after the call.
10164 Register subject = r4;
10165 Register regexp_data = r5;
10166 Register last_match_info_elements = r6;
10167
10168 // Ensure that a RegExp stack is allocated.
10169 ExternalReference address_of_regexp_stack_memory_address =
10170 ExternalReference::address_of_regexp_stack_memory_address();
10171 ExternalReference address_of_regexp_stack_memory_size =
10172 ExternalReference::address_of_regexp_stack_memory_size();
10173 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
10174 __ ldr(r0, MemOperand(r0, 0));
10175 __ tst(r0, Operand(r0));
10176 __ b(eq, &runtime);
10177
10178 // Check that the first argument is a JSRegExp object.
10179 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010180 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010181 __ tst(r0, Operand(kSmiTagMask));
10182 __ b(eq, &runtime);
10183 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
10184 __ b(ne, &runtime);
10185
10186 // Check that the RegExp has been compiled (data contains a fixed array).
10187 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
10188 if (FLAG_debug_code) {
10189 __ tst(regexp_data, Operand(kSmiTagMask));
10190 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
10191 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
10192 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
10193 }
10194
10195 // regexp_data: RegExp data (FixedArray)
10196 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
10197 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
10198 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
10199 __ b(ne, &runtime);
10200
10201 // regexp_data: RegExp data (FixedArray)
10202 // Check that the number of captures fit in the static offsets vector buffer.
10203 __ ldr(r2,
10204 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10205 // Calculate number of capture registers (number_of_captures + 1) * 2. This
10206 // uses the asumption that smis are 2 * their untagged value.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010207 STATIC_ASSERT(kSmiTag == 0);
10208 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010209 __ add(r2, r2, Operand(2)); // r2 was a smi.
10210 // Check that the static offsets vector buffer is large enough.
10211 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
10212 __ b(hi, &runtime);
10213
10214 // r2: Number of capture registers
10215 // regexp_data: RegExp data (FixedArray)
10216 // Check that the second argument is a string.
10217 __ ldr(subject, MemOperand(sp, kSubjectOffset));
10218 __ tst(subject, Operand(kSmiTagMask));
10219 __ b(eq, &runtime);
10220 Condition is_string = masm->IsObjectStringType(subject, r0);
10221 __ b(NegateCondition(is_string), &runtime);
10222 // Get the length of the string to r3.
10223 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
10224
10225 // r2: Number of capture registers
ager@chromium.orgac091b72010-05-05 07:34:42 +000010226 // r3: Length of subject string as a smi
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010227 // subject: Subject string
10228 // regexp_data: RegExp data (FixedArray)
10229 // Check that the third argument is a positive smi less than the subject
10230 // string length. A negative value will be greater (unsigned comparison).
10231 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010232 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010233 __ b(ne, &runtime);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010234 __ cmp(r3, Operand(r0));
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000010235 __ b(ls, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010236
10237 // r2: Number of capture registers
10238 // subject: Subject string
10239 // regexp_data: RegExp data (FixedArray)
10240 // Check that the fourth object is a JSArray object.
10241 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10242 __ tst(r0, Operand(kSmiTagMask));
10243 __ b(eq, &runtime);
10244 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
10245 __ b(ne, &runtime);
10246 // Check that the JSArray is in fast case.
10247 __ ldr(last_match_info_elements,
10248 FieldMemOperand(r0, JSArray::kElementsOffset));
10249 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010250 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000010251 __ cmp(r0, ip);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010252 __ b(ne, &runtime);
10253 // Check that the last match info has space for the capture registers and the
10254 // additional information.
10255 __ ldr(r0,
10256 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
10257 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010258 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010259 __ b(gt, &runtime);
10260
10261 // subject: Subject string
10262 // regexp_data: RegExp data (FixedArray)
10263 // Check the representation and encoding of the subject string.
10264 Label seq_string;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010265 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10266 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010267 // First check for flat string.
10268 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010269 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010270 __ b(eq, &seq_string);
10271
10272 // subject: Subject string
10273 // regexp_data: RegExp data (FixedArray)
10274 // Check for flat cons string.
10275 // A flat cons string is a cons string where the second part is the empty
10276 // string. In that case the subject string is just the first part of the cons
10277 // string. Also in this case the first part of the cons string is known to be
10278 // a sequential string or an external string.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010279 STATIC_ASSERT(kExternalStringTag !=0);
10280 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010281 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010282 __ b(ne, &runtime);
10283 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
10284 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
10285 __ cmp(r0, r1);
10286 __ b(ne, &runtime);
10287 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
10288 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
10289 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010290 // Is first part a flat string?
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010291 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010292 __ tst(r0, Operand(kStringRepresentationMask));
10293 __ b(nz, &runtime);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010294
10295 __ bind(&seq_string);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010296 // subject: Subject string
10297 // regexp_data: RegExp data (FixedArray)
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010298 // r0: Instance type of subject string
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010299 STATIC_ASSERT(4 == kAsciiStringTag);
10300 STATIC_ASSERT(kTwoByteStringTag == 0);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010301 // Find the code object based on the assumptions above.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000010302 __ and_(r0, r0, Operand(kStringEncodingMask));
10303 __ mov(r3, Operand(r0, ASR, 2), SetCC);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010304 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
10305 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
10306
10307 // Check that the irregexp code has been generated for the actual string
10308 // encoding. If it has, the field contains a code object otherwise it contains
10309 // the hole.
10310 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
10311 __ b(ne, &runtime);
10312
10313 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10314 // r7: code
10315 // subject: Subject string
10316 // regexp_data: RegExp data (FixedArray)
10317 // Load used arguments before starting to push arguments for call to native
10318 // RegExp code to avoid handling changing stack height.
10319 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
10320 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
10321
10322 // r1: previous index
10323 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
10324 // r7: code
10325 // subject: Subject string
10326 // regexp_data: RegExp data (FixedArray)
10327 // All checks done. Now push arguments for native regexp code.
10328 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
10329
10330 static const int kRegExpExecuteArguments = 7;
10331 __ push(lr);
10332 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
10333
10334 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
10335 __ mov(r0, Operand(1));
10336 __ str(r0, MemOperand(sp, 2 * kPointerSize));
10337
10338 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
10339 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
10340 __ ldr(r0, MemOperand(r0, 0));
10341 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
10342 __ ldr(r2, MemOperand(r2, 0));
10343 __ add(r0, r0, Operand(r2));
10344 __ str(r0, MemOperand(sp, 1 * kPointerSize));
10345
10346 // Argument 5 (sp[0]): static offsets vector buffer.
10347 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
10348 __ str(r0, MemOperand(sp, 0 * kPointerSize));
10349
10350 // For arguments 4 and 3 get string length, calculate start of string data and
10351 // calculate the shift of the index (0 for ASCII and 1 for two byte).
10352 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010353 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010354 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010355 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
10356 __ eor(r3, r3, Operand(1));
10357 // Argument 4 (r3): End of string data
10358 // Argument 3 (r2): Start of string data
10359 __ add(r2, r9, Operand(r1, LSL, r3));
10360 __ add(r3, r9, Operand(r0, LSL, r3));
10361
10362 // Argument 2 (r1): Previous index.
10363 // Already there
10364
10365 // Argument 1 (r0): Subject string.
10366 __ mov(r0, subject);
10367
10368 // Locate the code entry and call it.
10369 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
10370 __ CallCFunction(r7, kRegExpExecuteArguments);
10371 __ pop(lr);
10372
10373 // r0: result
10374 // subject: subject string (callee saved)
10375 // regexp_data: RegExp data (callee saved)
10376 // last_match_info_elements: Last match info elements (callee saved)
10377
10378 // Check the result.
10379 Label success;
10380 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
10381 __ b(eq, &success);
10382 Label failure;
10383 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
10384 __ b(eq, &failure);
10385 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
10386 // If not exception it can only be retry. Handle that in the runtime system.
10387 __ b(ne, &runtime);
10388 // Result must now be exception. If there is no pending exception already a
10389 // stack overflow (on the backtrack stack) was detected in RegExp code but
10390 // haven't created the exception yet. Handle that in the runtime system.
10391 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
10392 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
10393 __ ldr(r0, MemOperand(r0, 0));
10394 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
10395 __ ldr(r1, MemOperand(r1, 0));
10396 __ cmp(r0, r1);
10397 __ b(eq, &runtime);
10398 __ bind(&failure);
10399 // For failure and exception return null.
10400 __ mov(r0, Operand(Factory::null_value()));
10401 __ add(sp, sp, Operand(4 * kPointerSize));
10402 __ Ret();
10403
10404 // Process the result from the native regexp code.
10405 __ bind(&success);
10406 __ ldr(r1,
10407 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
10408 // Calculate number of capture registers (number_of_captures + 1) * 2.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010409 STATIC_ASSERT(kSmiTag == 0);
10410 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010411 __ add(r1, r1, Operand(2)); // r1 was a smi.
10412
10413 // r1: number of capture registers
10414 // r4: subject string
10415 // Store the capture count.
10416 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
10417 __ str(r2, FieldMemOperand(last_match_info_elements,
10418 RegExpImpl::kLastCaptureCountOffset));
10419 // Store last subject and last input.
10420 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010421 __ str(subject,
10422 FieldMemOperand(last_match_info_elements,
10423 RegExpImpl::kLastSubjectOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010424 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010425 __ str(subject,
10426 FieldMemOperand(last_match_info_elements,
10427 RegExpImpl::kLastInputOffset));
10428 __ mov(r3, last_match_info_elements);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000010429 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010430
10431 // Get the static offsets vector filled by the native regexp code.
10432 ExternalReference address_of_static_offsets_vector =
10433 ExternalReference::address_of_static_offsets_vector();
10434 __ mov(r2, Operand(address_of_static_offsets_vector));
10435
10436 // r1: number of capture registers
10437 // r2: offsets vector
10438 Label next_capture, done;
10439 // Capture register counter starts from number of capture registers and
10440 // counts down until wraping after zero.
10441 __ add(r0,
10442 last_match_info_elements,
10443 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
10444 __ bind(&next_capture);
10445 __ sub(r1, r1, Operand(1), SetCC);
10446 __ b(mi, &done);
10447 // Read the value from the static offsets vector buffer.
10448 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
10449 // Store the smi value in the last match info.
10450 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
10451 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
10452 __ jmp(&next_capture);
10453 __ bind(&done);
10454
10455 // Return last match info.
10456 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
10457 __ add(sp, sp, Operand(4 * kPointerSize));
10458 __ Ret();
10459
10460 // Do the runtime call to execute the regexp.
10461 __ bind(&runtime);
10462 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000010463#endif // V8_INTERPRETED_REGEXP
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000010464}
10465
10466
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010467void CallFunctionStub::Generate(MacroAssembler* masm) {
10468 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000010469
10470 // If the receiver might be a value (string, number or boolean) check for this
10471 // and box it if it is.
10472 if (ReceiverMightBeValue()) {
10473 // Get the receiver from the stack.
10474 // function, receiver [, arguments]
10475 Label receiver_is_value, receiver_is_js_object;
10476 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
10477
10478 // Check if receiver is a smi (which is a number value).
10479 __ BranchOnSmi(r1, &receiver_is_value);
10480
10481 // Check if the receiver is a valid JS object.
10482 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
10483 __ b(ge, &receiver_is_js_object);
10484
10485 // Call the runtime to box the value.
10486 __ bind(&receiver_is_value);
10487 __ EnterInternalFrame();
10488 __ push(r1);
10489 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
10490 __ LeaveInternalFrame();
10491 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
10492
10493 __ bind(&receiver_is_js_object);
10494 }
10495
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010496 // Get the function to call from the stack.
10497 // function, receiver [, arguments]
10498 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
10499
10500 // Check that the function is really a JavaScript function.
10501 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010502 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010503 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000010504 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010505 __ b(ne, &slow);
10506
10507 // Fast-case: Invoke the function now.
10508 // r1: pushed function
10509 ParameterCount actual(argc_);
10510 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
10511
10512 // Slow-case: Non-function called.
10513 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +000010514 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
10515 // of the original receiver from the call site).
10516 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010517 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000010518 __ mov(r2, Operand(0));
10519 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
10520 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
10521 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000010522}
10523
10524
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010525// Unfortunately you have to run without snapshots to see most of these
10526// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010527const char* CompareStub::GetName() {
ager@chromium.orgb5737492010-07-15 09:29:43 +000010528 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10529 (lhs_.is(r1) && rhs_.is(r0)));
10530
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010531 if (name_ != NULL) return name_;
10532 const int kMaxNameLength = 100;
10533 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
10534 if (name_ == NULL) return "OOM";
10535
10536 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010537 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010538 case lt: cc_name = "LT"; break;
10539 case gt: cc_name = "GT"; break;
10540 case le: cc_name = "LE"; break;
10541 case ge: cc_name = "GE"; break;
10542 case eq: cc_name = "EQ"; break;
10543 case ne: cc_name = "NE"; break;
10544 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010545 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010546
ager@chromium.orgb5737492010-07-15 09:29:43 +000010547 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
10548 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
10549
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010550 const char* strict_name = "";
10551 if (strict_ && (cc_ == eq || cc_ == ne)) {
10552 strict_name = "_STRICT";
10553 }
10554
10555 const char* never_nan_nan_name = "";
10556 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
10557 never_nan_nan_name = "_NO_NAN";
10558 }
10559
10560 const char* include_number_compare_name = "";
10561 if (!include_number_compare_) {
10562 include_number_compare_name = "_NO_NUMBER";
10563 }
10564
10565 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
ager@chromium.orgb5737492010-07-15 09:29:43 +000010566 "CompareStub_%s%s%s%s%s%s",
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010567 cc_name,
ager@chromium.orgb5737492010-07-15 09:29:43 +000010568 lhs_name,
10569 rhs_name,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010570 strict_name,
10571 never_nan_nan_name,
10572 include_number_compare_name);
10573 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000010574}
10575
10576
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010577int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010578 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
10579 // stubs the never NaN NaN condition is only taken into account if the
10580 // condition is equals.
ager@chromium.orgb5737492010-07-15 09:29:43 +000010581 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
10582 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
10583 (lhs_.is(r1) && rhs_.is(r0)));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010584 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
ager@chromium.orgb5737492010-07-15 09:29:43 +000010585 | RegisterField::encode(lhs_.is(r0))
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000010586 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000010587 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
10588 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000010589}
10590
10591
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010592// StringCharCodeAtGenerator
10593
10594void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
10595 Label flat_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010596 Label ascii_string;
10597 Label got_char_code;
10598
10599 // If the receiver is a smi trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010600 __ BranchOnSmi(object_, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010601
10602 // Fetch the instance type of the receiver into result register.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010603 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10604 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010605 // If the receiver is not a string trigger the non-string case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010606 __ tst(result_, Operand(kIsNotStringMask));
10607 __ b(ne, receiver_not_string_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010608
10609 // If the index is non-smi trigger the non-smi case.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010610 __ BranchOnNotSmi(index_, &index_not_smi_);
10611
10612 // Put smi-tagged index into scratch register.
10613 __ mov(scratch_, index_);
10614 __ bind(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010615
10616 // Check for index out of range.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010617 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
10618 __ cmp(ip, Operand(scratch_));
10619 __ b(ls, index_out_of_range_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010620
10621 // We need special handling for non-flat strings.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010622 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010623 __ tst(result_, Operand(kStringRepresentationMask));
10624 __ b(eq, &flat_string);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010625
10626 // Handle non-flat strings.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010627 __ tst(result_, Operand(kIsConsStringMask));
10628 __ b(eq, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010629
10630 // ConsString.
10631 // Check whether the right hand side is the empty string (i.e. if
10632 // this is really a flat string in a cons string). If that is not
10633 // the case we would rather go to the runtime system now to flatten
10634 // the string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010635 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
10636 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
10637 __ cmp(result_, Operand(ip));
10638 __ b(ne, &call_runtime_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010639 // Get the first of the two strings and load its instance type.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010640 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
10641 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10642 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
10643 // If the first cons component is also non-flat, then go to runtime.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010644 STATIC_ASSERT(kSeqStringTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010645 __ tst(result_, Operand(kStringRepresentationMask));
10646 __ b(nz, &call_runtime_);
10647
10648 // Check for 1-byte or 2-byte string.
10649 __ bind(&flat_string);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010650 STATIC_ASSERT(kAsciiStringTag != 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010651 __ tst(result_, Operand(kStringEncodingMask));
10652 __ b(nz, &ascii_string);
10653
10654 // 2-byte string.
10655 // Load the 2-byte character code into the result register. We can
10656 // add without shifting since the smi tag size is the log2 of the
10657 // number of bytes in a two-byte character.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010658 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010659 __ add(scratch_, object_, Operand(scratch_));
10660 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
10661 __ jmp(&got_char_code);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010662
10663 // ASCII string.
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010664 // Load the byte into the result register.
ager@chromium.orgac091b72010-05-05 07:34:42 +000010665 __ bind(&ascii_string);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010666 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
10667 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
ager@chromium.orgac091b72010-05-05 07:34:42 +000010668
10669 __ bind(&got_char_code);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010670 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
10671 __ bind(&exit_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010672}
10673
10674
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010675void StringCharCodeAtGenerator::GenerateSlow(
10676 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10677 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
ager@chromium.orgac091b72010-05-05 07:34:42 +000010678
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010679 // Index is not a smi.
10680 __ bind(&index_not_smi_);
10681 // If index is a heap number, try converting it to an integer.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000010682 __ CheckMap(index_,
10683 scratch_,
10684 Heap::kHeapNumberMapRootIndex,
10685 index_not_number_,
10686 true);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010687 call_helper.BeforeCall(masm);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010688 __ Push(object_, index_);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010689 __ push(index_); // Consumed by runtime conversion function.
10690 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
10691 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
10692 } else {
10693 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
10694 // NumberToSmi discards numbers that are not exact integers.
10695 __ CallRuntime(Runtime::kNumberToSmi, 1);
10696 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010697 // Save the conversion result before the pop instructions below
10698 // have a chance to overwrite it.
10699 __ Move(scratch_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010700 __ pop(index_);
10701 __ pop(object_);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000010702 // Reload the instance type.
10703 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
10704 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010705 call_helper.AfterCall(masm);
10706 // If index is still not a smi, it must be out of range.
10707 __ BranchOnNotSmi(scratch_, index_out_of_range_);
10708 // Otherwise, return to the fast path.
10709 __ jmp(&got_smi_index_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010710
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010711 // Call runtime. We get here when the receiver is a string and the
10712 // index is a number, but the code of getting the actual character
10713 // is too complex (e.g., when the string needs to be flattened).
10714 __ bind(&call_runtime_);
10715 call_helper.BeforeCall(masm);
10716 __ Push(object_, index_);
10717 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010718 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010719 call_helper.AfterCall(masm);
10720 __ jmp(&exit_);
10721
10722 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
10723}
10724
10725
10726// -------------------------------------------------------------------------
10727// StringCharFromCodeGenerator
10728
10729void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
ager@chromium.orgac091b72010-05-05 07:34:42 +000010730 // Fast case of Heap::LookupSingleCharacterStringFromCode.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010731 STATIC_ASSERT(kSmiTag == 0);
10732 STATIC_ASSERT(kSmiShiftSize == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010733 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010734 __ tst(code_,
10735 Operand(kSmiTagMask |
10736 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
10737 __ b(nz, &slow_case_);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010738
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010739 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
10740 // At this point code register contains smi tagged ascii char code.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010741 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010742 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
10743 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
10744 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
10745 __ cmp(result_, Operand(ip));
10746 __ b(eq, &slow_case_);
10747 __ bind(&exit_);
10748}
ager@chromium.orgac091b72010-05-05 07:34:42 +000010749
ager@chromium.orgac091b72010-05-05 07:34:42 +000010750
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010751void StringCharFromCodeGenerator::GenerateSlow(
10752 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10753 __ Abort("Unexpected fallthrough to CharFromCode slow case");
10754
10755 __ bind(&slow_case_);
10756 call_helper.BeforeCall(masm);
10757 __ push(code_);
10758 __ CallRuntime(Runtime::kCharFromCode, 1);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +000010759 __ Move(result_, r0);
ricow@chromium.org30ce4112010-05-31 10:38:25 +000010760 call_helper.AfterCall(masm);
10761 __ jmp(&exit_);
10762
10763 __ Abort("Unexpected fallthrough from CharFromCode slow case");
10764}
10765
10766
10767// -------------------------------------------------------------------------
10768// StringCharAtGenerator
10769
10770void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
10771 char_code_at_generator_.GenerateFast(masm);
10772 char_from_code_generator_.GenerateFast(masm);
10773}
10774
10775
10776void StringCharAtGenerator::GenerateSlow(
10777 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
10778 char_code_at_generator_.GenerateSlow(masm, call_helper);
10779 char_from_code_generator_.GenerateSlow(masm, call_helper);
ager@chromium.orgac091b72010-05-05 07:34:42 +000010780}
10781
10782
10783void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
10784 Register dest,
10785 Register src,
10786 Register count,
10787 Register scratch,
10788 bool ascii) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010789 Label loop;
10790 Label done;
10791 // This loop just copies one character at a time, as it is only used for very
10792 // short strings.
10793 if (!ascii) {
10794 __ add(count, count, Operand(count), SetCC);
10795 } else {
10796 __ cmp(count, Operand(0));
10797 }
10798 __ b(eq, &done);
10799
10800 __ bind(&loop);
10801 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
10802 // Perform sub between load and dependent store to get the load time to
10803 // complete.
10804 __ sub(count, count, Operand(1), SetCC);
10805 __ strb(scratch, MemOperand(dest, 1, PostIndex));
10806 // last iteration.
10807 __ b(gt, &loop);
10808
10809 __ bind(&done);
10810}
10811
10812
10813enum CopyCharactersFlags {
10814 COPY_ASCII = 1,
10815 DEST_ALWAYS_ALIGNED = 2
10816};
10817
10818
ager@chromium.orgac091b72010-05-05 07:34:42 +000010819void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
10820 Register dest,
10821 Register src,
10822 Register count,
10823 Register scratch1,
10824 Register scratch2,
10825 Register scratch3,
10826 Register scratch4,
10827 Register scratch5,
10828 int flags) {
ager@chromium.org5c838252010-02-19 08:53:10 +000010829 bool ascii = (flags & COPY_ASCII) != 0;
10830 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
10831
10832 if (dest_always_aligned && FLAG_debug_code) {
10833 // Check that destination is actually word aligned if the flag says
10834 // that it is.
10835 __ tst(dest, Operand(kPointerAlignmentMask));
10836 __ Check(eq, "Destination of copy not aligned.");
10837 }
10838
10839 const int kReadAlignment = 4;
10840 const int kReadAlignmentMask = kReadAlignment - 1;
10841 // Ensure that reading an entire aligned word containing the last character
10842 // of a string will not read outside the allocated area (because we pad up
10843 // to kObjectAlignment).
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000010844 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
ager@chromium.org5c838252010-02-19 08:53:10 +000010845 // Assumes word reads and writes are little endian.
10846 // Nothing to do for zero characters.
10847 Label done;
10848 if (!ascii) {
10849 __ add(count, count, Operand(count), SetCC);
10850 } else {
10851 __ cmp(count, Operand(0));
10852 }
10853 __ b(eq, &done);
10854
10855 // Assume that you cannot read (or write) unaligned.
10856 Label byte_loop;
10857 // Must copy at least eight bytes, otherwise just do it one byte at a time.
10858 __ cmp(count, Operand(8));
10859 __ add(count, dest, Operand(count));
10860 Register limit = count; // Read until src equals this.
10861 __ b(lt, &byte_loop);
10862
10863 if (!dest_always_aligned) {
10864 // Align dest by byte copying. Copies between zero and three bytes.
10865 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
10866 Label dest_aligned;
10867 __ b(eq, &dest_aligned);
10868 __ cmp(scratch4, Operand(2));
10869 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
10870 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
10871 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
10872 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10873 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
10874 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
10875 __ bind(&dest_aligned);
10876 }
10877
10878 Label simple_loop;
10879
10880 __ sub(scratch4, dest, Operand(src));
10881 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
10882 __ b(eq, &simple_loop);
10883 // Shift register is number of bits in a source word that
10884 // must be combined with bits in the next source word in order
10885 // to create a destination word.
10886
10887 // Complex loop for src/dst that are not aligned the same way.
10888 {
10889 Label loop;
10890 __ mov(scratch4, Operand(scratch4, LSL, 3));
10891 Register left_shift = scratch4;
10892 __ and_(src, src, Operand(~3)); // Round down to load previous word.
10893 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10894 // Store the "shift" most significant bits of scratch in the least
10895 // signficant bits (i.e., shift down by (32-shift)).
10896 __ rsb(scratch2, left_shift, Operand(32));
10897 Register right_shift = scratch2;
10898 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
10899
10900 __ bind(&loop);
10901 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
10902 __ sub(scratch5, limit, Operand(dest));
10903 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
10904 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10905 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
10906 // Loop if four or more bytes left to copy.
10907 // Compare to eight, because we did the subtract before increasing dst.
10908 __ sub(scratch5, scratch5, Operand(8), SetCC);
10909 __ b(ge, &loop);
10910 }
10911 // There is now between zero and three bytes left to copy (negative that
10912 // number is in scratch5), and between one and three bytes already read into
10913 // scratch1 (eight times that number in scratch4). We may have read past
10914 // the end of the string, but because objects are aligned, we have not read
10915 // past the end of the object.
10916 // Find the minimum of remaining characters to move and preloaded characters
10917 // and write those as bytes.
10918 __ add(scratch5, scratch5, Operand(4), SetCC);
10919 __ b(eq, &done);
10920 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
10921 // Move minimum of bytes read and bytes left to copy to scratch4.
10922 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
10923 // Between one and three (value in scratch5) characters already read into
10924 // scratch ready to write.
10925 __ cmp(scratch5, Operand(2));
10926 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10927 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
10928 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
10929 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
10930 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
10931 // Copy any remaining bytes.
10932 __ b(&byte_loop);
10933
10934 // Simple loop.
10935 // Copy words from src to dst, until less than four bytes left.
10936 // Both src and dest are word aligned.
10937 __ bind(&simple_loop);
10938 {
10939 Label loop;
10940 __ bind(&loop);
10941 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
10942 __ sub(scratch3, limit, Operand(dest));
10943 __ str(scratch1, MemOperand(dest, 4, PostIndex));
10944 // Compare to 8, not 4, because we do the substraction before increasing
10945 // dest.
10946 __ cmp(scratch3, Operand(8));
10947 __ b(ge, &loop);
10948 }
10949
10950 // Copy bytes from src to dst until dst hits limit.
10951 __ bind(&byte_loop);
10952 __ cmp(dest, Operand(limit));
10953 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
10954 __ b(ge, &done);
10955 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
10956 __ b(&byte_loop);
10957
10958 __ bind(&done);
10959}
10960
10961
ager@chromium.orgac091b72010-05-05 07:34:42 +000010962void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
10963 Register c1,
10964 Register c2,
10965 Register scratch1,
10966 Register scratch2,
10967 Register scratch3,
10968 Register scratch4,
10969 Register scratch5,
10970 Label* not_found) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010971 // Register scratch3 is the general scratch register in this function.
10972 Register scratch = scratch3;
10973
10974 // Make sure that both characters are not digits as such strings has a
10975 // different hash algorithm. Don't try to look for these in the symbol table.
10976 Label not_array_index;
10977 __ sub(scratch, c1, Operand(static_cast<int>('0')));
10978 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10979 __ b(hi, &not_array_index);
10980 __ sub(scratch, c2, Operand(static_cast<int>('0')));
10981 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
10982
10983 // If check failed combine both characters into single halfword.
10984 // This is required by the contract of the method: code at the
10985 // not_found branch expects this combination in c1 register
10986 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
10987 __ b(ls, not_found);
10988
10989 __ bind(&not_array_index);
10990 // Calculate the two character string hash.
10991 Register hash = scratch1;
ager@chromium.orgac091b72010-05-05 07:34:42 +000010992 StringHelper::GenerateHashInit(masm, hash, c1);
10993 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
10994 StringHelper::GenerateHashGetHash(masm, hash);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000010995
10996 // Collect the two characters in a register.
10997 Register chars = c1;
10998 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
10999
11000 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11001 // hash: hash of two character string.
11002
11003 // Load symbol table
11004 // Load address of first element of the symbol table.
11005 Register symbol_table = c2;
11006 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
11007
11008 // Load undefined value
11009 Register undefined = scratch4;
11010 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
11011
11012 // Calculate capacity mask from the symbol table capacity.
11013 Register mask = scratch2;
11014 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
11015 __ mov(mask, Operand(mask, ASR, 1));
11016 __ sub(mask, mask, Operand(1));
11017
11018 // Calculate untagged address of the first element of the symbol table.
11019 Register first_symbol_table_element = symbol_table;
11020 __ add(first_symbol_table_element, symbol_table,
11021 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
11022
11023 // Registers
11024 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
11025 // hash: hash of two character string
11026 // mask: capacity mask
11027 // first_symbol_table_element: address of the first element of
11028 // the symbol table
11029 // scratch: -
11030
11031 // Perform a number of probes in the symbol table.
11032 static const int kProbes = 4;
11033 Label found_in_symbol_table;
11034 Label next_probe[kProbes];
11035 for (int i = 0; i < kProbes; i++) {
11036 Register candidate = scratch5; // Scratch register contains candidate.
11037
11038 // Calculate entry in symbol table.
11039 if (i > 0) {
11040 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
11041 } else {
11042 __ mov(candidate, hash);
11043 }
11044
11045 __ and_(candidate, candidate, Operand(mask));
11046
11047 // Load the entry from the symble table.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011048 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011049 __ ldr(candidate,
11050 MemOperand(first_symbol_table_element,
11051 candidate,
11052 LSL,
11053 kPointerSizeLog2));
11054
11055 // If entry is undefined no string with this hash can be found.
11056 __ cmp(candidate, undefined);
11057 __ b(eq, not_found);
11058
11059 // If length is not 2 the string is not a candidate.
11060 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011061 __ cmp(scratch, Operand(Smi::FromInt(2)));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011062 __ b(ne, &next_probe[i]);
11063
11064 // Check that the candidate is a non-external ascii string.
11065 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
11066 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
11067 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
11068 &next_probe[i]);
11069
11070 // Check if the two characters match.
11071 // Assumes that word load is little endian.
11072 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
11073 __ cmp(chars, scratch);
11074 __ b(eq, &found_in_symbol_table);
11075 __ bind(&next_probe[i]);
11076 }
11077
11078 // No matching 2 character string found by probing.
11079 __ jmp(not_found);
11080
11081 // Scratch register contains result when we fall through to here.
11082 Register result = scratch;
11083 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +000011084 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011085}
11086
11087
ager@chromium.orgac091b72010-05-05 07:34:42 +000011088void StringHelper::GenerateHashInit(MacroAssembler* masm,
11089 Register hash,
11090 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011091 // hash = character + (character << 10);
11092 __ add(hash, character, Operand(character, LSL, 10));
11093 // hash ^= hash >> 6;
11094 __ eor(hash, hash, Operand(hash, ASR, 6));
11095}
11096
11097
ager@chromium.orgac091b72010-05-05 07:34:42 +000011098void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
11099 Register hash,
11100 Register character) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011101 // hash += character;
11102 __ add(hash, hash, Operand(character));
11103 // hash += hash << 10;
11104 __ add(hash, hash, Operand(hash, LSL, 10));
11105 // hash ^= hash >> 6;
11106 __ eor(hash, hash, Operand(hash, ASR, 6));
11107}
11108
11109
ager@chromium.orgac091b72010-05-05 07:34:42 +000011110void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
11111 Register hash) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011112 // hash += hash << 3;
11113 __ add(hash, hash, Operand(hash, LSL, 3));
11114 // hash ^= hash >> 11;
11115 __ eor(hash, hash, Operand(hash, ASR, 11));
11116 // hash += hash << 15;
11117 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
11118
11119 // if (hash == 0) hash = 27;
11120 __ mov(hash, Operand(27), LeaveCC, nz);
11121}
11122
11123
ager@chromium.org5c838252010-02-19 08:53:10 +000011124void SubStringStub::Generate(MacroAssembler* masm) {
11125 Label runtime;
11126
11127 // Stack frame on entry.
11128 // lr: return address
11129 // sp[0]: to
11130 // sp[4]: from
11131 // sp[8]: string
11132
11133 // This stub is called from the native-call %_SubString(...), so
11134 // nothing can be assumed about the arguments. It is tested that:
11135 // "string" is a sequential string,
11136 // both "from" and "to" are smis, and
11137 // 0 <= from <= to <= string.length.
11138 // If any of these assumptions fail, we call the runtime system.
11139
11140 static const int kToOffset = 0 * kPointerSize;
11141 static const int kFromOffset = 1 * kPointerSize;
11142 static const int kStringOffset = 2 * kPointerSize;
11143
11144
11145 // Check bounds and smi-ness.
11146 __ ldr(r7, MemOperand(sp, kToOffset));
11147 __ ldr(r6, MemOperand(sp, kFromOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011148 STATIC_ASSERT(kSmiTag == 0);
11149 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011150 // I.e., arithmetic shift right by one un-smi-tags.
11151 __ mov(r2, Operand(r7, ASR, 1), SetCC);
11152 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
11153 // If either r2 or r6 had the smi tag bit set, then carry is set now.
11154 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
11155 __ b(mi, &runtime); // From is negative.
11156
11157 __ sub(r2, r2, Operand(r3), SetCC);
11158 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011159 // Special handling of sub-strings of length 1 and 2. One character strings
11160 // are handled in the runtime system (looked up in the single character
11161 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +000011162 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011163 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +000011164
11165 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011166 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011167 // r6: from (smi)
11168 // r7: to (smi)
11169
11170 // Make sure first argument is a sequential (or flat) string.
11171 __ ldr(r5, MemOperand(sp, kStringOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011172 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011173 __ tst(r5, Operand(kSmiTagMask));
11174 __ b(eq, &runtime);
11175 Condition is_string = masm->IsObjectStringType(r5, r1);
11176 __ b(NegateCondition(is_string), &runtime);
11177
11178 // r1: instance type
11179 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011180 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011181 // r5: string
11182 // r6: from (smi)
11183 // r7: to (smi)
11184 Label seq_string;
11185 __ and_(r4, r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011186 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
11187 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
ager@chromium.org5c838252010-02-19 08:53:10 +000011188 __ cmp(r4, Operand(kConsStringTag));
11189 __ b(gt, &runtime); // External strings go to runtime.
11190 __ b(lt, &seq_string); // Sequential strings are handled directly.
11191
11192 // Cons string. Try to recurse (once) on the first substring.
11193 // (This adds a little more generality than necessary to handle flattened
11194 // cons strings, but not much).
11195 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
11196 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
11197 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11198 __ tst(r1, Operand(kStringRepresentationMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011199 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011200 __ b(ne, &runtime); // Cons and External strings go to runtime.
11201
11202 // Definitly a sequential string.
11203 __ bind(&seq_string);
11204
11205 // r1: instance type.
11206 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011207 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011208 // r5: string
11209 // r6: from (smi)
11210 // r7: to (smi)
11211 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
ager@chromium.orgac091b72010-05-05 07:34:42 +000011212 __ cmp(r4, Operand(r7));
ager@chromium.org5c838252010-02-19 08:53:10 +000011213 __ b(lt, &runtime); // Fail if to > length.
11214
11215 // r1: instance type.
11216 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011217 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +000011218 // r5: string.
11219 // r6: from offset (smi)
11220 // Check for flat ascii string.
11221 Label non_ascii_flat;
11222 __ tst(r1, Operand(kStringEncodingMask));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011223 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011224 __ b(eq, &non_ascii_flat);
11225
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011226 Label result_longer_than_two;
11227 __ cmp(r2, Operand(2));
11228 __ b(gt, &result_longer_than_two);
11229
11230 // Sub string of length 2 requested.
11231 // Get the two characters forming the sub string.
11232 __ add(r5, r5, Operand(r3));
11233 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
11234 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
11235
11236 // Try to lookup two character string in symbol table.
11237 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011238 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11239 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011240 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11241 __ add(sp, sp, Operand(3 * kPointerSize));
11242 __ Ret();
11243
11244 // r2: result string length.
11245 // r3: two characters combined into halfword in little endian byte order.
11246 __ bind(&make_two_character_string);
11247 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
11248 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11249 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11250 __ add(sp, sp, Operand(3 * kPointerSize));
11251 __ Ret();
11252
11253 __ bind(&result_longer_than_two);
11254
ager@chromium.org5c838252010-02-19 08:53:10 +000011255 // Allocate the result.
11256 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
11257
11258 // r0: result string.
11259 // r2: result string length.
11260 // r5: string.
11261 // r6: from offset (smi)
11262 // Locate first character of result.
11263 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11264 // Locate 'from' character of string.
11265 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11266 __ add(r5, r5, Operand(r6, ASR, 1));
11267
11268 // r0: result string.
11269 // r1: first character of result string.
11270 // r2: result string length.
11271 // r5: first character of sub string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011272 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011273 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11274 COPY_ASCII | DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011275 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11276 __ add(sp, sp, Operand(3 * kPointerSize));
11277 __ Ret();
11278
11279 __ bind(&non_ascii_flat);
11280 // r2: result string length.
11281 // r5: string.
11282 // r6: from offset (smi)
11283 // Check for flat two byte string.
11284
11285 // Allocate the result.
11286 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
11287
11288 // r0: result string.
11289 // r2: result string length.
11290 // r5: string.
11291 // Locate first character of result.
11292 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11293 // Locate 'from' character of string.
11294 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11295 // As "from" is a smi it is 2 times the value which matches the size of a two
11296 // byte character.
11297 __ add(r5, r5, Operand(r6));
11298
11299 // r0: result string.
11300 // r1: first character of result.
11301 // r2: result length.
11302 // r5: first character of string to copy.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011303 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011304 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
11305 DEST_ALWAYS_ALIGNED);
ager@chromium.org5c838252010-02-19 08:53:10 +000011306 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
11307 __ add(sp, sp, Operand(3 * kPointerSize));
11308 __ Ret();
11309
11310 // Just jump to runtime to create the sub string.
11311 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011312 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011313}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011314
11315
11316void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
11317 Register left,
11318 Register right,
11319 Register scratch1,
11320 Register scratch2,
11321 Register scratch3,
11322 Register scratch4) {
11323 Label compare_lengths;
11324 // Find minimum length and length difference.
11325 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
11326 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
11327 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
11328 Register length_delta = scratch3;
11329 __ mov(scratch1, scratch2, LeaveCC, gt);
11330 Register min_length = scratch1;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011331 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011332 __ tst(min_length, Operand(min_length));
11333 __ b(eq, &compare_lengths);
11334
ager@chromium.orgac091b72010-05-05 07:34:42 +000011335 // Untag smi.
11336 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
11337
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011338 // Setup registers so that we only need to increment one register
11339 // in the loop.
11340 __ add(scratch2, min_length,
11341 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11342 __ add(left, left, Operand(scratch2));
11343 __ add(right, right, Operand(scratch2));
11344 // Registers left and right points to the min_length character of strings.
11345 __ rsb(min_length, min_length, Operand(-1));
11346 Register index = min_length;
11347 // Index starts at -min_length.
11348
11349 {
11350 // Compare loop.
11351 Label loop;
11352 __ bind(&loop);
11353 // Compare characters.
11354 __ add(index, index, Operand(1), SetCC);
11355 __ ldrb(scratch2, MemOperand(left, index), ne);
11356 __ ldrb(scratch4, MemOperand(right, index), ne);
11357 // Skip to compare lengths with eq condition true.
11358 __ b(eq, &compare_lengths);
11359 __ cmp(scratch2, scratch4);
11360 __ b(eq, &loop);
11361 // Fallthrough with eq condition false.
11362 }
11363 // Compare lengths - strings up to min-length are equal.
11364 __ bind(&compare_lengths);
11365 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
11366 // Use zero length_delta as result.
11367 __ mov(r0, Operand(length_delta), SetCC, eq);
11368 // Fall through to here if characters compare not-equal.
11369 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
11370 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
11371 __ Ret();
11372}
11373
11374
11375void StringCompareStub::Generate(MacroAssembler* masm) {
11376 Label runtime;
11377
11378 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +000011379 // sp[0]: right string
11380 // sp[4]: left string
11381 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
11382 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011383
11384 Label not_same;
11385 __ cmp(r0, r1);
11386 __ b(ne, &not_same);
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011387 STATIC_ASSERT(EQUAL == 0);
11388 STATIC_ASSERT(kSmiTag == 0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011389 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
11390 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
11391 __ add(sp, sp, Operand(2 * kPointerSize));
11392 __ Ret();
11393
11394 __ bind(&not_same);
11395
11396 // Check that both objects are sequential ascii strings.
11397 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
11398
11399 // Compare flat ascii strings natively. Remove arguments from stack first.
11400 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
11401 __ add(sp, sp, Operand(2 * kPointerSize));
11402 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
11403
11404 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
11405 // tagged as a small integer.
11406 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011407 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000011408}
11409
11410
ager@chromium.org5c838252010-02-19 08:53:10 +000011411void StringAddStub::Generate(MacroAssembler* masm) {
11412 Label string_add_runtime;
11413 // Stack on entry:
11414 // sp[0]: second argument.
11415 // sp[4]: first argument.
11416
11417 // Load the two arguments.
11418 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
11419 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
11420
11421 // Make sure that both arguments are strings if not known in advance.
11422 if (string_check_) {
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011423 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011424 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
11425 // Load instance types.
11426 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11427 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11428 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11429 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011430 STATIC_ASSERT(kStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011431 // If either is not a string, go to runtime.
11432 __ tst(r4, Operand(kIsNotStringMask));
11433 __ tst(r5, Operand(kIsNotStringMask), eq);
11434 __ b(ne, &string_add_runtime);
11435 }
11436
11437 // Both arguments are strings.
11438 // r0: first string
11439 // r1: second string
11440 // r4: first string instance type (if string_check_)
11441 // r5: second string instance type (if string_check_)
11442 {
11443 Label strings_not_empty;
11444 // Check if either of the strings are empty. In that case return the other.
11445 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
11446 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011447 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011448 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
ager@chromium.org5c838252010-02-19 08:53:10 +000011449 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011450 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +000011451 // Else test if second string is empty.
11452 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
ager@chromium.org5c838252010-02-19 08:53:10 +000011453 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
11454
11455 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11456 __ add(sp, sp, Operand(2 * kPointerSize));
11457 __ Ret();
11458
11459 __ bind(&strings_not_empty);
11460 }
11461
ager@chromium.orgac091b72010-05-05 07:34:42 +000011462 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
11463 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
ager@chromium.org5c838252010-02-19 08:53:10 +000011464 // Both strings are non-empty.
11465 // r0: first string
11466 // r1: second string
11467 // r2: length of first string
11468 // r3: length of second string
11469 // r4: first string instance type (if string_check_)
11470 // r5: second string instance type (if string_check_)
11471 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011472 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +000011473 // Adding two lengths can't overflow.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011474 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
ager@chromium.org5c838252010-02-19 08:53:10 +000011475 __ add(r6, r2, Operand(r3));
11476 // Use the runtime system when adding two one character strings, as it
11477 // contains optimizations for this specific case using the symbol table.
11478 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011479 __ b(ne, &longer_than_two);
11480
11481 // Check that both strings are non-external ascii strings.
11482 if (!string_check_) {
11483 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11484 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11485 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11486 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11487 }
11488 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
11489 &string_add_runtime);
11490
11491 // Get the two characters forming the sub string.
11492 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11493 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
11494
11495 // Try to lookup two character string in symbol table. If it is not found
11496 // just allocate a new one.
11497 Label make_two_character_string;
ager@chromium.orgac091b72010-05-05 07:34:42 +000011498 StringHelper::GenerateTwoCharacterSymbolTableProbe(
11499 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011500 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11501 __ add(sp, sp, Operand(2 * kPointerSize));
11502 __ Ret();
11503
11504 __ bind(&make_two_character_string);
11505 // Resulting string has length 2 and first chars of two strings
11506 // are combined into single halfword in r2 register.
11507 // So we can fill resulting string without two loops by a single
11508 // halfword store instruction (which assumes that processor is
11509 // in a little endian mode)
11510 __ mov(r6, Operand(2));
11511 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
11512 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
11513 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11514 __ add(sp, sp, Operand(2 * kPointerSize));
11515 __ Ret();
11516
11517 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +000011518 // Check if resulting string will be flat.
11519 __ cmp(r6, Operand(String::kMinNonFlatLength));
11520 __ b(lt, &string_add_flat_result);
11521 // Handle exceptionally long strings in the runtime system.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011522 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011523 ASSERT(IsPowerOf2(String::kMaxLength + 1));
11524 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
11525 __ cmp(r6, Operand(String::kMaxLength + 1));
11526 __ b(hs, &string_add_runtime);
11527
11528 // If result is not supposed to be flat, allocate a cons string object.
11529 // If both strings are ascii the result is an ascii cons string.
11530 if (!string_check_) {
11531 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11532 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11533 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11534 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11535 }
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011536 Label non_ascii, allocated, ascii_data;
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011537 STATIC_ASSERT(kTwoByteStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011538 __ tst(r4, Operand(kStringEncodingMask));
11539 __ tst(r5, Operand(kStringEncodingMask), ne);
11540 __ b(eq, &non_ascii);
11541
11542 // Allocate an ASCII cons string.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011543 __ bind(&ascii_data);
ager@chromium.org5c838252010-02-19 08:53:10 +000011544 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
11545 __ bind(&allocated);
11546 // Fill the fields of the cons string.
11547 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
11548 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
11549 __ mov(r0, Operand(r7));
11550 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11551 __ add(sp, sp, Operand(2 * kPointerSize));
11552 __ Ret();
11553
11554 __ bind(&non_ascii);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011555 // At least one of the strings is two-byte. Check whether it happens
11556 // to contain only ascii characters.
11557 // r4: first instance type.
11558 // r5: second instance type.
11559 __ tst(r4, Operand(kAsciiDataHintMask));
11560 __ tst(r5, Operand(kAsciiDataHintMask), ne);
11561 __ b(ne, &ascii_data);
11562 __ eor(r4, r4, Operand(r5));
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011563 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000011564 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11565 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
11566 __ b(eq, &ascii_data);
11567
ager@chromium.org5c838252010-02-19 08:53:10 +000011568 // Allocate a two byte cons string.
11569 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
11570 __ jmp(&allocated);
11571
11572 // Handle creating a flat result. First check that both strings are
11573 // sequential and that they have the same encoding.
11574 // r0: first string
11575 // r1: second string
11576 // r2: length of first string
11577 // r3: length of second string
11578 // r4: first string instance type (if string_check_)
11579 // r5: second string instance type (if string_check_)
11580 // r6: sum of lengths.
11581 __ bind(&string_add_flat_result);
11582 if (!string_check_) {
11583 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
11584 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
11585 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
11586 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
11587 }
11588 // Check that both strings are sequential.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +000011589 STATIC_ASSERT(kSeqStringTag == 0);
ager@chromium.org5c838252010-02-19 08:53:10 +000011590 __ tst(r4, Operand(kStringRepresentationMask));
11591 __ tst(r5, Operand(kStringRepresentationMask), eq);
11592 __ b(ne, &string_add_runtime);
11593 // Now check if both strings have the same encoding (ASCII/Two-byte).
11594 // r0: first string.
11595 // r1: second string.
11596 // r2: length of first string.
11597 // r3: length of second string.
11598 // r6: sum of lengths..
11599 Label non_ascii_string_add_flat_result;
11600 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
11601 __ eor(r7, r4, Operand(r5));
11602 __ tst(r7, Operand(kStringEncodingMask));
11603 __ b(ne, &string_add_runtime);
11604 // And see if it's ASCII or two-byte.
11605 __ tst(r4, Operand(kStringEncodingMask));
11606 __ b(eq, &non_ascii_string_add_flat_result);
11607
11608 // Both strings are sequential ASCII strings. We also know that they are
11609 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011610 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +000011611 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
11612 // Locate first character of result.
11613 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11614 // Locate first character of first argument.
11615 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11616 // r0: first character of first string.
11617 // r1: second string.
11618 // r2: length of first string.
11619 // r3: length of second string.
11620 // r6: first character of result.
11621 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011622 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011623
11624 // Load second argument and locate first character.
11625 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
11626 // r1: first character of second string.
11627 // r3: length of second string.
11628 // r6: next character of result.
11629 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011630 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
ager@chromium.org5c838252010-02-19 08:53:10 +000011631 __ mov(r0, Operand(r7));
11632 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11633 __ add(sp, sp, Operand(2 * kPointerSize));
11634 __ Ret();
11635
11636 __ bind(&non_ascii_string_add_flat_result);
11637 // Both strings are sequential two byte strings.
11638 // r0: first string.
11639 // r1: second string.
11640 // r2: length of first string.
11641 // r3: length of second string.
11642 // r6: sum of length of strings.
11643 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
11644 // r0: first string.
11645 // r1: second string.
11646 // r2: length of first string.
11647 // r3: length of second string.
11648 // r7: result string.
11649
11650 // Locate first character of result.
11651 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11652 // Locate first character of first argument.
11653 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11654
11655 // r0: first character of first string.
11656 // r1: second string.
11657 // r2: length of first string.
11658 // r3: length of second string.
11659 // r6: first character of result.
11660 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011661 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011662
11663 // Locate first character of second argument.
11664 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
11665
11666 // r1: first character of second string.
11667 // r3: length of second string.
11668 // r6: next character of result (after copy of first string).
11669 // r7: result string.
ager@chromium.orgac091b72010-05-05 07:34:42 +000011670 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
ager@chromium.org5c838252010-02-19 08:53:10 +000011671
11672 __ mov(r0, Operand(r7));
11673 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
11674 __ add(sp, sp, Operand(2 * kPointerSize));
11675 __ Ret();
11676
11677 // Just jump to runtime to add the two strings.
11678 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000011679 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +000011680}
11681
11682
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000011683#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011684
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000011685} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000011686
11687#endif // V8_TARGET_ARCH_ARM