blob: 70d8ab49550f57150c8e5bd9b0979e574c9d94b5 [file] [log] [blame]
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001// Copyright 2006-2009 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,
50 Label* rhs_not_nan,
51 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
ager@chromium.org7c537e22008-10-16 08:43:32 +0000124CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
125 bool is_eval)
126 : is_eval_(is_eval),
127 script_(script),
128 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 masm_(new MacroAssembler(NULL, buffer_size)),
130 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000131 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000132 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 cc_reg_(al),
134 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000135 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136}
137
138
139// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// cp: callee's context
144
ager@chromium.org7c537e22008-10-16 08:43:32 +0000145void CodeGenerator::GenCode(FunctionLiteral* fun) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000146 // Record the position for debugging purposes.
147 CodeForFunctionPosition(fun);
148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 ZoneList<Statement*>* body = fun->body();
150
151 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000152 ASSERT(scope_ == NULL);
153 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 ASSERT(allocator_ == NULL);
155 RegisterAllocator register_allocator(this);
156 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000158 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000159 cc_reg_ = al;
160 {
161 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000163 // Entry:
164 // Stack: receiver, arguments
165 // lr: return address
166 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000170 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000171 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // tos: code slot
173#ifdef DEBUG
174 if (strlen(FLAG_stop_at) > 0 &&
175 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000177 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 }
179#endif
180
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000181 // Allocate space for locals and initialize them. This also checks
182 // for stack overflow.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000183 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 // Initialize the function return target after the locals are set
185 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000186 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000187 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000189 VirtualFrame::SpilledScope spilled_scope;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000190 int heap_slots = scope_->num_heap_slots();
191 if (heap_slots > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192 // Allocate local context.
193 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000194 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 frame_->EmitPush(r0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000196 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
197 FastNewContextStub stub(heap_slots);
198 frame_->CallStub(&stub, 1);
199 } else {
200 frame_->CallRuntime(Runtime::kNewContext, 1);
201 }
kasper.lund7276f142008-07-30 08:49:36 +0000202
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000203#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000204 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000205 __ cmp(r0, Operand(cp));
206 verified_true.Branch(eq);
207 __ stop("NewContext: r0 is expected to be the same as cp");
208 verified_true.Bind();
209#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000211 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 }
213
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000214 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 // 1) only needed if we have a context
216 // 2) no need to recompute context ptr every single time
217 // 3) don't copy parameter operand code from SlotOperand!
218 {
219 Comment cmnt2(masm_, "[ copy context parameters into .context");
220
221 // Note that iteration order is relevant here! If we have the same
222 // parameter twice (e.g., function (x, y, x)), and that parameter
223 // needs to be copied into the context, it must be the last argument
224 // passed to the parameter that needs to be copied. This is a rare
225 // case so we don't check for it, instead we rely on the copying
226 // order: such a parameter is copied repeatedly into the same
227 // context location and thus the last value is what is seen inside
228 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000229 for (int i = 0; i < scope_->num_parameters(); i++) {
230 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 Slot* slot = par->slot();
232 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000233 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000234 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 // Loads r2 with context; used below in RecordWrite.
236 __ str(r1, SlotOperand(slot, r2));
237 // Load the offset into r3.
238 int slot_offset =
239 FixedArray::kHeaderSize + slot->index() * kPointerSize;
240 __ mov(r3, Operand(slot_offset));
241 __ RecordWrite(r2, r3, r1);
242 }
243 }
244 }
245
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246 // Store the arguments object. This must happen after context
247 // initialization because the arguments object may be stored in the
248 // context.
249 if (scope_->arguments() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 Comment cmnt(masm_, "[ allocate arguments object");
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000251 ASSERT(scope_->arguments_shadow() != NULL);
252 Variable* arguments = scope_->arguments()->var();
253 Variable* shadow = scope_->arguments_shadow()->var();
254 ASSERT(arguments != NULL && arguments->slot() != NULL);
255 ASSERT(shadow != NULL && shadow->slot() != NULL);
256 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
257 __ ldr(r2, frame_->Function());
258 // The receiver is below the arguments, the return address, and the
259 // frame pointer on the stack.
260 const int kReceiverDisplacement = 2 + scope_->num_parameters();
261 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
262 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
263 frame_->Adjust(3);
264 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
265 frame_->CallStub(&stub, 3);
266 frame_->EmitPush(r0);
267 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
268 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000269 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 }
271
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000272 // Initialize ThisFunction reference if present.
273 if (scope_->is_function_scope() && scope_->function() != NULL) {
274 __ mov(ip, Operand(Factory::the_hole_value()));
275 frame_->EmitPush(ip);
276 StoreToSlot(scope_->function()->slot(), NOT_CONST_INIT);
277 }
278
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000279 // Generate code to 'execute' declarations and initialize functions
280 // (source elements). In case of an illegal redeclaration we need to
281 // handle that instead of processing the declarations.
282 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000284 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 } else {
286 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000287 ProcessDeclarations(scope_->declarations());
288 // Bail out if a stack-overflow exception occurred when processing
289 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000290 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 }
292
mads.s.ager31e71382008-08-13 09:32:07 +0000293 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000294 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000295 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000296 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297
298 // Compile the body of the function in a vanilla state. Don't
299 // bother compiling all the code if the scope has an illegal
300 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000301 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 Comment cmnt(masm_, "[ function body");
303#ifdef DEBUG
304 bool is_builtin = Bootstrapper::IsActive();
305 bool should_trace =
306 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000307 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000308 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000309 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000310 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000312 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 }
315
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000317 if (has_valid_frame() || function_return_.is_linked()) {
318 if (!function_return_.is_linked()) {
319 CodeForReturnPosition(fun);
320 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321 // exit
322 // r0: result
323 // sp: stack pointer
324 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000325 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000326 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000327
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000328 function_return_.Bind();
329 if (FLAG_trace) {
330 // Push the return value on the stack as the parameter.
331 // Runtime::TraceExit returns the parameter as it is.
332 frame_->EmitPush(r0);
333 frame_->CallRuntime(Runtime::kTraceExit, 1);
334 }
335
ager@chromium.org4af710e2009-09-15 12:20:11 +0000336 // Add a label for checking the size of the code used for returning.
337 Label check_exit_codesize;
338 masm_->bind(&check_exit_codesize);
339
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000340 // Calculate the exact length of the return sequence and make sure that
341 // the constant pool is not emitted inside of the return sequence.
342 int32_t sp_delta = (scope_->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000343 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000344 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
345 // Additional mov instruction generated.
346 return_sequence_length++;
347 }
348 masm_->BlockConstPoolFor(return_sequence_length);
349
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000350 // Tear down the frame which will restore the caller's frame pointer and
351 // the link register.
352 frame_->Exit();
353
ager@chromium.org4af710e2009-09-15 12:20:11 +0000354 // Here we use masm_-> instead of the __ macro to avoid the code coverage
355 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000356 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000357 masm_->Jump(lr);
358
359 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000360 // expected by the debugger. The add instruction above is an addressing
361 // mode 1 instruction where there are restrictions on which immediate values
362 // can be encoded in the instruction and which immediate values requires
363 // use of an additional instruction for moving the immediate to a temporary
364 // register.
365 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000366 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000367 }
368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 ASSERT(!has_cc());
371 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000372 ASSERT(!function_return_is_shadowed_);
373 function_return_.Unuse();
374 DeleteFrame();
375
376 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000377 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000378 ProcessDeferred();
379 }
380
381 allocator_ = NULL;
382 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383}
384
385
ager@chromium.org7c537e22008-10-16 08:43:32 +0000386MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
387 // Currently, this assertion will fail if we try to assign to
388 // a constant variable that is constant because it is read-only
389 // (such as the variable referring to a named function expression).
390 // We need to implement assignments to read-only variables.
391 // Ideally, we should do this during AST generation (by converting
392 // such assignments into expression statements); however, in general
393 // we may not be able to make the decision until past AST generation,
394 // that is when the entire program is known.
395 ASSERT(slot != NULL);
396 int index = slot->index();
397 switch (slot->type()) {
398 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000399 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000400
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000401 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000402 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000403
404 case Slot::CONTEXT: {
405 // Follow the context chain if necessary.
406 ASSERT(!tmp.is(cp)); // do not overwrite context register
407 Register context = cp;
408 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000409 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410 // Load the closure.
411 // (All contexts, even 'with' contexts, have a closure,
412 // and it is the same for all contexts inside a function.
413 // There is no need to go to the function context first.)
414 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
415 // Load the function context (which is the incoming, outer context).
416 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
417 context = tmp;
418 }
419 // We may have a 'with' context now. Get the function context.
420 // (In fact this mov may never be the needed, since the scope analysis
421 // may not permit a direct context access in this case and thus we are
422 // always at a function context. However it is safe to dereference be-
423 // cause the function context of a function context is itself. Before
424 // deleting this mov we should try to create a counter-example first,
425 // though...)
426 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
427 return ContextOperand(tmp, index);
428 }
429
430 default:
431 UNREACHABLE();
432 return MemOperand(r0, 0);
433 }
434}
435
436
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000437MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
438 Slot* slot,
439 Register tmp,
440 Register tmp2,
441 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000442 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000443 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000444
ager@chromium.org381abbb2009-02-25 13:23:22 +0000445 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
446 if (s->num_heap_slots() > 0) {
447 if (s->calls_eval()) {
448 // Check that extension is NULL.
449 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
450 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000451 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000452 }
453 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
454 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
455 context = tmp;
456 }
457 }
458 // Check that last extension is NULL.
459 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
460 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000462 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000463 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000464}
465
466
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000467// Loads a value on TOS. If it is a boolean value, the result may have been
468// (partially) translated into branches, or it may have set the condition
469// code register. If force_cc is set, the value is forced to set the
470// condition code register and no value is pushed. If the condition code
471// register was set, has_cc() is true and cc_reg_ contains the condition to
472// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000473void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474 JumpTarget* true_target,
475 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000476 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000477 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000478 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000480 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000481 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000482
483 // If we hit a stack overflow, we may not have actually visited
484 // the expression. In that case, we ensure that we have a
485 // valid-looking frame state because we will continue to generate
486 // code as we unwind the C++ stack.
487 //
488 // It's possible to have both a stack overflow and a valid frame
489 // state (eg, a subexpression overflowed, visiting it returned
490 // with a dummied frame state, and visiting this expression
491 // returned with a normal-looking state).
492 if (HasStackOverflow() &&
493 has_valid_frame() &&
494 !has_cc() &&
495 frame_->height() == original_height) {
496 true_target->Jump();
497 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000498 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000500 // Convert the TOS value to a boolean in the condition code register.
501 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000503 ASSERT(!force_cc || !has_valid_frame() || has_cc());
504 ASSERT(!has_valid_frame() ||
505 (has_cc() && frame_->height() == original_height) ||
506 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507}
508
509
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000510void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000511#ifdef DEBUG
512 int original_height = frame_->height();
513#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000514 JumpTarget true_target;
515 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000516 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517
518 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000520 JumpTarget loaded;
521 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000522 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000523 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000524 frame_->EmitPush(r0);
525 loaded.Jump();
526 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000527 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
529 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 cc_reg_ = al;
531 }
532
533 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 // We have at least one condition value that has been "translated"
535 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000536 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 if (frame_ != NULL) {
538 loaded.Jump(); // Don't lose the current TOS.
539 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000544 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000545 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 // If both "true" and "false" need to be loaded jump across the code for
548 // "false".
549 if (both) {
550 loaded.Jump();
551 }
552 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000555 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 // A value is loaded on all paths reaching this point.
559 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564}
565
566
ager@chromium.org7c537e22008-10-16 08:43:32 +0000567void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000568 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000569 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571}
572
573
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000574void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000575 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000576 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
577 __ ldr(scratch,
578 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000579 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000580}
581
582
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000583void CodeGenerator::LoadTypeofExpression(Expression* expr) {
584 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000585 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000586 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000588 // For a global variable we build the property reference
589 // <global>.<variable> and perform a (regular non-contextual) property
590 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
592 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000593 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000594 Reference ref(this, &property);
595 ref.GetValueAndSpill();
596 } else if (variable != NULL && variable->slot() != NULL) {
597 // For a variable that rewrites to a slot, we signal it is the immediate
598 // subexpression of a typeof.
599 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
600 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000602 // Anything else can be handled normally.
603 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 }
605}
606
607
ager@chromium.org7c537e22008-10-16 08:43:32 +0000608Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
610 cgen->LoadReference(this);
611}
612
613
614Reference::~Reference() {
615 cgen_->UnloadReference(this);
616}
617
618
ager@chromium.org7c537e22008-10-16 08:43:32 +0000619void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000620 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000621 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 Expression* e = ref->expression();
623 Property* property = e->AsProperty();
624 Variable* var = e->AsVariableProxy()->AsVariable();
625
626 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000627 // The expression is either a property or a variable proxy that rewrites
628 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000629 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000630 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631 ref->set_type(Reference::NAMED);
632 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000633 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 ref->set_type(Reference::KEYED);
635 }
636 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000637 // The expression is a variable proxy that does not rewrite to a
638 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 LoadGlobal();
641 ref->set_type(Reference::NAMED);
642 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000643 ASSERT(var->slot() != NULL);
644 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 }
646 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000647 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 LoadAndSpill(e);
649 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 }
651}
652
653
ager@chromium.org7c537e22008-10-16 08:43:32 +0000654void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000655 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000656 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000657 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000659 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000660 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000661 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000662 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 }
664}
665
666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
668// register to a boolean in the condition code register. The code
669// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000670void CodeGenerator::ToBoolean(JumpTarget* true_target,
671 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000672 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000673 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000675 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676
677 // Fast case checks
678
mads.s.ager31e71382008-08-13 09:32:07 +0000679 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000680 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
681 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000682 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
mads.s.ager31e71382008-08-13 09:32:07 +0000684 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000685 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
686 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000687 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
mads.s.ager31e71382008-08-13 09:32:07 +0000689 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000690 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
691 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000692 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693
mads.s.ager31e71382008-08-13 09:32:07 +0000694 // Check if the value is a smi.
695 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000696 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000697 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000698 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
700 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000701 frame_->EmitPush(r0);
702 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000703 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000704 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
705 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
707 cc_reg_ = ne;
708}
709
710
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000711void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000712 OverwriteMode overwrite_mode,
713 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000714 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000715 // sp[0] : y
716 // sp[1] : x
717 // result : r0
718
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719 // Stub is entered with a call: 'return address' is in lr.
720 switch (op) {
721 case Token::ADD: // fall through.
722 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000723 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000724 case Token::DIV:
725 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000726 case Token::BIT_OR:
727 case Token::BIT_AND:
728 case Token::BIT_XOR:
729 case Token::SHL:
730 case Token::SHR:
731 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000732 frame_->EmitPop(r0); // r0 : y
733 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000734 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000735 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 break;
737 }
738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000740 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000742 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 break;
744
745 default:
746 // Other cases should have been handled before this point.
747 UNREACHABLE();
748 break;
749 }
750}
751
752
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000754 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000755 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000756 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000757 bool reversed,
758 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000759 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000761 reversed_(reversed),
762 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000763 set_comment("[ DeferredInlinedSmiOperation");
764 }
765
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000766 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000767
768 private:
769 Token::Value op_;
770 int value_;
771 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000772 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000773};
774
775
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000776void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000777 switch (op_) {
778 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000779 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000780 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000781 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
782 __ mov(r1, Operand(Smi::FromInt(value_)));
783 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000784 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
785 __ mov(r0, Operand(Smi::FromInt(value_)));
786 }
787 break;
788 }
789
790 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000791 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000792 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000793 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
794 __ mov(r1, Operand(Smi::FromInt(value_)));
795 } else {
796 __ add(r1, r0, Operand(Smi::FromInt(value_)));
797 __ mov(r0, Operand(Smi::FromInt(value_)));
798 }
799 break;
800 }
801
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000802 // For these operations there is no optimistic operation that needs to be
803 // reverted.
804 case Token::MUL:
805 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000806 case Token::BIT_OR:
807 case Token::BIT_XOR:
808 case Token::BIT_AND: {
809 if (reversed_) {
810 __ mov(r1, Operand(Smi::FromInt(value_)));
811 } else {
812 __ mov(r1, Operand(r0));
813 __ mov(r0, Operand(Smi::FromInt(value_)));
814 }
815 break;
816 }
817
818 case Token::SHL:
819 case Token::SHR:
820 case Token::SAR: {
821 if (!reversed_) {
822 __ mov(r1, Operand(r0));
823 __ mov(r0, Operand(Smi::FromInt(value_)));
824 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000825 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000826 }
827 break;
828 }
829
830 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000831 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000832 UNREACHABLE();
833 break;
834 }
835
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000836 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000837 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000838}
839
840
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000841static bool PopCountLessThanEqual2(unsigned int x) {
842 x &= x - 1;
843 return (x & (x - 1)) == 0;
844}
845
846
847// Returns the index of the lowest bit set.
848static int BitPosition(unsigned x) {
849 int bit_posn = 0;
850 while ((x & 0xf) == 0) {
851 bit_posn += 4;
852 x >>= 4;
853 }
854 while ((x & 1) == 0) {
855 bit_posn++;
856 x >>= 1;
857 }
858 return bit_posn;
859}
860
861
ager@chromium.org7c537e22008-10-16 08:43:32 +0000862void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000863 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000864 bool reversed,
865 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000866 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867 // NOTE: This is an attempt to inline (a bit) more of the code for
868 // some possible smi operations (like + and -) when (at least) one
869 // of the operands is a literal smi. With this optimization, the
870 // performance of the system is increased by ~15%, and the generated
871 // code size is increased by ~1% (measured on a combination of
872 // different benchmarks).
873
mads.s.ager31e71382008-08-13 09:32:07 +0000874 // sp[0] : operand
875
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000876 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000878 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000879 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000881 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 switch (op) {
883 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000884 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000885 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000887 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000888 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000890 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000891 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 break;
893 }
894
895 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000897 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
ager@chromium.orge2902be2009-06-08 12:21:35 +0000899 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000900 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000901 } else {
902 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000906 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000908 break;
909 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000911
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 case Token::BIT_OR:
913 case Token::BIT_XOR:
914 case Token::BIT_AND: {
915 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000916 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000917 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000918 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000919 switch (op) {
920 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
921 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
922 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
923 default: UNREACHABLE();
924 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000925 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000926 break;
927 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000929 case Token::SHL:
930 case Token::SHR:
931 case Token::SAR: {
932 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000933 something_to_inline = false;
934 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000936 int shift_value = int_value & 0x1f; // least significant 5 bits
937 DeferredCode* deferred =
938 new DeferredInlineSmiOperation(op, shift_value, false, mode);
939 __ tst(r0, Operand(kSmiTagMask));
940 deferred->Branch(ne);
941 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
942 switch (op) {
943 case Token::SHL: {
944 if (shift_value != 0) {
945 __ mov(r2, Operand(r2, LSL, shift_value));
946 }
947 // check that the *unsigned* result fits in a smi
948 __ add(r3, r2, Operand(0x40000000), SetCC);
949 deferred->Branch(mi);
950 break;
951 }
952 case Token::SHR: {
953 // LSR by immediate 0 means shifting 32 bits.
954 if (shift_value != 0) {
955 __ mov(r2, Operand(r2, LSR, shift_value));
956 }
957 // check that the *unsigned* result fits in a smi
958 // neither of the two high-order bits can be set:
959 // - 0x80000000: high bit would be lost when smi tagging
960 // - 0x40000000: this number would convert to negative when
961 // smi tagging these two cases can only happen with shifts
962 // by 0 or 1 when handed a valid smi
963 __ and_(r3, r2, Operand(0xc0000000), SetCC);
964 deferred->Branch(ne);
965 break;
966 }
967 case Token::SAR: {
968 if (shift_value != 0) {
969 // ASR by immediate 0 means shifting 32 bits.
970 __ mov(r2, Operand(r2, ASR, shift_value));
971 }
972 break;
973 }
974 default: UNREACHABLE();
975 }
976 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
977 deferred->BindExit();
978 break;
979 }
980
981 case Token::MOD: {
982 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
983 something_to_inline = false;
984 break;
985 }
986 DeferredCode* deferred =
987 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
988 unsigned mask = (0x80000000u | kSmiTagMask);
989 __ tst(r0, Operand(mask));
990 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
991 mask = (int_value << kSmiTagSize) - 1;
992 __ and_(r0, r0, Operand(mask));
993 deferred->BindExit();
994 break;
995 }
996
997 case Token::MUL: {
998 if (!IsEasyToMultiplyBy(int_value)) {
999 something_to_inline = false;
1000 break;
1001 }
1002 DeferredCode* deferred =
1003 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1004 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1005 max_smi_that_wont_overflow <<= kSmiTagSize;
1006 unsigned mask = 0x80000000u;
1007 while ((mask & max_smi_that_wont_overflow) == 0) {
1008 mask |= mask >> 1;
1009 }
1010 mask |= kSmiTagMask;
1011 // This does a single mask that checks for a too high value in a
1012 // conservative way and for a non-Smi. It also filters out negative
1013 // numbers, unfortunately, but since this code is inline we prefer
1014 // brevity to comprehensiveness.
1015 __ tst(r0, Operand(mask));
1016 deferred->Branch(ne);
1017 MultiplyByKnownInt(masm_, r0, r0, int_value);
1018 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 break;
1020 }
1021
1022 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001023 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 break;
1025 }
1026
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001027 if (!something_to_inline) {
1028 if (!reversed) {
1029 frame_->EmitPush(r0);
1030 __ mov(r0, Operand(value));
1031 frame_->EmitPush(r0);
1032 GenericBinaryOperation(op, mode, int_value);
1033 } else {
1034 __ mov(ip, Operand(value));
1035 frame_->EmitPush(ip);
1036 frame_->EmitPush(r0);
1037 GenericBinaryOperation(op, mode, kUnknownIntValue);
1038 }
1039 }
1040
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001041 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042}
1043
1044
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001045void CodeGenerator::Comparison(Condition cc,
1046 Expression* left,
1047 Expression* right,
1048 bool strict) {
1049 if (left != NULL) LoadAndSpill(left);
1050 if (right != NULL) LoadAndSpill(right);
1051
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001052 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001053 // sp[0] : y
1054 // sp[1] : x
1055 // result : cc register
1056
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 // Strict only makes sense for equality comparisons.
1058 ASSERT(!strict || cc == eq);
1059
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001060 JumpTarget exit;
1061 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001062 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1063 if (cc == gt || cc == le) {
1064 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001065 frame_->EmitPop(r1);
1066 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001067 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001068 frame_->EmitPop(r0);
1069 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001070 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 __ orr(r2, r0, Operand(r1));
1072 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001073 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001075 // Perform non-smi comparison by stub.
1076 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1077 // We call with 0 args because there are 0 on the stack.
1078 CompareStub stub(cc, strict);
1079 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001080 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001081 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001083 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001084 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 __ cmp(r1, Operand(r0));
1086
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001087 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 cc_reg_ = cc;
1089}
1090
1091
mads.s.ager31e71382008-08-13 09:32:07 +00001092// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001093void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001095 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001097 int arg_count = args->length();
1098 for (int i = 0; i < arg_count; i++) {
1099 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001100 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101
kasper.lund7276f142008-07-30 08:49:36 +00001102 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001103 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104
kasper.lund7276f142008-07-30 08:49:36 +00001105 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001106 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1107 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001108 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001111 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001112 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113}
1114
1115
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001116void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001117 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 ASSERT(has_cc());
1119 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001120 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 cc_reg_ = al;
1122}
1123
1124
ager@chromium.org7c537e22008-10-16 08:43:32 +00001125void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001126 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001127 Comment cmnt(masm_, "[ check stack");
1128 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1129 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1130 // the implicit 8 byte offset that always applies to operations with pc and
1131 // gives a return address 12 bytes down.
1132 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1133 masm_->cmp(sp, Operand(ip));
1134 StackCheckStub stub;
1135 // Call the stub if lower.
1136 masm_->mov(pc,
1137 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1138 RelocInfo::CODE_TARGET),
1139 LeaveCC,
1140 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141}
1142
1143
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001144void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1145#ifdef DEBUG
1146 int original_height = frame_->height();
1147#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001148 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001149 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1150 VisitAndSpill(statements->at(i));
1151 }
1152 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1153}
1154
1155
ager@chromium.org7c537e22008-10-16 08:43:32 +00001156void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001157#ifdef DEBUG
1158 int original_height = frame_->height();
1159#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001160 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001163 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001164 VisitStatementsAndSpill(node->statements());
1165 if (node->break_target()->is_linked()) {
1166 node->break_target()->Bind();
1167 }
1168 node->break_target()->Unuse();
1169 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170}
1171
1172
ager@chromium.org7c537e22008-10-16 08:43:32 +00001173void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001174 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001175 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001176 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001177 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001178 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001179 frame_->EmitPush(r0);
1180 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001181 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182}
1183
1184
ager@chromium.org7c537e22008-10-16 08:43:32 +00001185void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001186#ifdef DEBUG
1187 int original_height = frame_->height();
1188#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001189 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 Comment cmnt(masm_, "[ Declaration");
1191 Variable* var = node->proxy()->var();
1192 ASSERT(var != NULL); // must have been resolved
1193 Slot* slot = var->slot();
1194
1195 // If it was not possible to allocate the variable at compile time,
1196 // we need to "declare" it at runtime to make sure it actually
1197 // exists in the local context.
1198 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1199 // Variables with a "LOOKUP" slot were introduced as non-locals
1200 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001201 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001203 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001204 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001205 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 // Declaration nodes are always declared in only two modes.
1207 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1208 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001209 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001210 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 // Push initial value, if any.
1212 // Note: For variables we must not push an initial value (such as
1213 // 'undefined') because we may have a (legal) redeclaration and we
1214 // must not destroy the current value.
1215 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001216 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001221 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001225 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 return;
1228 }
1229
1230 ASSERT(!var->is_global());
1231
1232 // If we have a function or a constant, we need to initialize the variable.
1233 Expression* val = NULL;
1234 if (node->mode() == Variable::CONST) {
1235 val = new Literal(Factory::the_hole_value());
1236 } else {
1237 val = node->fun(); // NULL if we don't have a function
1238 }
1239
1240 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001241 {
1242 // Set initial value.
1243 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001244 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001245 target.SetValue(NOT_CONST_INIT);
1246 // The reference is removed from the stack (preserving TOS) when
1247 // it goes out of scope.
1248 }
1249 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001250 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001252 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253}
1254
1255
ager@chromium.org7c537e22008-10-16 08:43:32 +00001256void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001257#ifdef DEBUG
1258 int original_height = frame_->height();
1259#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001260 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001262 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Expression* expression = node->expression();
1264 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001265 LoadAndSpill(expression);
1266 frame_->Drop();
1267 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268}
1269
1270
ager@chromium.org7c537e22008-10-16 08:43:32 +00001271void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001272#ifdef DEBUG
1273 int original_height = frame_->height();
1274#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280}
1281
1282
ager@chromium.org7c537e22008-10-16 08:43:32 +00001283void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001284#ifdef DEBUG
1285 int original_height = frame_->height();
1286#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001287 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 // Generate different code depending on which parts of the if statement
1290 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 bool has_then_stm = node->HasThenStatement();
1292 bool has_else_stm = node->HasElseStatement();
1293
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001294 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001296 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001298 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001299 JumpTarget then;
1300 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001302 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001303 if (frame_ != NULL) {
1304 Branch(false, &else_);
1305 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001307 if (frame_ != NULL || then.is_linked()) {
1308 then.Bind();
1309 VisitAndSpill(node->then_statement());
1310 }
1311 if (frame_ != NULL) {
1312 exit.Jump();
1313 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315 if (else_.is_linked()) {
1316 else_.Bind();
1317 VisitAndSpill(node->else_statement());
1318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319
1320 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001321 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001323 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001325 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001326 if (frame_ != NULL) {
1327 Branch(false, &exit);
1328 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001330 if (frame_ != NULL || then.is_linked()) {
1331 then.Bind();
1332 VisitAndSpill(node->then_statement());
1333 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
1335 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001336 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001338 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001340 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001341 if (frame_ != NULL) {
1342 Branch(true, &exit);
1343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001345 if (frame_ != NULL || else_.is_linked()) {
1346 else_.Bind();
1347 VisitAndSpill(node->else_statement());
1348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349
1350 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001351 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 ASSERT(!has_then_stm && !has_else_stm);
1353 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001354 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 if (frame_ != NULL) {
1356 if (has_cc()) {
1357 cc_reg_ = al;
1358 } else {
1359 frame_->Drop();
1360 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 }
1362 }
1363
1364 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001365 if (exit.is_linked()) {
1366 exit.Bind();
1367 }
1368 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369}
1370
1371
ager@chromium.org7c537e22008-10-16 08:43:32 +00001372void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001373 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001375 CodeForStatementPosition(node);
1376 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377}
1378
1379
ager@chromium.org7c537e22008-10-16 08:43:32 +00001380void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001381 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001383 CodeForStatementPosition(node);
1384 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385}
1386
1387
ager@chromium.org7c537e22008-10-16 08:43:32 +00001388void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001389 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001391
ager@chromium.org4af710e2009-09-15 12:20:11 +00001392 CodeForStatementPosition(node);
1393 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001394 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001395 frame_->EmitPop(r0);
1396 function_return_.Jump();
1397 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 // Pop the result from the frame and prepare the frame for
1399 // returning thus making it easier to merge.
1400 frame_->EmitPop(r0);
1401 frame_->PrepareForReturn();
1402
1403 function_return_.Jump();
1404 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405}
1406
1407
ager@chromium.org7c537e22008-10-16 08:43:32 +00001408void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001409#ifdef DEBUG
1410 int original_height = frame_->height();
1411#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001412 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414 CodeForStatementPosition(node);
1415 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001416 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001417 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001418 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001420 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001421#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001422 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423 __ cmp(r0, Operand(cp));
1424 verified_true.Branch(eq);
1425 __ stop("PushContext: r0 is expected to be the same as cp");
1426 verified_true.Bind();
1427#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001429 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001430 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431}
1432
1433
ager@chromium.org7c537e22008-10-16 08:43:32 +00001434void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435#ifdef DEBUG
1436 int original_height = frame_->height();
1437#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001438 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001440 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 // Pop context.
1442 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1443 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001444 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446}
1447
1448
ager@chromium.org7c537e22008-10-16 08:43:32 +00001449void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450#ifdef DEBUG
1451 int original_height = frame_->height();
1452#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001453 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001456 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001458 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001459
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001460 JumpTarget next_test;
1461 JumpTarget fall_through;
1462 JumpTarget default_entry;
1463 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 ZoneList<CaseClause*>* cases = node->cases();
1465 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001466 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467
1468 for (int i = 0; i < length; i++) {
1469 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001471 // Remember the default clause and compile it at the end.
1472 default_clause = clause;
1473 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 }
1475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001476 Comment cmnt(masm_, "[ Case clause");
1477 // Compile the test.
1478 next_test.Bind();
1479 next_test.Unuse();
1480 // Duplicate TOS.
1481 __ ldr(r0, frame_->Top());
1482 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001483 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001484 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001485
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001486 // Before entering the body from the test, remove the switch value from
1487 // the stack.
1488 frame_->Drop();
1489
1490 // Label the body so that fall through is enabled.
1491 if (i > 0 && cases->at(i - 1)->is_default()) {
1492 default_exit.Bind();
1493 } else {
1494 fall_through.Bind();
1495 fall_through.Unuse();
1496 }
1497 VisitStatementsAndSpill(clause->statements());
1498
1499 // If control flow can fall through from the body, jump to the next body
1500 // or the end of the statement.
1501 if (frame_ != NULL) {
1502 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1503 default_entry.Jump();
1504 } else {
1505 fall_through.Jump();
1506 }
1507 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 }
1509
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001510 // The final "test" removes the switch value.
1511 next_test.Bind();
1512 frame_->Drop();
1513
1514 // If there is a default clause, compile it.
1515 if (default_clause != NULL) {
1516 Comment cmnt(masm_, "[ Default clause");
1517 default_entry.Bind();
1518 VisitStatementsAndSpill(default_clause->statements());
1519 // If control flow can fall out of the default and there is a case after
1520 // it, jup to that case's body.
1521 if (frame_ != NULL && default_exit.is_bound()) {
1522 default_exit.Jump();
1523 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001524 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001526 if (fall_through.is_linked()) {
1527 fall_through.Bind();
1528 }
1529
1530 if (node->break_target()->is_linked()) {
1531 node->break_target()->Bind();
1532 }
1533 node->break_target()->Unuse();
1534 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535}
1536
1537
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001538void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001539#ifdef DEBUG
1540 int original_height = frame_->height();
1541#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001542 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001543 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001545 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001546 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001548 // Label the top of the loop for the backward CFG edge. If the test
1549 // is always true we can use the continue target, and if the test is
1550 // always false there is no need.
1551 ConditionAnalysis info = AnalyzeCondition(node->cond());
1552 switch (info) {
1553 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001554 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001555 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001556 break;
1557 case ALWAYS_FALSE:
1558 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1559 break;
1560 case DONT_KNOW:
1561 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1562 body.Bind();
1563 break;
1564 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001565
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001566 CheckStack(); // TODO(1222600): ignore if body contains calls.
1567 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001568
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001569 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001570 switch (info) {
1571 case ALWAYS_TRUE:
1572 // If control can fall off the end of the body, jump back to the
1573 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001574 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001575 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001578 case ALWAYS_FALSE:
1579 // If we have a continue in the body, we only have to bind its
1580 // jump target.
1581 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001582 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001583 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001584 break;
1585 case DONT_KNOW:
1586 // We have to compile the test expression if it can be reached by
1587 // control flow falling out of the body or via continue.
1588 if (node->continue_target()->is_linked()) {
1589 node->continue_target()->Bind();
1590 }
1591 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001592 Comment cmnt(masm_, "[ DoWhileCondition");
1593 CodeForDoWhileConditionPosition(node);
1594 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001596 // A invalid frame here indicates that control did not
1597 // fall out of the test expression.
1598 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001599 }
1600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 break;
1602 }
1603
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001604 if (node->break_target()->is_linked()) {
1605 node->break_target()->Bind();
1606 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001607 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1608}
1609
1610
1611void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1612#ifdef DEBUG
1613 int original_height = frame_->height();
1614#endif
1615 VirtualFrame::SpilledScope spilled_scope;
1616 Comment cmnt(masm_, "[ WhileStatement");
1617 CodeForStatementPosition(node);
1618
1619 // If the test is never true and has no side effects there is no need
1620 // to compile the test or body.
1621 ConditionAnalysis info = AnalyzeCondition(node->cond());
1622 if (info == ALWAYS_FALSE) return;
1623
1624 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1625
1626 // Label the top of the loop with the continue target for the backward
1627 // CFG edge.
1628 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1629 node->continue_target()->Bind();
1630
1631 if (info == DONT_KNOW) {
1632 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001633 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001634 if (has_valid_frame()) {
1635 // A NULL frame indicates that control did not fall out of the
1636 // test expression.
1637 Branch(false, node->break_target());
1638 }
1639 if (has_valid_frame() || body.is_linked()) {
1640 body.Bind();
1641 }
1642 }
1643
1644 if (has_valid_frame()) {
1645 CheckStack(); // TODO(1222600): ignore if body contains calls.
1646 VisitAndSpill(node->body());
1647
1648 // If control flow can fall out of the body, jump back to the top.
1649 if (has_valid_frame()) {
1650 node->continue_target()->Jump();
1651 }
1652 }
1653 if (node->break_target()->is_linked()) {
1654 node->break_target()->Bind();
1655 }
1656 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1657}
1658
1659
1660void CodeGenerator::VisitForStatement(ForStatement* node) {
1661#ifdef DEBUG
1662 int original_height = frame_->height();
1663#endif
1664 VirtualFrame::SpilledScope spilled_scope;
1665 Comment cmnt(masm_, "[ ForStatement");
1666 CodeForStatementPosition(node);
1667 if (node->init() != NULL) {
1668 VisitAndSpill(node->init());
1669 }
1670
1671 // If the test is never true there is no need to compile the test or
1672 // body.
1673 ConditionAnalysis info = AnalyzeCondition(node->cond());
1674 if (info == ALWAYS_FALSE) return;
1675
1676 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1677
1678 // If there is no update statement, label the top of the loop with the
1679 // continue target, otherwise with the loop target.
1680 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1681 if (node->next() == NULL) {
1682 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1683 node->continue_target()->Bind();
1684 } else {
1685 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1686 loop.Bind();
1687 }
1688
1689 // If the test is always true, there is no need to compile it.
1690 if (info == DONT_KNOW) {
1691 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001692 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001693 if (has_valid_frame()) {
1694 Branch(false, node->break_target());
1695 }
1696 if (has_valid_frame() || body.is_linked()) {
1697 body.Bind();
1698 }
1699 }
1700
1701 if (has_valid_frame()) {
1702 CheckStack(); // TODO(1222600): ignore if body contains calls.
1703 VisitAndSpill(node->body());
1704
1705 if (node->next() == NULL) {
1706 // If there is no update statement and control flow can fall out
1707 // of the loop, jump directly to the continue label.
1708 if (has_valid_frame()) {
1709 node->continue_target()->Jump();
1710 }
1711 } else {
1712 // If there is an update statement and control flow can reach it
1713 // via falling out of the body of the loop or continuing, we
1714 // compile the update statement.
1715 if (node->continue_target()->is_linked()) {
1716 node->continue_target()->Bind();
1717 }
1718 if (has_valid_frame()) {
1719 // Record source position of the statement as this code which is
1720 // after the code for the body actually belongs to the loop
1721 // statement and not the body.
1722 CodeForStatementPosition(node);
1723 VisitAndSpill(node->next());
1724 loop.Jump();
1725 }
1726 }
1727 }
1728 if (node->break_target()->is_linked()) {
1729 node->break_target()->Bind();
1730 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001731 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732}
1733
1734
ager@chromium.org7c537e22008-10-16 08:43:32 +00001735void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001736#ifdef DEBUG
1737 int original_height = frame_->height();
1738#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001739 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001741 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001743 JumpTarget primitive;
1744 JumpTarget jsobject;
1745 JumpTarget fixed_array;
1746 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1747 JumpTarget end_del_check;
1748 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749
1750 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001751 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752
1753 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1754 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001755 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001756 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1757 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001759 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1760 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001761 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762
1763 // Stack layout in body:
1764 // [iteration counter (Smi)]
1765 // [length of array]
1766 // [FixedArray]
1767 // [Map or 0]
1768 // [Object]
1769
1770 // Check if enumerable is already a JSObject
1771 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001772 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001773 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001774 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001776 primitive.Bind();
1777 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001778 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001780 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001782 // r0: value to be iterated over
1783 frame_->EmitPush(r0); // Push the object being iterated over.
1784
1785 // Check cache validity in generated code. This is a fast case for
1786 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1787 // guarantee cache validity, call the runtime system to check cache
1788 // validity or get the property names in a fixed array.
1789 JumpTarget call_runtime;
1790 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1791 JumpTarget check_prototype;
1792 JumpTarget use_cache;
1793 __ mov(r1, Operand(r0));
1794 loop.Bind();
1795 // Check that there are no elements.
1796 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1797 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1798 __ cmp(r2, r4);
1799 call_runtime.Branch(ne);
1800 // Check that instance descriptors are not empty so that we can
1801 // check for an enum cache. Leave the map in r3 for the subsequent
1802 // prototype load.
1803 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1804 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1805 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1806 __ cmp(r2, ip);
1807 call_runtime.Branch(eq);
1808 // Check that there in an enum cache in the non-empty instance
1809 // descriptors. This is the case if the next enumeration index
1810 // field does not contain a smi.
1811 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1812 __ tst(r2, Operand(kSmiTagMask));
1813 call_runtime.Branch(eq);
1814 // For all objects but the receiver, check that the cache is empty.
1815 // r4: empty fixed array root.
1816 __ cmp(r1, r0);
1817 check_prototype.Branch(eq);
1818 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1819 __ cmp(r2, r4);
1820 call_runtime.Branch(ne);
1821 check_prototype.Bind();
1822 // Load the prototype from the map and loop if non-null.
1823 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1824 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1825 __ cmp(r1, ip);
1826 loop.Branch(ne);
1827 // The enum cache is valid. Load the map of the object being
1828 // iterated over and use the cache for the iteration.
1829 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1830 use_cache.Jump();
1831
1832 call_runtime.Bind();
1833 // Call the runtime to get the property names for the object.
1834 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001837 // If we got a map from the runtime call, we can do a fast
1838 // modification check. Otherwise, we got a fixed array, and we have
1839 // to do a slow check.
1840 // r0: map or fixed array (result from call to
1841 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 __ mov(r2, Operand(r0));
1843 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001844 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1845 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001848 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001850 // r0: map (either the result from a call to
1851 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1852 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 __ mov(r1, Operand(r0));
1854 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1855 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1856 __ ldr(r2,
1857 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1858
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 frame_->EmitPush(r0); // map
1860 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001861 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001864 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 frame_->EmitPush(r0);
1866 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 frame_->EmitPush(r1); // insert 0 in place of Map
1871 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872
1873 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001874 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001877 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
1880 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001881 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001882 // sp[0] : index
1883 // sp[1] : array/enum cache length
1884 // sp[2] : array or enum cache
1885 // sp[3] : 0 or map
1886 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001887 // Grab the current frame's height for the break and continue
1888 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001889 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1890 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001892 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1893 __ ldr(r1, frame_->ElementAt(1)); // load the length
1894 __ cmp(r0, Operand(r1)); // compare to the array length
1895 node->break_target()->Branch(hs);
1896
1897 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001900 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1902 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1903
1904 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001905 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 // Check if this (still) matches the map of the enumerable.
1907 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1910 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912
1913 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1915 frame_->EmitPush(r0);
1916 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001917 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001918 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919
1920 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001921 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1922 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 end_del_check.Bind();
1926 // Store the entry in the 'each' expression and take another spin in the
1927 // loop. r3: i'th entry of the enum cache (or string there of)
1928 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 { Reference each(this, node->each());
1930 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001931 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001932 __ ldr(r0, frame_->ElementAt(each.size()));
1933 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001934 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001935 // If the reference was to a slot we rely on the convenient property
1936 // that it doesn't matter whether a value (eg, r3 pushed above) is
1937 // right on top of or right underneath a zero-sized reference.
1938 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001939 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001940 // It's safe to pop the value lying on top of the reference before
1941 // unloading the reference itself (which preserves the top of stack,
1942 // ie, now the topmost value of the non-zero sized reference), since
1943 // we will discard the top of stack after unloading the reference
1944 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001945 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001946 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 }
1948 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001949 // Discard the i'th entry pushed above or else the remainder of the
1950 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001951 frame_->Drop();
1952
1953 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 VisitAndSpill(node->body());
1956
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001957 // Next. Reestablish a spilled frame in case we are coming here via
1958 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001960 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 frame_->EmitPop(r0);
1962 __ add(r0, r0, Operand(Smi::FromInt(1)));
1963 frame_->EmitPush(r0);
1964 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001966 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1967 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001969 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970
1971 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001972 exit.Bind();
1973 node->continue_target()->Unuse();
1974 node->break_target()->Unuse();
1975 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976}
1977
1978
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001979void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001980#ifdef DEBUG
1981 int original_height = frame_->height();
1982#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001983 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001984 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001987 JumpTarget try_block;
1988 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
1994 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001995 Variable* catch_var = node->catch_var()->var();
1996 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
1997 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998
1999 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002002 VisitStatementsAndSpill(node->catch_block()->statements());
2003 if (frame_ != NULL) {
2004 exit.Jump();
2005 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006
2007
2008 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002011 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2012 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002014 // Shadow the labels for all escapes from the try block, including
2015 // returns. During shadowing, the original label is hidden as the
2016 // LabelShadow and operations on the original actually affect the
2017 // shadowing label.
2018 //
2019 // We should probably try to unify the escaping labels and the return
2020 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 int nof_escapes = node->escaping_targets()->length();
2022 List<ShadowTarget*> shadows(1 + nof_escapes);
2023
2024 // Add the shadow target for the function return.
2025 static const int kReturnShadowIndex = 0;
2026 shadows.Add(new ShadowTarget(&function_return_));
2027 bool function_return_was_shadowed = function_return_is_shadowed_;
2028 function_return_is_shadowed_ = true;
2029 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2030
2031 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002033 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 }
2035
2036 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002037 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038
2039 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002040 // After shadowing stops, the original labels are unshadowed and the
2041 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 bool has_unlinks = false;
2043 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002045 has_unlinks = has_unlinks || shadows[i]->is_linked();
2046 }
2047 function_return_is_shadowed_ = function_return_was_shadowed;
2048
2049 // Get an external reference to the handler address.
2050 ExternalReference handler_address(Top::k_handler_address);
2051
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 // If we can fall off the end of the try block, unlink from try chain.
2053 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002054 // The next handler address is on top of the frame. Unlink from
2055 // the handler list and drop the rest of this handler from the
2056 // frame.
2057 ASSERT(StackHandlerConstants::kNextOffset == 0);
2058 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 __ mov(r3, Operand(handler_address));
2060 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002061 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002062 if (has_unlinks) {
2063 exit.Jump();
2064 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 }
2066
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002067 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002068 // jumped to. Deallocate each shadow target.
2069 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002071 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 shadows[i]->Bind();
2073 // Because we can be jumping here (to spilled code) from unspilled
2074 // code, we need to reestablish a spilled frame at this block.
2075 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 // Reload sp from the top handler, because some statements that we
2078 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002081 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002083 ASSERT(StackHandlerConstants::kNextOffset == 0);
2084 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002086 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2089 frame_->PrepareForReturn();
2090 }
2091 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 }
2093 }
2094
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002095 exit.Bind();
2096 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097}
2098
2099
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002100void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002101#ifdef DEBUG
2102 int original_height = frame_->height();
2103#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002104 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002105 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002106 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107
2108 // State: Used to keep track of reason for entering the finally
2109 // block. Should probably be extended to hold information for
2110 // break/continue from within the try block.
2111 enum { FALLING, THROWING, JUMPING };
2112
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002113 JumpTarget try_block;
2114 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 // In case of thrown exceptions, this is where we continue.
2120 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122
2123 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002124 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2127 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002129 // Shadow the labels for all escapes from the try block, including
2130 // returns. Shadowing hides the original label as the LabelShadow and
2131 // operations on the original actually affect the shadowing label.
2132 //
2133 // We should probably try to unify the escaping labels and the return
2134 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 int nof_escapes = node->escaping_targets()->length();
2136 List<ShadowTarget*> shadows(1 + nof_escapes);
2137
2138 // Add the shadow target for the function return.
2139 static const int kReturnShadowIndex = 0;
2140 shadows.Add(new ShadowTarget(&function_return_));
2141 bool function_return_was_shadowed = function_return_is_shadowed_;
2142 function_return_is_shadowed_ = true;
2143 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2144
2145 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002147 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 }
2149
2150 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002153 // Stop the introduced shadowing and count the number of required unlinks.
2154 // After shadowing stops, the original labels are unshadowed and the
2155 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 shadows[i]->StopShadowing();
2159 if (shadows[i]->is_linked()) nof_unlinks++;
2160 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002163 // Get an external reference to the handler address.
2164 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 // If we can fall off the end of the try block, unlink from the try
2167 // chain and set the state on the frame to FALLING.
2168 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002169 // The next handler address is on top of the frame.
2170 ASSERT(StackHandlerConstants::kNextOffset == 0);
2171 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 __ mov(r3, Operand(handler_address));
2173 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002174 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175
2176 // Fake a top of stack value (unneeded when FALLING) and set the
2177 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002178 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 frame_->EmitPush(r0);
2180 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2181 if (nof_unlinks > 0) {
2182 finally_block.Jump();
2183 }
2184 }
2185
2186 // Generate code to unlink and set the state for the (formerly)
2187 // shadowing targets that have been jumped to.
2188 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002190 // If we have come from the shadowed return, the return value is
2191 // in (a non-refcounted reference to) r0. We must preserve it
2192 // until it is pushed.
2193 //
2194 // Because we can be jumping here (to spilled code) from
2195 // unspilled code, we need to reestablish a spilled frame at
2196 // this block.
2197 shadows[i]->Bind();
2198 frame_->SpillAll();
2199
2200 // Reload sp from the top handler, because some statements that
2201 // we break from (eg, for...in) may have left stuff on the
2202 // stack.
2203 __ mov(r3, Operand(handler_address));
2204 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002205 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206
2207 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002208 // handler address is currently on top of the frame.
2209 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002210 frame_->EmitPop(r1);
2211 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002212 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213
2214 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002215 // If this label shadowed the function return, materialize the
2216 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002218 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002219 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002220 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 }
2223 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002224 if (--nof_unlinks > 0) {
2225 // If this is not the last unlink block, jump around the next.
2226 finally_block.Jump();
2227 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 }
2229 }
2230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002232 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002234 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002235 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002236
2237 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002238 // and the state - while evaluating the finally block.
2239 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002241 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 if (has_valid_frame()) {
2244 // Restore state and return value or faked TOS.
2245 frame_->EmitPop(r2);
2246 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 }
2248
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 // Generate code to jump to the right destination for all used
2250 // formerly shadowing targets. Deallocate each shadow target.
2251 for (int i = 0; i < shadows.length(); i++) {
2252 if (has_valid_frame() && shadows[i]->is_bound()) {
2253 JumpTarget* original = shadows[i]->other_target();
2254 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2255 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002256 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 skip.Branch(ne);
2258 frame_->PrepareForReturn();
2259 original->Jump();
2260 skip.Bind();
2261 } else {
2262 original->Branch(eq);
2263 }
2264 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002265 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 if (has_valid_frame()) {
2268 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002269 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2271 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002273 // Rethrow exception.
2274 frame_->EmitPush(r0);
2275 frame_->CallRuntime(Runtime::kReThrow, 1);
2276
2277 // Done.
2278 exit.Bind();
2279 }
2280 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281}
2282
2283
ager@chromium.org7c537e22008-10-16 08:43:32 +00002284void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002285#ifdef DEBUG
2286 int original_height = frame_->height();
2287#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002288 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002291#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002293#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002294 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296}
2297
2298
ager@chromium.org7c537e22008-10-16 08:43:32 +00002299void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002300 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 ASSERT(boilerplate->IsBoilerplate());
2302
ager@chromium.org3811b432009-10-28 14:53:37 +00002303 __ mov(r0, Operand(boilerplate));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002304 // Use the fast case closure allocation code that allocates in new
2305 // space for nested functions that don't need literals cloning.
2306 if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) {
2307 FastNewClosureStub stub;
2308 frame_->EmitPush(r0);
2309 frame_->CallStub(&stub, 1);
2310 frame_->EmitPush(r0);
2311 } else {
2312 // Create a new closure.
2313 frame_->EmitPush(cp);
2314 frame_->EmitPush(r0);
2315 frame_->CallRuntime(Runtime::kNewClosure, 2);
2316 frame_->EmitPush(r0);
2317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318}
2319
2320
ager@chromium.org7c537e22008-10-16 08:43:32 +00002321void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002322#ifdef DEBUG
2323 int original_height = frame_->height();
2324#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002325 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 Comment cmnt(masm_, "[ FunctionLiteral");
2327
2328 // Build the function boilerplate and instantiate it.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002329 Handle<JSFunction> boilerplate =
2330 Compiler::BuildBoilerplate(node, script_, this);
kasper.lund212ac232008-07-16 07:07:30 +00002331 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002332 if (HasStackOverflow()) {
2333 ASSERT(frame_->height() == original_height);
2334 return;
2335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338}
2339
2340
ager@chromium.org7c537e22008-10-16 08:43:32 +00002341void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343#ifdef DEBUG
2344 int original_height = frame_->height();
2345#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002346 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2348 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350}
2351
2352
ager@chromium.org7c537e22008-10-16 08:43:32 +00002353void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002354#ifdef DEBUG
2355 int original_height = frame_->height();
2356#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002357 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002359 JumpTarget then;
2360 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002361 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002362 if (has_valid_frame()) {
2363 Branch(false, &else_);
2364 }
2365 if (has_valid_frame() || then.is_linked()) {
2366 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002367 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002368 }
2369 if (else_.is_linked()) {
2370 JumpTarget exit;
2371 if (has_valid_frame()) exit.Jump();
2372 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002373 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002374 if (exit.is_linked()) exit.Bind();
2375 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377}
2378
2379
ager@chromium.org7c537e22008-10-16 08:43:32 +00002380void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002381 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002382 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002383 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002385 JumpTarget slow;
2386 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002387
2388 // Generate fast-case code for variables that might be shadowed by
2389 // eval-introduced variables. Eval is used a lot without
2390 // introducing variables. In those cases, we do not want to
2391 // perform a runtime call for all variables in the scope
2392 // containing the eval.
2393 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2394 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002395 // If there was no control flow to slow, we can exit early.
2396 if (!slow.is_linked()) {
2397 frame_->EmitPush(r0);
2398 return;
2399 }
2400
2401 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002402
2403 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2404 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2405 // Only generate the fast case for locals that rewrite to slots.
2406 // This rules out argument loads.
2407 if (potential_slot != NULL) {
2408 __ ldr(r0,
2409 ContextSlotOperandCheckExtensions(potential_slot,
2410 r1,
2411 r2,
2412 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002413 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002414 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2415 __ cmp(r0, ip);
2416 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002417 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002418 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002419 // ContextSlotOperandCheckExtensions so we have to jump around
2420 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002421 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002422 }
2423 }
2424
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 slow.Bind();
2426 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002427 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002428 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429
ager@chromium.org7c537e22008-10-16 08:43:32 +00002430 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002431 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002432 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002433 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002435
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002436 done.Bind();
2437 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438
2439 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002440 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002441 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002442 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002443 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002444 // Const slots may contain 'the hole' value (the constant hasn't been
2445 // initialized yet) which needs to be converted into the 'undefined'
2446 // value.
2447 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002448 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002449 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2450 __ cmp(r0, ip);
2451 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453 }
2454 }
2455}
2456
2457
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002458void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2459 ASSERT(slot != NULL);
2460 if (slot->type() == Slot::LOOKUP) {
2461 ASSERT(slot->var()->is_dynamic());
2462
2463 // For now, just do a runtime call.
2464 frame_->EmitPush(cp);
2465 __ mov(r0, Operand(slot->var()->name()));
2466 frame_->EmitPush(r0);
2467
2468 if (init_state == CONST_INIT) {
2469 // Same as the case for a normal store, but ignores attribute
2470 // (e.g. READ_ONLY) of context slot so that we can initialize
2471 // const properties (introduced via eval("const foo = (some
2472 // expr);")). Also, uses the current function context instead of
2473 // the top context.
2474 //
2475 // Note that we must declare the foo upon entry of eval(), via a
2476 // context slot declaration, but we cannot initialize it at the
2477 // same time, because the const declaration may be at the end of
2478 // the eval code (sigh...) and the const variable may have been
2479 // used before (where its value is 'undefined'). Thus, we can only
2480 // do the initialization when we actually encounter the expression
2481 // and when the expression operands are defined and valid, and
2482 // thus we need the split into 2 operations: declaration of the
2483 // context slot followed by initialization.
2484 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2485 } else {
2486 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2487 }
2488 // Storing a variable must keep the (new) value on the expression
2489 // stack. This is necessary for compiling assignment expressions.
2490 frame_->EmitPush(r0);
2491
2492 } else {
2493 ASSERT(!slot->var()->is_dynamic());
2494
2495 JumpTarget exit;
2496 if (init_state == CONST_INIT) {
2497 ASSERT(slot->var()->mode() == Variable::CONST);
2498 // Only the first const initialization must be executed (the slot
2499 // still contains 'the hole' value). When the assignment is
2500 // executed, the code is identical to a normal store (see below).
2501 Comment cmnt(masm_, "[ Init const");
2502 __ ldr(r2, SlotOperand(slot, r2));
2503 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2504 __ cmp(r2, ip);
2505 exit.Branch(ne);
2506 }
2507
2508 // We must execute the store. Storing a variable must keep the
2509 // (new) value on the stack. This is necessary for compiling
2510 // assignment expressions.
2511 //
2512 // Note: We will reach here even with slot->var()->mode() ==
2513 // Variable::CONST because of const declarations which will
2514 // initialize consts to 'the hole' value and by doing so, end up
2515 // calling this code. r2 may be loaded with context; used below in
2516 // RecordWrite.
2517 frame_->EmitPop(r0);
2518 __ str(r0, SlotOperand(slot, r2));
2519 frame_->EmitPush(r0);
2520 if (slot->type() == Slot::CONTEXT) {
2521 // Skip write barrier if the written value is a smi.
2522 __ tst(r0, Operand(kSmiTagMask));
2523 exit.Branch(eq);
2524 // r2 is loaded with context when calling SlotOperand above.
2525 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2526 __ mov(r3, Operand(offset));
2527 __ RecordWrite(r2, r3, r1);
2528 }
2529 // If we definitely did not jump over the assignment, we do not need
2530 // to bind the exit label. Doing so can defeat peephole
2531 // optimization.
2532 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2533 exit.Bind();
2534 }
2535 }
2536}
2537
2538
ager@chromium.org381abbb2009-02-25 13:23:22 +00002539void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2540 TypeofState typeof_state,
2541 Register tmp,
2542 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002544 // Check that no extension objects have been created by calls to
2545 // eval from the current scope to the global scope.
2546 Register context = cp;
2547 Scope* s = scope();
2548 while (s != NULL) {
2549 if (s->num_heap_slots() > 0) {
2550 if (s->calls_eval()) {
2551 // Check that extension is NULL.
2552 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2553 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002555 }
2556 // Load next context in chain.
2557 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2558 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2559 context = tmp;
2560 }
2561 // If no outer scope calls eval, we do not need to check more
2562 // context extensions.
2563 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2564 s = s->outer_scope();
2565 }
2566
2567 if (s->is_eval_scope()) {
2568 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002569 if (!context.is(tmp)) {
2570 __ mov(tmp, Operand(context));
2571 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002572 __ bind(&next);
2573 // Terminate at global context.
2574 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002575 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2576 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002577 __ b(eq, &fast);
2578 // Check that extension is NULL.
2579 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2580 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002581 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002582 // Load next context in chain.
2583 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2584 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2585 __ b(&next);
2586 __ bind(&fast);
2587 }
2588
2589 // All extension objects were empty and it is safe to use a global
2590 // load IC call.
2591 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2592 // Load the global object.
2593 LoadGlobal();
2594 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002595 Result name(r2);
2596 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002597 // Call IC stub.
2598 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002600 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002602 }
2603
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604 // Drop the global object. The result is in r0.
2605 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002606}
2607
2608
ager@chromium.org7c537e22008-10-16 08:43:32 +00002609void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002610#ifdef DEBUG
2611 int original_height = frame_->height();
2612#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002613 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002614 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002615 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002617}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618
ager@chromium.org7c537e22008-10-16 08:43:32 +00002619
2620void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002621#ifdef DEBUG
2622 int original_height = frame_->height();
2623#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002624 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002625 Comment cmnt(masm_, "[ VariableProxy");
2626
2627 Variable* var = node->var();
2628 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002629 if (expr != NULL) {
2630 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002632 ASSERT(var->is_global());
2633 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002634 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002636 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637}
2638
2639
ager@chromium.org7c537e22008-10-16 08:43:32 +00002640void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641#ifdef DEBUG
2642 int original_height = frame_->height();
2643#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002644 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002646 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002647 frame_->EmitPush(r0);
2648 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649}
2650
2651
ager@chromium.org7c537e22008-10-16 08:43:32 +00002652void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653#ifdef DEBUG
2654 int original_height = frame_->height();
2655#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002656 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 Comment cmnt(masm_, "[ RexExp Literal");
2658
2659 // Retrieve the literal array and check the allocated entry.
2660
2661 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002662 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663
2664 // Load the literals array of the function.
2665 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2666
2667 // Load the literal at the ast saved index.
2668 int literal_offset =
2669 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2670 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2671
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002672 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002673 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2674 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676
2677 // If the entry is undefined we call the runtime system to computed
2678 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002679 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002680 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002682 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002684 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 frame_->EmitPush(r0);
2686 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002687 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691 frame_->EmitPush(r2);
2692 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693}
2694
2695
ager@chromium.org7c537e22008-10-16 08:43:32 +00002696void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697#ifdef DEBUG
2698 int original_height = frame_->height();
2699#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002700 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 Comment cmnt(masm_, "[ ObjectLiteral");
2702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002704 __ ldr(r2, frame_->Function());
2705 // Literal array.
2706 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2707 // Literal index.
2708 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2709 // Constant properties.
2710 __ mov(r0, Operand(node->constant_properties()));
2711 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2712 if (node->depth() > 1) {
2713 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3);
2714 } else {
2715 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002716 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002717 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002718 // r0: created object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002719
2720 for (int i = 0; i < node->properties()->length(); i++) {
2721 ObjectLiteral::Property* property = node->properties()->at(i);
2722 Literal* key = property->key();
2723 Expression* value = property->value();
2724 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725 case ObjectLiteral::Property::CONSTANT:
2726 break;
2727 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2728 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2729 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 case ObjectLiteral::Property::COMPUTED: // fall through
2731 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002732 frame_->EmitPush(r0); // dup the result
2733 LoadAndSpill(key);
2734 LoadAndSpill(value);
2735 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002736 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002737 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 break;
2739 }
2740 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002741 frame_->EmitPush(r0);
2742 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002743 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002744 frame_->EmitPush(r0);
2745 LoadAndSpill(value);
2746 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002747 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 break;
2749 }
2750 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002751 frame_->EmitPush(r0);
2752 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002753 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0);
2755 LoadAndSpill(value);
2756 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002757 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758 break;
2759 }
2760 }
2761 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002762 ASSERT(frame_->height() == original_height + 1);
2763}
2764
2765
ager@chromium.org7c537e22008-10-16 08:43:32 +00002766void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767#ifdef DEBUG
2768 int original_height = frame_->height();
2769#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002770 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002772
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002773 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002774 __ ldr(r2, frame_->Function());
2775 // Literals array.
2776 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2777 // Literal index.
2778 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2779 // Constant elements.
2780 __ mov(r0, Operand(node->constant_elements()));
2781 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2782 if (node->depth() > 1) {
2783 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
2784 } else {
2785 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002787 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002788 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002789
2790 // Generate code to set the elements in the array that are not
2791 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792 for (int i = 0; i < node->values()->length(); i++) {
2793 Expression* value = node->values()->at(i);
2794
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002795 // If value is a literal the property value is already set in the
2796 // boilerplate object.
2797 if (value->AsLiteral() != NULL) continue;
2798 // If value is a materialized literal the property value is already set
2799 // in the boilerplate object if it is simple.
2800 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002801
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002802 // The property must be set by generated code.
2803 LoadAndSpill(value);
2804 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002806 // Fetch the object literal.
2807 __ ldr(r1, frame_->Top());
2808 // Get the elements array.
2809 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002811 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002812 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002813 __ str(r0, FieldMemOperand(r1, offset));
2814
2815 // Update the write barrier for the array address.
2816 __ mov(r3, Operand(offset));
2817 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002819 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820}
2821
2822
ager@chromium.org32912102009-01-16 10:38:43 +00002823void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824#ifdef DEBUG
2825 int original_height = frame_->height();
2826#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002827 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002828 // Call runtime routine to allocate the catch extension object and
2829 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002830 Comment cmnt(masm_, "[ CatchExtensionObject");
2831 LoadAndSpill(node->key());
2832 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002833 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2834 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002835 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002836}
2837
2838
ager@chromium.org7c537e22008-10-16 08:43:32 +00002839void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002840#ifdef DEBUG
2841 int original_height = frame_->height();
2842#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002843 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002844 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002845
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002846 { Reference target(this, node->target());
2847 if (target.is_illegal()) {
2848 // Fool the virtual frame into thinking that we left the assignment's
2849 // value on the frame.
2850 __ mov(r0, Operand(Smi::FromInt(0)));
2851 frame_->EmitPush(r0);
2852 ASSERT(frame_->height() == original_height + 1);
2853 return;
2854 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002856 if (node->op() == Token::ASSIGN ||
2857 node->op() == Token::INIT_VAR ||
2858 node->op() == Token::INIT_CONST) {
2859 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002862 // +=, *= and similar binary assignments.
2863 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002864 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002865 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002866 bool overwrite =
2867 (node->value()->AsBinaryOperation() != NULL &&
2868 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002869 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002870 SmiOperation(node->binary_op(),
2871 literal->handle(),
2872 false,
2873 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002874 frame_->EmitPush(r0);
2875
2876 } else {
2877 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002878 GenericBinaryOperation(node->binary_op(),
2879 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880 frame_->EmitPush(r0);
2881 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2885 if (var != NULL &&
2886 (var->mode() == Variable::CONST) &&
2887 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2888 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002891 CodeForSourcePosition(node->position());
2892 if (node->op() == Token::INIT_CONST) {
2893 // Dynamic constant initializations must use the function context
2894 // and initialize the actual constant declared. Dynamic variable
2895 // initializations are simply assignments and use SetValue.
2896 target.SetValue(CONST_INIT);
2897 } else {
2898 target.SetValue(NOT_CONST_INIT);
2899 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900 }
2901 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903}
2904
2905
ager@chromium.org7c537e22008-10-16 08:43:32 +00002906void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907#ifdef DEBUG
2908 int original_height = frame_->height();
2909#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002910 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 Comment cmnt(masm_, "[ Throw");
2912
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002914 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002915 frame_->CallRuntime(Runtime::kThrow, 1);
2916 frame_->EmitPush(r0);
2917 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918}
2919
2920
ager@chromium.org7c537e22008-10-16 08:43:32 +00002921void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002922#ifdef DEBUG
2923 int original_height = frame_->height();
2924#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002925 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002927
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002928 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002929 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002930 }
2931 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932}
2933
2934
ager@chromium.org7c537e22008-10-16 08:43:32 +00002935void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002936#ifdef DEBUG
2937 int original_height = frame_->height();
2938#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002939 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002940 Comment cmnt(masm_, "[ Call");
2941
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002942 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943 ZoneList<Expression*>* args = node->arguments();
2944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002945 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947 Variable* var = function->AsVariableProxy()->AsVariable();
2948 Property* property = function->AsProperty();
2949
2950 // ------------------------------------------------------------------------
2951 // Fast-case: Use inline caching.
2952 // ---
2953 // According to ECMA-262, section 11.2.3, page 44, the function to call
2954 // must be resolved after the arguments have been evaluated. The IC code
2955 // automatically handles this by loading the arguments before the function
2956 // is resolved in cache misses (this also holds for megamorphic calls).
2957 // ------------------------------------------------------------------------
2958
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002959 if (var != NULL && var->is_possibly_eval()) {
2960 // ----------------------------------
2961 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2962 // ----------------------------------
2963
2964 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2965 // resolve the function we need to call and the receiver of the
2966 // call. Then we call the resolved function using the given
2967 // arguments.
2968 // Prepare stack for call to resolved function.
2969 LoadAndSpill(function);
2970 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2971 frame_->EmitPush(r2); // Slot for receiver
2972 int arg_count = args->length();
2973 for (int i = 0; i < arg_count; i++) {
2974 LoadAndSpill(args->at(i));
2975 }
2976
2977 // Prepare stack for call to ResolvePossiblyDirectEval.
2978 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2979 frame_->EmitPush(r1);
2980 if (arg_count > 0) {
2981 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2982 frame_->EmitPush(r1);
2983 } else {
2984 frame_->EmitPush(r2);
2985 }
2986
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002987 // Push the receiver.
2988 __ ldr(r1, frame_->Receiver());
2989 frame_->EmitPush(r1);
2990
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002991 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002992 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002993
2994 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002995 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002996 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
2997
2998 // Call the function.
2999 CodeForSourcePosition(node->position());
3000
3001 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3002 CallFunctionStub call_function(arg_count, in_loop);
3003 frame_->CallStub(&call_function, arg_count + 1);
3004
3005 __ ldr(cp, frame_->Context());
3006 // Remove the function from the stack.
3007 frame_->Drop();
3008 frame_->EmitPush(r0);
3009
3010 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 // ----------------------------------
3012 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3013 // ----------------------------------
3014
3015 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003016 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003017 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003018
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003019 // Pass the global object as the receiver and let the IC stub
3020 // patch the stack to use the global proxy as 'this' in the
3021 // invoked function.
3022 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023
3024 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003025 int arg_count = args->length();
3026 for (int i = 0; i < arg_count; i++) {
3027 LoadAndSpill(args->at(i));
3028 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029
3030 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003031 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3032 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003033 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003034 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3035 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003036 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003038 frame_->Drop();
3039 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040
3041 } else if (var != NULL && var->slot() != NULL &&
3042 var->slot()->type() == Slot::LOOKUP) {
3043 // ----------------------------------
3044 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3045 // ----------------------------------
3046
3047 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003049 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003050 frame_->EmitPush(r0);
3051 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 // r0: slot value; r1: receiver
3053
3054 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003055 frame_->EmitPush(r0); // function
3056 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057
3058 // Call the function.
3059 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003060 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061
3062 } else if (property != NULL) {
3063 // Check if the key is a literal string.
3064 Literal* literal = property->key()->AsLiteral();
3065
3066 if (literal != NULL && literal->handle()->IsSymbol()) {
3067 // ------------------------------------------------------------------
3068 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3069 // ------------------------------------------------------------------
3070
3071 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003072 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003073 frame_->EmitPush(r0);
3074 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075
3076 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003077 int arg_count = args->length();
3078 for (int i = 0; i < arg_count; i++) {
3079 LoadAndSpill(args->at(i));
3080 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081
3082 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003083 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3084 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003085 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003086 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003087 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088
3089 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003091
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093
3094 } else {
3095 // -------------------------------------------
3096 // JavaScript example: 'array[index](1, 2, 3)'
3097 // -------------------------------------------
3098
3099 // Load the function to call from the property through a reference.
3100 Reference ref(this, property);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003101 ref.GetValueAndSpill(); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102
3103 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104 if (property->is_synthetic()) {
3105 LoadGlobalReceiver(r0);
3106 } else {
3107 __ ldr(r0, frame_->ElementAt(ref.size()));
3108 frame_->EmitPush(r0);
3109 }
3110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111 // Call the function.
3112 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 }
3115
3116 } else {
3117 // ----------------------------------
3118 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3119 // ----------------------------------
3120
3121 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003123
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003124 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003125 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127 // Call the function.
3128 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003129 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003131 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003132}
3133
3134
ager@chromium.org7c537e22008-10-16 08:43:32 +00003135void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003136#ifdef DEBUG
3137 int original_height = frame_->height();
3138#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003139 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 Comment cmnt(masm_, "[ CallNew");
3141
3142 // According to ECMA-262, section 11.2.2, page 44, the function
3143 // expression in new calls must be evaluated before the
3144 // arguments. This is different from ordinary calls, where the
3145 // actual function to call is resolved after the arguments have been
3146 // evaluated.
3147
3148 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003149 // receiver. There is no need to use the global proxy here because
3150 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003151 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003152 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153
3154 // Push the arguments ("left-to-right") on the stack.
3155 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156 int arg_count = args->length();
3157 for (int i = 0; i < arg_count; i++) {
3158 LoadAndSpill(args->at(i));
3159 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160
mads.s.ager31e71382008-08-13 09:32:07 +00003161 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003162 Result num_args(r0);
3163 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003164
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003165 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003166 Result function(r1);
3167 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 // Call the construct call builtin that handles allocation and
3170 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003171 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003172 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003173 frame_->CallCodeObject(ic,
3174 RelocInfo::CONSTRUCT_CALL,
3175 &num_args,
3176 &function,
3177 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003178
3179 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003180 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003181 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182}
3183
3184
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003185void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3186 VirtualFrame::SpilledScope spilled_scope;
3187 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003188 JumpTarget leave, null, function, non_function_constructor;
3189
3190 // Load the object into r0.
3191 LoadAndSpill(args->at(0));
3192 frame_->EmitPop(r0);
3193
3194 // If the object is a smi, we return null.
3195 __ tst(r0, Operand(kSmiTagMask));
3196 null.Branch(eq);
3197
3198 // Check that the object is a JS object but take special care of JS
3199 // functions to make sure they have 'Function' as their class.
3200 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3201 null.Branch(lt);
3202
3203 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3204 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3205 // LAST_JS_OBJECT_TYPE.
3206 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3207 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3208 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3209 function.Branch(eq);
3210
3211 // Check if the constructor in the map is a function.
3212 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3213 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3214 non_function_constructor.Branch(ne);
3215
3216 // The r0 register now contains the constructor function. Grab the
3217 // instance class name from there.
3218 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3219 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003220 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003221 leave.Jump();
3222
3223 // Functions have class 'Function'.
3224 function.Bind();
3225 __ mov(r0, Operand(Factory::function_class_symbol()));
3226 frame_->EmitPush(r0);
3227 leave.Jump();
3228
3229 // Objects with a non-function constructor have class 'Object'.
3230 non_function_constructor.Bind();
3231 __ mov(r0, Operand(Factory::Object_symbol()));
3232 frame_->EmitPush(r0);
3233 leave.Jump();
3234
3235 // Non-JS objects have class null.
3236 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003237 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003238 frame_->EmitPush(r0);
3239
3240 // All done.
3241 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003242}
3243
3244
ager@chromium.org7c537e22008-10-16 08:43:32 +00003245void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003246 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003248 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 LoadAndSpill(args->at(0));
3250 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003251 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003253 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003254 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3255 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003256 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 // Load the value.
3258 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003259 leave.Bind();
3260 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261}
3262
3263
ager@chromium.org7c537e22008-10-16 08:43:32 +00003264void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003265 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003267 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003268 LoadAndSpill(args->at(0)); // Load the object.
3269 LoadAndSpill(args->at(1)); // Load the value.
3270 frame_->EmitPop(r0); // r0 contains value
3271 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272 // if (object->IsSmi()) return object.
3273 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003274 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003275 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3276 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003277 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003278 // Store the value.
3279 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3280 // Update the write barrier.
3281 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3282 __ RecordWrite(r1, r2, r3);
3283 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284 leave.Bind();
3285 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286}
3287
3288
ager@chromium.org7c537e22008-10-16 08:43:32 +00003289void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003290 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 LoadAndSpill(args->at(0));
3293 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003294 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003295 cc_reg_ = eq;
3296}
3297
3298
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003299void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003301 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3302 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003303#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003304 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003305 LoadAndSpill(args->at(1));
3306 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003307 __ CallRuntime(Runtime::kLog, 2);
3308 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003309#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003310 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003311 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003312}
3313
3314
ager@chromium.org7c537e22008-10-16 08:43:32 +00003315void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003316 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003317 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003318 LoadAndSpill(args->at(0));
3319 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003320 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003321 cc_reg_ = eq;
3322}
3323
3324
kasper.lund7276f142008-07-30 08:49:36 +00003325// This should generate code that performs a charCodeAt() call or returns
3326// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3327// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003328void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003329 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003330 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003331 Comment(masm_, "[ GenerateFastCharCodeAt");
3332
3333 LoadAndSpill(args->at(0));
3334 LoadAndSpill(args->at(1));
3335 frame_->EmitPop(r0); // Index.
3336 frame_->EmitPop(r1); // String.
3337
3338 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3339
3340 __ tst(r1, Operand(kSmiTagMask));
3341 __ b(eq, &slow); // The 'string' was a Smi.
3342
3343 ASSERT(kSmiTag == 0);
3344 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3345 __ b(ne, &slow); // The index was negative or not a Smi.
3346
3347 __ bind(&try_again_with_new_string);
3348 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3349 __ b(ge, &slow);
3350
3351 // Now r2 has the string type.
3352 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003353 // Now r3 has the length of the string. Compare with the index.
3354 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3355 __ b(le, &slow);
3356
3357 // Here we know the index is in range. Check that string is sequential.
3358 ASSERT_EQ(0, kSeqStringTag);
3359 __ tst(r2, Operand(kStringRepresentationMask));
3360 __ b(ne, &not_a_flat_string);
3361
3362 // Check whether it is an ASCII string.
3363 ASSERT_EQ(0, kTwoByteStringTag);
3364 __ tst(r2, Operand(kStringEncodingMask));
3365 __ b(ne, &ascii_string);
3366
3367 // 2-byte string. We can add without shifting since the Smi tag size is the
3368 // log2 of the number of bytes in a two-byte character.
3369 ASSERT_EQ(1, kSmiTagSize);
3370 ASSERT_EQ(0, kSmiShiftSize);
3371 __ add(r1, r1, Operand(r0));
3372 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3373 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3374 __ jmp(&end);
3375
3376 __ bind(&ascii_string);
3377 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3378 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3379 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3380 __ jmp(&end);
3381
3382 __ bind(&not_a_flat_string);
3383 __ and_(r2, r2, Operand(kStringRepresentationMask));
3384 __ cmp(r2, Operand(kConsStringTag));
3385 __ b(ne, &slow);
3386
3387 // ConsString.
3388 // Check that the right hand side is the empty string (ie if this is really a
3389 // flat string in a cons string). If that is not the case we would rather go
3390 // to the runtime system now, to flatten the string.
3391 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3392 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3393 __ cmp(r2, Operand(r3));
3394 __ b(ne, &slow);
3395
3396 // Get the first of the two strings.
3397 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3398 __ jmp(&try_again_with_new_string);
3399
3400 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003401 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003402
3403 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003404 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003405}
3406
3407
ager@chromium.org7c537e22008-10-16 08:43:32 +00003408void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003409 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003410 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003411 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003412 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003413 // We need the CC bits to come out as not_equal in the case where the
3414 // object is a smi. This can't be done with the usual test opcode so
3415 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003416 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003417 __ and_(r1, r0, Operand(kSmiTagMask));
3418 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003419 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003420 // It is a heap object - get the map. Check if the object is a JS array.
3421 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003422 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003423 cc_reg_ = eq;
3424}
3425
3426
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003427void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3428 // This generates a fast version of:
3429 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3430 VirtualFrame::SpilledScope spilled_scope;
3431 ASSERT(args->length() == 1);
3432 LoadAndSpill(args->at(0));
3433 frame_->EmitPop(r1);
3434 __ tst(r1, Operand(kSmiTagMask));
3435 false_target()->Branch(eq);
3436
3437 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3438 __ cmp(r1, ip);
3439 true_target()->Branch(eq);
3440
3441 Register map_reg = r2;
3442 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3443 // Undetectable objects behave like undefined when tested with typeof.
3444 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3445 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3446 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3447 false_target()->Branch(eq);
3448
3449 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3450 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3451 false_target()->Branch(lt);
3452 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3453 cc_reg_ = le;
3454}
3455
3456
3457void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3458 // This generates a fast version of:
3459 // (%_ClassOf(arg) === 'Function')
3460 VirtualFrame::SpilledScope spilled_scope;
3461 ASSERT(args->length() == 1);
3462 LoadAndSpill(args->at(0));
3463 frame_->EmitPop(r0);
3464 __ tst(r0, Operand(kSmiTagMask));
3465 false_target()->Branch(eq);
3466 Register map_reg = r2;
3467 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3468 cc_reg_ = eq;
3469}
3470
3471
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003472void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3473 VirtualFrame::SpilledScope spilled_scope;
3474 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003475
3476 // Get the frame pointer for the calling frame.
3477 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3478
3479 // Skip the arguments adaptor frame if it exists.
3480 Label check_frame_marker;
3481 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003482 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003483 __ b(ne, &check_frame_marker);
3484 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3485
3486 // Check the marker in the calling frame.
3487 __ bind(&check_frame_marker);
3488 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3489 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3490 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003491}
3492
3493
ager@chromium.org7c537e22008-10-16 08:43:32 +00003494void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003495 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496 ASSERT(args->length() == 0);
3497
mads.s.ager31e71382008-08-13 09:32:07 +00003498 // Seed the result with the formal parameters count, which will be used
3499 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3501
3502 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003503 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003504 frame_->CallStub(&stub, 0);
3505 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506}
3507
3508
ager@chromium.org7c537e22008-10-16 08:43:32 +00003509void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003510 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511 ASSERT(args->length() == 1);
3512
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003513 // Satisfy contract with ArgumentsAccessStub:
3514 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003515 LoadAndSpill(args->at(0));
3516 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003517 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518
3519 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003520 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003521 frame_->CallStub(&stub, 0);
3522 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003523}
3524
3525
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003526void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3527 VirtualFrame::SpilledScope spilled_scope;
3528 ASSERT(args->length() == 0);
3529 __ Call(ExternalReference::random_positive_smi_function().address(),
3530 RelocInfo::RUNTIME_ENTRY);
3531 frame_->EmitPush(r0);
3532}
3533
3534
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003535void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3536 ASSERT_EQ(2, args->length());
3537
3538 Load(args->at(0));
3539 Load(args->at(1));
3540
3541 frame_->CallRuntime(Runtime::kStringAdd, 2);
3542 frame_->EmitPush(r0);
3543}
3544
3545
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003546void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3547 ASSERT_EQ(3, args->length());
3548
3549 Load(args->at(0));
3550 Load(args->at(1));
3551 Load(args->at(2));
3552
3553 frame_->CallRuntime(Runtime::kSubString, 3);
3554 frame_->EmitPush(r0);
3555}
3556
3557
3558void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3559 ASSERT_EQ(2, args->length());
3560
3561 Load(args->at(0));
3562 Load(args->at(1));
3563
3564 frame_->CallRuntime(Runtime::kStringCompare, 2);
3565 frame_->EmitPush(r0);
3566}
3567
3568
3569void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3570 ASSERT_EQ(4, args->length());
3571
3572 Load(args->at(0));
3573 Load(args->at(1));
3574 Load(args->at(2));
3575 Load(args->at(3));
3576
3577 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3578 frame_->EmitPush(r0);
3579}
3580
3581
ager@chromium.org7c537e22008-10-16 08:43:32 +00003582void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003583 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003584 ASSERT(args->length() == 2);
3585
3586 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003587 LoadAndSpill(args->at(0));
3588 LoadAndSpill(args->at(1));
3589 frame_->EmitPop(r0);
3590 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003591 __ cmp(r0, Operand(r1));
3592 cc_reg_ = eq;
3593}
3594
3595
ager@chromium.org7c537e22008-10-16 08:43:32 +00003596void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003597#ifdef DEBUG
3598 int original_height = frame_->height();
3599#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003600 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003601 if (CheckForInlineRuntimeCall(node)) {
3602 ASSERT((has_cc() && frame_->height() == original_height) ||
3603 (!has_cc() && frame_->height() == original_height + 1));
3604 return;
3605 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003606
3607 ZoneList<Expression*>* args = node->arguments();
3608 Comment cmnt(masm_, "[ CallRuntime");
3609 Runtime::Function* function = node->function();
3610
ager@chromium.org41826e72009-03-30 13:30:57 +00003611 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003612 // Prepare stack for calling JS runtime function.
3613 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003614 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003615 // Push the builtins object found in the current global object.
3616 __ ldr(r1, GlobalObject());
3617 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003618 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003619 }
mads.s.ager31e71382008-08-13 09:32:07 +00003620
ager@chromium.org41826e72009-03-30 13:30:57 +00003621 // Push the arguments ("left-to-right").
3622 int arg_count = args->length();
3623 for (int i = 0; i < arg_count; i++) {
3624 LoadAndSpill(args->at(i));
3625 }
mads.s.ager31e71382008-08-13 09:32:07 +00003626
ager@chromium.org41826e72009-03-30 13:30:57 +00003627 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003629 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3630 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003631 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003632 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003633 frame_->Drop();
3634 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003635 } else {
3636 // Call the C runtime function.
3637 frame_->CallRuntime(function, arg_count);
3638 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003640 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003641}
3642
3643
ager@chromium.org7c537e22008-10-16 08:43:32 +00003644void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003645#ifdef DEBUG
3646 int original_height = frame_->height();
3647#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003648 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649 Comment cmnt(masm_, "[ UnaryOperation");
3650
3651 Token::Value op = node->op();
3652
3653 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003654 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003655 false_target(),
3656 true_target(),
3657 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003658 // LoadCondition may (and usually does) leave a test and branch to
3659 // be emitted by the caller. In that case, negate the condition.
3660 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661
3662 } else if (op == Token::DELETE) {
3663 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003664 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003666 LoadAndSpill(property->obj());
3667 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003668 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669
mads.s.ager31e71382008-08-13 09:32:07 +00003670 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 Slot* slot = variable->slot();
3672 if (variable->is_global()) {
3673 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003674 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003675 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003676 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003677
3678 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3679 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003680 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003681 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003682 frame_->EmitPush(r0);
3683 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003685 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003686 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003687 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003688 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689
mads.s.ager31e71382008-08-13 09:32:07 +00003690 } else {
3691 // Default: Result of deleting non-global, not dynamically
3692 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003693 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695
3696 } else {
3697 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 LoadAndSpill(node->expression()); // may have side-effects
3699 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003700 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003702 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703
3704 } else if (op == Token::TYPEOF) {
3705 // Special case for loading the typeof expression; see comment on
3706 // LoadTypeofExpression().
3707 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003708 frame_->CallRuntime(Runtime::kTypeof, 1);
3709 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710
3711 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003712 LoadAndSpill(node->expression());
3713 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 switch (op) {
3715 case Token::NOT:
3716 case Token::DELETE:
3717 case Token::TYPEOF:
3718 UNREACHABLE(); // handled above
3719 break;
3720
3721 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003722 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003723 (node->expression()->AsBinaryOperation() != NULL &&
3724 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003725 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 break;
3728 }
3729
3730 case Token::BIT_NOT: {
3731 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003732 JumpTarget smi_label;
3733 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003734 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003735 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003737 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003738 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003740 continue_label.Jump();
3741 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003742 __ mvn(r0, Operand(r0));
3743 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003744 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003745 break;
3746 }
3747
3748 case Token::VOID:
3749 // since the stack top is cached in r0, popping and then
3750 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003751 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003752 break;
3753
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003754 case Token::ADD: {
3755 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003756 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003757 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003758 continue_label.Branch(eq);
3759 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003760 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003761 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 default:
3765 UNREACHABLE();
3766 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003767 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003768 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003769 ASSERT(!has_valid_frame() ||
3770 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003771 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772}
3773
3774
ager@chromium.org7c537e22008-10-16 08:43:32 +00003775void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003776#ifdef DEBUG
3777 int original_height = frame_->height();
3778#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003779 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780 Comment cmnt(masm_, "[ CountOperation");
3781
3782 bool is_postfix = node->is_postfix();
3783 bool is_increment = node->op() == Token::INC;
3784
3785 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3786 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3787
3788 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003789 if (is_postfix) {
3790 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003792 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793
3794 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003795 if (target.is_illegal()) {
3796 // Spoof the virtual frame to have the expected height (one higher
3797 // than on entry).
3798 if (!is_postfix) {
3799 __ mov(r0, Operand(Smi::FromInt(0)));
3800 frame_->EmitPush(r0);
3801 }
3802 ASSERT(frame_->height() == original_height + 1);
3803 return;
3804 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003805 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003808 JumpTarget slow;
3809 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003810
3811 // Load the value (1) into register r1.
3812 __ mov(r1, Operand(Smi::FromInt(1)));
3813
3814 // Check for smi operand.
3815 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003816 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817
3818 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003819 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003821 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003822
3823 // Perform optimistic increment/decrement.
3824 if (is_increment) {
3825 __ add(r0, r0, Operand(r1), SetCC);
3826 } else {
3827 __ sub(r0, r0, Operand(r1), SetCC);
3828 }
3829
3830 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832
3833 // Revert optimistic increment/decrement.
3834 if (is_increment) {
3835 __ sub(r0, r0, Operand(r1));
3836 } else {
3837 __ add(r0, r0, Operand(r1));
3838 }
3839
3840 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003841 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003842 {
3843 // Convert the operand to a number.
3844 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003845 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003846 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003848 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 }
3851
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003852 // Compute the new value.
3853 __ mov(r1, Operand(Smi::FromInt(1)));
3854 frame_->EmitPush(r0);
3855 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003857 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003859 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860 }
3861
3862 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003863 exit.Bind();
3864 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003865 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003866 }
3867
3868 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003869 if (is_postfix) frame_->EmitPop(r0);
3870 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871}
3872
3873
ager@chromium.org7c537e22008-10-16 08:43:32 +00003874void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003875#ifdef DEBUG
3876 int original_height = frame_->height();
3877#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003878 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879 Comment cmnt(masm_, "[ BinaryOperation");
3880 Token::Value op = node->op();
3881
3882 // According to ECMA-262 section 11.11, page 58, the binary logical
3883 // operators must yield the result of one of the two expressions
3884 // before any ToBoolean() conversions. This means that the value
3885 // produced by a && or || operator is not necessarily a boolean.
3886
3887 // NOTE: If the left hand side produces a materialized value (not in
3888 // the CC register), we force the right hand side to do the
3889 // same. This is necessary because we may have to branch to the exit
3890 // after evaluating the left hand side (due to the shortcut
3891 // semantics), but the compiler must (statically) know if the result
3892 // of compiling the binary operation is materialized or not.
3893
3894 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003895 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003896 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003897 &is_true,
3898 false_target(),
3899 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003900 if (has_valid_frame() && !has_cc()) {
3901 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003902 JumpTarget pop_and_continue;
3903 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003905 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003906 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 // Avoid popping the result if it converts to 'false' using the
3908 // standard ToBoolean() conversion as described in ECMA-262,
3909 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003910 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 Branch(false, &exit);
3912
3913 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003914 pop_and_continue.Bind();
3915 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916
3917 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 is_true.Bind();
3919 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920
3921 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003923 } else if (has_cc() || is_true.is_linked()) {
3924 // The left-hand side is either (a) partially compiled to
3925 // control flow with a final branch left to emit or (b) fully
3926 // compiled to control flow and possibly true.
3927 if (has_cc()) {
3928 Branch(false, false_target());
3929 }
3930 is_true.Bind();
3931 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003932 true_target(),
3933 false_target(),
3934 false);
3935 } else {
3936 // Nothing to do.
3937 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938 }
3939
3940 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003941 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003942 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003943 true_target(),
3944 &is_false,
3945 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003946 if (has_valid_frame() && !has_cc()) {
3947 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003948 JumpTarget pop_and_continue;
3949 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003951 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003952 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 // Avoid popping the result if it converts to 'true' using the
3954 // standard ToBoolean() conversion as described in ECMA-262,
3955 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003956 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 Branch(true, &exit);
3958
3959 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003960 pop_and_continue.Bind();
3961 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962
3963 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 is_false.Bind();
3965 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966
3967 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003969 } else if (has_cc() || is_false.is_linked()) {
3970 // The left-hand side is either (a) partially compiled to
3971 // control flow with a final branch left to emit or (b) fully
3972 // compiled to control flow and possibly false.
3973 if (has_cc()) {
3974 Branch(true, true_target());
3975 }
3976 is_false.Bind();
3977 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003978 true_target(),
3979 false_target(),
3980 false);
3981 } else {
3982 // Nothing to do.
3983 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 }
3985
3986 } else {
3987 // Optimize for the case where (at least) one of the expressions
3988 // is a literal small integer.
3989 Literal* lliteral = node->left()->AsLiteral();
3990 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003991 // NOTE: The code below assumes that the slow cases (calls to runtime)
3992 // never return a constant/immutable object.
3993 bool overwrite_left =
3994 (node->left()->AsBinaryOperation() != NULL &&
3995 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3996 bool overwrite_right =
3997 (node->right()->AsBinaryOperation() != NULL &&
3998 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999
4000 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004001 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004002 SmiOperation(node->op(),
4003 rliteral->handle(),
4004 false,
4005 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006
4007 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004008 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004009 SmiOperation(node->op(),
4010 lliteral->handle(),
4011 true,
4012 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004013
4014 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004015 OverwriteMode overwrite_mode = NO_OVERWRITE;
4016 if (overwrite_left) {
4017 overwrite_mode = OVERWRITE_LEFT;
4018 } else if (overwrite_right) {
4019 overwrite_mode = OVERWRITE_RIGHT;
4020 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 LoadAndSpill(node->left());
4022 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004023 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004025 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004027 ASSERT(!has_valid_frame() ||
4028 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004029 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030}
4031
4032
ager@chromium.org7c537e22008-10-16 08:43:32 +00004033void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004034#ifdef DEBUG
4035 int original_height = frame_->height();
4036#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004037 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004038 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004039 frame_->EmitPush(r0);
4040 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041}
4042
4043
ager@chromium.org7c537e22008-10-16 08:43:32 +00004044void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004045#ifdef DEBUG
4046 int original_height = frame_->height();
4047#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004048 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004049 Comment cmnt(masm_, "[ CompareOperation");
4050
4051 // Get the expressions from the node.
4052 Expression* left = node->left();
4053 Expression* right = node->right();
4054 Token::Value op = node->op();
4055
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004056 // To make null checks efficient, we check if either left or right is the
4057 // literal 'null'. If so, we optimize the code by inlining a null check
4058 // instead of calling the (very) general runtime routine for checking
4059 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004060 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004061 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004062 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004063 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004064 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4065 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004067 LoadAndSpill(left_is_null ? right : left);
4068 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004069 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4070 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004072 // The 'null' value is only equal to 'undefined' if using non-strict
4073 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004075 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004076
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004077 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4078 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004079 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004081 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004084 // It can be an undetectable object.
4085 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4086 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4087 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4088 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004089 }
4090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004091 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004093 return;
4094 }
4095 }
4096
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004097 // To make typeof testing for natives implemented in JavaScript really
4098 // efficient, we generate special code for expressions of the form:
4099 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004100 UnaryOperation* operation = left->AsUnaryOperation();
4101 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4102 (operation != NULL && operation->op() == Token::TYPEOF) &&
4103 (right->AsLiteral() != NULL &&
4104 right->AsLiteral()->handle()->IsString())) {
4105 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4106
mads.s.ager31e71382008-08-13 09:32:07 +00004107 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004109 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110
4111 if (check->Equals(Heap::number_symbol())) {
4112 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004113 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004115 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4116 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117 cc_reg_ = eq;
4118
4119 } else if (check->Equals(Heap::string_symbol())) {
4120 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004121 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004122
4123 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4124
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004125 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4127 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4128 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004129 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004130
4131 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4132 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4133 cc_reg_ = lt;
4134
4135 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004136 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4137 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004139 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4140 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004141 cc_reg_ = eq;
4142
4143 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004144 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4145 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004146 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004147
4148 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004149 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004151 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004152 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4153 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4154 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4155 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4156
4157 cc_reg_ = eq;
4158
4159 } else if (check->Equals(Heap::function_symbol())) {
4160 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004161 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004162 Register map_reg = r2;
4163 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4164 true_target()->Branch(eq);
4165 // Regular expressions are callable so typeof == 'function'.
4166 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167 cc_reg_ = eq;
4168
4169 } else if (check->Equals(Heap::object_symbol())) {
4170 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004171 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004172
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004173 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4174 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004175 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004177 Register map_reg = r2;
4178 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4179 false_target()->Branch(eq);
4180
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004181 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004182 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004183 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4184 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004185 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004187 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4188 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004189 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004190 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 cc_reg_ = le;
4192
4193 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004194 // Uncommon case: typeof testing against a string literal that is
4195 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004196 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004198 ASSERT(!has_valid_frame() ||
4199 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004200 return;
4201 }
4202
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004203 switch (op) {
4204 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004205 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004206 break;
4207
4208 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004209 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004210 break;
4211
4212 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004213 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214 break;
4215
4216 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004217 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004218 break;
4219
4220 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004221 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222 break;
4223
4224 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004225 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004226 break;
4227
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004228 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004229 LoadAndSpill(left);
4230 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004231 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004232 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004233 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004235
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004236 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004237 LoadAndSpill(left);
4238 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004239 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004240 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004241 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004242 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004243 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004244 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004245 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004246
4247 default:
4248 UNREACHABLE();
4249 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004250 ASSERT((has_cc() && frame_->height() == original_height) ||
4251 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252}
4253
4254
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004255#ifdef DEBUG
4256bool CodeGenerator::HasValidEntryRegisters() { return true; }
4257#endif
4258
4259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004261#define __ ACCESS_MASM(masm)
4262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004263
ager@chromium.org7c537e22008-10-16 08:43:32 +00004264Handle<String> Reference::GetName() {
4265 ASSERT(type_ == NAMED);
4266 Property* property = expression_->AsProperty();
4267 if (property == NULL) {
4268 // Global variable reference treated as a named property reference.
4269 VariableProxy* proxy = expression_->AsVariableProxy();
4270 ASSERT(proxy->AsVariable() != NULL);
4271 ASSERT(proxy->AsVariable()->is_global());
4272 return proxy->name();
4273 } else {
4274 Literal* raw_name = property->key()->AsLiteral();
4275 ASSERT(raw_name != NULL);
4276 return Handle<String>(String::cast(*raw_name->handle()));
4277 }
4278}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004279
ager@chromium.org7c537e22008-10-16 08:43:32 +00004280
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004281void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004282 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004283 ASSERT(!is_illegal());
4284 ASSERT(!cgen_->has_cc());
4285 MacroAssembler* masm = cgen_->masm();
4286 Property* property = expression_->AsProperty();
4287 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004288 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004289 }
4290
4291 switch (type_) {
4292 case SLOT: {
4293 Comment cmnt(masm, "[ Load from Slot");
4294 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4295 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004296 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004297 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004298 }
4299
ager@chromium.org7c537e22008-10-16 08:43:32 +00004300 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004302 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004303 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004304 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004305 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4306 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004307 Result name_reg(r2);
4308 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004309 ASSERT(var == NULL || var->is_global());
4310 RelocInfo::Mode rmode = (var == NULL)
4311 ? RelocInfo::CODE_TARGET
4312 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004313 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4314 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004315 break;
4316 }
4317
4318 case KEYED: {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004319 // TODO(181): Implement inlined version of array indexing once
4320 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004321 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004322 Comment cmnt(masm, "[ Load from keyed Property");
4323 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004324 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004325 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004326 ASSERT(var == NULL || var->is_global());
4327 RelocInfo::Mode rmode = (var == NULL)
4328 ? RelocInfo::CODE_TARGET
4329 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004330 frame->CallCodeObject(ic, rmode, 0);
4331 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004332 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004333 }
4334
4335 default:
4336 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004337 }
4338}
4339
4340
ager@chromium.org7c537e22008-10-16 08:43:32 +00004341void Reference::SetValue(InitState init_state) {
4342 ASSERT(!is_illegal());
4343 ASSERT(!cgen_->has_cc());
4344 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004345 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004346 Property* property = expression_->AsProperty();
4347 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004348 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004349 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004350
ager@chromium.org7c537e22008-10-16 08:43:32 +00004351 switch (type_) {
4352 case SLOT: {
4353 Comment cmnt(masm, "[ Store to Slot");
4354 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004355 cgen_->StoreToSlot(slot, init_state);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004356 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004357 }
4358
ager@chromium.org7c537e22008-10-16 08:43:32 +00004359 case NAMED: {
4360 Comment cmnt(masm, "[ Store to named Property");
4361 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004362 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004363 Handle<String> name(GetName());
4364
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004365 Result value(r0);
4366 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004367
4368 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004369 Result property_name(r2);
4370 __ mov(r2, Operand(name));
4371 frame->CallCodeObject(ic,
4372 RelocInfo::CODE_TARGET,
4373 &value,
4374 &property_name,
4375 0);
4376 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004377 break;
4378 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004379
ager@chromium.org7c537e22008-10-16 08:43:32 +00004380 case KEYED: {
4381 Comment cmnt(masm, "[ Store to keyed Property");
4382 Property* property = expression_->AsProperty();
4383 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004384 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004385
4386 // Call IC code.
4387 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4388 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004389 Result value(r0);
4390 frame->EmitPop(r0); // value
4391 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4392 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004393 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004394 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004395
4396 default:
4397 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004398 }
4399}
4400
4401
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004402void FastNewClosureStub::Generate(MacroAssembler* masm) {
4403 // Clone the boilerplate in new space. Set the context to the
4404 // current context in cp.
4405 Label gc;
4406
4407 // Pop the boilerplate function from the stack.
4408 __ pop(r3);
4409
4410 // Attempt to allocate new JSFunction in new space.
4411 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4412 r0,
4413 r1,
4414 r2,
4415 &gc,
4416 TAG_OBJECT);
4417
4418 // Compute the function map in the current global context and set that
4419 // as the map of the allocated object.
4420 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4421 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4422 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4423 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4424
4425 // Clone the rest of the boilerplate fields. We don't have to update
4426 // the write barrier because the allocated object is in new space.
4427 for (int offset = kPointerSize;
4428 offset < JSFunction::kSize;
4429 offset += kPointerSize) {
4430 if (offset == JSFunction::kContextOffset) {
4431 __ str(cp, FieldMemOperand(r0, offset));
4432 } else {
4433 __ ldr(r1, FieldMemOperand(r3, offset));
4434 __ str(r1, FieldMemOperand(r0, offset));
4435 }
4436 }
4437
4438 // Return result. The argument boilerplate has been popped already.
4439 __ Ret();
4440
4441 // Create a new closure through the slower runtime call.
4442 __ bind(&gc);
4443 __ push(cp);
4444 __ push(r3);
4445 __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1);
4446}
4447
4448
4449void FastNewContextStub::Generate(MacroAssembler* masm) {
4450 // Try to allocate the context in new space.
4451 Label gc;
4452 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4453
4454 // Attempt to allocate the context in new space.
4455 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4456 r0,
4457 r1,
4458 r2,
4459 &gc,
4460 TAG_OBJECT);
4461
4462 // Load the function from the stack.
4463 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
4464
4465 // Setup the object header.
4466 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4467 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4468 __ mov(r2, Operand(length));
4469 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4470
4471 // Setup the fixed slots.
4472 __ mov(r1, Operand(Smi::FromInt(0)));
4473 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4474 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4475 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4476 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4477
4478 // Copy the global object from the surrounding context.
4479 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4480 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4481
4482 // Initialize the rest of the slots to undefined.
4483 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4484 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4485 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4486 }
4487
4488 // Remove the on-stack argument and return.
4489 __ mov(cp, r0);
4490 __ pop();
4491 __ Ret();
4492
4493 // Need to collect. Call into runtime system.
4494 __ bind(&gc);
4495 __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1);
4496}
4497
4498
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004499// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4500// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4501// (31 instead of 32).
4502static void CountLeadingZeros(
4503 MacroAssembler* masm,
4504 Register source,
4505 Register scratch,
4506 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004507#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004508 __ clz(zeros, source); // This instruction is only supported after ARM5.
4509#else
4510 __ mov(zeros, Operand(0));
4511 __ mov(scratch, source);
4512 // Top 16.
4513 __ tst(scratch, Operand(0xffff0000));
4514 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4515 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4516 // Top 8.
4517 __ tst(scratch, Operand(0xff000000));
4518 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4519 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4520 // Top 4.
4521 __ tst(scratch, Operand(0xf0000000));
4522 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4523 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4524 // Top 2.
4525 __ tst(scratch, Operand(0xc0000000));
4526 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4527 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4528 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004529 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004530 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4531#endif
4532}
4533
4534
4535// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4536// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4537// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4538// scratch register. Destroys the source register. No GC occurs during this
4539// stub so you don't have to set up the frame.
4540class ConvertToDoubleStub : public CodeStub {
4541 public:
4542 ConvertToDoubleStub(Register result_reg_1,
4543 Register result_reg_2,
4544 Register source_reg,
4545 Register scratch_reg)
4546 : result1_(result_reg_1),
4547 result2_(result_reg_2),
4548 source_(source_reg),
4549 zeros_(scratch_reg) { }
4550
4551 private:
4552 Register result1_;
4553 Register result2_;
4554 Register source_;
4555 Register zeros_;
4556
4557 // Minor key encoding in 16 bits.
4558 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4559 class OpBits: public BitField<Token::Value, 2, 14> {};
4560
4561 Major MajorKey() { return ConvertToDouble; }
4562 int MinorKey() {
4563 // Encode the parameters in a unique 16 bit value.
4564 return result1_.code() +
4565 (result2_.code() << 4) +
4566 (source_.code() << 8) +
4567 (zeros_.code() << 12);
4568 }
4569
4570 void Generate(MacroAssembler* masm);
4571
4572 const char* GetName() { return "ConvertToDoubleStub"; }
4573
4574#ifdef DEBUG
4575 void Print() { PrintF("ConvertToDoubleStub\n"); }
4576#endif
4577};
4578
4579
4580void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4581#ifndef BIG_ENDIAN_FLOATING_POINT
4582 Register exponent = result1_;
4583 Register mantissa = result2_;
4584#else
4585 Register exponent = result2_;
4586 Register mantissa = result1_;
4587#endif
4588 Label not_special;
4589 // Convert from Smi to integer.
4590 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4591 // Move sign bit from source to destination. This works because the sign bit
4592 // in the exponent word of the double has the same position and polarity as
4593 // the 2's complement sign bit in a Smi.
4594 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4595 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4596 // Subtract from 0 if source was negative.
4597 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4598 __ cmp(source_, Operand(1));
4599 __ b(gt, &not_special);
4600
4601 // We have -1, 0 or 1, which we treat specially.
4602 __ cmp(source_, Operand(0));
4603 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4604 static const uint32_t exponent_word_for_1 =
4605 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4606 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4607 // 1, 0 and -1 all have 0 for the second word.
4608 __ mov(mantissa, Operand(0));
4609 __ Ret();
4610
4611 __ bind(&not_special);
4612 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4613 // Gets the wrong answer for 0, but we already checked for that case above.
4614 CountLeadingZeros(masm, source_, mantissa, zeros_);
4615 // Compute exponent and or it into the exponent register.
4616 // We use result2 as a scratch register here.
4617 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4618 __ orr(exponent,
4619 exponent,
4620 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4621 // Shift up the source chopping the top bit off.
4622 __ add(zeros_, zeros_, Operand(1));
4623 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4624 __ mov(source_, Operand(source_, LSL, zeros_));
4625 // Compute lower part of fraction (last 12 bits).
4626 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4627 // And the top (top 20 bits).
4628 __ orr(exponent,
4629 exponent,
4630 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4631 __ Ret();
4632}
4633
4634
4635// This stub can convert a signed int32 to a heap number (double). It does
4636// not work for int32s that are in Smi range! No GC occurs during this stub
4637// so you don't have to set up the frame.
4638class WriteInt32ToHeapNumberStub : public CodeStub {
4639 public:
4640 WriteInt32ToHeapNumberStub(Register the_int,
4641 Register the_heap_number,
4642 Register scratch)
4643 : the_int_(the_int),
4644 the_heap_number_(the_heap_number),
4645 scratch_(scratch) { }
4646
4647 private:
4648 Register the_int_;
4649 Register the_heap_number_;
4650 Register scratch_;
4651
4652 // Minor key encoding in 16 bits.
4653 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4654 class OpBits: public BitField<Token::Value, 2, 14> {};
4655
4656 Major MajorKey() { return WriteInt32ToHeapNumber; }
4657 int MinorKey() {
4658 // Encode the parameters in a unique 16 bit value.
4659 return the_int_.code() +
4660 (the_heap_number_.code() << 4) +
4661 (scratch_.code() << 8);
4662 }
4663
4664 void Generate(MacroAssembler* masm);
4665
4666 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4667
4668#ifdef DEBUG
4669 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4670#endif
4671};
4672
4673
4674// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004675void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004676 Label max_negative_int;
4677 // the_int_ has the answer which is a signed int32 but not a Smi.
4678 // We test for the special value that has a different exponent. This test
4679 // has the neat side effect of setting the flags according to the sign.
4680 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004681 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004682 __ b(eq, &max_negative_int);
4683 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4684 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4685 uint32_t non_smi_exponent =
4686 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4687 __ mov(scratch_, Operand(non_smi_exponent));
4688 // Set the sign bit in scratch_ if the value was negative.
4689 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4690 // Subtract from 0 if the value was negative.
4691 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4692 // We should be masking the implict first digit of the mantissa away here,
4693 // but it just ends up combining harmlessly with the last digit of the
4694 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4695 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4696 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4697 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4698 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4699 __ str(scratch_, FieldMemOperand(the_heap_number_,
4700 HeapNumber::kExponentOffset));
4701 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4702 __ str(scratch_, FieldMemOperand(the_heap_number_,
4703 HeapNumber::kMantissaOffset));
4704 __ Ret();
4705
4706 __ bind(&max_negative_int);
4707 // The max negative int32 is stored as a positive number in the mantissa of
4708 // a double because it uses a sign bit instead of using two's complement.
4709 // The actual mantissa bits stored are all 0 because the implicit most
4710 // significant 1 bit is not stored.
4711 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4712 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4713 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4714 __ mov(ip, Operand(0));
4715 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4716 __ Ret();
4717}
4718
4719
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004720// Handle the case where the lhs and rhs are the same object.
4721// Equality is almost reflexive (everything but NaN), so this is a test
4722// for "identity and not NaN".
4723static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4724 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004725 Condition cc,
4726 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004727 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004728 Label heap_number, return_equal;
4729 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004730 __ cmp(r0, Operand(r1));
4731 __ b(ne, &not_identical);
4732
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004733 // The two objects are identical. If we know that one of them isn't NaN then
4734 // we now know they test equal.
4735 if (cc != eq || !never_nan_nan) {
4736 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004737
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004738 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4739 // so we do the second best thing - test it ourselves.
4740 // They are both equal and they are not both Smis so both of them are not
4741 // Smis. If it's not a heap number, then return equal.
4742 if (cc == lt || cc == gt) {
4743 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004744 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004745 } else {
4746 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4747 __ b(eq, &heap_number);
4748 // Comparing JS objects with <=, >= is complicated.
4749 if (cc != eq) {
4750 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4751 __ b(ge, slow);
4752 // Normally here we fall through to return_equal, but undefined is
4753 // special: (undefined == undefined) == true, but
4754 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4755 if (cc == le || cc == ge) {
4756 __ cmp(r4, Operand(ODDBALL_TYPE));
4757 __ b(ne, &return_equal);
4758 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4759 __ cmp(r0, Operand(r2));
4760 __ b(ne, &return_equal);
4761 if (cc == le) {
4762 // undefined <= undefined should fail.
4763 __ mov(r0, Operand(GREATER));
4764 } else {
4765 // undefined >= undefined should fail.
4766 __ mov(r0, Operand(LESS));
4767 }
4768 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004769 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004770 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004771 }
4772 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004773
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004774 __ bind(&return_equal);
4775 if (cc == lt) {
4776 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4777 } else if (cc == gt) {
4778 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4779 } else {
4780 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4781 }
4782 __ mov(pc, Operand(lr)); // Return.
4783
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004784 if (cc != eq || !never_nan_nan) {
4785 // For less and greater we don't have to check for NaN since the result of
4786 // x < x is false regardless. For the others here is some code to check
4787 // for NaN.
4788 if (cc != lt && cc != gt) {
4789 __ bind(&heap_number);
4790 // It is a heap number, so return non-equal if it's NaN and equal if it's
4791 // not NaN.
4792 // The representation of NaN values has all exponent bits (52..62) set,
4793 // and not all mantissa bits (0..51) clear.
4794 // Read top bits of double representation (second word of value).
4795 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4796 // Test that exponent bits are all set.
4797 __ and_(r3, r2, Operand(exp_mask_reg));
4798 __ cmp(r3, Operand(exp_mask_reg));
4799 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004800
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004801 // Shift out flag and all exponent bits, retaining only mantissa.
4802 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4803 // Or with all low-bits of mantissa.
4804 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4805 __ orr(r0, r3, Operand(r2), SetCC);
4806 // For equal we already have the right value in r0: Return zero (equal)
4807 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4808 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4809 // if it's a NaN.
4810 if (cc != eq) {
4811 // All-zero means Infinity means equal.
4812 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4813 if (cc == le) {
4814 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4815 } else {
4816 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4817 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004818 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004819 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004820 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004821 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004822 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004823
4824 __ bind(&not_identical);
4825}
4826
4827
4828// See comment at call site.
4829static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4830 Label* rhs_not_nan,
4831 Label* slow,
4832 bool strict) {
4833 Label lhs_is_smi;
4834 __ tst(r0, Operand(kSmiTagMask));
4835 __ b(eq, &lhs_is_smi);
4836
4837 // Rhs is a Smi. Check whether the non-smi is a heap number.
4838 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4839 if (strict) {
4840 // If lhs was not a number and rhs was a Smi then strict equality cannot
4841 // succeed. Return non-equal (r0 is already not zero)
4842 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4843 } else {
4844 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4845 // the runtime.
4846 __ b(ne, slow);
4847 }
4848
4849 // Rhs is a smi, lhs is a number.
4850 __ push(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004851
4852 if (CpuFeatures::IsSupported(VFP3)) {
4853 CpuFeatures::Scope scope(VFP3);
4854 __ IntegerToDoubleConversionWithVFP3(r1, r3, r2);
4855 } else {
4856 __ mov(r7, Operand(r1));
4857 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4858 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4859 }
4860
4861
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004862 // r3 and r2 are rhs as double.
4863 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4864 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4865 // We now have both loaded as doubles but we can skip the lhs nan check
4866 // since it's a Smi.
4867 __ pop(lr);
4868 __ jmp(rhs_not_nan);
4869
4870 __ bind(&lhs_is_smi);
4871 // Lhs is a Smi. Check whether the non-smi is a heap number.
4872 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4873 if (strict) {
4874 // If lhs was not a number and rhs was a Smi then strict equality cannot
4875 // succeed. Return non-equal.
4876 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4877 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4878 } else {
4879 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4880 // the runtime.
4881 __ b(ne, slow);
4882 }
4883
4884 // Lhs is a smi, rhs is a number.
4885 // r0 is Smi and r1 is heap number.
4886 __ push(lr);
4887 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4888 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004889
4890 if (CpuFeatures::IsSupported(VFP3)) {
4891 CpuFeatures::Scope scope(VFP3);
4892 __ IntegerToDoubleConversionWithVFP3(r0, r1, r0);
4893 } else {
4894 __ mov(r7, Operand(r0));
4895 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4896 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4897 }
4898
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004899 __ pop(lr);
4900 // Fall through to both_loaded_as_doubles.
4901}
4902
4903
4904void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4905 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4906 Register lhs_exponent = exp_first ? r0 : r1;
4907 Register rhs_exponent = exp_first ? r2 : r3;
4908 Register lhs_mantissa = exp_first ? r1 : r0;
4909 Register rhs_mantissa = exp_first ? r3 : r2;
4910 Label one_is_nan, neither_is_nan;
4911
4912 Register exp_mask_reg = r5;
4913
4914 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4915 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4916 __ cmp(r4, Operand(exp_mask_reg));
4917 __ b(ne, rhs_not_nan);
4918 __ mov(r4,
4919 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4920 SetCC);
4921 __ b(ne, &one_is_nan);
4922 __ cmp(rhs_mantissa, Operand(0));
4923 __ b(ne, &one_is_nan);
4924
4925 __ bind(rhs_not_nan);
4926 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4927 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4928 __ cmp(r4, Operand(exp_mask_reg));
4929 __ b(ne, &neither_is_nan);
4930 __ mov(r4,
4931 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4932 SetCC);
4933 __ b(ne, &one_is_nan);
4934 __ cmp(lhs_mantissa, Operand(0));
4935 __ b(eq, &neither_is_nan);
4936
4937 __ bind(&one_is_nan);
4938 // NaN comparisons always fail.
4939 // Load whatever we need in r0 to make the comparison fail.
4940 if (cc == lt || cc == le) {
4941 __ mov(r0, Operand(GREATER));
4942 } else {
4943 __ mov(r0, Operand(LESS));
4944 }
4945 __ mov(pc, Operand(lr)); // Return.
4946
4947 __ bind(&neither_is_nan);
4948}
4949
4950
4951// See comment at call site.
4952static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4953 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4954 Register lhs_exponent = exp_first ? r0 : r1;
4955 Register rhs_exponent = exp_first ? r2 : r3;
4956 Register lhs_mantissa = exp_first ? r1 : r0;
4957 Register rhs_mantissa = exp_first ? r3 : r2;
4958
4959 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4960 if (cc == eq) {
4961 // Doubles are not equal unless they have the same bit pattern.
4962 // Exception: 0 and -0.
4963 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4964 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4965 // Return non-zero if the numbers are unequal.
4966 __ mov(pc, Operand(lr), LeaveCC, ne);
4967
4968 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4969 // If exponents are equal then return 0.
4970 __ mov(pc, Operand(lr), LeaveCC, eq);
4971
4972 // Exponents are unequal. The only way we can return that the numbers
4973 // are equal is if one is -0 and the other is 0. We already dealt
4974 // with the case where both are -0 or both are 0.
4975 // We start by seeing if the mantissas (that are equal) or the bottom
4976 // 31 bits of the rhs exponent are non-zero. If so we return not
4977 // equal.
4978 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4979 __ mov(r0, Operand(r4), LeaveCC, ne);
4980 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4981 // Now they are equal if and only if the lhs exponent is zero in its
4982 // low 31 bits.
4983 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4984 __ mov(pc, Operand(lr));
4985 } else {
4986 // Call a native function to do a comparison between two non-NaNs.
4987 // Call C routine that may not cause GC or other trouble.
4988 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4989 __ Jump(r5); // Tail call.
4990 }
4991}
4992
4993
4994// See comment at call site.
4995static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4996 // If either operand is a JSObject or an oddball value, then they are
4997 // not equal since their pointers are different.
4998 // There is no test for undetectability in strict equality.
4999 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5000 Label first_non_object;
5001 // Get the type of the first operand into r2 and compare it with
5002 // FIRST_JS_OBJECT_TYPE.
5003 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5004 __ b(lt, &first_non_object);
5005
5006 // Return non-zero (r0 is not zero)
5007 Label return_not_equal;
5008 __ bind(&return_not_equal);
5009 __ mov(pc, Operand(lr)); // Return.
5010
5011 __ bind(&first_non_object);
5012 // Check for oddballs: true, false, null, undefined.
5013 __ cmp(r2, Operand(ODDBALL_TYPE));
5014 __ b(eq, &return_not_equal);
5015
5016 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5017 __ b(ge, &return_not_equal);
5018
5019 // Check for oddballs: true, false, null, undefined.
5020 __ cmp(r3, Operand(ODDBALL_TYPE));
5021 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005022
5023 // Now that we have the types we might as well check for symbol-symbol.
5024 // Ensure that no non-strings have the symbol bit set.
5025 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5026 ASSERT(kSymbolTag != 0);
5027 __ and_(r2, r2, Operand(r3));
5028 __ tst(r2, Operand(kIsSymbolMask));
5029 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005030}
5031
5032
5033// See comment at call site.
5034static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5035 Label* both_loaded_as_doubles,
5036 Label* not_heap_numbers,
5037 Label* slow) {
5038 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
5039 __ b(ne, not_heap_numbers);
5040 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
5041 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5042
5043 // Both are heap numbers. Load them up then jump to the code we have
5044 // for that.
5045 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5046 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5047 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5048 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5049 __ jmp(both_loaded_as_doubles);
5050}
5051
5052
5053// Fast negative check for symbol-to-symbol equality.
5054static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5055 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005056 // Ensure that no non-strings have the symbol bit set.
5057 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5058 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005059 __ tst(r2, Operand(kIsSymbolMask));
5060 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005061 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5062 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005063 __ tst(r3, Operand(kIsSymbolMask));
5064 __ b(eq, slow);
5065
5066 // Both are symbols. We already checked they weren't the same pointer
5067 // so they are not equal.
5068 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5069 __ mov(pc, Operand(lr)); // Return.
5070}
5071
5072
5073// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
5074// positive or negative to indicate the result of the comparison.
5075void CompareStub::Generate(MacroAssembler* masm) {
5076 Label slow; // Call builtin.
5077 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
5078
5079 // NOTICE! This code is only reached after a smi-fast-case check, so
5080 // it is certain that at least one operand isn't a smi.
5081
5082 // Handle the case where the objects are identical. Either returns the answer
5083 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005084 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005085
5086 // If either is a Smi (we know that not both are), then they can only
5087 // be strictly equal if the other is a HeapNumber.
5088 ASSERT_EQ(0, kSmiTag);
5089 ASSERT_EQ(0, Smi::FromInt(0));
5090 __ and_(r2, r0, Operand(r1));
5091 __ tst(r2, Operand(kSmiTagMask));
5092 __ b(ne, &not_smis);
5093 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5094 // 1) Return the answer.
5095 // 2) Go to slow.
5096 // 3) Fall through to both_loaded_as_doubles.
5097 // 4) Jump to rhs_not_nan.
5098 // In cases 3 and 4 we have found out we were dealing with a number-number
5099 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
5100 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
5101
5102 __ bind(&both_loaded_as_doubles);
5103 // r0, r1, r2, r3 are the double representations of the left hand side
5104 // and the right hand side.
5105
5106 // Checks for NaN in the doubles we have loaded. Can return the answer or
5107 // fall through if neither is a NaN. Also binds rhs_not_nan.
5108 EmitNanCheck(masm, &rhs_not_nan, cc_);
5109
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005110 if (CpuFeatures::IsSupported(VFP3)) {
5111 CpuFeatures::Scope scope(VFP3);
5112 // ARMv7 VFP3 instructions to implement double precision comparison.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005113 __ vmov(d6, r0, r1);
5114 __ vmov(d7, r2, r3);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005115
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005116 __ vcmp(d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005117 __ vmrs(pc);
5118 __ mov(r0, Operand(0), LeaveCC, eq);
5119 __ mov(r0, Operand(1), LeaveCC, lt);
5120 __ mvn(r0, Operand(0), LeaveCC, gt);
5121 __ mov(pc, Operand(lr));
5122 } else {
5123 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5124 // answer. Never falls through.
5125 EmitTwoNonNanDoubleComparison(masm, cc_);
5126 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005127
5128 __ bind(&not_smis);
5129 // At this point we know we are dealing with two different objects,
5130 // and neither of them is a Smi. The objects are in r0 and r1.
5131 if (strict_) {
5132 // This returns non-equal for some object types, or falls through if it
5133 // was not lucky.
5134 EmitStrictTwoHeapObjectCompare(masm);
5135 }
5136
5137 Label check_for_symbols;
5138 // Check for heap-number-heap-number comparison. Can jump to slow case,
5139 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5140 // that case. If the inputs are not doubles then jumps to check_for_symbols.
5141 // In this case r2 will contain the type of r0.
5142 EmitCheckForTwoHeapNumbers(masm,
5143 &both_loaded_as_doubles,
5144 &check_for_symbols,
5145 &slow);
5146
5147 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005148 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5149 // symbols.
5150 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005151 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5152 // of r0 on entry.
5153 EmitCheckForSymbols(masm, &slow);
5154 }
5155
5156 __ bind(&slow);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005157 __ push(r1);
5158 __ push(r0);
5159 // Figure out which native to call and setup the arguments.
5160 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005161 if (cc_ == eq) {
5162 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5163 } else {
5164 native = Builtins::COMPARE;
5165 int ncr; // NaN compare result
5166 if (cc_ == lt || cc_ == le) {
5167 ncr = GREATER;
5168 } else {
5169 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5170 ncr = LESS;
5171 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005172 __ mov(r0, Operand(Smi::FromInt(ncr)));
5173 __ push(r0);
5174 }
5175
5176 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5177 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005178 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005179}
5180
5181
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005182// Allocates a heap number or jumps to the label if the young space is full and
5183// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005184static void AllocateHeapNumber(
5185 MacroAssembler* masm,
5186 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005187 Register result, // The tagged address of the new heap number.
5188 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005189 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005190 // Allocate an object in the heap for the heap number and tag it as a heap
5191 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005192 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
5193 result,
5194 scratch1,
5195 scratch2,
5196 need_gc,
5197 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005198
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005199 // Get heap number map and store it in the allocated object.
5200 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
5201 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005202}
5203
5204
5205// We fall into this code if the operands were Smis, but the result was
5206// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005207// the operands were not both Smi. The operands are in r0 and r1. In order
5208// to call the C-implemented binary fp operation routines we need to end up
5209// with the double precision floating point operands in r0 and r1 (for the
5210// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005211static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5212 Label* not_smi,
5213 const Builtins::JavaScript& builtin,
5214 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005215 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005216 Label slow, slow_pop_2_first, do_the_call;
5217 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5218 // Smi-smi case (overflow).
5219 // Since both are Smis there is no heap number to overwrite, so allocate.
5220 // The new heap number is in r5. r6 and r7 are scratch.
5221 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005222
5223 if (CpuFeatures::IsSupported(VFP3)) {
5224 CpuFeatures::Scope scope(VFP3);
5225 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
5226 __ IntegerToDoubleConversionWithVFP3(r1, r1, r0);
5227 } else {
5228 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5229 __ mov(r7, Operand(r0));
5230 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5231 __ push(lr);
5232 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5233 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5234 __ mov(r7, Operand(r1));
5235 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5236 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5237 __ pop(lr);
5238 }
5239
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005240 __ jmp(&do_the_call); // Tail call. No return.
5241
5242 // We jump to here if something goes wrong (one param is not a number of any
5243 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005244 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005245
5246 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005247 __ push(r1);
5248 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005249
5250 if (Token::ADD == operation) {
5251 // Test for string arguments before calling runtime.
5252 // r1 : first argument
5253 // r0 : second argument
5254 // sp[0] : second argument
5255 // sp[1] : first argument
5256
5257 Label not_strings, not_string1, string1;
5258 __ tst(r1, Operand(kSmiTagMask));
5259 __ b(eq, &not_string1);
5260 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5261 __ b(ge, &not_string1);
5262
5263 // First argument is a a string, test second.
5264 __ tst(r0, Operand(kSmiTagMask));
5265 __ b(eq, &string1);
5266 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5267 __ b(ge, &string1);
5268
5269 // First and second argument are strings.
5270 __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1);
5271
5272 // Only first argument is a string.
5273 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005274 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5275
5276 // First argument was not a string, test second.
5277 __ bind(&not_string1);
5278 __ tst(r0, Operand(kSmiTagMask));
5279 __ b(eq, &not_strings);
5280 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5281 __ b(ge, &not_strings);
5282
5283 // Only second argument is a string.
5284 __ b(&not_strings);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005285 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5286
5287 __ bind(&not_strings);
5288 }
5289
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005290 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005291
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005292 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005293 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005294 if (mode == NO_OVERWRITE) {
5295 // In the case where there is no chance of an overwritable float we may as
5296 // well do the allocation immediately while r0 and r1 are untouched.
5297 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5298 }
5299
5300 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005301 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005302 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5303 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005304 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005305 if (mode == OVERWRITE_RIGHT) {
5306 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5307 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005308 // Calling convention says that second double is in r2 and r3.
5309 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005310 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5311 __ jmp(&finished_loading_r0);
5312 __ bind(&r0_is_smi);
5313 if (mode == OVERWRITE_RIGHT) {
5314 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005315 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005316 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005317
5318
5319 if (CpuFeatures::IsSupported(VFP3)) {
5320 CpuFeatures::Scope scope(VFP3);
5321 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
5322 } else {
5323 // Write Smi from r0 to r3 and r2 in double format.
5324 __ mov(r7, Operand(r0));
5325 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5326 __ push(lr);
5327 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5328 __ pop(lr);
5329 }
5330
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005331 __ bind(&finished_loading_r0);
5332
5333 // Move r1 to a double in r0-r1.
5334 __ tst(r1, Operand(kSmiTagMask));
5335 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5336 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5337 __ b(ne, &slow);
5338 if (mode == OVERWRITE_LEFT) {
5339 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005340 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005341 // Calling convention says that first double is in r0 and r1.
5342 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005343 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5344 __ jmp(&finished_loading_r1);
5345 __ bind(&r1_is_smi);
5346 if (mode == OVERWRITE_LEFT) {
5347 // We can't overwrite a Smi so get address of new heap number into r5.
5348 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5349 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005350
5351 if (CpuFeatures::IsSupported(VFP3)) {
5352 CpuFeatures::Scope scope(VFP3);
5353 __ IntegerToDoubleConversionWithVFP3(r1, r1, r0);
5354 } else {
5355 // Write Smi from r1 to r1 and r0 in double format.
5356 __ mov(r7, Operand(r1));
5357 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5358 __ push(lr);
5359 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5360 __ pop(lr);
5361 }
5362
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005363 __ bind(&finished_loading_r1);
5364
5365 __ bind(&do_the_call);
5366 // r0: Left value (least significant part of mantissa).
5367 // r1: Left value (sign, exponent, top of mantissa).
5368 // r2: Right value (least significant part of mantissa).
5369 // r3: Right value (sign, exponent, top of mantissa).
5370 // r5: Address of heap number for result.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005371
5372 if (CpuFeatures::IsSupported(VFP3) &&
5373 ((Token::MUL == operation) ||
5374 (Token::DIV == operation) ||
5375 (Token::ADD == operation) ||
5376 (Token::SUB == operation))) {
5377 CpuFeatures::Scope scope(VFP3);
5378 // ARMv7 VFP3 instructions to implement
5379 // double precision, add, subtract, multiply, divide.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005380 __ vmov(d6, r0, r1);
5381 __ vmov(d7, r2, r3);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005382
5383 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005384 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005385 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005386 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005387 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005388 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005389 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005390 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005391 } else {
5392 UNREACHABLE();
5393 }
5394
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005395 __ vmov(r0, r1, d5);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005396
5397 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
5398 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
5399 __ mov(r0, Operand(r5));
5400 __ mov(pc, lr);
5401 return;
5402 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005403 __ push(lr); // For later.
5404 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005405 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005406 // Call C routine that may not cause GC or other trouble.
5407 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005408 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005409 __ pop(r4); // Address of heap number.
5410 __ cmp(r4, Operand(Smi::FromInt(0)));
5411 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005412 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005413#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005414 // Double returned in fp coprocessor register 0 and 1, encoded as register
5415 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5416 // substract the tag from r4.
5417 __ sub(r5, r4, Operand(kHeapObjectTag));
5418 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5419#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005420 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005421 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005422 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005423#endif
5424 __ mov(r0, Operand(r4));
5425 // And we are done.
5426 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005427}
5428
5429
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005430// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005431// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005432// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5433// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005434// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5435// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005436static void GetInt32(MacroAssembler* masm,
5437 Register source,
5438 Register dest,
5439 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005440 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005441 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005442 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005443 // Get exponent word.
5444 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5445 // Get exponent alone in scratch2.
5446 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005447 // Load dest with zero. We use this either for the final shift or
5448 // for the answer.
5449 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005450 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005451 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5452 // the exponent that we are fastest at and also the highest exponent we can
5453 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005454 const uint32_t non_smi_exponent =
5455 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5456 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005457 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5458 __ b(eq, &right_exponent);
5459 // If the exponent is higher than that then go to slow case. This catches
5460 // numbers that don't fit in a signed int32, infinities and NaNs.
5461 __ b(gt, slow);
5462
5463 // We know the exponent is smaller than 30 (biased). If it is less than
5464 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5465 // it rounds to zero.
5466 const uint32_t zero_exponent =
5467 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5468 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5469 // Dest already has a Smi zero.
5470 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005471 if (!CpuFeatures::IsSupported(VFP3)) {
5472 // We have a shifted exponent between 0 and 30 in scratch2.
5473 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5474 // We now have the exponent in dest. Subtract from 30 to get
5475 // how much to shift down.
5476 __ rsb(dest, dest, Operand(30));
5477 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005478 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005479 if (CpuFeatures::IsSupported(VFP3)) {
5480 CpuFeatures::Scope scope(VFP3);
5481 // ARMv7 VFP3 instructions implementing double precision to integer
5482 // conversion using round to zero.
5483 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005484 __ vmov(d7, scratch2, scratch);
5485 __ vcvt(s15, d7);
5486 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005487 } else {
5488 // Get the top bits of the mantissa.
5489 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5490 // Put back the implicit 1.
5491 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5492 // Shift up the mantissa bits to take up the space the exponent used to
5493 // take. We just orred in the implicit bit so that took care of one and
5494 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5495 // distance.
5496 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5497 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5498 // Put sign in zero flag.
5499 __ tst(scratch, Operand(HeapNumber::kSignMask));
5500 // Get the second half of the double. For some exponents we don't
5501 // actually need this because the bits get shifted out again, but
5502 // it's probably slower to test than just to do it.
5503 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5504 // Shift down 22 bits to get the last 10 bits.
5505 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5506 // Move down according to the exponent.
5507 __ mov(dest, Operand(scratch, LSR, dest));
5508 // Fix sign if sign bit was set.
5509 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5510 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005511 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005512}
5513
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005514// For bitwise ops where the inputs are not both Smis we here try to determine
5515// whether both inputs are either Smis or at least heap numbers that can be
5516// represented by a 32 bit signed value. We truncate towards zero as required
5517// by the ES spec. If this is the case we do the bitwise op and see if the
5518// result is a Smi. If so, great, otherwise we try to find a heap number to
5519// write the answer into (either by allocating or by overwriting).
5520// On entry the operands are in r0 and r1. On exit the answer is in r0.
5521void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5522 Label slow, result_not_a_smi;
5523 Label r0_is_smi, r1_is_smi;
5524 Label done_checking_r0, done_checking_r1;
5525
5526 __ tst(r1, Operand(kSmiTagMask));
5527 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5528 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5529 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005530 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005531 __ jmp(&done_checking_r1);
5532 __ bind(&r1_is_smi);
5533 __ mov(r3, Operand(r1, ASR, 1));
5534 __ bind(&done_checking_r1);
5535
5536 __ tst(r0, Operand(kSmiTagMask));
5537 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5538 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5539 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005540 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005541 __ jmp(&done_checking_r0);
5542 __ bind(&r0_is_smi);
5543 __ mov(r2, Operand(r0, ASR, 1));
5544 __ bind(&done_checking_r0);
5545
5546 // r0 and r1: Original operands (Smi or heap numbers).
5547 // r2 and r3: Signed int32 operands.
5548 switch (op_) {
5549 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5550 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5551 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5552 case Token::SAR:
5553 // Use only the 5 least significant bits of the shift count.
5554 __ and_(r2, r2, Operand(0x1f));
5555 __ mov(r2, Operand(r3, ASR, r2));
5556 break;
5557 case Token::SHR:
5558 // Use only the 5 least significant bits of the shift count.
5559 __ and_(r2, r2, Operand(0x1f));
5560 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5561 // SHR is special because it is required to produce a positive answer.
5562 // The code below for writing into heap numbers isn't capable of writing
5563 // the register as an unsigned int so we go to slow case if we hit this
5564 // case.
5565 __ b(mi, &slow);
5566 break;
5567 case Token::SHL:
5568 // Use only the 5 least significant bits of the shift count.
5569 __ and_(r2, r2, Operand(0x1f));
5570 __ mov(r2, Operand(r3, LSL, r2));
5571 break;
5572 default: UNREACHABLE();
5573 }
5574 // check that the *signed* result fits in a smi
5575 __ add(r3, r2, Operand(0x40000000), SetCC);
5576 __ b(mi, &result_not_a_smi);
5577 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5578 __ Ret();
5579
5580 Label have_to_allocate, got_a_heap_number;
5581 __ bind(&result_not_a_smi);
5582 switch (mode_) {
5583 case OVERWRITE_RIGHT: {
5584 __ tst(r0, Operand(kSmiTagMask));
5585 __ b(eq, &have_to_allocate);
5586 __ mov(r5, Operand(r0));
5587 break;
5588 }
5589 case OVERWRITE_LEFT: {
5590 __ tst(r1, Operand(kSmiTagMask));
5591 __ b(eq, &have_to_allocate);
5592 __ mov(r5, Operand(r1));
5593 break;
5594 }
5595 case NO_OVERWRITE: {
5596 // Get a new heap number in r5. r6 and r7 are scratch.
5597 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5598 }
5599 default: break;
5600 }
5601 __ bind(&got_a_heap_number);
5602 // r2: Answer as signed int32.
5603 // r5: Heap number to write answer into.
5604
5605 // Nothing can go wrong now, so move the heap number to r0, which is the
5606 // result.
5607 __ mov(r0, Operand(r5));
5608
5609 // Tail call that writes the int32 in r2 to the heap number in r0, using
5610 // r3 as scratch. r0 is preserved and returned.
5611 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5612 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5613
5614 if (mode_ != NO_OVERWRITE) {
5615 __ bind(&have_to_allocate);
5616 // Get a new heap number in r5. r6 and r7 are scratch.
5617 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5618 __ jmp(&got_a_heap_number);
5619 }
5620
5621 // If all else failed then we go to the runtime system.
5622 __ bind(&slow);
5623 __ push(r1); // restore stack
5624 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005625 switch (op_) {
5626 case Token::BIT_OR:
5627 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5628 break;
5629 case Token::BIT_AND:
5630 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5631 break;
5632 case Token::BIT_XOR:
5633 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5634 break;
5635 case Token::SAR:
5636 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5637 break;
5638 case Token::SHR:
5639 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5640 break;
5641 case Token::SHL:
5642 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5643 break;
5644 default:
5645 UNREACHABLE();
5646 }
5647}
5648
5649
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005650// Can we multiply by x with max two shifts and an add.
5651// This answers yes to all integers from 2 to 10.
5652static bool IsEasyToMultiplyBy(int x) {
5653 if (x < 2) return false; // Avoid special cases.
5654 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5655 if (IsPowerOf2(x)) return true; // Simple shift.
5656 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5657 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5658 return false;
5659}
5660
5661
5662// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5663// Source and destination may be the same register. This routine does
5664// not set carry and overflow the way a mul instruction would.
5665static void MultiplyByKnownInt(MacroAssembler* masm,
5666 Register source,
5667 Register destination,
5668 int known_int) {
5669 if (IsPowerOf2(known_int)) {
5670 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5671 } else if (PopCountLessThanEqual2(known_int)) {
5672 int first_bit = BitPosition(known_int);
5673 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5674 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5675 if (first_bit != 0) {
5676 __ mov(destination, Operand(destination, LSL, first_bit));
5677 }
5678 } else {
5679 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5680 int the_bit = BitPosition(known_int + 1);
5681 __ rsb(destination, source, Operand(source, LSL, the_bit));
5682 }
5683}
5684
5685
5686// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5687// a register for the cases where it doesn't know a good trick, and may deliver
5688// a result that needs shifting.
5689static void MultiplyByKnownInt2(
5690 MacroAssembler* masm,
5691 Register result,
5692 Register source,
5693 Register known_int_register, // Smi tagged.
5694 int known_int,
5695 int* required_shift) { // Including Smi tag shift
5696 switch (known_int) {
5697 case 3:
5698 __ add(result, source, Operand(source, LSL, 1));
5699 *required_shift = 1;
5700 break;
5701 case 5:
5702 __ add(result, source, Operand(source, LSL, 2));
5703 *required_shift = 1;
5704 break;
5705 case 6:
5706 __ add(result, source, Operand(source, LSL, 1));
5707 *required_shift = 2;
5708 break;
5709 case 7:
5710 __ rsb(result, source, Operand(source, LSL, 3));
5711 *required_shift = 1;
5712 break;
5713 case 9:
5714 __ add(result, source, Operand(source, LSL, 3));
5715 *required_shift = 1;
5716 break;
5717 case 10:
5718 __ add(result, source, Operand(source, LSL, 2));
5719 *required_shift = 2;
5720 break;
5721 default:
5722 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5723 __ mul(result, source, known_int_register);
5724 *required_shift = 0;
5725 }
5726}
5727
5728
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005729const char* GenericBinaryOpStub::GetName() {
5730 if (name_ != NULL) return name_;
5731 const int len = 100;
5732 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5733 if (name_ == NULL) return "OOM";
5734 const char* op_name = Token::Name(op_);
5735 const char* overwrite_name;
5736 switch (mode_) {
5737 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5738 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5739 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5740 default: overwrite_name = "UnknownOverwrite"; break;
5741 }
5742
5743 OS::SNPrintF(Vector<char>(name_, len),
5744 "GenericBinaryOpStub_%s_%s%s",
5745 op_name,
5746 overwrite_name,
5747 specialized_on_rhs_ ? "_ConstantRhs" : 0);
5748 return name_;
5749}
5750
5751
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005752void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5753 // r1 : x
5754 // r0 : y
5755 // result : r0
5756
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005757 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5758 // tell us that.
5759 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5760
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005761 switch (op_) {
5762 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005763 Label not_smi;
5764 // Fast path.
5765 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005766 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005767 __ b(ne, &not_smi);
5768 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5769 // Return if no overflow.
5770 __ Ret(vc);
5771 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5772
5773 HandleBinaryOpSlowCases(masm,
5774 &not_smi,
5775 Builtins::ADD,
5776 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005777 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005778 break;
5779 }
5780
5781 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005782 Label not_smi;
5783 // Fast path.
5784 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005785 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005786 __ b(ne, &not_smi);
5787 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5788 // Return if no overflow.
5789 __ Ret(vc);
5790 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5791
5792 HandleBinaryOpSlowCases(masm,
5793 &not_smi,
5794 Builtins::SUB,
5795 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005796 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005797 break;
5798 }
5799
5800 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005801 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005802 ASSERT(kSmiTag == 0); // adjust code below
5803 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005804 __ b(ne, &not_smi);
5805 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005806 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005807 // Do multiplication
5808 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5809 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005810 __ mov(ip, Operand(r3, ASR, 31));
5811 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5812 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005813 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005814 __ tst(r3, Operand(r3));
5815 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005816 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005817 // We need -0 if we were multiplying a negative number with 0 to get 0.
5818 // We know one of them was zero.
5819 __ add(r2, r0, Operand(r1), SetCC);
5820 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5821 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5822 // Slow case. We fall through here if we multiplied a negative number
5823 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005824 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005825
5826 HandleBinaryOpSlowCases(masm,
5827 &not_smi,
5828 Builtins::MUL,
5829 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005830 mode_);
5831 break;
5832 }
5833
5834 case Token::DIV:
5835 case Token::MOD: {
5836 Label not_smi;
5837 if (specialized_on_rhs_) {
5838 Label smi_is_unsuitable;
5839 __ BranchOnNotSmi(r1, &not_smi);
5840 if (IsPowerOf2(constant_rhs_)) {
5841 if (op_ == Token::MOD) {
5842 __ and_(r0,
5843 r1,
5844 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5845 SetCC);
5846 // We now have the answer, but if the input was negative we also
5847 // have the sign bit. Our work is done if the result is
5848 // positive or zero:
5849 __ Ret(pl);
5850 // A mod of a negative left hand side must return a negative number.
5851 // Unfortunately if the answer is 0 then we must return -0. And we
5852 // already optimistically trashed r0 so we may need to restore it.
5853 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5854 // Next two instructions are conditional on the answer being -0.
5855 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5856 __ b(eq, &smi_is_unsuitable);
5857 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5858 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5859 } else {
5860 ASSERT(op_ == Token::DIV);
5861 __ tst(r1,
5862 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5863 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5864 int shift = 0;
5865 int d = constant_rhs_;
5866 while ((d & 1) == 0) {
5867 d >>= 1;
5868 shift++;
5869 }
5870 __ mov(r0, Operand(r1, LSR, shift));
5871 __ bic(r0, r0, Operand(kSmiTagMask));
5872 }
5873 } else {
5874 // Not a power of 2.
5875 __ tst(r1, Operand(0x80000000u));
5876 __ b(ne, &smi_is_unsuitable);
5877 // Find a fixed point reciprocal of the divisor so we can divide by
5878 // multiplying.
5879 double divisor = 1.0 / constant_rhs_;
5880 int shift = 32;
5881 double scale = 4294967296.0; // 1 << 32.
5882 uint32_t mul;
5883 // Maximise the precision of the fixed point reciprocal.
5884 while (true) {
5885 mul = static_cast<uint32_t>(scale * divisor);
5886 if (mul >= 0x7fffffff) break;
5887 scale *= 2.0;
5888 shift++;
5889 }
5890 mul++;
5891 __ mov(r2, Operand(mul));
5892 __ umull(r3, r2, r2, r1);
5893 __ mov(r2, Operand(r2, LSR, shift - 31));
5894 // r2 is r1 / rhs. r2 is not Smi tagged.
5895 // r0 is still the known rhs. r0 is Smi tagged.
5896 // r1 is still the unkown lhs. r1 is Smi tagged.
5897 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5898 // r4 = r2 * r0.
5899 MultiplyByKnownInt2(masm,
5900 r4,
5901 r2,
5902 r0,
5903 constant_rhs_,
5904 &required_r4_shift);
5905 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5906 if (op_ == Token::DIV) {
5907 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5908 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5909 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5910 } else {
5911 ASSERT(op_ == Token::MOD);
5912 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5913 }
5914 }
5915 __ Ret();
5916 __ bind(&smi_is_unsuitable);
5917 } else {
5918 __ jmp(&not_smi);
5919 }
5920 HandleBinaryOpSlowCases(masm,
5921 &not_smi,
5922 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5923 op_,
5924 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005925 break;
5926 }
5927
5928 case Token::BIT_OR:
5929 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005930 case Token::BIT_XOR:
5931 case Token::SAR:
5932 case Token::SHR:
5933 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005934 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005935 ASSERT(kSmiTag == 0); // adjust code below
5936 __ tst(r2, Operand(kSmiTagMask));
5937 __ b(ne, &slow);
5938 switch (op_) {
5939 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5940 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5941 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005942 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005943 // Remove tags from right operand.
5944 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5945 // Use only the 5 least significant bits of the shift count.
5946 __ and_(r2, r2, Operand(0x1f));
5947 __ mov(r0, Operand(r1, ASR, r2));
5948 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005949 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005950 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005951 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005952 // Remove tags from operands. We can't do this on a 31 bit number
5953 // because then the 0s get shifted into bit 30 instead of bit 31.
5954 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5955 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5956 // Use only the 5 least significant bits of the shift count.
5957 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005958 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005959 // Unsigned shift is not allowed to produce a negative number, so
5960 // check the sign bit and the sign bit after Smi tagging.
5961 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005962 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005963 // Smi tag result.
5964 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005965 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005966 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005967 // Remove tags from operands.
5968 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5969 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5970 // Use only the 5 least significant bits of the shift count.
5971 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005972 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005973 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005974 __ add(r2, r3, Operand(0x40000000), SetCC);
5975 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005976 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005977 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005978 default: UNREACHABLE();
5979 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005980 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005981 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005982 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005983 break;
5984 }
5985
5986 default: UNREACHABLE();
5987 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005988 // This code should be unreachable.
5989 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005990}
5991
5992
5993void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005994 // Do tail-call to runtime routine. Runtime routines expect at least one
5995 // argument, so give it a Smi.
5996 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005997 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00005998 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005999
6000 __ StubReturn(1);
6001}
6002
6003
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006004void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
6005 ASSERT(op_ == Token::SUB);
6006
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006007 Label undo;
6008 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006009 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006010
6011 // Enter runtime system if the value is not a smi.
6012 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006013 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006014
6015 // Enter runtime system if the value of the expression is zero
6016 // to make sure that we switch between 0 and -0.
6017 __ cmp(r0, Operand(0));
6018 __ b(eq, &slow);
6019
6020 // The value of the expression is a smi that is not zero. Try
6021 // optimistic subtraction '0 - value'.
6022 __ rsb(r1, r0, Operand(0), SetCC);
6023 __ b(vs, &slow);
6024
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006025 __ mov(r0, Operand(r1)); // Set r0 to result.
6026 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006027
6028 // Enter runtime system.
6029 __ bind(&slow);
6030 __ push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006031 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6032
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006033 __ bind(&not_smi);
6034 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6035 __ b(ne, &slow);
6036 // r0 is a heap number. Get a new heap number in r1.
6037 if (overwrite_) {
6038 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6039 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6040 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6041 } else {
6042 AllocateHeapNumber(masm, &slow, r1, r2, r3);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006043 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006044 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006045 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006046 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6047 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6048 __ mov(r0, Operand(r1));
6049 }
6050 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006051}
6052
6053
ager@chromium.orga1645e22009-09-09 19:27:10 +00006054int CEntryStub::MinorKey() {
6055 ASSERT(result_size_ <= 2);
6056 // Result returned in r0 or r0+r1 by default.
6057 return 0;
6058}
6059
6060
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006061void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006062 // r0 holds the exception.
6063
6064 // Adjust this code if not the case.
6065 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6066
6067 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006068 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6069 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006070
6071 // Restore the next handler and frame pointer, discard handler state.
6072 ASSERT(StackHandlerConstants::kNextOffset == 0);
6073 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006074 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006075 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6076 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6077
6078 // Before returning we restore the context from the frame pointer if
6079 // not NULL. The frame pointer is NULL in the exception handler of a
6080 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006081 __ cmp(fp, Operand(0));
6082 // Set cp to NULL if fp is NULL.
6083 __ mov(cp, Operand(0), LeaveCC, eq);
6084 // Restore cp otherwise.
6085 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006086#ifdef DEBUG
6087 if (FLAG_debug_code) {
6088 __ mov(lr, Operand(pc));
6089 }
6090#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006091 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006092 __ pop(pc);
6093}
6094
6095
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006096void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6097 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006098 // Adjust this code if not the case.
6099 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6100
6101 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006102 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006103 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006104
6105 // Unwind the handlers until the ENTRY handler is found.
6106 Label loop, done;
6107 __ bind(&loop);
6108 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006109 const int kStateOffset = StackHandlerConstants::kStateOffset;
6110 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006111 __ cmp(r2, Operand(StackHandler::ENTRY));
6112 __ b(eq, &done);
6113 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006114 const int kNextOffset = StackHandlerConstants::kNextOffset;
6115 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006116 __ jmp(&loop);
6117 __ bind(&done);
6118
6119 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006120 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006121 __ pop(r2);
6122 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006123
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006124 if (type == OUT_OF_MEMORY) {
6125 // Set external caught exception to false.
6126 ExternalReference external_caught(Top::k_external_caught_exception_address);
6127 __ mov(r0, Operand(false));
6128 __ mov(r2, Operand(external_caught));
6129 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006130
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006131 // Set pending exception and r0 to out of memory exception.
6132 Failure* out_of_memory = Failure::OutOfMemoryException();
6133 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6134 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6135 __ str(r0, MemOperand(r2));
6136 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006137
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006138 // Stack layout at this point. See also StackHandlerConstants.
6139 // sp -> state (ENTRY)
6140 // fp
6141 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006142
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006143 // Discard handler state (r2 is not used) and restore frame pointer.
6144 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6145 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6146 // Before returning we restore the context from the frame pointer if
6147 // not NULL. The frame pointer is NULL in the exception handler of a
6148 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006149 __ cmp(fp, Operand(0));
6150 // Set cp to NULL if fp is NULL.
6151 __ mov(cp, Operand(0), LeaveCC, eq);
6152 // Restore cp otherwise.
6153 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006154#ifdef DEBUG
6155 if (FLAG_debug_code) {
6156 __ mov(lr, Operand(pc));
6157 }
6158#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006159 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006160 __ pop(pc);
6161}
6162
6163
6164void CEntryStub::GenerateCore(MacroAssembler* masm,
6165 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006166 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006167 Label* throw_out_of_memory_exception,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006168 ExitFrame::Mode mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006169 bool do_gc,
6170 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006171 // r0: result parameter for PerformGC, if any
6172 // r4: number of arguments including receiver (C callee-saved)
6173 // r5: pointer to builtin function (C callee-saved)
6174 // r6: pointer to the first argument (C callee-saved)
6175
6176 if (do_gc) {
6177 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006178 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6179 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006180 }
6181
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006182 ExternalReference scope_depth =
6183 ExternalReference::heap_always_allocate_scope_depth();
6184 if (always_allocate) {
6185 __ mov(r0, Operand(scope_depth));
6186 __ ldr(r1, MemOperand(r0));
6187 __ add(r1, r1, Operand(1));
6188 __ str(r1, MemOperand(r0));
6189 }
6190
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006191 // Call C built-in.
6192 // r0 = argc, r1 = argv
6193 __ mov(r0, Operand(r4));
6194 __ mov(r1, Operand(r6));
6195
6196 // TODO(1242173): To let the GC traverse the return address of the exit
6197 // frames, we need to know where the return address is. Right now,
6198 // we push it on the stack to be able to find it again, but we never
6199 // restore from it in case of changes, which makes it impossible to
6200 // support moving the C entry code stub. This should be fixed, but currently
6201 // this is OK because the CEntryStub gets generated so early in the V8 boot
6202 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006203 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6204 masm->push(lr);
6205 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006206
6207 if (always_allocate) {
6208 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6209 // though (contain the result).
6210 __ mov(r2, Operand(scope_depth));
6211 __ ldr(r3, MemOperand(r2));
6212 __ sub(r3, r3, Operand(1));
6213 __ str(r3, MemOperand(r2));
6214 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006215
6216 // check for failure result
6217 Label failure_returned;
6218 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6219 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6220 __ add(r2, r0, Operand(1));
6221 __ tst(r2, Operand(kFailureTagMask));
6222 __ b(eq, &failure_returned);
6223
6224 // Exit C frame and return.
6225 // r0:r1: result
6226 // sp: stack pointer
6227 // fp: frame pointer
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006228 __ LeaveExitFrame(mode);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006229
6230 // check if we should retry or throw exception
6231 Label retry;
6232 __ bind(&failure_returned);
6233 ASSERT(Failure::RETRY_AFTER_GC == 0);
6234 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6235 __ b(eq, &retry);
6236
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006237 // Special handling of out of memory exceptions.
6238 Failure* out_of_memory = Failure::OutOfMemoryException();
6239 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6240 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006241
6242 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006243 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006244 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006245 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006246 __ ldr(r0, MemOperand(ip));
6247 __ str(r3, MemOperand(ip));
6248
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006249 // Special handling of termination exceptions which are uncatchable
6250 // by javascript code.
6251 __ cmp(r0, Operand(Factory::termination_exception()));
6252 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006253
6254 // Handle normal exception.
6255 __ jmp(throw_normal_exception);
6256
6257 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6258}
6259
6260
6261void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
6262 // Called from JavaScript; parameters are on stack as if calling JS function
6263 // r0: number of arguments including receiver
6264 // r1: pointer to builtin function
6265 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006266 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006267 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006268
6269 // NOTE: Invocations of builtins may return failure objects
6270 // instead of a proper result. The builtin entry handles
6271 // this by performing a garbage collection and retrying the
6272 // builtin once.
6273
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006274 ExitFrame::Mode mode = is_debug_break
6275 ? ExitFrame::MODE_DEBUG
6276 : ExitFrame::MODE_NORMAL;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006277
6278 // Enter the exit frame that transitions from JavaScript to C++.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006279 __ EnterExitFrame(mode);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006280
6281 // r4: number of arguments (C callee-saved)
6282 // r5: pointer to builtin function (C callee-saved)
6283 // r6: pointer to first argument (C callee-saved)
6284
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006285 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006286 Label throw_termination_exception;
6287 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006288
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006289 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006290 GenerateCore(masm,
6291 &throw_normal_exception,
6292 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006293 &throw_out_of_memory_exception,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006294 mode,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006295 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006296 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006297
6298 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006299 GenerateCore(masm,
6300 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006301 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006302 &throw_out_of_memory_exception,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006303 mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006304 true,
6305 false);
6306
6307 // Do full GC and retry runtime call one final time.
6308 Failure* failure = Failure::InternalError();
6309 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6310 GenerateCore(masm,
6311 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006312 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006313 &throw_out_of_memory_exception,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006314 mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006315 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006316 true);
6317
6318 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006319 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6320
6321 __ bind(&throw_termination_exception);
6322 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006323
6324 __ bind(&throw_normal_exception);
6325 GenerateThrowTOS(masm);
6326}
6327
6328
6329void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6330 // r0: code entry
6331 // r1: function
6332 // r2: receiver
6333 // r3: argc
6334 // [sp+0]: argv
6335
6336 Label invoke, exit;
6337
6338 // Called from C, so do not pop argc and args on exit (preserve sp)
6339 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006340 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006341 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6342
6343 // Get address of argv, see stm above.
6344 // r0: code entry
6345 // r1: function
6346 // r2: receiver
6347 // r3: argc
6348 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
6349 __ ldr(r4, MemOperand(r4)); // argv
6350
6351 // Push a frame with special values setup to mark it as an entry frame.
6352 // r0: code entry
6353 // r1: function
6354 // r2: receiver
6355 // r3: argc
6356 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006357 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006358 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6359 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006360 __ mov(r6, Operand(Smi::FromInt(marker)));
6361 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6362 __ ldr(r5, MemOperand(r5));
6363 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6364
6365 // Setup frame pointer for the frame to be pushed.
6366 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6367
6368 // Call a faked try-block that does the invoke.
6369 __ bl(&invoke);
6370
6371 // Caught exception: Store result (exception) in the pending
6372 // exception field in the JSEnv and return a failure sentinel.
6373 // Coming in here the fp will be invalid because the PushTryHandler below
6374 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006375 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006376 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006377 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006378 __ b(&exit);
6379
6380 // Invoke: Link this frame into the handler chain.
6381 __ bind(&invoke);
6382 // Must preserve r0-r4, r5-r7 are available.
6383 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006384 // If an exception not caught by another handler occurs, this handler
6385 // returns control to the code after the bl(&invoke) above, which
6386 // restores all kCalleeSaved registers (including cp and fp) to their
6387 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006388
6389 // Clear any pending exceptions.
6390 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6391 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006392 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006393 __ str(r5, MemOperand(ip));
6394
6395 // Invoke the function by calling through JS entry trampoline builtin.
6396 // Notice that we cannot store a reference to the trampoline code directly in
6397 // this stub, because runtime stubs are not traversed when doing GC.
6398
6399 // Expected registers by Builtins::JSEntryTrampoline
6400 // r0: code entry
6401 // r1: function
6402 // r2: receiver
6403 // r3: argc
6404 // r4: argv
6405 if (is_construct) {
6406 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6407 __ mov(ip, Operand(construct_entry));
6408 } else {
6409 ExternalReference entry(Builtins::JSEntryTrampoline);
6410 __ mov(ip, Operand(entry));
6411 }
6412 __ ldr(ip, MemOperand(ip)); // deref address
6413
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006414 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6415 // macro for the add instruction because we don't want the coverage tool
6416 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006417 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006418 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006419
6420 // Unlink this frame from the handler chain. When reading the
6421 // address of the next handler, there is no need to use the address
6422 // displacement since the current stack pointer (sp) points directly
6423 // to the stack handler.
6424 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6425 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6426 __ str(r3, MemOperand(ip));
6427 // No need to restore registers
6428 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6429
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006430
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006431 __ bind(&exit); // r0 holds result
6432 // Restore the top frame descriptors from the stack.
6433 __ pop(r3);
6434 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6435 __ str(r3, MemOperand(ip));
6436
6437 // Reset the stack to the callee saved registers.
6438 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6439
6440 // Restore callee-saved registers and return.
6441#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006442 if (FLAG_debug_code) {
6443 __ mov(lr, Operand(pc));
6444 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006445#endif
6446 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6447}
6448
6449
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006450// This stub performs an instanceof, calling the builtin function if
6451// necessary. Uses r1 for the object, r0 for the function that it may
6452// be an instance of (these are fetched from the stack).
6453void InstanceofStub::Generate(MacroAssembler* masm) {
6454 // Get the object - slow case for smis (we may need to throw an exception
6455 // depending on the rhs).
6456 Label slow, loop, is_instance, is_not_instance;
6457 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6458 __ BranchOnSmi(r0, &slow);
6459
6460 // Check that the left hand is a JS object and put map in r3.
6461 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6462 __ b(lt, &slow);
6463 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6464 __ b(gt, &slow);
6465
6466 // Get the prototype of the function (r4 is result, r2 is scratch).
6467 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6468 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6469
6470 // Check that the function prototype is a JS object.
6471 __ BranchOnSmi(r4, &slow);
6472 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6473 __ b(lt, &slow);
6474 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6475 __ b(gt, &slow);
6476
6477 // Register mapping: r3 is object map and r4 is function prototype.
6478 // Get prototype of object into r2.
6479 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6480
6481 // Loop through the prototype chain looking for the function prototype.
6482 __ bind(&loop);
6483 __ cmp(r2, Operand(r4));
6484 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006485 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6486 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006487 __ b(eq, &is_not_instance);
6488 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6489 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6490 __ jmp(&loop);
6491
6492 __ bind(&is_instance);
6493 __ mov(r0, Operand(Smi::FromInt(0)));
6494 __ pop();
6495 __ pop();
6496 __ mov(pc, Operand(lr)); // Return.
6497
6498 __ bind(&is_not_instance);
6499 __ mov(r0, Operand(Smi::FromInt(1)));
6500 __ pop();
6501 __ pop();
6502 __ mov(pc, Operand(lr)); // Return.
6503
6504 // Slow-case. Tail call builtin.
6505 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006506 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6507}
6508
6509
ager@chromium.org7c537e22008-10-16 08:43:32 +00006510void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006511 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006512 Label adaptor;
6513 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6514 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006515 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006516 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006517
ager@chromium.org7c537e22008-10-16 08:43:32 +00006518 // Nothing to do: The formal number of parameters has already been
6519 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006520 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006521
ager@chromium.org7c537e22008-10-16 08:43:32 +00006522 // Arguments adaptor case: Read the arguments length from the
6523 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006524 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006525 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006526 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006527}
6528
6529
ager@chromium.org7c537e22008-10-16 08:43:32 +00006530void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6531 // The displacement is the offset of the last parameter (if any)
6532 // relative to the frame pointer.
6533 static const int kDisplacement =
6534 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006535
ager@chromium.org7c537e22008-10-16 08:43:32 +00006536 // Check that the key is a smi.
6537 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006538 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006539
ager@chromium.org7c537e22008-10-16 08:43:32 +00006540 // Check if the calling frame is an arguments adaptor frame.
6541 Label adaptor;
6542 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6543 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006544 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006545 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006546
ager@chromium.org7c537e22008-10-16 08:43:32 +00006547 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006548 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006549 // check for free.
6550 __ cmp(r1, r0);
6551 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006552
ager@chromium.org7c537e22008-10-16 08:43:32 +00006553 // Read the argument from the stack and return it.
6554 __ sub(r3, r0, r1);
6555 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6556 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006557 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006558
6559 // Arguments adaptor case: Check index against actual arguments
6560 // limit found in the arguments adaptor frame. Use unsigned
6561 // comparison to get negative check for free.
6562 __ bind(&adaptor);
6563 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6564 __ cmp(r1, r0);
6565 __ b(cs, &slow);
6566
6567 // Read the argument from the adaptor frame and return it.
6568 __ sub(r3, r0, r1);
6569 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6570 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006571 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006572
6573 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6574 // by calling the runtime system.
6575 __ bind(&slow);
6576 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006577 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006578}
6579
6580
6581void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6582 // Check if the calling frame is an arguments adaptor frame.
6583 Label runtime;
6584 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6585 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006586 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006587 __ b(ne, &runtime);
6588
6589 // Patch the arguments.length and the parameters pointer.
6590 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6591 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6592 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6593 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6594 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6595
6596 // Do the runtime call to allocate the arguments object.
6597 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006598 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006599}
6600
6601
6602void CallFunctionStub::Generate(MacroAssembler* masm) {
6603 Label slow;
6604 // Get the function to call from the stack.
6605 // function, receiver [, arguments]
6606 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6607
6608 // Check that the function is really a JavaScript function.
6609 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006610 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006611 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006612 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006613 __ b(ne, &slow);
6614
6615 // Fast-case: Invoke the function now.
6616 // r1: pushed function
6617 ParameterCount actual(argc_);
6618 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6619
6620 // Slow-case: Non-function called.
6621 __ bind(&slow);
6622 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006623 __ mov(r2, Operand(0));
6624 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6625 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6626 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006627}
6628
6629
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006630const char* CompareStub::GetName() {
6631 switch (cc_) {
6632 case lt: return "CompareStub_LT";
6633 case gt: return "CompareStub_GT";
6634 case le: return "CompareStub_LE";
6635 case ge: return "CompareStub_GE";
6636 case ne: {
6637 if (strict_) {
6638 if (never_nan_nan_) {
6639 return "CompareStub_NE_STRICT_NO_NAN";
6640 } else {
6641 return "CompareStub_NE_STRICT";
6642 }
6643 } else {
6644 if (never_nan_nan_) {
6645 return "CompareStub_NE_NO_NAN";
6646 } else {
6647 return "CompareStub_NE";
6648 }
6649 }
6650 }
6651 case eq: {
6652 if (strict_) {
6653 if (never_nan_nan_) {
6654 return "CompareStub_EQ_STRICT_NO_NAN";
6655 } else {
6656 return "CompareStub_EQ_STRICT";
6657 }
6658 } else {
6659 if (never_nan_nan_) {
6660 return "CompareStub_EQ_NO_NAN";
6661 } else {
6662 return "CompareStub_EQ";
6663 }
6664 }
6665 }
6666 default: return "CompareStub";
6667 }
6668}
6669
6670
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006671int CompareStub::MinorKey() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006672 // Encode the three parameters in a unique 16 bit value.
6673 ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16));
6674 int nnn_value = (never_nan_nan_ ? 2 : 0);
6675 if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs.
6676 return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006677}
6678
6679
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006680#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006682} } // namespace v8::internal