blob: 3292bdcff20ffda1c6601557b9ca55781c7e9be1 [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"
32#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000033#include "parser.h"
34#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "scopes.h"
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#define __ ACCESS_MASM(masm_)
43
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000044static void EmitIdenticalObjectComparison(MacroAssembler* masm,
45 Label* slow,
46 Condition cc);
47static void EmitSmiNonsmiComparison(MacroAssembler* masm,
48 Label* rhs_not_nan,
49 Label* slow,
50 bool strict);
51static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
52static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000053static void MultiplyByKnownInt(MacroAssembler* masm,
54 Register source,
55 Register destination,
56 int known_int);
57static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000058
59
60
ager@chromium.orge2902be2009-06-08 12:21:35 +000061// -------------------------------------------------------------------------
62// Platform-specific DeferredCode functions.
63
64void DeferredCode::SaveRegisters() {
65 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
66 int action = registers_[i];
67 if (action == kPush) {
68 __ push(RegisterAllocator::ToRegister(i));
69 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
70 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
71 }
72 }
73}
74
75
76void DeferredCode::RestoreRegisters() {
77 // Restore registers in reverse order due to the stack.
78 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
79 int action = registers_[i];
80 if (action == kPush) {
81 __ pop(RegisterAllocator::ToRegister(i));
82 } else if (action != kIgnore) {
83 action &= ~kSyncedFlag;
84 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
85 }
86 }
87}
88
ager@chromium.org3bf7b912008-11-17 09:09:45 +000089
90// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000091// CodeGenState implementation.
92
ager@chromium.org7c537e22008-10-16 08:43:32 +000093CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000094 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000095 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 true_target_(NULL),
97 false_target_(NULL),
98 previous_(NULL) {
99 owner_->set_state(this);
100}
101
102
ager@chromium.org7c537e22008-10-16 08:43:32 +0000103CodeGenState::CodeGenState(CodeGenerator* owner,
104 TypeofState typeof_state,
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),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000109 true_target_(true_target),
110 false_target_(false_target),
111 previous_(owner->state()) {
112 owner_->set_state(this);
113}
114
115
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000116CodeGenState::~CodeGenState() {
117 ASSERT(owner_->state() == this);
118 owner_->set_state(previous_);
119}
120
121
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000122// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000123// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
ager@chromium.org7c537e22008-10-16 08:43:32 +0000125CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
126 bool is_eval)
127 : is_eval_(is_eval),
128 script_(script),
129 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 masm_(new MacroAssembler(NULL, buffer_size)),
131 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 cc_reg_(al),
135 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000136 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000143// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144// cp: callee's context
145
ager@chromium.org7c537e22008-10-16 08:43:32 +0000146void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 ZoneList<Statement*>* body = fun->body();
148
149 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000150 ASSERT(scope_ == NULL);
151 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000152 ASSERT(allocator_ == NULL);
153 RegisterAllocator register_allocator(this);
154 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000155 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000156 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 cc_reg_ = al;
158 {
159 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000161 // Entry:
162 // Stack: receiver, arguments
163 // lr: return address
164 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000169 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 // tos: code slot
171#ifdef DEBUG
172 if (strlen(FLAG_stop_at) > 0 &&
173 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000175 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 }
177#endif
178
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000179 // Allocate space for locals and initialize them. This also checks
180 // for stack overflow.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000181 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 // Initialize the function return target after the locals are set
183 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000184 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000185 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000187 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000188 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 // Allocate local context.
190 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000191 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000192 frame_->EmitPush(r0);
193 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000194
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000195#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000196 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000197 __ cmp(r0, Operand(cp));
198 verified_true.Branch(eq);
199 __ stop("NewContext: r0 is expected to be the same as cp");
200 verified_true.Bind();
201#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 }
205
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000206 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 // 1) only needed if we have a context
208 // 2) no need to recompute context ptr every single time
209 // 3) don't copy parameter operand code from SlotOperand!
210 {
211 Comment cmnt2(masm_, "[ copy context parameters into .context");
212
213 // Note that iteration order is relevant here! If we have the same
214 // parameter twice (e.g., function (x, y, x)), and that parameter
215 // needs to be copied into the context, it must be the last argument
216 // passed to the parameter that needs to be copied. This is a rare
217 // case so we don't check for it, instead we rely on the copying
218 // order: such a parameter is copied repeatedly into the same
219 // context location and thus the last value is what is seen inside
220 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000221 for (int i = 0; i < scope_->num_parameters(); i++) {
222 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 Slot* slot = par->slot();
224 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000225 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000226 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 // Loads r2 with context; used below in RecordWrite.
228 __ str(r1, SlotOperand(slot, r2));
229 // Load the offset into r3.
230 int slot_offset =
231 FixedArray::kHeaderSize + slot->index() * kPointerSize;
232 __ mov(r3, Operand(slot_offset));
233 __ RecordWrite(r2, r3, r1);
234 }
235 }
236 }
237
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000238 // Store the arguments object. This must happen after context
239 // initialization because the arguments object may be stored in the
240 // context.
241 if (scope_->arguments() != NULL) {
242 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000244 { Reference shadow_ref(this, scope_->arguments_shadow());
245 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000246 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000247 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000248 // The receiver is below the arguments, the return address,
249 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000250 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000251 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000252 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000253 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000254 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000255 frame_->CallStub(&stub, 3);
256 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000257 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000258 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000261 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 }
263
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000264 // Generate code to 'execute' declarations and initialize functions
265 // (source elements). In case of an illegal redeclaration we need to
266 // handle that instead of processing the declarations.
267 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000269 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 } else {
271 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000272 ProcessDeclarations(scope_->declarations());
273 // Bail out if a stack-overflow exception occurred when processing
274 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000275 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 }
277
mads.s.ager31e71382008-08-13 09:32:07 +0000278 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000279 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000280 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000281 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282
283 // Compile the body of the function in a vanilla state. Don't
284 // bother compiling all the code if the scope has an illegal
285 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000286 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 Comment cmnt(masm_, "[ function body");
288#ifdef DEBUG
289 bool is_builtin = Bootstrapper::IsActive();
290 bool should_trace =
291 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000292 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000293 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000294 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000295 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 }
300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000302 if (has_valid_frame() || function_return_.is_linked()) {
303 if (!function_return_.is_linked()) {
304 CodeForReturnPosition(fun);
305 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000306 // exit
307 // r0: result
308 // sp: stack pointer
309 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000311 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000312
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000313 function_return_.Bind();
314 if (FLAG_trace) {
315 // Push the return value on the stack as the parameter.
316 // Runtime::TraceExit returns the parameter as it is.
317 frame_->EmitPush(r0);
318 frame_->CallRuntime(Runtime::kTraceExit, 1);
319 }
320
ager@chromium.org4af710e2009-09-15 12:20:11 +0000321 // Add a label for checking the size of the code used for returning.
322 Label check_exit_codesize;
323 masm_->bind(&check_exit_codesize);
324
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000325 // Tear down the frame which will restore the caller's frame pointer and
326 // the link register.
327 frame_->Exit();
328
ager@chromium.org4af710e2009-09-15 12:20:11 +0000329 // Here we use masm_-> instead of the __ macro to avoid the code coverage
330 // tool from instrumenting as we rely on the code size here.
331 masm_->add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
332 masm_->Jump(lr);
333
334 // Check that the size of the code used for returning matches what is
335 // expected by the debugger.
336 ASSERT_EQ(kJSReturnSequenceLength,
337 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000338 }
339
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 ASSERT(!has_cc());
342 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000343 ASSERT(!function_return_is_shadowed_);
344 function_return_.Unuse();
345 DeleteFrame();
346
347 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000348 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000349 ProcessDeferred();
350 }
351
352 allocator_ = NULL;
353 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354}
355
356
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
358 // Currently, this assertion will fail if we try to assign to
359 // a constant variable that is constant because it is read-only
360 // (such as the variable referring to a named function expression).
361 // We need to implement assignments to read-only variables.
362 // Ideally, we should do this during AST generation (by converting
363 // such assignments into expression statements); however, in general
364 // we may not be able to make the decision until past AST generation,
365 // that is when the entire program is known.
366 ASSERT(slot != NULL);
367 int index = slot->index();
368 switch (slot->type()) {
369 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000370 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000371
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000372 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000373 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000374
375 case Slot::CONTEXT: {
376 // Follow the context chain if necessary.
377 ASSERT(!tmp.is(cp)); // do not overwrite context register
378 Register context = cp;
379 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000380 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000381 // Load the closure.
382 // (All contexts, even 'with' contexts, have a closure,
383 // and it is the same for all contexts inside a function.
384 // There is no need to go to the function context first.)
385 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
386 // Load the function context (which is the incoming, outer context).
387 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
388 context = tmp;
389 }
390 // We may have a 'with' context now. Get the function context.
391 // (In fact this mov may never be the needed, since the scope analysis
392 // may not permit a direct context access in this case and thus we are
393 // always at a function context. However it is safe to dereference be-
394 // cause the function context of a function context is itself. Before
395 // deleting this mov we should try to create a counter-example first,
396 // though...)
397 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
398 return ContextOperand(tmp, index);
399 }
400
401 default:
402 UNREACHABLE();
403 return MemOperand(r0, 0);
404 }
405}
406
407
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000408MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
409 Slot* slot,
410 Register tmp,
411 Register tmp2,
412 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000413 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000414 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415
ager@chromium.org381abbb2009-02-25 13:23:22 +0000416 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
417 if (s->num_heap_slots() > 0) {
418 if (s->calls_eval()) {
419 // Check that extension is NULL.
420 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
421 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000422 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000423 }
424 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
425 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
426 context = tmp;
427 }
428 }
429 // Check that last extension is NULL.
430 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
431 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000433 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000434 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000435}
436
437
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000438// Loads a value on TOS. If it is a boolean value, the result may have been
439// (partially) translated into branches, or it may have set the condition
440// code register. If force_cc is set, the value is forced to set the
441// condition code register and no value is pushed. If the condition code
442// register was set, has_cc() is true and cc_reg_ contains the condition to
443// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000444void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000445 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000446 JumpTarget* true_target,
447 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000448 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000449 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000450 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451
ager@chromium.org7c537e22008-10-16 08:43:32 +0000452 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000453 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454
455 // If we hit a stack overflow, we may not have actually visited
456 // the expression. In that case, we ensure that we have a
457 // valid-looking frame state because we will continue to generate
458 // code as we unwind the C++ stack.
459 //
460 // It's possible to have both a stack overflow and a valid frame
461 // state (eg, a subexpression overflowed, visiting it returned
462 // with a dummied frame state, and visiting this expression
463 // returned with a normal-looking state).
464 if (HasStackOverflow() &&
465 has_valid_frame() &&
466 !has_cc() &&
467 frame_->height() == original_height) {
468 true_target->Jump();
469 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000470 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000471 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000472 // Convert the TOS value to a boolean in the condition code register.
473 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475 ASSERT(!force_cc || !has_valid_frame() || has_cc());
476 ASSERT(!has_valid_frame() ||
477 (has_cc() && frame_->height() == original_height) ||
478 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
ager@chromium.org7c537e22008-10-16 08:43:32 +0000482void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000483#ifdef DEBUG
484 int original_height = frame_->height();
485#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000486 JumpTarget true_target;
487 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000488 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489
490 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000492 JumpTarget loaded;
493 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000495 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000496 frame_->EmitPush(r0);
497 loaded.Jump();
498 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000499 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000500 frame_->EmitPush(r0);
501 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 cc_reg_ = al;
503 }
504
505 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506 // We have at least one condition value that has been "translated"
507 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000508 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000509 if (frame_ != NULL) {
510 loaded.Jump(); // Don't lose the current TOS.
511 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000513 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000516 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000517 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 // If both "true" and "false" need to be loaded jump across the code for
520 // "false".
521 if (both) {
522 loaded.Jump();
523 }
524 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000527 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 // A value is loaded on all paths reaching this point.
531 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000533 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536}
537
538
ager@chromium.org7c537e22008-10-16 08:43:32 +0000539void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000540 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000541 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543}
544
545
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000546void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000547 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000548 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
549 __ ldr(scratch,
550 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000551 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000552}
553
554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000556// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
557// variables w/o reference errors elsewhere.
558void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000559 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 Variable* variable = x->AsVariableProxy()->AsVariable();
561 if (variable != NULL && !variable->is_this() && variable->is_global()) {
562 // NOTE: This is somewhat nasty. We force the compiler to load
563 // the variable as if through '<global>.<variable>' to make sure we
564 // do not get reference errors.
565 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
566 Literal key(variable->name());
567 // TODO(1241834): Fetch the position from the variable instead of using
568 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000569 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000572 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
574}
575
576
ager@chromium.org7c537e22008-10-16 08:43:32 +0000577Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
579 cgen->LoadReference(this);
580}
581
582
583Reference::~Reference() {
584 cgen_->UnloadReference(this);
585}
586
587
ager@chromium.org7c537e22008-10-16 08:43:32 +0000588void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000589 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000590 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 Expression* e = ref->expression();
592 Property* property = e->AsProperty();
593 Variable* var = e->AsVariableProxy()->AsVariable();
594
595 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000596 // The expression is either a property or a variable proxy that rewrites
597 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000598 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000599 // We use a named reference if the key is a literal symbol, unless it is
600 // a string that can be legally parsed as an integer. This is because
601 // otherwise we will not get into the slow case code that handles [] on
602 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 Literal* literal = property->key()->AsLiteral();
604 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000605 if (literal != NULL &&
606 literal->handle()->IsSymbol() &&
607 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 ref->set_type(Reference::NAMED);
609 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000610 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 ref->set_type(Reference::KEYED);
612 }
613 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000614 // The expression is a variable proxy that does not rewrite to a
615 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 LoadGlobal();
618 ref->set_type(Reference::NAMED);
619 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000620 ASSERT(var->slot() != NULL);
621 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 }
623 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000624 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000625 LoadAndSpill(e);
626 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 }
628}
629
630
ager@chromium.org7c537e22008-10-16 08:43:32 +0000631void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000632 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000633 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000634 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000636 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000637 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000638 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000639 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 }
641}
642
643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
645// register to a boolean in the condition code register. The code
646// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647void CodeGenerator::ToBoolean(JumpTarget* true_target,
648 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000649 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000650 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000652 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 // Fast case checks
655
mads.s.ager31e71382008-08-13 09:32:07 +0000656 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000657 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
658 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000659 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660
mads.s.ager31e71382008-08-13 09:32:07 +0000661 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000662 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
663 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000664 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
mads.s.ager31e71382008-08-13 09:32:07 +0000666 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000667 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
668 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
mads.s.ager31e71382008-08-13 09:32:07 +0000671 // Check if the value is a smi.
672 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000673 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000674 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000675 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676
677 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000678 frame_->EmitPush(r0);
679 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000680 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000681 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
682 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
684 cc_reg_ = ne;
685}
686
687
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000688void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000689 OverwriteMode overwrite_mode,
690 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000692 // sp[0] : y
693 // sp[1] : x
694 // result : r0
695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 // Stub is entered with a call: 'return address' is in lr.
697 switch (op) {
698 case Token::ADD: // fall through.
699 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000700 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000701 case Token::DIV:
702 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000703 case Token::BIT_OR:
704 case Token::BIT_AND:
705 case Token::BIT_XOR:
706 case Token::SHL:
707 case Token::SHR:
708 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000709 frame_->EmitPop(r0); // r0 : y
710 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000711 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000712 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 break;
714 }
715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000717 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 break;
721
722 default:
723 // Other cases should have been handled before this point.
724 UNREACHABLE();
725 break;
726 }
727}
728
729
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000730class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000731 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000732 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000733 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000734 bool reversed,
735 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000736 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000737 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000738 reversed_(reversed),
739 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000740 set_comment("[ DeferredInlinedSmiOperation");
741 }
742
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000744
745 private:
746 Token::Value op_;
747 int value_;
748 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000749 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000750};
751
752
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000754 switch (op_) {
755 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000756 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000757 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000758 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
759 __ mov(r1, Operand(Smi::FromInt(value_)));
760 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000761 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
762 __ mov(r0, Operand(Smi::FromInt(value_)));
763 }
764 break;
765 }
766
767 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000768 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000770 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
771 __ mov(r1, Operand(Smi::FromInt(value_)));
772 } else {
773 __ add(r1, r0, Operand(Smi::FromInt(value_)));
774 __ mov(r0, Operand(Smi::FromInt(value_)));
775 }
776 break;
777 }
778
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000779 // For these operations there is no optimistic operation that needs to be
780 // reverted.
781 case Token::MUL:
782 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783 case Token::BIT_OR:
784 case Token::BIT_XOR:
785 case Token::BIT_AND: {
786 if (reversed_) {
787 __ mov(r1, Operand(Smi::FromInt(value_)));
788 } else {
789 __ mov(r1, Operand(r0));
790 __ mov(r0, Operand(Smi::FromInt(value_)));
791 }
792 break;
793 }
794
795 case Token::SHL:
796 case Token::SHR:
797 case Token::SAR: {
798 if (!reversed_) {
799 __ mov(r1, Operand(r0));
800 __ mov(r0, Operand(Smi::FromInt(value_)));
801 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000802 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000803 }
804 break;
805 }
806
807 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000808 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000809 UNREACHABLE();
810 break;
811 }
812
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000813 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000814 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000815}
816
817
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000818static bool PopCountLessThanEqual2(unsigned int x) {
819 x &= x - 1;
820 return (x & (x - 1)) == 0;
821}
822
823
824// Returns the index of the lowest bit set.
825static int BitPosition(unsigned x) {
826 int bit_posn = 0;
827 while ((x & 0xf) == 0) {
828 bit_posn += 4;
829 x >>= 4;
830 }
831 while ((x & 1) == 0) {
832 bit_posn++;
833 x >>= 1;
834 }
835 return bit_posn;
836}
837
838
ager@chromium.org7c537e22008-10-16 08:43:32 +0000839void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000840 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000841 bool reversed,
842 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000843 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844 // NOTE: This is an attempt to inline (a bit) more of the code for
845 // some possible smi operations (like + and -) when (at least) one
846 // of the operands is a literal smi. With this optimization, the
847 // performance of the system is increased by ~15%, and the generated
848 // code size is increased by ~1% (measured on a combination of
849 // different benchmarks).
850
mads.s.ager31e71382008-08-13 09:32:07 +0000851 // sp[0] : operand
852
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000853 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000855 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000856 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000858 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 switch (op) {
860 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000861 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000862 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000864 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000865 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000867 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000868 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 break;
870 }
871
872 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000873 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000874 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875
ager@chromium.orge2902be2009-06-08 12:21:35 +0000876 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000877 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000878 } else {
879 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000881 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000883 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000884 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 break;
886 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000888
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 case Token::BIT_OR:
890 case Token::BIT_XOR:
891 case Token::BIT_AND: {
892 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000893 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000894 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000895 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000896 switch (op) {
897 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
898 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
899 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
900 default: UNREACHABLE();
901 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 break;
904 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 case Token::SHL:
907 case Token::SHR:
908 case Token::SAR: {
909 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000910 something_to_inline = false;
911 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000913 int shift_value = int_value & 0x1f; // least significant 5 bits
914 DeferredCode* deferred =
915 new DeferredInlineSmiOperation(op, shift_value, false, mode);
916 __ tst(r0, Operand(kSmiTagMask));
917 deferred->Branch(ne);
918 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
919 switch (op) {
920 case Token::SHL: {
921 if (shift_value != 0) {
922 __ mov(r2, Operand(r2, LSL, shift_value));
923 }
924 // check that the *unsigned* result fits in a smi
925 __ add(r3, r2, Operand(0x40000000), SetCC);
926 deferred->Branch(mi);
927 break;
928 }
929 case Token::SHR: {
930 // LSR by immediate 0 means shifting 32 bits.
931 if (shift_value != 0) {
932 __ mov(r2, Operand(r2, LSR, shift_value));
933 }
934 // check that the *unsigned* result fits in a smi
935 // neither of the two high-order bits can be set:
936 // - 0x80000000: high bit would be lost when smi tagging
937 // - 0x40000000: this number would convert to negative when
938 // smi tagging these two cases can only happen with shifts
939 // by 0 or 1 when handed a valid smi
940 __ and_(r3, r2, Operand(0xc0000000), SetCC);
941 deferred->Branch(ne);
942 break;
943 }
944 case Token::SAR: {
945 if (shift_value != 0) {
946 // ASR by immediate 0 means shifting 32 bits.
947 __ mov(r2, Operand(r2, ASR, shift_value));
948 }
949 break;
950 }
951 default: UNREACHABLE();
952 }
953 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
954 deferred->BindExit();
955 break;
956 }
957
958 case Token::MOD: {
959 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
960 something_to_inline = false;
961 break;
962 }
963 DeferredCode* deferred =
964 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
965 unsigned mask = (0x80000000u | kSmiTagMask);
966 __ tst(r0, Operand(mask));
967 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
968 mask = (int_value << kSmiTagSize) - 1;
969 __ and_(r0, r0, Operand(mask));
970 deferred->BindExit();
971 break;
972 }
973
974 case Token::MUL: {
975 if (!IsEasyToMultiplyBy(int_value)) {
976 something_to_inline = false;
977 break;
978 }
979 DeferredCode* deferred =
980 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
981 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
982 max_smi_that_wont_overflow <<= kSmiTagSize;
983 unsigned mask = 0x80000000u;
984 while ((mask & max_smi_that_wont_overflow) == 0) {
985 mask |= mask >> 1;
986 }
987 mask |= kSmiTagMask;
988 // This does a single mask that checks for a too high value in a
989 // conservative way and for a non-Smi. It also filters out negative
990 // numbers, unfortunately, but since this code is inline we prefer
991 // brevity to comprehensiveness.
992 __ tst(r0, Operand(mask));
993 deferred->Branch(ne);
994 MultiplyByKnownInt(masm_, r0, r0, int_value);
995 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 break;
997 }
998
999 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001000 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 break;
1002 }
1003
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001004 if (!something_to_inline) {
1005 if (!reversed) {
1006 frame_->EmitPush(r0);
1007 __ mov(r0, Operand(value));
1008 frame_->EmitPush(r0);
1009 GenericBinaryOperation(op, mode, int_value);
1010 } else {
1011 __ mov(ip, Operand(value));
1012 frame_->EmitPush(ip);
1013 frame_->EmitPush(r0);
1014 GenericBinaryOperation(op, mode, kUnknownIntValue);
1015 }
1016 }
1017
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001018 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019}
1020
1021
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001022void CodeGenerator::Comparison(Condition cc,
1023 Expression* left,
1024 Expression* right,
1025 bool strict) {
1026 if (left != NULL) LoadAndSpill(left);
1027 if (right != NULL) LoadAndSpill(right);
1028
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001029 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001030 // sp[0] : y
1031 // sp[1] : x
1032 // result : cc register
1033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 // Strict only makes sense for equality comparisons.
1035 ASSERT(!strict || cc == eq);
1036
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001037 JumpTarget exit;
1038 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001039 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1040 if (cc == gt || cc == le) {
1041 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001042 frame_->EmitPop(r1);
1043 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001044 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001045 frame_->EmitPop(r0);
1046 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001047 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 __ orr(r2, r0, Operand(r1));
1049 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001050 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001052 // Perform non-smi comparison by stub.
1053 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1054 // We call with 0 args because there are 0 on the stack.
1055 CompareStub stub(cc, strict);
1056 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001057 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001058 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001060 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001061 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 __ cmp(r1, Operand(r0));
1063
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001064 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 cc_reg_ = cc;
1066}
1067
1068
kasper.lund7276f142008-07-30 08:49:36 +00001069class CallFunctionStub: public CodeStub {
1070 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001071 CallFunctionStub(int argc, InLoopFlag in_loop)
1072 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001073
1074 void Generate(MacroAssembler* masm);
1075
1076 private:
1077 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001078 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001079
kasper.lund7276f142008-07-30 08:49:36 +00001080#if defined(DEBUG)
1081 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1082#endif // defined(DEBUG)
1083
1084 Major MajorKey() { return CallFunction; }
1085 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001086 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001087};
1088
1089
mads.s.ager31e71382008-08-13 09:32:07 +00001090// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001091void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001093 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001095 int arg_count = args->length();
1096 for (int i = 0; i < arg_count; i++) {
1097 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001098 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099
kasper.lund7276f142008-07-30 08:49:36 +00001100 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001101 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
kasper.lund7276f142008-07-30 08:49:36 +00001103 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001104 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1105 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001106 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107
1108 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001109 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001110 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111}
1112
1113
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001114void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001115 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 ASSERT(has_cc());
1117 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001118 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 cc_reg_ = al;
1120}
1121
1122
ager@chromium.org7c537e22008-10-16 08:43:32 +00001123void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001124 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001125 Comment cmnt(masm_, "[ check stack");
1126 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1127 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1128 // the implicit 8 byte offset that always applies to operations with pc and
1129 // gives a return address 12 bytes down.
1130 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1131 masm_->cmp(sp, Operand(ip));
1132 StackCheckStub stub;
1133 // Call the stub if lower.
1134 masm_->mov(pc,
1135 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1136 RelocInfo::CODE_TARGET),
1137 LeaveCC,
1138 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139}
1140
1141
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001142void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1143#ifdef DEBUG
1144 int original_height = frame_->height();
1145#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001146 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001147 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1148 VisitAndSpill(statements->at(i));
1149 }
1150 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1151}
1152
1153
ager@chromium.org7c537e22008-10-16 08:43:32 +00001154void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001155#ifdef DEBUG
1156 int original_height = frame_->height();
1157#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001158 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001160 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001161 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 VisitStatementsAndSpill(node->statements());
1163 if (node->break_target()->is_linked()) {
1164 node->break_target()->Bind();
1165 }
1166 node->break_target()->Unuse();
1167 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168}
1169
1170
ager@chromium.org7c537e22008-10-16 08:43:32 +00001171void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001172 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001173 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001174 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001175 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001176 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001177 frame_->EmitPush(r0);
1178 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001179 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180}
1181
1182
ager@chromium.org7c537e22008-10-16 08:43:32 +00001183void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001184#ifdef DEBUG
1185 int original_height = frame_->height();
1186#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001187 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 Comment cmnt(masm_, "[ Declaration");
1189 Variable* var = node->proxy()->var();
1190 ASSERT(var != NULL); // must have been resolved
1191 Slot* slot = var->slot();
1192
1193 // If it was not possible to allocate the variable at compile time,
1194 // we need to "declare" it at runtime to make sure it actually
1195 // exists in the local context.
1196 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1197 // Variables with a "LOOKUP" slot were introduced as non-locals
1198 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001199 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001201 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001202 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001203 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 // Declaration nodes are always declared in only two modes.
1205 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1206 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001207 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001208 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209 // Push initial value, if any.
1210 // Note: For variables we must not push an initial value (such as
1211 // 'undefined') because we may have a (legal) redeclaration and we
1212 // must not destroy the current value.
1213 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001214 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001215 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001219 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001223 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 return;
1226 }
1227
1228 ASSERT(!var->is_global());
1229
1230 // If we have a function or a constant, we need to initialize the variable.
1231 Expression* val = NULL;
1232 if (node->mode() == Variable::CONST) {
1233 val = new Literal(Factory::the_hole_value());
1234 } else {
1235 val = node->fun(); // NULL if we don't have a function
1236 }
1237
1238 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001239 {
1240 // Set initial value.
1241 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001242 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001243 target.SetValue(NOT_CONST_INIT);
1244 // The reference is removed from the stack (preserving TOS) when
1245 // it goes out of scope.
1246 }
1247 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001248 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001250 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251}
1252
1253
ager@chromium.org7c537e22008-10-16 08:43:32 +00001254void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001255#ifdef DEBUG
1256 int original_height = frame_->height();
1257#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001258 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001260 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 Expression* expression = node->expression();
1262 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001263 LoadAndSpill(expression);
1264 frame_->Drop();
1265 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266}
1267
1268
ager@chromium.org7c537e22008-10-16 08:43:32 +00001269void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001270#ifdef DEBUG
1271 int original_height = frame_->height();
1272#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001273 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001275 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278}
1279
1280
ager@chromium.org7c537e22008-10-16 08:43:32 +00001281void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001282#ifdef DEBUG
1283 int original_height = frame_->height();
1284#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001285 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001287 // Generate different code depending on which parts of the if statement
1288 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 bool has_then_stm = node->HasThenStatement();
1290 bool has_else_stm = node->HasElseStatement();
1291
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001292 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001294 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001296 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001297 JumpTarget then;
1298 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001300 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1301 &then, &else_, true);
1302 if (frame_ != NULL) {
1303 Branch(false, &else_);
1304 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 if (frame_ != NULL || then.is_linked()) {
1307 then.Bind();
1308 VisitAndSpill(node->then_statement());
1309 }
1310 if (frame_ != NULL) {
1311 exit.Jump();
1312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001314 if (else_.is_linked()) {
1315 else_.Bind();
1316 VisitAndSpill(node->else_statement());
1317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318
1319 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001320 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001322 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001324 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1325 &then, &exit, true);
1326 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)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001340 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1341 &exit, &else_, true);
1342 if (frame_ != NULL) {
1343 Branch(true, &exit);
1344 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001346 if (frame_ != NULL || else_.is_linked()) {
1347 else_.Bind();
1348 VisitAndSpill(node->else_statement());
1349 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350
1351 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001352 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 ASSERT(!has_then_stm && !has_else_stm);
1354 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1356 &exit, &exit, false);
1357 if (frame_ != NULL) {
1358 if (has_cc()) {
1359 cc_reg_ = al;
1360 } else {
1361 frame_->Drop();
1362 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 }
1364 }
1365
1366 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001367 if (exit.is_linked()) {
1368 exit.Bind();
1369 }
1370 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371}
1372
1373
ager@chromium.org7c537e22008-10-16 08:43:32 +00001374void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001375 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001377 CodeForStatementPosition(node);
1378 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379}
1380
1381
ager@chromium.org7c537e22008-10-16 08:43:32 +00001382void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001383 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001385 CodeForStatementPosition(node);
1386 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387}
1388
1389
ager@chromium.org7c537e22008-10-16 08:43:32 +00001390void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001391 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001393
ager@chromium.org4af710e2009-09-15 12:20:11 +00001394 CodeForStatementPosition(node);
1395 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001396 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001397 frame_->EmitPop(r0);
1398 function_return_.Jump();
1399 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001400 // Pop the result from the frame and prepare the frame for
1401 // returning thus making it easier to merge.
1402 frame_->EmitPop(r0);
1403 frame_->PrepareForReturn();
1404
1405 function_return_.Jump();
1406 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407}
1408
1409
ager@chromium.org7c537e22008-10-16 08:43:32 +00001410void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001411#ifdef DEBUG
1412 int original_height = frame_->height();
1413#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001414 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001416 CodeForStatementPosition(node);
1417 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001418 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001420 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001422 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001424 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001425 __ cmp(r0, Operand(cp));
1426 verified_true.Branch(eq);
1427 __ stop("PushContext: r0 is expected to be the same as cp");
1428 verified_true.Bind();
1429#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001431 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001432 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433}
1434
1435
ager@chromium.org7c537e22008-10-16 08:43:32 +00001436void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001437#ifdef DEBUG
1438 int original_height = frame_->height();
1439#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001440 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001442 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // Pop context.
1444 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1445 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001446 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001447 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448}
1449
1450
ager@chromium.org7c537e22008-10-16 08:43:32 +00001451void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452#ifdef DEBUG
1453 int original_height = frame_->height();
1454#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001455 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001457 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001458 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001461
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001462 JumpTarget next_test;
1463 JumpTarget fall_through;
1464 JumpTarget default_entry;
1465 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 ZoneList<CaseClause*>* cases = node->cases();
1467 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001468 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
1470 for (int i = 0; i < length; i++) {
1471 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001473 // Remember the default clause and compile it at the end.
1474 default_clause = clause;
1475 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 }
1477
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001478 Comment cmnt(masm_, "[ Case clause");
1479 // Compile the test.
1480 next_test.Bind();
1481 next_test.Unuse();
1482 // Duplicate TOS.
1483 __ ldr(r0, frame_->Top());
1484 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001485 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001486 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001487
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001488 // Before entering the body from the test, remove the switch value from
1489 // the stack.
1490 frame_->Drop();
1491
1492 // Label the body so that fall through is enabled.
1493 if (i > 0 && cases->at(i - 1)->is_default()) {
1494 default_exit.Bind();
1495 } else {
1496 fall_through.Bind();
1497 fall_through.Unuse();
1498 }
1499 VisitStatementsAndSpill(clause->statements());
1500
1501 // If control flow can fall through from the body, jump to the next body
1502 // or the end of the statement.
1503 if (frame_ != NULL) {
1504 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1505 default_entry.Jump();
1506 } else {
1507 fall_through.Jump();
1508 }
1509 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 }
1511
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001512 // The final "test" removes the switch value.
1513 next_test.Bind();
1514 frame_->Drop();
1515
1516 // If there is a default clause, compile it.
1517 if (default_clause != NULL) {
1518 Comment cmnt(masm_, "[ Default clause");
1519 default_entry.Bind();
1520 VisitStatementsAndSpill(default_clause->statements());
1521 // If control flow can fall out of the default and there is a case after
1522 // it, jup to that case's body.
1523 if (frame_ != NULL && default_exit.is_bound()) {
1524 default_exit.Jump();
1525 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001526 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001528 if (fall_through.is_linked()) {
1529 fall_through.Bind();
1530 }
1531
1532 if (node->break_target()->is_linked()) {
1533 node->break_target()->Bind();
1534 }
1535 node->break_target()->Unuse();
1536 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537}
1538
1539
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001540void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001541#ifdef DEBUG
1542 int original_height = frame_->height();
1543#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001544 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001545 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001547 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001548 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001550 // Label the top of the loop for the backward CFG edge. If the test
1551 // is always true we can use the continue target, and if the test is
1552 // always false there is no need.
1553 ConditionAnalysis info = AnalyzeCondition(node->cond());
1554 switch (info) {
1555 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001556 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001557 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001558 break;
1559 case ALWAYS_FALSE:
1560 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1561 break;
1562 case DONT_KNOW:
1563 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1564 body.Bind();
1565 break;
1566 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001567
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001568 CheckStack(); // TODO(1222600): ignore if body contains calls.
1569 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001570
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001571 // Compile the test.
1572 switch (info) {
1573 case ALWAYS_TRUE:
1574 // If control can fall off the end of the body, jump back to the
1575 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001577 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001578 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001580 case ALWAYS_FALSE:
1581 // If we have a continue in the body, we only have to bind its
1582 // jump target.
1583 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001584 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001585 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001586 break;
1587 case DONT_KNOW:
1588 // We have to compile the test expression if it can be reached by
1589 // control flow falling out of the body or via continue.
1590 if (node->continue_target()->is_linked()) {
1591 node->continue_target()->Bind();
1592 }
1593 if (has_valid_frame()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001594 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1595 &body, node->break_target(), true);
1596 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001597 // A invalid frame here indicates that control did not
1598 // fall out of the test expression.
1599 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 }
1601 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 break;
1603 }
1604
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001605 if (node->break_target()->is_linked()) {
1606 node->break_target()->Bind();
1607 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001608 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1609}
1610
1611
1612void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1613#ifdef DEBUG
1614 int original_height = frame_->height();
1615#endif
1616 VirtualFrame::SpilledScope spilled_scope;
1617 Comment cmnt(masm_, "[ WhileStatement");
1618 CodeForStatementPosition(node);
1619
1620 // If the test is never true and has no side effects there is no need
1621 // to compile the test or body.
1622 ConditionAnalysis info = AnalyzeCondition(node->cond());
1623 if (info == ALWAYS_FALSE) return;
1624
1625 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1626
1627 // Label the top of the loop with the continue target for the backward
1628 // CFG edge.
1629 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1630 node->continue_target()->Bind();
1631
1632 if (info == DONT_KNOW) {
1633 JumpTarget body;
1634 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1635 &body, node->break_target(), true);
1636 if (has_valid_frame()) {
1637 // A NULL frame indicates that control did not fall out of the
1638 // test expression.
1639 Branch(false, node->break_target());
1640 }
1641 if (has_valid_frame() || body.is_linked()) {
1642 body.Bind();
1643 }
1644 }
1645
1646 if (has_valid_frame()) {
1647 CheckStack(); // TODO(1222600): ignore if body contains calls.
1648 VisitAndSpill(node->body());
1649
1650 // If control flow can fall out of the body, jump back to the top.
1651 if (has_valid_frame()) {
1652 node->continue_target()->Jump();
1653 }
1654 }
1655 if (node->break_target()->is_linked()) {
1656 node->break_target()->Bind();
1657 }
1658 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1659}
1660
1661
1662void CodeGenerator::VisitForStatement(ForStatement* node) {
1663#ifdef DEBUG
1664 int original_height = frame_->height();
1665#endif
1666 VirtualFrame::SpilledScope spilled_scope;
1667 Comment cmnt(masm_, "[ ForStatement");
1668 CodeForStatementPosition(node);
1669 if (node->init() != NULL) {
1670 VisitAndSpill(node->init());
1671 }
1672
1673 // If the test is never true there is no need to compile the test or
1674 // body.
1675 ConditionAnalysis info = AnalyzeCondition(node->cond());
1676 if (info == ALWAYS_FALSE) return;
1677
1678 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1679
1680 // If there is no update statement, label the top of the loop with the
1681 // continue target, otherwise with the loop target.
1682 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1683 if (node->next() == NULL) {
1684 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1685 node->continue_target()->Bind();
1686 } else {
1687 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1688 loop.Bind();
1689 }
1690
1691 // If the test is always true, there is no need to compile it.
1692 if (info == DONT_KNOW) {
1693 JumpTarget body;
1694 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1695 &body, node->break_target(), true);
1696 if (has_valid_frame()) {
1697 Branch(false, node->break_target());
1698 }
1699 if (has_valid_frame() || body.is_linked()) {
1700 body.Bind();
1701 }
1702 }
1703
1704 if (has_valid_frame()) {
1705 CheckStack(); // TODO(1222600): ignore if body contains calls.
1706 VisitAndSpill(node->body());
1707
1708 if (node->next() == NULL) {
1709 // If there is no update statement and control flow can fall out
1710 // of the loop, jump directly to the continue label.
1711 if (has_valid_frame()) {
1712 node->continue_target()->Jump();
1713 }
1714 } else {
1715 // If there is an update statement and control flow can reach it
1716 // via falling out of the body of the loop or continuing, we
1717 // compile the update statement.
1718 if (node->continue_target()->is_linked()) {
1719 node->continue_target()->Bind();
1720 }
1721 if (has_valid_frame()) {
1722 // Record source position of the statement as this code which is
1723 // after the code for the body actually belongs to the loop
1724 // statement and not the body.
1725 CodeForStatementPosition(node);
1726 VisitAndSpill(node->next());
1727 loop.Jump();
1728 }
1729 }
1730 }
1731 if (node->break_target()->is_linked()) {
1732 node->break_target()->Bind();
1733 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001734 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735}
1736
1737
ager@chromium.org7c537e22008-10-16 08:43:32 +00001738void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739#ifdef DEBUG
1740 int original_height = frame_->height();
1741#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001742 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001744 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001746 JumpTarget primitive;
1747 JumpTarget jsobject;
1748 JumpTarget fixed_array;
1749 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1750 JumpTarget end_del_check;
1751 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752
1753 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001754 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755
1756 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1757 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001759 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1760 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001761 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001762 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1763 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001764 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765
1766 // Stack layout in body:
1767 // [iteration counter (Smi)]
1768 // [length of array]
1769 // [FixedArray]
1770 // [Map or 0]
1771 // [Object]
1772
1773 // Check if enumerable is already a JSObject
1774 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001775 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001776 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 primitive.Bind();
1780 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001781 Result arg_count(r0);
1782 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001787 frame_->EmitPush(r0); // duplicate the object being enumerated
1788 frame_->EmitPush(r0);
1789 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790
1791 // If we got a Map, we can do a fast modification check.
1792 // Otherwise, we got a FixedArray, and we have to do a slow check.
1793 __ mov(r2, Operand(r0));
1794 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001795 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1796 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798
1799 // Get enum cache
1800 __ mov(r1, Operand(r0));
1801 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1802 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1803 __ ldr(r2,
1804 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1805
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001806 frame_->EmitPush(r0); // map
1807 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001808 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001810 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001811 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001812 frame_->EmitPush(r0);
1813 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001815 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 frame_->EmitPush(r1); // insert 0 in place of Map
1818 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001821 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001824 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826
1827 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001829 // sp[0] : index
1830 // sp[1] : array/enum cache length
1831 // sp[2] : array or enum cache
1832 // sp[3] : 0 or map
1833 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001834 // Grab the current frame's height for the break and continue
1835 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001836 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1837 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1840 __ ldr(r1, frame_->ElementAt(1)); // load the length
1841 __ cmp(r0, Operand(r1)); // compare to the array length
1842 node->break_target()->Branch(hs);
1843
1844 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001845
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1849 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1850
1851 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 // Check if this (still) matches the map of the enumerable.
1854 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1857 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001858 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859
1860 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1862 frame_->EmitPush(r0);
1863 frame_->EmitPush(r3); // push entry
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001864 Result arg_count_reg(r0);
1865 __ mov(r0, Operand(1));
1866 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2);
1867 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
1869 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001870 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1871 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001872 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 end_del_check.Bind();
1875 // Store the entry in the 'each' expression and take another spin in the
1876 // loop. r3: i'th entry of the enum cache (or string there of)
1877 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 { Reference each(this, node->each());
1879 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001880 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001881 __ ldr(r0, frame_->ElementAt(each.size()));
1882 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001883 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001884 // If the reference was to a slot we rely on the convenient property
1885 // that it doesn't matter whether a value (eg, r3 pushed above) is
1886 // right on top of or right underneath a zero-sized reference.
1887 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001888 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001889 // It's safe to pop the value lying on top of the reference before
1890 // unloading the reference itself (which preserves the top of stack,
1891 // ie, now the topmost value of the non-zero sized reference), since
1892 // we will discard the top of stack after unloading the reference
1893 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001895 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 }
1897 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001898 // Discard the i'th entry pushed above or else the remainder of the
1899 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001900 frame_->Drop();
1901
1902 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 VisitAndSpill(node->body());
1905
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001906 // Next. Reestablish a spilled frame in case we are coming here via
1907 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001909 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910 frame_->EmitPop(r0);
1911 __ add(r0, r0, Operand(Smi::FromInt(1)));
1912 frame_->EmitPush(r0);
1913 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001915 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1916 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001918 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919
1920 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001921 exit.Bind();
1922 node->continue_target()->Unuse();
1923 node->break_target()->Unuse();
1924 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925}
1926
1927
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001928void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929#ifdef DEBUG
1930 int original_height = frame_->height();
1931#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001932 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001933 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001934 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001936 JumpTarget try_block;
1937 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942
1943 // Store the caught exception in the catch variable.
1944 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001945 ASSERT(ref.is_slot());
1946 // Here we make use of the convenient property that it doesn't matter
1947 // whether a value is immediately on top of or underneath a zero-sized
1948 // reference.
1949 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 }
1951
1952 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 VisitStatementsAndSpill(node->catch_block()->statements());
1956 if (frame_ != NULL) {
1957 exit.Jump();
1958 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959
1960
1961 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001962 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1965 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001967 // Shadow the labels for all escapes from the try block, including
1968 // returns. During shadowing, the original label is hidden as the
1969 // LabelShadow and operations on the original actually affect the
1970 // shadowing label.
1971 //
1972 // We should probably try to unify the escaping labels and the return
1973 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001974 int nof_escapes = node->escaping_targets()->length();
1975 List<ShadowTarget*> shadows(1 + nof_escapes);
1976
1977 // Add the shadow target for the function return.
1978 static const int kReturnShadowIndex = 0;
1979 shadows.Add(new ShadowTarget(&function_return_));
1980 bool function_return_was_shadowed = function_return_is_shadowed_;
1981 function_return_is_shadowed_ = true;
1982 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1983
1984 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 }
1988
1989 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
1992 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001993 // After shadowing stops, the original labels are unshadowed and the
1994 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 bool has_unlinks = false;
1996 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 has_unlinks = has_unlinks || shadows[i]->is_linked();
1999 }
2000 function_return_is_shadowed_ = function_return_was_shadowed;
2001
2002 // Get an external reference to the handler address.
2003 ExternalReference handler_address(Top::k_handler_address);
2004
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 // If we can fall off the end of the try block, unlink from try chain.
2006 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002007 // The next handler address is on top of the frame. Unlink from
2008 // the handler list and drop the rest of this handler from the
2009 // frame.
2010 ASSERT(StackHandlerConstants::kNextOffset == 0);
2011 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 __ mov(r3, Operand(handler_address));
2013 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002014 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 if (has_unlinks) {
2016 exit.Jump();
2017 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 }
2019
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002020 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 // jumped to. Deallocate each shadow target.
2022 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002024 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002025 shadows[i]->Bind();
2026 // Because we can be jumping here (to spilled code) from unspilled
2027 // code, we need to reestablish a spilled frame at this block.
2028 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 // Reload sp from the top handler, because some statements that we
2031 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002032 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002034 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002036 ASSERT(StackHandlerConstants::kNextOffset == 0);
2037 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002039 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002041 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2042 frame_->PrepareForReturn();
2043 }
2044 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 }
2046 }
2047
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048 exit.Bind();
2049 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050}
2051
2052
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002053void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054#ifdef DEBUG
2055 int original_height = frame_->height();
2056#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002057 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002058 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060
2061 // State: Used to keep track of reason for entering the finally
2062 // block. Should probably be extended to hold information for
2063 // break/continue from within the try block.
2064 enum { FALLING, THROWING, JUMPING };
2065
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002066 JumpTarget try_block;
2067 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 // In case of thrown exceptions, this is where we continue.
2073 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002074 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
2076 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2080 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002082 // Shadow the labels for all escapes from the try block, including
2083 // returns. Shadowing hides the original label as the LabelShadow and
2084 // operations on the original actually affect the shadowing label.
2085 //
2086 // We should probably try to unify the escaping labels and the return
2087 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 int nof_escapes = node->escaping_targets()->length();
2089 List<ShadowTarget*> shadows(1 + nof_escapes);
2090
2091 // Add the shadow target for the function return.
2092 static const int kReturnShadowIndex = 0;
2093 shadows.Add(new ShadowTarget(&function_return_));
2094 bool function_return_was_shadowed = function_return_is_shadowed_;
2095 function_return_is_shadowed_ = true;
2096 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2097
2098 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002100 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 }
2102
2103 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002104 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002106 // Stop the introduced shadowing and count the number of required unlinks.
2107 // After shadowing stops, the original labels are unshadowed and the
2108 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 shadows[i]->StopShadowing();
2112 if (shadows[i]->is_linked()) nof_unlinks++;
2113 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002114 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 // Get an external reference to the handler address.
2117 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 // If we can fall off the end of the try block, unlink from the try
2120 // chain and set the state on the frame to FALLING.
2121 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002122 // The next handler address is on top of the frame.
2123 ASSERT(StackHandlerConstants::kNextOffset == 0);
2124 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 __ mov(r3, Operand(handler_address));
2126 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002127 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128
2129 // Fake a top of stack value (unneeded when FALLING) and set the
2130 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002131 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132 frame_->EmitPush(r0);
2133 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2134 if (nof_unlinks > 0) {
2135 finally_block.Jump();
2136 }
2137 }
2138
2139 // Generate code to unlink and set the state for the (formerly)
2140 // shadowing targets that have been jumped to.
2141 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143 // If we have come from the shadowed return, the return value is
2144 // in (a non-refcounted reference to) r0. We must preserve it
2145 // until it is pushed.
2146 //
2147 // Because we can be jumping here (to spilled code) from
2148 // unspilled code, we need to reestablish a spilled frame at
2149 // this block.
2150 shadows[i]->Bind();
2151 frame_->SpillAll();
2152
2153 // Reload sp from the top handler, because some statements that
2154 // we break from (eg, for...in) may have left stuff on the
2155 // stack.
2156 __ mov(r3, Operand(handler_address));
2157 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002158 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159
2160 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002161 // handler address is currently on top of the frame.
2162 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002163 frame_->EmitPop(r1);
2164 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002165 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166
2167 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002168 // If this label shadowed the function return, materialize the
2169 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002171 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002173 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 }
2176 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177 if (--nof_unlinks > 0) {
2178 // If this is not the last unlink block, jump around the next.
2179 finally_block.Jump();
2180 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 }
2182 }
2183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002187 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002189
2190 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002191 // and the state - while evaluating the finally block.
2192 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002194 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 if (has_valid_frame()) {
2197 // Restore state and return value or faked TOS.
2198 frame_->EmitPop(r2);
2199 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 }
2201
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002202 // Generate code to jump to the right destination for all used
2203 // formerly shadowing targets. Deallocate each shadow target.
2204 for (int i = 0; i < shadows.length(); i++) {
2205 if (has_valid_frame() && shadows[i]->is_bound()) {
2206 JumpTarget* original = shadows[i]->other_target();
2207 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2208 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002209 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002210 skip.Branch(ne);
2211 frame_->PrepareForReturn();
2212 original->Jump();
2213 skip.Bind();
2214 } else {
2215 original->Branch(eq);
2216 }
2217 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 if (has_valid_frame()) {
2221 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002222 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2224 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 // Rethrow exception.
2227 frame_->EmitPush(r0);
2228 frame_->CallRuntime(Runtime::kReThrow, 1);
2229
2230 // Done.
2231 exit.Bind();
2232 }
2233 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234}
2235
2236
ager@chromium.org7c537e22008-10-16 08:43:32 +00002237void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002238#ifdef DEBUG
2239 int original_height = frame_->height();
2240#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002241 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002244#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002246#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002247 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249}
2250
2251
ager@chromium.org7c537e22008-10-16 08:43:32 +00002252void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002253 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 ASSERT(boilerplate->IsBoilerplate());
2255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 frame_->EmitPush(cp);
ager@chromium.org3811b432009-10-28 14:53:37 +00002258 __ mov(r0, Operand(boilerplate));
2259 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260 frame_->CallRuntime(Runtime::kNewClosure, 2);
2261 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262}
2263
2264
ager@chromium.org7c537e22008-10-16 08:43:32 +00002265void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266#ifdef DEBUG
2267 int original_height = frame_->height();
2268#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002269 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 Comment cmnt(masm_, "[ FunctionLiteral");
2271
2272 // Build the function boilerplate and instantiate it.
2273 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002274 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002275 if (HasStackOverflow()) {
2276 ASSERT(frame_->height() == original_height);
2277 return;
2278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281}
2282
2283
ager@chromium.org7c537e22008-10-16 08:43:32 +00002284void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002286#ifdef DEBUG
2287 int original_height = frame_->height();
2288#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002289 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2291 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293}
2294
2295
ager@chromium.org7c537e22008-10-16 08:43:32 +00002296void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002297#ifdef DEBUG
2298 int original_height = frame_->height();
2299#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002300 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002302 JumpTarget then;
2303 JumpTarget else_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2305 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002306 if (has_valid_frame()) {
2307 Branch(false, &else_);
2308 }
2309 if (has_valid_frame() || then.is_linked()) {
2310 then.Bind();
2311 LoadAndSpill(node->then_expression(), typeof_state());
2312 }
2313 if (else_.is_linked()) {
2314 JumpTarget exit;
2315 if (has_valid_frame()) exit.Jump();
2316 else_.Bind();
2317 LoadAndSpill(node->else_expression(), typeof_state());
2318 if (exit.is_linked()) exit.Bind();
2319 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321}
2322
2323
ager@chromium.org7c537e22008-10-16 08:43:32 +00002324void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002325 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002326 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002327 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002329 JumpTarget slow;
2330 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002331
2332 // Generate fast-case code for variables that might be shadowed by
2333 // eval-introduced variables. Eval is used a lot without
2334 // introducing variables. In those cases, we do not want to
2335 // perform a runtime call for all variables in the scope
2336 // containing the eval.
2337 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2338 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 // If there was no control flow to slow, we can exit early.
2340 if (!slow.is_linked()) {
2341 frame_->EmitPush(r0);
2342 return;
2343 }
2344
2345 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002346
2347 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2348 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2349 // Only generate the fast case for locals that rewrite to slots.
2350 // This rules out argument loads.
2351 if (potential_slot != NULL) {
2352 __ ldr(r0,
2353 ContextSlotOperandCheckExtensions(potential_slot,
2354 r1,
2355 r2,
2356 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002357 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002358 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2359 __ cmp(r0, ip);
2360 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002361 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002363 // ContextSlotOperandCheckExtensions so we have to jump around
2364 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002366 }
2367 }
2368
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369 slow.Bind();
2370 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002371 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002372 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373
ager@chromium.org7c537e22008-10-16 08:43:32 +00002374 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002376 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002377 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002379
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002380 done.Bind();
2381 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382
2383 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002384 // Note: We would like to keep the assert below, but it fires because of
2385 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002386 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002388 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002389 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002391 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002392 // Const slots may contain 'the hole' value (the constant hasn't been
2393 // initialized yet) which needs to be converted into the 'undefined'
2394 // value.
2395 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002396 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002397 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2398 __ cmp(r0, ip);
2399 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002400 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 }
2402 }
2403}
2404
2405
ager@chromium.org381abbb2009-02-25 13:23:22 +00002406void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2407 TypeofState typeof_state,
2408 Register tmp,
2409 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002410 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002411 // Check that no extension objects have been created by calls to
2412 // eval from the current scope to the global scope.
2413 Register context = cp;
2414 Scope* s = scope();
2415 while (s != NULL) {
2416 if (s->num_heap_slots() > 0) {
2417 if (s->calls_eval()) {
2418 // Check that extension is NULL.
2419 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2420 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002421 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002422 }
2423 // Load next context in chain.
2424 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2425 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2426 context = tmp;
2427 }
2428 // If no outer scope calls eval, we do not need to check more
2429 // context extensions.
2430 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2431 s = s->outer_scope();
2432 }
2433
2434 if (s->is_eval_scope()) {
2435 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002436 if (!context.is(tmp)) {
2437 __ mov(tmp, Operand(context));
2438 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002439 __ bind(&next);
2440 // Terminate at global context.
2441 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002442 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2443 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002444 __ b(eq, &fast);
2445 // Check that extension is NULL.
2446 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2447 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002448 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002449 // Load next context in chain.
2450 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2451 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2452 __ b(&next);
2453 __ bind(&fast);
2454 }
2455
2456 // All extension objects were empty and it is safe to use a global
2457 // load IC call.
2458 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2459 // Load the global object.
2460 LoadGlobal();
2461 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002462 Result name(r2);
2463 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002464 // Call IC stub.
2465 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002467 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002468 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002469 }
2470
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 // Drop the global object. The result is in r0.
2472 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002473}
2474
2475
ager@chromium.org7c537e22008-10-16 08:43:32 +00002476void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477#ifdef DEBUG
2478 int original_height = frame_->height();
2479#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002480 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002481 Comment cmnt(masm_, "[ Slot");
2482 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002483 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002484}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485
ager@chromium.org7c537e22008-10-16 08:43:32 +00002486
2487void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488#ifdef DEBUG
2489 int original_height = frame_->height();
2490#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002491 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002492 Comment cmnt(masm_, "[ VariableProxy");
2493
2494 Variable* var = node->var();
2495 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002496 if (expr != NULL) {
2497 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002499 ASSERT(var->is_global());
2500 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002501 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002503 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504}
2505
2506
ager@chromium.org7c537e22008-10-16 08:43:32 +00002507void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002508#ifdef DEBUG
2509 int original_height = frame_->height();
2510#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002511 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002513 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002514 frame_->EmitPush(r0);
2515 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516}
2517
2518
ager@chromium.org7c537e22008-10-16 08:43:32 +00002519void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002520#ifdef DEBUG
2521 int original_height = frame_->height();
2522#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002523 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 Comment cmnt(masm_, "[ RexExp Literal");
2525
2526 // Retrieve the literal array and check the allocated entry.
2527
2528 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002529 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530
2531 // Load the literals array of the function.
2532 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2533
2534 // Load the literal at the ast saved index.
2535 int literal_offset =
2536 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2537 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2538
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002539 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002540 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2541 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543
2544 // If the entry is undefined we call the runtime system to computed
2545 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002546 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002547 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002549 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002551 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002552 frame_->EmitPush(r0);
2553 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002554 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002558 frame_->EmitPush(r2);
2559 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560}
2561
2562
2563// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002564// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565// Each created boilerplate is stored in the JSFunction and they are
2566// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002567class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002569 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 private:
2576 ObjectLiteral* node_;
2577};
2578
2579
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580void DeferredObjectLiteral::Generate() {
2581 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002583 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002586 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002588 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002589 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002591 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002592 __ push(r0);
2593 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2594 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596}
2597
2598
ager@chromium.org7c537e22008-10-16 08:43:32 +00002599void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002600#ifdef DEBUG
2601 int original_height = frame_->height();
2602#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002603 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604 Comment cmnt(masm_, "[ ObjectLiteral");
2605
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002606 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607
2608 // Retrieve the literal array and check the allocated entry.
2609
2610 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002611 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612
2613 // Load the literals array of the function.
2614 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2615
2616 // Load the literal at the ast saved index.
2617 int literal_offset =
2618 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2619 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2620
2621 // Check whether we need to materialize the object literal boilerplate.
2622 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002623 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2624 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002625 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627
2628 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002632 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2633 if (node->depth() == 1) {
2634 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2635 }
2636 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002637 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002638 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639
2640 for (int i = 0; i < node->properties()->length(); i++) {
2641 ObjectLiteral::Property* property = node->properties()->at(i);
2642 Literal* key = property->key();
2643 Expression* value = property->value();
2644 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002645 case ObjectLiteral::Property::CONSTANT:
2646 break;
2647 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2648 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2649 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 case ObjectLiteral::Property::COMPUTED: // fall through
2651 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002652 frame_->EmitPush(r0); // dup the result
2653 LoadAndSpill(key);
2654 LoadAndSpill(value);
2655 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002656 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002657 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 break;
2659 }
2660 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 frame_->EmitPush(r0);
2662 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002663 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 frame_->EmitPush(r0);
2665 LoadAndSpill(value);
2666 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002667 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 break;
2669 }
2670 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002671 frame_->EmitPush(r0);
2672 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002673 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002674 frame_->EmitPush(r0);
2675 LoadAndSpill(value);
2676 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002677 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 break;
2679 }
2680 }
2681 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 ASSERT(frame_->height() == original_height + 1);
2683}
2684
2685
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002686// This deferred code stub will be used for creating the boilerplate
2687// by calling Runtime_CreateArrayLiteralBoilerplate.
2688// Each created boilerplate is stored in the JSFunction and they are
2689// therefore context dependent.
2690class DeferredArrayLiteral: public DeferredCode {
2691 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002692 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002693 set_comment("[ DeferredArrayLiteral");
2694 }
2695
2696 virtual void Generate();
2697
2698 private:
2699 ArrayLiteral* node_;
2700};
2701
2702
2703void DeferredArrayLiteral::Generate() {
2704 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002705
2706 // If the entry is undefined we call the runtime system to computed
2707 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002708 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002709 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002710 // Literal index (1).
2711 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002712 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002713 // Constant properties (2).
2714 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002715 __ push(r0);
2716 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2717 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002718 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002719}
2720
2721
ager@chromium.org7c537e22008-10-16 08:43:32 +00002722void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002723#ifdef DEBUG
2724 int original_height = frame_->height();
2725#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002726 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002728
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002729 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002730
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002731 // Retrieve the literal array and check the allocated entry.
2732
2733 // Load the function of this activation.
2734 __ ldr(r1, frame_->Function());
2735
2736 // Load the literals array of the function.
2737 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2738
2739 // Load the literal at the ast saved index.
2740 int literal_offset =
2741 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2742 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2743
2744 // Check whether we need to materialize the object literal boilerplate.
2745 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002746 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2747 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002748 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002749 deferred->BindExit();
2750
2751 // Push the object literal boilerplate.
2752 frame_->EmitPush(r2);
2753
2754 // Clone the boilerplate object.
2755 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2756 if (node->depth() == 1) {
2757 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2758 }
2759 frame_->CallRuntime(clone_function_id, 1);
2760 frame_->EmitPush(r0); // save the result
2761 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002762
2763 // Generate code to set the elements in the array that are not
2764 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 for (int i = 0; i < node->values()->length(); i++) {
2766 Expression* value = node->values()->at(i);
2767
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002768 // If value is a literal the property value is already set in the
2769 // boilerplate object.
2770 if (value->AsLiteral() != NULL) continue;
2771 // If value is a materialized literal the property value is already set
2772 // in the boilerplate object if it is simple.
2773 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002775 // The property must be set by generated code.
2776 LoadAndSpill(value);
2777 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002779 // Fetch the object literal.
2780 __ ldr(r1, frame_->Top());
2781 // Get the elements array.
2782 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002784 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002785 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786 __ str(r0, FieldMemOperand(r1, offset));
2787
2788 // Update the write barrier for the array address.
2789 __ mov(r3, Operand(offset));
2790 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002792 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793}
2794
2795
ager@chromium.org32912102009-01-16 10:38:43 +00002796void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002797#ifdef DEBUG
2798 int original_height = frame_->height();
2799#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002800 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002801 // Call runtime routine to allocate the catch extension object and
2802 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002803 Comment cmnt(masm_, "[ CatchExtensionObject");
2804 LoadAndSpill(node->key());
2805 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002806 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2807 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002808 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002809}
2810
2811
ager@chromium.org7c537e22008-10-16 08:43:32 +00002812void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002813#ifdef DEBUG
2814 int original_height = frame_->height();
2815#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002816 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002818
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002819 { Reference target(this, node->target());
2820 if (target.is_illegal()) {
2821 // Fool the virtual frame into thinking that we left the assignment's
2822 // value on the frame.
2823 __ mov(r0, Operand(Smi::FromInt(0)));
2824 frame_->EmitPush(r0);
2825 ASSERT(frame_->height() == original_height + 1);
2826 return;
2827 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002829 if (node->op() == Token::ASSIGN ||
2830 node->op() == Token::INIT_VAR ||
2831 node->op() == Token::INIT_CONST) {
2832 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002833
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002835 // +=, *= and similar binary assignments.
2836 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002837 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2838 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002839 bool overwrite =
2840 (node->value()->AsBinaryOperation() != NULL &&
2841 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002842 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002843 SmiOperation(node->binary_op(),
2844 literal->handle(),
2845 false,
2846 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 frame_->EmitPush(r0);
2848
2849 } else {
2850 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002851 GenericBinaryOperation(node->binary_op(),
2852 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002853 frame_->EmitPush(r0);
2854 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002855 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002856
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2858 if (var != NULL &&
2859 (var->mode() == Variable::CONST) &&
2860 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2861 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002864 CodeForSourcePosition(node->position());
2865 if (node->op() == Token::INIT_CONST) {
2866 // Dynamic constant initializations must use the function context
2867 // and initialize the actual constant declared. Dynamic variable
2868 // initializations are simply assignments and use SetValue.
2869 target.SetValue(CONST_INIT);
2870 } else {
2871 target.SetValue(NOT_CONST_INIT);
2872 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873 }
2874 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002875 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002876}
2877
2878
ager@chromium.org7c537e22008-10-16 08:43:32 +00002879void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880#ifdef DEBUG
2881 int original_height = frame_->height();
2882#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002883 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884 Comment cmnt(masm_, "[ Throw");
2885
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002886 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002887 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002888 frame_->CallRuntime(Runtime::kThrow, 1);
2889 frame_->EmitPush(r0);
2890 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891}
2892
2893
ager@chromium.org7c537e22008-10-16 08:43:32 +00002894void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002895#ifdef DEBUG
2896 int original_height = frame_->height();
2897#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002898 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002900
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002901 { Reference property(this, node);
2902 property.GetValueAndSpill(typeof_state());
2903 }
2904 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905}
2906
2907
ager@chromium.org7c537e22008-10-16 08:43:32 +00002908void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002909#ifdef DEBUG
2910 int original_height = frame_->height();
2911#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002912 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002913 Comment cmnt(masm_, "[ Call");
2914
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002915 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916 ZoneList<Expression*>* args = node->arguments();
2917
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 Variable* var = function->AsVariableProxy()->AsVariable();
2921 Property* property = function->AsProperty();
2922
2923 // ------------------------------------------------------------------------
2924 // Fast-case: Use inline caching.
2925 // ---
2926 // According to ECMA-262, section 11.2.3, page 44, the function to call
2927 // must be resolved after the arguments have been evaluated. The IC code
2928 // automatically handles this by loading the arguments before the function
2929 // is resolved in cache misses (this also holds for megamorphic calls).
2930 // ------------------------------------------------------------------------
2931
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002932 if (var != NULL && var->is_possibly_eval()) {
2933 // ----------------------------------
2934 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2935 // ----------------------------------
2936
2937 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2938 // resolve the function we need to call and the receiver of the
2939 // call. Then we call the resolved function using the given
2940 // arguments.
2941 // Prepare stack for call to resolved function.
2942 LoadAndSpill(function);
2943 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2944 frame_->EmitPush(r2); // Slot for receiver
2945 int arg_count = args->length();
2946 for (int i = 0; i < arg_count; i++) {
2947 LoadAndSpill(args->at(i));
2948 }
2949
2950 // Prepare stack for call to ResolvePossiblyDirectEval.
2951 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2952 frame_->EmitPush(r1);
2953 if (arg_count > 0) {
2954 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2955 frame_->EmitPush(r1);
2956 } else {
2957 frame_->EmitPush(r2);
2958 }
2959
2960 // Resolve the call.
2961 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
2962
2963 // Touch up stack with the right values for the function and the receiver.
2964 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
2965 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2966 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
2967 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
2968
2969 // Call the function.
2970 CodeForSourcePosition(node->position());
2971
2972 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2973 CallFunctionStub call_function(arg_count, in_loop);
2974 frame_->CallStub(&call_function, arg_count + 1);
2975
2976 __ ldr(cp, frame_->Context());
2977 // Remove the function from the stack.
2978 frame_->Drop();
2979 frame_->EmitPush(r0);
2980
2981 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 // ----------------------------------
2983 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2984 // ----------------------------------
2985
2986 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002987 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002988 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002989
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002990 // Pass the global object as the receiver and let the IC stub
2991 // patch the stack to use the global proxy as 'this' in the
2992 // invoked function.
2993 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994
2995 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002996 int arg_count = args->length();
2997 for (int i = 0; i < arg_count; i++) {
2998 LoadAndSpill(args->at(i));
2999 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000
3001 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003002 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3003 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003004 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003005 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3006 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003007 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003009 frame_->Drop();
3010 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011
3012 } else if (var != NULL && var->slot() != NULL &&
3013 var->slot()->type() == Slot::LOOKUP) {
3014 // ----------------------------------
3015 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3016 // ----------------------------------
3017
3018 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003020 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003021 frame_->EmitPush(r0);
3022 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 // r0: slot value; r1: receiver
3024
3025 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003026 frame_->EmitPush(r0); // function
3027 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003028
3029 // Call the function.
3030 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003031 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032
3033 } else if (property != NULL) {
3034 // Check if the key is a literal string.
3035 Literal* literal = property->key()->AsLiteral();
3036
3037 if (literal != NULL && literal->handle()->IsSymbol()) {
3038 // ------------------------------------------------------------------
3039 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3040 // ------------------------------------------------------------------
3041
3042 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003043 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003044 frame_->EmitPush(r0);
3045 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 int arg_count = args->length();
3049 for (int i = 0; i < arg_count; i++) {
3050 LoadAndSpill(args->at(i));
3051 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052
3053 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003054 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3055 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003056 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003058 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059
3060 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003063 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064
3065 } else {
3066 // -------------------------------------------
3067 // JavaScript example: 'array[index](1, 2, 3)'
3068 // -------------------------------------------
3069
3070 // Load the function to call from the property through a reference.
3071 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073
3074 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 if (property->is_synthetic()) {
3076 LoadGlobalReceiver(r0);
3077 } else {
3078 __ ldr(r0, frame_->ElementAt(ref.size()));
3079 frame_->EmitPush(r0);
3080 }
3081
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082 // Call the function.
3083 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003084 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 }
3086
3087 } else {
3088 // ----------------------------------
3089 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3090 // ----------------------------------
3091
3092 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003093 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003094
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003095 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003096 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 // Call the function.
3099 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003100 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003102 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103}
3104
3105
ager@chromium.org7c537e22008-10-16 08:43:32 +00003106void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003107#ifdef DEBUG
3108 int original_height = frame_->height();
3109#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003110 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111 Comment cmnt(masm_, "[ CallNew");
3112
3113 // According to ECMA-262, section 11.2.2, page 44, the function
3114 // expression in new calls must be evaluated before the
3115 // arguments. This is different from ordinary calls, where the
3116 // actual function to call is resolved after the arguments have been
3117 // evaluated.
3118
3119 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003120 // receiver. There is no need to use the global proxy here because
3121 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003123 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124
3125 // Push the arguments ("left-to-right") on the stack.
3126 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003127 int arg_count = args->length();
3128 for (int i = 0; i < arg_count; i++) {
3129 LoadAndSpill(args->at(i));
3130 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003131
mads.s.ager31e71382008-08-13 09:32:07 +00003132 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003133 Result num_args(r0);
3134 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003136 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003137 Result function(r1);
3138 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003140 // Call the construct call builtin that handles allocation and
3141 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003142 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003143 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003144 frame_->CallCodeObject(ic,
3145 RelocInfo::CONSTRUCT_CALL,
3146 &num_args,
3147 &function,
3148 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003149
3150 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003151 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003152 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153}
3154
3155
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003156void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3157 VirtualFrame::SpilledScope spilled_scope;
3158 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003159 JumpTarget leave, null, function, non_function_constructor;
3160
3161 // Load the object into r0.
3162 LoadAndSpill(args->at(0));
3163 frame_->EmitPop(r0);
3164
3165 // If the object is a smi, we return null.
3166 __ tst(r0, Operand(kSmiTagMask));
3167 null.Branch(eq);
3168
3169 // Check that the object is a JS object but take special care of JS
3170 // functions to make sure they have 'Function' as their class.
3171 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3172 null.Branch(lt);
3173
3174 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3175 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3176 // LAST_JS_OBJECT_TYPE.
3177 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3178 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3179 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3180 function.Branch(eq);
3181
3182 // Check if the constructor in the map is a function.
3183 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3184 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3185 non_function_constructor.Branch(ne);
3186
3187 // The r0 register now contains the constructor function. Grab the
3188 // instance class name from there.
3189 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3190 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003191 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003192 leave.Jump();
3193
3194 // Functions have class 'Function'.
3195 function.Bind();
3196 __ mov(r0, Operand(Factory::function_class_symbol()));
3197 frame_->EmitPush(r0);
3198 leave.Jump();
3199
3200 // Objects with a non-function constructor have class 'Object'.
3201 non_function_constructor.Bind();
3202 __ mov(r0, Operand(Factory::Object_symbol()));
3203 frame_->EmitPush(r0);
3204 leave.Jump();
3205
3206 // Non-JS objects have class null.
3207 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003208 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003209 frame_->EmitPush(r0);
3210
3211 // All done.
3212 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003213}
3214
3215
ager@chromium.org7c537e22008-10-16 08:43:32 +00003216void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003217 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003219 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003220 LoadAndSpill(args->at(0));
3221 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003222 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003224 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003225 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3226 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003227 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228 // Load the value.
3229 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003230 leave.Bind();
3231 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232}
3233
3234
ager@chromium.org7c537e22008-10-16 08:43:32 +00003235void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003236 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003238 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003239 LoadAndSpill(args->at(0)); // Load the object.
3240 LoadAndSpill(args->at(1)); // Load the value.
3241 frame_->EmitPop(r0); // r0 contains value
3242 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243 // if (object->IsSmi()) return object.
3244 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003245 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003246 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3247 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249 // Store the value.
3250 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3251 // Update the write barrier.
3252 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3253 __ RecordWrite(r1, r2, r3);
3254 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Bind();
3256 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257}
3258
3259
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003261 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003262 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003263 LoadAndSpill(args->at(0));
3264 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003265 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 cc_reg_ = eq;
3267}
3268
3269
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003270void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003271 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003272 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3273 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003274#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003275 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 LoadAndSpill(args->at(1));
3277 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003278 __ CallRuntime(Runtime::kLog, 2);
3279 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003280#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003281 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003283}
3284
3285
ager@chromium.org7c537e22008-10-16 08:43:32 +00003286void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003287 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003288 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003289 LoadAndSpill(args->at(0));
3290 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003291 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003292 cc_reg_ = eq;
3293}
3294
3295
kasper.lund7276f142008-07-30 08:49:36 +00003296// This should generate code that performs a charCodeAt() call or returns
3297// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3298// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003299void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003301 ASSERT(args->length() == 2);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003302 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003303 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003304}
3305
3306
ager@chromium.org7c537e22008-10-16 08:43:32 +00003307void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003308 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003309 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003310 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003311 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003312 // We need the CC bits to come out as not_equal in the case where the
3313 // object is a smi. This can't be done with the usual test opcode so
3314 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003315 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003316 __ and_(r1, r0, Operand(kSmiTagMask));
3317 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003318 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003319 // It is a heap object - get the map. Check if the object is a JS array.
3320 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003321 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322 cc_reg_ = eq;
3323}
3324
3325
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003326void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3327 VirtualFrame::SpilledScope spilled_scope;
3328 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003329
3330 // Get the frame pointer for the calling frame.
3331 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3332
3333 // Skip the arguments adaptor frame if it exists.
3334 Label check_frame_marker;
3335 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003336 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003337 __ b(ne, &check_frame_marker);
3338 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3339
3340 // Check the marker in the calling frame.
3341 __ bind(&check_frame_marker);
3342 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3343 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3344 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003345}
3346
3347
ager@chromium.org7c537e22008-10-16 08:43:32 +00003348void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003349 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 ASSERT(args->length() == 0);
3351
mads.s.ager31e71382008-08-13 09:32:07 +00003352 // Seed the result with the formal parameters count, which will be used
3353 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3355
3356 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003357 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003358 frame_->CallStub(&stub, 0);
3359 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360}
3361
3362
ager@chromium.org7c537e22008-10-16 08:43:32 +00003363void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003364 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365 ASSERT(args->length() == 1);
3366
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003367 // Satisfy contract with ArgumentsAccessStub:
3368 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003369 LoadAndSpill(args->at(0));
3370 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003371 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372
3373 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003374 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003375 frame_->CallStub(&stub, 0);
3376 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377}
3378
3379
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003380void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3381 VirtualFrame::SpilledScope spilled_scope;
3382 ASSERT(args->length() == 0);
3383 __ Call(ExternalReference::random_positive_smi_function().address(),
3384 RelocInfo::RUNTIME_ENTRY);
3385 frame_->EmitPush(r0);
3386}
3387
3388
3389void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3390 VirtualFrame::SpilledScope spilled_scope;
3391 LoadAndSpill(args->at(0));
3392 switch (op) {
3393 case SIN:
3394 frame_->CallRuntime(Runtime::kMath_sin, 1);
3395 break;
3396 case COS:
3397 frame_->CallRuntime(Runtime::kMath_cos, 1);
3398 break;
3399 }
3400 frame_->EmitPush(r0);
3401}
3402
3403
ager@chromium.org7c537e22008-10-16 08:43:32 +00003404void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003405 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003406 ASSERT(args->length() == 2);
3407
3408 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003409 LoadAndSpill(args->at(0));
3410 LoadAndSpill(args->at(1));
3411 frame_->EmitPop(r0);
3412 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003413 __ cmp(r0, Operand(r1));
3414 cc_reg_ = eq;
3415}
3416
3417
ager@chromium.org7c537e22008-10-16 08:43:32 +00003418void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003419#ifdef DEBUG
3420 int original_height = frame_->height();
3421#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003422 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003423 if (CheckForInlineRuntimeCall(node)) {
3424 ASSERT((has_cc() && frame_->height() == original_height) ||
3425 (!has_cc() && frame_->height() == original_height + 1));
3426 return;
3427 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428
3429 ZoneList<Expression*>* args = node->arguments();
3430 Comment cmnt(masm_, "[ CallRuntime");
3431 Runtime::Function* function = node->function();
3432
ager@chromium.org41826e72009-03-30 13:30:57 +00003433 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003434 // Prepare stack for calling JS runtime function.
3435 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003436 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003437 // Push the builtins object found in the current global object.
3438 __ ldr(r1, GlobalObject());
3439 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003441 }
mads.s.ager31e71382008-08-13 09:32:07 +00003442
ager@chromium.org41826e72009-03-30 13:30:57 +00003443 // Push the arguments ("left-to-right").
3444 int arg_count = args->length();
3445 for (int i = 0; i < arg_count; i++) {
3446 LoadAndSpill(args->at(i));
3447 }
mads.s.ager31e71382008-08-13 09:32:07 +00003448
ager@chromium.org41826e72009-03-30 13:30:57 +00003449 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003451 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3452 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003453 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003454 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003455 frame_->Drop();
3456 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003457 } else {
3458 // Call the C runtime function.
3459 frame_->CallRuntime(function, arg_count);
3460 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003461 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003462 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463}
3464
3465
ager@chromium.org7c537e22008-10-16 08:43:32 +00003466void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003467#ifdef DEBUG
3468 int original_height = frame_->height();
3469#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003470 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471 Comment cmnt(masm_, "[ UnaryOperation");
3472
3473 Token::Value op = node->op();
3474
3475 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003476 LoadConditionAndSpill(node->expression(),
3477 NOT_INSIDE_TYPEOF,
3478 false_target(),
3479 true_target(),
3480 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003481 // LoadCondition may (and usually does) leave a test and branch to
3482 // be emitted by the caller. In that case, negate the condition.
3483 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484
3485 } else if (op == Token::DELETE) {
3486 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003487 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003489 LoadAndSpill(property->obj());
3490 LoadAndSpill(property->key());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003491 Result arg_count(r0);
3492 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003493 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494
mads.s.ager31e71382008-08-13 09:32:07 +00003495 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496 Slot* slot = variable->slot();
3497 if (variable->is_global()) {
3498 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003499 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003500 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003501 Result arg_count(r0);
3502 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003503 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504
3505 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3506 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003507 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003508 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003509 frame_->EmitPush(r0);
3510 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003512 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003513 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003514 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003515 Result arg_count(r0);
3516 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003517 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518
mads.s.ager31e71382008-08-13 09:32:07 +00003519 } else {
3520 // Default: Result of deleting non-global, not dynamically
3521 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003522 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003523 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524
3525 } else {
3526 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003527 LoadAndSpill(node->expression()); // may have side-effects
3528 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003529 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003531 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532
3533 } else if (op == Token::TYPEOF) {
3534 // Special case for loading the typeof expression; see comment on
3535 // LoadTypeofExpression().
3536 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 frame_->CallRuntime(Runtime::kTypeof, 1);
3538 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539
3540 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003541 LoadAndSpill(node->expression());
3542 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543 switch (op) {
3544 case Token::NOT:
3545 case Token::DELETE:
3546 case Token::TYPEOF:
3547 UNREACHABLE(); // handled above
3548 break;
3549
3550 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003551 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003552 (node->expression()->AsBinaryOperation() != NULL &&
3553 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003554 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003555 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 break;
3557 }
3558
3559 case Token::BIT_NOT: {
3560 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003561 JumpTarget smi_label;
3562 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003566 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003567 Result arg_count(r0);
3568 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003569 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003571 continue_label.Jump();
3572 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 __ mvn(r0, Operand(r0));
3574 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576 break;
3577 }
3578
3579 case Token::VOID:
3580 // since the stack top is cached in r0, popping and then
3581 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003582 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 break;
3584
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003585 case Token::ADD: {
3586 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003587 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003588 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003589 continue_label.Branch(eq);
3590 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003591 Result arg_count(r0);
3592 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3594 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003595 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003596 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003597 default:
3598 UNREACHABLE();
3599 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003600 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003602 ASSERT(!has_valid_frame() ||
3603 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605}
3606
3607
ager@chromium.org7c537e22008-10-16 08:43:32 +00003608void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003609#ifdef DEBUG
3610 int original_height = frame_->height();
3611#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003612 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 Comment cmnt(masm_, "[ CountOperation");
3614
3615 bool is_postfix = node->is_postfix();
3616 bool is_increment = node->op() == Token::INC;
3617
3618 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3619 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3620
3621 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003622 if (is_postfix) {
3623 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003624 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003625 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626
3627 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003628 if (target.is_illegal()) {
3629 // Spoof the virtual frame to have the expected height (one higher
3630 // than on entry).
3631 if (!is_postfix) {
3632 __ mov(r0, Operand(Smi::FromInt(0)));
3633 frame_->EmitPush(r0);
3634 }
3635 ASSERT(frame_->height() == original_height + 1);
3636 return;
3637 }
3638 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3639 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003641 JumpTarget slow;
3642 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643
3644 // Load the value (1) into register r1.
3645 __ mov(r1, Operand(Smi::FromInt(1)));
3646
3647 // Check for smi operand.
3648 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003649 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003650
3651 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003652 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003653 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003654 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655
3656 // Perform optimistic increment/decrement.
3657 if (is_increment) {
3658 __ add(r0, r0, Operand(r1), SetCC);
3659 } else {
3660 __ sub(r0, r0, Operand(r1), SetCC);
3661 }
3662
3663 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003664 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665
3666 // Revert optimistic increment/decrement.
3667 if (is_increment) {
3668 __ sub(r0, r0, Operand(r1));
3669 } else {
3670 __ add(r0, r0, Operand(r1));
3671 }
3672
3673 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003674 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003675 {
3676 // Convert the operand to a number.
3677 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003678 Result arg_count(r0);
3679 __ mov(r0, Operand(0));
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003680 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3681 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003683 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003684 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685 }
3686
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003687 // Compute the new value.
3688 __ mov(r1, Operand(Smi::FromInt(1)));
3689 frame_->EmitPush(r0);
3690 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003692 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003694 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695 }
3696
3697 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 exit.Bind();
3699 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003700 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 }
3702
3703 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003704 if (is_postfix) frame_->EmitPop(r0);
3705 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003706}
3707
3708
ager@chromium.org7c537e22008-10-16 08:43:32 +00003709void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003710#ifdef DEBUG
3711 int original_height = frame_->height();
3712#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003713 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 Comment cmnt(masm_, "[ BinaryOperation");
3715 Token::Value op = node->op();
3716
3717 // According to ECMA-262 section 11.11, page 58, the binary logical
3718 // operators must yield the result of one of the two expressions
3719 // before any ToBoolean() conversions. This means that the value
3720 // produced by a && or || operator is not necessarily a boolean.
3721
3722 // NOTE: If the left hand side produces a materialized value (not in
3723 // the CC register), we force the right hand side to do the
3724 // same. This is necessary because we may have to branch to the exit
3725 // after evaluating the left hand side (due to the shortcut
3726 // semantics), but the compiler must (statically) know if the result
3727 // of compiling the binary operation is materialized or not.
3728
3729 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003730 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 LoadConditionAndSpill(node->left(),
3732 NOT_INSIDE_TYPEOF,
3733 &is_true,
3734 false_target(),
3735 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003736 if (has_valid_frame() && !has_cc()) {
3737 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003738 JumpTarget pop_and_continue;
3739 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003740
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003741 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003742 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743 // Avoid popping the result if it converts to 'false' using the
3744 // standard ToBoolean() conversion as described in ECMA-262,
3745 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003746 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 Branch(false, &exit);
3748
3749 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003750 pop_and_continue.Bind();
3751 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003752
3753 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003754 is_true.Bind();
3755 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756
3757 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003758 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003759 } else if (has_cc() || is_true.is_linked()) {
3760 // The left-hand side is either (a) partially compiled to
3761 // control flow with a final branch left to emit or (b) fully
3762 // compiled to control flow and possibly true.
3763 if (has_cc()) {
3764 Branch(false, false_target());
3765 }
3766 is_true.Bind();
3767 LoadConditionAndSpill(node->right(),
3768 NOT_INSIDE_TYPEOF,
3769 true_target(),
3770 false_target(),
3771 false);
3772 } else {
3773 // Nothing to do.
3774 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775 }
3776
3777 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003778 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003779 LoadConditionAndSpill(node->left(),
3780 NOT_INSIDE_TYPEOF,
3781 true_target(),
3782 &is_false,
3783 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003784 if (has_valid_frame() && !has_cc()) {
3785 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003786 JumpTarget pop_and_continue;
3787 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003789 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003790 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791 // Avoid popping the result if it converts to 'true' using the
3792 // standard ToBoolean() conversion as described in ECMA-262,
3793 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003794 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 Branch(true, &exit);
3796
3797 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003798 pop_and_continue.Bind();
3799 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800
3801 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003802 is_false.Bind();
3803 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804
3805 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003807 } else if (has_cc() || is_false.is_linked()) {
3808 // The left-hand side is either (a) partially compiled to
3809 // control flow with a final branch left to emit or (b) fully
3810 // compiled to control flow and possibly false.
3811 if (has_cc()) {
3812 Branch(true, true_target());
3813 }
3814 is_false.Bind();
3815 LoadConditionAndSpill(node->right(),
3816 NOT_INSIDE_TYPEOF,
3817 true_target(),
3818 false_target(),
3819 false);
3820 } else {
3821 // Nothing to do.
3822 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003823 }
3824
3825 } else {
3826 // Optimize for the case where (at least) one of the expressions
3827 // is a literal small integer.
3828 Literal* lliteral = node->left()->AsLiteral();
3829 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003830 // NOTE: The code below assumes that the slow cases (calls to runtime)
3831 // never return a constant/immutable object.
3832 bool overwrite_left =
3833 (node->left()->AsBinaryOperation() != NULL &&
3834 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3835 bool overwrite_right =
3836 (node->right()->AsBinaryOperation() != NULL &&
3837 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003838
3839 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003840 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003841 SmiOperation(node->op(),
3842 rliteral->handle(),
3843 false,
3844 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003845
3846 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003847 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003848 SmiOperation(node->op(),
3849 lliteral->handle(),
3850 true,
3851 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852
3853 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003854 OverwriteMode overwrite_mode = NO_OVERWRITE;
3855 if (overwrite_left) {
3856 overwrite_mode = OVERWRITE_LEFT;
3857 } else if (overwrite_right) {
3858 overwrite_mode = OVERWRITE_RIGHT;
3859 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003860 LoadAndSpill(node->left());
3861 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003862 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003866 ASSERT(!has_valid_frame() ||
3867 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869}
3870
3871
ager@chromium.org7c537e22008-10-16 08:43:32 +00003872void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873#ifdef DEBUG
3874 int original_height = frame_->height();
3875#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003876 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003877 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003878 frame_->EmitPush(r0);
3879 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880}
3881
3882
ager@chromium.org7c537e22008-10-16 08:43:32 +00003883void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003884#ifdef DEBUG
3885 int original_height = frame_->height();
3886#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003887 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888 Comment cmnt(masm_, "[ CompareOperation");
3889
3890 // Get the expressions from the node.
3891 Expression* left = node->left();
3892 Expression* right = node->right();
3893 Token::Value op = node->op();
3894
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003895 // To make null checks efficient, we check if either left or right is the
3896 // literal 'null'. If so, we optimize the code by inlining a null check
3897 // instead of calling the (very) general runtime routine for checking
3898 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003900 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003901 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003902 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003903 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3904 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003905 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003906 LoadAndSpill(left_is_null ? right : left);
3907 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003908 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3909 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003910
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003911 // The 'null' value is only equal to 'undefined' if using non-strict
3912 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003913 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003914 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003915
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003916 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3917 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003922
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003923 // It can be an undetectable object.
3924 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3925 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3926 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3927 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 }
3929
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003931 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 return;
3933 }
3934 }
3935
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003936 // To make typeof testing for natives implemented in JavaScript really
3937 // efficient, we generate special code for expressions of the form:
3938 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939 UnaryOperation* operation = left->AsUnaryOperation();
3940 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3941 (operation != NULL && operation->op() == Token::TYPEOF) &&
3942 (right->AsLiteral() != NULL &&
3943 right->AsLiteral()->handle()->IsString())) {
3944 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3945
mads.s.ager31e71382008-08-13 09:32:07 +00003946 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003948 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949
3950 if (check->Equals(Heap::number_symbol())) {
3951 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003952 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003954 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3955 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956 cc_reg_ = eq;
3957
3958 } else if (check->Equals(Heap::string_symbol())) {
3959 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003960 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003961
3962 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3963
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003964 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3966 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3967 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969
3970 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3971 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3972 cc_reg_ = lt;
3973
3974 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003975 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3976 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003977 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003978 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3979 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 cc_reg_ = eq;
3981
3982 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003983 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3984 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003985 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986
3987 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003988 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003990 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3992 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3993 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3994 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3995
3996 cc_reg_ = eq;
3997
3998 } else if (check->Equals(Heap::function_symbol())) {
3999 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004000 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004001 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 cc_reg_ = eq;
4003
4004 } else if (check->Equals(Heap::object_symbol())) {
4005 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004006 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004007
4008 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004009 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4010 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004011 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004013 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4015 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4016 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004017 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018
4019 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4020 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4023 cc_reg_ = le;
4024
4025 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004026 // Uncommon case: typeof testing against a string literal that is
4027 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004028 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004030 ASSERT(!has_valid_frame() ||
4031 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032 return;
4033 }
4034
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 switch (op) {
4036 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004037 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 break;
4039
4040 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004041 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042 break;
4043
4044 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004045 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 break;
4047
4048 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004049 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050 break;
4051
4052 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004053 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054 break;
4055
4056 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004057 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004058 break;
4059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004060 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004061 LoadAndSpill(left);
4062 LoadAndSpill(right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004063 Result arg_count(r0);
4064 __ mov(r0, Operand(1)); // not counting receiver
4065 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2);
4066 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004068 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004071 LoadAndSpill(left);
4072 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004073 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004074 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004075 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004076 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004077 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004078 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004079 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080
4081 default:
4082 UNREACHABLE();
4083 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004084 ASSERT((has_cc() && frame_->height() == original_height) ||
4085 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086}
4087
4088
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089#ifdef DEBUG
4090bool CodeGenerator::HasValidEntryRegisters() { return true; }
4091#endif
4092
4093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004095#define __ ACCESS_MASM(masm)
4096
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004097
ager@chromium.org7c537e22008-10-16 08:43:32 +00004098Handle<String> Reference::GetName() {
4099 ASSERT(type_ == NAMED);
4100 Property* property = expression_->AsProperty();
4101 if (property == NULL) {
4102 // Global variable reference treated as a named property reference.
4103 VariableProxy* proxy = expression_->AsVariableProxy();
4104 ASSERT(proxy->AsVariable() != NULL);
4105 ASSERT(proxy->AsVariable()->is_global());
4106 return proxy->name();
4107 } else {
4108 Literal* raw_name = property->key()->AsLiteral();
4109 ASSERT(raw_name != NULL);
4110 return Handle<String>(String::cast(*raw_name->handle()));
4111 }
4112}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004113
ager@chromium.org7c537e22008-10-16 08:43:32 +00004114
4115void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004117 ASSERT(!is_illegal());
4118 ASSERT(!cgen_->has_cc());
4119 MacroAssembler* masm = cgen_->masm();
4120 Property* property = expression_->AsProperty();
4121 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004122 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004123 }
4124
4125 switch (type_) {
4126 case SLOT: {
4127 Comment cmnt(masm, "[ Load from Slot");
4128 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4129 ASSERT(slot != NULL);
4130 cgen_->LoadFromSlot(slot, typeof_state);
4131 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004132 }
4133
ager@chromium.org7c537e22008-10-16 08:43:32 +00004134 case NAMED: {
4135 // TODO(1241834): Make sure that this it is safe to ignore the
4136 // distinction between expressions in a typeof and not in a typeof. If
4137 // there is a chance that reference errors can be thrown below, we
4138 // must distinguish between the two kinds of loads (typeof expression
4139 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004140 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004141 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004142 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004143 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004144 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4145 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004146 Result name_reg(r2);
4147 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004148 ASSERT(var == NULL || var->is_global());
4149 RelocInfo::Mode rmode = (var == NULL)
4150 ? RelocInfo::CODE_TARGET
4151 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004152 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4153 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004154 break;
4155 }
4156
4157 case KEYED: {
4158 // TODO(1241834): Make sure that this it is safe to ignore the
4159 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004160
4161 // TODO(181): Implement inlined version of array indexing once
4162 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004163 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004164 Comment cmnt(masm, "[ Load from keyed Property");
4165 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004166 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004167 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004168 ASSERT(var == NULL || var->is_global());
4169 RelocInfo::Mode rmode = (var == NULL)
4170 ? RelocInfo::CODE_TARGET
4171 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004172 frame->CallCodeObject(ic, rmode, 0);
4173 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004175 }
4176
4177 default:
4178 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004179 }
4180}
4181
4182
ager@chromium.org7c537e22008-10-16 08:43:32 +00004183void Reference::SetValue(InitState init_state) {
4184 ASSERT(!is_illegal());
4185 ASSERT(!cgen_->has_cc());
4186 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004187 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004188 Property* property = expression_->AsProperty();
4189 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004190 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004191 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004192
ager@chromium.org7c537e22008-10-16 08:43:32 +00004193 switch (type_) {
4194 case SLOT: {
4195 Comment cmnt(masm, "[ Store to Slot");
4196 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4197 ASSERT(slot != NULL);
4198 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004199 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004200
ager@chromium.org7c537e22008-10-16 08:43:32 +00004201 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004202 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004203 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004204 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004205
ager@chromium.org7c537e22008-10-16 08:43:32 +00004206 if (init_state == CONST_INIT) {
4207 // Same as the case for a normal store, but ignores attribute
4208 // (e.g. READ_ONLY) of context slot so that we can initialize
4209 // const properties (introduced via eval("const foo = (some
4210 // expr);")). Also, uses the current function context instead of
4211 // the top context.
4212 //
4213 // Note that we must declare the foo upon entry of eval(), via a
4214 // context slot declaration, but we cannot initialize it at the
4215 // same time, because the const declaration may be at the end of
4216 // the eval code (sigh...) and the const variable may have been
4217 // used before (where its value is 'undefined'). Thus, we can only
4218 // do the initialization when we actually encounter the expression
4219 // and when the expression operands are defined and valid, and
4220 // thus we need the split into 2 operations: declaration of the
4221 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004222 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004223 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004224 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004225 }
4226 // Storing a variable must keep the (new) value on the expression
4227 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004228 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004229
ager@chromium.org7c537e22008-10-16 08:43:32 +00004230 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004231 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004232
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004233 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004234 if (init_state == CONST_INIT) {
4235 ASSERT(slot->var()->mode() == Variable::CONST);
4236 // Only the first const initialization must be executed (the slot
4237 // still contains 'the hole' value). When the assignment is
4238 // executed, the code is identical to a normal store (see below).
4239 Comment cmnt(masm, "[ Init const");
4240 __ ldr(r2, cgen_->SlotOperand(slot, r2));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004241 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
4242 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004245
ager@chromium.org7c537e22008-10-16 08:43:32 +00004246 // We must execute the store. Storing a variable must keep the
4247 // (new) value on the stack. This is necessary for compiling
4248 // assignment expressions.
4249 //
4250 // Note: We will reach here even with slot->var()->mode() ==
4251 // Variable::CONST because of const declarations which will
4252 // initialize consts to 'the hole' value and by doing so, end up
4253 // calling this code. r2 may be loaded with context; used below in
4254 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004255 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004256 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004257 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004258 if (slot->type() == Slot::CONTEXT) {
4259 // Skip write barrier if the written value is a smi.
4260 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004261 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004262 // r2 is loaded with context when calling SlotOperand above.
4263 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4264 __ mov(r3, Operand(offset));
4265 __ RecordWrite(r2, r3, r1);
4266 }
4267 // If we definitely did not jump over the assignment, we do not need
4268 // to bind the exit label. Doing so can defeat peephole
4269 // optimization.
4270 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004271 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004272 }
4273 }
4274 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004275 }
4276
ager@chromium.org7c537e22008-10-16 08:43:32 +00004277 case NAMED: {
4278 Comment cmnt(masm, "[ Store to named Property");
4279 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004280 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004281 Handle<String> name(GetName());
4282
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004283 Result value(r0);
4284 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004285
4286 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004287 Result property_name(r2);
4288 __ mov(r2, Operand(name));
4289 frame->CallCodeObject(ic,
4290 RelocInfo::CODE_TARGET,
4291 &value,
4292 &property_name,
4293 0);
4294 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004295 break;
4296 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004297
ager@chromium.org7c537e22008-10-16 08:43:32 +00004298 case KEYED: {
4299 Comment cmnt(masm, "[ Store to keyed Property");
4300 Property* property = expression_->AsProperty();
4301 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004302 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004303
4304 // Call IC code.
4305 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4306 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004307 Result value(r0);
4308 frame->EmitPop(r0); // value
4309 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4310 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004311 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004312 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004313
4314 default:
4315 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004316 }
4317}
4318
4319
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004320// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4321// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4322// (31 instead of 32).
4323static void CountLeadingZeros(
4324 MacroAssembler* masm,
4325 Register source,
4326 Register scratch,
4327 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004328#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004329 __ clz(zeros, source); // This instruction is only supported after ARM5.
4330#else
4331 __ mov(zeros, Operand(0));
4332 __ mov(scratch, source);
4333 // Top 16.
4334 __ tst(scratch, Operand(0xffff0000));
4335 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4336 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4337 // Top 8.
4338 __ tst(scratch, Operand(0xff000000));
4339 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4340 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4341 // Top 4.
4342 __ tst(scratch, Operand(0xf0000000));
4343 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4344 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4345 // Top 2.
4346 __ tst(scratch, Operand(0xc0000000));
4347 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4348 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4349 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004350 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004351 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4352#endif
4353}
4354
4355
4356// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4357// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4358// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4359// scratch register. Destroys the source register. No GC occurs during this
4360// stub so you don't have to set up the frame.
4361class ConvertToDoubleStub : public CodeStub {
4362 public:
4363 ConvertToDoubleStub(Register result_reg_1,
4364 Register result_reg_2,
4365 Register source_reg,
4366 Register scratch_reg)
4367 : result1_(result_reg_1),
4368 result2_(result_reg_2),
4369 source_(source_reg),
4370 zeros_(scratch_reg) { }
4371
4372 private:
4373 Register result1_;
4374 Register result2_;
4375 Register source_;
4376 Register zeros_;
4377
4378 // Minor key encoding in 16 bits.
4379 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4380 class OpBits: public BitField<Token::Value, 2, 14> {};
4381
4382 Major MajorKey() { return ConvertToDouble; }
4383 int MinorKey() {
4384 // Encode the parameters in a unique 16 bit value.
4385 return result1_.code() +
4386 (result2_.code() << 4) +
4387 (source_.code() << 8) +
4388 (zeros_.code() << 12);
4389 }
4390
4391 void Generate(MacroAssembler* masm);
4392
4393 const char* GetName() { return "ConvertToDoubleStub"; }
4394
4395#ifdef DEBUG
4396 void Print() { PrintF("ConvertToDoubleStub\n"); }
4397#endif
4398};
4399
4400
4401void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4402#ifndef BIG_ENDIAN_FLOATING_POINT
4403 Register exponent = result1_;
4404 Register mantissa = result2_;
4405#else
4406 Register exponent = result2_;
4407 Register mantissa = result1_;
4408#endif
4409 Label not_special;
4410 // Convert from Smi to integer.
4411 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4412 // Move sign bit from source to destination. This works because the sign bit
4413 // in the exponent word of the double has the same position and polarity as
4414 // the 2's complement sign bit in a Smi.
4415 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4416 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4417 // Subtract from 0 if source was negative.
4418 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4419 __ cmp(source_, Operand(1));
4420 __ b(gt, &not_special);
4421
4422 // We have -1, 0 or 1, which we treat specially.
4423 __ cmp(source_, Operand(0));
4424 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4425 static const uint32_t exponent_word_for_1 =
4426 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4427 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4428 // 1, 0 and -1 all have 0 for the second word.
4429 __ mov(mantissa, Operand(0));
4430 __ Ret();
4431
4432 __ bind(&not_special);
4433 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4434 // Gets the wrong answer for 0, but we already checked for that case above.
4435 CountLeadingZeros(masm, source_, mantissa, zeros_);
4436 // Compute exponent and or it into the exponent register.
4437 // We use result2 as a scratch register here.
4438 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4439 __ orr(exponent,
4440 exponent,
4441 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4442 // Shift up the source chopping the top bit off.
4443 __ add(zeros_, zeros_, Operand(1));
4444 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4445 __ mov(source_, Operand(source_, LSL, zeros_));
4446 // Compute lower part of fraction (last 12 bits).
4447 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4448 // And the top (top 20 bits).
4449 __ orr(exponent,
4450 exponent,
4451 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4452 __ Ret();
4453}
4454
4455
4456// This stub can convert a signed int32 to a heap number (double). It does
4457// not work for int32s that are in Smi range! No GC occurs during this stub
4458// so you don't have to set up the frame.
4459class WriteInt32ToHeapNumberStub : public CodeStub {
4460 public:
4461 WriteInt32ToHeapNumberStub(Register the_int,
4462 Register the_heap_number,
4463 Register scratch)
4464 : the_int_(the_int),
4465 the_heap_number_(the_heap_number),
4466 scratch_(scratch) { }
4467
4468 private:
4469 Register the_int_;
4470 Register the_heap_number_;
4471 Register scratch_;
4472
4473 // Minor key encoding in 16 bits.
4474 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4475 class OpBits: public BitField<Token::Value, 2, 14> {};
4476
4477 Major MajorKey() { return WriteInt32ToHeapNumber; }
4478 int MinorKey() {
4479 // Encode the parameters in a unique 16 bit value.
4480 return the_int_.code() +
4481 (the_heap_number_.code() << 4) +
4482 (scratch_.code() << 8);
4483 }
4484
4485 void Generate(MacroAssembler* masm);
4486
4487 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4488
4489#ifdef DEBUG
4490 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4491#endif
4492};
4493
4494
4495// See comment for class.
4496void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4497 Label max_negative_int;
4498 // the_int_ has the answer which is a signed int32 but not a Smi.
4499 // We test for the special value that has a different exponent. This test
4500 // has the neat side effect of setting the flags according to the sign.
4501 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004502 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004503 __ b(eq, &max_negative_int);
4504 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4505 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4506 uint32_t non_smi_exponent =
4507 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4508 __ mov(scratch_, Operand(non_smi_exponent));
4509 // Set the sign bit in scratch_ if the value was negative.
4510 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4511 // Subtract from 0 if the value was negative.
4512 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4513 // We should be masking the implict first digit of the mantissa away here,
4514 // but it just ends up combining harmlessly with the last digit of the
4515 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4516 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4517 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4518 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4519 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4520 __ str(scratch_, FieldMemOperand(the_heap_number_,
4521 HeapNumber::kExponentOffset));
4522 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4523 __ str(scratch_, FieldMemOperand(the_heap_number_,
4524 HeapNumber::kMantissaOffset));
4525 __ Ret();
4526
4527 __ bind(&max_negative_int);
4528 // The max negative int32 is stored as a positive number in the mantissa of
4529 // a double because it uses a sign bit instead of using two's complement.
4530 // The actual mantissa bits stored are all 0 because the implicit most
4531 // significant 1 bit is not stored.
4532 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4533 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4534 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4535 __ mov(ip, Operand(0));
4536 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4537 __ Ret();
4538}
4539
4540
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004541// Handle the case where the lhs and rhs are the same object.
4542// Equality is almost reflexive (everything but NaN), so this is a test
4543// for "identity and not NaN".
4544static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4545 Label* slow,
4546 Condition cc) {
4547 Label not_identical;
4548 __ cmp(r0, Operand(r1));
4549 __ b(ne, &not_identical);
4550
4551 Register exp_mask_reg = r5;
4552 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4553
4554 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4555 // so we do the second best thing - test it ourselves.
4556 Label heap_number, return_equal;
4557 // They are both equal and they are not both Smis so both of them are not
4558 // Smis. If it's not a heap number, then return equal.
4559 if (cc == lt || cc == gt) {
4560 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4561 __ b(ge, slow);
4562 } else {
4563 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4564 __ b(eq, &heap_number);
4565 // Comparing JS objects with <=, >= is complicated.
4566 if (cc != eq) {
4567 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4568 __ b(ge, slow);
4569 }
4570 }
4571 __ bind(&return_equal);
4572 if (cc == lt) {
4573 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4574 } else if (cc == gt) {
4575 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4576 } else {
4577 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4578 }
4579 __ mov(pc, Operand(lr)); // Return.
4580
4581 // For less and greater we don't have to check for NaN since the result of
4582 // x < x is false regardless. For the others here is some code to check
4583 // for NaN.
4584 if (cc != lt && cc != gt) {
4585 __ bind(&heap_number);
4586 // It is a heap number, so return non-equal if it's NaN and equal if it's
4587 // not NaN.
4588 // The representation of NaN values has all exponent bits (52..62) set,
4589 // and not all mantissa bits (0..51) clear.
4590 // Read top bits of double representation (second word of value).
4591 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4592 // Test that exponent bits are all set.
4593 __ and_(r3, r2, Operand(exp_mask_reg));
4594 __ cmp(r3, Operand(exp_mask_reg));
4595 __ b(ne, &return_equal);
4596
4597 // Shift out flag and all exponent bits, retaining only mantissa.
4598 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4599 // Or with all low-bits of mantissa.
4600 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4601 __ orr(r0, r3, Operand(r2), SetCC);
4602 // For equal we already have the right value in r0: Return zero (equal)
4603 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4604 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4605 // if it's a NaN.
4606 if (cc != eq) {
4607 // All-zero means Infinity means equal.
4608 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4609 if (cc == le) {
4610 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4611 } else {
4612 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4613 }
4614 }
4615 __ mov(pc, Operand(lr)); // Return.
4616 }
4617 // No fall through here.
4618
4619 __ bind(&not_identical);
4620}
4621
4622
4623// See comment at call site.
4624static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4625 Label* rhs_not_nan,
4626 Label* slow,
4627 bool strict) {
4628 Label lhs_is_smi;
4629 __ tst(r0, Operand(kSmiTagMask));
4630 __ b(eq, &lhs_is_smi);
4631
4632 // Rhs is a Smi. Check whether the non-smi is a heap number.
4633 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4634 if (strict) {
4635 // If lhs was not a number and rhs was a Smi then strict equality cannot
4636 // succeed. Return non-equal (r0 is already not zero)
4637 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4638 } else {
4639 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4640 // the runtime.
4641 __ b(ne, slow);
4642 }
4643
4644 // Rhs is a smi, lhs is a number.
4645 __ push(lr);
4646 __ mov(r7, Operand(r1));
4647 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4648 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4649 // r3 and r2 are rhs as double.
4650 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4651 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4652 // We now have both loaded as doubles but we can skip the lhs nan check
4653 // since it's a Smi.
4654 __ pop(lr);
4655 __ jmp(rhs_not_nan);
4656
4657 __ bind(&lhs_is_smi);
4658 // Lhs is a Smi. Check whether the non-smi is a heap number.
4659 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4660 if (strict) {
4661 // If lhs was not a number and rhs was a Smi then strict equality cannot
4662 // succeed. Return non-equal.
4663 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4664 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4665 } else {
4666 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4667 // the runtime.
4668 __ b(ne, slow);
4669 }
4670
4671 // Lhs is a smi, rhs is a number.
4672 // r0 is Smi and r1 is heap number.
4673 __ push(lr);
4674 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4675 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4676 __ mov(r7, Operand(r0));
4677 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4678 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4679 __ pop(lr);
4680 // Fall through to both_loaded_as_doubles.
4681}
4682
4683
4684void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4685 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4686 Register lhs_exponent = exp_first ? r0 : r1;
4687 Register rhs_exponent = exp_first ? r2 : r3;
4688 Register lhs_mantissa = exp_first ? r1 : r0;
4689 Register rhs_mantissa = exp_first ? r3 : r2;
4690 Label one_is_nan, neither_is_nan;
4691
4692 Register exp_mask_reg = r5;
4693
4694 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4695 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4696 __ cmp(r4, Operand(exp_mask_reg));
4697 __ b(ne, rhs_not_nan);
4698 __ mov(r4,
4699 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4700 SetCC);
4701 __ b(ne, &one_is_nan);
4702 __ cmp(rhs_mantissa, Operand(0));
4703 __ b(ne, &one_is_nan);
4704
4705 __ bind(rhs_not_nan);
4706 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4707 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4708 __ cmp(r4, Operand(exp_mask_reg));
4709 __ b(ne, &neither_is_nan);
4710 __ mov(r4,
4711 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4712 SetCC);
4713 __ b(ne, &one_is_nan);
4714 __ cmp(lhs_mantissa, Operand(0));
4715 __ b(eq, &neither_is_nan);
4716
4717 __ bind(&one_is_nan);
4718 // NaN comparisons always fail.
4719 // Load whatever we need in r0 to make the comparison fail.
4720 if (cc == lt || cc == le) {
4721 __ mov(r0, Operand(GREATER));
4722 } else {
4723 __ mov(r0, Operand(LESS));
4724 }
4725 __ mov(pc, Operand(lr)); // Return.
4726
4727 __ bind(&neither_is_nan);
4728}
4729
4730
4731// See comment at call site.
4732static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4733 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4734 Register lhs_exponent = exp_first ? r0 : r1;
4735 Register rhs_exponent = exp_first ? r2 : r3;
4736 Register lhs_mantissa = exp_first ? r1 : r0;
4737 Register rhs_mantissa = exp_first ? r3 : r2;
4738
4739 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4740 if (cc == eq) {
4741 // Doubles are not equal unless they have the same bit pattern.
4742 // Exception: 0 and -0.
4743 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4744 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4745 // Return non-zero if the numbers are unequal.
4746 __ mov(pc, Operand(lr), LeaveCC, ne);
4747
4748 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4749 // If exponents are equal then return 0.
4750 __ mov(pc, Operand(lr), LeaveCC, eq);
4751
4752 // Exponents are unequal. The only way we can return that the numbers
4753 // are equal is if one is -0 and the other is 0. We already dealt
4754 // with the case where both are -0 or both are 0.
4755 // We start by seeing if the mantissas (that are equal) or the bottom
4756 // 31 bits of the rhs exponent are non-zero. If so we return not
4757 // equal.
4758 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4759 __ mov(r0, Operand(r4), LeaveCC, ne);
4760 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4761 // Now they are equal if and only if the lhs exponent is zero in its
4762 // low 31 bits.
4763 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4764 __ mov(pc, Operand(lr));
4765 } else {
4766 // Call a native function to do a comparison between two non-NaNs.
4767 // Call C routine that may not cause GC or other trouble.
4768 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4769 __ Jump(r5); // Tail call.
4770 }
4771}
4772
4773
4774// See comment at call site.
4775static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4776 // If either operand is a JSObject or an oddball value, then they are
4777 // not equal since their pointers are different.
4778 // There is no test for undetectability in strict equality.
4779 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4780 Label first_non_object;
4781 // Get the type of the first operand into r2 and compare it with
4782 // FIRST_JS_OBJECT_TYPE.
4783 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4784 __ b(lt, &first_non_object);
4785
4786 // Return non-zero (r0 is not zero)
4787 Label return_not_equal;
4788 __ bind(&return_not_equal);
4789 __ mov(pc, Operand(lr)); // Return.
4790
4791 __ bind(&first_non_object);
4792 // Check for oddballs: true, false, null, undefined.
4793 __ cmp(r2, Operand(ODDBALL_TYPE));
4794 __ b(eq, &return_not_equal);
4795
4796 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4797 __ b(ge, &return_not_equal);
4798
4799 // Check for oddballs: true, false, null, undefined.
4800 __ cmp(r3, Operand(ODDBALL_TYPE));
4801 __ b(eq, &return_not_equal);
4802}
4803
4804
4805// See comment at call site.
4806static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4807 Label* both_loaded_as_doubles,
4808 Label* not_heap_numbers,
4809 Label* slow) {
4810 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4811 __ b(ne, not_heap_numbers);
4812 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4813 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4814
4815 // Both are heap numbers. Load them up then jump to the code we have
4816 // for that.
4817 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4818 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4819 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4820 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4821 __ jmp(both_loaded_as_doubles);
4822}
4823
4824
4825// Fast negative check for symbol-to-symbol equality.
4826static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4827 // r2 is object type of r0.
4828 __ tst(r2, Operand(kIsNotStringMask));
4829 __ b(ne, slow);
4830 __ tst(r2, Operand(kIsSymbolMask));
4831 __ b(eq, slow);
4832 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4833 __ b(ge, slow);
4834 __ tst(r3, Operand(kIsSymbolMask));
4835 __ b(eq, slow);
4836
4837 // Both are symbols. We already checked they weren't the same pointer
4838 // so they are not equal.
4839 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4840 __ mov(pc, Operand(lr)); // Return.
4841}
4842
4843
4844// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4845// positive or negative to indicate the result of the comparison.
4846void CompareStub::Generate(MacroAssembler* masm) {
4847 Label slow; // Call builtin.
4848 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4849
4850 // NOTICE! This code is only reached after a smi-fast-case check, so
4851 // it is certain that at least one operand isn't a smi.
4852
4853 // Handle the case where the objects are identical. Either returns the answer
4854 // or goes to slow. Only falls through if the objects were not identical.
4855 EmitIdenticalObjectComparison(masm, &slow, cc_);
4856
4857 // If either is a Smi (we know that not both are), then they can only
4858 // be strictly equal if the other is a HeapNumber.
4859 ASSERT_EQ(0, kSmiTag);
4860 ASSERT_EQ(0, Smi::FromInt(0));
4861 __ and_(r2, r0, Operand(r1));
4862 __ tst(r2, Operand(kSmiTagMask));
4863 __ b(ne, &not_smis);
4864 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4865 // 1) Return the answer.
4866 // 2) Go to slow.
4867 // 3) Fall through to both_loaded_as_doubles.
4868 // 4) Jump to rhs_not_nan.
4869 // In cases 3 and 4 we have found out we were dealing with a number-number
4870 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4871 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4872
4873 __ bind(&both_loaded_as_doubles);
4874 // r0, r1, r2, r3 are the double representations of the left hand side
4875 // and the right hand side.
4876
4877 // Checks for NaN in the doubles we have loaded. Can return the answer or
4878 // fall through if neither is a NaN. Also binds rhs_not_nan.
4879 EmitNanCheck(masm, &rhs_not_nan, cc_);
4880
4881 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4882 // answer. Never falls through.
4883 EmitTwoNonNanDoubleComparison(masm, cc_);
4884
4885 __ bind(&not_smis);
4886 // At this point we know we are dealing with two different objects,
4887 // and neither of them is a Smi. The objects are in r0 and r1.
4888 if (strict_) {
4889 // This returns non-equal for some object types, or falls through if it
4890 // was not lucky.
4891 EmitStrictTwoHeapObjectCompare(masm);
4892 }
4893
4894 Label check_for_symbols;
4895 // Check for heap-number-heap-number comparison. Can jump to slow case,
4896 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4897 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4898 // In this case r2 will contain the type of r0.
4899 EmitCheckForTwoHeapNumbers(masm,
4900 &both_loaded_as_doubles,
4901 &check_for_symbols,
4902 &slow);
4903
4904 __ bind(&check_for_symbols);
4905 if (cc_ == eq) {
4906 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4907 // of r0 on entry.
4908 EmitCheckForSymbols(masm, &slow);
4909 }
4910
4911 __ bind(&slow);
4912 __ push(lr);
4913 __ push(r1);
4914 __ push(r0);
4915 // Figure out which native to call and setup the arguments.
4916 Builtins::JavaScript native;
4917 int arg_count = 1; // Not counting receiver.
4918 if (cc_ == eq) {
4919 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4920 } else {
4921 native = Builtins::COMPARE;
4922 int ncr; // NaN compare result
4923 if (cc_ == lt || cc_ == le) {
4924 ncr = GREATER;
4925 } else {
4926 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4927 ncr = LESS;
4928 }
4929 arg_count++;
4930 __ mov(r0, Operand(Smi::FromInt(ncr)));
4931 __ push(r0);
4932 }
4933
4934 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4935 // tagged as a small integer.
4936 __ mov(r0, Operand(arg_count));
4937 __ InvokeBuiltin(native, CALL_JS);
4938 __ cmp(r0, Operand(0));
4939 __ pop(pc);
4940}
4941
4942
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004943// Allocates a heap number or jumps to the label if the young space is full and
4944// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004945static void AllocateHeapNumber(
4946 MacroAssembler* masm,
4947 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004948 Register result, // The tagged address of the new heap number.
4949 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004950 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004951 // Allocate an object in the heap for the heap number and tag it as a heap
4952 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004953 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
4954 result,
4955 scratch1,
4956 scratch2,
4957 need_gc,
4958 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004959
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004960 // Get heap number map and store it in the allocated object.
4961 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
4962 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004963}
4964
4965
4966// We fall into this code if the operands were Smis, but the result was
4967// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004968// the operands were not both Smi. The operands are in r0 and r1. In order
4969// to call the C-implemented binary fp operation routines we need to end up
4970// with the double precision floating point operands in r0 and r1 (for the
4971// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004972static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4973 Label* not_smi,
4974 const Builtins::JavaScript& builtin,
4975 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004976 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004977 Label slow, slow_pop_2_first, do_the_call;
4978 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4979 // Smi-smi case (overflow).
4980 // Since both are Smis there is no heap number to overwrite, so allocate.
4981 // The new heap number is in r5. r6 and r7 are scratch.
4982 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4983 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004984 __ mov(r7, Operand(r0));
4985 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004986 __ push(lr);
4987 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4988 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4989 __ mov(r7, Operand(r1));
4990 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4991 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4992 __ pop(lr);
4993 __ jmp(&do_the_call); // Tail call. No return.
4994
4995 // We jump to here if something goes wrong (one param is not a number of any
4996 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004997 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004998 __ push(r1);
4999 __ push(r0);
5000 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005001 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005002
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005003 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005004 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005005 if (mode == NO_OVERWRITE) {
5006 // In the case where there is no chance of an overwritable float we may as
5007 // well do the allocation immediately while r0 and r1 are untouched.
5008 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5009 }
5010
5011 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005012 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005013 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5014 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005015 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005016 if (mode == OVERWRITE_RIGHT) {
5017 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5018 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005019 // Calling convention says that second double is in r2 and r3.
5020 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005021 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5022 __ jmp(&finished_loading_r0);
5023 __ bind(&r0_is_smi);
5024 if (mode == OVERWRITE_RIGHT) {
5025 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005026 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005027 }
5028 // Write Smi from r0 to r3 and r2 in double format.
5029 __ mov(r7, Operand(r0));
5030 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5031 __ push(lr);
5032 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5033 __ pop(lr);
5034 __ bind(&finished_loading_r0);
5035
5036 // Move r1 to a double in r0-r1.
5037 __ tst(r1, Operand(kSmiTagMask));
5038 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5039 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5040 __ b(ne, &slow);
5041 if (mode == OVERWRITE_LEFT) {
5042 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005043 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005044 // Calling convention says that first double is in r0 and r1.
5045 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005046 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5047 __ jmp(&finished_loading_r1);
5048 __ bind(&r1_is_smi);
5049 if (mode == OVERWRITE_LEFT) {
5050 // We can't overwrite a Smi so get address of new heap number into r5.
5051 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5052 }
5053 // Write Smi from r1 to r1 and r0 in double format.
5054 __ mov(r7, Operand(r1));
5055 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5056 __ push(lr);
5057 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5058 __ pop(lr);
5059 __ bind(&finished_loading_r1);
5060
5061 __ bind(&do_the_call);
5062 // r0: Left value (least significant part of mantissa).
5063 // r1: Left value (sign, exponent, top of mantissa).
5064 // r2: Right value (least significant part of mantissa).
5065 // r3: Right value (sign, exponent, top of mantissa).
5066 // r5: Address of heap number for result.
5067 __ push(lr); // For later.
5068 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005069 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005070 // Call C routine that may not cause GC or other trouble.
5071 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005072 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005073 __ pop(r4); // Address of heap number.
5074 __ cmp(r4, Operand(Smi::FromInt(0)));
5075 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005076 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005077#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005078 // Double returned in fp coprocessor register 0 and 1, encoded as register
5079 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5080 // substract the tag from r4.
5081 __ sub(r5, r4, Operand(kHeapObjectTag));
5082 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5083#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005084 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005085 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005086 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005087#endif
5088 __ mov(r0, Operand(r4));
5089 // And we are done.
5090 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005091}
5092
5093
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005094// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005095// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005096// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5097// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005098// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5099// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005100static void GetInt32(MacroAssembler* masm,
5101 Register source,
5102 Register dest,
5103 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005104 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005105 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005106 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005107 // Get exponent word.
5108 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5109 // Get exponent alone in scratch2.
5110 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005111 // Load dest with zero. We use this either for the final shift or
5112 // for the answer.
5113 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005114 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005115 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5116 // the exponent that we are fastest at and also the highest exponent we can
5117 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005118 const uint32_t non_smi_exponent =
5119 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5120 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005121 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5122 __ b(eq, &right_exponent);
5123 // If the exponent is higher than that then go to slow case. This catches
5124 // numbers that don't fit in a signed int32, infinities and NaNs.
5125 __ b(gt, slow);
5126
5127 // We know the exponent is smaller than 30 (biased). If it is less than
5128 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5129 // it rounds to zero.
5130 const uint32_t zero_exponent =
5131 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5132 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5133 // Dest already has a Smi zero.
5134 __ b(lt, &done);
5135 // We have a shifted exponent between 0 and 30 in scratch2.
5136 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5137 // We now have the exponent in dest. Subtract from 30 to get
5138 // how much to shift down.
5139 __ rsb(dest, dest, Operand(30));
5140
5141 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005142 // Get the top bits of the mantissa.
5143 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5144 // Put back the implicit 1.
5145 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5146 // Shift up the mantissa bits to take up the space the exponent used to take.
5147 // We just orred in the implicit bit so that took care of one and we want to
5148 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5149 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5150 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5151 // Put sign in zero flag.
5152 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005153 // Get the second half of the double. For some exponents we don't actually
5154 // need this because the bits get shifted out again, but it's probably slower
5155 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005156 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5157 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005158 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5159 // Move down according to the exponent.
5160 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005161 // Fix sign if sign bit was set.
5162 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005163 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005164}
5165
5166
5167// For bitwise ops where the inputs are not both Smis we here try to determine
5168// whether both inputs are either Smis or at least heap numbers that can be
5169// represented by a 32 bit signed value. We truncate towards zero as required
5170// by the ES spec. If this is the case we do the bitwise op and see if the
5171// result is a Smi. If so, great, otherwise we try to find a heap number to
5172// write the answer into (either by allocating or by overwriting).
5173// On entry the operands are in r0 and r1. On exit the answer is in r0.
5174void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5175 Label slow, result_not_a_smi;
5176 Label r0_is_smi, r1_is_smi;
5177 Label done_checking_r0, done_checking_r1;
5178
5179 __ tst(r1, Operand(kSmiTagMask));
5180 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5181 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5182 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005183 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005184 __ jmp(&done_checking_r1);
5185 __ bind(&r1_is_smi);
5186 __ mov(r3, Operand(r1, ASR, 1));
5187 __ bind(&done_checking_r1);
5188
5189 __ tst(r0, Operand(kSmiTagMask));
5190 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5191 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5192 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005193 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005194 __ jmp(&done_checking_r0);
5195 __ bind(&r0_is_smi);
5196 __ mov(r2, Operand(r0, ASR, 1));
5197 __ bind(&done_checking_r0);
5198
5199 // r0 and r1: Original operands (Smi or heap numbers).
5200 // r2 and r3: Signed int32 operands.
5201 switch (op_) {
5202 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5203 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5204 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5205 case Token::SAR:
5206 // Use only the 5 least significant bits of the shift count.
5207 __ and_(r2, r2, Operand(0x1f));
5208 __ mov(r2, Operand(r3, ASR, r2));
5209 break;
5210 case Token::SHR:
5211 // Use only the 5 least significant bits of the shift count.
5212 __ and_(r2, r2, Operand(0x1f));
5213 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5214 // SHR is special because it is required to produce a positive answer.
5215 // The code below for writing into heap numbers isn't capable of writing
5216 // the register as an unsigned int so we go to slow case if we hit this
5217 // case.
5218 __ b(mi, &slow);
5219 break;
5220 case Token::SHL:
5221 // Use only the 5 least significant bits of the shift count.
5222 __ and_(r2, r2, Operand(0x1f));
5223 __ mov(r2, Operand(r3, LSL, r2));
5224 break;
5225 default: UNREACHABLE();
5226 }
5227 // check that the *signed* result fits in a smi
5228 __ add(r3, r2, Operand(0x40000000), SetCC);
5229 __ b(mi, &result_not_a_smi);
5230 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5231 __ Ret();
5232
5233 Label have_to_allocate, got_a_heap_number;
5234 __ bind(&result_not_a_smi);
5235 switch (mode_) {
5236 case OVERWRITE_RIGHT: {
5237 __ tst(r0, Operand(kSmiTagMask));
5238 __ b(eq, &have_to_allocate);
5239 __ mov(r5, Operand(r0));
5240 break;
5241 }
5242 case OVERWRITE_LEFT: {
5243 __ tst(r1, Operand(kSmiTagMask));
5244 __ b(eq, &have_to_allocate);
5245 __ mov(r5, Operand(r1));
5246 break;
5247 }
5248 case NO_OVERWRITE: {
5249 // Get a new heap number in r5. r6 and r7 are scratch.
5250 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5251 }
5252 default: break;
5253 }
5254 __ bind(&got_a_heap_number);
5255 // r2: Answer as signed int32.
5256 // r5: Heap number to write answer into.
5257
5258 // Nothing can go wrong now, so move the heap number to r0, which is the
5259 // result.
5260 __ mov(r0, Operand(r5));
5261
5262 // Tail call that writes the int32 in r2 to the heap number in r0, using
5263 // r3 as scratch. r0 is preserved and returned.
5264 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5265 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5266
5267 if (mode_ != NO_OVERWRITE) {
5268 __ bind(&have_to_allocate);
5269 // Get a new heap number in r5. r6 and r7 are scratch.
5270 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5271 __ jmp(&got_a_heap_number);
5272 }
5273
5274 // If all else failed then we go to the runtime system.
5275 __ bind(&slow);
5276 __ push(r1); // restore stack
5277 __ push(r0);
5278 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5279 switch (op_) {
5280 case Token::BIT_OR:
5281 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5282 break;
5283 case Token::BIT_AND:
5284 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5285 break;
5286 case Token::BIT_XOR:
5287 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5288 break;
5289 case Token::SAR:
5290 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5291 break;
5292 case Token::SHR:
5293 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5294 break;
5295 case Token::SHL:
5296 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5297 break;
5298 default:
5299 UNREACHABLE();
5300 }
5301}
5302
5303
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005304// Can we multiply by x with max two shifts and an add.
5305// This answers yes to all integers from 2 to 10.
5306static bool IsEasyToMultiplyBy(int x) {
5307 if (x < 2) return false; // Avoid special cases.
5308 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5309 if (IsPowerOf2(x)) return true; // Simple shift.
5310 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5311 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5312 return false;
5313}
5314
5315
5316// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5317// Source and destination may be the same register. This routine does
5318// not set carry and overflow the way a mul instruction would.
5319static void MultiplyByKnownInt(MacroAssembler* masm,
5320 Register source,
5321 Register destination,
5322 int known_int) {
5323 if (IsPowerOf2(known_int)) {
5324 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5325 } else if (PopCountLessThanEqual2(known_int)) {
5326 int first_bit = BitPosition(known_int);
5327 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5328 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5329 if (first_bit != 0) {
5330 __ mov(destination, Operand(destination, LSL, first_bit));
5331 }
5332 } else {
5333 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5334 int the_bit = BitPosition(known_int + 1);
5335 __ rsb(destination, source, Operand(source, LSL, the_bit));
5336 }
5337}
5338
5339
5340// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5341// a register for the cases where it doesn't know a good trick, and may deliver
5342// a result that needs shifting.
5343static void MultiplyByKnownInt2(
5344 MacroAssembler* masm,
5345 Register result,
5346 Register source,
5347 Register known_int_register, // Smi tagged.
5348 int known_int,
5349 int* required_shift) { // Including Smi tag shift
5350 switch (known_int) {
5351 case 3:
5352 __ add(result, source, Operand(source, LSL, 1));
5353 *required_shift = 1;
5354 break;
5355 case 5:
5356 __ add(result, source, Operand(source, LSL, 2));
5357 *required_shift = 1;
5358 break;
5359 case 6:
5360 __ add(result, source, Operand(source, LSL, 1));
5361 *required_shift = 2;
5362 break;
5363 case 7:
5364 __ rsb(result, source, Operand(source, LSL, 3));
5365 *required_shift = 1;
5366 break;
5367 case 9:
5368 __ add(result, source, Operand(source, LSL, 3));
5369 *required_shift = 1;
5370 break;
5371 case 10:
5372 __ add(result, source, Operand(source, LSL, 2));
5373 *required_shift = 2;
5374 break;
5375 default:
5376 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5377 __ mul(result, source, known_int_register);
5378 *required_shift = 0;
5379 }
5380}
5381
5382
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005383void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5384 // r1 : x
5385 // r0 : y
5386 // result : r0
5387
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005388 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5389 // tell us that.
5390 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5391
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005392 switch (op_) {
5393 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005394 Label not_smi;
5395 // Fast path.
5396 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005397 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005398 __ b(ne, &not_smi);
5399 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5400 // Return if no overflow.
5401 __ Ret(vc);
5402 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5403
5404 HandleBinaryOpSlowCases(masm,
5405 &not_smi,
5406 Builtins::ADD,
5407 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005408 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005409 break;
5410 }
5411
5412 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005413 Label not_smi;
5414 // Fast path.
5415 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005416 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005417 __ b(ne, &not_smi);
5418 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5419 // Return if no overflow.
5420 __ Ret(vc);
5421 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5422
5423 HandleBinaryOpSlowCases(masm,
5424 &not_smi,
5425 Builtins::SUB,
5426 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005427 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005428 break;
5429 }
5430
5431 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005432 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005433 ASSERT(kSmiTag == 0); // adjust code below
5434 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005435 __ b(ne, &not_smi);
5436 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005437 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005438 // Do multiplication
5439 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5440 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005441 __ mov(ip, Operand(r3, ASR, 31));
5442 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5443 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005444 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005445 __ tst(r3, Operand(r3));
5446 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005447 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005448 // We need -0 if we were multiplying a negative number with 0 to get 0.
5449 // We know one of them was zero.
5450 __ add(r2, r0, Operand(r1), SetCC);
5451 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5452 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5453 // Slow case. We fall through here if we multiplied a negative number
5454 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005455 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005456
5457 HandleBinaryOpSlowCases(masm,
5458 &not_smi,
5459 Builtins::MUL,
5460 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005461 mode_);
5462 break;
5463 }
5464
5465 case Token::DIV:
5466 case Token::MOD: {
5467 Label not_smi;
5468 if (specialized_on_rhs_) {
5469 Label smi_is_unsuitable;
5470 __ BranchOnNotSmi(r1, &not_smi);
5471 if (IsPowerOf2(constant_rhs_)) {
5472 if (op_ == Token::MOD) {
5473 __ and_(r0,
5474 r1,
5475 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5476 SetCC);
5477 // We now have the answer, but if the input was negative we also
5478 // have the sign bit. Our work is done if the result is
5479 // positive or zero:
5480 __ Ret(pl);
5481 // A mod of a negative left hand side must return a negative number.
5482 // Unfortunately if the answer is 0 then we must return -0. And we
5483 // already optimistically trashed r0 so we may need to restore it.
5484 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5485 // Next two instructions are conditional on the answer being -0.
5486 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5487 __ b(eq, &smi_is_unsuitable);
5488 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5489 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5490 } else {
5491 ASSERT(op_ == Token::DIV);
5492 __ tst(r1,
5493 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5494 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5495 int shift = 0;
5496 int d = constant_rhs_;
5497 while ((d & 1) == 0) {
5498 d >>= 1;
5499 shift++;
5500 }
5501 __ mov(r0, Operand(r1, LSR, shift));
5502 __ bic(r0, r0, Operand(kSmiTagMask));
5503 }
5504 } else {
5505 // Not a power of 2.
5506 __ tst(r1, Operand(0x80000000u));
5507 __ b(ne, &smi_is_unsuitable);
5508 // Find a fixed point reciprocal of the divisor so we can divide by
5509 // multiplying.
5510 double divisor = 1.0 / constant_rhs_;
5511 int shift = 32;
5512 double scale = 4294967296.0; // 1 << 32.
5513 uint32_t mul;
5514 // Maximise the precision of the fixed point reciprocal.
5515 while (true) {
5516 mul = static_cast<uint32_t>(scale * divisor);
5517 if (mul >= 0x7fffffff) break;
5518 scale *= 2.0;
5519 shift++;
5520 }
5521 mul++;
5522 __ mov(r2, Operand(mul));
5523 __ umull(r3, r2, r2, r1);
5524 __ mov(r2, Operand(r2, LSR, shift - 31));
5525 // r2 is r1 / rhs. r2 is not Smi tagged.
5526 // r0 is still the known rhs. r0 is Smi tagged.
5527 // r1 is still the unkown lhs. r1 is Smi tagged.
5528 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5529 // r4 = r2 * r0.
5530 MultiplyByKnownInt2(masm,
5531 r4,
5532 r2,
5533 r0,
5534 constant_rhs_,
5535 &required_r4_shift);
5536 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5537 if (op_ == Token::DIV) {
5538 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5539 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5540 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5541 } else {
5542 ASSERT(op_ == Token::MOD);
5543 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5544 }
5545 }
5546 __ Ret();
5547 __ bind(&smi_is_unsuitable);
5548 } else {
5549 __ jmp(&not_smi);
5550 }
5551 HandleBinaryOpSlowCases(masm,
5552 &not_smi,
5553 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5554 op_,
5555 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005556 break;
5557 }
5558
5559 case Token::BIT_OR:
5560 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005561 case Token::BIT_XOR:
5562 case Token::SAR:
5563 case Token::SHR:
5564 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005565 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005566 ASSERT(kSmiTag == 0); // adjust code below
5567 __ tst(r2, Operand(kSmiTagMask));
5568 __ b(ne, &slow);
5569 switch (op_) {
5570 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5571 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5572 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005573 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005574 // Remove tags from right operand.
5575 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5576 // Use only the 5 least significant bits of the shift count.
5577 __ and_(r2, r2, Operand(0x1f));
5578 __ mov(r0, Operand(r1, ASR, r2));
5579 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005580 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005581 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005582 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005583 // Remove tags from operands. We can't do this on a 31 bit number
5584 // because then the 0s get shifted into bit 30 instead of bit 31.
5585 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5586 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5587 // Use only the 5 least significant bits of the shift count.
5588 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005589 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005590 // Unsigned shift is not allowed to produce a negative number, so
5591 // check the sign bit and the sign bit after Smi tagging.
5592 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005593 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005594 // Smi tag result.
5595 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005596 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005597 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005598 // Remove tags from operands.
5599 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5600 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5601 // Use only the 5 least significant bits of the shift count.
5602 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005603 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005604 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005605 __ add(r2, r3, Operand(0x40000000), SetCC);
5606 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005607 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005608 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005609 default: UNREACHABLE();
5610 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005611 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005612 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005613 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005614 break;
5615 }
5616
5617 default: UNREACHABLE();
5618 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005619 // This code should be unreachable.
5620 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005621}
5622
5623
5624void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005625 // Do tail-call to runtime routine. Runtime routines expect at least one
5626 // argument, so give it a Smi.
5627 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005628 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00005629 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005630
5631 __ StubReturn(1);
5632}
5633
5634
5635void UnarySubStub::Generate(MacroAssembler* masm) {
5636 Label undo;
5637 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005638 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005639
5640 // Enter runtime system if the value is not a smi.
5641 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005642 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005643
5644 // Enter runtime system if the value of the expression is zero
5645 // to make sure that we switch between 0 and -0.
5646 __ cmp(r0, Operand(0));
5647 __ b(eq, &slow);
5648
5649 // The value of the expression is a smi that is not zero. Try
5650 // optimistic subtraction '0 - value'.
5651 __ rsb(r1, r0, Operand(0), SetCC);
5652 __ b(vs, &slow);
5653
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005654 __ mov(r0, Operand(r1)); // Set r0 to result.
5655 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005656
5657 // Enter runtime system.
5658 __ bind(&slow);
5659 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005660 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005661 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5662
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005663 __ bind(&not_smi);
5664 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5665 __ b(ne, &slow);
5666 // r0 is a heap number. Get a new heap number in r1.
5667 if (overwrite_) {
5668 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5669 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5670 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5671 } else {
5672 AllocateHeapNumber(masm, &slow, r1, r2, r3);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005673 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005674 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005675 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005676 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5677 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5678 __ mov(r0, Operand(r1));
5679 }
5680 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005681}
5682
5683
ager@chromium.orga1645e22009-09-09 19:27:10 +00005684int CEntryStub::MinorKey() {
5685 ASSERT(result_size_ <= 2);
5686 // Result returned in r0 or r0+r1 by default.
5687 return 0;
5688}
5689
5690
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005691void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005692 // r0 holds the exception.
5693
5694 // Adjust this code if not the case.
5695 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5696
5697 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005698 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5699 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005700
5701 // Restore the next handler and frame pointer, discard handler state.
5702 ASSERT(StackHandlerConstants::kNextOffset == 0);
5703 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005704 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005705 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5706 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5707
5708 // Before returning we restore the context from the frame pointer if
5709 // not NULL. The frame pointer is NULL in the exception handler of a
5710 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005711 __ cmp(fp, Operand(0));
5712 // Set cp to NULL if fp is NULL.
5713 __ mov(cp, Operand(0), LeaveCC, eq);
5714 // Restore cp otherwise.
5715 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005716#ifdef DEBUG
5717 if (FLAG_debug_code) {
5718 __ mov(lr, Operand(pc));
5719 }
5720#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005721 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005722 __ pop(pc);
5723}
5724
5725
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005726void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
5727 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005728 // Adjust this code if not the case.
5729 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5730
5731 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005732 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005733 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005734
5735 // Unwind the handlers until the ENTRY handler is found.
5736 Label loop, done;
5737 __ bind(&loop);
5738 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005739 const int kStateOffset = StackHandlerConstants::kStateOffset;
5740 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005741 __ cmp(r2, Operand(StackHandler::ENTRY));
5742 __ b(eq, &done);
5743 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005744 const int kNextOffset = StackHandlerConstants::kNextOffset;
5745 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005746 __ jmp(&loop);
5747 __ bind(&done);
5748
5749 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005750 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005751 __ pop(r2);
5752 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005753
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005754 if (type == OUT_OF_MEMORY) {
5755 // Set external caught exception to false.
5756 ExternalReference external_caught(Top::k_external_caught_exception_address);
5757 __ mov(r0, Operand(false));
5758 __ mov(r2, Operand(external_caught));
5759 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005760
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005761 // Set pending exception and r0 to out of memory exception.
5762 Failure* out_of_memory = Failure::OutOfMemoryException();
5763 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5764 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5765 __ str(r0, MemOperand(r2));
5766 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005767
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005768 // Stack layout at this point. See also StackHandlerConstants.
5769 // sp -> state (ENTRY)
5770 // fp
5771 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005772
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005773 // Discard handler state (r2 is not used) and restore frame pointer.
5774 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5775 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5776 // Before returning we restore the context from the frame pointer if
5777 // not NULL. The frame pointer is NULL in the exception handler of a
5778 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005779 __ cmp(fp, Operand(0));
5780 // Set cp to NULL if fp is NULL.
5781 __ mov(cp, Operand(0), LeaveCC, eq);
5782 // Restore cp otherwise.
5783 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005784#ifdef DEBUG
5785 if (FLAG_debug_code) {
5786 __ mov(lr, Operand(pc));
5787 }
5788#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005789 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005790 __ pop(pc);
5791}
5792
5793
5794void CEntryStub::GenerateCore(MacroAssembler* masm,
5795 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005796 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005797 Label* throw_out_of_memory_exception,
ager@chromium.org3811b432009-10-28 14:53:37 +00005798 ExitFrame::Mode mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005799 bool do_gc,
5800 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005801 // r0: result parameter for PerformGC, if any
5802 // r4: number of arguments including receiver (C callee-saved)
5803 // r5: pointer to builtin function (C callee-saved)
5804 // r6: pointer to the first argument (C callee-saved)
5805
5806 if (do_gc) {
5807 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005808 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5809 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005810 }
5811
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005812 ExternalReference scope_depth =
5813 ExternalReference::heap_always_allocate_scope_depth();
5814 if (always_allocate) {
5815 __ mov(r0, Operand(scope_depth));
5816 __ ldr(r1, MemOperand(r0));
5817 __ add(r1, r1, Operand(1));
5818 __ str(r1, MemOperand(r0));
5819 }
5820
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005821 // Call C built-in.
5822 // r0 = argc, r1 = argv
5823 __ mov(r0, Operand(r4));
5824 __ mov(r1, Operand(r6));
5825
5826 // TODO(1242173): To let the GC traverse the return address of the exit
5827 // frames, we need to know where the return address is. Right now,
5828 // we push it on the stack to be able to find it again, but we never
5829 // restore from it in case of changes, which makes it impossible to
5830 // support moving the C entry code stub. This should be fixed, but currently
5831 // this is OK because the CEntryStub gets generated so early in the V8 boot
5832 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005833 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5834 masm->push(lr);
5835 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005836
5837 if (always_allocate) {
5838 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5839 // though (contain the result).
5840 __ mov(r2, Operand(scope_depth));
5841 __ ldr(r3, MemOperand(r2));
5842 __ sub(r3, r3, Operand(1));
5843 __ str(r3, MemOperand(r2));
5844 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005845
5846 // check for failure result
5847 Label failure_returned;
5848 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5849 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5850 __ add(r2, r0, Operand(1));
5851 __ tst(r2, Operand(kFailureTagMask));
5852 __ b(eq, &failure_returned);
5853
5854 // Exit C frame and return.
5855 // r0:r1: result
5856 // sp: stack pointer
5857 // fp: frame pointer
ager@chromium.org3811b432009-10-28 14:53:37 +00005858 __ LeaveExitFrame(mode);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005859
5860 // check if we should retry or throw exception
5861 Label retry;
5862 __ bind(&failure_returned);
5863 ASSERT(Failure::RETRY_AFTER_GC == 0);
5864 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5865 __ b(eq, &retry);
5866
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005867 // Special handling of out of memory exceptions.
5868 Failure* out_of_memory = Failure::OutOfMemoryException();
5869 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5870 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005871
5872 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005873 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005874 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005875 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005876 __ ldr(r0, MemOperand(ip));
5877 __ str(r3, MemOperand(ip));
5878
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005879 // Special handling of termination exceptions which are uncatchable
5880 // by javascript code.
5881 __ cmp(r0, Operand(Factory::termination_exception()));
5882 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005883
5884 // Handle normal exception.
5885 __ jmp(throw_normal_exception);
5886
5887 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5888}
5889
5890
5891void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5892 // Called from JavaScript; parameters are on stack as if calling JS function
5893 // r0: number of arguments including receiver
5894 // r1: pointer to builtin function
5895 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005896 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005897 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005898
5899 // NOTE: Invocations of builtins may return failure objects
5900 // instead of a proper result. The builtin entry handles
5901 // this by performing a garbage collection and retrying the
5902 // builtin once.
5903
ager@chromium.org3811b432009-10-28 14:53:37 +00005904 ExitFrame::Mode mode = is_debug_break
5905 ? ExitFrame::MODE_DEBUG
5906 : ExitFrame::MODE_NORMAL;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005907
5908 // Enter the exit frame that transitions from JavaScript to C++.
ager@chromium.org3811b432009-10-28 14:53:37 +00005909 __ EnterExitFrame(mode);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005910
5911 // r4: number of arguments (C callee-saved)
5912 // r5: pointer to builtin function (C callee-saved)
5913 // r6: pointer to first argument (C callee-saved)
5914
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005915 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005916 Label throw_termination_exception;
5917 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005918
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005919 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005920 GenerateCore(masm,
5921 &throw_normal_exception,
5922 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005923 &throw_out_of_memory_exception,
ager@chromium.org3811b432009-10-28 14:53:37 +00005924 mode,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005925 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005926 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005927
5928 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005929 GenerateCore(masm,
5930 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005931 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005932 &throw_out_of_memory_exception,
ager@chromium.org3811b432009-10-28 14:53:37 +00005933 mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005934 true,
5935 false);
5936
5937 // Do full GC and retry runtime call one final time.
5938 Failure* failure = Failure::InternalError();
5939 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5940 GenerateCore(masm,
5941 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005942 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005943 &throw_out_of_memory_exception,
ager@chromium.org3811b432009-10-28 14:53:37 +00005944 mode,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005945 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005946 true);
5947
5948 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005949 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
5950
5951 __ bind(&throw_termination_exception);
5952 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005953
5954 __ bind(&throw_normal_exception);
5955 GenerateThrowTOS(masm);
5956}
5957
5958
5959void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5960 // r0: code entry
5961 // r1: function
5962 // r2: receiver
5963 // r3: argc
5964 // [sp+0]: argv
5965
5966 Label invoke, exit;
5967
5968 // Called from C, so do not pop argc and args on exit (preserve sp)
5969 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005970 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005971 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5972
5973 // Get address of argv, see stm above.
5974 // r0: code entry
5975 // r1: function
5976 // r2: receiver
5977 // r3: argc
5978 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5979 __ ldr(r4, MemOperand(r4)); // argv
5980
5981 // Push a frame with special values setup to mark it as an entry frame.
5982 // r0: code entry
5983 // r1: function
5984 // r2: receiver
5985 // r3: argc
5986 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005987 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005988 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5989 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005990 __ mov(r6, Operand(Smi::FromInt(marker)));
5991 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5992 __ ldr(r5, MemOperand(r5));
5993 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5994
5995 // Setup frame pointer for the frame to be pushed.
5996 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5997
5998 // Call a faked try-block that does the invoke.
5999 __ bl(&invoke);
6000
6001 // Caught exception: Store result (exception) in the pending
6002 // exception field in the JSEnv and return a failure sentinel.
6003 // Coming in here the fp will be invalid because the PushTryHandler below
6004 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006005 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006006 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006007 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006008 __ b(&exit);
6009
6010 // Invoke: Link this frame into the handler chain.
6011 __ bind(&invoke);
6012 // Must preserve r0-r4, r5-r7 are available.
6013 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006014 // If an exception not caught by another handler occurs, this handler
6015 // returns control to the code after the bl(&invoke) above, which
6016 // restores all kCalleeSaved registers (including cp and fp) to their
6017 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006018
6019 // Clear any pending exceptions.
6020 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6021 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006022 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006023 __ str(r5, MemOperand(ip));
6024
6025 // Invoke the function by calling through JS entry trampoline builtin.
6026 // Notice that we cannot store a reference to the trampoline code directly in
6027 // this stub, because runtime stubs are not traversed when doing GC.
6028
6029 // Expected registers by Builtins::JSEntryTrampoline
6030 // r0: code entry
6031 // r1: function
6032 // r2: receiver
6033 // r3: argc
6034 // r4: argv
6035 if (is_construct) {
6036 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6037 __ mov(ip, Operand(construct_entry));
6038 } else {
6039 ExternalReference entry(Builtins::JSEntryTrampoline);
6040 __ mov(ip, Operand(entry));
6041 }
6042 __ ldr(ip, MemOperand(ip)); // deref address
6043
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006044 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6045 // macro for the add instruction because we don't want the coverage tool
6046 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006047 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006048 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006049
6050 // Unlink this frame from the handler chain. When reading the
6051 // address of the next handler, there is no need to use the address
6052 // displacement since the current stack pointer (sp) points directly
6053 // to the stack handler.
6054 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6055 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6056 __ str(r3, MemOperand(ip));
6057 // No need to restore registers
6058 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6059
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006060
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006061 __ bind(&exit); // r0 holds result
6062 // Restore the top frame descriptors from the stack.
6063 __ pop(r3);
6064 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6065 __ str(r3, MemOperand(ip));
6066
6067 // Reset the stack to the callee saved registers.
6068 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6069
6070 // Restore callee-saved registers and return.
6071#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006072 if (FLAG_debug_code) {
6073 __ mov(lr, Operand(pc));
6074 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006075#endif
6076 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6077}
6078
6079
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006080// This stub performs an instanceof, calling the builtin function if
6081// necessary. Uses r1 for the object, r0 for the function that it may
6082// be an instance of (these are fetched from the stack).
6083void InstanceofStub::Generate(MacroAssembler* masm) {
6084 // Get the object - slow case for smis (we may need to throw an exception
6085 // depending on the rhs).
6086 Label slow, loop, is_instance, is_not_instance;
6087 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6088 __ BranchOnSmi(r0, &slow);
6089
6090 // Check that the left hand is a JS object and put map in r3.
6091 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6092 __ b(lt, &slow);
6093 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6094 __ b(gt, &slow);
6095
6096 // Get the prototype of the function (r4 is result, r2 is scratch).
6097 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6098 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6099
6100 // Check that the function prototype is a JS object.
6101 __ BranchOnSmi(r4, &slow);
6102 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6103 __ b(lt, &slow);
6104 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6105 __ b(gt, &slow);
6106
6107 // Register mapping: r3 is object map and r4 is function prototype.
6108 // Get prototype of object into r2.
6109 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6110
6111 // Loop through the prototype chain looking for the function prototype.
6112 __ bind(&loop);
6113 __ cmp(r2, Operand(r4));
6114 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006115 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6116 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006117 __ b(eq, &is_not_instance);
6118 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6119 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6120 __ jmp(&loop);
6121
6122 __ bind(&is_instance);
6123 __ mov(r0, Operand(Smi::FromInt(0)));
6124 __ pop();
6125 __ pop();
6126 __ mov(pc, Operand(lr)); // Return.
6127
6128 __ bind(&is_not_instance);
6129 __ mov(r0, Operand(Smi::FromInt(1)));
6130 __ pop();
6131 __ pop();
6132 __ mov(pc, Operand(lr)); // Return.
6133
6134 // Slow-case. Tail call builtin.
6135 __ bind(&slow);
6136 __ mov(r0, Operand(1)); // Arg count without receiver.
6137 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6138}
6139
6140
ager@chromium.org7c537e22008-10-16 08:43:32 +00006141void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006142 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006143 Label adaptor;
6144 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6145 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006146 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006147 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006148
ager@chromium.org7c537e22008-10-16 08:43:32 +00006149 // Nothing to do: The formal number of parameters has already been
6150 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006151 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006152
ager@chromium.org7c537e22008-10-16 08:43:32 +00006153 // Arguments adaptor case: Read the arguments length from the
6154 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006156 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006157 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006158}
6159
6160
ager@chromium.org7c537e22008-10-16 08:43:32 +00006161void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6162 // The displacement is the offset of the last parameter (if any)
6163 // relative to the frame pointer.
6164 static const int kDisplacement =
6165 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006166
ager@chromium.org7c537e22008-10-16 08:43:32 +00006167 // Check that the key is a smi.
6168 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006169 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170
ager@chromium.org7c537e22008-10-16 08:43:32 +00006171 // Check if the calling frame is an arguments adaptor frame.
6172 Label adaptor;
6173 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6174 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006175 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006176 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006177
ager@chromium.org7c537e22008-10-16 08:43:32 +00006178 // Check index against formal parameters count limit passed in
6179 // through register eax. Use unsigned comparison to get negative
6180 // check for free.
6181 __ cmp(r1, r0);
6182 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006183
ager@chromium.org7c537e22008-10-16 08:43:32 +00006184 // Read the argument from the stack and return it.
6185 __ sub(r3, r0, r1);
6186 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6187 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006188 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006189
6190 // Arguments adaptor case: Check index against actual arguments
6191 // limit found in the arguments adaptor frame. Use unsigned
6192 // comparison to get negative check for free.
6193 __ bind(&adaptor);
6194 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6195 __ cmp(r1, r0);
6196 __ b(cs, &slow);
6197
6198 // Read the argument from the adaptor frame and return it.
6199 __ sub(r3, r0, r1);
6200 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6201 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006202 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006203
6204 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6205 // by calling the runtime system.
6206 __ bind(&slow);
6207 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006208 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006209}
6210
6211
6212void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6213 // Check if the calling frame is an arguments adaptor frame.
6214 Label runtime;
6215 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6216 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006217 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006218 __ b(ne, &runtime);
6219
6220 // Patch the arguments.length and the parameters pointer.
6221 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6222 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6223 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6224 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6225 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6226
6227 // Do the runtime call to allocate the arguments object.
6228 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006229 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006230}
6231
6232
6233void CallFunctionStub::Generate(MacroAssembler* masm) {
6234 Label slow;
6235 // Get the function to call from the stack.
6236 // function, receiver [, arguments]
6237 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6238
6239 // Check that the function is really a JavaScript function.
6240 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006241 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006242 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006243 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006244 __ b(ne, &slow);
6245
6246 // Fast-case: Invoke the function now.
6247 // r1: pushed function
6248 ParameterCount actual(argc_);
6249 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6250
6251 // Slow-case: Non-function called.
6252 __ bind(&slow);
6253 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006254 __ mov(r2, Operand(0));
6255 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6256 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6257 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006258}
6259
6260
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006261int CompareStub::MinorKey() {
6262 // Encode the two parameters in a unique 16 bit value.
6263 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6264 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6265}
6266
6267
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006268#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006270} } // namespace v8::internal