blob: ea4b165fd3c5451ce921297ae525d02fc8a93237 [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
30#include "bootstrapper.h"
31#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000032#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000034#include "parser.h"
35#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000037#include "scopes.h"
38
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
ager@chromium.org65dad4b2009-04-23 08:48:43 +000043#define __ ACCESS_MASM(masm_)
44
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000045static void EmitIdenticalObjectComparison(MacroAssembler* masm,
46 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000047 Condition cc,
48 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000050 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000051 Label* slow,
52 bool strict);
53static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
54static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055static void MultiplyByKnownInt(MacroAssembler* masm,
56 Register source,
57 Register destination,
58 int known_int);
59static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000060
61
62
ager@chromium.orge2902be2009-06-08 12:21:35 +000063// -------------------------------------------------------------------------
64// Platform-specific DeferredCode functions.
65
66void DeferredCode::SaveRegisters() {
67 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
68 int action = registers_[i];
69 if (action == kPush) {
70 __ push(RegisterAllocator::ToRegister(i));
71 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
72 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
73 }
74 }
75}
76
77
78void DeferredCode::RestoreRegisters() {
79 // Restore registers in reverse order due to the stack.
80 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
81 int action = registers_[i];
82 if (action == kPush) {
83 __ pop(RegisterAllocator::ToRegister(i));
84 } else if (action != kIgnore) {
85 action &= ~kSyncedFlag;
86 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
87 }
88 }
89}
90
ager@chromium.org3bf7b912008-11-17 09:09:45 +000091
92// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093// CodeGenState implementation.
94
ager@chromium.org7c537e22008-10-16 08:43:32 +000095CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097 true_target_(NULL),
98 false_target_(NULL),
99 previous_(NULL) {
100 owner_->set_state(this);
101}
102
103
ager@chromium.org7c537e22008-10-16 08:43:32 +0000104CodeGenState::CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000105 JumpTarget* true_target,
106 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000107 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108 true_target_(true_target),
109 false_target_(false_target),
110 previous_(owner->state()) {
111 owner_->set_state(this);
112}
113
114
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000115CodeGenState::~CodeGenState() {
116 ASSERT(owner_->state() == this);
117 owner_->set_state(previous_);
118}
119
120
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000121// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000122// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000124CodeGenerator::CodeGenerator(MacroAssembler* masm,
125 Handle<Script> script,
ager@chromium.org7c537e22008-10-16 08:43:32 +0000126 bool is_eval)
127 : is_eval_(is_eval),
128 script_(script),
129 deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000130 masm_(masm),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 cc_reg_(al),
135 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000136 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000143// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144// cp: callee's context
145
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000146void CodeGenerator::Generate(FunctionLiteral* fun,
147 Mode mode,
148 CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000149 // Record the position for debugging purposes.
150 CodeForFunctionPosition(fun);
151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 ZoneList<Statement*>* body = fun->body();
153
154 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000155 ASSERT(scope_ == NULL);
156 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000157 ASSERT(allocator_ == NULL);
158 RegisterAllocator register_allocator(this);
159 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000160 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000161 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000162 cc_reg_ = al;
163 {
164 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 // Entry:
167 // Stack: receiver, arguments
168 // lr: return address
169 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175#ifdef DEBUG
176 if (strlen(FLAG_stop_at) > 0 &&
177 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000178 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000179 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180 }
181#endif
182
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000183 if (mode == PRIMARY) {
184 frame_->Enter();
185 // tos: code slot
186
187 // Allocate space for locals and initialize them. This also checks
188 // for stack overflow.
189 frame_->AllocateStackSlots();
190
191 VirtualFrame::SpilledScope spilled_scope;
192 int heap_slots = scope_->num_heap_slots();
193 if (heap_slots > 0) {
194 // Allocate local context.
195 // Get outer context and create a new context based on it.
196 __ ldr(r0, frame_->Function());
197 frame_->EmitPush(r0);
198 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
199 FastNewContextStub stub(heap_slots);
200 frame_->CallStub(&stub, 1);
201 } else {
202 frame_->CallRuntime(Runtime::kNewContext, 1);
203 }
204
205#ifdef DEBUG
206 JumpTarget verified_true;
207 __ cmp(r0, Operand(cp));
208 verified_true.Branch(eq);
209 __ stop("NewContext: r0 is expected to be the same as cp");
210 verified_true.Bind();
211#endif
212 // Update context local.
213 __ str(cp, frame_->Context());
214 }
215
216 // TODO(1241774): Improve this code:
217 // 1) only needed if we have a context
218 // 2) no need to recompute context ptr every single time
219 // 3) don't copy parameter operand code from SlotOperand!
220 {
221 Comment cmnt2(masm_, "[ copy context parameters into .context");
222
223 // Note that iteration order is relevant here! If we have the same
224 // parameter twice (e.g., function (x, y, x)), and that parameter
225 // needs to be copied into the context, it must be the last argument
226 // passed to the parameter that needs to be copied. This is a rare
227 // case so we don't check for it, instead we rely on the copying
228 // order: such a parameter is copied repeatedly into the same
229 // context location and thus the last value is what is seen inside
230 // the function.
231 for (int i = 0; i < scope_->num_parameters(); i++) {
232 Variable* par = scope_->parameter(i);
233 Slot* slot = par->slot();
234 if (slot != NULL && slot->type() == Slot::CONTEXT) {
235 // No parameters in global scope.
236 ASSERT(!scope_->is_global_scope());
237 __ ldr(r1, frame_->ParameterAt(i));
238 // Loads r2 with context; used below in RecordWrite.
239 __ str(r1, SlotOperand(slot, r2));
240 // Load the offset into r3.
241 int slot_offset =
242 FixedArray::kHeaderSize + slot->index() * kPointerSize;
243 __ mov(r3, Operand(slot_offset));
244 __ RecordWrite(r2, r3, r1);
245 }
246 }
247 }
248
249 // Store the arguments object. This must happen after context
250 // initialization because the arguments object may be stored in the
251 // context.
252 if (scope_->arguments() != NULL) {
253 Comment cmnt(masm_, "[ allocate arguments object");
254 ASSERT(scope_->arguments_shadow() != NULL);
255 Variable* arguments = scope_->arguments()->var();
256 Variable* shadow = scope_->arguments_shadow()->var();
257 ASSERT(arguments != NULL && arguments->slot() != NULL);
258 ASSERT(shadow != NULL && shadow->slot() != NULL);
259 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
260 __ ldr(r2, frame_->Function());
261 // The receiver is below the arguments, the return address, and the
262 // frame pointer on the stack.
263 const int kReceiverDisplacement = 2 + scope_->num_parameters();
264 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
265 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
266 frame_->Adjust(3);
267 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
268 frame_->CallStub(&stub, 3);
269 frame_->EmitPush(r0);
270 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
271 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
272 frame_->Drop(); // Value is no longer needed.
273 }
274
275 // Initialize ThisFunction reference if present.
276 if (scope_->is_function_scope() && scope_->function() != NULL) {
277 __ mov(ip, Operand(Factory::the_hole_value()));
278 frame_->EmitPush(ip);
279 StoreToSlot(scope_->function()->slot(), NOT_CONST_INIT);
280 }
281 } else {
282 // When used as the secondary compiler for splitting, r1, cp,
283 // fp, and lr have been pushed on the stack. Adjust the virtual
284 // frame to match this state.
285 frame_->Adjust(4);
286 allocator_->Unuse(r1);
287 allocator_->Unuse(lr);
288 }
289
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000290 // Initialize the function return target after the locals are set
291 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000292 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000293 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000295 // Generate code to 'execute' declarations and initialize functions
296 // (source elements). In case of an illegal redeclaration we need to
297 // handle that instead of processing the declarations.
298 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000300 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 } else {
302 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000303 ProcessDeclarations(scope_->declarations());
304 // Bail out if a stack-overflow exception occurred when processing
305 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000306 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 }
308
mads.s.ager31e71382008-08-13 09:32:07 +0000309 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000311 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313
314 // Compile the body of the function in a vanilla state. Don't
315 // bother compiling all the code if the scope has an illegal
316 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000317 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 Comment cmnt(masm_, "[ function body");
319#ifdef DEBUG
320 bool is_builtin = Bootstrapper::IsActive();
321 bool should_trace =
322 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000323 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000324 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000325 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000328 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 }
331
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000332 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000333 if (has_valid_frame() || function_return_.is_linked()) {
334 if (!function_return_.is_linked()) {
335 CodeForReturnPosition(fun);
336 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000337 // exit
338 // r0: result
339 // sp: stack pointer
340 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000342 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000343
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000344 function_return_.Bind();
345 if (FLAG_trace) {
346 // Push the return value on the stack as the parameter.
347 // Runtime::TraceExit returns the parameter as it is.
348 frame_->EmitPush(r0);
349 frame_->CallRuntime(Runtime::kTraceExit, 1);
350 }
351
ager@chromium.org4af710e2009-09-15 12:20:11 +0000352 // Add a label for checking the size of the code used for returning.
353 Label check_exit_codesize;
354 masm_->bind(&check_exit_codesize);
355
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000356 // Calculate the exact length of the return sequence and make sure that
357 // the constant pool is not emitted inside of the return sequence.
358 int32_t sp_delta = (scope_->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000359 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000360 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
361 // Additional mov instruction generated.
362 return_sequence_length++;
363 }
364 masm_->BlockConstPoolFor(return_sequence_length);
365
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000366 // Tear down the frame which will restore the caller's frame pointer and
367 // the link register.
368 frame_->Exit();
369
ager@chromium.org4af710e2009-09-15 12:20:11 +0000370 // Here we use masm_-> instead of the __ macro to avoid the code coverage
371 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000372 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000373 masm_->Jump(lr);
374
375 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000376 // expected by the debugger. The add instruction above is an addressing
377 // mode 1 instruction where there are restrictions on which immediate values
378 // can be encoded in the instruction and which immediate values requires
379 // use of an additional instruction for moving the immediate to a temporary
380 // register.
381 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000382 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000383 }
384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 ASSERT(!has_cc());
387 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000388 ASSERT(!function_return_is_shadowed_);
389 function_return_.Unuse();
390 DeleteFrame();
391
392 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000393 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394 ProcessDeferred();
395 }
396
397 allocator_ = NULL;
398 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399}
400
401
ager@chromium.org7c537e22008-10-16 08:43:32 +0000402MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
403 // Currently, this assertion will fail if we try to assign to
404 // a constant variable that is constant because it is read-only
405 // (such as the variable referring to a named function expression).
406 // We need to implement assignments to read-only variables.
407 // Ideally, we should do this during AST generation (by converting
408 // such assignments into expression statements); however, in general
409 // we may not be able to make the decision until past AST generation,
410 // that is when the entire program is known.
411 ASSERT(slot != NULL);
412 int index = slot->index();
413 switch (slot->type()) {
414 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000416
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000417 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000418 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000419
420 case Slot::CONTEXT: {
421 // Follow the context chain if necessary.
422 ASSERT(!tmp.is(cp)); // do not overwrite context register
423 Register context = cp;
424 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000425 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000426 // Load the closure.
427 // (All contexts, even 'with' contexts, have a closure,
428 // and it is the same for all contexts inside a function.
429 // There is no need to go to the function context first.)
430 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
431 // Load the function context (which is the incoming, outer context).
432 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
433 context = tmp;
434 }
435 // We may have a 'with' context now. Get the function context.
436 // (In fact this mov may never be the needed, since the scope analysis
437 // may not permit a direct context access in this case and thus we are
438 // always at a function context. However it is safe to dereference be-
439 // cause the function context of a function context is itself. Before
440 // deleting this mov we should try to create a counter-example first,
441 // though...)
442 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
443 return ContextOperand(tmp, index);
444 }
445
446 default:
447 UNREACHABLE();
448 return MemOperand(r0, 0);
449 }
450}
451
452
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000453MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
454 Slot* slot,
455 Register tmp,
456 Register tmp2,
457 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000458 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000460
ager@chromium.org381abbb2009-02-25 13:23:22 +0000461 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
462 if (s->num_heap_slots() > 0) {
463 if (s->calls_eval()) {
464 // Check that extension is NULL.
465 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
466 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000467 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000468 }
469 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
470 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
471 context = tmp;
472 }
473 }
474 // Check that last extension is NULL.
475 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
476 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000477 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000478 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000479 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000480}
481
482
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000483// Loads a value on TOS. If it is a boolean value, the result may have been
484// (partially) translated into branches, or it may have set the condition
485// code register. If force_cc is set, the value is forced to set the
486// condition code register and no value is pushed. If the condition code
487// register was set, has_cc() is true and cc_reg_ contains the condition to
488// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000489void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 JumpTarget* true_target,
491 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000492 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000493 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000496 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000497 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498
499 // If we hit a stack overflow, we may not have actually visited
500 // the expression. In that case, we ensure that we have a
501 // valid-looking frame state because we will continue to generate
502 // code as we unwind the C++ stack.
503 //
504 // It's possible to have both a stack overflow and a valid frame
505 // state (eg, a subexpression overflowed, visiting it returned
506 // with a dummied frame state, and visiting this expression
507 // returned with a normal-looking state).
508 if (HasStackOverflow() &&
509 has_valid_frame() &&
510 !has_cc() &&
511 frame_->height() == original_height) {
512 true_target->Jump();
513 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000514 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000516 // Convert the TOS value to a boolean in the condition code register.
517 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 ASSERT(!force_cc || !has_valid_frame() || has_cc());
520 ASSERT(!has_valid_frame() ||
521 (has_cc() && frame_->height() == original_height) ||
522 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523}
524
525
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000526void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527#ifdef DEBUG
528 int original_height = frame_->height();
529#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000530 JumpTarget true_target;
531 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000532 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533
534 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000536 JumpTarget loaded;
537 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000538 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000539 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000540 frame_->EmitPush(r0);
541 loaded.Jump();
542 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000543 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 frame_->EmitPush(r0);
545 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 cc_reg_ = al;
547 }
548
549 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 // We have at least one condition value that has been "translated"
551 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000552 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 if (frame_ != NULL) {
554 loaded.Jump(); // Don't lose the current TOS.
555 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000557 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000559 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000560 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 // If both "true" and "false" need to be loaded jump across the code for
564 // "false".
565 if (both) {
566 loaded.Jump();
567 }
568 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000571 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000572 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000574 // A value is loaded on all paths reaching this point.
575 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000577 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000579 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580}
581
582
ager@chromium.org7c537e22008-10-16 08:43:32 +0000583void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000584 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000585 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000586 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587}
588
589
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000590void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000591 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000592 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
593 __ ldr(scratch,
594 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000595 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000596}
597
598
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000599void CodeGenerator::LoadTypeofExpression(Expression* expr) {
600 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000601 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000602 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000604 // For a global variable we build the property reference
605 // <global>.<variable> and perform a (regular non-contextual) property
606 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
608 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000609 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000610 Reference ref(this, &property);
611 ref.GetValueAndSpill();
612 } else if (variable != NULL && variable->slot() != NULL) {
613 // For a variable that rewrites to a slot, we signal it is the immediate
614 // subexpression of a typeof.
615 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
616 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000618 // Anything else can be handled normally.
619 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620 }
621}
622
623
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000624Reference::Reference(CodeGenerator* cgen,
625 Expression* expression,
626 bool persist_after_get)
627 : cgen_(cgen),
628 expression_(expression),
629 type_(ILLEGAL),
630 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 cgen->LoadReference(this);
632}
633
634
635Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000636 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637}
638
639
ager@chromium.org7c537e22008-10-16 08:43:32 +0000640void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000641 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000642 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 Expression* e = ref->expression();
644 Property* property = e->AsProperty();
645 Variable* var = e->AsVariableProxy()->AsVariable();
646
647 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000648 // The expression is either a property or a variable proxy that rewrites
649 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000650 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000651 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 ref->set_type(Reference::NAMED);
653 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000654 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 ref->set_type(Reference::KEYED);
656 }
657 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000658 // The expression is a variable proxy that does not rewrite to a
659 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 LoadGlobal();
662 ref->set_type(Reference::NAMED);
663 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000664 ASSERT(var->slot() != NULL);
665 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 }
667 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000668 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669 LoadAndSpill(e);
670 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 }
672}
673
674
ager@chromium.org7c537e22008-10-16 08:43:32 +0000675void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000676 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000677 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000678 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000680 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000681 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000682 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000683 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000685 ref->set_unloaded();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686}
687
688
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
690// register to a boolean in the condition code register. The code
691// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000692void CodeGenerator::ToBoolean(JumpTarget* true_target,
693 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000694 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000695 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000697 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
699 // Fast case checks
700
mads.s.ager31e71382008-08-13 09:32:07 +0000701 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000702 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
703 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000704 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
mads.s.ager31e71382008-08-13 09:32:07 +0000706 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000707 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
708 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000709 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
mads.s.ager31e71382008-08-13 09:32:07 +0000711 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000712 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
713 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000714 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
mads.s.ager31e71382008-08-13 09:32:07 +0000716 // Check if the value is a smi.
717 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000718 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000719 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000720 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721
722 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000723 frame_->EmitPush(r0);
724 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000725 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000726 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
727 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728
729 cc_reg_ = ne;
730}
731
732
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000733void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000734 OverwriteMode overwrite_mode,
735 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000736 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000737 // sp[0] : y
738 // sp[1] : x
739 // result : r0
740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 // Stub is entered with a call: 'return address' is in lr.
742 switch (op) {
743 case Token::ADD: // fall through.
744 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000745 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000746 case Token::DIV:
747 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000748 case Token::BIT_OR:
749 case Token::BIT_AND:
750 case Token::BIT_XOR:
751 case Token::SHL:
752 case Token::SHR:
753 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000754 frame_->EmitPop(r0); // r0 : y
755 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000756 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000757 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 break;
759 }
760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000762 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000764 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 break;
766
767 default:
768 // Other cases should have been handled before this point.
769 UNREACHABLE();
770 break;
771 }
772}
773
774
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000775class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000776 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000777 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000778 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000779 bool reversed,
780 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000781 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000782 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000783 reversed_(reversed),
784 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000785 set_comment("[ DeferredInlinedSmiOperation");
786 }
787
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000788 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000789
790 private:
791 Token::Value op_;
792 int value_;
793 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000794 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000795};
796
797
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000798void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000799 switch (op_) {
800 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000801 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000802 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000803 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
804 __ mov(r1, Operand(Smi::FromInt(value_)));
805 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000806 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
807 __ mov(r0, Operand(Smi::FromInt(value_)));
808 }
809 break;
810 }
811
812 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000813 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000814 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000815 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
816 __ mov(r1, Operand(Smi::FromInt(value_)));
817 } else {
818 __ add(r1, r0, Operand(Smi::FromInt(value_)));
819 __ mov(r0, Operand(Smi::FromInt(value_)));
820 }
821 break;
822 }
823
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000824 // For these operations there is no optimistic operation that needs to be
825 // reverted.
826 case Token::MUL:
827 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000828 case Token::BIT_OR:
829 case Token::BIT_XOR:
830 case Token::BIT_AND: {
831 if (reversed_) {
832 __ mov(r1, Operand(Smi::FromInt(value_)));
833 } else {
834 __ mov(r1, Operand(r0));
835 __ mov(r0, Operand(Smi::FromInt(value_)));
836 }
837 break;
838 }
839
840 case Token::SHL:
841 case Token::SHR:
842 case Token::SAR: {
843 if (!reversed_) {
844 __ mov(r1, Operand(r0));
845 __ mov(r0, Operand(Smi::FromInt(value_)));
846 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000847 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000848 }
849 break;
850 }
851
852 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000853 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000854 UNREACHABLE();
855 break;
856 }
857
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000858 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000859 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000860}
861
862
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000863static bool PopCountLessThanEqual2(unsigned int x) {
864 x &= x - 1;
865 return (x & (x - 1)) == 0;
866}
867
868
869// Returns the index of the lowest bit set.
870static int BitPosition(unsigned x) {
871 int bit_posn = 0;
872 while ((x & 0xf) == 0) {
873 bit_posn += 4;
874 x >>= 4;
875 }
876 while ((x & 1) == 0) {
877 bit_posn++;
878 x >>= 1;
879 }
880 return bit_posn;
881}
882
883
ager@chromium.org7c537e22008-10-16 08:43:32 +0000884void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000885 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000886 bool reversed,
887 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000888 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 // NOTE: This is an attempt to inline (a bit) more of the code for
890 // some possible smi operations (like + and -) when (at least) one
891 // of the operands is a literal smi. With this optimization, the
892 // performance of the system is increased by ~15%, and the generated
893 // code size is increased by ~1% (measured on a combination of
894 // different benchmarks).
895
mads.s.ager31e71382008-08-13 09:32:07 +0000896 // sp[0] : operand
897
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000898 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000900 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000901 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000903 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 switch (op) {
905 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000907 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000909 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000910 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000912 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000913 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 break;
915 }
916
917 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000918 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000919 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920
ager@chromium.orge2902be2009-06-08 12:21:35 +0000921 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000922 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000923 } else {
924 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000926 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000927 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000928 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000929 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 break;
931 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000933
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000934 case Token::BIT_OR:
935 case Token::BIT_XOR:
936 case Token::BIT_AND: {
937 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000938 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000940 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000941 switch (op) {
942 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
943 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
944 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
945 default: UNREACHABLE();
946 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000947 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000948 break;
949 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000951 case Token::SHL:
952 case Token::SHR:
953 case Token::SAR: {
954 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000955 something_to_inline = false;
956 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000957 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000958 int shift_value = int_value & 0x1f; // least significant 5 bits
959 DeferredCode* deferred =
960 new DeferredInlineSmiOperation(op, shift_value, false, mode);
961 __ tst(r0, Operand(kSmiTagMask));
962 deferred->Branch(ne);
963 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
964 switch (op) {
965 case Token::SHL: {
966 if (shift_value != 0) {
967 __ mov(r2, Operand(r2, LSL, shift_value));
968 }
969 // check that the *unsigned* result fits in a smi
970 __ add(r3, r2, Operand(0x40000000), SetCC);
971 deferred->Branch(mi);
972 break;
973 }
974 case Token::SHR: {
975 // LSR by immediate 0 means shifting 32 bits.
976 if (shift_value != 0) {
977 __ mov(r2, Operand(r2, LSR, shift_value));
978 }
979 // check that the *unsigned* result fits in a smi
980 // neither of the two high-order bits can be set:
981 // - 0x80000000: high bit would be lost when smi tagging
982 // - 0x40000000: this number would convert to negative when
983 // smi tagging these two cases can only happen with shifts
984 // by 0 or 1 when handed a valid smi
985 __ and_(r3, r2, Operand(0xc0000000), SetCC);
986 deferred->Branch(ne);
987 break;
988 }
989 case Token::SAR: {
990 if (shift_value != 0) {
991 // ASR by immediate 0 means shifting 32 bits.
992 __ mov(r2, Operand(r2, ASR, shift_value));
993 }
994 break;
995 }
996 default: UNREACHABLE();
997 }
998 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
999 deferred->BindExit();
1000 break;
1001 }
1002
1003 case Token::MOD: {
1004 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1005 something_to_inline = false;
1006 break;
1007 }
1008 DeferredCode* deferred =
1009 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1010 unsigned mask = (0x80000000u | kSmiTagMask);
1011 __ tst(r0, Operand(mask));
1012 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1013 mask = (int_value << kSmiTagSize) - 1;
1014 __ and_(r0, r0, Operand(mask));
1015 deferred->BindExit();
1016 break;
1017 }
1018
1019 case Token::MUL: {
1020 if (!IsEasyToMultiplyBy(int_value)) {
1021 something_to_inline = false;
1022 break;
1023 }
1024 DeferredCode* deferred =
1025 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1026 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1027 max_smi_that_wont_overflow <<= kSmiTagSize;
1028 unsigned mask = 0x80000000u;
1029 while ((mask & max_smi_that_wont_overflow) == 0) {
1030 mask |= mask >> 1;
1031 }
1032 mask |= kSmiTagMask;
1033 // This does a single mask that checks for a too high value in a
1034 // conservative way and for a non-Smi. It also filters out negative
1035 // numbers, unfortunately, but since this code is inline we prefer
1036 // brevity to comprehensiveness.
1037 __ tst(r0, Operand(mask));
1038 deferred->Branch(ne);
1039 MultiplyByKnownInt(masm_, r0, r0, int_value);
1040 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041 break;
1042 }
1043
1044 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001045 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 break;
1047 }
1048
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001049 if (!something_to_inline) {
1050 if (!reversed) {
1051 frame_->EmitPush(r0);
1052 __ mov(r0, Operand(value));
1053 frame_->EmitPush(r0);
1054 GenericBinaryOperation(op, mode, int_value);
1055 } else {
1056 __ mov(ip, Operand(value));
1057 frame_->EmitPush(ip);
1058 frame_->EmitPush(r0);
1059 GenericBinaryOperation(op, mode, kUnknownIntValue);
1060 }
1061 }
1062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001063 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064}
1065
1066
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001067void CodeGenerator::Comparison(Condition cc,
1068 Expression* left,
1069 Expression* right,
1070 bool strict) {
1071 if (left != NULL) LoadAndSpill(left);
1072 if (right != NULL) LoadAndSpill(right);
1073
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001074 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001075 // sp[0] : y
1076 // sp[1] : x
1077 // result : cc register
1078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 // Strict only makes sense for equality comparisons.
1080 ASSERT(!strict || cc == eq);
1081
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001082 JumpTarget exit;
1083 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001084 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1085 if (cc == gt || cc == le) {
1086 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001087 frame_->EmitPop(r1);
1088 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001089 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001090 frame_->EmitPop(r0);
1091 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001092 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 __ orr(r2, r0, Operand(r1));
1094 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001095 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001097 // Perform non-smi comparison by stub.
1098 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1099 // We call with 0 args because there are 0 on the stack.
1100 CompareStub stub(cc, strict);
1101 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001102 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001103 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001105 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001106 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 __ cmp(r1, Operand(r0));
1108
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001109 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 cc_reg_ = cc;
1111}
1112
1113
mads.s.ager31e71382008-08-13 09:32:07 +00001114// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001115void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001116 CallFunctionFlags flags,
1117 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001118 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001120 int arg_count = args->length();
1121 for (int i = 0; i < arg_count; i++) {
1122 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001123 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124
kasper.lund7276f142008-07-30 08:49:36 +00001125 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001126 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127
kasper.lund7276f142008-07-30 08:49:36 +00001128 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001129 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001130 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001131 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132
1133 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001134 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001135 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136}
1137
1138
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001139void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001140 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 ASSERT(has_cc());
1142 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001143 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 cc_reg_ = al;
1145}
1146
1147
ager@chromium.org7c537e22008-10-16 08:43:32 +00001148void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001149 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001150 Comment cmnt(masm_, "[ check stack");
1151 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1152 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1153 // the implicit 8 byte offset that always applies to operations with pc and
1154 // gives a return address 12 bytes down.
1155 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1156 masm_->cmp(sp, Operand(ip));
1157 StackCheckStub stub;
1158 // Call the stub if lower.
1159 masm_->mov(pc,
1160 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1161 RelocInfo::CODE_TARGET),
1162 LeaveCC,
1163 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164}
1165
1166
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001167void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1168#ifdef DEBUG
1169 int original_height = frame_->height();
1170#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001171 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001172 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1173 VisitAndSpill(statements->at(i));
1174 }
1175 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1176}
1177
1178
ager@chromium.org7c537e22008-10-16 08:43:32 +00001179void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001180#ifdef DEBUG
1181 int original_height = frame_->height();
1182#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001183 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001185 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001186 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001187 VisitStatementsAndSpill(node->statements());
1188 if (node->break_target()->is_linked()) {
1189 node->break_target()->Bind();
1190 }
1191 node->break_target()->Unuse();
1192 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193}
1194
1195
ager@chromium.org7c537e22008-10-16 08:43:32 +00001196void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001197 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001198 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001199 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001201 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202 frame_->EmitPush(r0);
1203 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001204 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205}
1206
1207
ager@chromium.org7c537e22008-10-16 08:43:32 +00001208void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001209#ifdef DEBUG
1210 int original_height = frame_->height();
1211#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001212 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 Comment cmnt(masm_, "[ Declaration");
1214 Variable* var = node->proxy()->var();
1215 ASSERT(var != NULL); // must have been resolved
1216 Slot* slot = var->slot();
1217
1218 // If it was not possible to allocate the variable at compile time,
1219 // we need to "declare" it at runtime to make sure it actually
1220 // exists in the local context.
1221 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1222 // Variables with a "LOOKUP" slot were introduced as non-locals
1223 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001224 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001227 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001228 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 // Declaration nodes are always declared in only two modes.
1230 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1231 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001232 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 // Push initial value, if any.
1235 // Note: For variables we must not push an initial value (such as
1236 // 'undefined') because we may have a (legal) redeclaration and we
1237 // must not destroy the current value.
1238 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001239 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001240 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001242 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001244 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001245 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001247 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001248 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001249 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 return;
1251 }
1252
1253 ASSERT(!var->is_global());
1254
1255 // If we have a function or a constant, we need to initialize the variable.
1256 Expression* val = NULL;
1257 if (node->mode() == Variable::CONST) {
1258 val = new Literal(Factory::the_hole_value());
1259 } else {
1260 val = node->fun(); // NULL if we don't have a function
1261 }
1262
1263 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001264 {
1265 // Set initial value.
1266 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001268 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001269 }
1270 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001271 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001273 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274}
1275
1276
ager@chromium.org7c537e22008-10-16 08:43:32 +00001277void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001278#ifdef DEBUG
1279 int original_height = frame_->height();
1280#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001281 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001283 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 Expression* expression = node->expression();
1285 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001286 LoadAndSpill(expression);
1287 frame_->Drop();
1288 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289}
1290
1291
ager@chromium.org7c537e22008-10-16 08:43:32 +00001292void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001293#ifdef DEBUG
1294 int original_height = frame_->height();
1295#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001296 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001298 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001300 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301}
1302
1303
ager@chromium.org7c537e22008-10-16 08:43:32 +00001304void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001305#ifdef DEBUG
1306 int original_height = frame_->height();
1307#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001308 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001310 // Generate different code depending on which parts of the if statement
1311 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 bool has_then_stm = node->HasThenStatement();
1313 bool has_else_stm = node->HasElseStatement();
1314
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001317 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001319 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001320 JumpTarget then;
1321 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001323 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001324 if (frame_ != NULL) {
1325 Branch(false, &else_);
1326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001328 if (frame_ != NULL || then.is_linked()) {
1329 then.Bind();
1330 VisitAndSpill(node->then_statement());
1331 }
1332 if (frame_ != NULL) {
1333 exit.Jump();
1334 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001336 if (else_.is_linked()) {
1337 else_.Bind();
1338 VisitAndSpill(node->else_statement());
1339 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340
1341 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001342 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001344 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001346 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001347 if (frame_ != NULL) {
1348 Branch(false, &exit);
1349 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001351 if (frame_ != NULL || then.is_linked()) {
1352 then.Bind();
1353 VisitAndSpill(node->then_statement());
1354 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355
1356 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001357 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001359 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001361 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 if (frame_ != NULL) {
1363 Branch(true, &exit);
1364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001366 if (frame_ != NULL || else_.is_linked()) {
1367 else_.Bind();
1368 VisitAndSpill(node->else_statement());
1369 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370
1371 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001372 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 ASSERT(!has_then_stm && !has_else_stm);
1374 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001375 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001376 if (frame_ != NULL) {
1377 if (has_cc()) {
1378 cc_reg_ = al;
1379 } else {
1380 frame_->Drop();
1381 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 }
1383 }
1384
1385 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001386 if (exit.is_linked()) {
1387 exit.Bind();
1388 }
1389 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390}
1391
1392
ager@chromium.org7c537e22008-10-16 08:43:32 +00001393void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001394 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001396 CodeForStatementPosition(node);
1397 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398}
1399
1400
ager@chromium.org7c537e22008-10-16 08:43:32 +00001401void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001402 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001404 CodeForStatementPosition(node);
1405 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406}
1407
1408
ager@chromium.org7c537e22008-10-16 08:43:32 +00001409void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001410 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001412
ager@chromium.org4af710e2009-09-15 12:20:11 +00001413 CodeForStatementPosition(node);
1414 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001415 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001416 frame_->EmitPop(r0);
1417 function_return_.Jump();
1418 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 // Pop the result from the frame and prepare the frame for
1420 // returning thus making it easier to merge.
1421 frame_->EmitPop(r0);
1422 frame_->PrepareForReturn();
1423
1424 function_return_.Jump();
1425 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426}
1427
1428
ager@chromium.org7c537e22008-10-16 08:43:32 +00001429void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001430#ifdef DEBUG
1431 int original_height = frame_->height();
1432#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001433 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 CodeForStatementPosition(node);
1436 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001437 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001438 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001439 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001440 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001441 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001442#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001443 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001444 __ cmp(r0, Operand(cp));
1445 verified_true.Branch(eq);
1446 __ stop("PushContext: r0 is expected to be the same as cp");
1447 verified_true.Bind();
1448#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001450 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001451 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452}
1453
1454
ager@chromium.org7c537e22008-10-16 08:43:32 +00001455void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001456#ifdef DEBUG
1457 int original_height = frame_->height();
1458#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001459 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001461 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 // Pop context.
1463 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1464 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001465 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001466 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467}
1468
1469
ager@chromium.org7c537e22008-10-16 08:43:32 +00001470void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001471#ifdef DEBUG
1472 int original_height = frame_->height();
1473#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001474 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001476 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001477 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001479 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001480
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001481 JumpTarget next_test;
1482 JumpTarget fall_through;
1483 JumpTarget default_entry;
1484 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 ZoneList<CaseClause*>* cases = node->cases();
1486 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001487 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488
1489 for (int i = 0; i < length; i++) {
1490 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001492 // Remember the default clause and compile it at the end.
1493 default_clause = clause;
1494 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 }
1496
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497 Comment cmnt(masm_, "[ Case clause");
1498 // Compile the test.
1499 next_test.Bind();
1500 next_test.Unuse();
1501 // Duplicate TOS.
1502 __ ldr(r0, frame_->Top());
1503 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001504 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001506
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001507 // Before entering the body from the test, remove the switch value from
1508 // the stack.
1509 frame_->Drop();
1510
1511 // Label the body so that fall through is enabled.
1512 if (i > 0 && cases->at(i - 1)->is_default()) {
1513 default_exit.Bind();
1514 } else {
1515 fall_through.Bind();
1516 fall_through.Unuse();
1517 }
1518 VisitStatementsAndSpill(clause->statements());
1519
1520 // If control flow can fall through from the body, jump to the next body
1521 // or the end of the statement.
1522 if (frame_ != NULL) {
1523 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1524 default_entry.Jump();
1525 } else {
1526 fall_through.Jump();
1527 }
1528 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 }
1530
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001531 // The final "test" removes the switch value.
1532 next_test.Bind();
1533 frame_->Drop();
1534
1535 // If there is a default clause, compile it.
1536 if (default_clause != NULL) {
1537 Comment cmnt(masm_, "[ Default clause");
1538 default_entry.Bind();
1539 VisitStatementsAndSpill(default_clause->statements());
1540 // If control flow can fall out of the default and there is a case after
1541 // it, jup to that case's body.
1542 if (frame_ != NULL && default_exit.is_bound()) {
1543 default_exit.Jump();
1544 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001545 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001547 if (fall_through.is_linked()) {
1548 fall_through.Bind();
1549 }
1550
1551 if (node->break_target()->is_linked()) {
1552 node->break_target()->Bind();
1553 }
1554 node->break_target()->Unuse();
1555 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556}
1557
1558
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001559void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001560#ifdef DEBUG
1561 int original_height = frame_->height();
1562#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001563 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001564 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001565 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001566 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001567 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001569 // Label the top of the loop for the backward CFG edge. If the test
1570 // is always true we can use the continue target, and if the test is
1571 // always false there is no need.
1572 ConditionAnalysis info = AnalyzeCondition(node->cond());
1573 switch (info) {
1574 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001575 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001577 break;
1578 case ALWAYS_FALSE:
1579 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1580 break;
1581 case DONT_KNOW:
1582 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1583 body.Bind();
1584 break;
1585 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001587 CheckStack(); // TODO(1222600): ignore if body contains calls.
1588 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001589
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001590 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001591 switch (info) {
1592 case ALWAYS_TRUE:
1593 // If control can fall off the end of the body, jump back to the
1594 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001596 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001599 case ALWAYS_FALSE:
1600 // If we have a continue in the body, we only have to bind its
1601 // jump target.
1602 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001603 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001604 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001605 break;
1606 case DONT_KNOW:
1607 // We have to compile the test expression if it can be reached by
1608 // control flow falling out of the body or via continue.
1609 if (node->continue_target()->is_linked()) {
1610 node->continue_target()->Bind();
1611 }
1612 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001613 Comment cmnt(masm_, "[ DoWhileCondition");
1614 CodeForDoWhileConditionPosition(node);
1615 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001617 // A invalid frame here indicates that control did not
1618 // fall out of the test expression.
1619 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001620 }
1621 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 break;
1623 }
1624
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001625 if (node->break_target()->is_linked()) {
1626 node->break_target()->Bind();
1627 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001628 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1629}
1630
1631
1632void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1633#ifdef DEBUG
1634 int original_height = frame_->height();
1635#endif
1636 VirtualFrame::SpilledScope spilled_scope;
1637 Comment cmnt(masm_, "[ WhileStatement");
1638 CodeForStatementPosition(node);
1639
1640 // If the test is never true and has no side effects there is no need
1641 // to compile the test or body.
1642 ConditionAnalysis info = AnalyzeCondition(node->cond());
1643 if (info == ALWAYS_FALSE) return;
1644
1645 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1646
1647 // Label the top of the loop with the continue target for the backward
1648 // CFG edge.
1649 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1650 node->continue_target()->Bind();
1651
1652 if (info == DONT_KNOW) {
1653 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001654 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001655 if (has_valid_frame()) {
1656 // A NULL frame indicates that control did not fall out of the
1657 // test expression.
1658 Branch(false, node->break_target());
1659 }
1660 if (has_valid_frame() || body.is_linked()) {
1661 body.Bind();
1662 }
1663 }
1664
1665 if (has_valid_frame()) {
1666 CheckStack(); // TODO(1222600): ignore if body contains calls.
1667 VisitAndSpill(node->body());
1668
1669 // If control flow can fall out of the body, jump back to the top.
1670 if (has_valid_frame()) {
1671 node->continue_target()->Jump();
1672 }
1673 }
1674 if (node->break_target()->is_linked()) {
1675 node->break_target()->Bind();
1676 }
1677 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1678}
1679
1680
1681void CodeGenerator::VisitForStatement(ForStatement* node) {
1682#ifdef DEBUG
1683 int original_height = frame_->height();
1684#endif
1685 VirtualFrame::SpilledScope spilled_scope;
1686 Comment cmnt(masm_, "[ ForStatement");
1687 CodeForStatementPosition(node);
1688 if (node->init() != NULL) {
1689 VisitAndSpill(node->init());
1690 }
1691
1692 // If the test is never true there is no need to compile the test or
1693 // body.
1694 ConditionAnalysis info = AnalyzeCondition(node->cond());
1695 if (info == ALWAYS_FALSE) return;
1696
1697 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1698
1699 // If there is no update statement, label the top of the loop with the
1700 // continue target, otherwise with the loop target.
1701 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1702 if (node->next() == NULL) {
1703 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1704 node->continue_target()->Bind();
1705 } else {
1706 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1707 loop.Bind();
1708 }
1709
1710 // If the test is always true, there is no need to compile it.
1711 if (info == DONT_KNOW) {
1712 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001713 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001714 if (has_valid_frame()) {
1715 Branch(false, node->break_target());
1716 }
1717 if (has_valid_frame() || body.is_linked()) {
1718 body.Bind();
1719 }
1720 }
1721
1722 if (has_valid_frame()) {
1723 CheckStack(); // TODO(1222600): ignore if body contains calls.
1724 VisitAndSpill(node->body());
1725
1726 if (node->next() == NULL) {
1727 // If there is no update statement and control flow can fall out
1728 // of the loop, jump directly to the continue label.
1729 if (has_valid_frame()) {
1730 node->continue_target()->Jump();
1731 }
1732 } else {
1733 // If there is an update statement and control flow can reach it
1734 // via falling out of the body of the loop or continuing, we
1735 // compile the update statement.
1736 if (node->continue_target()->is_linked()) {
1737 node->continue_target()->Bind();
1738 }
1739 if (has_valid_frame()) {
1740 // Record source position of the statement as this code which is
1741 // after the code for the body actually belongs to the loop
1742 // statement and not the body.
1743 CodeForStatementPosition(node);
1744 VisitAndSpill(node->next());
1745 loop.Jump();
1746 }
1747 }
1748 }
1749 if (node->break_target()->is_linked()) {
1750 node->break_target()->Bind();
1751 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753}
1754
1755
ager@chromium.org7c537e22008-10-16 08:43:32 +00001756void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001757#ifdef DEBUG
1758 int original_height = frame_->height();
1759#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001760 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001762 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001764 JumpTarget primitive;
1765 JumpTarget jsobject;
1766 JumpTarget fixed_array;
1767 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1768 JumpTarget end_del_check;
1769 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770
1771 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001772 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773
1774 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1775 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001776 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001777 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1778 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001780 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1781 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783
1784 // Stack layout in body:
1785 // [iteration counter (Smi)]
1786 // [length of array]
1787 // [FixedArray]
1788 // [Map or 0]
1789 // [Object]
1790
1791 // Check if enumerable is already a JSObject
1792 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001794 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001795 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 primitive.Bind();
1798 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001799 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001803 // r0: value to be iterated over
1804 frame_->EmitPush(r0); // Push the object being iterated over.
1805
1806 // Check cache validity in generated code. This is a fast case for
1807 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1808 // guarantee cache validity, call the runtime system to check cache
1809 // validity or get the property names in a fixed array.
1810 JumpTarget call_runtime;
1811 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1812 JumpTarget check_prototype;
1813 JumpTarget use_cache;
1814 __ mov(r1, Operand(r0));
1815 loop.Bind();
1816 // Check that there are no elements.
1817 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1818 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1819 __ cmp(r2, r4);
1820 call_runtime.Branch(ne);
1821 // Check that instance descriptors are not empty so that we can
1822 // check for an enum cache. Leave the map in r3 for the subsequent
1823 // prototype load.
1824 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1825 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1826 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1827 __ cmp(r2, ip);
1828 call_runtime.Branch(eq);
1829 // Check that there in an enum cache in the non-empty instance
1830 // descriptors. This is the case if the next enumeration index
1831 // field does not contain a smi.
1832 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1833 __ tst(r2, Operand(kSmiTagMask));
1834 call_runtime.Branch(eq);
1835 // For all objects but the receiver, check that the cache is empty.
1836 // r4: empty fixed array root.
1837 __ cmp(r1, r0);
1838 check_prototype.Branch(eq);
1839 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1840 __ cmp(r2, r4);
1841 call_runtime.Branch(ne);
1842 check_prototype.Bind();
1843 // Load the prototype from the map and loop if non-null.
1844 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1845 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1846 __ cmp(r1, ip);
1847 loop.Branch(ne);
1848 // The enum cache is valid. Load the map of the object being
1849 // iterated over and use the cache for the iteration.
1850 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1851 use_cache.Jump();
1852
1853 call_runtime.Bind();
1854 // Call the runtime to get the property names for the object.
1855 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001858 // If we got a map from the runtime call, we can do a fast
1859 // modification check. Otherwise, we got a fixed array, and we have
1860 // to do a slow check.
1861 // r0: map or fixed array (result from call to
1862 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 __ mov(r2, Operand(r0));
1864 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001865 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1866 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001869 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001871 // r0: map (either the result from a call to
1872 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1873 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 __ mov(r1, Operand(r0));
1875 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1876 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1877 __ ldr(r2,
1878 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1879
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 frame_->EmitPush(r0); // map
1881 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001882 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001884 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001885 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886 frame_->EmitPush(r0);
1887 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001889 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001891 frame_->EmitPush(r1); // insert 0 in place of Map
1892 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893
1894 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001895 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001897 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001898 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001899 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
1901 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001903 // sp[0] : index
1904 // sp[1] : array/enum cache length
1905 // sp[2] : array or enum cache
1906 // sp[3] : 0 or map
1907 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 // Grab the current frame's height for the break and continue
1909 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001910 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1911 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001913 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1914 __ ldr(r1, frame_->ElementAt(1)); // load the length
1915 __ cmp(r0, Operand(r1)); // compare to the array length
1916 node->break_target()->Branch(hs);
1917
1918 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001921 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1923 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1924
1925 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 // Check if this (still) matches the map of the enumerable.
1928 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1931 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001932 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933
1934 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1936 frame_->EmitPush(r0);
1937 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001938 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001939 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940
1941 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001942 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1943 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001946 end_del_check.Bind();
1947 // Store the entry in the 'each' expression and take another spin in the
1948 // loop. r3: i'th entry of the enum cache (or string there of)
1949 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 { Reference each(this, node->each());
1951 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001952 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 __ ldr(r0, frame_->ElementAt(each.size()));
1954 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001955 each.SetValue(NOT_CONST_INIT);
1956 frame_->Drop(2);
1957 } else {
1958 // If the reference was to a slot we rely on the convenient property
1959 // that it doesn't matter whether a value (eg, r3 pushed above) is
1960 // right on top of or right underneath a zero-sized reference.
1961 each.SetValue(NOT_CONST_INIT);
1962 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00001963 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 }
1965 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 VisitAndSpill(node->body());
1969
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001970 // Next. Reestablish a spilled frame in case we are coming here via
1971 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001972 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001973 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001974 frame_->EmitPop(r0);
1975 __ add(r0, r0, Operand(Smi::FromInt(1)));
1976 frame_->EmitPush(r0);
1977 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001979 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1980 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001982 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983
1984 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 exit.Bind();
1986 node->continue_target()->Unuse();
1987 node->break_target()->Unuse();
1988 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989}
1990
1991
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001992void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001993#ifdef DEBUG
1994 int original_height = frame_->height();
1995#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001996 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001997 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002000 JumpTarget try_block;
2001 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002003 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006
2007 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002008 Variable* catch_var = node->catch_var()->var();
2009 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2010 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011
2012 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 VisitStatementsAndSpill(node->catch_block()->statements());
2016 if (frame_ != NULL) {
2017 exit.Jump();
2018 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
2020
2021 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2025 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002027 // Shadow the labels for all escapes from the try block, including
2028 // returns. During shadowing, the original label is hidden as the
2029 // LabelShadow and operations on the original actually affect the
2030 // shadowing label.
2031 //
2032 // We should probably try to unify the escaping labels and the return
2033 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 int nof_escapes = node->escaping_targets()->length();
2035 List<ShadowTarget*> shadows(1 + nof_escapes);
2036
2037 // Add the shadow target for the function return.
2038 static const int kReturnShadowIndex = 0;
2039 shadows.Add(new ShadowTarget(&function_return_));
2040 bool function_return_was_shadowed = function_return_is_shadowed_;
2041 function_return_is_shadowed_ = true;
2042 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2043
2044 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 }
2048
2049 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
2052 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002053 // After shadowing stops, the original labels are unshadowed and the
2054 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002055 bool has_unlinks = false;
2056 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002058 has_unlinks = has_unlinks || shadows[i]->is_linked();
2059 }
2060 function_return_is_shadowed_ = function_return_was_shadowed;
2061
2062 // Get an external reference to the handler address.
2063 ExternalReference handler_address(Top::k_handler_address);
2064
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065 // If we can fall off the end of the try block, unlink from try chain.
2066 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002067 // The next handler address is on top of the frame. Unlink from
2068 // the handler list and drop the rest of this handler from the
2069 // frame.
2070 ASSERT(StackHandlerConstants::kNextOffset == 0);
2071 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 __ mov(r3, Operand(handler_address));
2073 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002074 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 if (has_unlinks) {
2076 exit.Jump();
2077 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 }
2079
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002080 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081 // jumped to. Deallocate each shadow target.
2082 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002084 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 shadows[i]->Bind();
2086 // Because we can be jumping here (to spilled code) from unspilled
2087 // code, we need to reestablish a spilled frame at this block.
2088 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 // Reload sp from the top handler, because some statements that we
2091 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002094 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002096 ASSERT(StackHandlerConstants::kNextOffset == 0);
2097 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002099 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002101 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2102 frame_->PrepareForReturn();
2103 }
2104 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 }
2106 }
2107
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 exit.Bind();
2109 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110}
2111
2112
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002113void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002114#ifdef DEBUG
2115 int original_height = frame_->height();
2116#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002117 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002118 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120
2121 // State: Used to keep track of reason for entering the finally
2122 // block. Should probably be extended to hold information for
2123 // break/continue from within the try block.
2124 enum { FALLING, THROWING, JUMPING };
2125
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002126 JumpTarget try_block;
2127 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002129 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 // In case of thrown exceptions, this is where we continue.
2133 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002134 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135
2136 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2140 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002142 // Shadow the labels for all escapes from the try block, including
2143 // returns. Shadowing hides the original label as the LabelShadow and
2144 // operations on the original actually affect the shadowing label.
2145 //
2146 // We should probably try to unify the escaping labels and the return
2147 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148 int nof_escapes = node->escaping_targets()->length();
2149 List<ShadowTarget*> shadows(1 + nof_escapes);
2150
2151 // Add the shadow target for the function return.
2152 static const int kReturnShadowIndex = 0;
2153 shadows.Add(new ShadowTarget(&function_return_));
2154 bool function_return_was_shadowed = function_return_is_shadowed_;
2155 function_return_is_shadowed_ = true;
2156 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2157
2158 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 }
2162
2163 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002166 // Stop the introduced shadowing and count the number of required unlinks.
2167 // After shadowing stops, the original labels are unshadowed and the
2168 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 shadows[i]->StopShadowing();
2172 if (shadows[i]->is_linked()) nof_unlinks++;
2173 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 // Get an external reference to the handler address.
2177 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 // If we can fall off the end of the try block, unlink from the try
2180 // chain and set the state on the frame to FALLING.
2181 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002182 // The next handler address is on top of the frame.
2183 ASSERT(StackHandlerConstants::kNextOffset == 0);
2184 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 __ mov(r3, Operand(handler_address));
2186 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002187 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188
2189 // Fake a top of stack value (unneeded when FALLING) and set the
2190 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002191 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002192 frame_->EmitPush(r0);
2193 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2194 if (nof_unlinks > 0) {
2195 finally_block.Jump();
2196 }
2197 }
2198
2199 // Generate code to unlink and set the state for the (formerly)
2200 // shadowing targets that have been jumped to.
2201 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002203 // If we have come from the shadowed return, the return value is
2204 // in (a non-refcounted reference to) r0. We must preserve it
2205 // until it is pushed.
2206 //
2207 // Because we can be jumping here (to spilled code) from
2208 // unspilled code, we need to reestablish a spilled frame at
2209 // this block.
2210 shadows[i]->Bind();
2211 frame_->SpillAll();
2212
2213 // Reload sp from the top handler, because some statements that
2214 // we break from (eg, for...in) may have left stuff on the
2215 // stack.
2216 __ mov(r3, Operand(handler_address));
2217 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002218 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002219
2220 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002221 // handler address is currently on top of the frame.
2222 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 frame_->EmitPop(r1);
2224 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002225 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226
2227 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002228 // If this label shadowed the function return, materialize the
2229 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002230 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002231 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002232 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002233 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 }
2236 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002237 if (--nof_unlinks > 0) {
2238 // If this is not the last unlink block, jump around the next.
2239 finally_block.Jump();
2240 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 }
2242 }
2243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002247 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002249
2250 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002251 // and the state - while evaluating the finally block.
2252 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002254 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002256 if (has_valid_frame()) {
2257 // Restore state and return value or faked TOS.
2258 frame_->EmitPop(r2);
2259 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 }
2261
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002262 // Generate code to jump to the right destination for all used
2263 // formerly shadowing targets. Deallocate each shadow target.
2264 for (int i = 0; i < shadows.length(); i++) {
2265 if (has_valid_frame() && shadows[i]->is_bound()) {
2266 JumpTarget* original = shadows[i]->other_target();
2267 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2268 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002269 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 skip.Branch(ne);
2271 frame_->PrepareForReturn();
2272 original->Jump();
2273 skip.Bind();
2274 } else {
2275 original->Branch(eq);
2276 }
2277 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 if (has_valid_frame()) {
2281 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002282 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002283 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2284 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002286 // Rethrow exception.
2287 frame_->EmitPush(r0);
2288 frame_->CallRuntime(Runtime::kReThrow, 1);
2289
2290 // Done.
2291 exit.Bind();
2292 }
2293 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294}
2295
2296
ager@chromium.org7c537e22008-10-16 08:43:32 +00002297void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002298#ifdef DEBUG
2299 int original_height = frame_->height();
2300#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002301 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002303 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002304#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002305 DebuggerStatementStub ces;
2306 frame_->CallStub(&ces, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002307#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002308 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002309 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310}
2311
2312
ager@chromium.org7c537e22008-10-16 08:43:32 +00002313void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002314 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 ASSERT(boilerplate->IsBoilerplate());
2316
ager@chromium.org3811b432009-10-28 14:53:37 +00002317 __ mov(r0, Operand(boilerplate));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002318 // Use the fast case closure allocation code that allocates in new
2319 // space for nested functions that don't need literals cloning.
2320 if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) {
2321 FastNewClosureStub stub;
2322 frame_->EmitPush(r0);
2323 frame_->CallStub(&stub, 1);
2324 frame_->EmitPush(r0);
2325 } else {
2326 // Create a new closure.
2327 frame_->EmitPush(cp);
2328 frame_->EmitPush(r0);
2329 frame_->CallRuntime(Runtime::kNewClosure, 2);
2330 frame_->EmitPush(r0);
2331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332}
2333
2334
ager@chromium.org7c537e22008-10-16 08:43:32 +00002335void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002336#ifdef DEBUG
2337 int original_height = frame_->height();
2338#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002339 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 Comment cmnt(masm_, "[ FunctionLiteral");
2341
2342 // Build the function boilerplate and instantiate it.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002343 Handle<JSFunction> boilerplate =
2344 Compiler::BuildBoilerplate(node, script_, this);
kasper.lund212ac232008-07-16 07:07:30 +00002345 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 if (HasStackOverflow()) {
2347 ASSERT(frame_->height() == original_height);
2348 return;
2349 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352}
2353
2354
ager@chromium.org7c537e22008-10-16 08:43:32 +00002355void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002357#ifdef DEBUG
2358 int original_height = frame_->height();
2359#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002360 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2362 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
ager@chromium.org7c537e22008-10-16 08:43:32 +00002367void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368#ifdef DEBUG
2369 int original_height = frame_->height();
2370#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002371 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002373 JumpTarget then;
2374 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002375 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002376 if (has_valid_frame()) {
2377 Branch(false, &else_);
2378 }
2379 if (has_valid_frame() || then.is_linked()) {
2380 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002381 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002382 }
2383 if (else_.is_linked()) {
2384 JumpTarget exit;
2385 if (has_valid_frame()) exit.Jump();
2386 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002387 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002388 if (exit.is_linked()) exit.Bind();
2389 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391}
2392
2393
ager@chromium.org7c537e22008-10-16 08:43:32 +00002394void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002395 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002396 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002397 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002399 JumpTarget slow;
2400 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002401
2402 // Generate fast-case code for variables that might be shadowed by
2403 // eval-introduced variables. Eval is used a lot without
2404 // introducing variables. In those cases, we do not want to
2405 // perform a runtime call for all variables in the scope
2406 // containing the eval.
2407 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2408 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002409 // If there was no control flow to slow, we can exit early.
2410 if (!slow.is_linked()) {
2411 frame_->EmitPush(r0);
2412 return;
2413 }
2414
2415 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002416
2417 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2418 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2419 // Only generate the fast case for locals that rewrite to slots.
2420 // This rules out argument loads.
2421 if (potential_slot != NULL) {
2422 __ ldr(r0,
2423 ContextSlotOperandCheckExtensions(potential_slot,
2424 r1,
2425 r2,
2426 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002427 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002428 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2429 __ cmp(r0, ip);
2430 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002431 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002433 // ContextSlotOperandCheckExtensions so we have to jump around
2434 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002436 }
2437 }
2438
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002439 slow.Bind();
2440 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002441 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002442 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443
ager@chromium.org7c537e22008-10-16 08:43:32 +00002444 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002445 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002446 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002447 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002449
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450 done.Bind();
2451 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452
2453 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002454 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002455 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002457 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002458 // Const slots may contain 'the hole' value (the constant hasn't been
2459 // initialized yet) which needs to be converted into the 'undefined'
2460 // value.
2461 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002462 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002463 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2464 __ cmp(r0, ip);
2465 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 }
2468 }
2469}
2470
2471
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002472void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2473 ASSERT(slot != NULL);
2474 if (slot->type() == Slot::LOOKUP) {
2475 ASSERT(slot->var()->is_dynamic());
2476
2477 // For now, just do a runtime call.
2478 frame_->EmitPush(cp);
2479 __ mov(r0, Operand(slot->var()->name()));
2480 frame_->EmitPush(r0);
2481
2482 if (init_state == CONST_INIT) {
2483 // Same as the case for a normal store, but ignores attribute
2484 // (e.g. READ_ONLY) of context slot so that we can initialize
2485 // const properties (introduced via eval("const foo = (some
2486 // expr);")). Also, uses the current function context instead of
2487 // the top context.
2488 //
2489 // Note that we must declare the foo upon entry of eval(), via a
2490 // context slot declaration, but we cannot initialize it at the
2491 // same time, because the const declaration may be at the end of
2492 // the eval code (sigh...) and the const variable may have been
2493 // used before (where its value is 'undefined'). Thus, we can only
2494 // do the initialization when we actually encounter the expression
2495 // and when the expression operands are defined and valid, and
2496 // thus we need the split into 2 operations: declaration of the
2497 // context slot followed by initialization.
2498 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2499 } else {
2500 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2501 }
2502 // Storing a variable must keep the (new) value on the expression
2503 // stack. This is necessary for compiling assignment expressions.
2504 frame_->EmitPush(r0);
2505
2506 } else {
2507 ASSERT(!slot->var()->is_dynamic());
2508
2509 JumpTarget exit;
2510 if (init_state == CONST_INIT) {
2511 ASSERT(slot->var()->mode() == Variable::CONST);
2512 // Only the first const initialization must be executed (the slot
2513 // still contains 'the hole' value). When the assignment is
2514 // executed, the code is identical to a normal store (see below).
2515 Comment cmnt(masm_, "[ Init const");
2516 __ ldr(r2, SlotOperand(slot, r2));
2517 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2518 __ cmp(r2, ip);
2519 exit.Branch(ne);
2520 }
2521
2522 // We must execute the store. Storing a variable must keep the
2523 // (new) value on the stack. This is necessary for compiling
2524 // assignment expressions.
2525 //
2526 // Note: We will reach here even with slot->var()->mode() ==
2527 // Variable::CONST because of const declarations which will
2528 // initialize consts to 'the hole' value and by doing so, end up
2529 // calling this code. r2 may be loaded with context; used below in
2530 // RecordWrite.
2531 frame_->EmitPop(r0);
2532 __ str(r0, SlotOperand(slot, r2));
2533 frame_->EmitPush(r0);
2534 if (slot->type() == Slot::CONTEXT) {
2535 // Skip write barrier if the written value is a smi.
2536 __ tst(r0, Operand(kSmiTagMask));
2537 exit.Branch(eq);
2538 // r2 is loaded with context when calling SlotOperand above.
2539 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2540 __ mov(r3, Operand(offset));
2541 __ RecordWrite(r2, r3, r1);
2542 }
2543 // If we definitely did not jump over the assignment, we do not need
2544 // to bind the exit label. Doing so can defeat peephole
2545 // optimization.
2546 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2547 exit.Bind();
2548 }
2549 }
2550}
2551
2552
ager@chromium.org381abbb2009-02-25 13:23:22 +00002553void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2554 TypeofState typeof_state,
2555 Register tmp,
2556 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002557 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002558 // Check that no extension objects have been created by calls to
2559 // eval from the current scope to the global scope.
2560 Register context = cp;
2561 Scope* s = scope();
2562 while (s != NULL) {
2563 if (s->num_heap_slots() > 0) {
2564 if (s->calls_eval()) {
2565 // Check that extension is NULL.
2566 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2567 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002569 }
2570 // Load next context in chain.
2571 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2572 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2573 context = tmp;
2574 }
2575 // If no outer scope calls eval, we do not need to check more
2576 // context extensions.
2577 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2578 s = s->outer_scope();
2579 }
2580
2581 if (s->is_eval_scope()) {
2582 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002583 if (!context.is(tmp)) {
2584 __ mov(tmp, Operand(context));
2585 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002586 __ bind(&next);
2587 // Terminate at global context.
2588 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002589 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2590 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002591 __ b(eq, &fast);
2592 // Check that extension is NULL.
2593 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2594 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002596 // Load next context in chain.
2597 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2598 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2599 __ b(&next);
2600 __ bind(&fast);
2601 }
2602
2603 // All extension objects were empty and it is safe to use a global
2604 // load IC call.
2605 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2606 // Load the global object.
2607 LoadGlobal();
2608 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002609 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002610 // Call IC stub.
2611 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002612 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002613 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002614 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002615 }
2616
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002617 // Drop the global object. The result is in r0.
2618 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002619}
2620
2621
ager@chromium.org7c537e22008-10-16 08:43:32 +00002622void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623#ifdef DEBUG
2624 int original_height = frame_->height();
2625#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002626 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002627 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002628 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002630}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
ager@chromium.org7c537e22008-10-16 08:43:32 +00002632
2633void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002634#ifdef DEBUG
2635 int original_height = frame_->height();
2636#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002637 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002638 Comment cmnt(masm_, "[ VariableProxy");
2639
2640 Variable* var = node->var();
2641 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002642 if (expr != NULL) {
2643 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002645 ASSERT(var->is_global());
2646 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002647 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002649 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650}
2651
2652
ager@chromium.org7c537e22008-10-16 08:43:32 +00002653void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654#ifdef DEBUG
2655 int original_height = frame_->height();
2656#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002657 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002659 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660 frame_->EmitPush(r0);
2661 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662}
2663
2664
ager@chromium.org7c537e22008-10-16 08:43:32 +00002665void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002666#ifdef DEBUG
2667 int original_height = frame_->height();
2668#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002669 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 Comment cmnt(masm_, "[ RexExp Literal");
2671
2672 // Retrieve the literal array and check the allocated entry.
2673
2674 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002675 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676
2677 // Load the literals array of the function.
2678 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2679
2680 // Load the literal at the ast saved index.
2681 int literal_offset =
2682 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2683 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2684
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002685 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002686 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2687 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689
2690 // If the entry is undefined we call the runtime system to computed
2691 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002693 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002695 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002696 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002697 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 frame_->EmitPush(r0);
2699 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002700 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002702 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002704 frame_->EmitPush(r2);
2705 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706}
2707
2708
ager@chromium.org7c537e22008-10-16 08:43:32 +00002709void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002710#ifdef DEBUG
2711 int original_height = frame_->height();
2712#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002713 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002714 Comment cmnt(masm_, "[ ObjectLiteral");
2715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002717 __ ldr(r2, frame_->Function());
2718 // Literal array.
2719 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2720 // Literal index.
2721 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2722 // Constant properties.
2723 __ mov(r0, Operand(node->constant_properties()));
2724 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2725 if (node->depth() > 1) {
2726 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3);
2727 } else {
2728 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002729 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002731 // r0: created object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732
2733 for (int i = 0; i < node->properties()->length(); i++) {
2734 ObjectLiteral::Property* property = node->properties()->at(i);
2735 Literal* key = property->key();
2736 Expression* value = property->value();
2737 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002738 case ObjectLiteral::Property::CONSTANT:
2739 break;
2740 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2741 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2742 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 case ObjectLiteral::Property::COMPUTED: // fall through
2744 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 frame_->EmitPush(r0); // dup the result
2746 LoadAndSpill(key);
2747 LoadAndSpill(value);
2748 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002749 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002750 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 break;
2752 }
2753 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0);
2755 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002756 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
2758 LoadAndSpill(value);
2759 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002760 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 break;
2762 }
2763 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764 frame_->EmitPush(r0);
2765 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 LoadAndSpill(value);
2769 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002770 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 break;
2772 }
2773 }
2774 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002775 ASSERT(frame_->height() == original_height + 1);
2776}
2777
2778
ager@chromium.org7c537e22008-10-16 08:43:32 +00002779void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002780#ifdef DEBUG
2781 int original_height = frame_->height();
2782#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002783 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002785
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002787 __ ldr(r2, frame_->Function());
2788 // Literals array.
2789 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2790 // Literal index.
2791 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2792 // Constant elements.
2793 __ mov(r0, Operand(node->constant_elements()));
2794 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2795 if (node->depth() > 1) {
2796 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
2797 } else {
2798 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002799 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002800 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002801 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002802
2803 // Generate code to set the elements in the array that are not
2804 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805 for (int i = 0; i < node->values()->length(); i++) {
2806 Expression* value = node->values()->at(i);
2807
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002808 // If value is a literal the property value is already set in the
2809 // boilerplate object.
2810 if (value->AsLiteral() != NULL) continue;
2811 // If value is a materialized literal the property value is already set
2812 // in the boilerplate object if it is simple.
2813 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002815 // The property must be set by generated code.
2816 LoadAndSpill(value);
2817 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002819 // Fetch the object literal.
2820 __ ldr(r1, frame_->Top());
2821 // Get the elements array.
2822 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002823
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002824 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002825 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002826 __ str(r0, FieldMemOperand(r1, offset));
2827
2828 // Update the write barrier for the array address.
2829 __ mov(r3, Operand(offset));
2830 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002832 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833}
2834
2835
ager@chromium.org32912102009-01-16 10:38:43 +00002836void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002837#ifdef DEBUG
2838 int original_height = frame_->height();
2839#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002840 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002841 // Call runtime routine to allocate the catch extension object and
2842 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002843 Comment cmnt(masm_, "[ CatchExtensionObject");
2844 LoadAndSpill(node->key());
2845 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002846 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2847 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002848 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002849}
2850
2851
ager@chromium.org7c537e22008-10-16 08:43:32 +00002852void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002853#ifdef DEBUG
2854 int original_height = frame_->height();
2855#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002856 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002858
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002859 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002860 if (target.is_illegal()) {
2861 // Fool the virtual frame into thinking that we left the assignment's
2862 // value on the frame.
2863 __ mov(r0, Operand(Smi::FromInt(0)));
2864 frame_->EmitPush(r0);
2865 ASSERT(frame_->height() == original_height + 1);
2866 return;
2867 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002869 if (node->op() == Token::ASSIGN ||
2870 node->op() == Token::INIT_VAR ||
2871 node->op() == Token::INIT_CONST) {
2872 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002873
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002874 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002875 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002876 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002877 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002878 bool overwrite =
2879 (node->value()->AsBinaryOperation() != NULL &&
2880 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002881 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002882 SmiOperation(node->binary_op(),
2883 literal->handle(),
2884 false,
2885 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002886 frame_->EmitPush(r0);
2887
2888 } else {
2889 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002890 GenericBinaryOperation(node->binary_op(),
2891 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002892 frame_->EmitPush(r0);
2893 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002895 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2896 if (var != NULL &&
2897 (var->mode() == Variable::CONST) &&
2898 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2899 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002900 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 CodeForSourcePosition(node->position());
2903 if (node->op() == Token::INIT_CONST) {
2904 // Dynamic constant initializations must use the function context
2905 // and initialize the actual constant declared. Dynamic variable
2906 // initializations are simply assignments and use SetValue.
2907 target.SetValue(CONST_INIT);
2908 } else {
2909 target.SetValue(NOT_CONST_INIT);
2910 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 }
2912 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914}
2915
2916
ager@chromium.org7c537e22008-10-16 08:43:32 +00002917void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002918#ifdef DEBUG
2919 int original_height = frame_->height();
2920#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002921 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 Comment cmnt(masm_, "[ Throw");
2923
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002925 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002926 frame_->CallRuntime(Runtime::kThrow, 1);
2927 frame_->EmitPush(r0);
2928 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929}
2930
2931
ager@chromium.org7c537e22008-10-16 08:43:32 +00002932void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002933#ifdef DEBUG
2934 int original_height = frame_->height();
2935#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002936 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002938
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002939 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002940 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 }
2942 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943}
2944
2945
ager@chromium.org7c537e22008-10-16 08:43:32 +00002946void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002947#ifdef DEBUG
2948 int original_height = frame_->height();
2949#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002950 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 Comment cmnt(masm_, "[ Call");
2952
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002953 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954 ZoneList<Expression*>* args = node->arguments();
2955
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002956 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 Variable* var = function->AsVariableProxy()->AsVariable();
2959 Property* property = function->AsProperty();
2960
2961 // ------------------------------------------------------------------------
2962 // Fast-case: Use inline caching.
2963 // ---
2964 // According to ECMA-262, section 11.2.3, page 44, the function to call
2965 // must be resolved after the arguments have been evaluated. The IC code
2966 // automatically handles this by loading the arguments before the function
2967 // is resolved in cache misses (this also holds for megamorphic calls).
2968 // ------------------------------------------------------------------------
2969
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002970 if (var != NULL && var->is_possibly_eval()) {
2971 // ----------------------------------
2972 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2973 // ----------------------------------
2974
2975 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2976 // resolve the function we need to call and the receiver of the
2977 // call. Then we call the resolved function using the given
2978 // arguments.
2979 // Prepare stack for call to resolved function.
2980 LoadAndSpill(function);
2981 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2982 frame_->EmitPush(r2); // Slot for receiver
2983 int arg_count = args->length();
2984 for (int i = 0; i < arg_count; i++) {
2985 LoadAndSpill(args->at(i));
2986 }
2987
2988 // Prepare stack for call to ResolvePossiblyDirectEval.
2989 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2990 frame_->EmitPush(r1);
2991 if (arg_count > 0) {
2992 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2993 frame_->EmitPush(r1);
2994 } else {
2995 frame_->EmitPush(r2);
2996 }
2997
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002998 // Push the receiver.
2999 __ ldr(r1, frame_->Receiver());
3000 frame_->EmitPush(r1);
3001
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003002 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003003 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003004
3005 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003006 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003007 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3008
3009 // Call the function.
3010 CodeForSourcePosition(node->position());
3011
3012 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003013 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003014 frame_->CallStub(&call_function, arg_count + 1);
3015
3016 __ ldr(cp, frame_->Context());
3017 // Remove the function from the stack.
3018 frame_->Drop();
3019 frame_->EmitPush(r0);
3020
3021 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022 // ----------------------------------
3023 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3024 // ----------------------------------
3025
3026 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003027 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003028 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003029
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003030 // Pass the global object as the receiver and let the IC stub
3031 // patch the stack to use the global proxy as 'this' in the
3032 // invoked function.
3033 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003034
3035 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003036 int arg_count = args->length();
3037 for (int i = 0; i < arg_count; i++) {
3038 LoadAndSpill(args->at(i));
3039 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040
3041 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003042 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3043 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003044 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003045 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3046 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003047 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003049 frame_->Drop();
3050 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003051
3052 } else if (var != NULL && var->slot() != NULL &&
3053 var->slot()->type() == Slot::LOOKUP) {
3054 // ----------------------------------
3055 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3056 // ----------------------------------
3057
3058 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003059 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003060 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 frame_->EmitPush(r0);
3062 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063 // r0: slot value; r1: receiver
3064
3065 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003066 frame_->EmitPush(r0); // function
3067 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068
3069 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003070 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003071 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072
3073 } else if (property != NULL) {
3074 // Check if the key is a literal string.
3075 Literal* literal = property->key()->AsLiteral();
3076
3077 if (literal != NULL && literal->handle()->IsSymbol()) {
3078 // ------------------------------------------------------------------
3079 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3080 // ------------------------------------------------------------------
3081
3082 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003083 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003084 frame_->EmitPush(r0);
3085 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086
3087 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 int arg_count = args->length();
3089 for (int i = 0; i < arg_count; i++) {
3090 LoadAndSpill(args->at(i));
3091 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003092
3093 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003094 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3095 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003096 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003098 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099
3100 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003101 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003102
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003103 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003104
3105 } else {
3106 // -------------------------------------------
3107 // JavaScript example: 'array[index](1, 2, 3)'
3108 // -------------------------------------------
3109
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003110 LoadAndSpill(property->obj());
3111 LoadAndSpill(property->key());
3112 EmitKeyedLoad(false);
3113 frame_->Drop(); // key
3114 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003115 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003116 // Use the global receiver.
3117 frame_->Drop();
3118 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003119 LoadGlobalReceiver(r0);
3120 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003121 frame_->EmitPop(r1); // receiver
3122 frame_->EmitPush(r0); // function
3123 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003124 }
3125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003127 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 }
3130
3131 } else {
3132 // ----------------------------------
3133 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3134 // ----------------------------------
3135
3136 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003138
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003139 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003140 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003143 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003144 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003146 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003147}
3148
3149
ager@chromium.org7c537e22008-10-16 08:43:32 +00003150void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003151#ifdef DEBUG
3152 int original_height = frame_->height();
3153#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003154 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155 Comment cmnt(masm_, "[ CallNew");
3156
3157 // According to ECMA-262, section 11.2.2, page 44, the function
3158 // expression in new calls must be evaluated before the
3159 // arguments. This is different from ordinary calls, where the
3160 // actual function to call is resolved after the arguments have been
3161 // evaluated.
3162
3163 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003164 // receiver. There is no need to use the global proxy here because
3165 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003166 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003167 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168
3169 // Push the arguments ("left-to-right") on the stack.
3170 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003171 int arg_count = args->length();
3172 for (int i = 0; i < arg_count; i++) {
3173 LoadAndSpill(args->at(i));
3174 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175
mads.s.ager31e71382008-08-13 09:32:07 +00003176 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003177 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003178 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003179 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181 // Call the construct call builtin that handles allocation and
3182 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003183 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003184 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003185 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003186
3187 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003188 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003189 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003190}
3191
3192
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003193void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3194 VirtualFrame::SpilledScope spilled_scope;
3195 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003196 JumpTarget leave, null, function, non_function_constructor;
3197
3198 // Load the object into r0.
3199 LoadAndSpill(args->at(0));
3200 frame_->EmitPop(r0);
3201
3202 // If the object is a smi, we return null.
3203 __ tst(r0, Operand(kSmiTagMask));
3204 null.Branch(eq);
3205
3206 // Check that the object is a JS object but take special care of JS
3207 // functions to make sure they have 'Function' as their class.
3208 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3209 null.Branch(lt);
3210
3211 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3212 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3213 // LAST_JS_OBJECT_TYPE.
3214 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3215 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3216 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3217 function.Branch(eq);
3218
3219 // Check if the constructor in the map is a function.
3220 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3221 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3222 non_function_constructor.Branch(ne);
3223
3224 // The r0 register now contains the constructor function. Grab the
3225 // instance class name from there.
3226 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3227 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003228 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003229 leave.Jump();
3230
3231 // Functions have class 'Function'.
3232 function.Bind();
3233 __ mov(r0, Operand(Factory::function_class_symbol()));
3234 frame_->EmitPush(r0);
3235 leave.Jump();
3236
3237 // Objects with a non-function constructor have class 'Object'.
3238 non_function_constructor.Bind();
3239 __ mov(r0, Operand(Factory::Object_symbol()));
3240 frame_->EmitPush(r0);
3241 leave.Jump();
3242
3243 // Non-JS objects have class null.
3244 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003245 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003246 frame_->EmitPush(r0);
3247
3248 // All done.
3249 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003250}
3251
3252
ager@chromium.org7c537e22008-10-16 08:43:32 +00003253void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003254 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003255 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003256 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003257 LoadAndSpill(args->at(0));
3258 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003259 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003260 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003262 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3263 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003264 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003265 // Load the value.
3266 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003267 leave.Bind();
3268 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003269}
3270
3271
ager@chromium.org7c537e22008-10-16 08:43:32 +00003272void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003273 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003275 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 LoadAndSpill(args->at(0)); // Load the object.
3277 LoadAndSpill(args->at(1)); // Load the value.
3278 frame_->EmitPop(r0); // r0 contains value
3279 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 // if (object->IsSmi()) return object.
3281 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003283 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3284 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003285 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286 // Store the value.
3287 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3288 // Update the write barrier.
3289 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3290 __ RecordWrite(r1, r2, r3);
3291 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 leave.Bind();
3293 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294}
3295
3296
ager@chromium.org7c537e22008-10-16 08:43:32 +00003297void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003298 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003300 LoadAndSpill(args->at(0));
3301 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003302 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303 cc_reg_ = eq;
3304}
3305
3306
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003307void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003308 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003309 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3310 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003311#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003312 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 LoadAndSpill(args->at(1));
3314 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003315 __ CallRuntime(Runtime::kLog, 2);
3316 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003317#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003318 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003319 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003320}
3321
3322
ager@chromium.org7c537e22008-10-16 08:43:32 +00003323void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003324 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003325 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003326 LoadAndSpill(args->at(0));
3327 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003328 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003329 cc_reg_ = eq;
3330}
3331
3332
kasper.lund7276f142008-07-30 08:49:36 +00003333// This should generate code that performs a charCodeAt() call or returns
3334// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3335// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003336void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003337 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003338 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003339 Comment(masm_, "[ GenerateFastCharCodeAt");
3340
3341 LoadAndSpill(args->at(0));
3342 LoadAndSpill(args->at(1));
3343 frame_->EmitPop(r0); // Index.
3344 frame_->EmitPop(r1); // String.
3345
3346 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3347
3348 __ tst(r1, Operand(kSmiTagMask));
3349 __ b(eq, &slow); // The 'string' was a Smi.
3350
3351 ASSERT(kSmiTag == 0);
3352 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3353 __ b(ne, &slow); // The index was negative or not a Smi.
3354
3355 __ bind(&try_again_with_new_string);
3356 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3357 __ b(ge, &slow);
3358
3359 // Now r2 has the string type.
3360 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003361 // Now r3 has the length of the string. Compare with the index.
3362 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3363 __ b(le, &slow);
3364
3365 // Here we know the index is in range. Check that string is sequential.
3366 ASSERT_EQ(0, kSeqStringTag);
3367 __ tst(r2, Operand(kStringRepresentationMask));
3368 __ b(ne, &not_a_flat_string);
3369
3370 // Check whether it is an ASCII string.
3371 ASSERT_EQ(0, kTwoByteStringTag);
3372 __ tst(r2, Operand(kStringEncodingMask));
3373 __ b(ne, &ascii_string);
3374
3375 // 2-byte string. We can add without shifting since the Smi tag size is the
3376 // log2 of the number of bytes in a two-byte character.
3377 ASSERT_EQ(1, kSmiTagSize);
3378 ASSERT_EQ(0, kSmiShiftSize);
3379 __ add(r1, r1, Operand(r0));
3380 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3381 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3382 __ jmp(&end);
3383
3384 __ bind(&ascii_string);
3385 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3386 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3387 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3388 __ jmp(&end);
3389
3390 __ bind(&not_a_flat_string);
3391 __ and_(r2, r2, Operand(kStringRepresentationMask));
3392 __ cmp(r2, Operand(kConsStringTag));
3393 __ b(ne, &slow);
3394
3395 // ConsString.
3396 // Check that the right hand side is the empty string (ie if this is really a
3397 // flat string in a cons string). If that is not the case we would rather go
3398 // to the runtime system now, to flatten the string.
3399 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3400 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3401 __ cmp(r2, Operand(r3));
3402 __ b(ne, &slow);
3403
3404 // Get the first of the two strings.
3405 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3406 __ jmp(&try_again_with_new_string);
3407
3408 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003409 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003410
3411 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003412 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003413}
3414
3415
ager@chromium.org7c537e22008-10-16 08:43:32 +00003416void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003417 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003418 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003419 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003420 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003421 // We need the CC bits to come out as not_equal in the case where the
3422 // object is a smi. This can't be done with the usual test opcode so
3423 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003424 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003425 __ and_(r1, r0, Operand(kSmiTagMask));
3426 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003427 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003428 // It is a heap object - get the map. Check if the object is a JS array.
3429 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431 cc_reg_ = eq;
3432}
3433
3434
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003435void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3436 // This generates a fast version of:
3437 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3438 VirtualFrame::SpilledScope spilled_scope;
3439 ASSERT(args->length() == 1);
3440 LoadAndSpill(args->at(0));
3441 frame_->EmitPop(r1);
3442 __ tst(r1, Operand(kSmiTagMask));
3443 false_target()->Branch(eq);
3444
3445 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3446 __ cmp(r1, ip);
3447 true_target()->Branch(eq);
3448
3449 Register map_reg = r2;
3450 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3451 // Undetectable objects behave like undefined when tested with typeof.
3452 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3453 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3454 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3455 false_target()->Branch(eq);
3456
3457 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3458 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3459 false_target()->Branch(lt);
3460 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3461 cc_reg_ = le;
3462}
3463
3464
3465void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3466 // This generates a fast version of:
3467 // (%_ClassOf(arg) === 'Function')
3468 VirtualFrame::SpilledScope spilled_scope;
3469 ASSERT(args->length() == 1);
3470 LoadAndSpill(args->at(0));
3471 frame_->EmitPop(r0);
3472 __ tst(r0, Operand(kSmiTagMask));
3473 false_target()->Branch(eq);
3474 Register map_reg = r2;
3475 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3476 cc_reg_ = eq;
3477}
3478
3479
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003480void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
3481 VirtualFrame::SpilledScope spilled_scope;
3482 ASSERT(args->length() == 1);
3483 LoadAndSpill(args->at(0));
3484 frame_->EmitPop(r0);
3485 __ tst(r0, Operand(kSmiTagMask));
3486 false_target()->Branch(eq);
3487 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3488 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3489 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3490 cc_reg_ = ne;
3491}
3492
3493
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003494void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3495 VirtualFrame::SpilledScope spilled_scope;
3496 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003497
3498 // Get the frame pointer for the calling frame.
3499 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3500
3501 // Skip the arguments adaptor frame if it exists.
3502 Label check_frame_marker;
3503 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003504 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003505 __ b(ne, &check_frame_marker);
3506 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3507
3508 // Check the marker in the calling frame.
3509 __ bind(&check_frame_marker);
3510 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3511 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3512 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003513}
3514
3515
ager@chromium.org7c537e22008-10-16 08:43:32 +00003516void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003517 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 ASSERT(args->length() == 0);
3519
mads.s.ager31e71382008-08-13 09:32:07 +00003520 // Seed the result with the formal parameters count, which will be used
3521 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3523
3524 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003525 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003526 frame_->CallStub(&stub, 0);
3527 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528}
3529
3530
ager@chromium.org7c537e22008-10-16 08:43:32 +00003531void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003532 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533 ASSERT(args->length() == 1);
3534
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003535 // Satisfy contract with ArgumentsAccessStub:
3536 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 LoadAndSpill(args->at(0));
3538 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003539 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540
3541 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003542 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003543 frame_->CallStub(&stub, 0);
3544 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545}
3546
3547
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003548void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3549 VirtualFrame::SpilledScope spilled_scope;
3550 ASSERT(args->length() == 0);
3551 __ Call(ExternalReference::random_positive_smi_function().address(),
3552 RelocInfo::RUNTIME_ENTRY);
3553 frame_->EmitPush(r0);
3554}
3555
3556
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003557void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3558 ASSERT_EQ(2, args->length());
3559
3560 Load(args->at(0));
3561 Load(args->at(1));
3562
3563 frame_->CallRuntime(Runtime::kStringAdd, 2);
3564 frame_->EmitPush(r0);
3565}
3566
3567
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003568void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3569 ASSERT_EQ(3, args->length());
3570
3571 Load(args->at(0));
3572 Load(args->at(1));
3573 Load(args->at(2));
3574
3575 frame_->CallRuntime(Runtime::kSubString, 3);
3576 frame_->EmitPush(r0);
3577}
3578
3579
3580void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3581 ASSERT_EQ(2, args->length());
3582
3583 Load(args->at(0));
3584 Load(args->at(1));
3585
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003586 StringCompareStub stub;
3587 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003588 frame_->EmitPush(r0);
3589}
3590
3591
3592void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3593 ASSERT_EQ(4, args->length());
3594
3595 Load(args->at(0));
3596 Load(args->at(1));
3597 Load(args->at(2));
3598 Load(args->at(3));
3599
3600 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3601 frame_->EmitPush(r0);
3602}
3603
3604
ager@chromium.org7c537e22008-10-16 08:43:32 +00003605void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003606 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003607 ASSERT(args->length() == 2);
3608
3609 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003610 LoadAndSpill(args->at(0));
3611 LoadAndSpill(args->at(1));
3612 frame_->EmitPop(r0);
3613 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003614 __ cmp(r0, Operand(r1));
3615 cc_reg_ = eq;
3616}
3617
3618
ager@chromium.org7c537e22008-10-16 08:43:32 +00003619void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003620#ifdef DEBUG
3621 int original_height = frame_->height();
3622#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003623 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003624 if (CheckForInlineRuntimeCall(node)) {
3625 ASSERT((has_cc() && frame_->height() == original_height) ||
3626 (!has_cc() && frame_->height() == original_height + 1));
3627 return;
3628 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003629
3630 ZoneList<Expression*>* args = node->arguments();
3631 Comment cmnt(masm_, "[ CallRuntime");
3632 Runtime::Function* function = node->function();
3633
ager@chromium.org41826e72009-03-30 13:30:57 +00003634 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003635 // Prepare stack for calling JS runtime function.
3636 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003637 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003638 // Push the builtins object found in the current global object.
3639 __ ldr(r1, GlobalObject());
3640 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003641 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003642 }
mads.s.ager31e71382008-08-13 09:32:07 +00003643
ager@chromium.org41826e72009-03-30 13:30:57 +00003644 // Push the arguments ("left-to-right").
3645 int arg_count = args->length();
3646 for (int i = 0; i < arg_count; i++) {
3647 LoadAndSpill(args->at(i));
3648 }
mads.s.ager31e71382008-08-13 09:32:07 +00003649
ager@chromium.org41826e72009-03-30 13:30:57 +00003650 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003652 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3653 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003654 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003655 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003656 frame_->Drop();
3657 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003658 } else {
3659 // Call the C runtime function.
3660 frame_->CallRuntime(function, arg_count);
3661 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664}
3665
3666
ager@chromium.org7c537e22008-10-16 08:43:32 +00003667void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003668#ifdef DEBUG
3669 int original_height = frame_->height();
3670#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003671 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 Comment cmnt(masm_, "[ UnaryOperation");
3673
3674 Token::Value op = node->op();
3675
3676 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003677 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003678 false_target(),
3679 true_target(),
3680 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003681 // LoadCondition may (and usually does) leave a test and branch to
3682 // be emitted by the caller. In that case, negate the condition.
3683 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684
3685 } else if (op == Token::DELETE) {
3686 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003687 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003688 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003689 LoadAndSpill(property->obj());
3690 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003691 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692
mads.s.ager31e71382008-08-13 09:32:07 +00003693 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003694 Slot* slot = variable->slot();
3695 if (variable->is_global()) {
3696 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003697 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003699 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003700
3701 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3702 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003703 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003704 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003705 frame_->EmitPush(r0);
3706 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003708 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003709 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003710 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003711 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712
mads.s.ager31e71382008-08-13 09:32:07 +00003713 } else {
3714 // Default: Result of deleting non-global, not dynamically
3715 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003716 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003717 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718
3719 } else {
3720 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003721 LoadAndSpill(node->expression()); // may have side-effects
3722 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003723 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003725 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003726
3727 } else if (op == Token::TYPEOF) {
3728 // Special case for loading the typeof expression; see comment on
3729 // LoadTypeofExpression().
3730 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 frame_->CallRuntime(Runtime::kTypeof, 1);
3732 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733
3734 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003735 bool overwrite =
3736 (node->expression()->AsBinaryOperation() != NULL &&
3737 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003738 LoadAndSpill(node->expression());
3739 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003740 switch (op) {
3741 case Token::NOT:
3742 case Token::DELETE:
3743 case Token::TYPEOF:
3744 UNREACHABLE(); // handled above
3745 break;
3746
3747 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003748 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003749 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750 break;
3751 }
3752
3753 case Token::BIT_NOT: {
3754 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003755 JumpTarget smi_label;
3756 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003758 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003759
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003760 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
3761 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003763
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765 __ mvn(r0, Operand(r0));
3766 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003767 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003768 break;
3769 }
3770
3771 case Token::VOID:
3772 // since the stack top is cached in r0, popping and then
3773 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003774 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775 break;
3776
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003777 case Token::ADD: {
3778 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003779 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003780 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003781 continue_label.Branch(eq);
3782 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003783 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003784 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003786 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787 default:
3788 UNREACHABLE();
3789 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003790 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003792 ASSERT(!has_valid_frame() ||
3793 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003794 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795}
3796
3797
ager@chromium.org7c537e22008-10-16 08:43:32 +00003798void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003799#ifdef DEBUG
3800 int original_height = frame_->height();
3801#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003802 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003803 Comment cmnt(masm_, "[ CountOperation");
3804
3805 bool is_postfix = node->is_postfix();
3806 bool is_increment = node->op() == Token::INC;
3807
3808 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3809 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3810
3811 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003812 if (is_postfix) {
3813 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003814 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003815 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003817 // A constant reference is not saved to, so a constant reference is not a
3818 // compound assignment reference.
3819 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820 if (target.is_illegal()) {
3821 // Spoof the virtual frame to have the expected height (one higher
3822 // than on entry).
3823 if (!is_postfix) {
3824 __ mov(r0, Operand(Smi::FromInt(0)));
3825 frame_->EmitPush(r0);
3826 }
3827 ASSERT(frame_->height() == original_height + 1);
3828 return;
3829 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003830 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003833 JumpTarget slow;
3834 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003835
3836 // Load the value (1) into register r1.
3837 __ mov(r1, Operand(Smi::FromInt(1)));
3838
3839 // Check for smi operand.
3840 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003841 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842
3843 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003844 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003845 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003846 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847
3848 // Perform optimistic increment/decrement.
3849 if (is_increment) {
3850 __ add(r0, r0, Operand(r1), SetCC);
3851 } else {
3852 __ sub(r0, r0, Operand(r1), SetCC);
3853 }
3854
3855 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003856 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857
3858 // Revert optimistic increment/decrement.
3859 if (is_increment) {
3860 __ sub(r0, r0, Operand(r1));
3861 } else {
3862 __ add(r0, r0, Operand(r1));
3863 }
3864
3865 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003866 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003867 {
3868 // Convert the operand to a number.
3869 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003870 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003871 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003872 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003873 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003874 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 }
3876
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003877 // Compute the new value.
3878 __ mov(r1, Operand(Smi::FromInt(1)));
3879 frame_->EmitPush(r0);
3880 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003882 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003884 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003885 }
3886
3887 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003888 exit.Bind();
3889 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003890 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 }
3892
3893 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894 if (is_postfix) frame_->EmitPop(r0);
3895 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896}
3897
3898
ager@chromium.org7c537e22008-10-16 08:43:32 +00003899void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003900#ifdef DEBUG
3901 int original_height = frame_->height();
3902#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003903 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904 Comment cmnt(masm_, "[ BinaryOperation");
3905 Token::Value op = node->op();
3906
3907 // According to ECMA-262 section 11.11, page 58, the binary logical
3908 // operators must yield the result of one of the two expressions
3909 // before any ToBoolean() conversions. This means that the value
3910 // produced by a && or || operator is not necessarily a boolean.
3911
3912 // NOTE: If the left hand side produces a materialized value (not in
3913 // the CC register), we force the right hand side to do the
3914 // same. This is necessary because we may have to branch to the exit
3915 // after evaluating the left hand side (due to the shortcut
3916 // semantics), but the compiler must (statically) know if the result
3917 // of compiling the binary operation is materialized or not.
3918
3919 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003920 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 &is_true,
3923 false_target(),
3924 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003925 if (has_valid_frame() && !has_cc()) {
3926 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003927 JumpTarget pop_and_continue;
3928 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003930 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003931 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 // Avoid popping the result if it converts to 'false' using the
3933 // standard ToBoolean() conversion as described in ECMA-262,
3934 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003935 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 Branch(false, &exit);
3937
3938 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003939 pop_and_continue.Bind();
3940 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941
3942 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003943 is_true.Bind();
3944 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003945
3946 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003947 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003948 } else if (has_cc() || is_true.is_linked()) {
3949 // The left-hand side is either (a) partially compiled to
3950 // control flow with a final branch left to emit or (b) fully
3951 // compiled to control flow and possibly true.
3952 if (has_cc()) {
3953 Branch(false, false_target());
3954 }
3955 is_true.Bind();
3956 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003957 true_target(),
3958 false_target(),
3959 false);
3960 } else {
3961 // Nothing to do.
3962 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 }
3964
3965 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003966 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003967 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 true_target(),
3969 &is_false,
3970 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003971 if (has_valid_frame() && !has_cc()) {
3972 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003973 JumpTarget pop_and_continue;
3974 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003976 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003977 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003978 // Avoid popping the result if it converts to 'true' using the
3979 // standard ToBoolean() conversion as described in ECMA-262,
3980 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003981 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 Branch(true, &exit);
3983
3984 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003985 pop_and_continue.Bind();
3986 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987
3988 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 is_false.Bind();
3990 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991
3992 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003993 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003994 } else if (has_cc() || is_false.is_linked()) {
3995 // The left-hand side is either (a) partially compiled to
3996 // control flow with a final branch left to emit or (b) fully
3997 // compiled to control flow and possibly false.
3998 if (has_cc()) {
3999 Branch(true, true_target());
4000 }
4001 is_false.Bind();
4002 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004003 true_target(),
4004 false_target(),
4005 false);
4006 } else {
4007 // Nothing to do.
4008 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004009 }
4010
4011 } else {
4012 // Optimize for the case where (at least) one of the expressions
4013 // is a literal small integer.
4014 Literal* lliteral = node->left()->AsLiteral();
4015 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004016 // NOTE: The code below assumes that the slow cases (calls to runtime)
4017 // never return a constant/immutable object.
4018 bool overwrite_left =
4019 (node->left()->AsBinaryOperation() != NULL &&
4020 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4021 bool overwrite_right =
4022 (node->right()->AsBinaryOperation() != NULL &&
4023 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024
4025 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004026 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004027 SmiOperation(node->op(),
4028 rliteral->handle(),
4029 false,
4030 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031
4032 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004033 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004034 SmiOperation(node->op(),
4035 lliteral->handle(),
4036 true,
4037 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038
4039 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004040 OverwriteMode overwrite_mode = NO_OVERWRITE;
4041 if (overwrite_left) {
4042 overwrite_mode = OVERWRITE_LEFT;
4043 } else if (overwrite_right) {
4044 overwrite_mode = OVERWRITE_RIGHT;
4045 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004046 LoadAndSpill(node->left());
4047 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004048 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004049 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004050 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004051 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004052 ASSERT(!has_valid_frame() ||
4053 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004054 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004055}
4056
4057
ager@chromium.org7c537e22008-10-16 08:43:32 +00004058void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004059#ifdef DEBUG
4060 int original_height = frame_->height();
4061#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004062 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004063 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004064 frame_->EmitPush(r0);
4065 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066}
4067
4068
ager@chromium.org7c537e22008-10-16 08:43:32 +00004069void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070#ifdef DEBUG
4071 int original_height = frame_->height();
4072#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004073 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074 Comment cmnt(masm_, "[ CompareOperation");
4075
4076 // Get the expressions from the node.
4077 Expression* left = node->left();
4078 Expression* right = node->right();
4079 Token::Value op = node->op();
4080
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004081 // To make null checks efficient, we check if either left or right is the
4082 // literal 'null'. If so, we optimize the code by inlining a null check
4083 // instead of calling the (very) general runtime routine for checking
4084 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004085 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004086 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004087 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004088 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004089 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4090 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004091 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 LoadAndSpill(left_is_null ? right : left);
4093 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004094 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4095 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004096
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004097 // The 'null' value is only equal to 'undefined' if using non-strict
4098 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004100 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004101
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004102 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4103 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004104 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004106 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004107 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004109 // It can be an undetectable object.
4110 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4111 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4112 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4113 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114 }
4115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004117 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004118 return;
4119 }
4120 }
4121
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004122 // To make typeof testing for natives implemented in JavaScript really
4123 // efficient, we generate special code for expressions of the form:
4124 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004125 UnaryOperation* operation = left->AsUnaryOperation();
4126 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4127 (operation != NULL && operation->op() == Token::TYPEOF) &&
4128 (right->AsLiteral() != NULL &&
4129 right->AsLiteral()->handle()->IsString())) {
4130 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4131
mads.s.ager31e71382008-08-13 09:32:07 +00004132 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004133 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004134 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004135
4136 if (check->Equals(Heap::number_symbol())) {
4137 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004140 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4141 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004142 cc_reg_ = eq;
4143
4144 } else if (check->Equals(Heap::string_symbol())) {
4145 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004146 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004147
4148 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4149
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004150 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004151 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4152 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4153 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004154 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004155
4156 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4157 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4158 cc_reg_ = lt;
4159
4160 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004161 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4162 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004163 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004164 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4165 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004166 cc_reg_ = eq;
4167
4168 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004169 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4170 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004171 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004172
4173 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004174 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004175
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004176 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004177 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4178 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4179 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4180 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4181
4182 cc_reg_ = eq;
4183
4184 } else if (check->Equals(Heap::function_symbol())) {
4185 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004186 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004187 Register map_reg = r2;
4188 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4189 true_target()->Branch(eq);
4190 // Regular expressions are callable so typeof == 'function'.
4191 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004192 cc_reg_ = eq;
4193
4194 } else if (check->Equals(Heap::object_symbol())) {
4195 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004196 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004198 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4199 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004200 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004202 Register map_reg = r2;
4203 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4204 false_target()->Branch(eq);
4205
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004206 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004207 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004208 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4209 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004210 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004211
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004212 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4213 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004214 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004215 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004216 cc_reg_ = le;
4217
4218 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004219 // Uncommon case: typeof testing against a string literal that is
4220 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004221 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004223 ASSERT(!has_valid_frame() ||
4224 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004225 return;
4226 }
4227
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004228 switch (op) {
4229 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004230 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004231 break;
4232
4233 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004234 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004235 break;
4236
4237 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004238 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004239 break;
4240
4241 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004242 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004243 break;
4244
4245 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004246 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 break;
4248
4249 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004250 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 break;
4252
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004253 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004254 LoadAndSpill(left);
4255 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004256 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004257 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004258 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004259 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004261 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004262 LoadAndSpill(left);
4263 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004264 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004265 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004266 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004267 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004268 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004269 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004271
4272 default:
4273 UNREACHABLE();
4274 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 ASSERT((has_cc() && frame_->height() == original_height) ||
4276 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004277}
4278
4279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004280void CodeGenerator::EmitKeyedLoad(bool is_global) {
4281 Comment cmnt(masm_, "[ Load from keyed Property");
4282 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4283 RelocInfo::Mode rmode = is_global
4284 ? RelocInfo::CODE_TARGET_CONTEXT
4285 : RelocInfo::CODE_TARGET;
4286 frame_->CallCodeObject(ic, rmode, 0);
4287}
4288
4289
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004290#ifdef DEBUG
4291bool CodeGenerator::HasValidEntryRegisters() { return true; }
4292#endif
4293
4294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004295#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004296#define __ ACCESS_MASM(masm)
4297
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004298
ager@chromium.org7c537e22008-10-16 08:43:32 +00004299Handle<String> Reference::GetName() {
4300 ASSERT(type_ == NAMED);
4301 Property* property = expression_->AsProperty();
4302 if (property == NULL) {
4303 // Global variable reference treated as a named property reference.
4304 VariableProxy* proxy = expression_->AsVariableProxy();
4305 ASSERT(proxy->AsVariable() != NULL);
4306 ASSERT(proxy->AsVariable()->is_global());
4307 return proxy->name();
4308 } else {
4309 Literal* raw_name = property->key()->AsLiteral();
4310 ASSERT(raw_name != NULL);
4311 return Handle<String>(String::cast(*raw_name->handle()));
4312 }
4313}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004314
ager@chromium.org7c537e22008-10-16 08:43:32 +00004315
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004316void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004317 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004318 ASSERT(!is_illegal());
4319 ASSERT(!cgen_->has_cc());
4320 MacroAssembler* masm = cgen_->masm();
4321 Property* property = expression_->AsProperty();
4322 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004323 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004324 }
4325
4326 switch (type_) {
4327 case SLOT: {
4328 Comment cmnt(masm, "[ Load from Slot");
4329 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4330 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004331 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004332 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004333 }
4334
ager@chromium.org7c537e22008-10-16 08:43:32 +00004335 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004336 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004337 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004338 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004339 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004340 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4341 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004342 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004343 ASSERT(var == NULL || var->is_global());
4344 RelocInfo::Mode rmode = (var == NULL)
4345 ? RelocInfo::CODE_TARGET
4346 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004347 frame->CallCodeObject(ic, rmode, 0);
4348 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004349 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004350 }
4351
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004352 case KEYED: {
4353 // TODO(181): Implement inlined version of array indexing once
4354 // loop nesting is properly tracked on ARM.
4355 ASSERT(property != NULL);
4356 Variable* var = expression_->AsVariableProxy()->AsVariable();
4357 ASSERT(var == NULL || var->is_global());
4358 cgen_->EmitKeyedLoad(var != NULL);
4359 cgen_->frame()->EmitPush(r0);
4360 break;
4361 }
4362
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004363 default:
4364 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004365 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004366
4367 if (!persist_after_get_) {
4368 cgen_->UnloadReference(this);
4369 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004370}
4371
4372
ager@chromium.org7c537e22008-10-16 08:43:32 +00004373void Reference::SetValue(InitState init_state) {
4374 ASSERT(!is_illegal());
4375 ASSERT(!cgen_->has_cc());
4376 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004377 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004378 Property* property = expression_->AsProperty();
4379 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004380 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004381 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004382
ager@chromium.org7c537e22008-10-16 08:43:32 +00004383 switch (type_) {
4384 case SLOT: {
4385 Comment cmnt(masm, "[ Store to Slot");
4386 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004387 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004388 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004389 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004390 }
4391
ager@chromium.org7c537e22008-10-16 08:43:32 +00004392 case NAMED: {
4393 Comment cmnt(masm, "[ Store to named Property");
4394 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004395 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004396 Handle<String> name(GetName());
4397
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004398 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004399 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004400 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004401 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004402 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004403 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004404 break;
4405 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004406
ager@chromium.org7c537e22008-10-16 08:43:32 +00004407 case KEYED: {
4408 Comment cmnt(masm, "[ Store to keyed Property");
4409 Property* property = expression_->AsProperty();
4410 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004411 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004412
4413 // Call IC code.
4414 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4415 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004416 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004417 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004418 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004419 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004420 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004421 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004422
4423 default:
4424 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004425 }
4426}
4427
4428
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004429void FastNewClosureStub::Generate(MacroAssembler* masm) {
4430 // Clone the boilerplate in new space. Set the context to the
4431 // current context in cp.
4432 Label gc;
4433
4434 // Pop the boilerplate function from the stack.
4435 __ pop(r3);
4436
4437 // Attempt to allocate new JSFunction in new space.
4438 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4439 r0,
4440 r1,
4441 r2,
4442 &gc,
4443 TAG_OBJECT);
4444
4445 // Compute the function map in the current global context and set that
4446 // as the map of the allocated object.
4447 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4448 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4449 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4450 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4451
4452 // Clone the rest of the boilerplate fields. We don't have to update
4453 // the write barrier because the allocated object is in new space.
4454 for (int offset = kPointerSize;
4455 offset < JSFunction::kSize;
4456 offset += kPointerSize) {
4457 if (offset == JSFunction::kContextOffset) {
4458 __ str(cp, FieldMemOperand(r0, offset));
4459 } else {
4460 __ ldr(r1, FieldMemOperand(r3, offset));
4461 __ str(r1, FieldMemOperand(r0, offset));
4462 }
4463 }
4464
4465 // Return result. The argument boilerplate has been popped already.
4466 __ Ret();
4467
4468 // Create a new closure through the slower runtime call.
4469 __ bind(&gc);
4470 __ push(cp);
4471 __ push(r3);
4472 __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1);
4473}
4474
4475
4476void FastNewContextStub::Generate(MacroAssembler* masm) {
4477 // Try to allocate the context in new space.
4478 Label gc;
4479 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4480
4481 // Attempt to allocate the context in new space.
4482 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4483 r0,
4484 r1,
4485 r2,
4486 &gc,
4487 TAG_OBJECT);
4488
4489 // Load the function from the stack.
4490 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
4491
4492 // Setup the object header.
4493 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4494 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4495 __ mov(r2, Operand(length));
4496 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4497
4498 // Setup the fixed slots.
4499 __ mov(r1, Operand(Smi::FromInt(0)));
4500 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4501 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4502 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4503 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4504
4505 // Copy the global object from the surrounding context.
4506 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4507 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4508
4509 // Initialize the rest of the slots to undefined.
4510 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4511 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4512 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4513 }
4514
4515 // Remove the on-stack argument and return.
4516 __ mov(cp, r0);
4517 __ pop();
4518 __ Ret();
4519
4520 // Need to collect. Call into runtime system.
4521 __ bind(&gc);
4522 __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1);
4523}
4524
4525
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004526// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4527// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4528// (31 instead of 32).
4529static void CountLeadingZeros(
4530 MacroAssembler* masm,
4531 Register source,
4532 Register scratch,
4533 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004534#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004535 __ clz(zeros, source); // This instruction is only supported after ARM5.
4536#else
4537 __ mov(zeros, Operand(0));
4538 __ mov(scratch, source);
4539 // Top 16.
4540 __ tst(scratch, Operand(0xffff0000));
4541 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4542 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4543 // Top 8.
4544 __ tst(scratch, Operand(0xff000000));
4545 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4546 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4547 // Top 4.
4548 __ tst(scratch, Operand(0xf0000000));
4549 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4550 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4551 // Top 2.
4552 __ tst(scratch, Operand(0xc0000000));
4553 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4554 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4555 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004556 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004557 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4558#endif
4559}
4560
4561
4562// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4563// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4564// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4565// scratch register. Destroys the source register. No GC occurs during this
4566// stub so you don't have to set up the frame.
4567class ConvertToDoubleStub : public CodeStub {
4568 public:
4569 ConvertToDoubleStub(Register result_reg_1,
4570 Register result_reg_2,
4571 Register source_reg,
4572 Register scratch_reg)
4573 : result1_(result_reg_1),
4574 result2_(result_reg_2),
4575 source_(source_reg),
4576 zeros_(scratch_reg) { }
4577
4578 private:
4579 Register result1_;
4580 Register result2_;
4581 Register source_;
4582 Register zeros_;
4583
4584 // Minor key encoding in 16 bits.
4585 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4586 class OpBits: public BitField<Token::Value, 2, 14> {};
4587
4588 Major MajorKey() { return ConvertToDouble; }
4589 int MinorKey() {
4590 // Encode the parameters in a unique 16 bit value.
4591 return result1_.code() +
4592 (result2_.code() << 4) +
4593 (source_.code() << 8) +
4594 (zeros_.code() << 12);
4595 }
4596
4597 void Generate(MacroAssembler* masm);
4598
4599 const char* GetName() { return "ConvertToDoubleStub"; }
4600
4601#ifdef DEBUG
4602 void Print() { PrintF("ConvertToDoubleStub\n"); }
4603#endif
4604};
4605
4606
4607void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4608#ifndef BIG_ENDIAN_FLOATING_POINT
4609 Register exponent = result1_;
4610 Register mantissa = result2_;
4611#else
4612 Register exponent = result2_;
4613 Register mantissa = result1_;
4614#endif
4615 Label not_special;
4616 // Convert from Smi to integer.
4617 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4618 // Move sign bit from source to destination. This works because the sign bit
4619 // in the exponent word of the double has the same position and polarity as
4620 // the 2's complement sign bit in a Smi.
4621 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4622 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4623 // Subtract from 0 if source was negative.
4624 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4625 __ cmp(source_, Operand(1));
4626 __ b(gt, &not_special);
4627
4628 // We have -1, 0 or 1, which we treat specially.
4629 __ cmp(source_, Operand(0));
4630 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4631 static const uint32_t exponent_word_for_1 =
4632 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4633 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4634 // 1, 0 and -1 all have 0 for the second word.
4635 __ mov(mantissa, Operand(0));
4636 __ Ret();
4637
4638 __ bind(&not_special);
4639 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4640 // Gets the wrong answer for 0, but we already checked for that case above.
4641 CountLeadingZeros(masm, source_, mantissa, zeros_);
4642 // Compute exponent and or it into the exponent register.
4643 // We use result2 as a scratch register here.
4644 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4645 __ orr(exponent,
4646 exponent,
4647 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4648 // Shift up the source chopping the top bit off.
4649 __ add(zeros_, zeros_, Operand(1));
4650 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4651 __ mov(source_, Operand(source_, LSL, zeros_));
4652 // Compute lower part of fraction (last 12 bits).
4653 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4654 // And the top (top 20 bits).
4655 __ orr(exponent,
4656 exponent,
4657 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4658 __ Ret();
4659}
4660
4661
4662// This stub can convert a signed int32 to a heap number (double). It does
4663// not work for int32s that are in Smi range! No GC occurs during this stub
4664// so you don't have to set up the frame.
4665class WriteInt32ToHeapNumberStub : public CodeStub {
4666 public:
4667 WriteInt32ToHeapNumberStub(Register the_int,
4668 Register the_heap_number,
4669 Register scratch)
4670 : the_int_(the_int),
4671 the_heap_number_(the_heap_number),
4672 scratch_(scratch) { }
4673
4674 private:
4675 Register the_int_;
4676 Register the_heap_number_;
4677 Register scratch_;
4678
4679 // Minor key encoding in 16 bits.
4680 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4681 class OpBits: public BitField<Token::Value, 2, 14> {};
4682
4683 Major MajorKey() { return WriteInt32ToHeapNumber; }
4684 int MinorKey() {
4685 // Encode the parameters in a unique 16 bit value.
4686 return the_int_.code() +
4687 (the_heap_number_.code() << 4) +
4688 (scratch_.code() << 8);
4689 }
4690
4691 void Generate(MacroAssembler* masm);
4692
4693 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4694
4695#ifdef DEBUG
4696 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4697#endif
4698};
4699
4700
4701// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004702void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004703 Label max_negative_int;
4704 // the_int_ has the answer which is a signed int32 but not a Smi.
4705 // We test for the special value that has a different exponent. This test
4706 // has the neat side effect of setting the flags according to the sign.
4707 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004708 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004709 __ b(eq, &max_negative_int);
4710 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4711 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4712 uint32_t non_smi_exponent =
4713 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4714 __ mov(scratch_, Operand(non_smi_exponent));
4715 // Set the sign bit in scratch_ if the value was negative.
4716 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4717 // Subtract from 0 if the value was negative.
4718 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4719 // We should be masking the implict first digit of the mantissa away here,
4720 // but it just ends up combining harmlessly with the last digit of the
4721 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4722 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4723 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4724 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4725 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4726 __ str(scratch_, FieldMemOperand(the_heap_number_,
4727 HeapNumber::kExponentOffset));
4728 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4729 __ str(scratch_, FieldMemOperand(the_heap_number_,
4730 HeapNumber::kMantissaOffset));
4731 __ Ret();
4732
4733 __ bind(&max_negative_int);
4734 // The max negative int32 is stored as a positive number in the mantissa of
4735 // a double because it uses a sign bit instead of using two's complement.
4736 // The actual mantissa bits stored are all 0 because the implicit most
4737 // significant 1 bit is not stored.
4738 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4739 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4740 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4741 __ mov(ip, Operand(0));
4742 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4743 __ Ret();
4744}
4745
4746
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004747// Handle the case where the lhs and rhs are the same object.
4748// Equality is almost reflexive (everything but NaN), so this is a test
4749// for "identity and not NaN".
4750static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4751 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004752 Condition cc,
4753 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004754 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004755 Label heap_number, return_equal;
4756 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004757 __ cmp(r0, Operand(r1));
4758 __ b(ne, &not_identical);
4759
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004760 // The two objects are identical. If we know that one of them isn't NaN then
4761 // we now know they test equal.
4762 if (cc != eq || !never_nan_nan) {
4763 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004764
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004765 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4766 // so we do the second best thing - test it ourselves.
4767 // They are both equal and they are not both Smis so both of them are not
4768 // Smis. If it's not a heap number, then return equal.
4769 if (cc == lt || cc == gt) {
4770 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004771 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004772 } else {
4773 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4774 __ b(eq, &heap_number);
4775 // Comparing JS objects with <=, >= is complicated.
4776 if (cc != eq) {
4777 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4778 __ b(ge, slow);
4779 // Normally here we fall through to return_equal, but undefined is
4780 // special: (undefined == undefined) == true, but
4781 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4782 if (cc == le || cc == ge) {
4783 __ cmp(r4, Operand(ODDBALL_TYPE));
4784 __ b(ne, &return_equal);
4785 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4786 __ cmp(r0, Operand(r2));
4787 __ b(ne, &return_equal);
4788 if (cc == le) {
4789 // undefined <= undefined should fail.
4790 __ mov(r0, Operand(GREATER));
4791 } else {
4792 // undefined >= undefined should fail.
4793 __ mov(r0, Operand(LESS));
4794 }
4795 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004796 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004797 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004798 }
4799 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004800
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004801 __ bind(&return_equal);
4802 if (cc == lt) {
4803 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4804 } else if (cc == gt) {
4805 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4806 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004807 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004808 }
4809 __ mov(pc, Operand(lr)); // Return.
4810
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004811 if (cc != eq || !never_nan_nan) {
4812 // For less and greater we don't have to check for NaN since the result of
4813 // x < x is false regardless. For the others here is some code to check
4814 // for NaN.
4815 if (cc != lt && cc != gt) {
4816 __ bind(&heap_number);
4817 // It is a heap number, so return non-equal if it's NaN and equal if it's
4818 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004819
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004820 // The representation of NaN values has all exponent bits (52..62) set,
4821 // and not all mantissa bits (0..51) clear.
4822 // Read top bits of double representation (second word of value).
4823 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4824 // Test that exponent bits are all set.
4825 __ and_(r3, r2, Operand(exp_mask_reg));
4826 __ cmp(r3, Operand(exp_mask_reg));
4827 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004828
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004829 // Shift out flag and all exponent bits, retaining only mantissa.
4830 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4831 // Or with all low-bits of mantissa.
4832 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4833 __ orr(r0, r3, Operand(r2), SetCC);
4834 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004835 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
4836 // not (it's a NaN). For <= and >= we need to load r0 with the failing
4837 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004838 if (cc != eq) {
4839 // All-zero means Infinity means equal.
4840 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4841 if (cc == le) {
4842 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4843 } else {
4844 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4845 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004846 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004847 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004848 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004849 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004850 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004851
4852 __ bind(&not_identical);
4853}
4854
4855
4856// See comment at call site.
4857static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004858 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004859 Label* slow,
4860 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004861 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004862 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004863 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004864
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004865 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004866 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4867 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004868 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004869 // succeed. Return non-equal (r0 is already not zero)
4870 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4871 } else {
4872 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4873 // the runtime.
4874 __ b(ne, slow);
4875 }
4876
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004877 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004878 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004879 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004880 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004881 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
4882 __ vmov(s15, r7);
4883 __ vcvt(d7, s15);
4884 // Load the double from rhs, tagged HeapNumber r0, to d6.
4885 __ sub(r7, r0, Operand(kHeapObjectTag));
4886 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004887 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004888 __ push(lr);
4889 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004890 __ mov(r7, Operand(r1));
4891 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4892 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004893 // Load rhs to a double in r0, r1.
4894 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4895 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4896 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004897 }
4898
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004899 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004900 // since it's a smi.
4901 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004902
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004903 __ bind(&rhs_is_smi);
4904 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004905 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4906 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004907 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004908 // succeed. Return non-equal.
4909 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4910 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4911 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004912 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004913 // the runtime.
4914 __ b(ne, slow);
4915 }
4916
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004917 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004918 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004919 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004920 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004921 // Load the double from lhs, tagged HeapNumber r1, to d7.
4922 __ sub(r7, r1, Operand(kHeapObjectTag));
4923 __ vldr(d7, r7, HeapNumber::kValueOffset);
4924 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
4925 __ vmov(s13, r7);
4926 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004927 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004928 __ push(lr);
4929 // Load lhs to a double in r2, r3.
4930 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4931 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4932 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004933 __ mov(r7, Operand(r0));
4934 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4935 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004936 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004937 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004938 // Fall through to both_loaded_as_doubles.
4939}
4940
4941
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004942void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004943 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004944 Register rhs_exponent = exp_first ? r0 : r1;
4945 Register lhs_exponent = exp_first ? r2 : r3;
4946 Register rhs_mantissa = exp_first ? r1 : r0;
4947 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004948 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004949 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004950
4951 Register exp_mask_reg = r5;
4952
4953 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004954 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4955 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004956 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004957 __ mov(r4,
4958 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4959 SetCC);
4960 __ b(ne, &one_is_nan);
4961 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004962 __ b(ne, &one_is_nan);
4963
4964 __ bind(lhs_not_nan);
4965 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4966 __ bind(&lhs_not_nan_exp_mask_is_loaded);
4967 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4968 __ cmp(r4, Operand(exp_mask_reg));
4969 __ b(ne, &neither_is_nan);
4970 __ mov(r4,
4971 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4972 SetCC);
4973 __ b(ne, &one_is_nan);
4974 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004975 __ b(eq, &neither_is_nan);
4976
4977 __ bind(&one_is_nan);
4978 // NaN comparisons always fail.
4979 // Load whatever we need in r0 to make the comparison fail.
4980 if (cc == lt || cc == le) {
4981 __ mov(r0, Operand(GREATER));
4982 } else {
4983 __ mov(r0, Operand(LESS));
4984 }
4985 __ mov(pc, Operand(lr)); // Return.
4986
4987 __ bind(&neither_is_nan);
4988}
4989
4990
4991// See comment at call site.
4992static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4993 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004994 Register rhs_exponent = exp_first ? r0 : r1;
4995 Register lhs_exponent = exp_first ? r2 : r3;
4996 Register rhs_mantissa = exp_first ? r1 : r0;
4997 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004998
4999 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5000 if (cc == eq) {
5001 // Doubles are not equal unless they have the same bit pattern.
5002 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005003 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5004 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005005 // Return non-zero if the numbers are unequal.
5006 __ mov(pc, Operand(lr), LeaveCC, ne);
5007
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005008 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005009 // If exponents are equal then return 0.
5010 __ mov(pc, Operand(lr), LeaveCC, eq);
5011
5012 // Exponents are unequal. The only way we can return that the numbers
5013 // are equal is if one is -0 and the other is 0. We already dealt
5014 // with the case where both are -0 or both are 0.
5015 // We start by seeing if the mantissas (that are equal) or the bottom
5016 // 31 bits of the rhs exponent are non-zero. If so we return not
5017 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005018 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005019 __ mov(r0, Operand(r4), LeaveCC, ne);
5020 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5021 // Now they are equal if and only if the lhs exponent is zero in its
5022 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005023 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005024 __ mov(pc, Operand(lr));
5025 } else {
5026 // Call a native function to do a comparison between two non-NaNs.
5027 // Call C routine that may not cause GC or other trouble.
5028 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5029 __ Jump(r5); // Tail call.
5030 }
5031}
5032
5033
5034// See comment at call site.
5035static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5036 // If either operand is a JSObject or an oddball value, then they are
5037 // not equal since their pointers are different.
5038 // There is no test for undetectability in strict equality.
5039 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5040 Label first_non_object;
5041 // Get the type of the first operand into r2 and compare it with
5042 // FIRST_JS_OBJECT_TYPE.
5043 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5044 __ b(lt, &first_non_object);
5045
5046 // Return non-zero (r0 is not zero)
5047 Label return_not_equal;
5048 __ bind(&return_not_equal);
5049 __ mov(pc, Operand(lr)); // Return.
5050
5051 __ bind(&first_non_object);
5052 // Check for oddballs: true, false, null, undefined.
5053 __ cmp(r2, Operand(ODDBALL_TYPE));
5054 __ b(eq, &return_not_equal);
5055
5056 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5057 __ b(ge, &return_not_equal);
5058
5059 // Check for oddballs: true, false, null, undefined.
5060 __ cmp(r3, Operand(ODDBALL_TYPE));
5061 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005062
5063 // Now that we have the types we might as well check for symbol-symbol.
5064 // Ensure that no non-strings have the symbol bit set.
5065 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5066 ASSERT(kSymbolTag != 0);
5067 __ and_(r2, r2, Operand(r3));
5068 __ tst(r2, Operand(kIsSymbolMask));
5069 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005070}
5071
5072
5073// See comment at call site.
5074static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5075 Label* both_loaded_as_doubles,
5076 Label* not_heap_numbers,
5077 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005078 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005079 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005080 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5081 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005082 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5083
5084 // Both are heap numbers. Load them up then jump to the code we have
5085 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005086 if (CpuFeatures::IsSupported(VFP3)) {
5087 CpuFeatures::Scope scope(VFP3);
5088 __ sub(r7, r0, Operand(kHeapObjectTag));
5089 __ vldr(d6, r7, HeapNumber::kValueOffset);
5090 __ sub(r7, r1, Operand(kHeapObjectTag));
5091 __ vldr(d7, r7, HeapNumber::kValueOffset);
5092 } else {
5093 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5094 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5095 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5096 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5097 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005098 __ jmp(both_loaded_as_doubles);
5099}
5100
5101
5102// Fast negative check for symbol-to-symbol equality.
5103static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5104 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005105 // Ensure that no non-strings have the symbol bit set.
5106 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5107 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005108 __ tst(r2, Operand(kIsSymbolMask));
5109 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005110 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5111 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005112 __ tst(r3, Operand(kIsSymbolMask));
5113 __ b(eq, slow);
5114
5115 // Both are symbols. We already checked they weren't the same pointer
5116 // so they are not equal.
5117 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5118 __ mov(pc, Operand(lr)); // Return.
5119}
5120
5121
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005122// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5123// On exit r0 is 0, positive or negative to indicate the result of
5124// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005125void CompareStub::Generate(MacroAssembler* masm) {
5126 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005127 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005128
5129 // NOTICE! This code is only reached after a smi-fast-case check, so
5130 // it is certain that at least one operand isn't a smi.
5131
5132 // Handle the case where the objects are identical. Either returns the answer
5133 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005134 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005135
5136 // If either is a Smi (we know that not both are), then they can only
5137 // be strictly equal if the other is a HeapNumber.
5138 ASSERT_EQ(0, kSmiTag);
5139 ASSERT_EQ(0, Smi::FromInt(0));
5140 __ and_(r2, r0, Operand(r1));
5141 __ tst(r2, Operand(kSmiTagMask));
5142 __ b(ne, &not_smis);
5143 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5144 // 1) Return the answer.
5145 // 2) Go to slow.
5146 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005147 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005148 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005149 // comparison. If VFP3 is supported the double values of the numbers have
5150 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5151 // into r0, r1, r2, and r3.
5152 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005153
5154 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005155 // The arguments have been converted to doubles and stored in d6 and d7, if
5156 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005157 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005158 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005159 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005160 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005161 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005162 __ vcmp(d7, d6);
5163 __ vmrs(pc); // Move vector status bits to normal status bits.
5164 Label nan;
5165 __ b(vs, &nan);
5166 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5167 __ mov(r0, Operand(LESS), LeaveCC, lt);
5168 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5169 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005170
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005171 __ bind(&nan);
5172 // If one of the sides was a NaN then the v flag is set. Load r0 with
5173 // whatever it takes to make the comparison fail, since comparisons with NaN
5174 // always fail.
5175 if (cc_ == lt || cc_ == le) {
5176 __ mov(r0, Operand(GREATER));
5177 } else {
5178 __ mov(r0, Operand(LESS));
5179 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005180 __ mov(pc, Operand(lr));
5181 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005182 // Checks for NaN in the doubles we have loaded. Can return the answer or
5183 // fall through if neither is a NaN. Also binds lhs_not_nan.
5184 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005185 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5186 // answer. Never falls through.
5187 EmitTwoNonNanDoubleComparison(masm, cc_);
5188 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005189
5190 __ bind(&not_smis);
5191 // At this point we know we are dealing with two different objects,
5192 // and neither of them is a Smi. The objects are in r0 and r1.
5193 if (strict_) {
5194 // This returns non-equal for some object types, or falls through if it
5195 // was not lucky.
5196 EmitStrictTwoHeapObjectCompare(masm);
5197 }
5198
5199 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005200 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005201 // Check for heap-number-heap-number comparison. Can jump to slow case,
5202 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5203 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005204 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005205 EmitCheckForTwoHeapNumbers(masm,
5206 &both_loaded_as_doubles,
5207 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005208 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005209
5210 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005211 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5212 // symbols.
5213 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005214 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5215 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005216 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005217 }
5218
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005219 // Check for both being sequential ASCII strings, and inline if that is the
5220 // case.
5221 __ bind(&flat_string_check);
5222
5223 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5224
5225 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5226 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5227 r1,
5228 r0,
5229 r2,
5230 r3,
5231 r4,
5232 r5);
5233 // Never falls through to here.
5234
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005235 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005236
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005237 __ push(r1);
5238 __ push(r0);
5239 // Figure out which native to call and setup the arguments.
5240 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005241 if (cc_ == eq) {
5242 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5243 } else {
5244 native = Builtins::COMPARE;
5245 int ncr; // NaN compare result
5246 if (cc_ == lt || cc_ == le) {
5247 ncr = GREATER;
5248 } else {
5249 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5250 ncr = LESS;
5251 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005252 __ mov(r0, Operand(Smi::FromInt(ncr)));
5253 __ push(r0);
5254 }
5255
5256 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5257 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005258 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005259}
5260
5261
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005262// Allocates a heap number or jumps to the label if the young space is full and
5263// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005264static void AllocateHeapNumber(
5265 MacroAssembler* masm,
5266 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005267 Register result, // The tagged address of the new heap number.
5268 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005269 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005270 // Allocate an object in the heap for the heap number and tag it as a heap
5271 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005272 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
5273 result,
5274 scratch1,
5275 scratch2,
5276 need_gc,
5277 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005278
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005279 // Get heap number map and store it in the allocated object.
5280 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
5281 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005282}
5283
5284
5285// We fall into this code if the operands were Smis, but the result was
5286// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005287// the operands were not both Smi. The operands are in r0 and r1. In order
5288// to call the C-implemented binary fp operation routines we need to end up
5289// with the double precision floating point operands in r0 and r1 (for the
5290// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005291static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5292 Label* not_smi,
5293 const Builtins::JavaScript& builtin,
5294 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005295 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005296 Label slow, slow_pop_2_first, do_the_call;
5297 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5298 // Smi-smi case (overflow).
5299 // Since both are Smis there is no heap number to overwrite, so allocate.
5300 // The new heap number is in r5. r6 and r7 are scratch.
5301 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005302
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005303 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5304 // using registers d7 and d6 for the double values.
5305 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) &&
5306 Token::MOD != operation;
5307 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005308 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005309 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5310 __ vmov(s15, r7);
5311 __ vcvt(d7, s15);
5312 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5313 __ vmov(s13, r7);
5314 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005315 } else {
5316 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5317 __ mov(r7, Operand(r0));
5318 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5319 __ push(lr);
5320 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5321 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5322 __ mov(r7, Operand(r1));
5323 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5324 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5325 __ pop(lr);
5326 }
5327
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005328 __ jmp(&do_the_call); // Tail call. No return.
5329
5330 // We jump to here if something goes wrong (one param is not a number of any
5331 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005332 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005333
5334 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005335 __ push(r1);
5336 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005337
5338 if (Token::ADD == operation) {
5339 // Test for string arguments before calling runtime.
5340 // r1 : first argument
5341 // r0 : second argument
5342 // sp[0] : second argument
5343 // sp[1] : first argument
5344
5345 Label not_strings, not_string1, string1;
5346 __ tst(r1, Operand(kSmiTagMask));
5347 __ b(eq, &not_string1);
5348 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5349 __ b(ge, &not_string1);
5350
5351 // First argument is a a string, test second.
5352 __ tst(r0, Operand(kSmiTagMask));
5353 __ b(eq, &string1);
5354 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5355 __ b(ge, &string1);
5356
5357 // First and second argument are strings.
5358 __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1);
5359
5360 // Only first argument is a string.
5361 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005362 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5363
5364 // First argument was not a string, test second.
5365 __ bind(&not_string1);
5366 __ tst(r0, Operand(kSmiTagMask));
5367 __ b(eq, &not_strings);
5368 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5369 __ b(ge, &not_strings);
5370
5371 // Only second argument is a string.
5372 __ b(&not_strings);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005373 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5374
5375 __ bind(&not_strings);
5376 }
5377
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005378 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005379
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005380 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005381 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005382 if (mode == NO_OVERWRITE) {
5383 // In the case where there is no chance of an overwritable float we may as
5384 // well do the allocation immediately while r0 and r1 are untouched.
5385 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5386 }
5387
5388 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005389 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005390 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5391 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005392 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005393 if (mode == OVERWRITE_RIGHT) {
5394 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5395 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005396 if (use_fp_registers) {
5397 CpuFeatures::Scope scope(VFP3);
5398 // Load the double from tagged HeapNumber r0 to d7.
5399 __ sub(r7, r0, Operand(kHeapObjectTag));
5400 __ vldr(d7, r7, HeapNumber::kValueOffset);
5401 } else {
5402 // Calling convention says that second double is in r2 and r3.
5403 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5404 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5405 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005406 __ jmp(&finished_loading_r0);
5407 __ bind(&r0_is_smi);
5408 if (mode == OVERWRITE_RIGHT) {
5409 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005410 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005411 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005412
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005413 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005414 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005415 // Convert smi in r0 to double in d7.
5416 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5417 __ vmov(s15, r7);
5418 __ vcvt(d7, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005419 } else {
5420 // Write Smi from r0 to r3 and r2 in double format.
5421 __ mov(r7, Operand(r0));
5422 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5423 __ push(lr);
5424 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5425 __ pop(lr);
5426 }
5427
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005428 __ bind(&finished_loading_r0);
5429
5430 // Move r1 to a double in r0-r1.
5431 __ tst(r1, Operand(kSmiTagMask));
5432 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5433 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5434 __ b(ne, &slow);
5435 if (mode == OVERWRITE_LEFT) {
5436 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005437 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005438 if (use_fp_registers) {
5439 CpuFeatures::Scope scope(VFP3);
5440 // Load the double from tagged HeapNumber r1 to d6.
5441 __ sub(r7, r1, Operand(kHeapObjectTag));
5442 __ vldr(d6, r7, HeapNumber::kValueOffset);
5443 } else {
5444 // Calling convention says that first double is in r0 and r1.
5445 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
5446 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5447 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005448 __ jmp(&finished_loading_r1);
5449 __ bind(&r1_is_smi);
5450 if (mode == OVERWRITE_LEFT) {
5451 // We can't overwrite a Smi so get address of new heap number into r5.
5452 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5453 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005454
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005455 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005456 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005457 // Convert smi in r1 to double in d6.
5458 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5459 __ vmov(s13, r7);
5460 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005461 } else {
5462 // Write Smi from r1 to r1 and r0 in double format.
5463 __ mov(r7, Operand(r1));
5464 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5465 __ push(lr);
5466 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5467 __ pop(lr);
5468 }
5469
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005470 __ bind(&finished_loading_r1);
5471
5472 __ bind(&do_the_call);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005473 // If we are inlining the operation using VFP3 instructions for
5474 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
5475 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005476 CpuFeatures::Scope scope(VFP3);
5477 // ARMv7 VFP3 instructions to implement
5478 // double precision, add, subtract, multiply, divide.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005479
5480 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005481 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005482 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005483 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005484 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005485 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005486 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005487 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005488 } else {
5489 UNREACHABLE();
5490 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005491 __ sub(r0, r5, Operand(kHeapObjectTag));
5492 __ vstr(d5, r0, HeapNumber::kValueOffset);
5493 __ add(r0, r0, Operand(kHeapObjectTag));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005494 __ mov(pc, lr);
5495 return;
5496 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005497
5498 // If we did not inline the operation, then the arguments are in:
5499 // r0: Left value (least significant part of mantissa).
5500 // r1: Left value (sign, exponent, top of mantissa).
5501 // r2: Right value (least significant part of mantissa).
5502 // r3: Right value (sign, exponent, top of mantissa).
5503 // r5: Address of heap number for result.
5504
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005505 __ push(lr); // For later.
5506 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005507 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005508 // Call C routine that may not cause GC or other trouble.
5509 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005510 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005511 __ pop(r4); // Address of heap number.
5512 __ cmp(r4, Operand(Smi::FromInt(0)));
5513 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005514 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005515#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005516 // Double returned in fp coprocessor register 0 and 1, encoded as register
5517 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5518 // substract the tag from r4.
5519 __ sub(r5, r4, Operand(kHeapObjectTag));
5520 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5521#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005522 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005523 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005524 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005525#endif
5526 __ mov(r0, Operand(r4));
5527 // And we are done.
5528 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005529}
5530
5531
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005532// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005533// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005534// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5535// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005536// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5537// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005538static void GetInt32(MacroAssembler* masm,
5539 Register source,
5540 Register dest,
5541 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005542 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005543 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005544 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005545 // Get exponent word.
5546 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5547 // Get exponent alone in scratch2.
5548 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005549 // Load dest with zero. We use this either for the final shift or
5550 // for the answer.
5551 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005552 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005553 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5554 // the exponent that we are fastest at and also the highest exponent we can
5555 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005556 const uint32_t non_smi_exponent =
5557 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5558 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005559 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5560 __ b(eq, &right_exponent);
5561 // If the exponent is higher than that then go to slow case. This catches
5562 // numbers that don't fit in a signed int32, infinities and NaNs.
5563 __ b(gt, slow);
5564
5565 // We know the exponent is smaller than 30 (biased). If it is less than
5566 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5567 // it rounds to zero.
5568 const uint32_t zero_exponent =
5569 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5570 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5571 // Dest already has a Smi zero.
5572 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005573 if (!CpuFeatures::IsSupported(VFP3)) {
5574 // We have a shifted exponent between 0 and 30 in scratch2.
5575 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5576 // We now have the exponent in dest. Subtract from 30 to get
5577 // how much to shift down.
5578 __ rsb(dest, dest, Operand(30));
5579 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005580 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005581 if (CpuFeatures::IsSupported(VFP3)) {
5582 CpuFeatures::Scope scope(VFP3);
5583 // ARMv7 VFP3 instructions implementing double precision to integer
5584 // conversion using round to zero.
5585 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005586 __ vmov(d7, scratch2, scratch);
5587 __ vcvt(s15, d7);
5588 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005589 } else {
5590 // Get the top bits of the mantissa.
5591 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5592 // Put back the implicit 1.
5593 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5594 // Shift up the mantissa bits to take up the space the exponent used to
5595 // take. We just orred in the implicit bit so that took care of one and
5596 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5597 // distance.
5598 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5599 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5600 // Put sign in zero flag.
5601 __ tst(scratch, Operand(HeapNumber::kSignMask));
5602 // Get the second half of the double. For some exponents we don't
5603 // actually need this because the bits get shifted out again, but
5604 // it's probably slower to test than just to do it.
5605 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5606 // Shift down 22 bits to get the last 10 bits.
5607 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5608 // Move down according to the exponent.
5609 __ mov(dest, Operand(scratch, LSR, dest));
5610 // Fix sign if sign bit was set.
5611 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5612 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005613 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005614}
5615
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005616// For bitwise ops where the inputs are not both Smis we here try to determine
5617// whether both inputs are either Smis or at least heap numbers that can be
5618// represented by a 32 bit signed value. We truncate towards zero as required
5619// by the ES spec. If this is the case we do the bitwise op and see if the
5620// result is a Smi. If so, great, otherwise we try to find a heap number to
5621// write the answer into (either by allocating or by overwriting).
5622// On entry the operands are in r0 and r1. On exit the answer is in r0.
5623void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5624 Label slow, result_not_a_smi;
5625 Label r0_is_smi, r1_is_smi;
5626 Label done_checking_r0, done_checking_r1;
5627
5628 __ tst(r1, Operand(kSmiTagMask));
5629 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5630 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5631 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005632 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005633 __ jmp(&done_checking_r1);
5634 __ bind(&r1_is_smi);
5635 __ mov(r3, Operand(r1, ASR, 1));
5636 __ bind(&done_checking_r1);
5637
5638 __ tst(r0, Operand(kSmiTagMask));
5639 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5640 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5641 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005642 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005643 __ jmp(&done_checking_r0);
5644 __ bind(&r0_is_smi);
5645 __ mov(r2, Operand(r0, ASR, 1));
5646 __ bind(&done_checking_r0);
5647
5648 // r0 and r1: Original operands (Smi or heap numbers).
5649 // r2 and r3: Signed int32 operands.
5650 switch (op_) {
5651 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5652 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5653 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5654 case Token::SAR:
5655 // Use only the 5 least significant bits of the shift count.
5656 __ and_(r2, r2, Operand(0x1f));
5657 __ mov(r2, Operand(r3, ASR, r2));
5658 break;
5659 case Token::SHR:
5660 // Use only the 5 least significant bits of the shift count.
5661 __ and_(r2, r2, Operand(0x1f));
5662 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5663 // SHR is special because it is required to produce a positive answer.
5664 // The code below for writing into heap numbers isn't capable of writing
5665 // the register as an unsigned int so we go to slow case if we hit this
5666 // case.
5667 __ b(mi, &slow);
5668 break;
5669 case Token::SHL:
5670 // Use only the 5 least significant bits of the shift count.
5671 __ and_(r2, r2, Operand(0x1f));
5672 __ mov(r2, Operand(r3, LSL, r2));
5673 break;
5674 default: UNREACHABLE();
5675 }
5676 // check that the *signed* result fits in a smi
5677 __ add(r3, r2, Operand(0x40000000), SetCC);
5678 __ b(mi, &result_not_a_smi);
5679 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5680 __ Ret();
5681
5682 Label have_to_allocate, got_a_heap_number;
5683 __ bind(&result_not_a_smi);
5684 switch (mode_) {
5685 case OVERWRITE_RIGHT: {
5686 __ tst(r0, Operand(kSmiTagMask));
5687 __ b(eq, &have_to_allocate);
5688 __ mov(r5, Operand(r0));
5689 break;
5690 }
5691 case OVERWRITE_LEFT: {
5692 __ tst(r1, Operand(kSmiTagMask));
5693 __ b(eq, &have_to_allocate);
5694 __ mov(r5, Operand(r1));
5695 break;
5696 }
5697 case NO_OVERWRITE: {
5698 // Get a new heap number in r5. r6 and r7 are scratch.
5699 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5700 }
5701 default: break;
5702 }
5703 __ bind(&got_a_heap_number);
5704 // r2: Answer as signed int32.
5705 // r5: Heap number to write answer into.
5706
5707 // Nothing can go wrong now, so move the heap number to r0, which is the
5708 // result.
5709 __ mov(r0, Operand(r5));
5710
5711 // Tail call that writes the int32 in r2 to the heap number in r0, using
5712 // r3 as scratch. r0 is preserved and returned.
5713 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5714 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5715
5716 if (mode_ != NO_OVERWRITE) {
5717 __ bind(&have_to_allocate);
5718 // Get a new heap number in r5. r6 and r7 are scratch.
5719 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5720 __ jmp(&got_a_heap_number);
5721 }
5722
5723 // If all else failed then we go to the runtime system.
5724 __ bind(&slow);
5725 __ push(r1); // restore stack
5726 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005727 switch (op_) {
5728 case Token::BIT_OR:
5729 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5730 break;
5731 case Token::BIT_AND:
5732 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5733 break;
5734 case Token::BIT_XOR:
5735 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5736 break;
5737 case Token::SAR:
5738 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5739 break;
5740 case Token::SHR:
5741 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5742 break;
5743 case Token::SHL:
5744 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5745 break;
5746 default:
5747 UNREACHABLE();
5748 }
5749}
5750
5751
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005752// Can we multiply by x with max two shifts and an add.
5753// This answers yes to all integers from 2 to 10.
5754static bool IsEasyToMultiplyBy(int x) {
5755 if (x < 2) return false; // Avoid special cases.
5756 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5757 if (IsPowerOf2(x)) return true; // Simple shift.
5758 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5759 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5760 return false;
5761}
5762
5763
5764// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5765// Source and destination may be the same register. This routine does
5766// not set carry and overflow the way a mul instruction would.
5767static void MultiplyByKnownInt(MacroAssembler* masm,
5768 Register source,
5769 Register destination,
5770 int known_int) {
5771 if (IsPowerOf2(known_int)) {
5772 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5773 } else if (PopCountLessThanEqual2(known_int)) {
5774 int first_bit = BitPosition(known_int);
5775 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5776 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5777 if (first_bit != 0) {
5778 __ mov(destination, Operand(destination, LSL, first_bit));
5779 }
5780 } else {
5781 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5782 int the_bit = BitPosition(known_int + 1);
5783 __ rsb(destination, source, Operand(source, LSL, the_bit));
5784 }
5785}
5786
5787
5788// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5789// a register for the cases where it doesn't know a good trick, and may deliver
5790// a result that needs shifting.
5791static void MultiplyByKnownInt2(
5792 MacroAssembler* masm,
5793 Register result,
5794 Register source,
5795 Register known_int_register, // Smi tagged.
5796 int known_int,
5797 int* required_shift) { // Including Smi tag shift
5798 switch (known_int) {
5799 case 3:
5800 __ add(result, source, Operand(source, LSL, 1));
5801 *required_shift = 1;
5802 break;
5803 case 5:
5804 __ add(result, source, Operand(source, LSL, 2));
5805 *required_shift = 1;
5806 break;
5807 case 6:
5808 __ add(result, source, Operand(source, LSL, 1));
5809 *required_shift = 2;
5810 break;
5811 case 7:
5812 __ rsb(result, source, Operand(source, LSL, 3));
5813 *required_shift = 1;
5814 break;
5815 case 9:
5816 __ add(result, source, Operand(source, LSL, 3));
5817 *required_shift = 1;
5818 break;
5819 case 10:
5820 __ add(result, source, Operand(source, LSL, 2));
5821 *required_shift = 2;
5822 break;
5823 default:
5824 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5825 __ mul(result, source, known_int_register);
5826 *required_shift = 0;
5827 }
5828}
5829
5830
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005831const char* GenericBinaryOpStub::GetName() {
5832 if (name_ != NULL) return name_;
5833 const int len = 100;
5834 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5835 if (name_ == NULL) return "OOM";
5836 const char* op_name = Token::Name(op_);
5837 const char* overwrite_name;
5838 switch (mode_) {
5839 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5840 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5841 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5842 default: overwrite_name = "UnknownOverwrite"; break;
5843 }
5844
5845 OS::SNPrintF(Vector<char>(name_, len),
5846 "GenericBinaryOpStub_%s_%s%s",
5847 op_name,
5848 overwrite_name,
5849 specialized_on_rhs_ ? "_ConstantRhs" : 0);
5850 return name_;
5851}
5852
5853
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005854void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5855 // r1 : x
5856 // r0 : y
5857 // result : r0
5858
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005859 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5860 // tell us that.
5861 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5862
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005863 switch (op_) {
5864 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005865 Label not_smi;
5866 // Fast path.
5867 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005868 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005869 __ b(ne, &not_smi);
5870 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5871 // Return if no overflow.
5872 __ Ret(vc);
5873 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5874
5875 HandleBinaryOpSlowCases(masm,
5876 &not_smi,
5877 Builtins::ADD,
5878 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005879 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005880 break;
5881 }
5882
5883 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005884 Label not_smi;
5885 // Fast path.
5886 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005887 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005888 __ b(ne, &not_smi);
5889 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5890 // Return if no overflow.
5891 __ Ret(vc);
5892 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5893
5894 HandleBinaryOpSlowCases(masm,
5895 &not_smi,
5896 Builtins::SUB,
5897 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005898 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005899 break;
5900 }
5901
5902 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005903 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005904 ASSERT(kSmiTag == 0); // adjust code below
5905 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005906 __ b(ne, &not_smi);
5907 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005908 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005909 // Do multiplication
5910 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5911 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005912 __ mov(ip, Operand(r3, ASR, 31));
5913 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5914 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005915 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005916 __ tst(r3, Operand(r3));
5917 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005918 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005919 // We need -0 if we were multiplying a negative number with 0 to get 0.
5920 // We know one of them was zero.
5921 __ add(r2, r0, Operand(r1), SetCC);
5922 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5923 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5924 // Slow case. We fall through here if we multiplied a negative number
5925 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005926 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005927
5928 HandleBinaryOpSlowCases(masm,
5929 &not_smi,
5930 Builtins::MUL,
5931 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005932 mode_);
5933 break;
5934 }
5935
5936 case Token::DIV:
5937 case Token::MOD: {
5938 Label not_smi;
5939 if (specialized_on_rhs_) {
5940 Label smi_is_unsuitable;
5941 __ BranchOnNotSmi(r1, &not_smi);
5942 if (IsPowerOf2(constant_rhs_)) {
5943 if (op_ == Token::MOD) {
5944 __ and_(r0,
5945 r1,
5946 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5947 SetCC);
5948 // We now have the answer, but if the input was negative we also
5949 // have the sign bit. Our work is done if the result is
5950 // positive or zero:
5951 __ Ret(pl);
5952 // A mod of a negative left hand side must return a negative number.
5953 // Unfortunately if the answer is 0 then we must return -0. And we
5954 // already optimistically trashed r0 so we may need to restore it.
5955 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5956 // Next two instructions are conditional on the answer being -0.
5957 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5958 __ b(eq, &smi_is_unsuitable);
5959 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5960 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5961 } else {
5962 ASSERT(op_ == Token::DIV);
5963 __ tst(r1,
5964 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5965 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5966 int shift = 0;
5967 int d = constant_rhs_;
5968 while ((d & 1) == 0) {
5969 d >>= 1;
5970 shift++;
5971 }
5972 __ mov(r0, Operand(r1, LSR, shift));
5973 __ bic(r0, r0, Operand(kSmiTagMask));
5974 }
5975 } else {
5976 // Not a power of 2.
5977 __ tst(r1, Operand(0x80000000u));
5978 __ b(ne, &smi_is_unsuitable);
5979 // Find a fixed point reciprocal of the divisor so we can divide by
5980 // multiplying.
5981 double divisor = 1.0 / constant_rhs_;
5982 int shift = 32;
5983 double scale = 4294967296.0; // 1 << 32.
5984 uint32_t mul;
5985 // Maximise the precision of the fixed point reciprocal.
5986 while (true) {
5987 mul = static_cast<uint32_t>(scale * divisor);
5988 if (mul >= 0x7fffffff) break;
5989 scale *= 2.0;
5990 shift++;
5991 }
5992 mul++;
5993 __ mov(r2, Operand(mul));
5994 __ umull(r3, r2, r2, r1);
5995 __ mov(r2, Operand(r2, LSR, shift - 31));
5996 // r2 is r1 / rhs. r2 is not Smi tagged.
5997 // r0 is still the known rhs. r0 is Smi tagged.
5998 // r1 is still the unkown lhs. r1 is Smi tagged.
5999 int required_r4_shift = 0; // Including the Smi tag shift of 1.
6000 // r4 = r2 * r0.
6001 MultiplyByKnownInt2(masm,
6002 r4,
6003 r2,
6004 r0,
6005 constant_rhs_,
6006 &required_r4_shift);
6007 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
6008 if (op_ == Token::DIV) {
6009 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
6010 __ b(ne, &smi_is_unsuitable); // There was a remainder.
6011 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6012 } else {
6013 ASSERT(op_ == Token::MOD);
6014 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
6015 }
6016 }
6017 __ Ret();
6018 __ bind(&smi_is_unsuitable);
6019 } else {
6020 __ jmp(&not_smi);
6021 }
6022 HandleBinaryOpSlowCases(masm,
6023 &not_smi,
6024 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
6025 op_,
6026 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006027 break;
6028 }
6029
6030 case Token::BIT_OR:
6031 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006032 case Token::BIT_XOR:
6033 case Token::SAR:
6034 case Token::SHR:
6035 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006036 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006037 ASSERT(kSmiTag == 0); // adjust code below
6038 __ tst(r2, Operand(kSmiTagMask));
6039 __ b(ne, &slow);
6040 switch (op_) {
6041 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
6042 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
6043 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006044 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006045 // Remove tags from right operand.
6046 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
6047 // Use only the 5 least significant bits of the shift count.
6048 __ and_(r2, r2, Operand(0x1f));
6049 __ mov(r0, Operand(r1, ASR, r2));
6050 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006051 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006052 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006053 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006054 // Remove tags from operands. We can't do this on a 31 bit number
6055 // because then the 0s get shifted into bit 30 instead of bit 31.
6056 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
6057 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
6058 // Use only the 5 least significant bits of the shift count.
6059 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006060 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006061 // Unsigned shift is not allowed to produce a negative number, so
6062 // check the sign bit and the sign bit after Smi tagging.
6063 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006064 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006065 // Smi tag result.
6066 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006067 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006068 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006069 // Remove tags from operands.
6070 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
6071 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
6072 // Use only the 5 least significant bits of the shift count.
6073 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006074 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006075 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006076 __ add(r2, r3, Operand(0x40000000), SetCC);
6077 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006078 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006079 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006080 default: UNREACHABLE();
6081 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006082 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006083 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006084 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006085 break;
6086 }
6087
6088 default: UNREACHABLE();
6089 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006090 // This code should be unreachable.
6091 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006092}
6093
6094
6095void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006096 // Do tail-call to runtime routine. Runtime routines expect at least one
6097 // argument, so give it a Smi.
6098 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006099 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006100 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006101
6102 __ StubReturn(1);
6103}
6104
6105
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006106void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006107 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006108
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006109 if (op_ == Token::SUB) {
6110 // Check whether the value is a smi.
6111 Label try_float;
6112 __ tst(r0, Operand(kSmiTagMask));
6113 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006114
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006115 // Go slow case if the value of the expression is zero
6116 // to make sure that we switch between 0 and -0.
6117 __ cmp(r0, Operand(0));
6118 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006119
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006120 // The value of the expression is a smi that is not zero. Try
6121 // optimistic subtraction '0 - value'.
6122 __ rsb(r1, r0, Operand(0), SetCC);
6123 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006124
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006125 __ mov(r0, Operand(r1)); // Set r0 to result.
6126 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006127
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006128 __ bind(&try_float);
6129 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6130 __ b(ne, &slow);
6131 // r0 is a heap number. Get a new heap number in r1.
6132 if (overwrite_) {
6133 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6134 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6135 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6136 } else {
6137 AllocateHeapNumber(masm, &slow, r1, r2, r3);
6138 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6139 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6140 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6141 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6142 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6143 __ mov(r0, Operand(r1));
6144 }
6145 } else if (op_ == Token::BIT_NOT) {
6146 // Check if the operand is a heap number.
6147 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6148 __ b(ne, &slow);
6149
6150 // Convert the heap number is r0 to an untagged integer in r1.
6151 GetInt32(masm, r0, r1, r2, r3, &slow);
6152
6153 // Do the bitwise operation (move negated) and check if the result
6154 // fits in a smi.
6155 Label try_float;
6156 __ mvn(r1, Operand(r1));
6157 __ add(r2, r1, Operand(0x40000000), SetCC);
6158 __ b(mi, &try_float);
6159 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6160 __ b(&done);
6161
6162 __ bind(&try_float);
6163 if (!overwrite_) {
6164 // Allocate a fresh heap number, but don't overwrite r0 until
6165 // we're sure we can do it without going through the slow case
6166 // that needs the value in r0.
6167 AllocateHeapNumber(masm, &slow, r2, r3, r4);
6168 __ mov(r0, Operand(r2));
6169 }
6170
6171 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6172 // have to set up a frame.
6173 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6174 __ push(lr);
6175 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6176 __ pop(lr);
6177 } else {
6178 UNIMPLEMENTED();
6179 }
6180
6181 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006182 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006183
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006184 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006185 __ bind(&slow);
6186 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006187 switch (op_) {
6188 case Token::SUB:
6189 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6190 break;
6191 case Token::BIT_NOT:
6192 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6193 break;
6194 default:
6195 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006196 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006197}
6198
6199
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006200void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006201 // r0 holds the exception.
6202
6203 // Adjust this code if not the case.
6204 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6205
6206 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006207 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6208 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006209
6210 // Restore the next handler and frame pointer, discard handler state.
6211 ASSERT(StackHandlerConstants::kNextOffset == 0);
6212 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006213 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006214 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6215 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6216
6217 // Before returning we restore the context from the frame pointer if
6218 // not NULL. The frame pointer is NULL in the exception handler of a
6219 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006220 __ cmp(fp, Operand(0));
6221 // Set cp to NULL if fp is NULL.
6222 __ mov(cp, Operand(0), LeaveCC, eq);
6223 // Restore cp otherwise.
6224 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006225#ifdef DEBUG
6226 if (FLAG_debug_code) {
6227 __ mov(lr, Operand(pc));
6228 }
6229#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006230 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006231 __ pop(pc);
6232}
6233
6234
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006235void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6236 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006237 // Adjust this code if not the case.
6238 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6239
6240 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006241 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006242 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006243
6244 // Unwind the handlers until the ENTRY handler is found.
6245 Label loop, done;
6246 __ bind(&loop);
6247 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006248 const int kStateOffset = StackHandlerConstants::kStateOffset;
6249 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006250 __ cmp(r2, Operand(StackHandler::ENTRY));
6251 __ b(eq, &done);
6252 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006253 const int kNextOffset = StackHandlerConstants::kNextOffset;
6254 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006255 __ jmp(&loop);
6256 __ bind(&done);
6257
6258 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006259 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006260 __ pop(r2);
6261 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006262
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006263 if (type == OUT_OF_MEMORY) {
6264 // Set external caught exception to false.
6265 ExternalReference external_caught(Top::k_external_caught_exception_address);
6266 __ mov(r0, Operand(false));
6267 __ mov(r2, Operand(external_caught));
6268 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006269
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006270 // Set pending exception and r0 to out of memory exception.
6271 Failure* out_of_memory = Failure::OutOfMemoryException();
6272 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6273 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6274 __ str(r0, MemOperand(r2));
6275 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006276
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006277 // Stack layout at this point. See also StackHandlerConstants.
6278 // sp -> state (ENTRY)
6279 // fp
6280 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006281
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006282 // Discard handler state (r2 is not used) and restore frame pointer.
6283 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6284 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6285 // Before returning we restore the context from the frame pointer if
6286 // not NULL. The frame pointer is NULL in the exception handler of a
6287 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006288 __ cmp(fp, Operand(0));
6289 // Set cp to NULL if fp is NULL.
6290 __ mov(cp, Operand(0), LeaveCC, eq);
6291 // Restore cp otherwise.
6292 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006293#ifdef DEBUG
6294 if (FLAG_debug_code) {
6295 __ mov(lr, Operand(pc));
6296 }
6297#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006298 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006299 __ pop(pc);
6300}
6301
6302
6303void CEntryStub::GenerateCore(MacroAssembler* masm,
6304 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006305 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006306 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006307 bool do_gc,
6308 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006309 // r0: result parameter for PerformGC, if any
6310 // r4: number of arguments including receiver (C callee-saved)
6311 // r5: pointer to builtin function (C callee-saved)
6312 // r6: pointer to the first argument (C callee-saved)
6313
6314 if (do_gc) {
6315 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006316 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6317 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006318 }
6319
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006320 ExternalReference scope_depth =
6321 ExternalReference::heap_always_allocate_scope_depth();
6322 if (always_allocate) {
6323 __ mov(r0, Operand(scope_depth));
6324 __ ldr(r1, MemOperand(r0));
6325 __ add(r1, r1, Operand(1));
6326 __ str(r1, MemOperand(r0));
6327 }
6328
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006329 // Call C built-in.
6330 // r0 = argc, r1 = argv
6331 __ mov(r0, Operand(r4));
6332 __ mov(r1, Operand(r6));
6333
6334 // TODO(1242173): To let the GC traverse the return address of the exit
6335 // frames, we need to know where the return address is. Right now,
6336 // we push it on the stack to be able to find it again, but we never
6337 // restore from it in case of changes, which makes it impossible to
6338 // support moving the C entry code stub. This should be fixed, but currently
6339 // this is OK because the CEntryStub gets generated so early in the V8 boot
6340 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006341 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6342 masm->push(lr);
6343 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006344
6345 if (always_allocate) {
6346 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6347 // though (contain the result).
6348 __ mov(r2, Operand(scope_depth));
6349 __ ldr(r3, MemOperand(r2));
6350 __ sub(r3, r3, Operand(1));
6351 __ str(r3, MemOperand(r2));
6352 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006353
6354 // check for failure result
6355 Label failure_returned;
6356 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6357 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6358 __ add(r2, r0, Operand(1));
6359 __ tst(r2, Operand(kFailureTagMask));
6360 __ b(eq, &failure_returned);
6361
6362 // Exit C frame and return.
6363 // r0:r1: result
6364 // sp: stack pointer
6365 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006366 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006367
6368 // check if we should retry or throw exception
6369 Label retry;
6370 __ bind(&failure_returned);
6371 ASSERT(Failure::RETRY_AFTER_GC == 0);
6372 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6373 __ b(eq, &retry);
6374
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006375 // Special handling of out of memory exceptions.
6376 Failure* out_of_memory = Failure::OutOfMemoryException();
6377 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6378 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006379
6380 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006381 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006382 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006383 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006384 __ ldr(r0, MemOperand(ip));
6385 __ str(r3, MemOperand(ip));
6386
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006387 // Special handling of termination exceptions which are uncatchable
6388 // by javascript code.
6389 __ cmp(r0, Operand(Factory::termination_exception()));
6390 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006391
6392 // Handle normal exception.
6393 __ jmp(throw_normal_exception);
6394
6395 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6396}
6397
6398
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006399void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006400 // Called from JavaScript; parameters are on stack as if calling JS function
6401 // r0: number of arguments including receiver
6402 // r1: pointer to builtin function
6403 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006404 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006405 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006406
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006407 // Result returned in r0 or r0+r1 by default.
6408
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006409 // NOTE: Invocations of builtins may return failure objects
6410 // instead of a proper result. The builtin entry handles
6411 // this by performing a garbage collection and retrying the
6412 // builtin once.
6413
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006414 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006415 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006416
6417 // r4: number of arguments (C callee-saved)
6418 // r5: pointer to builtin function (C callee-saved)
6419 // r6: pointer to first argument (C callee-saved)
6420
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006421 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006422 Label throw_termination_exception;
6423 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006424
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006425 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006426 GenerateCore(masm,
6427 &throw_normal_exception,
6428 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006429 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006430 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006431 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006432
6433 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006434 GenerateCore(masm,
6435 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006436 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006437 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006438 true,
6439 false);
6440
6441 // Do full GC and retry runtime call one final time.
6442 Failure* failure = Failure::InternalError();
6443 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6444 GenerateCore(masm,
6445 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006446 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006447 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006448 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006449 true);
6450
6451 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006452 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6453
6454 __ bind(&throw_termination_exception);
6455 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006456
6457 __ bind(&throw_normal_exception);
6458 GenerateThrowTOS(masm);
6459}
6460
6461
6462void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6463 // r0: code entry
6464 // r1: function
6465 // r2: receiver
6466 // r3: argc
6467 // [sp+0]: argv
6468
6469 Label invoke, exit;
6470
6471 // Called from C, so do not pop argc and args on exit (preserve sp)
6472 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006473 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006474 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6475
6476 // Get address of argv, see stm above.
6477 // r0: code entry
6478 // r1: function
6479 // r2: receiver
6480 // r3: argc
6481 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
6482 __ ldr(r4, MemOperand(r4)); // argv
6483
6484 // Push a frame with special values setup to mark it as an entry frame.
6485 // r0: code entry
6486 // r1: function
6487 // r2: receiver
6488 // r3: argc
6489 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006490 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006491 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6492 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006493 __ mov(r6, Operand(Smi::FromInt(marker)));
6494 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6495 __ ldr(r5, MemOperand(r5));
6496 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6497
6498 // Setup frame pointer for the frame to be pushed.
6499 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6500
6501 // Call a faked try-block that does the invoke.
6502 __ bl(&invoke);
6503
6504 // Caught exception: Store result (exception) in the pending
6505 // exception field in the JSEnv and return a failure sentinel.
6506 // Coming in here the fp will be invalid because the PushTryHandler below
6507 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006508 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006509 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006510 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006511 __ b(&exit);
6512
6513 // Invoke: Link this frame into the handler chain.
6514 __ bind(&invoke);
6515 // Must preserve r0-r4, r5-r7 are available.
6516 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006517 // If an exception not caught by another handler occurs, this handler
6518 // returns control to the code after the bl(&invoke) above, which
6519 // restores all kCalleeSaved registers (including cp and fp) to their
6520 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006521
6522 // Clear any pending exceptions.
6523 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6524 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006525 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006526 __ str(r5, MemOperand(ip));
6527
6528 // Invoke the function by calling through JS entry trampoline builtin.
6529 // Notice that we cannot store a reference to the trampoline code directly in
6530 // this stub, because runtime stubs are not traversed when doing GC.
6531
6532 // Expected registers by Builtins::JSEntryTrampoline
6533 // r0: code entry
6534 // r1: function
6535 // r2: receiver
6536 // r3: argc
6537 // r4: argv
6538 if (is_construct) {
6539 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6540 __ mov(ip, Operand(construct_entry));
6541 } else {
6542 ExternalReference entry(Builtins::JSEntryTrampoline);
6543 __ mov(ip, Operand(entry));
6544 }
6545 __ ldr(ip, MemOperand(ip)); // deref address
6546
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006547 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6548 // macro for the add instruction because we don't want the coverage tool
6549 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006550 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006551 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006552
6553 // Unlink this frame from the handler chain. When reading the
6554 // address of the next handler, there is no need to use the address
6555 // displacement since the current stack pointer (sp) points directly
6556 // to the stack handler.
6557 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6558 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6559 __ str(r3, MemOperand(ip));
6560 // No need to restore registers
6561 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6562
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006563
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006564 __ bind(&exit); // r0 holds result
6565 // Restore the top frame descriptors from the stack.
6566 __ pop(r3);
6567 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6568 __ str(r3, MemOperand(ip));
6569
6570 // Reset the stack to the callee saved registers.
6571 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6572
6573 // Restore callee-saved registers and return.
6574#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006575 if (FLAG_debug_code) {
6576 __ mov(lr, Operand(pc));
6577 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006578#endif
6579 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6580}
6581
6582
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006583// This stub performs an instanceof, calling the builtin function if
6584// necessary. Uses r1 for the object, r0 for the function that it may
6585// be an instance of (these are fetched from the stack).
6586void InstanceofStub::Generate(MacroAssembler* masm) {
6587 // Get the object - slow case for smis (we may need to throw an exception
6588 // depending on the rhs).
6589 Label slow, loop, is_instance, is_not_instance;
6590 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6591 __ BranchOnSmi(r0, &slow);
6592
6593 // Check that the left hand is a JS object and put map in r3.
6594 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6595 __ b(lt, &slow);
6596 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6597 __ b(gt, &slow);
6598
6599 // Get the prototype of the function (r4 is result, r2 is scratch).
6600 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6601 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6602
6603 // Check that the function prototype is a JS object.
6604 __ BranchOnSmi(r4, &slow);
6605 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6606 __ b(lt, &slow);
6607 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6608 __ b(gt, &slow);
6609
6610 // Register mapping: r3 is object map and r4 is function prototype.
6611 // Get prototype of object into r2.
6612 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6613
6614 // Loop through the prototype chain looking for the function prototype.
6615 __ bind(&loop);
6616 __ cmp(r2, Operand(r4));
6617 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006618 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6619 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006620 __ b(eq, &is_not_instance);
6621 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6622 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6623 __ jmp(&loop);
6624
6625 __ bind(&is_instance);
6626 __ mov(r0, Operand(Smi::FromInt(0)));
6627 __ pop();
6628 __ pop();
6629 __ mov(pc, Operand(lr)); // Return.
6630
6631 __ bind(&is_not_instance);
6632 __ mov(r0, Operand(Smi::FromInt(1)));
6633 __ pop();
6634 __ pop();
6635 __ mov(pc, Operand(lr)); // Return.
6636
6637 // Slow-case. Tail call builtin.
6638 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006639 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6640}
6641
6642
ager@chromium.org7c537e22008-10-16 08:43:32 +00006643void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006644 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006645 Label adaptor;
6646 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6647 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006648 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006649 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006650
ager@chromium.org7c537e22008-10-16 08:43:32 +00006651 // Nothing to do: The formal number of parameters has already been
6652 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006653 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006654
ager@chromium.org7c537e22008-10-16 08:43:32 +00006655 // Arguments adaptor case: Read the arguments length from the
6656 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006657 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006658 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006659 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006660}
6661
6662
ager@chromium.org7c537e22008-10-16 08:43:32 +00006663void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6664 // The displacement is the offset of the last parameter (if any)
6665 // relative to the frame pointer.
6666 static const int kDisplacement =
6667 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006668
ager@chromium.org7c537e22008-10-16 08:43:32 +00006669 // Check that the key is a smi.
6670 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006671 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006672
ager@chromium.org7c537e22008-10-16 08:43:32 +00006673 // Check if the calling frame is an arguments adaptor frame.
6674 Label adaptor;
6675 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6676 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006677 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006678 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006679
ager@chromium.org7c537e22008-10-16 08:43:32 +00006680 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006681 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006682 // check for free.
6683 __ cmp(r1, r0);
6684 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006685
ager@chromium.org7c537e22008-10-16 08:43:32 +00006686 // Read the argument from the stack and return it.
6687 __ sub(r3, r0, r1);
6688 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6689 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006690 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006691
6692 // Arguments adaptor case: Check index against actual arguments
6693 // limit found in the arguments adaptor frame. Use unsigned
6694 // comparison to get negative check for free.
6695 __ bind(&adaptor);
6696 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6697 __ cmp(r1, r0);
6698 __ b(cs, &slow);
6699
6700 // Read the argument from the adaptor frame and return it.
6701 __ sub(r3, r0, r1);
6702 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6703 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006704 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006705
6706 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6707 // by calling the runtime system.
6708 __ bind(&slow);
6709 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006710 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006711}
6712
6713
6714void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6715 // Check if the calling frame is an arguments adaptor frame.
6716 Label runtime;
6717 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6718 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006719 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006720 __ b(ne, &runtime);
6721
6722 // Patch the arguments.length and the parameters pointer.
6723 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6724 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6725 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6726 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6727 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6728
6729 // Do the runtime call to allocate the arguments object.
6730 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006731 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006732}
6733
6734
6735void CallFunctionStub::Generate(MacroAssembler* masm) {
6736 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006737
6738 // If the receiver might be a value (string, number or boolean) check for this
6739 // and box it if it is.
6740 if (ReceiverMightBeValue()) {
6741 // Get the receiver from the stack.
6742 // function, receiver [, arguments]
6743 Label receiver_is_value, receiver_is_js_object;
6744 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
6745
6746 // Check if receiver is a smi (which is a number value).
6747 __ BranchOnSmi(r1, &receiver_is_value);
6748
6749 // Check if the receiver is a valid JS object.
6750 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
6751 __ b(ge, &receiver_is_js_object);
6752
6753 // Call the runtime to box the value.
6754 __ bind(&receiver_is_value);
6755 __ EnterInternalFrame();
6756 __ push(r1);
6757 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
6758 __ LeaveInternalFrame();
6759 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
6760
6761 __ bind(&receiver_is_js_object);
6762 }
6763
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006764 // Get the function to call from the stack.
6765 // function, receiver [, arguments]
6766 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6767
6768 // Check that the function is really a JavaScript function.
6769 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006770 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006771 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006772 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006773 __ b(ne, &slow);
6774
6775 // Fast-case: Invoke the function now.
6776 // r1: pushed function
6777 ParameterCount actual(argc_);
6778 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6779
6780 // Slow-case: Non-function called.
6781 __ bind(&slow);
6782 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006783 __ mov(r2, Operand(0));
6784 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6785 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6786 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006787}
6788
6789
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006790const char* CompareStub::GetName() {
6791 switch (cc_) {
6792 case lt: return "CompareStub_LT";
6793 case gt: return "CompareStub_GT";
6794 case le: return "CompareStub_LE";
6795 case ge: return "CompareStub_GE";
6796 case ne: {
6797 if (strict_) {
6798 if (never_nan_nan_) {
6799 return "CompareStub_NE_STRICT_NO_NAN";
6800 } else {
6801 return "CompareStub_NE_STRICT";
6802 }
6803 } else {
6804 if (never_nan_nan_) {
6805 return "CompareStub_NE_NO_NAN";
6806 } else {
6807 return "CompareStub_NE";
6808 }
6809 }
6810 }
6811 case eq: {
6812 if (strict_) {
6813 if (never_nan_nan_) {
6814 return "CompareStub_EQ_STRICT_NO_NAN";
6815 } else {
6816 return "CompareStub_EQ_STRICT";
6817 }
6818 } else {
6819 if (never_nan_nan_) {
6820 return "CompareStub_EQ_NO_NAN";
6821 } else {
6822 return "CompareStub_EQ";
6823 }
6824 }
6825 }
6826 default: return "CompareStub";
6827 }
6828}
6829
6830
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006831int CompareStub::MinorKey() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006832 // Encode the three parameters in a unique 16 bit value.
6833 ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16));
6834 int nnn_value = (never_nan_nan_ ? 2 : 0);
6835 if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs.
6836 return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006837}
6838
6839
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006840
6841
6842void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6843 Register left,
6844 Register right,
6845 Register scratch1,
6846 Register scratch2,
6847 Register scratch3,
6848 Register scratch4) {
6849 Label compare_lengths;
6850 // Find minimum length and length difference.
6851 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
6852 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
6853 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
6854 Register length_delta = scratch3;
6855 __ mov(scratch1, scratch2, LeaveCC, gt);
6856 Register min_length = scratch1;
6857 __ tst(min_length, Operand(min_length));
6858 __ b(eq, &compare_lengths);
6859
6860 // Setup registers so that we only need to increment one register
6861 // in the loop.
6862 __ add(scratch2, min_length,
6863 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6864 __ add(left, left, Operand(scratch2));
6865 __ add(right, right, Operand(scratch2));
6866 // Registers left and right points to the min_length character of strings.
6867 __ rsb(min_length, min_length, Operand(-1));
6868 Register index = min_length;
6869 // Index starts at -min_length.
6870
6871 {
6872 // Compare loop.
6873 Label loop;
6874 __ bind(&loop);
6875 // Compare characters.
6876 __ add(index, index, Operand(1), SetCC);
6877 __ ldrb(scratch2, MemOperand(left, index), ne);
6878 __ ldrb(scratch4, MemOperand(right, index), ne);
6879 // Skip to compare lengths with eq condition true.
6880 __ b(eq, &compare_lengths);
6881 __ cmp(scratch2, scratch4);
6882 __ b(eq, &loop);
6883 // Fallthrough with eq condition false.
6884 }
6885 // Compare lengths - strings up to min-length are equal.
6886 __ bind(&compare_lengths);
6887 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
6888 // Use zero length_delta as result.
6889 __ mov(r0, Operand(length_delta), SetCC, eq);
6890 // Fall through to here if characters compare not-equal.
6891 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
6892 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
6893 __ Ret();
6894}
6895
6896
6897void StringCompareStub::Generate(MacroAssembler* masm) {
6898 Label runtime;
6899
6900 // Stack frame on entry.
6901 // sp[0]: return address
6902 // sp[4]: right string
6903 // sp[8]: left string
6904
6905 __ ldr(r0, MemOperand(sp, 2 * kPointerSize)); // left
6906 __ ldr(r1, MemOperand(sp, 1 * kPointerSize)); // right
6907
6908 Label not_same;
6909 __ cmp(r0, r1);
6910 __ b(ne, &not_same);
6911 ASSERT_EQ(0, EQUAL);
6912 ASSERT_EQ(0, kSmiTag);
6913 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
6914 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
6915 __ add(sp, sp, Operand(2 * kPointerSize));
6916 __ Ret();
6917
6918 __ bind(&not_same);
6919
6920 // Check that both objects are sequential ascii strings.
6921 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
6922
6923 // Compare flat ascii strings natively. Remove arguments from stack first.
6924 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
6925 __ add(sp, sp, Operand(2 * kPointerSize));
6926 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
6927
6928 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6929 // tagged as a small integer.
6930 __ bind(&runtime);
6931 __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1);
6932}
6933
6934
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006935#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006937} } // namespace v8::internal