blob: 147c5e354c55fe4206f4b25dafa6e7f65f1ed669 [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;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 if (FLAG_check_stack) {
1126 Comment cmnt(masm_, "[ check stack");
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001127 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
ager@chromium.org4af710e2009-09-15 12:20:11 +00001128 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1129 // the implicit 8 byte offset that always applies to operations with pc and
1130 // gives a return address 12 bytes down.
1131 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001132 masm_->cmp(sp, Operand(ip));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 StackCheckStub stub;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001134 // Call the stub if lower.
1135 masm_->mov(pc,
1136 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1137 RelocInfo::CODE_TARGET),
1138 LeaveCC,
1139 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 }
1141}
1142
1143
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001144void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1145#ifdef DEBUG
1146 int original_height = frame_->height();
1147#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001148 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001149 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1150 VisitAndSpill(statements->at(i));
1151 }
1152 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1153}
1154
1155
ager@chromium.org7c537e22008-10-16 08:43:32 +00001156void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001157#ifdef DEBUG
1158 int original_height = frame_->height();
1159#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001160 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001163 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001164 VisitStatementsAndSpill(node->statements());
1165 if (node->break_target()->is_linked()) {
1166 node->break_target()->Bind();
1167 }
1168 node->break_target()->Unuse();
1169 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170}
1171
1172
ager@chromium.org7c537e22008-10-16 08:43:32 +00001173void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001174 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001175 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001176 frame_->EmitPush(r0);
1177 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001178 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001179 frame_->EmitPush(r0);
1180 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001181 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182}
1183
1184
ager@chromium.org7c537e22008-10-16 08:43:32 +00001185void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001186#ifdef DEBUG
1187 int original_height = frame_->height();
1188#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001189 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 Comment cmnt(masm_, "[ Declaration");
1191 Variable* var = node->proxy()->var();
1192 ASSERT(var != NULL); // must have been resolved
1193 Slot* slot = var->slot();
1194
1195 // If it was not possible to allocate the variable at compile time,
1196 // we need to "declare" it at runtime to make sure it actually
1197 // exists in the local context.
1198 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1199 // Variables with a "LOOKUP" slot were introduced as non-locals
1200 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001201 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001203 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001204 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001205 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 // Declaration nodes are always declared in only two modes.
1207 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1208 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001209 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001210 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 // Push initial value, if any.
1212 // Note: For variables we must not push an initial value (such as
1213 // 'undefined') because we may have a (legal) redeclaration and we
1214 // must not destroy the current value.
1215 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001216 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001221 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001225 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 return;
1228 }
1229
1230 ASSERT(!var->is_global());
1231
1232 // If we have a function or a constant, we need to initialize the variable.
1233 Expression* val = NULL;
1234 if (node->mode() == Variable::CONST) {
1235 val = new Literal(Factory::the_hole_value());
1236 } else {
1237 val = node->fun(); // NULL if we don't have a function
1238 }
1239
1240 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001241 {
1242 // Set initial value.
1243 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001244 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001245 target.SetValue(NOT_CONST_INIT);
1246 // The reference is removed from the stack (preserving TOS) when
1247 // it goes out of scope.
1248 }
1249 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001250 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001252 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253}
1254
1255
ager@chromium.org7c537e22008-10-16 08:43:32 +00001256void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001257#ifdef DEBUG
1258 int original_height = frame_->height();
1259#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001260 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001262 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Expression* expression = node->expression();
1264 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001265 LoadAndSpill(expression);
1266 frame_->Drop();
1267 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268}
1269
1270
ager@chromium.org7c537e22008-10-16 08:43:32 +00001271void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001272#ifdef DEBUG
1273 int original_height = frame_->height();
1274#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280}
1281
1282
ager@chromium.org7c537e22008-10-16 08:43:32 +00001283void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001284#ifdef DEBUG
1285 int original_height = frame_->height();
1286#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001287 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 // Generate different code depending on which parts of the if statement
1290 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 bool has_then_stm = node->HasThenStatement();
1292 bool has_else_stm = node->HasElseStatement();
1293
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001294 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001296 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001298 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001299 JumpTarget then;
1300 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001302 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1303 &then, &else_, true);
1304 if (frame_ != NULL) {
1305 Branch(false, &else_);
1306 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001308 if (frame_ != NULL || then.is_linked()) {
1309 then.Bind();
1310 VisitAndSpill(node->then_statement());
1311 }
1312 if (frame_ != NULL) {
1313 exit.Jump();
1314 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001316 if (else_.is_linked()) {
1317 else_.Bind();
1318 VisitAndSpill(node->else_statement());
1319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320
1321 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001322 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001324 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001326 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1327 &then, &exit, true);
1328 if (frame_ != NULL) {
1329 Branch(false, &exit);
1330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001332 if (frame_ != NULL || then.is_linked()) {
1333 then.Bind();
1334 VisitAndSpill(node->then_statement());
1335 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336
1337 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001338 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001340 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1343 &exit, &else_, true);
1344 if (frame_ != NULL) {
1345 Branch(true, &exit);
1346 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001348 if (frame_ != NULL || else_.is_linked()) {
1349 else_.Bind();
1350 VisitAndSpill(node->else_statement());
1351 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
1353 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001354 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 ASSERT(!has_then_stm && !has_else_stm);
1356 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001357 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1358 &exit, &exit, false);
1359 if (frame_ != NULL) {
1360 if (has_cc()) {
1361 cc_reg_ = al;
1362 } else {
1363 frame_->Drop();
1364 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 }
1366 }
1367
1368 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001369 if (exit.is_linked()) {
1370 exit.Bind();
1371 }
1372 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373}
1374
1375
ager@chromium.org7c537e22008-10-16 08:43:32 +00001376void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001377 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001379 CodeForStatementPosition(node);
1380 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381}
1382
1383
ager@chromium.org7c537e22008-10-16 08:43:32 +00001384void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001385 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001387 CodeForStatementPosition(node);
1388 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389}
1390
1391
ager@chromium.org7c537e22008-10-16 08:43:32 +00001392void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001393 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001395
ager@chromium.org4af710e2009-09-15 12:20:11 +00001396 CodeForStatementPosition(node);
1397 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001399 frame_->EmitPop(r0);
1400 function_return_.Jump();
1401 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001402 // Pop the result from the frame and prepare the frame for
1403 // returning thus making it easier to merge.
1404 frame_->EmitPop(r0);
1405 frame_->PrepareForReturn();
1406
1407 function_return_.Jump();
1408 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409}
1410
1411
ager@chromium.org7c537e22008-10-16 08:43:32 +00001412void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001413#ifdef DEBUG
1414 int original_height = frame_->height();
1415#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001416 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001418 CodeForStatementPosition(node);
1419 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001420 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001422 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001423 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001424 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001425#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001426 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001427 __ cmp(r0, Operand(cp));
1428 verified_true.Branch(eq);
1429 __ stop("PushContext: r0 is expected to be the same as cp");
1430 verified_true.Bind();
1431#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001433 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435}
1436
1437
ager@chromium.org7c537e22008-10-16 08:43:32 +00001438void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001439#ifdef DEBUG
1440 int original_height = frame_->height();
1441#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001442 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001444 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445 // Pop context.
1446 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1447 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001448 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001449 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450}
1451
1452
ager@chromium.org7c537e22008-10-16 08:43:32 +00001453void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001454#ifdef DEBUG
1455 int original_height = frame_->height();
1456#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001457 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001460 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001462 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001463
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001464 JumpTarget next_test;
1465 JumpTarget fall_through;
1466 JumpTarget default_entry;
1467 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 ZoneList<CaseClause*>* cases = node->cases();
1469 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471
1472 for (int i = 0; i < length; i++) {
1473 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 // Remember the default clause and compile it at the end.
1476 default_clause = clause;
1477 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 }
1479
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001480 Comment cmnt(masm_, "[ Case clause");
1481 // Compile the test.
1482 next_test.Bind();
1483 next_test.Unuse();
1484 // Duplicate TOS.
1485 __ ldr(r0, frame_->Top());
1486 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001487 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001488 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001489
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001490 // Before entering the body from the test, remove the switch value from
1491 // the stack.
1492 frame_->Drop();
1493
1494 // Label the body so that fall through is enabled.
1495 if (i > 0 && cases->at(i - 1)->is_default()) {
1496 default_exit.Bind();
1497 } else {
1498 fall_through.Bind();
1499 fall_through.Unuse();
1500 }
1501 VisitStatementsAndSpill(clause->statements());
1502
1503 // If control flow can fall through from the body, jump to the next body
1504 // or the end of the statement.
1505 if (frame_ != NULL) {
1506 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1507 default_entry.Jump();
1508 } else {
1509 fall_through.Jump();
1510 }
1511 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 }
1513
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001514 // The final "test" removes the switch value.
1515 next_test.Bind();
1516 frame_->Drop();
1517
1518 // If there is a default clause, compile it.
1519 if (default_clause != NULL) {
1520 Comment cmnt(masm_, "[ Default clause");
1521 default_entry.Bind();
1522 VisitStatementsAndSpill(default_clause->statements());
1523 // If control flow can fall out of the default and there is a case after
1524 // it, jup to that case's body.
1525 if (frame_ != NULL && default_exit.is_bound()) {
1526 default_exit.Jump();
1527 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001528 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001530 if (fall_through.is_linked()) {
1531 fall_through.Bind();
1532 }
1533
1534 if (node->break_target()->is_linked()) {
1535 node->break_target()->Bind();
1536 }
1537 node->break_target()->Unuse();
1538 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539}
1540
1541
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001542void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543#ifdef DEBUG
1544 int original_height = frame_->height();
1545#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001546 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001547 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001548 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001549 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001550 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001552 // Label the top of the loop for the backward CFG edge. If the test
1553 // is always true we can use the continue target, and if the test is
1554 // always false there is no need.
1555 ConditionAnalysis info = AnalyzeCondition(node->cond());
1556 switch (info) {
1557 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001558 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001559 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001560 break;
1561 case ALWAYS_FALSE:
1562 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1563 break;
1564 case DONT_KNOW:
1565 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1566 body.Bind();
1567 break;
1568 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001569
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001570 CheckStack(); // TODO(1222600): ignore if body contains calls.
1571 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001572
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001573 // Compile the test.
1574 switch (info) {
1575 case ALWAYS_TRUE:
1576 // If control can fall off the end of the body, jump back to the
1577 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001578 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001579 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001580 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001582 case ALWAYS_FALSE:
1583 // If we have a continue in the body, we only have to bind its
1584 // jump target.
1585 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001587 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001588 break;
1589 case DONT_KNOW:
1590 // We have to compile the test expression if it can be reached by
1591 // control flow falling out of the body or via continue.
1592 if (node->continue_target()->is_linked()) {
1593 node->continue_target()->Bind();
1594 }
1595 if (has_valid_frame()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001596 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1597 &body, node->break_target(), true);
1598 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001599 // A invalid frame here indicates that control did not
1600 // fall out of the test expression.
1601 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001602 }
1603 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 break;
1605 }
1606
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001607 if (node->break_target()->is_linked()) {
1608 node->break_target()->Bind();
1609 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001610 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1611}
1612
1613
1614void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1615#ifdef DEBUG
1616 int original_height = frame_->height();
1617#endif
1618 VirtualFrame::SpilledScope spilled_scope;
1619 Comment cmnt(masm_, "[ WhileStatement");
1620 CodeForStatementPosition(node);
1621
1622 // If the test is never true and has no side effects there is no need
1623 // to compile the test or body.
1624 ConditionAnalysis info = AnalyzeCondition(node->cond());
1625 if (info == ALWAYS_FALSE) return;
1626
1627 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1628
1629 // Label the top of the loop with the continue target for the backward
1630 // CFG edge.
1631 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1632 node->continue_target()->Bind();
1633
1634 if (info == DONT_KNOW) {
1635 JumpTarget body;
1636 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1637 &body, node->break_target(), true);
1638 if (has_valid_frame()) {
1639 // A NULL frame indicates that control did not fall out of the
1640 // test expression.
1641 Branch(false, node->break_target());
1642 }
1643 if (has_valid_frame() || body.is_linked()) {
1644 body.Bind();
1645 }
1646 }
1647
1648 if (has_valid_frame()) {
1649 CheckStack(); // TODO(1222600): ignore if body contains calls.
1650 VisitAndSpill(node->body());
1651
1652 // If control flow can fall out of the body, jump back to the top.
1653 if (has_valid_frame()) {
1654 node->continue_target()->Jump();
1655 }
1656 }
1657 if (node->break_target()->is_linked()) {
1658 node->break_target()->Bind();
1659 }
1660 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1661}
1662
1663
1664void CodeGenerator::VisitForStatement(ForStatement* node) {
1665#ifdef DEBUG
1666 int original_height = frame_->height();
1667#endif
1668 VirtualFrame::SpilledScope spilled_scope;
1669 Comment cmnt(masm_, "[ ForStatement");
1670 CodeForStatementPosition(node);
1671 if (node->init() != NULL) {
1672 VisitAndSpill(node->init());
1673 }
1674
1675 // If the test is never true there is no need to compile the test or
1676 // body.
1677 ConditionAnalysis info = AnalyzeCondition(node->cond());
1678 if (info == ALWAYS_FALSE) return;
1679
1680 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1681
1682 // If there is no update statement, label the top of the loop with the
1683 // continue target, otherwise with the loop target.
1684 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1685 if (node->next() == NULL) {
1686 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1687 node->continue_target()->Bind();
1688 } else {
1689 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1690 loop.Bind();
1691 }
1692
1693 // If the test is always true, there is no need to compile it.
1694 if (info == DONT_KNOW) {
1695 JumpTarget body;
1696 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1697 &body, node->break_target(), true);
1698 if (has_valid_frame()) {
1699 Branch(false, node->break_target());
1700 }
1701 if (has_valid_frame() || body.is_linked()) {
1702 body.Bind();
1703 }
1704 }
1705
1706 if (has_valid_frame()) {
1707 CheckStack(); // TODO(1222600): ignore if body contains calls.
1708 VisitAndSpill(node->body());
1709
1710 if (node->next() == NULL) {
1711 // If there is no update statement and control flow can fall out
1712 // of the loop, jump directly to the continue label.
1713 if (has_valid_frame()) {
1714 node->continue_target()->Jump();
1715 }
1716 } else {
1717 // If there is an update statement and control flow can reach it
1718 // via falling out of the body of the loop or continuing, we
1719 // compile the update statement.
1720 if (node->continue_target()->is_linked()) {
1721 node->continue_target()->Bind();
1722 }
1723 if (has_valid_frame()) {
1724 // Record source position of the statement as this code which is
1725 // after the code for the body actually belongs to the loop
1726 // statement and not the body.
1727 CodeForStatementPosition(node);
1728 VisitAndSpill(node->next());
1729 loop.Jump();
1730 }
1731 }
1732 }
1733 if (node->break_target()->is_linked()) {
1734 node->break_target()->Bind();
1735 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001736 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737}
1738
1739
ager@chromium.org7c537e22008-10-16 08:43:32 +00001740void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001741#ifdef DEBUG
1742 int original_height = frame_->height();
1743#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001744 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001748 JumpTarget primitive;
1749 JumpTarget jsobject;
1750 JumpTarget fixed_array;
1751 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1752 JumpTarget end_del_check;
1753 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754
1755 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001756 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757
1758 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1759 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001760 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001761 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1762 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001763 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001764 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1765 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001766 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767
1768 // Stack layout in body:
1769 // [iteration counter (Smi)]
1770 // [length of array]
1771 // [FixedArray]
1772 // [Map or 0]
1773 // [Object]
1774
1775 // Check if enumerable is already a JSObject
1776 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001778 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 primitive.Bind();
1782 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001783 Result arg_count(r0);
1784 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001787 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789 frame_->EmitPush(r0); // duplicate the object being enumerated
1790 frame_->EmitPush(r0);
1791 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
1793 // If we got a Map, we can do a fast modification check.
1794 // Otherwise, we got a FixedArray, and we have to do a slow check.
1795 __ mov(r2, Operand(r0));
1796 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001797 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1798 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001799 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800
1801 // Get enum cache
1802 __ mov(r1, Operand(r0));
1803 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1804 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1805 __ ldr(r2,
1806 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1807
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001808 frame_->EmitPush(r0); // map
1809 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001810 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001812 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001813 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814 frame_->EmitPush(r0);
1815 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 frame_->EmitPush(r1); // insert 0 in place of Map
1820 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821
1822 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001823 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001826 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001827 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828
1829 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001831 // sp[0] : index
1832 // sp[1] : array/enum cache length
1833 // sp[2] : array or enum cache
1834 // sp[3] : 0 or map
1835 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001836 // Grab the current frame's height for the break and continue
1837 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001838 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1839 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1842 __ ldr(r1, frame_->ElementAt(1)); // load the length
1843 __ cmp(r0, Operand(r1)); // compare to the array length
1844 node->break_target()->Branch(hs);
1845
1846 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001849 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1851 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1852
1853 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 // Check if this (still) matches the map of the enumerable.
1856 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001857 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1859 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861
1862 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1864 frame_->EmitPush(r0);
1865 frame_->EmitPush(r3); // push entry
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001866 Result arg_count_reg(r0);
1867 __ mov(r0, Operand(1));
1868 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2);
1869 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870
1871 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001872 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1873 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 end_del_check.Bind();
1877 // Store the entry in the 'each' expression and take another spin in the
1878 // loop. r3: i'th entry of the enum cache (or string there of)
1879 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 { Reference each(this, node->each());
1881 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001882 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 __ ldr(r0, frame_->ElementAt(each.size()));
1884 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001885 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001886 // If the reference was to a slot we rely on the convenient property
1887 // that it doesn't matter whether a value (eg, r3 pushed above) is
1888 // right on top of or right underneath a zero-sized reference.
1889 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001890 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001891 // It's safe to pop the value lying on top of the reference before
1892 // unloading the reference itself (which preserves the top of stack,
1893 // ie, now the topmost value of the non-zero sized reference), since
1894 // we will discard the top of stack after unloading the reference
1895 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001896 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001897 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 }
1899 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001900 // Discard the i'th entry pushed above or else the remainder of the
1901 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 frame_->Drop();
1903
1904 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 VisitAndSpill(node->body());
1907
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001908 // Next. Reestablish a spilled frame in case we are coming here via
1909 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001911 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 frame_->EmitPop(r0);
1913 __ add(r0, r0, Operand(Smi::FromInt(1)));
1914 frame_->EmitPush(r0);
1915 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001917 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1918 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001919 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001920 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921
1922 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 exit.Bind();
1924 node->continue_target()->Unuse();
1925 node->break_target()->Unuse();
1926 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927}
1928
1929
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001930void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001931#ifdef DEBUG
1932 int original_height = frame_->height();
1933#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001934 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001935 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001936 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001938 JumpTarget try_block;
1939 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001943 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944
1945 // Store the caught exception in the catch variable.
1946 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001947 ASSERT(ref.is_slot());
1948 // Here we make use of the convenient property that it doesn't matter
1949 // whether a value is immediately on top of or underneath a zero-sized
1950 // reference.
1951 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 }
1953
1954 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001957 VisitStatementsAndSpill(node->catch_block()->statements());
1958 if (frame_ != NULL) {
1959 exit.Jump();
1960 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961
1962
1963 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1967 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001969 // Shadow the labels for all escapes from the try block, including
1970 // returns. During shadowing, the original label is hidden as the
1971 // LabelShadow and operations on the original actually affect the
1972 // shadowing label.
1973 //
1974 // We should probably try to unify the escaping labels and the return
1975 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001976 int nof_escapes = node->escaping_targets()->length();
1977 List<ShadowTarget*> shadows(1 + nof_escapes);
1978
1979 // Add the shadow target for the function return.
1980 static const int kReturnShadowIndex = 0;
1981 shadows.Add(new ShadowTarget(&function_return_));
1982 bool function_return_was_shadowed = function_return_is_shadowed_;
1983 function_return_is_shadowed_ = true;
1984 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1985
1986 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001988 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 }
1990
1991 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
1994 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001995 // After shadowing stops, the original labels are unshadowed and the
1996 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 bool has_unlinks = false;
1998 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 has_unlinks = has_unlinks || shadows[i]->is_linked();
2001 }
2002 function_return_is_shadowed_ = function_return_was_shadowed;
2003
2004 // Get an external reference to the handler address.
2005 ExternalReference handler_address(Top::k_handler_address);
2006
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002007 // If we can fall off the end of the try block, unlink from try chain.
2008 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002009 // The next handler address is on top of the frame. Unlink from
2010 // the handler list and drop the rest of this handler from the
2011 // frame.
2012 ASSERT(StackHandlerConstants::kNextOffset == 0);
2013 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002014 __ mov(r3, Operand(handler_address));
2015 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002016 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002017 if (has_unlinks) {
2018 exit.Jump();
2019 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 }
2021
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002022 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023 // jumped to. Deallocate each shadow target.
2024 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002026 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002027 shadows[i]->Bind();
2028 // Because we can be jumping here (to spilled code) from unspilled
2029 // code, we need to reestablish a spilled frame at this block.
2030 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 // Reload sp from the top handler, because some statements that we
2033 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002036 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002038 ASSERT(StackHandlerConstants::kNextOffset == 0);
2039 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002041 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2044 frame_->PrepareForReturn();
2045 }
2046 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 }
2048 }
2049
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 exit.Bind();
2051 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052}
2053
2054
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002055void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056#ifdef DEBUG
2057 int original_height = frame_->height();
2058#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002059 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002060 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062
2063 // State: Used to keep track of reason for entering the finally
2064 // block. Should probably be extended to hold information for
2065 // break/continue from within the try block.
2066 enum { FALLING, THROWING, JUMPING };
2067
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002068 JumpTarget try_block;
2069 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 // In case of thrown exceptions, this is where we continue.
2075 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002076 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077
2078 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2082 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002084 // Shadow the labels for all escapes from the try block, including
2085 // returns. Shadowing hides the original label as the LabelShadow and
2086 // operations on the original actually affect the shadowing label.
2087 //
2088 // We should probably try to unify the escaping labels and the return
2089 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 int nof_escapes = node->escaping_targets()->length();
2091 List<ShadowTarget*> shadows(1 + nof_escapes);
2092
2093 // Add the shadow target for the function return.
2094 static const int kReturnShadowIndex = 0;
2095 shadows.Add(new ShadowTarget(&function_return_));
2096 bool function_return_was_shadowed = function_return_is_shadowed_;
2097 function_return_is_shadowed_ = true;
2098 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2099
2100 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002102 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 }
2104
2105 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002106 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002108 // Stop the introduced shadowing and count the number of required unlinks.
2109 // After shadowing stops, the original labels are unshadowed and the
2110 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 shadows[i]->StopShadowing();
2114 if (shadows[i]->is_linked()) nof_unlinks++;
2115 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 // Get an external reference to the handler address.
2119 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 // If we can fall off the end of the try block, unlink from the try
2122 // chain and set the state on the frame to FALLING.
2123 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002124 // The next handler address is on top of the frame.
2125 ASSERT(StackHandlerConstants::kNextOffset == 0);
2126 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 __ mov(r3, Operand(handler_address));
2128 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002129 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130
2131 // Fake a top of stack value (unneeded when FALLING) and set the
2132 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002133 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002134 frame_->EmitPush(r0);
2135 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2136 if (nof_unlinks > 0) {
2137 finally_block.Jump();
2138 }
2139 }
2140
2141 // Generate code to unlink and set the state for the (formerly)
2142 // shadowing targets that have been jumped to.
2143 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 // If we have come from the shadowed return, the return value is
2146 // in (a non-refcounted reference to) r0. We must preserve it
2147 // until it is pushed.
2148 //
2149 // Because we can be jumping here (to spilled code) from
2150 // unspilled code, we need to reestablish a spilled frame at
2151 // this block.
2152 shadows[i]->Bind();
2153 frame_->SpillAll();
2154
2155 // Reload sp from the top handler, because some statements that
2156 // we break from (eg, for...in) may have left stuff on the
2157 // stack.
2158 __ mov(r3, Operand(handler_address));
2159 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002160 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161
2162 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002163 // handler address is currently on top of the frame.
2164 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165 frame_->EmitPop(r1);
2166 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002167 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168
2169 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002170 // If this label shadowed the function return, materialize the
2171 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002173 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002175 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 }
2178 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 if (--nof_unlinks > 0) {
2180 // If this is not the last unlink block, jump around the next.
2181 finally_block.Jump();
2182 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 }
2184 }
2185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002187 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002189 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002190 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002191
2192 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002193 // and the state - while evaluating the finally block.
2194 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002198 if (has_valid_frame()) {
2199 // Restore state and return value or faked TOS.
2200 frame_->EmitPop(r2);
2201 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 }
2203
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002204 // Generate code to jump to the right destination for all used
2205 // formerly shadowing targets. Deallocate each shadow target.
2206 for (int i = 0; i < shadows.length(); i++) {
2207 if (has_valid_frame() && shadows[i]->is_bound()) {
2208 JumpTarget* original = shadows[i]->other_target();
2209 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2210 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002211 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002212 skip.Branch(ne);
2213 frame_->PrepareForReturn();
2214 original->Jump();
2215 skip.Bind();
2216 } else {
2217 original->Branch(eq);
2218 }
2219 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002222 if (has_valid_frame()) {
2223 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002224 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2226 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002228 // Rethrow exception.
2229 frame_->EmitPush(r0);
2230 frame_->CallRuntime(Runtime::kReThrow, 1);
2231
2232 // Done.
2233 exit.Bind();
2234 }
2235 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236}
2237
2238
ager@chromium.org7c537e22008-10-16 08:43:32 +00002239void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002240#ifdef DEBUG
2241 int original_height = frame_->height();
2242#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002243 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002246#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002247 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002248#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002249 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251}
2252
2253
ager@chromium.org7c537e22008-10-16 08:43:32 +00002254void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002255 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 ASSERT(boilerplate->IsBoilerplate());
2257
2258 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002259 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261
2262 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002263 frame_->EmitPush(cp);
2264 frame_->CallRuntime(Runtime::kNewClosure, 2);
2265 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266}
2267
2268
ager@chromium.org7c537e22008-10-16 08:43:32 +00002269void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270#ifdef DEBUG
2271 int original_height = frame_->height();
2272#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002273 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 Comment cmnt(masm_, "[ FunctionLiteral");
2275
2276 // Build the function boilerplate and instantiate it.
2277 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002278 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002279 if (HasStackOverflow()) {
2280 ASSERT(frame_->height() == original_height);
2281 return;
2282 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002284 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285}
2286
2287
ager@chromium.org7c537e22008-10-16 08:43:32 +00002288void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290#ifdef DEBUG
2291 int original_height = frame_->height();
2292#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002293 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2295 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002296 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297}
2298
2299
ager@chromium.org7c537e22008-10-16 08:43:32 +00002300void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301#ifdef DEBUG
2302 int original_height = frame_->height();
2303#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002304 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002306 JumpTarget then;
2307 JumpTarget else_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002308 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2309 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002310 if (has_valid_frame()) {
2311 Branch(false, &else_);
2312 }
2313 if (has_valid_frame() || then.is_linked()) {
2314 then.Bind();
2315 LoadAndSpill(node->then_expression(), typeof_state());
2316 }
2317 if (else_.is_linked()) {
2318 JumpTarget exit;
2319 if (has_valid_frame()) exit.Jump();
2320 else_.Bind();
2321 LoadAndSpill(node->else_expression(), typeof_state());
2322 if (exit.is_linked()) exit.Bind();
2323 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002324 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325}
2326
2327
ager@chromium.org7c537e22008-10-16 08:43:32 +00002328void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002329 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002330 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002331 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002333 JumpTarget slow;
2334 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002335
2336 // Generate fast-case code for variables that might be shadowed by
2337 // eval-introduced variables. Eval is used a lot without
2338 // introducing variables. In those cases, we do not want to
2339 // perform a runtime call for all variables in the scope
2340 // containing the eval.
2341 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2342 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 // If there was no control flow to slow, we can exit early.
2344 if (!slow.is_linked()) {
2345 frame_->EmitPush(r0);
2346 return;
2347 }
2348
2349 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002350
2351 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2352 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2353 // Only generate the fast case for locals that rewrite to slots.
2354 // This rules out argument loads.
2355 if (potential_slot != NULL) {
2356 __ ldr(r0,
2357 ContextSlotOperandCheckExtensions(potential_slot,
2358 r1,
2359 r2,
2360 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002361 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002362 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2363 __ cmp(r0, ip);
2364 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002365 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002366 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002367 // ContextSlotOperandCheckExtensions so we have to jump around
2368 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002370 }
2371 }
2372
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 slow.Bind();
2374 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002375 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
ager@chromium.org7c537e22008-10-16 08:43:32 +00002378 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002379 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002380 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002381 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002383
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002384 done.Bind();
2385 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386
2387 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002388 // Note: We would like to keep the assert below, but it fires because of
2389 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002390 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002392 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002393 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002394 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002395 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002396 // Const slots may contain 'the hole' value (the constant hasn't been
2397 // initialized yet) which needs to be converted into the 'undefined'
2398 // value.
2399 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002400 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002401 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2402 __ cmp(r0, ip);
2403 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002404 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 }
2406 }
2407}
2408
2409
ager@chromium.org381abbb2009-02-25 13:23:22 +00002410void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2411 TypeofState typeof_state,
2412 Register tmp,
2413 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002414 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002415 // Check that no extension objects have been created by calls to
2416 // eval from the current scope to the global scope.
2417 Register context = cp;
2418 Scope* s = scope();
2419 while (s != NULL) {
2420 if (s->num_heap_slots() > 0) {
2421 if (s->calls_eval()) {
2422 // Check that extension is NULL.
2423 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2424 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002426 }
2427 // Load next context in chain.
2428 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2429 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2430 context = tmp;
2431 }
2432 // If no outer scope calls eval, we do not need to check more
2433 // context extensions.
2434 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2435 s = s->outer_scope();
2436 }
2437
2438 if (s->is_eval_scope()) {
2439 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002440 if (!context.is(tmp)) {
2441 __ mov(tmp, Operand(context));
2442 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002443 __ bind(&next);
2444 // Terminate at global context.
2445 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002446 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2447 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002448 __ b(eq, &fast);
2449 // Check that extension is NULL.
2450 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2451 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002453 // Load next context in chain.
2454 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2455 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2456 __ b(&next);
2457 __ bind(&fast);
2458 }
2459
2460 // All extension objects were empty and it is safe to use a global
2461 // load IC call.
2462 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2463 // Load the global object.
2464 LoadGlobal();
2465 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002466 Result name(r2);
2467 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002468 // Call IC stub.
2469 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002471 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002472 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002473 }
2474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 // Drop the global object. The result is in r0.
2476 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002477}
2478
2479
ager@chromium.org7c537e22008-10-16 08:43:32 +00002480void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002481#ifdef DEBUG
2482 int original_height = frame_->height();
2483#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002484 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002485 Comment cmnt(masm_, "[ Slot");
2486 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002487 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002488}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489
ager@chromium.org7c537e22008-10-16 08:43:32 +00002490
2491void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002492#ifdef DEBUG
2493 int original_height = frame_->height();
2494#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002495 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002496 Comment cmnt(masm_, "[ VariableProxy");
2497
2498 Variable* var = node->var();
2499 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002500 if (expr != NULL) {
2501 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002503 ASSERT(var->is_global());
2504 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002505 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002507 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508}
2509
2510
ager@chromium.org7c537e22008-10-16 08:43:32 +00002511void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002512#ifdef DEBUG
2513 int original_height = frame_->height();
2514#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002515 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002517 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002518 frame_->EmitPush(r0);
2519 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520}
2521
2522
ager@chromium.org7c537e22008-10-16 08:43:32 +00002523void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002524#ifdef DEBUG
2525 int original_height = frame_->height();
2526#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002527 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 Comment cmnt(masm_, "[ RexExp Literal");
2529
2530 // Retrieve the literal array and check the allocated entry.
2531
2532 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002533 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534
2535 // Load the literals array of the function.
2536 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2537
2538 // Load the literal at the ast saved index.
2539 int literal_offset =
2540 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2541 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2542
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002543 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002544 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2545 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002546 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
2548 // If the entry is undefined we call the runtime system to computed
2549 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002551 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002552 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002553 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002555 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 frame_->EmitPush(r0);
2557 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002558 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 frame_->EmitPush(r2);
2563 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564}
2565
2566
2567// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002568// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569// Each created boilerplate is stored in the JSFunction and they are
2570// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002573 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 private:
2580 ObjectLiteral* node_;
2581};
2582
2583
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002584void DeferredObjectLiteral::Generate() {
2585 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002586
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002587 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002590 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002592 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002593 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002595 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002596 __ push(r0);
2597 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2598 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600}
2601
2602
ager@chromium.org7c537e22008-10-16 08:43:32 +00002603void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604#ifdef DEBUG
2605 int original_height = frame_->height();
2606#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002607 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 Comment cmnt(masm_, "[ ObjectLiteral");
2609
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002610 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
2612 // Retrieve the literal array and check the allocated entry.
2613
2614 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002615 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616
2617 // Load the literals array of the function.
2618 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2619
2620 // Load the literal at the ast saved index.
2621 int literal_offset =
2622 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2623 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2624
2625 // Check whether we need to materialize the object literal boilerplate.
2626 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002627 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2628 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002629 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
2632 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002633 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002636 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2637 if (node->depth() == 1) {
2638 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2639 }
2640 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002642 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643
2644 for (int i = 0; i < node->properties()->length(); i++) {
2645 ObjectLiteral::Property* property = node->properties()->at(i);
2646 Literal* key = property->key();
2647 Expression* value = property->value();
2648 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002649 case ObjectLiteral::Property::CONSTANT:
2650 break;
2651 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2652 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2653 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 case ObjectLiteral::Property::COMPUTED: // fall through
2655 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002656 frame_->EmitPush(r0); // dup the result
2657 LoadAndSpill(key);
2658 LoadAndSpill(value);
2659 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002660 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002661 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 break;
2663 }
2664 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665 frame_->EmitPush(r0);
2666 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002667 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002668 frame_->EmitPush(r0);
2669 LoadAndSpill(value);
2670 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002671 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 break;
2673 }
2674 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 frame_->EmitPush(r0);
2676 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002677 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 frame_->EmitPush(r0);
2679 LoadAndSpill(value);
2680 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002681 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 break;
2683 }
2684 }
2685 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002686 ASSERT(frame_->height() == original_height + 1);
2687}
2688
2689
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002690// This deferred code stub will be used for creating the boilerplate
2691// by calling Runtime_CreateArrayLiteralBoilerplate.
2692// Each created boilerplate is stored in the JSFunction and they are
2693// therefore context dependent.
2694class DeferredArrayLiteral: public DeferredCode {
2695 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002696 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002697 set_comment("[ DeferredArrayLiteral");
2698 }
2699
2700 virtual void Generate();
2701
2702 private:
2703 ArrayLiteral* node_;
2704};
2705
2706
2707void DeferredArrayLiteral::Generate() {
2708 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002709
2710 // If the entry is undefined we call the runtime system to computed
2711 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002712 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002713 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002714 // Literal index (1).
2715 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002716 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002717 // Constant properties (2).
2718 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002719 __ push(r0);
2720 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2721 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002722 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002723}
2724
2725
ager@chromium.org7c537e22008-10-16 08:43:32 +00002726void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002727#ifdef DEBUG
2728 int original_height = frame_->height();
2729#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002730 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002732
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002733 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002734
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002735 // Retrieve the literal array and check the allocated entry.
2736
2737 // Load the function of this activation.
2738 __ ldr(r1, frame_->Function());
2739
2740 // Load the literals array of the function.
2741 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2742
2743 // Load the literal at the ast saved index.
2744 int literal_offset =
2745 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2746 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2747
2748 // Check whether we need to materialize the object literal boilerplate.
2749 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002750 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2751 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002752 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002753 deferred->BindExit();
2754
2755 // Push the object literal boilerplate.
2756 frame_->EmitPush(r2);
2757
2758 // Clone the boilerplate object.
2759 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2760 if (node->depth() == 1) {
2761 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2762 }
2763 frame_->CallRuntime(clone_function_id, 1);
2764 frame_->EmitPush(r0); // save the result
2765 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002766
2767 // Generate code to set the elements in the array that are not
2768 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 for (int i = 0; i < node->values()->length(); i++) {
2770 Expression* value = node->values()->at(i);
2771
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002772 // If value is a literal the property value is already set in the
2773 // boilerplate object.
2774 if (value->AsLiteral() != NULL) continue;
2775 // If value is a materialized literal the property value is already set
2776 // in the boilerplate object if it is simple.
2777 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002779 // The property must be set by generated code.
2780 LoadAndSpill(value);
2781 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002783 // Fetch the object literal.
2784 __ ldr(r1, frame_->Top());
2785 // Get the elements array.
2786 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002788 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002789 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002790 __ str(r0, FieldMemOperand(r1, offset));
2791
2792 // Update the write barrier for the array address.
2793 __ mov(r3, Operand(offset));
2794 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002796 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797}
2798
2799
ager@chromium.org32912102009-01-16 10:38:43 +00002800void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002801#ifdef DEBUG
2802 int original_height = frame_->height();
2803#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002804 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002805 // Call runtime routine to allocate the catch extension object and
2806 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002807 Comment cmnt(masm_, "[ CatchExtensionObject");
2808 LoadAndSpill(node->key());
2809 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002810 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2811 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002812 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002813}
2814
2815
ager@chromium.org7c537e22008-10-16 08:43:32 +00002816void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002817#ifdef DEBUG
2818 int original_height = frame_->height();
2819#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002820 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002822
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002823 { Reference target(this, node->target());
2824 if (target.is_illegal()) {
2825 // Fool the virtual frame into thinking that we left the assignment's
2826 // value on the frame.
2827 __ mov(r0, Operand(Smi::FromInt(0)));
2828 frame_->EmitPush(r0);
2829 ASSERT(frame_->height() == original_height + 1);
2830 return;
2831 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833 if (node->op() == Token::ASSIGN ||
2834 node->op() == Token::INIT_VAR ||
2835 node->op() == Token::INIT_CONST) {
2836 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002837
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002839 // +=, *= and similar binary assignments.
2840 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002841 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2842 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002843 bool overwrite =
2844 (node->value()->AsBinaryOperation() != NULL &&
2845 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002846 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002847 SmiOperation(node->binary_op(),
2848 literal->handle(),
2849 false,
2850 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002851 frame_->EmitPush(r0);
2852
2853 } else {
2854 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002855 GenericBinaryOperation(node->binary_op(),
2856 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857 frame_->EmitPush(r0);
2858 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002861 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2862 if (var != NULL &&
2863 (var->mode() == Variable::CONST) &&
2864 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2865 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002868 CodeForSourcePosition(node->position());
2869 if (node->op() == Token::INIT_CONST) {
2870 // Dynamic constant initializations must use the function context
2871 // and initialize the actual constant declared. Dynamic variable
2872 // initializations are simply assignments and use SetValue.
2873 target.SetValue(CONST_INIT);
2874 } else {
2875 target.SetValue(NOT_CONST_INIT);
2876 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877 }
2878 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002879 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880}
2881
2882
ager@chromium.org7c537e22008-10-16 08:43:32 +00002883void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884#ifdef DEBUG
2885 int original_height = frame_->height();
2886#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002887 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888 Comment cmnt(masm_, "[ Throw");
2889
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002890 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002891 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002892 frame_->CallRuntime(Runtime::kThrow, 1);
2893 frame_->EmitPush(r0);
2894 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895}
2896
2897
ager@chromium.org7c537e22008-10-16 08:43:32 +00002898void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899#ifdef DEBUG
2900 int original_height = frame_->height();
2901#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002902 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002904
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002905 { Reference property(this, node);
2906 property.GetValueAndSpill(typeof_state());
2907 }
2908 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909}
2910
2911
ager@chromium.org7c537e22008-10-16 08:43:32 +00002912void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913#ifdef DEBUG
2914 int original_height = frame_->height();
2915#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002916 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 Comment cmnt(masm_, "[ Call");
2918
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002919 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 ZoneList<Expression*>* args = node->arguments();
2921
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924 Variable* var = function->AsVariableProxy()->AsVariable();
2925 Property* property = function->AsProperty();
2926
2927 // ------------------------------------------------------------------------
2928 // Fast-case: Use inline caching.
2929 // ---
2930 // According to ECMA-262, section 11.2.3, page 44, the function to call
2931 // must be resolved after the arguments have been evaluated. The IC code
2932 // automatically handles this by loading the arguments before the function
2933 // is resolved in cache misses (this also holds for megamorphic calls).
2934 // ------------------------------------------------------------------------
2935
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002936 if (var != NULL && var->is_possibly_eval()) {
2937 // ----------------------------------
2938 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2939 // ----------------------------------
2940
2941 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2942 // resolve the function we need to call and the receiver of the
2943 // call. Then we call the resolved function using the given
2944 // arguments.
2945 // Prepare stack for call to resolved function.
2946 LoadAndSpill(function);
2947 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2948 frame_->EmitPush(r2); // Slot for receiver
2949 int arg_count = args->length();
2950 for (int i = 0; i < arg_count; i++) {
2951 LoadAndSpill(args->at(i));
2952 }
2953
2954 // Prepare stack for call to ResolvePossiblyDirectEval.
2955 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2956 frame_->EmitPush(r1);
2957 if (arg_count > 0) {
2958 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2959 frame_->EmitPush(r1);
2960 } else {
2961 frame_->EmitPush(r2);
2962 }
2963
2964 // Resolve the call.
2965 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
2966
2967 // Touch up stack with the right values for the function and the receiver.
2968 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
2969 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
2970 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
2971 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
2972
2973 // Call the function.
2974 CodeForSourcePosition(node->position());
2975
2976 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2977 CallFunctionStub call_function(arg_count, in_loop);
2978 frame_->CallStub(&call_function, arg_count + 1);
2979
2980 __ ldr(cp, frame_->Context());
2981 // Remove the function from the stack.
2982 frame_->Drop();
2983 frame_->EmitPush(r0);
2984
2985 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986 // ----------------------------------
2987 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2988 // ----------------------------------
2989
2990 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002991 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002993
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002994 // Pass the global object as the receiver and let the IC stub
2995 // patch the stack to use the global proxy as 'this' in the
2996 // invoked function.
2997 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002998
2999 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003000 int arg_count = args->length();
3001 for (int i = 0; i < arg_count; i++) {
3002 LoadAndSpill(args->at(i));
3003 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003004
3005 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003006 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3007 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003008 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003009 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3010 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003011 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 frame_->Drop();
3014 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015
3016 } else if (var != NULL && var->slot() != NULL &&
3017 var->slot()->type() == Slot::LOOKUP) {
3018 // ----------------------------------
3019 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3020 // ----------------------------------
3021
3022 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003023 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003024 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003025 frame_->EmitPush(r0);
3026 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 // r0: slot value; r1: receiver
3028
3029 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003030 frame_->EmitPush(r0); // function
3031 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032
3033 // Call the function.
3034 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
3037 } else if (property != NULL) {
3038 // Check if the key is a literal string.
3039 Literal* literal = property->key()->AsLiteral();
3040
3041 if (literal != NULL && literal->handle()->IsSymbol()) {
3042 // ------------------------------------------------------------------
3043 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3044 // ------------------------------------------------------------------
3045
3046 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003047 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 frame_->EmitPush(r0);
3049 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050
3051 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052 int arg_count = args->length();
3053 for (int i = 0; i < arg_count; i++) {
3054 LoadAndSpill(args->at(i));
3055 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056
3057 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003058 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3059 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003060 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003062 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063
3064 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003065 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003066
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068
3069 } else {
3070 // -------------------------------------------
3071 // JavaScript example: 'array[index](1, 2, 3)'
3072 // -------------------------------------------
3073
3074 // Load the function to call from the property through a reference.
3075 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003076 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077
3078 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003079 if (property->is_synthetic()) {
3080 LoadGlobalReceiver(r0);
3081 } else {
3082 __ ldr(r0, frame_->ElementAt(ref.size()));
3083 frame_->EmitPush(r0);
3084 }
3085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086 // Call the function.
3087 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089 }
3090
3091 } else {
3092 // ----------------------------------
3093 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3094 // ----------------------------------
3095
3096 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003098
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003099 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003100 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102 // Call the function.
3103 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003106 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107}
3108
3109
ager@chromium.org7c537e22008-10-16 08:43:32 +00003110void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003111#ifdef DEBUG
3112 int original_height = frame_->height();
3113#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003114 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 Comment cmnt(masm_, "[ CallNew");
3116
3117 // According to ECMA-262, section 11.2.2, page 44, the function
3118 // expression in new calls must be evaluated before the
3119 // arguments. This is different from ordinary calls, where the
3120 // actual function to call is resolved after the arguments have been
3121 // evaluated.
3122
3123 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003124 // receiver. There is no need to use the global proxy here because
3125 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003126 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003127 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128
3129 // Push the arguments ("left-to-right") on the stack.
3130 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003131 int arg_count = args->length();
3132 for (int i = 0; i < arg_count; i++) {
3133 LoadAndSpill(args->at(i));
3134 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135
mads.s.ager31e71382008-08-13 09:32:07 +00003136 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003137 Result num_args(r0);
3138 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003139
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003140 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003141 Result function(r1);
3142 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 // Call the construct call builtin that handles allocation and
3145 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003146 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003147 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003148 frame_->CallCodeObject(ic,
3149 RelocInfo::CONSTRUCT_CALL,
3150 &num_args,
3151 &function,
3152 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003153
3154 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003155 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157}
3158
3159
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003160void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3161 VirtualFrame::SpilledScope spilled_scope;
3162 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003163 JumpTarget leave, null, function, non_function_constructor;
3164
3165 // Load the object into r0.
3166 LoadAndSpill(args->at(0));
3167 frame_->EmitPop(r0);
3168
3169 // If the object is a smi, we return null.
3170 __ tst(r0, Operand(kSmiTagMask));
3171 null.Branch(eq);
3172
3173 // Check that the object is a JS object but take special care of JS
3174 // functions to make sure they have 'Function' as their class.
3175 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3176 null.Branch(lt);
3177
3178 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3179 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3180 // LAST_JS_OBJECT_TYPE.
3181 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3182 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3183 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3184 function.Branch(eq);
3185
3186 // Check if the constructor in the map is a function.
3187 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3188 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3189 non_function_constructor.Branch(ne);
3190
3191 // The r0 register now contains the constructor function. Grab the
3192 // instance class name from there.
3193 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3194 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003195 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003196 leave.Jump();
3197
3198 // Functions have class 'Function'.
3199 function.Bind();
3200 __ mov(r0, Operand(Factory::function_class_symbol()));
3201 frame_->EmitPush(r0);
3202 leave.Jump();
3203
3204 // Objects with a non-function constructor have class 'Object'.
3205 non_function_constructor.Bind();
3206 __ mov(r0, Operand(Factory::Object_symbol()));
3207 frame_->EmitPush(r0);
3208 leave.Jump();
3209
3210 // Non-JS objects have class null.
3211 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003212 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003213 frame_->EmitPush(r0);
3214
3215 // All done.
3216 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003217}
3218
3219
ager@chromium.org7c537e22008-10-16 08:43:32 +00003220void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003221 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003222 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003223 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003224 LoadAndSpill(args->at(0));
3225 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003226 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003228 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003229 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3230 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003231 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232 // Load the value.
3233 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003234 leave.Bind();
3235 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236}
3237
3238
ager@chromium.org7c537e22008-10-16 08:43:32 +00003239void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003240 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003242 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003243 LoadAndSpill(args->at(0)); // Load the object.
3244 LoadAndSpill(args->at(1)); // Load the value.
3245 frame_->EmitPop(r0); // r0 contains value
3246 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 // if (object->IsSmi()) return object.
3248 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003250 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3251 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 // Store the value.
3254 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3255 // Update the write barrier.
3256 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3257 __ RecordWrite(r1, r2, r3);
3258 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003259 leave.Bind();
3260 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261}
3262
3263
ager@chromium.org7c537e22008-10-16 08:43:32 +00003264void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003265 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003267 LoadAndSpill(args->at(0));
3268 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003269 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 cc_reg_ = eq;
3271}
3272
3273
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003274void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003276 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3277 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003278#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003279 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003280 LoadAndSpill(args->at(1));
3281 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003282 __ CallRuntime(Runtime::kLog, 2);
3283 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003284#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003285 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003287}
3288
3289
ager@chromium.org7c537e22008-10-16 08:43:32 +00003290void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003291 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003292 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003293 LoadAndSpill(args->at(0));
3294 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003295 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003296 cc_reg_ = eq;
3297}
3298
3299
kasper.lund7276f142008-07-30 08:49:36 +00003300// This should generate code that performs a charCodeAt() call or returns
3301// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3302// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003303void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003304 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003305 ASSERT(args->length() == 2);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003306 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003308}
3309
3310
ager@chromium.org7c537e22008-10-16 08:43:32 +00003311void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003312 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003313 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003314 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003315 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003316 // We need the CC bits to come out as not_equal in the case where the
3317 // object is a smi. This can't be done with the usual test opcode so
3318 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003319 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003320 __ and_(r1, r0, Operand(kSmiTagMask));
3321 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003322 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003323 // It is a heap object - get the map. Check if the object is a JS array.
3324 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003325 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 cc_reg_ = eq;
3327}
3328
3329
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003330void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3331 VirtualFrame::SpilledScope spilled_scope;
3332 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003333
3334 // Get the frame pointer for the calling frame.
3335 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3336
3337 // Skip the arguments adaptor frame if it exists.
3338 Label check_frame_marker;
3339 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003340 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003341 __ b(ne, &check_frame_marker);
3342 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3343
3344 // Check the marker in the calling frame.
3345 __ bind(&check_frame_marker);
3346 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3347 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3348 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003349}
3350
3351
ager@chromium.org7c537e22008-10-16 08:43:32 +00003352void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003353 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354 ASSERT(args->length() == 0);
3355
mads.s.ager31e71382008-08-13 09:32:07 +00003356 // Seed the result with the formal parameters count, which will be used
3357 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3359
3360 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003361 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003362 frame_->CallStub(&stub, 0);
3363 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364}
3365
3366
ager@chromium.org7c537e22008-10-16 08:43:32 +00003367void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003368 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003369 ASSERT(args->length() == 1);
3370
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003371 // Satisfy contract with ArgumentsAccessStub:
3372 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003373 LoadAndSpill(args->at(0));
3374 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003375 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376
3377 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003378 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003379 frame_->CallStub(&stub, 0);
3380 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381}
3382
3383
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003384void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3385 VirtualFrame::SpilledScope spilled_scope;
3386 ASSERT(args->length() == 0);
3387 __ Call(ExternalReference::random_positive_smi_function().address(),
3388 RelocInfo::RUNTIME_ENTRY);
3389 frame_->EmitPush(r0);
3390}
3391
3392
3393void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3394 VirtualFrame::SpilledScope spilled_scope;
3395 LoadAndSpill(args->at(0));
3396 switch (op) {
3397 case SIN:
3398 frame_->CallRuntime(Runtime::kMath_sin, 1);
3399 break;
3400 case COS:
3401 frame_->CallRuntime(Runtime::kMath_cos, 1);
3402 break;
3403 }
3404 frame_->EmitPush(r0);
3405}
3406
3407
ager@chromium.org7c537e22008-10-16 08:43:32 +00003408void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003409 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003410 ASSERT(args->length() == 2);
3411
3412 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003413 LoadAndSpill(args->at(0));
3414 LoadAndSpill(args->at(1));
3415 frame_->EmitPop(r0);
3416 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003417 __ cmp(r0, Operand(r1));
3418 cc_reg_ = eq;
3419}
3420
3421
ager@chromium.org7c537e22008-10-16 08:43:32 +00003422void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003423#ifdef DEBUG
3424 int original_height = frame_->height();
3425#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003426 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003427 if (CheckForInlineRuntimeCall(node)) {
3428 ASSERT((has_cc() && frame_->height() == original_height) ||
3429 (!has_cc() && frame_->height() == original_height + 1));
3430 return;
3431 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432
3433 ZoneList<Expression*>* args = node->arguments();
3434 Comment cmnt(masm_, "[ CallRuntime");
3435 Runtime::Function* function = node->function();
3436
ager@chromium.org41826e72009-03-30 13:30:57 +00003437 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003438 // Prepare stack for calling JS runtime function.
3439 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003441 // Push the builtins object found in the current global object.
3442 __ ldr(r1, GlobalObject());
3443 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003444 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003445 }
mads.s.ager31e71382008-08-13 09:32:07 +00003446
ager@chromium.org41826e72009-03-30 13:30:57 +00003447 // Push the arguments ("left-to-right").
3448 int arg_count = args->length();
3449 for (int i = 0; i < arg_count; i++) {
3450 LoadAndSpill(args->at(i));
3451 }
mads.s.ager31e71382008-08-13 09:32:07 +00003452
ager@chromium.org41826e72009-03-30 13:30:57 +00003453 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003454 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003455 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3456 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003457 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003458 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003459 frame_->Drop();
3460 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003461 } else {
3462 // Call the C runtime function.
3463 frame_->CallRuntime(function, arg_count);
3464 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003465 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003466 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467}
3468
3469
ager@chromium.org7c537e22008-10-16 08:43:32 +00003470void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471#ifdef DEBUG
3472 int original_height = frame_->height();
3473#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003474 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003475 Comment cmnt(masm_, "[ UnaryOperation");
3476
3477 Token::Value op = node->op();
3478
3479 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003480 LoadConditionAndSpill(node->expression(),
3481 NOT_INSIDE_TYPEOF,
3482 false_target(),
3483 true_target(),
3484 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003485 // LoadCondition may (and usually does) leave a test and branch to
3486 // be emitted by the caller. In that case, negate the condition.
3487 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488
3489 } else if (op == Token::DELETE) {
3490 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003491 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003493 LoadAndSpill(property->obj());
3494 LoadAndSpill(property->key());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003495 Result arg_count(r0);
3496 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003497 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498
mads.s.ager31e71382008-08-13 09:32:07 +00003499 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500 Slot* slot = variable->slot();
3501 if (variable->is_global()) {
3502 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003503 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003504 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003505 Result arg_count(r0);
3506 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003507 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508
3509 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3510 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003511 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003512 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003513 frame_->EmitPush(r0);
3514 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003515 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003516 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003517 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003518 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003519 Result arg_count(r0);
3520 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003521 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522
mads.s.ager31e71382008-08-13 09:32:07 +00003523 } else {
3524 // Default: Result of deleting non-global, not dynamically
3525 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003526 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003527 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528
3529 } else {
3530 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003531 LoadAndSpill(node->expression()); // may have side-effects
3532 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003533 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003535 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536
3537 } else if (op == Token::TYPEOF) {
3538 // Special case for loading the typeof expression; see comment on
3539 // LoadTypeofExpression().
3540 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003541 frame_->CallRuntime(Runtime::kTypeof, 1);
3542 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543
3544 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003545 LoadAndSpill(node->expression());
3546 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547 switch (op) {
3548 case Token::NOT:
3549 case Token::DELETE:
3550 case Token::TYPEOF:
3551 UNREACHABLE(); // handled above
3552 break;
3553
3554 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003555 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003556 (node->expression()->AsBinaryOperation() != NULL &&
3557 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003558 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003559 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560 break;
3561 }
3562
3563 case Token::BIT_NOT: {
3564 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003565 JumpTarget smi_label;
3566 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003567 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003568 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003569
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003570 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003571 Result arg_count(r0);
3572 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003573 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 continue_label.Jump();
3576 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577 __ mvn(r0, Operand(r0));
3578 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003579 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580 break;
3581 }
3582
3583 case Token::VOID:
3584 // since the stack top is cached in r0, popping and then
3585 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003586 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 break;
3588
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003589 case Token::ADD: {
3590 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003591 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003592 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 continue_label.Branch(eq);
3594 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003595 Result arg_count(r0);
3596 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003597 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3598 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601 default:
3602 UNREACHABLE();
3603 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003606 ASSERT(!has_valid_frame() ||
3607 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003608 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609}
3610
3611
ager@chromium.org7c537e22008-10-16 08:43:32 +00003612void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003613#ifdef DEBUG
3614 int original_height = frame_->height();
3615#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003616 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617 Comment cmnt(masm_, "[ CountOperation");
3618
3619 bool is_postfix = node->is_postfix();
3620 bool is_increment = node->op() == Token::INC;
3621
3622 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3623 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3624
3625 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003626 if (is_postfix) {
3627 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003628 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630
3631 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003632 if (target.is_illegal()) {
3633 // Spoof the virtual frame to have the expected height (one higher
3634 // than on entry).
3635 if (!is_postfix) {
3636 __ mov(r0, Operand(Smi::FromInt(0)));
3637 frame_->EmitPush(r0);
3638 }
3639 ASSERT(frame_->height() == original_height + 1);
3640 return;
3641 }
3642 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3643 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003645 JumpTarget slow;
3646 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003647
3648 // Load the value (1) into register r1.
3649 __ mov(r1, Operand(Smi::FromInt(1)));
3650
3651 // Check for smi operand.
3652 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003653 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654
3655 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003656 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003657 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003658 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659
3660 // Perform optimistic increment/decrement.
3661 if (is_increment) {
3662 __ add(r0, r0, Operand(r1), SetCC);
3663 } else {
3664 __ sub(r0, r0, Operand(r1), SetCC);
3665 }
3666
3667 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003668 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669
3670 // Revert optimistic increment/decrement.
3671 if (is_increment) {
3672 __ sub(r0, r0, Operand(r1));
3673 } else {
3674 __ add(r0, r0, Operand(r1));
3675 }
3676
3677 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003678 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003679 {
3680 // Convert the operand to a number.
3681 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003682 Result arg_count(r0);
3683 __ mov(r0, Operand(0));
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003684 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3685 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003687 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003688 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689 }
3690
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003691 // Compute the new value.
3692 __ mov(r1, Operand(Smi::FromInt(1)));
3693 frame_->EmitPush(r0);
3694 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003696 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003697 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003698 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699 }
3700
3701 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003702 exit.Bind();
3703 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003704 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 }
3706
3707 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003708 if (is_postfix) frame_->EmitPop(r0);
3709 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710}
3711
3712
ager@chromium.org7c537e22008-10-16 08:43:32 +00003713void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003714#ifdef DEBUG
3715 int original_height = frame_->height();
3716#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003717 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718 Comment cmnt(masm_, "[ BinaryOperation");
3719 Token::Value op = node->op();
3720
3721 // According to ECMA-262 section 11.11, page 58, the binary logical
3722 // operators must yield the result of one of the two expressions
3723 // before any ToBoolean() conversions. This means that the value
3724 // produced by a && or || operator is not necessarily a boolean.
3725
3726 // NOTE: If the left hand side produces a materialized value (not in
3727 // the CC register), we force the right hand side to do the
3728 // same. This is necessary because we may have to branch to the exit
3729 // after evaluating the left hand side (due to the shortcut
3730 // semantics), but the compiler must (statically) know if the result
3731 // of compiling the binary operation is materialized or not.
3732
3733 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003734 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003735 LoadConditionAndSpill(node->left(),
3736 NOT_INSIDE_TYPEOF,
3737 &is_true,
3738 false_target(),
3739 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003740 if (has_valid_frame() && !has_cc()) {
3741 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003742 JumpTarget pop_and_continue;
3743 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003744
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003745 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003746 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 // Avoid popping the result if it converts to 'false' using the
3748 // standard ToBoolean() conversion as described in ECMA-262,
3749 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003750 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751 Branch(false, &exit);
3752
3753 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003754 pop_and_continue.Bind();
3755 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756
3757 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003758 is_true.Bind();
3759 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003760
3761 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003763 } else if (has_cc() || is_true.is_linked()) {
3764 // The left-hand side is either (a) partially compiled to
3765 // control flow with a final branch left to emit or (b) fully
3766 // compiled to control flow and possibly true.
3767 if (has_cc()) {
3768 Branch(false, false_target());
3769 }
3770 is_true.Bind();
3771 LoadConditionAndSpill(node->right(),
3772 NOT_INSIDE_TYPEOF,
3773 true_target(),
3774 false_target(),
3775 false);
3776 } else {
3777 // Nothing to do.
3778 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779 }
3780
3781 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003782 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 LoadConditionAndSpill(node->left(),
3784 NOT_INSIDE_TYPEOF,
3785 true_target(),
3786 &is_false,
3787 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003788 if (has_valid_frame() && !has_cc()) {
3789 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003790 JumpTarget pop_and_continue;
3791 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003793 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003794 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 // Avoid popping the result if it converts to 'true' using the
3796 // standard ToBoolean() conversion as described in ECMA-262,
3797 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003798 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799 Branch(true, &exit);
3800
3801 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003802 pop_and_continue.Bind();
3803 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804
3805 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 is_false.Bind();
3807 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808
3809 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003811 } else if (has_cc() || is_false.is_linked()) {
3812 // The left-hand side is either (a) partially compiled to
3813 // control flow with a final branch left to emit or (b) fully
3814 // compiled to control flow and possibly false.
3815 if (has_cc()) {
3816 Branch(true, true_target());
3817 }
3818 is_false.Bind();
3819 LoadConditionAndSpill(node->right(),
3820 NOT_INSIDE_TYPEOF,
3821 true_target(),
3822 false_target(),
3823 false);
3824 } else {
3825 // Nothing to do.
3826 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827 }
3828
3829 } else {
3830 // Optimize for the case where (at least) one of the expressions
3831 // is a literal small integer.
3832 Literal* lliteral = node->left()->AsLiteral();
3833 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003834 // NOTE: The code below assumes that the slow cases (calls to runtime)
3835 // never return a constant/immutable object.
3836 bool overwrite_left =
3837 (node->left()->AsBinaryOperation() != NULL &&
3838 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3839 bool overwrite_right =
3840 (node->right()->AsBinaryOperation() != NULL &&
3841 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842
3843 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003844 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003845 SmiOperation(node->op(),
3846 rliteral->handle(),
3847 false,
3848 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849
3850 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003851 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003852 SmiOperation(node->op(),
3853 lliteral->handle(),
3854 true,
3855 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856
3857 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003858 OverwriteMode overwrite_mode = NO_OVERWRITE;
3859 if (overwrite_left) {
3860 overwrite_mode = OVERWRITE_LEFT;
3861 } else if (overwrite_right) {
3862 overwrite_mode = OVERWRITE_RIGHT;
3863 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864 LoadAndSpill(node->left());
3865 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003866 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003867 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003870 ASSERT(!has_valid_frame() ||
3871 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003872 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873}
3874
3875
ager@chromium.org7c537e22008-10-16 08:43:32 +00003876void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003877#ifdef DEBUG
3878 int original_height = frame_->height();
3879#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003880 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003881 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003882 frame_->EmitPush(r0);
3883 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884}
3885
3886
ager@chromium.org7c537e22008-10-16 08:43:32 +00003887void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003888#ifdef DEBUG
3889 int original_height = frame_->height();
3890#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003891 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892 Comment cmnt(masm_, "[ CompareOperation");
3893
3894 // Get the expressions from the node.
3895 Expression* left = node->left();
3896 Expression* right = node->right();
3897 Token::Value op = node->op();
3898
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003899 // To make null checks efficient, we check if either left or right is the
3900 // literal 'null'. If so, we optimize the code by inlining a null check
3901 // instead of calling the (very) general runtime routine for checking
3902 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003904 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003905 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003906 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003907 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3908 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003910 LoadAndSpill(left_is_null ? right : left);
3911 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003912 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3913 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003915 // The 'null' value is only equal to 'undefined' if using non-strict
3916 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003919
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003920 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3921 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003925 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003927 // It can be an undetectable object.
3928 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3929 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3930 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3931 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 }
3933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003934 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003935 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 return;
3937 }
3938 }
3939
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003940 // To make typeof testing for natives implemented in JavaScript really
3941 // efficient, we generate special code for expressions of the form:
3942 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003943 UnaryOperation* operation = left->AsUnaryOperation();
3944 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3945 (operation != NULL && operation->op() == Token::TYPEOF) &&
3946 (right->AsLiteral() != NULL &&
3947 right->AsLiteral()->handle()->IsString())) {
3948 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3949
mads.s.ager31e71382008-08-13 09:32:07 +00003950 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003951 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003952 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953
3954 if (check->Equals(Heap::number_symbol())) {
3955 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003956 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003958 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3959 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 cc_reg_ = eq;
3961
3962 } else if (check->Equals(Heap::string_symbol())) {
3963 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
3966 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3967
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003968 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3970 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3971 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003972 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003973
3974 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3975 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3976 cc_reg_ = lt;
3977
3978 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003979 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3980 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003981 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003982 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3983 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 cc_reg_ = eq;
3985
3986 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003987 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3988 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990
3991 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003992 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003993
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003994 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003995 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3996 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3997 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3998 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3999
4000 cc_reg_ = eq;
4001
4002 } else if (check->Equals(Heap::function_symbol())) {
4003 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004004 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004005 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 cc_reg_ = eq;
4007
4008 } else if (check->Equals(Heap::object_symbol())) {
4009 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011
4012 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004013 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4014 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004015 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004017 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4019 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4020 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022
4023 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4024 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004025 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4027 cc_reg_ = le;
4028
4029 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004030 // Uncommon case: typeof testing against a string literal that is
4031 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004034 ASSERT(!has_valid_frame() ||
4035 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036 return;
4037 }
4038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039 switch (op) {
4040 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004041 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042 break;
4043
4044 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004045 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 break;
4047
4048 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004049 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050 break;
4051
4052 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004053 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054 break;
4055
4056 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004057 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004058 break;
4059
4060 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004061 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062 break;
4063
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004064 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004065 LoadAndSpill(left);
4066 LoadAndSpill(right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004067 Result arg_count(r0);
4068 __ mov(r0, Operand(1)); // not counting receiver
4069 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2);
4070 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004072 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004074 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004075 LoadAndSpill(left);
4076 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004077 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004078 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004079 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004080 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004081 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084
4085 default:
4086 UNREACHABLE();
4087 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 ASSERT((has_cc() && frame_->height() == original_height) ||
4089 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090}
4091
4092
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004093#ifdef DEBUG
4094bool CodeGenerator::HasValidEntryRegisters() { return true; }
4095#endif
4096
4097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004099#define __ ACCESS_MASM(masm)
4100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101
ager@chromium.org7c537e22008-10-16 08:43:32 +00004102Handle<String> Reference::GetName() {
4103 ASSERT(type_ == NAMED);
4104 Property* property = expression_->AsProperty();
4105 if (property == NULL) {
4106 // Global variable reference treated as a named property reference.
4107 VariableProxy* proxy = expression_->AsVariableProxy();
4108 ASSERT(proxy->AsVariable() != NULL);
4109 ASSERT(proxy->AsVariable()->is_global());
4110 return proxy->name();
4111 } else {
4112 Literal* raw_name = property->key()->AsLiteral();
4113 ASSERT(raw_name != NULL);
4114 return Handle<String>(String::cast(*raw_name->handle()));
4115 }
4116}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004117
ager@chromium.org7c537e22008-10-16 08:43:32 +00004118
4119void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004120 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004121 ASSERT(!is_illegal());
4122 ASSERT(!cgen_->has_cc());
4123 MacroAssembler* masm = cgen_->masm();
4124 Property* property = expression_->AsProperty();
4125 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004126 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004127 }
4128
4129 switch (type_) {
4130 case SLOT: {
4131 Comment cmnt(masm, "[ Load from Slot");
4132 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4133 ASSERT(slot != NULL);
4134 cgen_->LoadFromSlot(slot, typeof_state);
4135 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004136 }
4137
ager@chromium.org7c537e22008-10-16 08:43:32 +00004138 case NAMED: {
4139 // TODO(1241834): Make sure that this it is safe to ignore the
4140 // distinction between expressions in a typeof and not in a typeof. If
4141 // there is a chance that reference errors can be thrown below, we
4142 // must distinguish between the two kinds of loads (typeof expression
4143 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004144 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004145 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004146 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004147 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004148 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4149 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004150 Result name_reg(r2);
4151 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004152 ASSERT(var == NULL || var->is_global());
4153 RelocInfo::Mode rmode = (var == NULL)
4154 ? RelocInfo::CODE_TARGET
4155 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004156 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4157 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004158 break;
4159 }
4160
4161 case KEYED: {
4162 // TODO(1241834): Make sure that this it is safe to ignore the
4163 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004164
4165 // TODO(181): Implement inlined version of array indexing once
4166 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004167 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004168 Comment cmnt(masm, "[ Load from keyed Property");
4169 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004170 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004171 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004172 ASSERT(var == NULL || var->is_global());
4173 RelocInfo::Mode rmode = (var == NULL)
4174 ? RelocInfo::CODE_TARGET
4175 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004176 frame->CallCodeObject(ic, rmode, 0);
4177 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004178 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004179 }
4180
4181 default:
4182 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004183 }
4184}
4185
4186
ager@chromium.org7c537e22008-10-16 08:43:32 +00004187void Reference::SetValue(InitState init_state) {
4188 ASSERT(!is_illegal());
4189 ASSERT(!cgen_->has_cc());
4190 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004191 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004192 Property* property = expression_->AsProperty();
4193 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004194 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004195 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004196
ager@chromium.org7c537e22008-10-16 08:43:32 +00004197 switch (type_) {
4198 case SLOT: {
4199 Comment cmnt(masm, "[ Store to Slot");
4200 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4201 ASSERT(slot != NULL);
4202 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004203 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004204
ager@chromium.org7c537e22008-10-16 08:43:32 +00004205 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004206 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004207 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004208 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004209
ager@chromium.org7c537e22008-10-16 08:43:32 +00004210 if (init_state == CONST_INIT) {
4211 // Same as the case for a normal store, but ignores attribute
4212 // (e.g. READ_ONLY) of context slot so that we can initialize
4213 // const properties (introduced via eval("const foo = (some
4214 // expr);")). Also, uses the current function context instead of
4215 // the top context.
4216 //
4217 // Note that we must declare the foo upon entry of eval(), via a
4218 // context slot declaration, but we cannot initialize it at the
4219 // same time, because the const declaration may be at the end of
4220 // the eval code (sigh...) and the const variable may have been
4221 // used before (where its value is 'undefined'). Thus, we can only
4222 // do the initialization when we actually encounter the expression
4223 // and when the expression operands are defined and valid, and
4224 // thus we need the split into 2 operations: declaration of the
4225 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004226 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004227 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004228 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004229 }
4230 // Storing a variable must keep the (new) value on the expression
4231 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004232 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004233
ager@chromium.org7c537e22008-10-16 08:43:32 +00004234 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004235 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004236
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004237 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004238 if (init_state == CONST_INIT) {
4239 ASSERT(slot->var()->mode() == Variable::CONST);
4240 // Only the first const initialization must be executed (the slot
4241 // still contains 'the hole' value). When the assignment is
4242 // executed, the code is identical to a normal store (see below).
4243 Comment cmnt(masm, "[ Init const");
4244 __ ldr(r2, cgen_->SlotOperand(slot, r2));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004245 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
4246 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004247 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004248 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004249
ager@chromium.org7c537e22008-10-16 08:43:32 +00004250 // We must execute the store. Storing a variable must keep the
4251 // (new) value on the stack. This is necessary for compiling
4252 // assignment expressions.
4253 //
4254 // Note: We will reach here even with slot->var()->mode() ==
4255 // Variable::CONST because of const declarations which will
4256 // initialize consts to 'the hole' value and by doing so, end up
4257 // calling this code. r2 may be loaded with context; used below in
4258 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004259 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004260 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004261 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004262 if (slot->type() == Slot::CONTEXT) {
4263 // Skip write barrier if the written value is a smi.
4264 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004265 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004266 // r2 is loaded with context when calling SlotOperand above.
4267 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4268 __ mov(r3, Operand(offset));
4269 __ RecordWrite(r2, r3, r1);
4270 }
4271 // If we definitely did not jump over the assignment, we do not need
4272 // to bind the exit label. Doing so can defeat peephole
4273 // optimization.
4274 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004276 }
4277 }
4278 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004279 }
4280
ager@chromium.org7c537e22008-10-16 08:43:32 +00004281 case NAMED: {
4282 Comment cmnt(masm, "[ Store to named Property");
4283 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004284 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004285 Handle<String> name(GetName());
4286
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004287 Result value(r0);
4288 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004289
4290 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004291 Result property_name(r2);
4292 __ mov(r2, Operand(name));
4293 frame->CallCodeObject(ic,
4294 RelocInfo::CODE_TARGET,
4295 &value,
4296 &property_name,
4297 0);
4298 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004299 break;
4300 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004301
ager@chromium.org7c537e22008-10-16 08:43:32 +00004302 case KEYED: {
4303 Comment cmnt(masm, "[ Store to keyed Property");
4304 Property* property = expression_->AsProperty();
4305 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004306 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004307
4308 // Call IC code.
4309 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4310 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004311 Result value(r0);
4312 frame->EmitPop(r0); // value
4313 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4314 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004315 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004316 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004317
4318 default:
4319 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004320 }
4321}
4322
4323
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004324// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4325// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4326// (31 instead of 32).
4327static void CountLeadingZeros(
4328 MacroAssembler* masm,
4329 Register source,
4330 Register scratch,
4331 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004332#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004333 __ clz(zeros, source); // This instruction is only supported after ARM5.
4334#else
4335 __ mov(zeros, Operand(0));
4336 __ mov(scratch, source);
4337 // Top 16.
4338 __ tst(scratch, Operand(0xffff0000));
4339 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4340 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4341 // Top 8.
4342 __ tst(scratch, Operand(0xff000000));
4343 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4344 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4345 // Top 4.
4346 __ tst(scratch, Operand(0xf0000000));
4347 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4348 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4349 // Top 2.
4350 __ tst(scratch, Operand(0xc0000000));
4351 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4352 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4353 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004354 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004355 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4356#endif
4357}
4358
4359
4360// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4361// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4362// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4363// scratch register. Destroys the source register. No GC occurs during this
4364// stub so you don't have to set up the frame.
4365class ConvertToDoubleStub : public CodeStub {
4366 public:
4367 ConvertToDoubleStub(Register result_reg_1,
4368 Register result_reg_2,
4369 Register source_reg,
4370 Register scratch_reg)
4371 : result1_(result_reg_1),
4372 result2_(result_reg_2),
4373 source_(source_reg),
4374 zeros_(scratch_reg) { }
4375
4376 private:
4377 Register result1_;
4378 Register result2_;
4379 Register source_;
4380 Register zeros_;
4381
4382 // Minor key encoding in 16 bits.
4383 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4384 class OpBits: public BitField<Token::Value, 2, 14> {};
4385
4386 Major MajorKey() { return ConvertToDouble; }
4387 int MinorKey() {
4388 // Encode the parameters in a unique 16 bit value.
4389 return result1_.code() +
4390 (result2_.code() << 4) +
4391 (source_.code() << 8) +
4392 (zeros_.code() << 12);
4393 }
4394
4395 void Generate(MacroAssembler* masm);
4396
4397 const char* GetName() { return "ConvertToDoubleStub"; }
4398
4399#ifdef DEBUG
4400 void Print() { PrintF("ConvertToDoubleStub\n"); }
4401#endif
4402};
4403
4404
4405void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4406#ifndef BIG_ENDIAN_FLOATING_POINT
4407 Register exponent = result1_;
4408 Register mantissa = result2_;
4409#else
4410 Register exponent = result2_;
4411 Register mantissa = result1_;
4412#endif
4413 Label not_special;
4414 // Convert from Smi to integer.
4415 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4416 // Move sign bit from source to destination. This works because the sign bit
4417 // in the exponent word of the double has the same position and polarity as
4418 // the 2's complement sign bit in a Smi.
4419 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4420 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4421 // Subtract from 0 if source was negative.
4422 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4423 __ cmp(source_, Operand(1));
4424 __ b(gt, &not_special);
4425
4426 // We have -1, 0 or 1, which we treat specially.
4427 __ cmp(source_, Operand(0));
4428 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4429 static const uint32_t exponent_word_for_1 =
4430 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4431 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4432 // 1, 0 and -1 all have 0 for the second word.
4433 __ mov(mantissa, Operand(0));
4434 __ Ret();
4435
4436 __ bind(&not_special);
4437 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4438 // Gets the wrong answer for 0, but we already checked for that case above.
4439 CountLeadingZeros(masm, source_, mantissa, zeros_);
4440 // Compute exponent and or it into the exponent register.
4441 // We use result2 as a scratch register here.
4442 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4443 __ orr(exponent,
4444 exponent,
4445 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4446 // Shift up the source chopping the top bit off.
4447 __ add(zeros_, zeros_, Operand(1));
4448 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4449 __ mov(source_, Operand(source_, LSL, zeros_));
4450 // Compute lower part of fraction (last 12 bits).
4451 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4452 // And the top (top 20 bits).
4453 __ orr(exponent,
4454 exponent,
4455 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4456 __ Ret();
4457}
4458
4459
4460// This stub can convert a signed int32 to a heap number (double). It does
4461// not work for int32s that are in Smi range! No GC occurs during this stub
4462// so you don't have to set up the frame.
4463class WriteInt32ToHeapNumberStub : public CodeStub {
4464 public:
4465 WriteInt32ToHeapNumberStub(Register the_int,
4466 Register the_heap_number,
4467 Register scratch)
4468 : the_int_(the_int),
4469 the_heap_number_(the_heap_number),
4470 scratch_(scratch) { }
4471
4472 private:
4473 Register the_int_;
4474 Register the_heap_number_;
4475 Register scratch_;
4476
4477 // Minor key encoding in 16 bits.
4478 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4479 class OpBits: public BitField<Token::Value, 2, 14> {};
4480
4481 Major MajorKey() { return WriteInt32ToHeapNumber; }
4482 int MinorKey() {
4483 // Encode the parameters in a unique 16 bit value.
4484 return the_int_.code() +
4485 (the_heap_number_.code() << 4) +
4486 (scratch_.code() << 8);
4487 }
4488
4489 void Generate(MacroAssembler* masm);
4490
4491 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4492
4493#ifdef DEBUG
4494 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4495#endif
4496};
4497
4498
4499// See comment for class.
4500void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4501 Label max_negative_int;
4502 // the_int_ has the answer which is a signed int32 but not a Smi.
4503 // We test for the special value that has a different exponent. This test
4504 // has the neat side effect of setting the flags according to the sign.
4505 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004506 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004507 __ b(eq, &max_negative_int);
4508 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4509 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4510 uint32_t non_smi_exponent =
4511 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4512 __ mov(scratch_, Operand(non_smi_exponent));
4513 // Set the sign bit in scratch_ if the value was negative.
4514 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4515 // Subtract from 0 if the value was negative.
4516 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4517 // We should be masking the implict first digit of the mantissa away here,
4518 // but it just ends up combining harmlessly with the last digit of the
4519 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4520 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4521 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4522 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4523 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4524 __ str(scratch_, FieldMemOperand(the_heap_number_,
4525 HeapNumber::kExponentOffset));
4526 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4527 __ str(scratch_, FieldMemOperand(the_heap_number_,
4528 HeapNumber::kMantissaOffset));
4529 __ Ret();
4530
4531 __ bind(&max_negative_int);
4532 // The max negative int32 is stored as a positive number in the mantissa of
4533 // a double because it uses a sign bit instead of using two's complement.
4534 // The actual mantissa bits stored are all 0 because the implicit most
4535 // significant 1 bit is not stored.
4536 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4537 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4538 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4539 __ mov(ip, Operand(0));
4540 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4541 __ Ret();
4542}
4543
4544
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004545// Handle the case where the lhs and rhs are the same object.
4546// Equality is almost reflexive (everything but NaN), so this is a test
4547// for "identity and not NaN".
4548static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4549 Label* slow,
4550 Condition cc) {
4551 Label not_identical;
4552 __ cmp(r0, Operand(r1));
4553 __ b(ne, &not_identical);
4554
4555 Register exp_mask_reg = r5;
4556 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4557
4558 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4559 // so we do the second best thing - test it ourselves.
4560 Label heap_number, return_equal;
4561 // They are both equal and they are not both Smis so both of them are not
4562 // Smis. If it's not a heap number, then return equal.
4563 if (cc == lt || cc == gt) {
4564 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4565 __ b(ge, slow);
4566 } else {
4567 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4568 __ b(eq, &heap_number);
4569 // Comparing JS objects with <=, >= is complicated.
4570 if (cc != eq) {
4571 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4572 __ b(ge, slow);
4573 }
4574 }
4575 __ bind(&return_equal);
4576 if (cc == lt) {
4577 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4578 } else if (cc == gt) {
4579 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4580 } else {
4581 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4582 }
4583 __ mov(pc, Operand(lr)); // Return.
4584
4585 // For less and greater we don't have to check for NaN since the result of
4586 // x < x is false regardless. For the others here is some code to check
4587 // for NaN.
4588 if (cc != lt && cc != gt) {
4589 __ bind(&heap_number);
4590 // It is a heap number, so return non-equal if it's NaN and equal if it's
4591 // not NaN.
4592 // The representation of NaN values has all exponent bits (52..62) set,
4593 // and not all mantissa bits (0..51) clear.
4594 // Read top bits of double representation (second word of value).
4595 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4596 // Test that exponent bits are all set.
4597 __ and_(r3, r2, Operand(exp_mask_reg));
4598 __ cmp(r3, Operand(exp_mask_reg));
4599 __ b(ne, &return_equal);
4600
4601 // Shift out flag and all exponent bits, retaining only mantissa.
4602 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4603 // Or with all low-bits of mantissa.
4604 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4605 __ orr(r0, r3, Operand(r2), SetCC);
4606 // For equal we already have the right value in r0: Return zero (equal)
4607 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4608 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4609 // if it's a NaN.
4610 if (cc != eq) {
4611 // All-zero means Infinity means equal.
4612 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4613 if (cc == le) {
4614 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4615 } else {
4616 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4617 }
4618 }
4619 __ mov(pc, Operand(lr)); // Return.
4620 }
4621 // No fall through here.
4622
4623 __ bind(&not_identical);
4624}
4625
4626
4627// See comment at call site.
4628static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4629 Label* rhs_not_nan,
4630 Label* slow,
4631 bool strict) {
4632 Label lhs_is_smi;
4633 __ tst(r0, Operand(kSmiTagMask));
4634 __ b(eq, &lhs_is_smi);
4635
4636 // Rhs is a Smi. Check whether the non-smi is a heap number.
4637 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4638 if (strict) {
4639 // If lhs was not a number and rhs was a Smi then strict equality cannot
4640 // succeed. Return non-equal (r0 is already not zero)
4641 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4642 } else {
4643 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4644 // the runtime.
4645 __ b(ne, slow);
4646 }
4647
4648 // Rhs is a smi, lhs is a number.
4649 __ push(lr);
4650 __ mov(r7, Operand(r1));
4651 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4652 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4653 // r3 and r2 are rhs as double.
4654 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4655 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4656 // We now have both loaded as doubles but we can skip the lhs nan check
4657 // since it's a Smi.
4658 __ pop(lr);
4659 __ jmp(rhs_not_nan);
4660
4661 __ bind(&lhs_is_smi);
4662 // Lhs is a Smi. Check whether the non-smi is a heap number.
4663 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4664 if (strict) {
4665 // If lhs was not a number and rhs was a Smi then strict equality cannot
4666 // succeed. Return non-equal.
4667 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4668 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4669 } else {
4670 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4671 // the runtime.
4672 __ b(ne, slow);
4673 }
4674
4675 // Lhs is a smi, rhs is a number.
4676 // r0 is Smi and r1 is heap number.
4677 __ push(lr);
4678 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4679 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4680 __ mov(r7, Operand(r0));
4681 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4682 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4683 __ pop(lr);
4684 // Fall through to both_loaded_as_doubles.
4685}
4686
4687
4688void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4689 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4690 Register lhs_exponent = exp_first ? r0 : r1;
4691 Register rhs_exponent = exp_first ? r2 : r3;
4692 Register lhs_mantissa = exp_first ? r1 : r0;
4693 Register rhs_mantissa = exp_first ? r3 : r2;
4694 Label one_is_nan, neither_is_nan;
4695
4696 Register exp_mask_reg = r5;
4697
4698 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4699 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4700 __ cmp(r4, Operand(exp_mask_reg));
4701 __ b(ne, rhs_not_nan);
4702 __ mov(r4,
4703 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4704 SetCC);
4705 __ b(ne, &one_is_nan);
4706 __ cmp(rhs_mantissa, Operand(0));
4707 __ b(ne, &one_is_nan);
4708
4709 __ bind(rhs_not_nan);
4710 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4711 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4712 __ cmp(r4, Operand(exp_mask_reg));
4713 __ b(ne, &neither_is_nan);
4714 __ mov(r4,
4715 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4716 SetCC);
4717 __ b(ne, &one_is_nan);
4718 __ cmp(lhs_mantissa, Operand(0));
4719 __ b(eq, &neither_is_nan);
4720
4721 __ bind(&one_is_nan);
4722 // NaN comparisons always fail.
4723 // Load whatever we need in r0 to make the comparison fail.
4724 if (cc == lt || cc == le) {
4725 __ mov(r0, Operand(GREATER));
4726 } else {
4727 __ mov(r0, Operand(LESS));
4728 }
4729 __ mov(pc, Operand(lr)); // Return.
4730
4731 __ bind(&neither_is_nan);
4732}
4733
4734
4735// See comment at call site.
4736static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4737 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4738 Register lhs_exponent = exp_first ? r0 : r1;
4739 Register rhs_exponent = exp_first ? r2 : r3;
4740 Register lhs_mantissa = exp_first ? r1 : r0;
4741 Register rhs_mantissa = exp_first ? r3 : r2;
4742
4743 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4744 if (cc == eq) {
4745 // Doubles are not equal unless they have the same bit pattern.
4746 // Exception: 0 and -0.
4747 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4748 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4749 // Return non-zero if the numbers are unequal.
4750 __ mov(pc, Operand(lr), LeaveCC, ne);
4751
4752 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4753 // If exponents are equal then return 0.
4754 __ mov(pc, Operand(lr), LeaveCC, eq);
4755
4756 // Exponents are unequal. The only way we can return that the numbers
4757 // are equal is if one is -0 and the other is 0. We already dealt
4758 // with the case where both are -0 or both are 0.
4759 // We start by seeing if the mantissas (that are equal) or the bottom
4760 // 31 bits of the rhs exponent are non-zero. If so we return not
4761 // equal.
4762 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4763 __ mov(r0, Operand(r4), LeaveCC, ne);
4764 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4765 // Now they are equal if and only if the lhs exponent is zero in its
4766 // low 31 bits.
4767 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4768 __ mov(pc, Operand(lr));
4769 } else {
4770 // Call a native function to do a comparison between two non-NaNs.
4771 // Call C routine that may not cause GC or other trouble.
4772 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4773 __ Jump(r5); // Tail call.
4774 }
4775}
4776
4777
4778// See comment at call site.
4779static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4780 // If either operand is a JSObject or an oddball value, then they are
4781 // not equal since their pointers are different.
4782 // There is no test for undetectability in strict equality.
4783 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4784 Label first_non_object;
4785 // Get the type of the first operand into r2 and compare it with
4786 // FIRST_JS_OBJECT_TYPE.
4787 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4788 __ b(lt, &first_non_object);
4789
4790 // Return non-zero (r0 is not zero)
4791 Label return_not_equal;
4792 __ bind(&return_not_equal);
4793 __ mov(pc, Operand(lr)); // Return.
4794
4795 __ bind(&first_non_object);
4796 // Check for oddballs: true, false, null, undefined.
4797 __ cmp(r2, Operand(ODDBALL_TYPE));
4798 __ b(eq, &return_not_equal);
4799
4800 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4801 __ b(ge, &return_not_equal);
4802
4803 // Check for oddballs: true, false, null, undefined.
4804 __ cmp(r3, Operand(ODDBALL_TYPE));
4805 __ b(eq, &return_not_equal);
4806}
4807
4808
4809// See comment at call site.
4810static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4811 Label* both_loaded_as_doubles,
4812 Label* not_heap_numbers,
4813 Label* slow) {
4814 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4815 __ b(ne, not_heap_numbers);
4816 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4817 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4818
4819 // Both are heap numbers. Load them up then jump to the code we have
4820 // for that.
4821 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4822 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4823 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4824 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4825 __ jmp(both_loaded_as_doubles);
4826}
4827
4828
4829// Fast negative check for symbol-to-symbol equality.
4830static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4831 // r2 is object type of r0.
4832 __ tst(r2, Operand(kIsNotStringMask));
4833 __ b(ne, slow);
4834 __ tst(r2, Operand(kIsSymbolMask));
4835 __ b(eq, slow);
4836 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4837 __ b(ge, slow);
4838 __ tst(r3, Operand(kIsSymbolMask));
4839 __ b(eq, slow);
4840
4841 // Both are symbols. We already checked they weren't the same pointer
4842 // so they are not equal.
4843 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4844 __ mov(pc, Operand(lr)); // Return.
4845}
4846
4847
4848// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4849// positive or negative to indicate the result of the comparison.
4850void CompareStub::Generate(MacroAssembler* masm) {
4851 Label slow; // Call builtin.
4852 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4853
4854 // NOTICE! This code is only reached after a smi-fast-case check, so
4855 // it is certain that at least one operand isn't a smi.
4856
4857 // Handle the case where the objects are identical. Either returns the answer
4858 // or goes to slow. Only falls through if the objects were not identical.
4859 EmitIdenticalObjectComparison(masm, &slow, cc_);
4860
4861 // If either is a Smi (we know that not both are), then they can only
4862 // be strictly equal if the other is a HeapNumber.
4863 ASSERT_EQ(0, kSmiTag);
4864 ASSERT_EQ(0, Smi::FromInt(0));
4865 __ and_(r2, r0, Operand(r1));
4866 __ tst(r2, Operand(kSmiTagMask));
4867 __ b(ne, &not_smis);
4868 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4869 // 1) Return the answer.
4870 // 2) Go to slow.
4871 // 3) Fall through to both_loaded_as_doubles.
4872 // 4) Jump to rhs_not_nan.
4873 // In cases 3 and 4 we have found out we were dealing with a number-number
4874 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4875 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4876
4877 __ bind(&both_loaded_as_doubles);
4878 // r0, r1, r2, r3 are the double representations of the left hand side
4879 // and the right hand side.
4880
4881 // Checks for NaN in the doubles we have loaded. Can return the answer or
4882 // fall through if neither is a NaN. Also binds rhs_not_nan.
4883 EmitNanCheck(masm, &rhs_not_nan, cc_);
4884
4885 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4886 // answer. Never falls through.
4887 EmitTwoNonNanDoubleComparison(masm, cc_);
4888
4889 __ bind(&not_smis);
4890 // At this point we know we are dealing with two different objects,
4891 // and neither of them is a Smi. The objects are in r0 and r1.
4892 if (strict_) {
4893 // This returns non-equal for some object types, or falls through if it
4894 // was not lucky.
4895 EmitStrictTwoHeapObjectCompare(masm);
4896 }
4897
4898 Label check_for_symbols;
4899 // Check for heap-number-heap-number comparison. Can jump to slow case,
4900 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4901 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4902 // In this case r2 will contain the type of r0.
4903 EmitCheckForTwoHeapNumbers(masm,
4904 &both_loaded_as_doubles,
4905 &check_for_symbols,
4906 &slow);
4907
4908 __ bind(&check_for_symbols);
4909 if (cc_ == eq) {
4910 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4911 // of r0 on entry.
4912 EmitCheckForSymbols(masm, &slow);
4913 }
4914
4915 __ bind(&slow);
4916 __ push(lr);
4917 __ push(r1);
4918 __ push(r0);
4919 // Figure out which native to call and setup the arguments.
4920 Builtins::JavaScript native;
4921 int arg_count = 1; // Not counting receiver.
4922 if (cc_ == eq) {
4923 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4924 } else {
4925 native = Builtins::COMPARE;
4926 int ncr; // NaN compare result
4927 if (cc_ == lt || cc_ == le) {
4928 ncr = GREATER;
4929 } else {
4930 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4931 ncr = LESS;
4932 }
4933 arg_count++;
4934 __ mov(r0, Operand(Smi::FromInt(ncr)));
4935 __ push(r0);
4936 }
4937
4938 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4939 // tagged as a small integer.
4940 __ mov(r0, Operand(arg_count));
4941 __ InvokeBuiltin(native, CALL_JS);
4942 __ cmp(r0, Operand(0));
4943 __ pop(pc);
4944}
4945
4946
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004947// Allocates a heap number or jumps to the label if the young space is full and
4948// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004949static void AllocateHeapNumber(
4950 MacroAssembler* masm,
4951 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004952 Register result, // The tagged address of the new heap number.
4953 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004954 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004955 // Allocate an object in the heap for the heap number and tag it as a heap
4956 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004957 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
4958 result,
4959 scratch1,
4960 scratch2,
4961 need_gc,
4962 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004963
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004964 // Get heap number map and store it in the allocated object.
4965 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
4966 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004967}
4968
4969
4970// We fall into this code if the operands were Smis, but the result was
4971// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004972// the operands were not both Smi. The operands are in r0 and r1. In order
4973// to call the C-implemented binary fp operation routines we need to end up
4974// with the double precision floating point operands in r0 and r1 (for the
4975// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004976static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4977 Label* not_smi,
4978 const Builtins::JavaScript& builtin,
4979 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004980 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004981 Label slow, slow_pop_2_first, do_the_call;
4982 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4983 // Smi-smi case (overflow).
4984 // Since both are Smis there is no heap number to overwrite, so allocate.
4985 // The new heap number is in r5. r6 and r7 are scratch.
4986 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4987 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004988 __ mov(r7, Operand(r0));
4989 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004990 __ push(lr);
4991 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4992 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4993 __ mov(r7, Operand(r1));
4994 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4995 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4996 __ pop(lr);
4997 __ jmp(&do_the_call); // Tail call. No return.
4998
4999 // We jump to here if something goes wrong (one param is not a number of any
5000 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005001 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005002 __ push(r1);
5003 __ push(r0);
5004 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005005 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005006
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005007 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005008 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005009 if (mode == NO_OVERWRITE) {
5010 // In the case where there is no chance of an overwritable float we may as
5011 // well do the allocation immediately while r0 and r1 are untouched.
5012 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5013 }
5014
5015 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005016 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005017 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5018 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005019 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005020 if (mode == OVERWRITE_RIGHT) {
5021 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5022 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005023 // Calling convention says that second double is in r2 and r3.
5024 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005025 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5026 __ jmp(&finished_loading_r0);
5027 __ bind(&r0_is_smi);
5028 if (mode == OVERWRITE_RIGHT) {
5029 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005030 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005031 }
5032 // Write Smi from r0 to r3 and r2 in double format.
5033 __ mov(r7, Operand(r0));
5034 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5035 __ push(lr);
5036 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5037 __ pop(lr);
5038 __ bind(&finished_loading_r0);
5039
5040 // Move r1 to a double in r0-r1.
5041 __ tst(r1, Operand(kSmiTagMask));
5042 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5043 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5044 __ b(ne, &slow);
5045 if (mode == OVERWRITE_LEFT) {
5046 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005047 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005048 // Calling convention says that first double is in r0 and r1.
5049 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005050 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5051 __ jmp(&finished_loading_r1);
5052 __ bind(&r1_is_smi);
5053 if (mode == OVERWRITE_LEFT) {
5054 // We can't overwrite a Smi so get address of new heap number into r5.
5055 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5056 }
5057 // Write Smi from r1 to r1 and r0 in double format.
5058 __ mov(r7, Operand(r1));
5059 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5060 __ push(lr);
5061 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5062 __ pop(lr);
5063 __ bind(&finished_loading_r1);
5064
5065 __ bind(&do_the_call);
5066 // r0: Left value (least significant part of mantissa).
5067 // r1: Left value (sign, exponent, top of mantissa).
5068 // r2: Right value (least significant part of mantissa).
5069 // r3: Right value (sign, exponent, top of mantissa).
5070 // r5: Address of heap number for result.
5071 __ push(lr); // For later.
5072 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005073 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005074 // Call C routine that may not cause GC or other trouble.
5075 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005076 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005077 __ pop(r4); // Address of heap number.
5078 __ cmp(r4, Operand(Smi::FromInt(0)));
5079 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005080 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005081#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005082 // Double returned in fp coprocessor register 0 and 1, encoded as register
5083 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5084 // substract the tag from r4.
5085 __ sub(r5, r4, Operand(kHeapObjectTag));
5086 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5087#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005088 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005089 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005090 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005091#endif
5092 __ mov(r0, Operand(r4));
5093 // And we are done.
5094 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005095}
5096
5097
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005098// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005099// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005100// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5101// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005102// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5103// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005104static void GetInt32(MacroAssembler* masm,
5105 Register source,
5106 Register dest,
5107 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005108 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005109 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005110 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005111 // Get exponent word.
5112 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5113 // Get exponent alone in scratch2.
5114 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005115 // Load dest with zero. We use this either for the final shift or
5116 // for the answer.
5117 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005118 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005119 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5120 // the exponent that we are fastest at and also the highest exponent we can
5121 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005122 const uint32_t non_smi_exponent =
5123 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5124 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005125 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5126 __ b(eq, &right_exponent);
5127 // If the exponent is higher than that then go to slow case. This catches
5128 // numbers that don't fit in a signed int32, infinities and NaNs.
5129 __ b(gt, slow);
5130
5131 // We know the exponent is smaller than 30 (biased). If it is less than
5132 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5133 // it rounds to zero.
5134 const uint32_t zero_exponent =
5135 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5136 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5137 // Dest already has a Smi zero.
5138 __ b(lt, &done);
5139 // We have a shifted exponent between 0 and 30 in scratch2.
5140 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5141 // We now have the exponent in dest. Subtract from 30 to get
5142 // how much to shift down.
5143 __ rsb(dest, dest, Operand(30));
5144
5145 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005146 // Get the top bits of the mantissa.
5147 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5148 // Put back the implicit 1.
5149 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5150 // Shift up the mantissa bits to take up the space the exponent used to take.
5151 // We just orred in the implicit bit so that took care of one and we want to
5152 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5153 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5154 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5155 // Put sign in zero flag.
5156 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005157 // Get the second half of the double. For some exponents we don't actually
5158 // need this because the bits get shifted out again, but it's probably slower
5159 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005160 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5161 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005162 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5163 // Move down according to the exponent.
5164 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005165 // Fix sign if sign bit was set.
5166 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005167 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005168}
5169
5170
5171// For bitwise ops where the inputs are not both Smis we here try to determine
5172// whether both inputs are either Smis or at least heap numbers that can be
5173// represented by a 32 bit signed value. We truncate towards zero as required
5174// by the ES spec. If this is the case we do the bitwise op and see if the
5175// result is a Smi. If so, great, otherwise we try to find a heap number to
5176// write the answer into (either by allocating or by overwriting).
5177// On entry the operands are in r0 and r1. On exit the answer is in r0.
5178void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5179 Label slow, result_not_a_smi;
5180 Label r0_is_smi, r1_is_smi;
5181 Label done_checking_r0, done_checking_r1;
5182
5183 __ tst(r1, Operand(kSmiTagMask));
5184 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5185 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5186 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005187 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005188 __ jmp(&done_checking_r1);
5189 __ bind(&r1_is_smi);
5190 __ mov(r3, Operand(r1, ASR, 1));
5191 __ bind(&done_checking_r1);
5192
5193 __ tst(r0, Operand(kSmiTagMask));
5194 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5195 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5196 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005197 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005198 __ jmp(&done_checking_r0);
5199 __ bind(&r0_is_smi);
5200 __ mov(r2, Operand(r0, ASR, 1));
5201 __ bind(&done_checking_r0);
5202
5203 // r0 and r1: Original operands (Smi or heap numbers).
5204 // r2 and r3: Signed int32 operands.
5205 switch (op_) {
5206 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5207 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5208 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5209 case Token::SAR:
5210 // Use only the 5 least significant bits of the shift count.
5211 __ and_(r2, r2, Operand(0x1f));
5212 __ mov(r2, Operand(r3, ASR, r2));
5213 break;
5214 case Token::SHR:
5215 // Use only the 5 least significant bits of the shift count.
5216 __ and_(r2, r2, Operand(0x1f));
5217 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5218 // SHR is special because it is required to produce a positive answer.
5219 // The code below for writing into heap numbers isn't capable of writing
5220 // the register as an unsigned int so we go to slow case if we hit this
5221 // case.
5222 __ b(mi, &slow);
5223 break;
5224 case Token::SHL:
5225 // Use only the 5 least significant bits of the shift count.
5226 __ and_(r2, r2, Operand(0x1f));
5227 __ mov(r2, Operand(r3, LSL, r2));
5228 break;
5229 default: UNREACHABLE();
5230 }
5231 // check that the *signed* result fits in a smi
5232 __ add(r3, r2, Operand(0x40000000), SetCC);
5233 __ b(mi, &result_not_a_smi);
5234 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5235 __ Ret();
5236
5237 Label have_to_allocate, got_a_heap_number;
5238 __ bind(&result_not_a_smi);
5239 switch (mode_) {
5240 case OVERWRITE_RIGHT: {
5241 __ tst(r0, Operand(kSmiTagMask));
5242 __ b(eq, &have_to_allocate);
5243 __ mov(r5, Operand(r0));
5244 break;
5245 }
5246 case OVERWRITE_LEFT: {
5247 __ tst(r1, Operand(kSmiTagMask));
5248 __ b(eq, &have_to_allocate);
5249 __ mov(r5, Operand(r1));
5250 break;
5251 }
5252 case NO_OVERWRITE: {
5253 // Get a new heap number in r5. r6 and r7 are scratch.
5254 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5255 }
5256 default: break;
5257 }
5258 __ bind(&got_a_heap_number);
5259 // r2: Answer as signed int32.
5260 // r5: Heap number to write answer into.
5261
5262 // Nothing can go wrong now, so move the heap number to r0, which is the
5263 // result.
5264 __ mov(r0, Operand(r5));
5265
5266 // Tail call that writes the int32 in r2 to the heap number in r0, using
5267 // r3 as scratch. r0 is preserved and returned.
5268 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5269 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5270
5271 if (mode_ != NO_OVERWRITE) {
5272 __ bind(&have_to_allocate);
5273 // Get a new heap number in r5. r6 and r7 are scratch.
5274 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5275 __ jmp(&got_a_heap_number);
5276 }
5277
5278 // If all else failed then we go to the runtime system.
5279 __ bind(&slow);
5280 __ push(r1); // restore stack
5281 __ push(r0);
5282 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5283 switch (op_) {
5284 case Token::BIT_OR:
5285 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5286 break;
5287 case Token::BIT_AND:
5288 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5289 break;
5290 case Token::BIT_XOR:
5291 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5292 break;
5293 case Token::SAR:
5294 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5295 break;
5296 case Token::SHR:
5297 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5298 break;
5299 case Token::SHL:
5300 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5301 break;
5302 default:
5303 UNREACHABLE();
5304 }
5305}
5306
5307
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005308// Can we multiply by x with max two shifts and an add.
5309// This answers yes to all integers from 2 to 10.
5310static bool IsEasyToMultiplyBy(int x) {
5311 if (x < 2) return false; // Avoid special cases.
5312 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5313 if (IsPowerOf2(x)) return true; // Simple shift.
5314 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5315 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5316 return false;
5317}
5318
5319
5320// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5321// Source and destination may be the same register. This routine does
5322// not set carry and overflow the way a mul instruction would.
5323static void MultiplyByKnownInt(MacroAssembler* masm,
5324 Register source,
5325 Register destination,
5326 int known_int) {
5327 if (IsPowerOf2(known_int)) {
5328 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5329 } else if (PopCountLessThanEqual2(known_int)) {
5330 int first_bit = BitPosition(known_int);
5331 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5332 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5333 if (first_bit != 0) {
5334 __ mov(destination, Operand(destination, LSL, first_bit));
5335 }
5336 } else {
5337 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5338 int the_bit = BitPosition(known_int + 1);
5339 __ rsb(destination, source, Operand(source, LSL, the_bit));
5340 }
5341}
5342
5343
5344// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5345// a register for the cases where it doesn't know a good trick, and may deliver
5346// a result that needs shifting.
5347static void MultiplyByKnownInt2(
5348 MacroAssembler* masm,
5349 Register result,
5350 Register source,
5351 Register known_int_register, // Smi tagged.
5352 int known_int,
5353 int* required_shift) { // Including Smi tag shift
5354 switch (known_int) {
5355 case 3:
5356 __ add(result, source, Operand(source, LSL, 1));
5357 *required_shift = 1;
5358 break;
5359 case 5:
5360 __ add(result, source, Operand(source, LSL, 2));
5361 *required_shift = 1;
5362 break;
5363 case 6:
5364 __ add(result, source, Operand(source, LSL, 1));
5365 *required_shift = 2;
5366 break;
5367 case 7:
5368 __ rsb(result, source, Operand(source, LSL, 3));
5369 *required_shift = 1;
5370 break;
5371 case 9:
5372 __ add(result, source, Operand(source, LSL, 3));
5373 *required_shift = 1;
5374 break;
5375 case 10:
5376 __ add(result, source, Operand(source, LSL, 2));
5377 *required_shift = 2;
5378 break;
5379 default:
5380 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5381 __ mul(result, source, known_int_register);
5382 *required_shift = 0;
5383 }
5384}
5385
5386
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005387void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5388 // r1 : x
5389 // r0 : y
5390 // result : r0
5391
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005392 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5393 // tell us that.
5394 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5395
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005396 switch (op_) {
5397 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005398 Label not_smi;
5399 // Fast path.
5400 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005401 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005402 __ b(ne, &not_smi);
5403 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5404 // Return if no overflow.
5405 __ Ret(vc);
5406 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5407
5408 HandleBinaryOpSlowCases(masm,
5409 &not_smi,
5410 Builtins::ADD,
5411 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005412 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005413 break;
5414 }
5415
5416 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005417 Label not_smi;
5418 // Fast path.
5419 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005420 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005421 __ b(ne, &not_smi);
5422 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5423 // Return if no overflow.
5424 __ Ret(vc);
5425 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5426
5427 HandleBinaryOpSlowCases(masm,
5428 &not_smi,
5429 Builtins::SUB,
5430 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005431 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005432 break;
5433 }
5434
5435 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005436 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005437 ASSERT(kSmiTag == 0); // adjust code below
5438 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005439 __ b(ne, &not_smi);
5440 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005441 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005442 // Do multiplication
5443 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5444 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005445 __ mov(ip, Operand(r3, ASR, 31));
5446 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5447 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005448 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005449 __ tst(r3, Operand(r3));
5450 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005451 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005452 // We need -0 if we were multiplying a negative number with 0 to get 0.
5453 // We know one of them was zero.
5454 __ add(r2, r0, Operand(r1), SetCC);
5455 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5456 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5457 // Slow case. We fall through here if we multiplied a negative number
5458 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005459 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005460
5461 HandleBinaryOpSlowCases(masm,
5462 &not_smi,
5463 Builtins::MUL,
5464 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005465 mode_);
5466 break;
5467 }
5468
5469 case Token::DIV:
5470 case Token::MOD: {
5471 Label not_smi;
5472 if (specialized_on_rhs_) {
5473 Label smi_is_unsuitable;
5474 __ BranchOnNotSmi(r1, &not_smi);
5475 if (IsPowerOf2(constant_rhs_)) {
5476 if (op_ == Token::MOD) {
5477 __ and_(r0,
5478 r1,
5479 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5480 SetCC);
5481 // We now have the answer, but if the input was negative we also
5482 // have the sign bit. Our work is done if the result is
5483 // positive or zero:
5484 __ Ret(pl);
5485 // A mod of a negative left hand side must return a negative number.
5486 // Unfortunately if the answer is 0 then we must return -0. And we
5487 // already optimistically trashed r0 so we may need to restore it.
5488 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5489 // Next two instructions are conditional on the answer being -0.
5490 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5491 __ b(eq, &smi_is_unsuitable);
5492 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5493 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5494 } else {
5495 ASSERT(op_ == Token::DIV);
5496 __ tst(r1,
5497 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5498 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5499 int shift = 0;
5500 int d = constant_rhs_;
5501 while ((d & 1) == 0) {
5502 d >>= 1;
5503 shift++;
5504 }
5505 __ mov(r0, Operand(r1, LSR, shift));
5506 __ bic(r0, r0, Operand(kSmiTagMask));
5507 }
5508 } else {
5509 // Not a power of 2.
5510 __ tst(r1, Operand(0x80000000u));
5511 __ b(ne, &smi_is_unsuitable);
5512 // Find a fixed point reciprocal of the divisor so we can divide by
5513 // multiplying.
5514 double divisor = 1.0 / constant_rhs_;
5515 int shift = 32;
5516 double scale = 4294967296.0; // 1 << 32.
5517 uint32_t mul;
5518 // Maximise the precision of the fixed point reciprocal.
5519 while (true) {
5520 mul = static_cast<uint32_t>(scale * divisor);
5521 if (mul >= 0x7fffffff) break;
5522 scale *= 2.0;
5523 shift++;
5524 }
5525 mul++;
5526 __ mov(r2, Operand(mul));
5527 __ umull(r3, r2, r2, r1);
5528 __ mov(r2, Operand(r2, LSR, shift - 31));
5529 // r2 is r1 / rhs. r2 is not Smi tagged.
5530 // r0 is still the known rhs. r0 is Smi tagged.
5531 // r1 is still the unkown lhs. r1 is Smi tagged.
5532 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5533 // r4 = r2 * r0.
5534 MultiplyByKnownInt2(masm,
5535 r4,
5536 r2,
5537 r0,
5538 constant_rhs_,
5539 &required_r4_shift);
5540 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5541 if (op_ == Token::DIV) {
5542 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5543 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5544 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5545 } else {
5546 ASSERT(op_ == Token::MOD);
5547 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5548 }
5549 }
5550 __ Ret();
5551 __ bind(&smi_is_unsuitable);
5552 } else {
5553 __ jmp(&not_smi);
5554 }
5555 HandleBinaryOpSlowCases(masm,
5556 &not_smi,
5557 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5558 op_,
5559 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005560 break;
5561 }
5562
5563 case Token::BIT_OR:
5564 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005565 case Token::BIT_XOR:
5566 case Token::SAR:
5567 case Token::SHR:
5568 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005569 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005570 ASSERT(kSmiTag == 0); // adjust code below
5571 __ tst(r2, Operand(kSmiTagMask));
5572 __ b(ne, &slow);
5573 switch (op_) {
5574 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5575 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5576 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005577 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005578 // Remove tags from right operand.
5579 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5580 // Use only the 5 least significant bits of the shift count.
5581 __ and_(r2, r2, Operand(0x1f));
5582 __ mov(r0, Operand(r1, ASR, r2));
5583 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005584 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005585 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005586 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005587 // Remove tags from operands. We can't do this on a 31 bit number
5588 // because then the 0s get shifted into bit 30 instead of bit 31.
5589 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5590 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5591 // Use only the 5 least significant bits of the shift count.
5592 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005593 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005594 // Unsigned shift is not allowed to produce a negative number, so
5595 // check the sign bit and the sign bit after Smi tagging.
5596 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005597 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005598 // Smi tag result.
5599 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005600 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005601 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005602 // Remove tags from operands.
5603 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5604 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5605 // Use only the 5 least significant bits of the shift count.
5606 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005607 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005608 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005609 __ add(r2, r3, Operand(0x40000000), SetCC);
5610 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005611 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005612 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005613 default: UNREACHABLE();
5614 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005615 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005616 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005617 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005618 break;
5619 }
5620
5621 default: UNREACHABLE();
5622 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005623 // This code should be unreachable.
5624 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005625}
5626
5627
5628void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005629 // Do tail-call to runtime routine. Runtime routines expect at least one
5630 // argument, so give it a Smi.
5631 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005632 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00005633 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005634
5635 __ StubReturn(1);
5636}
5637
5638
5639void UnarySubStub::Generate(MacroAssembler* masm) {
5640 Label undo;
5641 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005642 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005643
5644 // Enter runtime system if the value is not a smi.
5645 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005646 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005647
5648 // Enter runtime system if the value of the expression is zero
5649 // to make sure that we switch between 0 and -0.
5650 __ cmp(r0, Operand(0));
5651 __ b(eq, &slow);
5652
5653 // The value of the expression is a smi that is not zero. Try
5654 // optimistic subtraction '0 - value'.
5655 __ rsb(r1, r0, Operand(0), SetCC);
5656 __ b(vs, &slow);
5657
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005658 __ mov(r0, Operand(r1)); // Set r0 to result.
5659 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005660
5661 // Enter runtime system.
5662 __ bind(&slow);
5663 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005664 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005665 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5666
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005667 __ bind(&not_smi);
5668 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5669 __ b(ne, &slow);
5670 // r0 is a heap number. Get a new heap number in r1.
5671 if (overwrite_) {
5672 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5673 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5674 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5675 } else {
5676 AllocateHeapNumber(masm, &slow, r1, r2, r3);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005677 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005678 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005679 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005680 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5681 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5682 __ mov(r0, Operand(r1));
5683 }
5684 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005685}
5686
5687
ager@chromium.orga1645e22009-09-09 19:27:10 +00005688int CEntryStub::MinorKey() {
5689 ASSERT(result_size_ <= 2);
5690 // Result returned in r0 or r0+r1 by default.
5691 return 0;
5692}
5693
5694
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005695void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005696 // r0 holds the exception.
5697
5698 // Adjust this code if not the case.
5699 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5700
5701 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005702 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5703 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005704
5705 // Restore the next handler and frame pointer, discard handler state.
5706 ASSERT(StackHandlerConstants::kNextOffset == 0);
5707 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005708 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005709 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5710 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5711
5712 // Before returning we restore the context from the frame pointer if
5713 // not NULL. The frame pointer is NULL in the exception handler of a
5714 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005715 __ cmp(fp, Operand(0));
5716 // Set cp to NULL if fp is NULL.
5717 __ mov(cp, Operand(0), LeaveCC, eq);
5718 // Restore cp otherwise.
5719 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005720#ifdef DEBUG
5721 if (FLAG_debug_code) {
5722 __ mov(lr, Operand(pc));
5723 }
5724#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005725 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005726 __ pop(pc);
5727}
5728
5729
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005730void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
5731 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005732 // Adjust this code if not the case.
5733 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5734
5735 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005736 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005737 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005738
5739 // Unwind the handlers until the ENTRY handler is found.
5740 Label loop, done;
5741 __ bind(&loop);
5742 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005743 const int kStateOffset = StackHandlerConstants::kStateOffset;
5744 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005745 __ cmp(r2, Operand(StackHandler::ENTRY));
5746 __ b(eq, &done);
5747 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005748 const int kNextOffset = StackHandlerConstants::kNextOffset;
5749 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005750 __ jmp(&loop);
5751 __ bind(&done);
5752
5753 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005754 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005755 __ pop(r2);
5756 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005757
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005758 if (type == OUT_OF_MEMORY) {
5759 // Set external caught exception to false.
5760 ExternalReference external_caught(Top::k_external_caught_exception_address);
5761 __ mov(r0, Operand(false));
5762 __ mov(r2, Operand(external_caught));
5763 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005764
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005765 // Set pending exception and r0 to out of memory exception.
5766 Failure* out_of_memory = Failure::OutOfMemoryException();
5767 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5768 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5769 __ str(r0, MemOperand(r2));
5770 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005771
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005772 // Stack layout at this point. See also StackHandlerConstants.
5773 // sp -> state (ENTRY)
5774 // fp
5775 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005776
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005777 // Discard handler state (r2 is not used) and restore frame pointer.
5778 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5779 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5780 // Before returning we restore the context from the frame pointer if
5781 // not NULL. The frame pointer is NULL in the exception handler of a
5782 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005783 __ cmp(fp, Operand(0));
5784 // Set cp to NULL if fp is NULL.
5785 __ mov(cp, Operand(0), LeaveCC, eq);
5786 // Restore cp otherwise.
5787 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005788#ifdef DEBUG
5789 if (FLAG_debug_code) {
5790 __ mov(lr, Operand(pc));
5791 }
5792#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005793 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005794 __ pop(pc);
5795}
5796
5797
5798void CEntryStub::GenerateCore(MacroAssembler* masm,
5799 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005800 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005801 Label* throw_out_of_memory_exception,
5802 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005803 bool do_gc,
5804 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005805 // r0: result parameter for PerformGC, if any
5806 // r4: number of arguments including receiver (C callee-saved)
5807 // r5: pointer to builtin function (C callee-saved)
5808 // r6: pointer to the first argument (C callee-saved)
5809
5810 if (do_gc) {
5811 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005812 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5813 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005814 }
5815
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005816 ExternalReference scope_depth =
5817 ExternalReference::heap_always_allocate_scope_depth();
5818 if (always_allocate) {
5819 __ mov(r0, Operand(scope_depth));
5820 __ ldr(r1, MemOperand(r0));
5821 __ add(r1, r1, Operand(1));
5822 __ str(r1, MemOperand(r0));
5823 }
5824
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005825 // Call C built-in.
5826 // r0 = argc, r1 = argv
5827 __ mov(r0, Operand(r4));
5828 __ mov(r1, Operand(r6));
5829
5830 // TODO(1242173): To let the GC traverse the return address of the exit
5831 // frames, we need to know where the return address is. Right now,
5832 // we push it on the stack to be able to find it again, but we never
5833 // restore from it in case of changes, which makes it impossible to
5834 // support moving the C entry code stub. This should be fixed, but currently
5835 // this is OK because the CEntryStub gets generated so early in the V8 boot
5836 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005837 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5838 masm->push(lr);
5839 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005840
5841 if (always_allocate) {
5842 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5843 // though (contain the result).
5844 __ mov(r2, Operand(scope_depth));
5845 __ ldr(r3, MemOperand(r2));
5846 __ sub(r3, r3, Operand(1));
5847 __ str(r3, MemOperand(r2));
5848 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005849
5850 // check for failure result
5851 Label failure_returned;
5852 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5853 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5854 __ add(r2, r0, Operand(1));
5855 __ tst(r2, Operand(kFailureTagMask));
5856 __ b(eq, &failure_returned);
5857
5858 // Exit C frame and return.
5859 // r0:r1: result
5860 // sp: stack pointer
5861 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005862 __ LeaveExitFrame(frame_type);
5863
5864 // check if we should retry or throw exception
5865 Label retry;
5866 __ bind(&failure_returned);
5867 ASSERT(Failure::RETRY_AFTER_GC == 0);
5868 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5869 __ b(eq, &retry);
5870
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005871 // Special handling of out of memory exceptions.
5872 Failure* out_of_memory = Failure::OutOfMemoryException();
5873 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5874 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005875
5876 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005877 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005878 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005879 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005880 __ ldr(r0, MemOperand(ip));
5881 __ str(r3, MemOperand(ip));
5882
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005883 // Special handling of termination exceptions which are uncatchable
5884 // by javascript code.
5885 __ cmp(r0, Operand(Factory::termination_exception()));
5886 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005887
5888 // Handle normal exception.
5889 __ jmp(throw_normal_exception);
5890
5891 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5892}
5893
5894
5895void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5896 // Called from JavaScript; parameters are on stack as if calling JS function
5897 // r0: number of arguments including receiver
5898 // r1: pointer to builtin function
5899 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005900 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005901 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005902
5903 // NOTE: Invocations of builtins may return failure objects
5904 // instead of a proper result. The builtin entry handles
5905 // this by performing a garbage collection and retrying the
5906 // builtin once.
5907
5908 StackFrame::Type frame_type = is_debug_break
5909 ? StackFrame::EXIT_DEBUG
5910 : StackFrame::EXIT;
5911
5912 // Enter the exit frame that transitions from JavaScript to C++.
5913 __ EnterExitFrame(frame_type);
5914
5915 // r4: number of arguments (C callee-saved)
5916 // r5: pointer to builtin function (C callee-saved)
5917 // r6: pointer to first argument (C callee-saved)
5918
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005919 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005920 Label throw_termination_exception;
5921 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005922
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005923 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005924 GenerateCore(masm,
5925 &throw_normal_exception,
5926 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005927 &throw_out_of_memory_exception,
5928 frame_type,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005929 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005930 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005931
5932 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005933 GenerateCore(masm,
5934 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005935 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005936 &throw_out_of_memory_exception,
5937 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005938 true,
5939 false);
5940
5941 // Do full GC and retry runtime call one final time.
5942 Failure* failure = Failure::InternalError();
5943 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5944 GenerateCore(masm,
5945 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005946 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005947 &throw_out_of_memory_exception,
5948 frame_type,
5949 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005950 true);
5951
5952 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005953 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
5954
5955 __ bind(&throw_termination_exception);
5956 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005957
5958 __ bind(&throw_normal_exception);
5959 GenerateThrowTOS(masm);
5960}
5961
5962
5963void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5964 // r0: code entry
5965 // r1: function
5966 // r2: receiver
5967 // r3: argc
5968 // [sp+0]: argv
5969
5970 Label invoke, exit;
5971
5972 // Called from C, so do not pop argc and args on exit (preserve sp)
5973 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005974 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005975 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5976
5977 // Get address of argv, see stm above.
5978 // r0: code entry
5979 // r1: function
5980 // r2: receiver
5981 // r3: argc
5982 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5983 __ ldr(r4, MemOperand(r4)); // argv
5984
5985 // Push a frame with special values setup to mark it as an entry frame.
5986 // r0: code entry
5987 // r1: function
5988 // r2: receiver
5989 // r3: argc
5990 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005991 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005992 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5993 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005994 __ mov(r6, Operand(Smi::FromInt(marker)));
5995 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5996 __ ldr(r5, MemOperand(r5));
5997 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5998
5999 // Setup frame pointer for the frame to be pushed.
6000 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6001
6002 // Call a faked try-block that does the invoke.
6003 __ bl(&invoke);
6004
6005 // Caught exception: Store result (exception) in the pending
6006 // exception field in the JSEnv and return a failure sentinel.
6007 // Coming in here the fp will be invalid because the PushTryHandler below
6008 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006009 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006010 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006011 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006012 __ b(&exit);
6013
6014 // Invoke: Link this frame into the handler chain.
6015 __ bind(&invoke);
6016 // Must preserve r0-r4, r5-r7 are available.
6017 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006018 // If an exception not caught by another handler occurs, this handler
6019 // returns control to the code after the bl(&invoke) above, which
6020 // restores all kCalleeSaved registers (including cp and fp) to their
6021 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006022
6023 // Clear any pending exceptions.
6024 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6025 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006026 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006027 __ str(r5, MemOperand(ip));
6028
6029 // Invoke the function by calling through JS entry trampoline builtin.
6030 // Notice that we cannot store a reference to the trampoline code directly in
6031 // this stub, because runtime stubs are not traversed when doing GC.
6032
6033 // Expected registers by Builtins::JSEntryTrampoline
6034 // r0: code entry
6035 // r1: function
6036 // r2: receiver
6037 // r3: argc
6038 // r4: argv
6039 if (is_construct) {
6040 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6041 __ mov(ip, Operand(construct_entry));
6042 } else {
6043 ExternalReference entry(Builtins::JSEntryTrampoline);
6044 __ mov(ip, Operand(entry));
6045 }
6046 __ ldr(ip, MemOperand(ip)); // deref address
6047
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006048 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6049 // macro for the add instruction because we don't want the coverage tool
6050 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006051 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006052 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006053
6054 // Unlink this frame from the handler chain. When reading the
6055 // address of the next handler, there is no need to use the address
6056 // displacement since the current stack pointer (sp) points directly
6057 // to the stack handler.
6058 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6059 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6060 __ str(r3, MemOperand(ip));
6061 // No need to restore registers
6062 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6063
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006064
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006065 __ bind(&exit); // r0 holds result
6066 // Restore the top frame descriptors from the stack.
6067 __ pop(r3);
6068 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6069 __ str(r3, MemOperand(ip));
6070
6071 // Reset the stack to the callee saved registers.
6072 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6073
6074 // Restore callee-saved registers and return.
6075#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006076 if (FLAG_debug_code) {
6077 __ mov(lr, Operand(pc));
6078 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006079#endif
6080 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6081}
6082
6083
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006084// This stub performs an instanceof, calling the builtin function if
6085// necessary. Uses r1 for the object, r0 for the function that it may
6086// be an instance of (these are fetched from the stack).
6087void InstanceofStub::Generate(MacroAssembler* masm) {
6088 // Get the object - slow case for smis (we may need to throw an exception
6089 // depending on the rhs).
6090 Label slow, loop, is_instance, is_not_instance;
6091 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6092 __ BranchOnSmi(r0, &slow);
6093
6094 // Check that the left hand is a JS object and put map in r3.
6095 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6096 __ b(lt, &slow);
6097 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6098 __ b(gt, &slow);
6099
6100 // Get the prototype of the function (r4 is result, r2 is scratch).
6101 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6102 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6103
6104 // Check that the function prototype is a JS object.
6105 __ BranchOnSmi(r4, &slow);
6106 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6107 __ b(lt, &slow);
6108 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6109 __ b(gt, &slow);
6110
6111 // Register mapping: r3 is object map and r4 is function prototype.
6112 // Get prototype of object into r2.
6113 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6114
6115 // Loop through the prototype chain looking for the function prototype.
6116 __ bind(&loop);
6117 __ cmp(r2, Operand(r4));
6118 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006119 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6120 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006121 __ b(eq, &is_not_instance);
6122 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6123 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6124 __ jmp(&loop);
6125
6126 __ bind(&is_instance);
6127 __ mov(r0, Operand(Smi::FromInt(0)));
6128 __ pop();
6129 __ pop();
6130 __ mov(pc, Operand(lr)); // Return.
6131
6132 __ bind(&is_not_instance);
6133 __ mov(r0, Operand(Smi::FromInt(1)));
6134 __ pop();
6135 __ pop();
6136 __ mov(pc, Operand(lr)); // Return.
6137
6138 // Slow-case. Tail call builtin.
6139 __ bind(&slow);
6140 __ mov(r0, Operand(1)); // Arg count without receiver.
6141 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6142}
6143
6144
ager@chromium.org7c537e22008-10-16 08:43:32 +00006145void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006146 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006147 Label adaptor;
6148 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6149 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006150 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006151 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006152
ager@chromium.org7c537e22008-10-16 08:43:32 +00006153 // Nothing to do: The formal number of parameters has already been
6154 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006155 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006156
ager@chromium.org7c537e22008-10-16 08:43:32 +00006157 // Arguments adaptor case: Read the arguments length from the
6158 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006159 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006160 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006161 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006162}
6163
6164
ager@chromium.org7c537e22008-10-16 08:43:32 +00006165void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6166 // The displacement is the offset of the last parameter (if any)
6167 // relative to the frame pointer.
6168 static const int kDisplacement =
6169 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170
ager@chromium.org7c537e22008-10-16 08:43:32 +00006171 // Check that the key is a smi.
6172 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006173 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006174
ager@chromium.org7c537e22008-10-16 08:43:32 +00006175 // Check if the calling frame is an arguments adaptor frame.
6176 Label adaptor;
6177 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6178 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006179 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006180 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006181
ager@chromium.org7c537e22008-10-16 08:43:32 +00006182 // Check index against formal parameters count limit passed in
6183 // through register eax. Use unsigned comparison to get negative
6184 // check for free.
6185 __ cmp(r1, r0);
6186 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006187
ager@chromium.org7c537e22008-10-16 08:43:32 +00006188 // Read the argument from the stack and return it.
6189 __ sub(r3, r0, r1);
6190 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6191 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006192 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006193
6194 // Arguments adaptor case: Check index against actual arguments
6195 // limit found in the arguments adaptor frame. Use unsigned
6196 // comparison to get negative check for free.
6197 __ bind(&adaptor);
6198 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6199 __ cmp(r1, r0);
6200 __ b(cs, &slow);
6201
6202 // Read the argument from the adaptor frame and return it.
6203 __ sub(r3, r0, r1);
6204 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6205 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006206 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006207
6208 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6209 // by calling the runtime system.
6210 __ bind(&slow);
6211 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006212 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006213}
6214
6215
6216void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6217 // Check if the calling frame is an arguments adaptor frame.
6218 Label runtime;
6219 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6220 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006221 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006222 __ b(ne, &runtime);
6223
6224 // Patch the arguments.length and the parameters pointer.
6225 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6226 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6227 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6228 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6229 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6230
6231 // Do the runtime call to allocate the arguments object.
6232 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006233 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006234}
6235
6236
6237void CallFunctionStub::Generate(MacroAssembler* masm) {
6238 Label slow;
6239 // Get the function to call from the stack.
6240 // function, receiver [, arguments]
6241 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6242
6243 // Check that the function is really a JavaScript function.
6244 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006245 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006246 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006247 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006248 __ b(ne, &slow);
6249
6250 // Fast-case: Invoke the function now.
6251 // r1: pushed function
6252 ParameterCount actual(argc_);
6253 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6254
6255 // Slow-case: Non-function called.
6256 __ bind(&slow);
6257 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006258 __ mov(r2, Operand(0));
6259 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6260 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6261 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006262}
6263
6264
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006265int CompareStub::MinorKey() {
6266 // Encode the two parameters in a unique 16 bit value.
6267 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6268 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6269}
6270
6271
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006272#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006274} } // namespace v8::internal