blob: 9ef879a5f59deaac7914eb8a56d543bd2583027f [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");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001191 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 Variable* var = node->proxy()->var();
1193 ASSERT(var != NULL); // must have been resolved
1194 Slot* slot = var->slot();
1195
1196 // If it was not possible to allocate the variable at compile time,
1197 // we need to "declare" it at runtime to make sure it actually
1198 // exists in the local context.
1199 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1200 // Variables with a "LOOKUP" slot were introduced as non-locals
1201 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001202 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001204 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001205 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001206 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 // Declaration nodes are always declared in only two modes.
1208 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1209 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001210 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001211 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 // Push initial value, if any.
1213 // Note: For variables we must not push an initial value (such as
1214 // 'undefined') because we may have a (legal) redeclaration and we
1215 // must not destroy the current value.
1216 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001217 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001218 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001222 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001223 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001226 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001227 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 return;
1229 }
1230
1231 ASSERT(!var->is_global());
1232
1233 // If we have a function or a constant, we need to initialize the variable.
1234 Expression* val = NULL;
1235 if (node->mode() == Variable::CONST) {
1236 val = new Literal(Factory::the_hole_value());
1237 } else {
1238 val = node->fun(); // NULL if we don't have a function
1239 }
1240
1241 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001242 {
1243 // Set initial value.
1244 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001245 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001246 target.SetValue(NOT_CONST_INIT);
1247 // The reference is removed from the stack (preserving TOS) when
1248 // it goes out of scope.
1249 }
1250 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001251 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001253 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254}
1255
1256
ager@chromium.org7c537e22008-10-16 08:43:32 +00001257void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001258#ifdef DEBUG
1259 int original_height = frame_->height();
1260#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001261 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001263 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 Expression* expression = node->expression();
1265 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001266 LoadAndSpill(expression);
1267 frame_->Drop();
1268 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269}
1270
1271
ager@chromium.org7c537e22008-10-16 08:43:32 +00001272void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001273#ifdef DEBUG
1274 int original_height = frame_->height();
1275#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001276 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001278 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001280 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281}
1282
1283
ager@chromium.org7c537e22008-10-16 08:43:32 +00001284void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001285#ifdef DEBUG
1286 int original_height = frame_->height();
1287#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001288 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001290 // Generate different code depending on which parts of the if statement
1291 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 bool has_then_stm = node->HasThenStatement();
1293 bool has_else_stm = node->HasElseStatement();
1294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001295 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001297 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001299 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001300 JumpTarget then;
1301 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001303 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1304 &then, &else_, true);
1305 if (frame_ != NULL) {
1306 Branch(false, &else_);
1307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001309 if (frame_ != NULL || then.is_linked()) {
1310 then.Bind();
1311 VisitAndSpill(node->then_statement());
1312 }
1313 if (frame_ != NULL) {
1314 exit.Jump();
1315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001317 if (else_.is_linked()) {
1318 else_.Bind();
1319 VisitAndSpill(node->else_statement());
1320 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321
1322 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001323 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001325 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001327 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1328 &then, &exit, true);
1329 if (frame_ != NULL) {
1330 Branch(false, &exit);
1331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001333 if (frame_ != NULL || then.is_linked()) {
1334 then.Bind();
1335 VisitAndSpill(node->then_statement());
1336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337
1338 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001339 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001341 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1344 &exit, &else_, true);
1345 if (frame_ != NULL) {
1346 Branch(true, &exit);
1347 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001349 if (frame_ != NULL || else_.is_linked()) {
1350 else_.Bind();
1351 VisitAndSpill(node->else_statement());
1352 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353
1354 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001355 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 ASSERT(!has_then_stm && !has_else_stm);
1357 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001358 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1359 &exit, &exit, false);
1360 if (frame_ != NULL) {
1361 if (has_cc()) {
1362 cc_reg_ = al;
1363 } else {
1364 frame_->Drop();
1365 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 }
1367 }
1368
1369 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001370 if (exit.is_linked()) {
1371 exit.Bind();
1372 }
1373 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374}
1375
1376
ager@chromium.org7c537e22008-10-16 08:43:32 +00001377void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001378 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001380 CodeForStatementPosition(node);
1381 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382}
1383
1384
ager@chromium.org7c537e22008-10-16 08:43:32 +00001385void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001386 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001388 CodeForStatementPosition(node);
1389 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390}
1391
1392
ager@chromium.org7c537e22008-10-16 08:43:32 +00001393void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001394 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001396
ager@chromium.org4af710e2009-09-15 12:20:11 +00001397 CodeForStatementPosition(node);
1398 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001399 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001400 frame_->EmitPop(r0);
1401 function_return_.Jump();
1402 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 // Pop the result from the frame and prepare the frame for
1404 // returning thus making it easier to merge.
1405 frame_->EmitPop(r0);
1406 frame_->PrepareForReturn();
1407
1408 function_return_.Jump();
1409 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410}
1411
1412
ager@chromium.org7c537e22008-10-16 08:43:32 +00001413void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414#ifdef DEBUG
1415 int original_height = frame_->height();
1416#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001417 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 CodeForStatementPosition(node);
1420 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001421 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001422 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001423 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001425 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001426#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001427 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001428 __ cmp(r0, Operand(cp));
1429 verified_true.Branch(eq);
1430 __ stop("PushContext: r0 is expected to be the same as cp");
1431 verified_true.Bind();
1432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001434 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436}
1437
1438
ager@chromium.org7c537e22008-10-16 08:43:32 +00001439void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001440#ifdef DEBUG
1441 int original_height = frame_->height();
1442#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001443 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 // Pop context.
1447 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1448 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001449 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451}
1452
1453
ager@chromium.org7c537e22008-10-16 08:43:32 +00001454void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455#ifdef DEBUG
1456 int original_height = frame_->height();
1457#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001458 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001461 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001463 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001464
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001465 JumpTarget next_test;
1466 JumpTarget fall_through;
1467 JumpTarget default_entry;
1468 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 ZoneList<CaseClause*>* cases = node->cases();
1470 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001471 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472
1473 for (int i = 0; i < length; i++) {
1474 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001476 // Remember the default clause and compile it at the end.
1477 default_clause = clause;
1478 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 }
1480
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001481 Comment cmnt(masm_, "[ Case clause");
1482 // Compile the test.
1483 next_test.Bind();
1484 next_test.Unuse();
1485 // Duplicate TOS.
1486 __ ldr(r0, frame_->Top());
1487 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001488 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001489 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001490
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 // Before entering the body from the test, remove the switch value from
1492 // the stack.
1493 frame_->Drop();
1494
1495 // Label the body so that fall through is enabled.
1496 if (i > 0 && cases->at(i - 1)->is_default()) {
1497 default_exit.Bind();
1498 } else {
1499 fall_through.Bind();
1500 fall_through.Unuse();
1501 }
1502 VisitStatementsAndSpill(clause->statements());
1503
1504 // If control flow can fall through from the body, jump to the next body
1505 // or the end of the statement.
1506 if (frame_ != NULL) {
1507 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1508 default_entry.Jump();
1509 } else {
1510 fall_through.Jump();
1511 }
1512 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 }
1514
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001515 // The final "test" removes the switch value.
1516 next_test.Bind();
1517 frame_->Drop();
1518
1519 // If there is a default clause, compile it.
1520 if (default_clause != NULL) {
1521 Comment cmnt(masm_, "[ Default clause");
1522 default_entry.Bind();
1523 VisitStatementsAndSpill(default_clause->statements());
1524 // If control flow can fall out of the default and there is a case after
1525 // it, jup to that case's body.
1526 if (frame_ != NULL && default_exit.is_bound()) {
1527 default_exit.Jump();
1528 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001529 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001531 if (fall_through.is_linked()) {
1532 fall_through.Bind();
1533 }
1534
1535 if (node->break_target()->is_linked()) {
1536 node->break_target()->Bind();
1537 }
1538 node->break_target()->Unuse();
1539 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540}
1541
1542
ager@chromium.org7c537e22008-10-16 08:43:32 +00001543void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544#ifdef DEBUG
1545 int original_height = frame_->height();
1546#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001547 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001549 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001550 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001552 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1553 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1555 if (node->cond() == NULL) {
1556 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1557 info = ALWAYS_TRUE;
1558 } else {
1559 Literal* lit = node->cond()->AsLiteral();
1560 if (lit != NULL) {
1561 if (lit->IsTrue()) {
1562 info = ALWAYS_TRUE;
1563 } else if (lit->IsFalse()) {
1564 info = ALWAYS_FALSE;
1565 }
1566 }
1567 }
1568
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001569 switch (node->type()) {
1570 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001571 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001573 // Label the top of the loop for the backward CFG edge. If the test
1574 // is always true we can use the continue target, and if the test is
1575 // always false there is no need.
1576 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001577 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001578 node->continue_target()->Bind();
1579 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001580 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001581 } else {
1582 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001583 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001584 body.Bind();
1585 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001588 VisitAndSpill(node->body());
1589
1590 // Compile the test.
1591 if (info == ALWAYS_TRUE) {
1592 if (has_valid_frame()) {
1593 // If control can fall off the end of the body, jump back to the
1594 // top.
1595 node->continue_target()->Jump();
1596 }
1597 } else if (info == ALWAYS_FALSE) {
1598 // If we have a continue in the body, we only have to bind its jump
1599 // target.
1600 if (node->continue_target()->is_linked()) {
1601 node->continue_target()->Bind();
1602 }
1603 } else {
1604 ASSERT(info == DONT_KNOW);
1605 // We have to compile the test expression if it can be reached by
1606 // control flow falling out of the body or via continue.
1607 if (node->continue_target()->is_linked()) {
1608 node->continue_target()->Bind();
1609 }
1610 if (has_valid_frame()) {
1611 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1612 &body, node->break_target(), true);
1613 if (has_valid_frame()) {
1614 // A invalid frame here indicates that control did not
1615 // fall out of the test expression.
1616 Branch(true, &body);
1617 }
1618 }
1619 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001621 }
1622
1623 case LoopStatement::WHILE_LOOP: {
1624 // If the test is never true and has no side effects there is no need
1625 // to compile the test or body.
1626 if (info == ALWAYS_FALSE) break;
1627
1628 // Label the top of the loop with the continue target for the backward
1629 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001630 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001631 node->continue_target()->Bind();
1632
1633 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001634 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1636 &body, node->break_target(), true);
1637 if (has_valid_frame()) {
1638 // A NULL frame indicates that control did not fall out of the
1639 // test expression.
1640 Branch(false, node->break_target());
1641 }
1642 if (has_valid_frame() || body.is_linked()) {
1643 body.Bind();
1644 }
1645 }
1646
1647 if (has_valid_frame()) {
1648 CheckStack(); // TODO(1222600): ignore if body contains calls.
1649 VisitAndSpill(node->body());
1650
1651 // If control flow can fall out of the body, jump back to the top.
1652 if (has_valid_frame()) {
1653 node->continue_target()->Jump();
1654 }
1655 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001657 }
1658
1659 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001660 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001661
1662 if (node->init() != NULL) {
1663 VisitAndSpill(node->init());
1664 }
1665
1666 // There is no need to compile the test or body.
1667 if (info == ALWAYS_FALSE) break;
1668
1669 // If there is no update statement, label the top of the loop with the
1670 // continue target, otherwise with the loop target.
1671 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001672 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001673 node->continue_target()->Bind();
1674 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001675 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001676 loop.Bind();
1677 }
1678
1679 // If the test is always true, there is no need to compile it.
1680 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001681 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001682 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1683 &body, node->break_target(), true);
1684 if (has_valid_frame()) {
1685 Branch(false, node->break_target());
1686 }
1687 if (has_valid_frame() || body.is_linked()) {
1688 body.Bind();
1689 }
1690 }
1691
1692 if (has_valid_frame()) {
1693 CheckStack(); // TODO(1222600): ignore if body contains calls.
1694 VisitAndSpill(node->body());
1695
1696 if (node->next() == NULL) {
1697 // If there is no update statement and control flow can fall out
1698 // of the loop, jump directly to the continue label.
1699 if (has_valid_frame()) {
1700 node->continue_target()->Jump();
1701 }
1702 } else {
1703 // If there is an update statement and control flow can reach it
1704 // via falling out of the body of the loop or continuing, we
1705 // compile the update statement.
1706 if (node->continue_target()->is_linked()) {
1707 node->continue_target()->Bind();
1708 }
1709 if (has_valid_frame()) {
1710 // Record source position of the statement as this code which is
1711 // after the code for the body actually belongs to the loop
1712 // statement and not the body.
1713 CodeForStatementPosition(node);
1714 VisitAndSpill(node->next());
1715 loop.Jump();
1716 }
1717 }
1718 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001720 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 }
1722
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001723 if (node->break_target()->is_linked()) {
1724 node->break_target()->Bind();
1725 }
1726 node->continue_target()->Unuse();
1727 node->break_target()->Unuse();
1728 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729}
1730
1731
ager@chromium.org7c537e22008-10-16 08:43:32 +00001732void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001733#ifdef DEBUG
1734 int original_height = frame_->height();
1735#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001736 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001738 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001740 JumpTarget primitive;
1741 JumpTarget jsobject;
1742 JumpTarget fixed_array;
1743 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1744 JumpTarget end_del_check;
1745 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746
1747 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749
1750 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1751 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001753 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1754 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001755 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001756 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1757 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759
1760 // Stack layout in body:
1761 // [iteration counter (Smi)]
1762 // [length of array]
1763 // [FixedArray]
1764 // [Map or 0]
1765 // [Object]
1766
1767 // Check if enumerable is already a JSObject
1768 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001769 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001770 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001771 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 primitive.Bind();
1774 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001775 Result arg_count(r0);
1776 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 frame_->EmitPush(r0); // duplicate the object being enumerated
1782 frame_->EmitPush(r0);
1783 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784
1785 // If we got a Map, we can do a fast modification check.
1786 // Otherwise, we got a FixedArray, and we have to do a slow check.
1787 __ mov(r2, Operand(r0));
1788 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001789 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1790 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
1793 // Get enum cache
1794 __ mov(r1, Operand(r0));
1795 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1796 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1797 __ ldr(r2,
1798 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1799
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001800 frame_->EmitPush(r0); // map
1801 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001802 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001804 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001805 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001806 frame_->EmitPush(r0);
1807 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001811 frame_->EmitPush(r1); // insert 0 in place of Map
1812 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813
1814 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001815 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001818 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820
1821 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001823 // sp[0] : index
1824 // sp[1] : array/enum cache length
1825 // sp[2] : array or enum cache
1826 // sp[3] : 0 or map
1827 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 // Grab the current frame's height for the break and continue
1829 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001830 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1831 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001833 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1834 __ ldr(r1, frame_->ElementAt(1)); // load the length
1835 __ cmp(r0, Operand(r1)); // compare to the array length
1836 node->break_target()->Branch(hs);
1837
1838 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1843 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1844
1845 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 // Check if this (still) matches the map of the enumerable.
1848 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001849 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1851 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853
1854 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1856 frame_->EmitPush(r0);
1857 frame_->EmitPush(r3); // push entry
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001858 Result arg_count_reg(r0);
1859 __ mov(r0, Operand(1));
1860 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2);
1861 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
1863 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001864 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1865 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001866 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 end_del_check.Bind();
1869 // Store the entry in the 'each' expression and take another spin in the
1870 // loop. r3: i'th entry of the enum cache (or string there of)
1871 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 { Reference each(this, node->each());
1873 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001874 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001875 __ ldr(r0, frame_->ElementAt(each.size()));
1876 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001877 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001878 // If the reference was to a slot we rely on the convenient property
1879 // that it doesn't matter whether a value (eg, r3 pushed above) is
1880 // right on top of or right underneath a zero-sized reference.
1881 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001882 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001883 // It's safe to pop the value lying on top of the reference before
1884 // unloading the reference itself (which preserves the top of stack,
1885 // ie, now the topmost value of the non-zero sized reference), since
1886 // we will discard the top of stack after unloading the reference
1887 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001889 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 }
1891 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001892 // Discard the i'th entry pushed above or else the remainder of the
1893 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 frame_->Drop();
1895
1896 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 VisitAndSpill(node->body());
1899
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001900 // Next. Reestablish a spilled frame in case we are coming here via
1901 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001903 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 frame_->EmitPop(r0);
1905 __ add(r0, r0, Operand(Smi::FromInt(1)));
1906 frame_->EmitPush(r0);
1907 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001909 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1910 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001912 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913
1914 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001915 exit.Bind();
1916 node->continue_target()->Unuse();
1917 node->break_target()->Unuse();
1918 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919}
1920
1921
ager@chromium.org7c537e22008-10-16 08:43:32 +00001922void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923#ifdef DEBUG
1924 int original_height = frame_->height();
1925#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001926 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001930 JumpTarget try_block;
1931 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001933 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
1937 // Store the caught exception in the catch variable.
1938 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001939 ASSERT(ref.is_slot());
1940 // Here we make use of the convenient property that it doesn't matter
1941 // whether a value is immediately on top of or underneath a zero-sized
1942 // reference.
1943 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 }
1945
1946 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 VisitStatementsAndSpill(node->catch_block()->statements());
1950 if (frame_ != NULL) {
1951 exit.Jump();
1952 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953
1954
1955 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001958 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1959 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001961 // Shadow the labels for all escapes from the try block, including
1962 // returns. During shadowing, the original label is hidden as the
1963 // LabelShadow and operations on the original actually affect the
1964 // shadowing label.
1965 //
1966 // We should probably try to unify the escaping labels and the return
1967 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 int nof_escapes = node->escaping_targets()->length();
1969 List<ShadowTarget*> shadows(1 + nof_escapes);
1970
1971 // Add the shadow target for the function return.
1972 static const int kReturnShadowIndex = 0;
1973 shadows.Add(new ShadowTarget(&function_return_));
1974 bool function_return_was_shadowed = function_return_is_shadowed_;
1975 function_return_is_shadowed_ = true;
1976 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1977
1978 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001980 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 }
1982
1983 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001984 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985
1986 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001987 // After shadowing stops, the original labels are unshadowed and the
1988 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989 bool has_unlinks = false;
1990 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 has_unlinks = has_unlinks || shadows[i]->is_linked();
1993 }
1994 function_return_is_shadowed_ = function_return_was_shadowed;
1995
1996 // Get an external reference to the handler address.
1997 ExternalReference handler_address(Top::k_handler_address);
1998
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 // If we can fall off the end of the try block, unlink from try chain.
2000 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002001 // The next handler address is on top of the frame. Unlink from
2002 // the handler list and drop the rest of this handler from the
2003 // frame.
2004 ASSERT(StackHandlerConstants::kNextOffset == 0);
2005 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002006 __ mov(r3, Operand(handler_address));
2007 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002008 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 if (has_unlinks) {
2010 exit.Jump();
2011 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 }
2013
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002014 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 // jumped to. Deallocate each shadow target.
2016 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002018 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002019 shadows[i]->Bind();
2020 // Because we can be jumping here (to spilled code) from unspilled
2021 // code, we need to reestablish a spilled frame at this block.
2022 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 // Reload sp from the top handler, because some statements that we
2025 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002028 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002030 ASSERT(StackHandlerConstants::kNextOffset == 0);
2031 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002033 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2036 frame_->PrepareForReturn();
2037 }
2038 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 }
2040 }
2041
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 exit.Bind();
2043 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044}
2045
2046
ager@chromium.org7c537e22008-10-16 08:43:32 +00002047void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048#ifdef DEBUG
2049 int original_height = frame_->height();
2050#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002051 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002053 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054
2055 // State: Used to keep track of reason for entering the finally
2056 // block. Should probably be extended to hold information for
2057 // break/continue from within the try block.
2058 enum { FALLING, THROWING, JUMPING };
2059
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002060 JumpTarget try_block;
2061 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002063 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 // In case of thrown exceptions, this is where we continue.
2067 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002068 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069
2070 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2074 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002076 // Shadow the labels for all escapes from the try block, including
2077 // returns. Shadowing hides the original label as the LabelShadow and
2078 // operations on the original actually affect the shadowing label.
2079 //
2080 // We should probably try to unify the escaping labels and the return
2081 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 int nof_escapes = node->escaping_targets()->length();
2083 List<ShadowTarget*> shadows(1 + nof_escapes);
2084
2085 // Add the shadow target for the function return.
2086 static const int kReturnShadowIndex = 0;
2087 shadows.Add(new ShadowTarget(&function_return_));
2088 bool function_return_was_shadowed = function_return_is_shadowed_;
2089 function_return_is_shadowed_ = true;
2090 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2091
2092 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002094 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 }
2096
2097 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002100 // Stop the introduced shadowing and count the number of required unlinks.
2101 // After shadowing stops, the original labels are unshadowed and the
2102 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002104 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 shadows[i]->StopShadowing();
2106 if (shadows[i]->is_linked()) nof_unlinks++;
2107 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 // Get an external reference to the handler address.
2111 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 // If we can fall off the end of the try block, unlink from the try
2114 // chain and set the state on the frame to FALLING.
2115 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002116 // The next handler address is on top of the frame.
2117 ASSERT(StackHandlerConstants::kNextOffset == 0);
2118 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 __ mov(r3, Operand(handler_address));
2120 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002121 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122
2123 // Fake a top of stack value (unneeded when FALLING) and set the
2124 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002125 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 frame_->EmitPush(r0);
2127 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2128 if (nof_unlinks > 0) {
2129 finally_block.Jump();
2130 }
2131 }
2132
2133 // Generate code to unlink and set the state for the (formerly)
2134 // shadowing targets that have been jumped to.
2135 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 // If we have come from the shadowed return, the return value is
2138 // in (a non-refcounted reference to) r0. We must preserve it
2139 // until it is pushed.
2140 //
2141 // Because we can be jumping here (to spilled code) from
2142 // unspilled code, we need to reestablish a spilled frame at
2143 // this block.
2144 shadows[i]->Bind();
2145 frame_->SpillAll();
2146
2147 // Reload sp from the top handler, because some statements that
2148 // we break from (eg, for...in) may have left stuff on the
2149 // stack.
2150 __ mov(r3, Operand(handler_address));
2151 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002152 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002153
2154 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002155 // handler address is currently on top of the frame.
2156 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 frame_->EmitPop(r1);
2158 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002159 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160
2161 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002162 // If this label shadowed the function return, materialize the
2163 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002165 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002167 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 }
2170 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002171 if (--nof_unlinks > 0) {
2172 // If this is not the last unlink block, jump around the next.
2173 finally_block.Jump();
2174 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 }
2176 }
2177
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002181 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002183
2184 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 // and the state - while evaluating the finally block.
2186 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002190 if (has_valid_frame()) {
2191 // Restore state and return value or faked TOS.
2192 frame_->EmitPop(r2);
2193 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 }
2195
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 // Generate code to jump to the right destination for all used
2197 // formerly shadowing targets. Deallocate each shadow target.
2198 for (int i = 0; i < shadows.length(); i++) {
2199 if (has_valid_frame() && shadows[i]->is_bound()) {
2200 JumpTarget* original = shadows[i]->other_target();
2201 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2202 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002203 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002204 skip.Branch(ne);
2205 frame_->PrepareForReturn();
2206 original->Jump();
2207 skip.Bind();
2208 } else {
2209 original->Branch(eq);
2210 }
2211 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002212 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002214 if (has_valid_frame()) {
2215 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002216 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2218 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 // Rethrow exception.
2221 frame_->EmitPush(r0);
2222 frame_->CallRuntime(Runtime::kReThrow, 1);
2223
2224 // Done.
2225 exit.Bind();
2226 }
2227 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228}
2229
2230
ager@chromium.org7c537e22008-10-16 08:43:32 +00002231void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002232#ifdef DEBUG
2233 int original_height = frame_->height();
2234#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002235 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002237 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002238#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002239 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002240#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002241 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243}
2244
2245
ager@chromium.org7c537e22008-10-16 08:43:32 +00002246void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002247 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 ASSERT(boilerplate->IsBoilerplate());
2249
2250 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002251 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002252 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253
2254 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002255 frame_->EmitPush(cp);
2256 frame_->CallRuntime(Runtime::kNewClosure, 2);
2257 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258}
2259
2260
ager@chromium.org7c537e22008-10-16 08:43:32 +00002261void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002262#ifdef DEBUG
2263 int original_height = frame_->height();
2264#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002265 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 Comment cmnt(masm_, "[ FunctionLiteral");
2267
2268 // Build the function boilerplate and instantiate it.
2269 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002270 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 if (HasStackOverflow()) {
2272 ASSERT(frame_->height() == original_height);
2273 return;
2274 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002276 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277}
2278
2279
ager@chromium.org7c537e22008-10-16 08:43:32 +00002280void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002282#ifdef DEBUG
2283 int original_height = frame_->height();
2284#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002285 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2287 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002288 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289}
2290
2291
ager@chromium.org7c537e22008-10-16 08:43:32 +00002292void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002293#ifdef DEBUG
2294 int original_height = frame_->height();
2295#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002296 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002298 JumpTarget then;
2299 JumpTarget else_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002300 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2301 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002302 if (has_valid_frame()) {
2303 Branch(false, &else_);
2304 }
2305 if (has_valid_frame() || then.is_linked()) {
2306 then.Bind();
2307 LoadAndSpill(node->then_expression(), typeof_state());
2308 }
2309 if (else_.is_linked()) {
2310 JumpTarget exit;
2311 if (has_valid_frame()) exit.Jump();
2312 else_.Bind();
2313 LoadAndSpill(node->else_expression(), typeof_state());
2314 if (exit.is_linked()) exit.Bind();
2315 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002316 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317}
2318
2319
ager@chromium.org7c537e22008-10-16 08:43:32 +00002320void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002321 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002322 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002323 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002325 JumpTarget slow;
2326 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002327
2328 // Generate fast-case code for variables that might be shadowed by
2329 // eval-introduced variables. Eval is used a lot without
2330 // introducing variables. In those cases, we do not want to
2331 // perform a runtime call for all variables in the scope
2332 // containing the eval.
2333 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2334 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 // If there was no control flow to slow, we can exit early.
2336 if (!slow.is_linked()) {
2337 frame_->EmitPush(r0);
2338 return;
2339 }
2340
2341 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002342
2343 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2344 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2345 // Only generate the fast case for locals that rewrite to slots.
2346 // This rules out argument loads.
2347 if (potential_slot != NULL) {
2348 __ ldr(r0,
2349 ContextSlotOperandCheckExtensions(potential_slot,
2350 r1,
2351 r2,
2352 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002353 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002354 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2355 __ cmp(r0, ip);
2356 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002357 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002359 // ContextSlotOperandCheckExtensions so we have to jump around
2360 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002361 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002362 }
2363 }
2364
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 slow.Bind();
2366 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002367 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369
ager@chromium.org7c537e22008-10-16 08:43:32 +00002370 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002372 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002373 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002375
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 done.Bind();
2377 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378
2379 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002380 // Note: We would like to keep the assert below, but it fires because of
2381 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002382 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002384 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002385 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002387 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002388 // Const slots may contain 'the hole' value (the constant hasn't been
2389 // initialized yet) which needs to be converted into the 'undefined'
2390 // value.
2391 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002392 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002393 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2394 __ cmp(r0, ip);
2395 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002396 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 }
2398 }
2399}
2400
2401
ager@chromium.org381abbb2009-02-25 13:23:22 +00002402void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2403 TypeofState typeof_state,
2404 Register tmp,
2405 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002406 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002407 // Check that no extension objects have been created by calls to
2408 // eval from the current scope to the global scope.
2409 Register context = cp;
2410 Scope* s = scope();
2411 while (s != NULL) {
2412 if (s->num_heap_slots() > 0) {
2413 if (s->calls_eval()) {
2414 // Check that extension is NULL.
2415 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2416 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002417 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002418 }
2419 // Load next context in chain.
2420 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2421 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2422 context = tmp;
2423 }
2424 // If no outer scope calls eval, we do not need to check more
2425 // context extensions.
2426 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2427 s = s->outer_scope();
2428 }
2429
2430 if (s->is_eval_scope()) {
2431 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002432 if (!context.is(tmp)) {
2433 __ mov(tmp, Operand(context));
2434 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002435 __ bind(&next);
2436 // Terminate at global context.
2437 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002438 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2439 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002440 __ b(eq, &fast);
2441 // Check that extension is NULL.
2442 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2443 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002445 // Load next context in chain.
2446 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2447 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2448 __ b(&next);
2449 __ bind(&fast);
2450 }
2451
2452 // All extension objects were empty and it is safe to use a global
2453 // load IC call.
2454 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2455 // Load the global object.
2456 LoadGlobal();
2457 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002458 Result name(r2);
2459 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002460 // Call IC stub.
2461 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002462 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002463 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002465 }
2466
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 // Drop the global object. The result is in r0.
2468 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002469}
2470
2471
ager@chromium.org7c537e22008-10-16 08:43:32 +00002472void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473#ifdef DEBUG
2474 int original_height = frame_->height();
2475#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002476 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002477 Comment cmnt(masm_, "[ Slot");
2478 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002479 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002480}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481
ager@chromium.org7c537e22008-10-16 08:43:32 +00002482
2483void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484#ifdef DEBUG
2485 int original_height = frame_->height();
2486#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002487 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002488 Comment cmnt(masm_, "[ VariableProxy");
2489
2490 Variable* var = node->var();
2491 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002492 if (expr != NULL) {
2493 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002495 ASSERT(var->is_global());
2496 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002497 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500}
2501
2502
ager@chromium.org7c537e22008-10-16 08:43:32 +00002503void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504#ifdef DEBUG
2505 int original_height = frame_->height();
2506#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002507 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002509 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002510 frame_->EmitPush(r0);
2511 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512}
2513
2514
ager@chromium.org7c537e22008-10-16 08:43:32 +00002515void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002516#ifdef DEBUG
2517 int original_height = frame_->height();
2518#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002519 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520 Comment cmnt(masm_, "[ RexExp Literal");
2521
2522 // Retrieve the literal array and check the allocated entry.
2523
2524 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002525 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526
2527 // Load the literals array of the function.
2528 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2529
2530 // Load the literal at the ast saved index.
2531 int literal_offset =
2532 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2533 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2534
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002535 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002536 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2537 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002538 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539
2540 // If the entry is undefined we call the runtime system to computed
2541 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002543 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002544 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002545 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002546 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002547 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 frame_->EmitPush(r0);
2549 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002550 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002552 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 frame_->EmitPush(r2);
2555 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556}
2557
2558
2559// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002560// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561// Each created boilerplate is stored in the JSFunction and they are
2562// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002565 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 private:
2572 ObjectLiteral* node_;
2573};
2574
2575
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002576void DeferredObjectLiteral::Generate() {
2577 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002579 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002582 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002584 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002585 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002587 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002588 __ push(r0);
2589 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2590 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592}
2593
2594
ager@chromium.org7c537e22008-10-16 08:43:32 +00002595void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002596#ifdef DEBUG
2597 int original_height = frame_->height();
2598#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002599 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 Comment cmnt(masm_, "[ ObjectLiteral");
2601
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002602 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
2604 // Retrieve the literal array and check the allocated entry.
2605
2606 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002607 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608
2609 // Load the literals array of the function.
2610 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2611
2612 // Load the literal at the ast saved index.
2613 int literal_offset =
2614 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2615 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2616
2617 // Check whether we need to materialize the object literal boilerplate.
2618 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002619 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2620 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002621 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002622 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623
2624 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002625 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002628 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2629 if (node->depth() == 1) {
2630 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2631 }
2632 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002633 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002634 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635
2636 for (int i = 0; i < node->properties()->length(); i++) {
2637 ObjectLiteral::Property* property = node->properties()->at(i);
2638 Literal* key = property->key();
2639 Expression* value = property->value();
2640 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002641 case ObjectLiteral::Property::CONSTANT:
2642 break;
2643 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2644 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2645 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 case ObjectLiteral::Property::COMPUTED: // fall through
2647 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002648 frame_->EmitPush(r0); // dup the result
2649 LoadAndSpill(key);
2650 LoadAndSpill(value);
2651 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002652 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002653 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 break;
2655 }
2656 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002657 frame_->EmitPush(r0);
2658 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002659 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660 frame_->EmitPush(r0);
2661 LoadAndSpill(value);
2662 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002663 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 break;
2665 }
2666 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667 frame_->EmitPush(r0);
2668 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002669 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670 frame_->EmitPush(r0);
2671 LoadAndSpill(value);
2672 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002673 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 break;
2675 }
2676 }
2677 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 ASSERT(frame_->height() == original_height + 1);
2679}
2680
2681
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002682// This deferred code stub will be used for creating the boilerplate
2683// by calling Runtime_CreateArrayLiteralBoilerplate.
2684// Each created boilerplate is stored in the JSFunction and they are
2685// therefore context dependent.
2686class DeferredArrayLiteral: public DeferredCode {
2687 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002688 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002689 set_comment("[ DeferredArrayLiteral");
2690 }
2691
2692 virtual void Generate();
2693
2694 private:
2695 ArrayLiteral* node_;
2696};
2697
2698
2699void DeferredArrayLiteral::Generate() {
2700 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002701
2702 // If the entry is undefined we call the runtime system to computed
2703 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002704 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002705 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002706 // Literal index (1).
2707 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002708 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002709 // Constant properties (2).
2710 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002711 __ push(r0);
2712 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2713 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002714 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002715}
2716
2717
ager@chromium.org7c537e22008-10-16 08:43:32 +00002718void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002719#ifdef DEBUG
2720 int original_height = frame_->height();
2721#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002722 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002724
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002725 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002726
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002727 // Retrieve the literal array and check the allocated entry.
2728
2729 // Load the function of this activation.
2730 __ ldr(r1, frame_->Function());
2731
2732 // Load the literals array of the function.
2733 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2734
2735 // Load the literal at the ast saved index.
2736 int literal_offset =
2737 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2738 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2739
2740 // Check whether we need to materialize the object literal boilerplate.
2741 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002742 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2743 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002744 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002745 deferred->BindExit();
2746
2747 // Push the object literal boilerplate.
2748 frame_->EmitPush(r2);
2749
2750 // Clone the boilerplate object.
2751 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2752 if (node->depth() == 1) {
2753 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2754 }
2755 frame_->CallRuntime(clone_function_id, 1);
2756 frame_->EmitPush(r0); // save the result
2757 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002758
2759 // Generate code to set the elements in the array that are not
2760 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 for (int i = 0; i < node->values()->length(); i++) {
2762 Expression* value = node->values()->at(i);
2763
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002764 // If value is a literal the property value is already set in the
2765 // boilerplate object.
2766 if (value->AsLiteral() != NULL) continue;
2767 // If value is a materialized literal the property value is already set
2768 // in the boilerplate object if it is simple.
2769 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002771 // The property must be set by generated code.
2772 LoadAndSpill(value);
2773 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002775 // Fetch the object literal.
2776 __ ldr(r1, frame_->Top());
2777 // Get the elements array.
2778 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002780 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002781 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002782 __ str(r0, FieldMemOperand(r1, offset));
2783
2784 // Update the write barrier for the array address.
2785 __ mov(r3, Operand(offset));
2786 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002788 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789}
2790
2791
ager@chromium.org32912102009-01-16 10:38:43 +00002792void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002793#ifdef DEBUG
2794 int original_height = frame_->height();
2795#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002796 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002797 // Call runtime routine to allocate the catch extension object and
2798 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002799 Comment cmnt(masm_, "[ CatchExtensionObject");
2800 LoadAndSpill(node->key());
2801 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002802 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2803 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002804 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002805}
2806
2807
ager@chromium.org7c537e22008-10-16 08:43:32 +00002808void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002809#ifdef DEBUG
2810 int original_height = frame_->height();
2811#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002812 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002814 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002815
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002816 { Reference target(this, node->target());
2817 if (target.is_illegal()) {
2818 // Fool the virtual frame into thinking that we left the assignment's
2819 // value on the frame.
2820 __ mov(r0, Operand(Smi::FromInt(0)));
2821 frame_->EmitPush(r0);
2822 ASSERT(frame_->height() == original_height + 1);
2823 return;
2824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002826 if (node->op() == Token::ASSIGN ||
2827 node->op() == Token::INIT_VAR ||
2828 node->op() == Token::INIT_CONST) {
2829 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002830
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002832 // +=, *= and similar binary assignments.
2833 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002834 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2835 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002836 bool overwrite =
2837 (node->value()->AsBinaryOperation() != NULL &&
2838 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002840 SmiOperation(node->binary_op(),
2841 literal->handle(),
2842 false,
2843 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844 frame_->EmitPush(r0);
2845
2846 } else {
2847 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002848 GenericBinaryOperation(node->binary_op(),
2849 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002850 frame_->EmitPush(r0);
2851 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002854 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2855 if (var != NULL &&
2856 (var->mode() == Variable::CONST) &&
2857 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2858 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002859
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002861 CodeForSourcePosition(node->position());
2862 if (node->op() == Token::INIT_CONST) {
2863 // Dynamic constant initializations must use the function context
2864 // and initialize the actual constant declared. Dynamic variable
2865 // initializations are simply assignments and use SetValue.
2866 target.SetValue(CONST_INIT);
2867 } else {
2868 target.SetValue(NOT_CONST_INIT);
2869 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870 }
2871 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002872 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873}
2874
2875
ager@chromium.org7c537e22008-10-16 08:43:32 +00002876void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002877#ifdef DEBUG
2878 int original_height = frame_->height();
2879#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002880 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881 Comment cmnt(masm_, "[ Throw");
2882
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002883 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002884 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002885 frame_->CallRuntime(Runtime::kThrow, 1);
2886 frame_->EmitPush(r0);
2887 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888}
2889
2890
ager@chromium.org7c537e22008-10-16 08:43:32 +00002891void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002892#ifdef DEBUG
2893 int original_height = frame_->height();
2894#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002895 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002897
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002898 { Reference property(this, node);
2899 property.GetValueAndSpill(typeof_state());
2900 }
2901 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902}
2903
2904
ager@chromium.org7c537e22008-10-16 08:43:32 +00002905void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002906#ifdef DEBUG
2907 int original_height = frame_->height();
2908#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002909 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 Comment cmnt(masm_, "[ Call");
2911
2912 ZoneList<Expression*>* args = node->arguments();
2913
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002914 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 // Standard function call.
2916
2917 // Check if the function is a variable or a property.
2918 Expression* function = node->expression();
2919 Variable* var = function->AsVariableProxy()->AsVariable();
2920 Property* property = function->AsProperty();
2921
2922 // ------------------------------------------------------------------------
2923 // Fast-case: Use inline caching.
2924 // ---
2925 // According to ECMA-262, section 11.2.3, page 44, the function to call
2926 // must be resolved after the arguments have been evaluated. The IC code
2927 // automatically handles this by loading the arguments before the function
2928 // is resolved in cache misses (this also holds for megamorphic calls).
2929 // ------------------------------------------------------------------------
2930
2931 if (var != NULL && !var->is_this() && var->is_global()) {
2932 // ----------------------------------
2933 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2934 // ----------------------------------
2935
2936 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002937 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002938 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002939
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002940 // Pass the global object as the receiver and let the IC stub
2941 // patch the stack to use the global proxy as 'this' in the
2942 // invoked function.
2943 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944
2945 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 int arg_count = args->length();
2947 for (int i = 0; i < arg_count; i++) {
2948 LoadAndSpill(args->at(i));
2949 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950
2951 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002952 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2953 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002954 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
2956 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002957 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 frame_->Drop();
2960 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961
2962 } else if (var != NULL && var->slot() != NULL &&
2963 var->slot()->type() == Slot::LOOKUP) {
2964 // ----------------------------------
2965 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2966 // ----------------------------------
2967
2968 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002969 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002970 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002971 frame_->EmitPush(r0);
2972 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 // r0: slot value; r1: receiver
2974
2975 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002976 frame_->EmitPush(r0); // function
2977 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002978
2979 // Call the function.
2980 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002981 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982
2983 } else if (property != NULL) {
2984 // Check if the key is a literal string.
2985 Literal* literal = property->key()->AsLiteral();
2986
2987 if (literal != NULL && literal->handle()->IsSymbol()) {
2988 // ------------------------------------------------------------------
2989 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2990 // ------------------------------------------------------------------
2991
2992 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002993 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002994 frame_->EmitPush(r0);
2995 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002996
2997 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002998 int arg_count = args->length();
2999 for (int i = 0; i < arg_count; i++) {
3000 LoadAndSpill(args->at(i));
3001 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002
3003 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003004 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3005 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003006 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003007 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003008 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009
3010 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003011 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003012
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014
3015 } else {
3016 // -------------------------------------------
3017 // JavaScript example: 'array[index](1, 2, 3)'
3018 // -------------------------------------------
3019
3020 // Load the function to call from the property through a reference.
3021 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003022 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023
3024 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003025 if (property->is_synthetic()) {
3026 LoadGlobalReceiver(r0);
3027 } else {
3028 __ ldr(r0, frame_->ElementAt(ref.size()));
3029 frame_->EmitPush(r0);
3030 }
3031
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032 // Call the function.
3033 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003034 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003035 }
3036
3037 } else {
3038 // ----------------------------------
3039 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3040 // ----------------------------------
3041
3042 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003043 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003044
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003045 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003046 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 // Call the function.
3049 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003050 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003051 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053}
3054
3055
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003056void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057#ifdef DEBUG
3058 int original_height = frame_->height();
3059#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003060 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003061 Comment cmnt(masm_, "[ CallEval");
3062
3063 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3064 // the function we need to call and the receiver of the call.
3065 // Then we call the resolved function using the given arguments.
3066
3067 ZoneList<Expression*>* args = node->arguments();
3068 Expression* function = node->expression();
3069
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003070 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003071
3072 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003073 LoadAndSpill(function);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003074 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 frame_->EmitPush(r2); // Slot for receiver
3076 int arg_count = args->length();
3077 for (int i = 0; i < arg_count; i++) {
3078 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003079 }
3080
3081 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003082 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3083 frame_->EmitPush(r1);
3084 if (arg_count > 0) {
3085 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3086 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003087 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003089 }
3090
3091 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003093
3094 // Touch up stack with the right values for the function and the receiver.
3095 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003097 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003098 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003099
3100 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003101 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003102
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003103 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3104 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003105 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003106
3107 __ ldr(cp, frame_->Context());
3108 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 frame_->Drop();
3110 frame_->EmitPush(r0);
3111 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003112}
3113
3114
ager@chromium.org7c537e22008-10-16 08:43:32 +00003115void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003116#ifdef DEBUG
3117 int original_height = frame_->height();
3118#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003119 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003121 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003122
3123 // According to ECMA-262, section 11.2.2, page 44, the function
3124 // expression in new calls must be evaluated before the
3125 // arguments. This is different from ordinary calls, where the
3126 // actual function to call is resolved after the arguments have been
3127 // evaluated.
3128
3129 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003130 // receiver. There is no need to use the global proxy here because
3131 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003133 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134
3135 // Push the arguments ("left-to-right") on the stack.
3136 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 int arg_count = args->length();
3138 for (int i = 0; i < arg_count; i++) {
3139 LoadAndSpill(args->at(i));
3140 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141
mads.s.ager31e71382008-08-13 09:32:07 +00003142 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003143 Result num_args(r0);
3144 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003145
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003146 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003147 Result function(r1);
3148 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150 // Call the construct call builtin that handles allocation and
3151 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003152 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003153 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003154 frame_->CallCodeObject(ic,
3155 RelocInfo::CONSTRUCT_CALL,
3156 &num_args,
3157 &function,
3158 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003159
3160 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003161 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003162 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163}
3164
3165
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003166void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3167 VirtualFrame::SpilledScope spilled_scope;
3168 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003169 JumpTarget leave, null, function, non_function_constructor;
3170
3171 // Load the object into r0.
3172 LoadAndSpill(args->at(0));
3173 frame_->EmitPop(r0);
3174
3175 // If the object is a smi, we return null.
3176 __ tst(r0, Operand(kSmiTagMask));
3177 null.Branch(eq);
3178
3179 // Check that the object is a JS object but take special care of JS
3180 // functions to make sure they have 'Function' as their class.
3181 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3182 null.Branch(lt);
3183
3184 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3185 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3186 // LAST_JS_OBJECT_TYPE.
3187 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3188 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3189 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3190 function.Branch(eq);
3191
3192 // Check if the constructor in the map is a function.
3193 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3194 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3195 non_function_constructor.Branch(ne);
3196
3197 // The r0 register now contains the constructor function. Grab the
3198 // instance class name from there.
3199 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3200 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003201 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003202 leave.Jump();
3203
3204 // Functions have class 'Function'.
3205 function.Bind();
3206 __ mov(r0, Operand(Factory::function_class_symbol()));
3207 frame_->EmitPush(r0);
3208 leave.Jump();
3209
3210 // Objects with a non-function constructor have class 'Object'.
3211 non_function_constructor.Bind();
3212 __ mov(r0, Operand(Factory::Object_symbol()));
3213 frame_->EmitPush(r0);
3214 leave.Jump();
3215
3216 // Non-JS objects have class null.
3217 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003218 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003219 frame_->EmitPush(r0);
3220
3221 // All done.
3222 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003223}
3224
3225
ager@chromium.org7c537e22008-10-16 08:43:32 +00003226void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003227 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003229 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003230 LoadAndSpill(args->at(0));
3231 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003232 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003233 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003234 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003235 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3236 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003237 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 // Load the value.
3239 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003240 leave.Bind();
3241 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003242}
3243
3244
ager@chromium.org7c537e22008-10-16 08:43:32 +00003245void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003246 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003248 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 LoadAndSpill(args->at(0)); // Load the object.
3250 LoadAndSpill(args->at(1)); // Load the value.
3251 frame_->EmitPop(r0); // r0 contains value
3252 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 // if (object->IsSmi()) return object.
3254 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003256 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3257 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 // Store the value.
3260 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3261 // Update the write barrier.
3262 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3263 __ RecordWrite(r1, r2, r3);
3264 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003265 leave.Bind();
3266 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267}
3268
3269
ager@chromium.org7c537e22008-10-16 08:43:32 +00003270void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003271 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273 LoadAndSpill(args->at(0));
3274 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003275 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003276 cc_reg_ = eq;
3277}
3278
3279
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003280void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003281 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003282 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3283 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003284#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003285 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 LoadAndSpill(args->at(1));
3287 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003288 __ CallRuntime(Runtime::kLog, 2);
3289 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003290#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003291 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003293}
3294
3295
ager@chromium.org7c537e22008-10-16 08:43:32 +00003296void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003297 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003298 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003299 LoadAndSpill(args->at(0));
3300 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003301 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003302 cc_reg_ = eq;
3303}
3304
3305
kasper.lund7276f142008-07-30 08:49:36 +00003306// This should generate code that performs a charCodeAt() call or returns
3307// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3308// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003309void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003310 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003311 ASSERT(args->length() == 2);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003312 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003314}
3315
3316
ager@chromium.org7c537e22008-10-16 08:43:32 +00003317void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003318 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003319 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003320 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003321 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003322 // We need the CC bits to come out as not_equal in the case where the
3323 // object is a smi. This can't be done with the usual test opcode so
3324 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003325 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003326 __ and_(r1, r0, Operand(kSmiTagMask));
3327 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003328 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003329 // It is a heap object - get the map. Check if the object is a JS array.
3330 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003331 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332 cc_reg_ = eq;
3333}
3334
3335
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003336void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3337 VirtualFrame::SpilledScope spilled_scope;
3338 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003339
3340 // Get the frame pointer for the calling frame.
3341 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3342
3343 // Skip the arguments adaptor frame if it exists.
3344 Label check_frame_marker;
3345 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003346 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003347 __ b(ne, &check_frame_marker);
3348 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3349
3350 // Check the marker in the calling frame.
3351 __ bind(&check_frame_marker);
3352 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3353 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3354 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003355}
3356
3357
ager@chromium.org7c537e22008-10-16 08:43:32 +00003358void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003359 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003360 ASSERT(args->length() == 0);
3361
mads.s.ager31e71382008-08-13 09:32:07 +00003362 // Seed the result with the formal parameters count, which will be used
3363 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3365
3366 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003367 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003368 frame_->CallStub(&stub, 0);
3369 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370}
3371
3372
ager@chromium.org7c537e22008-10-16 08:43:32 +00003373void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003374 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003375 ASSERT(args->length() == 1);
3376
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003377 // Satisfy contract with ArgumentsAccessStub:
3378 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003379 LoadAndSpill(args->at(0));
3380 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003381 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382
3383 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003384 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003385 frame_->CallStub(&stub, 0);
3386 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387}
3388
3389
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003390void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3391 VirtualFrame::SpilledScope spilled_scope;
3392 ASSERT(args->length() == 0);
3393 __ Call(ExternalReference::random_positive_smi_function().address(),
3394 RelocInfo::RUNTIME_ENTRY);
3395 frame_->EmitPush(r0);
3396}
3397
3398
3399void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3400 VirtualFrame::SpilledScope spilled_scope;
3401 LoadAndSpill(args->at(0));
3402 switch (op) {
3403 case SIN:
3404 frame_->CallRuntime(Runtime::kMath_sin, 1);
3405 break;
3406 case COS:
3407 frame_->CallRuntime(Runtime::kMath_cos, 1);
3408 break;
3409 }
3410 frame_->EmitPush(r0);
3411}
3412
3413
ager@chromium.org7c537e22008-10-16 08:43:32 +00003414void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003415 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003416 ASSERT(args->length() == 2);
3417
3418 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003419 LoadAndSpill(args->at(0));
3420 LoadAndSpill(args->at(1));
3421 frame_->EmitPop(r0);
3422 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003423 __ cmp(r0, Operand(r1));
3424 cc_reg_ = eq;
3425}
3426
3427
ager@chromium.org7c537e22008-10-16 08:43:32 +00003428void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003429#ifdef DEBUG
3430 int original_height = frame_->height();
3431#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003432 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003433 if (CheckForInlineRuntimeCall(node)) {
3434 ASSERT((has_cc() && frame_->height() == original_height) ||
3435 (!has_cc() && frame_->height() == original_height + 1));
3436 return;
3437 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003438
3439 ZoneList<Expression*>* args = node->arguments();
3440 Comment cmnt(masm_, "[ CallRuntime");
3441 Runtime::Function* function = node->function();
3442
ager@chromium.org41826e72009-03-30 13:30:57 +00003443 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003444 // Prepare stack for calling JS runtime function.
3445 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003446 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003447 // Push the builtins object found in the current global object.
3448 __ ldr(r1, GlobalObject());
3449 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003450 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003451 }
mads.s.ager31e71382008-08-13 09:32:07 +00003452
ager@chromium.org41826e72009-03-30 13:30:57 +00003453 // Push the arguments ("left-to-right").
3454 int arg_count = args->length();
3455 for (int i = 0; i < arg_count; i++) {
3456 LoadAndSpill(args->at(i));
3457 }
mads.s.ager31e71382008-08-13 09:32:07 +00003458
ager@chromium.org41826e72009-03-30 13:30:57 +00003459 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003461 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3462 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003463 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003464 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003465 frame_->Drop();
3466 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003467 } else {
3468 // Call the C runtime function.
3469 frame_->CallRuntime(function, arg_count);
3470 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003472 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473}
3474
3475
ager@chromium.org7c537e22008-10-16 08:43:32 +00003476void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003477#ifdef DEBUG
3478 int original_height = frame_->height();
3479#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003480 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 Comment cmnt(masm_, "[ UnaryOperation");
3482
3483 Token::Value op = node->op();
3484
3485 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003486 LoadConditionAndSpill(node->expression(),
3487 NOT_INSIDE_TYPEOF,
3488 false_target(),
3489 true_target(),
3490 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003491 // LoadCondition may (and usually does) leave a test and branch to
3492 // be emitted by the caller. In that case, negate the condition.
3493 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494
3495 } else if (op == Token::DELETE) {
3496 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003497 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003499 LoadAndSpill(property->obj());
3500 LoadAndSpill(property->key());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003501 Result arg_count(r0);
3502 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003503 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504
mads.s.ager31e71382008-08-13 09:32:07 +00003505 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506 Slot* slot = variable->slot();
3507 if (variable->is_global()) {
3508 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003509 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003510 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003511 Result arg_count(r0);
3512 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003513 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514
3515 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3516 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003517 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003518 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003519 frame_->EmitPush(r0);
3520 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003522 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003523 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003524 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003525 Result arg_count(r0);
3526 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003527 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528
mads.s.ager31e71382008-08-13 09:32:07 +00003529 } else {
3530 // Default: Result of deleting non-global, not dynamically
3531 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003532 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003533 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534
3535 } else {
3536 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 LoadAndSpill(node->expression()); // may have side-effects
3538 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003539 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003541 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542
3543 } else if (op == Token::TYPEOF) {
3544 // Special case for loading the typeof expression; see comment on
3545 // LoadTypeofExpression().
3546 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003547 frame_->CallRuntime(Runtime::kTypeof, 1);
3548 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549
3550 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003551 LoadAndSpill(node->expression());
3552 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 switch (op) {
3554 case Token::NOT:
3555 case Token::DELETE:
3556 case Token::TYPEOF:
3557 UNREACHABLE(); // handled above
3558 break;
3559
3560 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003561 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003562 (node->expression()->AsBinaryOperation() != NULL &&
3563 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003564 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003565 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003566 break;
3567 }
3568
3569 case Token::BIT_NOT: {
3570 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003571 JumpTarget smi_label;
3572 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003573 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003574 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003575
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003576 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003577 Result arg_count(r0);
3578 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003579 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003581 continue_label.Jump();
3582 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 __ mvn(r0, Operand(r0));
3584 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003585 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 break;
3587 }
3588
3589 case Token::VOID:
3590 // since the stack top is cached in r0, popping and then
3591 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003592 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 break;
3594
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003595 case Token::ADD: {
3596 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003597 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003598 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003599 continue_label.Branch(eq);
3600 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003601 Result arg_count(r0);
3602 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003603 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3604 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003606 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607 default:
3608 UNREACHABLE();
3609 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003610 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003612 ASSERT(!has_valid_frame() ||
3613 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003614 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615}
3616
3617
ager@chromium.org7c537e22008-10-16 08:43:32 +00003618void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003619#ifdef DEBUG
3620 int original_height = frame_->height();
3621#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003622 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623 Comment cmnt(masm_, "[ CountOperation");
3624
3625 bool is_postfix = node->is_postfix();
3626 bool is_increment = node->op() == Token::INC;
3627
3628 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3629 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3630
3631 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003632 if (is_postfix) {
3633 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003634 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003635 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003636
3637 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003638 if (target.is_illegal()) {
3639 // Spoof the virtual frame to have the expected height (one higher
3640 // than on entry).
3641 if (!is_postfix) {
3642 __ mov(r0, Operand(Smi::FromInt(0)));
3643 frame_->EmitPush(r0);
3644 }
3645 ASSERT(frame_->height() == original_height + 1);
3646 return;
3647 }
3648 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3649 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003650
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003651 JumpTarget slow;
3652 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653
3654 // Load the value (1) into register r1.
3655 __ mov(r1, Operand(Smi::FromInt(1)));
3656
3657 // Check for smi operand.
3658 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003659 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660
3661 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003662 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003664 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665
3666 // Perform optimistic increment/decrement.
3667 if (is_increment) {
3668 __ add(r0, r0, Operand(r1), SetCC);
3669 } else {
3670 __ sub(r0, r0, Operand(r1), SetCC);
3671 }
3672
3673 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003674 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675
3676 // Revert optimistic increment/decrement.
3677 if (is_increment) {
3678 __ sub(r0, r0, Operand(r1));
3679 } else {
3680 __ add(r0, r0, Operand(r1));
3681 }
3682
3683 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003684 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003685 {
3686 // Convert the operand to a number.
3687 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003688 Result arg_count(r0);
3689 __ mov(r0, Operand(0));
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003690 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3691 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003693 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003694 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695 }
3696
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003697 // Compute the new value.
3698 __ mov(r1, Operand(Smi::FromInt(1)));
3699 frame_->EmitPush(r0);
3700 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003702 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003704 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705 }
3706
3707 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003708 exit.Bind();
3709 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003710 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 }
3712
3713 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003714 if (is_postfix) frame_->EmitPop(r0);
3715 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716}
3717
3718
ager@chromium.org7c537e22008-10-16 08:43:32 +00003719void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720#ifdef DEBUG
3721 int original_height = frame_->height();
3722#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003723 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 Comment cmnt(masm_, "[ BinaryOperation");
3725 Token::Value op = node->op();
3726
3727 // According to ECMA-262 section 11.11, page 58, the binary logical
3728 // operators must yield the result of one of the two expressions
3729 // before any ToBoolean() conversions. This means that the value
3730 // produced by a && or || operator is not necessarily a boolean.
3731
3732 // NOTE: If the left hand side produces a materialized value (not in
3733 // the CC register), we force the right hand side to do the
3734 // same. This is necessary because we may have to branch to the exit
3735 // after evaluating the left hand side (due to the shortcut
3736 // semantics), but the compiler must (statically) know if the result
3737 // of compiling the binary operation is materialized or not.
3738
3739 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003740 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003741 LoadConditionAndSpill(node->left(),
3742 NOT_INSIDE_TYPEOF,
3743 &is_true,
3744 false_target(),
3745 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003746 if (has_valid_frame() && !has_cc()) {
3747 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003748 JumpTarget pop_and_continue;
3749 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003751 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003753 // Avoid popping the result if it converts to 'false' using the
3754 // standard ToBoolean() conversion as described in ECMA-262,
3755 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003756 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757 Branch(false, &exit);
3758
3759 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003760 pop_and_continue.Bind();
3761 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762
3763 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 is_true.Bind();
3765 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
3767 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003769 } else if (has_cc() || is_true.is_linked()) {
3770 // The left-hand side is either (a) partially compiled to
3771 // control flow with a final branch left to emit or (b) fully
3772 // compiled to control flow and possibly true.
3773 if (has_cc()) {
3774 Branch(false, false_target());
3775 }
3776 is_true.Bind();
3777 LoadConditionAndSpill(node->right(),
3778 NOT_INSIDE_TYPEOF,
3779 true_target(),
3780 false_target(),
3781 false);
3782 } else {
3783 // Nothing to do.
3784 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785 }
3786
3787 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003788 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 LoadConditionAndSpill(node->left(),
3790 NOT_INSIDE_TYPEOF,
3791 true_target(),
3792 &is_false,
3793 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003794 if (has_valid_frame() && !has_cc()) {
3795 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003796 JumpTarget pop_and_continue;
3797 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003799 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003800 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801 // Avoid popping the result if it converts to 'true' using the
3802 // standard ToBoolean() conversion as described in ECMA-262,
3803 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003804 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805 Branch(true, &exit);
3806
3807 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003808 pop_and_continue.Bind();
3809 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003810
3811 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003812 is_false.Bind();
3813 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814
3815 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003816 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003817 } else if (has_cc() || is_false.is_linked()) {
3818 // The left-hand side is either (a) partially compiled to
3819 // control flow with a final branch left to emit or (b) fully
3820 // compiled to control flow and possibly false.
3821 if (has_cc()) {
3822 Branch(true, true_target());
3823 }
3824 is_false.Bind();
3825 LoadConditionAndSpill(node->right(),
3826 NOT_INSIDE_TYPEOF,
3827 true_target(),
3828 false_target(),
3829 false);
3830 } else {
3831 // Nothing to do.
3832 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 }
3834
3835 } else {
3836 // Optimize for the case where (at least) one of the expressions
3837 // is a literal small integer.
3838 Literal* lliteral = node->left()->AsLiteral();
3839 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003840 // NOTE: The code below assumes that the slow cases (calls to runtime)
3841 // never return a constant/immutable object.
3842 bool overwrite_left =
3843 (node->left()->AsBinaryOperation() != NULL &&
3844 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3845 bool overwrite_right =
3846 (node->right()->AsBinaryOperation() != NULL &&
3847 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003848
3849 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003850 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003851 SmiOperation(node->op(),
3852 rliteral->handle(),
3853 false,
3854 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855
3856 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003857 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003858 SmiOperation(node->op(),
3859 lliteral->handle(),
3860 true,
3861 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862
3863 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003864 OverwriteMode overwrite_mode = NO_OVERWRITE;
3865 if (overwrite_left) {
3866 overwrite_mode = OVERWRITE_LEFT;
3867 } else if (overwrite_right) {
3868 overwrite_mode = OVERWRITE_RIGHT;
3869 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003870 LoadAndSpill(node->left());
3871 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003872 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003874 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003876 ASSERT(!has_valid_frame() ||
3877 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003878 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879}
3880
3881
ager@chromium.org7c537e22008-10-16 08:43:32 +00003882void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003883#ifdef DEBUG
3884 int original_height = frame_->height();
3885#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003886 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003887 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003888 frame_->EmitPush(r0);
3889 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890}
3891
3892
ager@chromium.org7c537e22008-10-16 08:43:32 +00003893void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894#ifdef DEBUG
3895 int original_height = frame_->height();
3896#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003897 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003898 Comment cmnt(masm_, "[ CompareOperation");
3899
3900 // Get the expressions from the node.
3901 Expression* left = node->left();
3902 Expression* right = node->right();
3903 Token::Value op = node->op();
3904
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003905 // To make null checks efficient, we check if either left or right is the
3906 // literal 'null'. If so, we optimize the code by inlining a null check
3907 // instead of calling the (very) general runtime routine for checking
3908 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003910 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003911 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003912 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003913 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3914 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003916 LoadAndSpill(left_is_null ? right : left);
3917 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003918 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3919 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003921 // The 'null' value is only equal to 'undefined' if using non-strict
3922 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003924 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003925
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003926 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3927 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003928 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003931 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003933 // It can be an undetectable object.
3934 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3935 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3936 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3937 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938 }
3939
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003940 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003941 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942 return;
3943 }
3944 }
3945
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003946 // To make typeof testing for natives implemented in JavaScript really
3947 // efficient, we generate special code for expressions of the form:
3948 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949 UnaryOperation* operation = left->AsUnaryOperation();
3950 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3951 (operation != NULL && operation->op() == Token::TYPEOF) &&
3952 (right->AsLiteral() != NULL &&
3953 right->AsLiteral()->handle()->IsString())) {
3954 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3955
mads.s.ager31e71382008-08-13 09:32:07 +00003956 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003958 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959
3960 if (check->Equals(Heap::number_symbol())) {
3961 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003962 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003964 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3965 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966 cc_reg_ = eq;
3967
3968 } else if (check->Equals(Heap::string_symbol())) {
3969 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003970 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971
3972 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3973
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003974 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3976 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3977 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003978 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003979
3980 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3981 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3982 cc_reg_ = lt;
3983
3984 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003985 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3986 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003987 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003988 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3989 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990 cc_reg_ = eq;
3991
3992 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003993 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3994 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003995 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996
3997 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003998 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004000 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004001 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4002 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4003 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4004 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4005
4006 cc_reg_ = eq;
4007
4008 } else if (check->Equals(Heap::function_symbol())) {
4009 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004011 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012 cc_reg_ = eq;
4013
4014 } else if (check->Equals(Heap::object_symbol())) {
4015 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004016 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004017
4018 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004019 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4020 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004023 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4025 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4026 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004027 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004028
4029 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4030 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4033 cc_reg_ = le;
4034
4035 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004036 // Uncommon case: typeof testing against a string literal that is
4037 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004038 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004040 ASSERT(!has_valid_frame() ||
4041 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042 return;
4043 }
4044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004045 switch (op) {
4046 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004047 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048 break;
4049
4050 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004051 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004052 break;
4053
4054 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004055 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056 break;
4057
4058 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004059 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004060 break;
4061
4062 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004063 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004064 break;
4065
4066 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004067 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004068 break;
4069
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004071 LoadAndSpill(left);
4072 LoadAndSpill(right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004073 Result arg_count(r0);
4074 __ mov(r0, Operand(1)); // not counting receiver
4075 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2);
4076 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004079
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004080 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004081 LoadAndSpill(left);
4082 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004083 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004084 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004085 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004086 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004087 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004088 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090
4091 default:
4092 UNREACHABLE();
4093 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004094 ASSERT((has_cc() && frame_->height() == original_height) ||
4095 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004096}
4097
4098
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004099#ifdef DEBUG
4100bool CodeGenerator::HasValidEntryRegisters() { return true; }
4101#endif
4102
4103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004105#define __ ACCESS_MASM(masm)
4106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107
ager@chromium.org7c537e22008-10-16 08:43:32 +00004108Handle<String> Reference::GetName() {
4109 ASSERT(type_ == NAMED);
4110 Property* property = expression_->AsProperty();
4111 if (property == NULL) {
4112 // Global variable reference treated as a named property reference.
4113 VariableProxy* proxy = expression_->AsVariableProxy();
4114 ASSERT(proxy->AsVariable() != NULL);
4115 ASSERT(proxy->AsVariable()->is_global());
4116 return proxy->name();
4117 } else {
4118 Literal* raw_name = property->key()->AsLiteral();
4119 ASSERT(raw_name != NULL);
4120 return Handle<String>(String::cast(*raw_name->handle()));
4121 }
4122}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004123
ager@chromium.org7c537e22008-10-16 08:43:32 +00004124
4125void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004126 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004127 ASSERT(!is_illegal());
4128 ASSERT(!cgen_->has_cc());
4129 MacroAssembler* masm = cgen_->masm();
4130 Property* property = expression_->AsProperty();
4131 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004132 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004133 }
4134
4135 switch (type_) {
4136 case SLOT: {
4137 Comment cmnt(masm, "[ Load from Slot");
4138 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4139 ASSERT(slot != NULL);
4140 cgen_->LoadFromSlot(slot, typeof_state);
4141 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004142 }
4143
ager@chromium.org7c537e22008-10-16 08:43:32 +00004144 case NAMED: {
4145 // TODO(1241834): Make sure that this it is safe to ignore the
4146 // distinction between expressions in a typeof and not in a typeof. If
4147 // there is a chance that reference errors can be thrown below, we
4148 // must distinguish between the two kinds of loads (typeof expression
4149 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004150 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004151 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004152 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004153 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004154 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4155 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004156 Result name_reg(r2);
4157 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004158 ASSERT(var == NULL || var->is_global());
4159 RelocInfo::Mode rmode = (var == NULL)
4160 ? RelocInfo::CODE_TARGET
4161 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004162 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4163 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004164 break;
4165 }
4166
4167 case KEYED: {
4168 // TODO(1241834): Make sure that this it is safe to ignore the
4169 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004170
4171 // TODO(181): Implement inlined version of array indexing once
4172 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004173 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174 Comment cmnt(masm, "[ Load from keyed Property");
4175 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004176 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004177 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004178 ASSERT(var == NULL || var->is_global());
4179 RelocInfo::Mode rmode = (var == NULL)
4180 ? RelocInfo::CODE_TARGET
4181 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004182 frame->CallCodeObject(ic, rmode, 0);
4183 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004184 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004185 }
4186
4187 default:
4188 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004189 }
4190}
4191
4192
ager@chromium.org7c537e22008-10-16 08:43:32 +00004193void Reference::SetValue(InitState init_state) {
4194 ASSERT(!is_illegal());
4195 ASSERT(!cgen_->has_cc());
4196 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004197 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198 Property* property = expression_->AsProperty();
4199 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004200 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004201 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004202
ager@chromium.org7c537e22008-10-16 08:43:32 +00004203 switch (type_) {
4204 case SLOT: {
4205 Comment cmnt(masm, "[ Store to Slot");
4206 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4207 ASSERT(slot != NULL);
4208 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004209 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004210
ager@chromium.org7c537e22008-10-16 08:43:32 +00004211 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004212 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004213 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004214 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004215
ager@chromium.org7c537e22008-10-16 08:43:32 +00004216 if (init_state == CONST_INIT) {
4217 // Same as the case for a normal store, but ignores attribute
4218 // (e.g. READ_ONLY) of context slot so that we can initialize
4219 // const properties (introduced via eval("const foo = (some
4220 // expr);")). Also, uses the current function context instead of
4221 // the top context.
4222 //
4223 // Note that we must declare the foo upon entry of eval(), via a
4224 // context slot declaration, but we cannot initialize it at the
4225 // same time, because the const declaration may be at the end of
4226 // the eval code (sigh...) and the const variable may have been
4227 // used before (where its value is 'undefined'). Thus, we can only
4228 // do the initialization when we actually encounter the expression
4229 // and when the expression operands are defined and valid, and
4230 // thus we need the split into 2 operations: declaration of the
4231 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004232 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004233 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004235 }
4236 // Storing a variable must keep the (new) value on the expression
4237 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004238 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004239
ager@chromium.org7c537e22008-10-16 08:43:32 +00004240 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004241 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004242
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004243 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 if (init_state == CONST_INIT) {
4245 ASSERT(slot->var()->mode() == Variable::CONST);
4246 // Only the first const initialization must be executed (the slot
4247 // still contains 'the hole' value). When the assignment is
4248 // executed, the code is identical to a normal store (see below).
4249 Comment cmnt(masm, "[ Init const");
4250 __ ldr(r2, cgen_->SlotOperand(slot, r2));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004251 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
4252 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004253 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004254 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004255
ager@chromium.org7c537e22008-10-16 08:43:32 +00004256 // We must execute the store. Storing a variable must keep the
4257 // (new) value on the stack. This is necessary for compiling
4258 // assignment expressions.
4259 //
4260 // Note: We will reach here even with slot->var()->mode() ==
4261 // Variable::CONST because of const declarations which will
4262 // initialize consts to 'the hole' value and by doing so, end up
4263 // calling this code. r2 may be loaded with context; used below in
4264 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004265 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004266 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004267 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004268 if (slot->type() == Slot::CONTEXT) {
4269 // Skip write barrier if the written value is a smi.
4270 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004271 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004272 // r2 is loaded with context when calling SlotOperand above.
4273 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4274 __ mov(r3, Operand(offset));
4275 __ RecordWrite(r2, r3, r1);
4276 }
4277 // If we definitely did not jump over the assignment, we do not need
4278 // to bind the exit label. Doing so can defeat peephole
4279 // optimization.
4280 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004281 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004282 }
4283 }
4284 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004285 }
4286
ager@chromium.org7c537e22008-10-16 08:43:32 +00004287 case NAMED: {
4288 Comment cmnt(masm, "[ Store to named Property");
4289 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004290 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004291 Handle<String> name(GetName());
4292
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004293 Result value(r0);
4294 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295
4296 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004297 Result property_name(r2);
4298 __ mov(r2, Operand(name));
4299 frame->CallCodeObject(ic,
4300 RelocInfo::CODE_TARGET,
4301 &value,
4302 &property_name,
4303 0);
4304 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004305 break;
4306 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004307
ager@chromium.org7c537e22008-10-16 08:43:32 +00004308 case KEYED: {
4309 Comment cmnt(masm, "[ Store to keyed Property");
4310 Property* property = expression_->AsProperty();
4311 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004312 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004313
4314 // Call IC code.
4315 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4316 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004317 Result value(r0);
4318 frame->EmitPop(r0); // value
4319 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4320 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004321 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004322 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004323
4324 default:
4325 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004326 }
4327}
4328
4329
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004330// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4331// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4332// (31 instead of 32).
4333static void CountLeadingZeros(
4334 MacroAssembler* masm,
4335 Register source,
4336 Register scratch,
4337 Register zeros) {
4338#ifdef __ARM_ARCH_5__
4339 __ clz(zeros, source); // This instruction is only supported after ARM5.
4340#else
4341 __ mov(zeros, Operand(0));
4342 __ mov(scratch, source);
4343 // Top 16.
4344 __ tst(scratch, Operand(0xffff0000));
4345 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4346 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4347 // Top 8.
4348 __ tst(scratch, Operand(0xff000000));
4349 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4350 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4351 // Top 4.
4352 __ tst(scratch, Operand(0xf0000000));
4353 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4354 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4355 // Top 2.
4356 __ tst(scratch, Operand(0xc0000000));
4357 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4358 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4359 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004360 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004361 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4362#endif
4363}
4364
4365
4366// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4367// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4368// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4369// scratch register. Destroys the source register. No GC occurs during this
4370// stub so you don't have to set up the frame.
4371class ConvertToDoubleStub : public CodeStub {
4372 public:
4373 ConvertToDoubleStub(Register result_reg_1,
4374 Register result_reg_2,
4375 Register source_reg,
4376 Register scratch_reg)
4377 : result1_(result_reg_1),
4378 result2_(result_reg_2),
4379 source_(source_reg),
4380 zeros_(scratch_reg) { }
4381
4382 private:
4383 Register result1_;
4384 Register result2_;
4385 Register source_;
4386 Register zeros_;
4387
4388 // Minor key encoding in 16 bits.
4389 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4390 class OpBits: public BitField<Token::Value, 2, 14> {};
4391
4392 Major MajorKey() { return ConvertToDouble; }
4393 int MinorKey() {
4394 // Encode the parameters in a unique 16 bit value.
4395 return result1_.code() +
4396 (result2_.code() << 4) +
4397 (source_.code() << 8) +
4398 (zeros_.code() << 12);
4399 }
4400
4401 void Generate(MacroAssembler* masm);
4402
4403 const char* GetName() { return "ConvertToDoubleStub"; }
4404
4405#ifdef DEBUG
4406 void Print() { PrintF("ConvertToDoubleStub\n"); }
4407#endif
4408};
4409
4410
4411void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4412#ifndef BIG_ENDIAN_FLOATING_POINT
4413 Register exponent = result1_;
4414 Register mantissa = result2_;
4415#else
4416 Register exponent = result2_;
4417 Register mantissa = result1_;
4418#endif
4419 Label not_special;
4420 // Convert from Smi to integer.
4421 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4422 // Move sign bit from source to destination. This works because the sign bit
4423 // in the exponent word of the double has the same position and polarity as
4424 // the 2's complement sign bit in a Smi.
4425 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4426 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4427 // Subtract from 0 if source was negative.
4428 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4429 __ cmp(source_, Operand(1));
4430 __ b(gt, &not_special);
4431
4432 // We have -1, 0 or 1, which we treat specially.
4433 __ cmp(source_, Operand(0));
4434 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4435 static const uint32_t exponent_word_for_1 =
4436 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4437 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4438 // 1, 0 and -1 all have 0 for the second word.
4439 __ mov(mantissa, Operand(0));
4440 __ Ret();
4441
4442 __ bind(&not_special);
4443 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4444 // Gets the wrong answer for 0, but we already checked for that case above.
4445 CountLeadingZeros(masm, source_, mantissa, zeros_);
4446 // Compute exponent and or it into the exponent register.
4447 // We use result2 as a scratch register here.
4448 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4449 __ orr(exponent,
4450 exponent,
4451 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4452 // Shift up the source chopping the top bit off.
4453 __ add(zeros_, zeros_, Operand(1));
4454 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4455 __ mov(source_, Operand(source_, LSL, zeros_));
4456 // Compute lower part of fraction (last 12 bits).
4457 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4458 // And the top (top 20 bits).
4459 __ orr(exponent,
4460 exponent,
4461 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4462 __ Ret();
4463}
4464
4465
4466// This stub can convert a signed int32 to a heap number (double). It does
4467// not work for int32s that are in Smi range! No GC occurs during this stub
4468// so you don't have to set up the frame.
4469class WriteInt32ToHeapNumberStub : public CodeStub {
4470 public:
4471 WriteInt32ToHeapNumberStub(Register the_int,
4472 Register the_heap_number,
4473 Register scratch)
4474 : the_int_(the_int),
4475 the_heap_number_(the_heap_number),
4476 scratch_(scratch) { }
4477
4478 private:
4479 Register the_int_;
4480 Register the_heap_number_;
4481 Register scratch_;
4482
4483 // Minor key encoding in 16 bits.
4484 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4485 class OpBits: public BitField<Token::Value, 2, 14> {};
4486
4487 Major MajorKey() { return WriteInt32ToHeapNumber; }
4488 int MinorKey() {
4489 // Encode the parameters in a unique 16 bit value.
4490 return the_int_.code() +
4491 (the_heap_number_.code() << 4) +
4492 (scratch_.code() << 8);
4493 }
4494
4495 void Generate(MacroAssembler* masm);
4496
4497 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4498
4499#ifdef DEBUG
4500 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4501#endif
4502};
4503
4504
4505// See comment for class.
4506void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4507 Label max_negative_int;
4508 // the_int_ has the answer which is a signed int32 but not a Smi.
4509 // We test for the special value that has a different exponent. This test
4510 // has the neat side effect of setting the flags according to the sign.
4511 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004512 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004513 __ b(eq, &max_negative_int);
4514 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4515 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4516 uint32_t non_smi_exponent =
4517 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4518 __ mov(scratch_, Operand(non_smi_exponent));
4519 // Set the sign bit in scratch_ if the value was negative.
4520 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4521 // Subtract from 0 if the value was negative.
4522 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4523 // We should be masking the implict first digit of the mantissa away here,
4524 // but it just ends up combining harmlessly with the last digit of the
4525 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4526 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4527 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4528 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4529 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4530 __ str(scratch_, FieldMemOperand(the_heap_number_,
4531 HeapNumber::kExponentOffset));
4532 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4533 __ str(scratch_, FieldMemOperand(the_heap_number_,
4534 HeapNumber::kMantissaOffset));
4535 __ Ret();
4536
4537 __ bind(&max_negative_int);
4538 // The max negative int32 is stored as a positive number in the mantissa of
4539 // a double because it uses a sign bit instead of using two's complement.
4540 // The actual mantissa bits stored are all 0 because the implicit most
4541 // significant 1 bit is not stored.
4542 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4543 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4544 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4545 __ mov(ip, Operand(0));
4546 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4547 __ Ret();
4548}
4549
4550
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004551// Handle the case where the lhs and rhs are the same object.
4552// Equality is almost reflexive (everything but NaN), so this is a test
4553// for "identity and not NaN".
4554static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4555 Label* slow,
4556 Condition cc) {
4557 Label not_identical;
4558 __ cmp(r0, Operand(r1));
4559 __ b(ne, &not_identical);
4560
4561 Register exp_mask_reg = r5;
4562 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4563
4564 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4565 // so we do the second best thing - test it ourselves.
4566 Label heap_number, return_equal;
4567 // They are both equal and they are not both Smis so both of them are not
4568 // Smis. If it's not a heap number, then return equal.
4569 if (cc == lt || cc == gt) {
4570 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4571 __ b(ge, slow);
4572 } else {
4573 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4574 __ b(eq, &heap_number);
4575 // Comparing JS objects with <=, >= is complicated.
4576 if (cc != eq) {
4577 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4578 __ b(ge, slow);
4579 }
4580 }
4581 __ bind(&return_equal);
4582 if (cc == lt) {
4583 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4584 } else if (cc == gt) {
4585 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4586 } else {
4587 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4588 }
4589 __ mov(pc, Operand(lr)); // Return.
4590
4591 // For less and greater we don't have to check for NaN since the result of
4592 // x < x is false regardless. For the others here is some code to check
4593 // for NaN.
4594 if (cc != lt && cc != gt) {
4595 __ bind(&heap_number);
4596 // It is a heap number, so return non-equal if it's NaN and equal if it's
4597 // not NaN.
4598 // The representation of NaN values has all exponent bits (52..62) set,
4599 // and not all mantissa bits (0..51) clear.
4600 // Read top bits of double representation (second word of value).
4601 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4602 // Test that exponent bits are all set.
4603 __ and_(r3, r2, Operand(exp_mask_reg));
4604 __ cmp(r3, Operand(exp_mask_reg));
4605 __ b(ne, &return_equal);
4606
4607 // Shift out flag and all exponent bits, retaining only mantissa.
4608 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4609 // Or with all low-bits of mantissa.
4610 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4611 __ orr(r0, r3, Operand(r2), SetCC);
4612 // For equal we already have the right value in r0: Return zero (equal)
4613 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4614 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4615 // if it's a NaN.
4616 if (cc != eq) {
4617 // All-zero means Infinity means equal.
4618 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4619 if (cc == le) {
4620 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4621 } else {
4622 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4623 }
4624 }
4625 __ mov(pc, Operand(lr)); // Return.
4626 }
4627 // No fall through here.
4628
4629 __ bind(&not_identical);
4630}
4631
4632
4633// See comment at call site.
4634static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4635 Label* rhs_not_nan,
4636 Label* slow,
4637 bool strict) {
4638 Label lhs_is_smi;
4639 __ tst(r0, Operand(kSmiTagMask));
4640 __ b(eq, &lhs_is_smi);
4641
4642 // Rhs is a Smi. Check whether the non-smi is a heap number.
4643 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4644 if (strict) {
4645 // If lhs was not a number and rhs was a Smi then strict equality cannot
4646 // succeed. Return non-equal (r0 is already not zero)
4647 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4648 } else {
4649 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4650 // the runtime.
4651 __ b(ne, slow);
4652 }
4653
4654 // Rhs is a smi, lhs is a number.
4655 __ push(lr);
4656 __ mov(r7, Operand(r1));
4657 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4658 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4659 // r3 and r2 are rhs as double.
4660 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4661 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4662 // We now have both loaded as doubles but we can skip the lhs nan check
4663 // since it's a Smi.
4664 __ pop(lr);
4665 __ jmp(rhs_not_nan);
4666
4667 __ bind(&lhs_is_smi);
4668 // Lhs is a Smi. Check whether the non-smi is a heap number.
4669 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4670 if (strict) {
4671 // If lhs was not a number and rhs was a Smi then strict equality cannot
4672 // succeed. Return non-equal.
4673 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4674 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4675 } else {
4676 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4677 // the runtime.
4678 __ b(ne, slow);
4679 }
4680
4681 // Lhs is a smi, rhs is a number.
4682 // r0 is Smi and r1 is heap number.
4683 __ push(lr);
4684 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4685 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4686 __ mov(r7, Operand(r0));
4687 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4688 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4689 __ pop(lr);
4690 // Fall through to both_loaded_as_doubles.
4691}
4692
4693
4694void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4695 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4696 Register lhs_exponent = exp_first ? r0 : r1;
4697 Register rhs_exponent = exp_first ? r2 : r3;
4698 Register lhs_mantissa = exp_first ? r1 : r0;
4699 Register rhs_mantissa = exp_first ? r3 : r2;
4700 Label one_is_nan, neither_is_nan;
4701
4702 Register exp_mask_reg = r5;
4703
4704 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4705 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4706 __ cmp(r4, Operand(exp_mask_reg));
4707 __ b(ne, rhs_not_nan);
4708 __ mov(r4,
4709 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4710 SetCC);
4711 __ b(ne, &one_is_nan);
4712 __ cmp(rhs_mantissa, Operand(0));
4713 __ b(ne, &one_is_nan);
4714
4715 __ bind(rhs_not_nan);
4716 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4717 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4718 __ cmp(r4, Operand(exp_mask_reg));
4719 __ b(ne, &neither_is_nan);
4720 __ mov(r4,
4721 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4722 SetCC);
4723 __ b(ne, &one_is_nan);
4724 __ cmp(lhs_mantissa, Operand(0));
4725 __ b(eq, &neither_is_nan);
4726
4727 __ bind(&one_is_nan);
4728 // NaN comparisons always fail.
4729 // Load whatever we need in r0 to make the comparison fail.
4730 if (cc == lt || cc == le) {
4731 __ mov(r0, Operand(GREATER));
4732 } else {
4733 __ mov(r0, Operand(LESS));
4734 }
4735 __ mov(pc, Operand(lr)); // Return.
4736
4737 __ bind(&neither_is_nan);
4738}
4739
4740
4741// See comment at call site.
4742static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4743 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4744 Register lhs_exponent = exp_first ? r0 : r1;
4745 Register rhs_exponent = exp_first ? r2 : r3;
4746 Register lhs_mantissa = exp_first ? r1 : r0;
4747 Register rhs_mantissa = exp_first ? r3 : r2;
4748
4749 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4750 if (cc == eq) {
4751 // Doubles are not equal unless they have the same bit pattern.
4752 // Exception: 0 and -0.
4753 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4754 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4755 // Return non-zero if the numbers are unequal.
4756 __ mov(pc, Operand(lr), LeaveCC, ne);
4757
4758 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4759 // If exponents are equal then return 0.
4760 __ mov(pc, Operand(lr), LeaveCC, eq);
4761
4762 // Exponents are unequal. The only way we can return that the numbers
4763 // are equal is if one is -0 and the other is 0. We already dealt
4764 // with the case where both are -0 or both are 0.
4765 // We start by seeing if the mantissas (that are equal) or the bottom
4766 // 31 bits of the rhs exponent are non-zero. If so we return not
4767 // equal.
4768 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4769 __ mov(r0, Operand(r4), LeaveCC, ne);
4770 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4771 // Now they are equal if and only if the lhs exponent is zero in its
4772 // low 31 bits.
4773 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4774 __ mov(pc, Operand(lr));
4775 } else {
4776 // Call a native function to do a comparison between two non-NaNs.
4777 // Call C routine that may not cause GC or other trouble.
4778 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4779 __ Jump(r5); // Tail call.
4780 }
4781}
4782
4783
4784// See comment at call site.
4785static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4786 // If either operand is a JSObject or an oddball value, then they are
4787 // not equal since their pointers are different.
4788 // There is no test for undetectability in strict equality.
4789 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4790 Label first_non_object;
4791 // Get the type of the first operand into r2 and compare it with
4792 // FIRST_JS_OBJECT_TYPE.
4793 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4794 __ b(lt, &first_non_object);
4795
4796 // Return non-zero (r0 is not zero)
4797 Label return_not_equal;
4798 __ bind(&return_not_equal);
4799 __ mov(pc, Operand(lr)); // Return.
4800
4801 __ bind(&first_non_object);
4802 // Check for oddballs: true, false, null, undefined.
4803 __ cmp(r2, Operand(ODDBALL_TYPE));
4804 __ b(eq, &return_not_equal);
4805
4806 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4807 __ b(ge, &return_not_equal);
4808
4809 // Check for oddballs: true, false, null, undefined.
4810 __ cmp(r3, Operand(ODDBALL_TYPE));
4811 __ b(eq, &return_not_equal);
4812}
4813
4814
4815// See comment at call site.
4816static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4817 Label* both_loaded_as_doubles,
4818 Label* not_heap_numbers,
4819 Label* slow) {
4820 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4821 __ b(ne, not_heap_numbers);
4822 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4823 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4824
4825 // Both are heap numbers. Load them up then jump to the code we have
4826 // for that.
4827 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4828 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4829 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4830 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4831 __ jmp(both_loaded_as_doubles);
4832}
4833
4834
4835// Fast negative check for symbol-to-symbol equality.
4836static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4837 // r2 is object type of r0.
4838 __ tst(r2, Operand(kIsNotStringMask));
4839 __ b(ne, slow);
4840 __ tst(r2, Operand(kIsSymbolMask));
4841 __ b(eq, slow);
4842 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4843 __ b(ge, slow);
4844 __ tst(r3, Operand(kIsSymbolMask));
4845 __ b(eq, slow);
4846
4847 // Both are symbols. We already checked they weren't the same pointer
4848 // so they are not equal.
4849 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4850 __ mov(pc, Operand(lr)); // Return.
4851}
4852
4853
4854// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4855// positive or negative to indicate the result of the comparison.
4856void CompareStub::Generate(MacroAssembler* masm) {
4857 Label slow; // Call builtin.
4858 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4859
4860 // NOTICE! This code is only reached after a smi-fast-case check, so
4861 // it is certain that at least one operand isn't a smi.
4862
4863 // Handle the case where the objects are identical. Either returns the answer
4864 // or goes to slow. Only falls through if the objects were not identical.
4865 EmitIdenticalObjectComparison(masm, &slow, cc_);
4866
4867 // If either is a Smi (we know that not both are), then they can only
4868 // be strictly equal if the other is a HeapNumber.
4869 ASSERT_EQ(0, kSmiTag);
4870 ASSERT_EQ(0, Smi::FromInt(0));
4871 __ and_(r2, r0, Operand(r1));
4872 __ tst(r2, Operand(kSmiTagMask));
4873 __ b(ne, &not_smis);
4874 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4875 // 1) Return the answer.
4876 // 2) Go to slow.
4877 // 3) Fall through to both_loaded_as_doubles.
4878 // 4) Jump to rhs_not_nan.
4879 // In cases 3 and 4 we have found out we were dealing with a number-number
4880 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4881 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4882
4883 __ bind(&both_loaded_as_doubles);
4884 // r0, r1, r2, r3 are the double representations of the left hand side
4885 // and the right hand side.
4886
4887 // Checks for NaN in the doubles we have loaded. Can return the answer or
4888 // fall through if neither is a NaN. Also binds rhs_not_nan.
4889 EmitNanCheck(masm, &rhs_not_nan, cc_);
4890
4891 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4892 // answer. Never falls through.
4893 EmitTwoNonNanDoubleComparison(masm, cc_);
4894
4895 __ bind(&not_smis);
4896 // At this point we know we are dealing with two different objects,
4897 // and neither of them is a Smi. The objects are in r0 and r1.
4898 if (strict_) {
4899 // This returns non-equal for some object types, or falls through if it
4900 // was not lucky.
4901 EmitStrictTwoHeapObjectCompare(masm);
4902 }
4903
4904 Label check_for_symbols;
4905 // Check for heap-number-heap-number comparison. Can jump to slow case,
4906 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4907 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4908 // In this case r2 will contain the type of r0.
4909 EmitCheckForTwoHeapNumbers(masm,
4910 &both_loaded_as_doubles,
4911 &check_for_symbols,
4912 &slow);
4913
4914 __ bind(&check_for_symbols);
4915 if (cc_ == eq) {
4916 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4917 // of r0 on entry.
4918 EmitCheckForSymbols(masm, &slow);
4919 }
4920
4921 __ bind(&slow);
4922 __ push(lr);
4923 __ push(r1);
4924 __ push(r0);
4925 // Figure out which native to call and setup the arguments.
4926 Builtins::JavaScript native;
4927 int arg_count = 1; // Not counting receiver.
4928 if (cc_ == eq) {
4929 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4930 } else {
4931 native = Builtins::COMPARE;
4932 int ncr; // NaN compare result
4933 if (cc_ == lt || cc_ == le) {
4934 ncr = GREATER;
4935 } else {
4936 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4937 ncr = LESS;
4938 }
4939 arg_count++;
4940 __ mov(r0, Operand(Smi::FromInt(ncr)));
4941 __ push(r0);
4942 }
4943
4944 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4945 // tagged as a small integer.
4946 __ mov(r0, Operand(arg_count));
4947 __ InvokeBuiltin(native, CALL_JS);
4948 __ cmp(r0, Operand(0));
4949 __ pop(pc);
4950}
4951
4952
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004953// Allocates a heap number or jumps to the label if the young space is full and
4954// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004955static void AllocateHeapNumber(
4956 MacroAssembler* masm,
4957 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004958 Register result, // The tagged address of the new heap number.
4959 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004960 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004961 // Allocate an object in the heap for the heap number and tag it as a heap
4962 // object.
ager@chromium.orga1645e22009-09-09 19:27:10 +00004963 __ AllocateObjectInNewSpace(HeapNumber::kSize / kPointerSize,
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004964 result,
4965 scratch1,
4966 scratch2,
4967 need_gc,
ager@chromium.orga1645e22009-09-09 19:27:10 +00004968 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004969
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004970 // Get heap number map and store it in the allocated object.
4971 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
4972 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004973}
4974
4975
4976// We fall into this code if the operands were Smis, but the result was
4977// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004978// the operands were not both Smi. The operands are in r0 and r1. In order
4979// to call the C-implemented binary fp operation routines we need to end up
4980// with the double precision floating point operands in r0 and r1 (for the
4981// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004982static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4983 Label* not_smi,
4984 const Builtins::JavaScript& builtin,
4985 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004986 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004987 Label slow, slow_pop_2_first, do_the_call;
4988 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4989 // Smi-smi case (overflow).
4990 // Since both are Smis there is no heap number to overwrite, so allocate.
4991 // The new heap number is in r5. r6 and r7 are scratch.
4992 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4993 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004994 __ mov(r7, Operand(r0));
4995 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004996 __ push(lr);
4997 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4998 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4999 __ mov(r7, Operand(r1));
5000 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5001 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5002 __ pop(lr);
5003 __ jmp(&do_the_call); // Tail call. No return.
5004
5005 // We jump to here if something goes wrong (one param is not a number of any
5006 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005007 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005008 __ push(r1);
5009 __ push(r0);
5010 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005011 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005012
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005013 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005014 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005015 if (mode == NO_OVERWRITE) {
5016 // In the case where there is no chance of an overwritable float we may as
5017 // well do the allocation immediately while r0 and r1 are untouched.
5018 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5019 }
5020
5021 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005022 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005023 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5024 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005025 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005026 if (mode == OVERWRITE_RIGHT) {
5027 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5028 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005029 // Calling convention says that second double is in r2 and r3.
5030 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005031 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5032 __ jmp(&finished_loading_r0);
5033 __ bind(&r0_is_smi);
5034 if (mode == OVERWRITE_RIGHT) {
5035 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005036 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005037 }
5038 // Write Smi from r0 to r3 and r2 in double format.
5039 __ mov(r7, Operand(r0));
5040 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5041 __ push(lr);
5042 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5043 __ pop(lr);
5044 __ bind(&finished_loading_r0);
5045
5046 // Move r1 to a double in r0-r1.
5047 __ tst(r1, Operand(kSmiTagMask));
5048 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5049 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5050 __ b(ne, &slow);
5051 if (mode == OVERWRITE_LEFT) {
5052 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005053 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005054 // Calling convention says that first double is in r0 and r1.
5055 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005056 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5057 __ jmp(&finished_loading_r1);
5058 __ bind(&r1_is_smi);
5059 if (mode == OVERWRITE_LEFT) {
5060 // We can't overwrite a Smi so get address of new heap number into r5.
5061 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5062 }
5063 // Write Smi from r1 to r1 and r0 in double format.
5064 __ mov(r7, Operand(r1));
5065 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5066 __ push(lr);
5067 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5068 __ pop(lr);
5069 __ bind(&finished_loading_r1);
5070
5071 __ bind(&do_the_call);
5072 // r0: Left value (least significant part of mantissa).
5073 // r1: Left value (sign, exponent, top of mantissa).
5074 // r2: Right value (least significant part of mantissa).
5075 // r3: Right value (sign, exponent, top of mantissa).
5076 // r5: Address of heap number for result.
5077 __ push(lr); // For later.
5078 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005079 // Call C routine that may not cause GC or other trouble.
5080 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005081 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005082 // Store answer in the overwritable heap number.
5083 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005084#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005085 // Double returned in fp coprocessor register 0 and 1, encoded as register
5086 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5087 // substract the tag from r4.
5088 __ sub(r5, r4, Operand(kHeapObjectTag));
5089 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5090#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005091 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005092 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005093 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005094#endif
5095 __ mov(r0, Operand(r4));
5096 // And we are done.
5097 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005098}
5099
5100
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005101// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005102// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005103// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5104// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005105// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5106// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005107static void GetInt32(MacroAssembler* masm,
5108 Register source,
5109 Register dest,
5110 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005111 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005112 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005113 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005114 // Get exponent word.
5115 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5116 // Get exponent alone in scratch2.
5117 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005118 // Load dest with zero. We use this either for the final shift or
5119 // for the answer.
5120 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005121 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005122 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5123 // the exponent that we are fastest at and also the highest exponent we can
5124 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005125 const uint32_t non_smi_exponent =
5126 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5127 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005128 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5129 __ b(eq, &right_exponent);
5130 // If the exponent is higher than that then go to slow case. This catches
5131 // numbers that don't fit in a signed int32, infinities and NaNs.
5132 __ b(gt, slow);
5133
5134 // We know the exponent is smaller than 30 (biased). If it is less than
5135 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5136 // it rounds to zero.
5137 const uint32_t zero_exponent =
5138 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5139 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5140 // Dest already has a Smi zero.
5141 __ b(lt, &done);
5142 // We have a shifted exponent between 0 and 30 in scratch2.
5143 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5144 // We now have the exponent in dest. Subtract from 30 to get
5145 // how much to shift down.
5146 __ rsb(dest, dest, Operand(30));
5147
5148 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005149 // Get the top bits of the mantissa.
5150 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5151 // Put back the implicit 1.
5152 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5153 // Shift up the mantissa bits to take up the space the exponent used to take.
5154 // We just orred in the implicit bit so that took care of one and we want to
5155 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5156 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5157 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5158 // Put sign in zero flag.
5159 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005160 // Get the second half of the double. For some exponents we don't actually
5161 // need this because the bits get shifted out again, but it's probably slower
5162 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005163 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5164 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005165 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5166 // Move down according to the exponent.
5167 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005168 // Fix sign if sign bit was set.
5169 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005170 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005171}
5172
5173
5174// For bitwise ops where the inputs are not both Smis we here try to determine
5175// whether both inputs are either Smis or at least heap numbers that can be
5176// represented by a 32 bit signed value. We truncate towards zero as required
5177// by the ES spec. If this is the case we do the bitwise op and see if the
5178// result is a Smi. If so, great, otherwise we try to find a heap number to
5179// write the answer into (either by allocating or by overwriting).
5180// On entry the operands are in r0 and r1. On exit the answer is in r0.
5181void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5182 Label slow, result_not_a_smi;
5183 Label r0_is_smi, r1_is_smi;
5184 Label done_checking_r0, done_checking_r1;
5185
5186 __ tst(r1, Operand(kSmiTagMask));
5187 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5188 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5189 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005190 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005191 __ jmp(&done_checking_r1);
5192 __ bind(&r1_is_smi);
5193 __ mov(r3, Operand(r1, ASR, 1));
5194 __ bind(&done_checking_r1);
5195
5196 __ tst(r0, Operand(kSmiTagMask));
5197 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5198 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5199 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005200 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005201 __ jmp(&done_checking_r0);
5202 __ bind(&r0_is_smi);
5203 __ mov(r2, Operand(r0, ASR, 1));
5204 __ bind(&done_checking_r0);
5205
5206 // r0 and r1: Original operands (Smi or heap numbers).
5207 // r2 and r3: Signed int32 operands.
5208 switch (op_) {
5209 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5210 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5211 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5212 case Token::SAR:
5213 // Use only the 5 least significant bits of the shift count.
5214 __ and_(r2, r2, Operand(0x1f));
5215 __ mov(r2, Operand(r3, ASR, r2));
5216 break;
5217 case Token::SHR:
5218 // Use only the 5 least significant bits of the shift count.
5219 __ and_(r2, r2, Operand(0x1f));
5220 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5221 // SHR is special because it is required to produce a positive answer.
5222 // The code below for writing into heap numbers isn't capable of writing
5223 // the register as an unsigned int so we go to slow case if we hit this
5224 // case.
5225 __ b(mi, &slow);
5226 break;
5227 case Token::SHL:
5228 // Use only the 5 least significant bits of the shift count.
5229 __ and_(r2, r2, Operand(0x1f));
5230 __ mov(r2, Operand(r3, LSL, r2));
5231 break;
5232 default: UNREACHABLE();
5233 }
5234 // check that the *signed* result fits in a smi
5235 __ add(r3, r2, Operand(0x40000000), SetCC);
5236 __ b(mi, &result_not_a_smi);
5237 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5238 __ Ret();
5239
5240 Label have_to_allocate, got_a_heap_number;
5241 __ bind(&result_not_a_smi);
5242 switch (mode_) {
5243 case OVERWRITE_RIGHT: {
5244 __ tst(r0, Operand(kSmiTagMask));
5245 __ b(eq, &have_to_allocate);
5246 __ mov(r5, Operand(r0));
5247 break;
5248 }
5249 case OVERWRITE_LEFT: {
5250 __ tst(r1, Operand(kSmiTagMask));
5251 __ b(eq, &have_to_allocate);
5252 __ mov(r5, Operand(r1));
5253 break;
5254 }
5255 case NO_OVERWRITE: {
5256 // Get a new heap number in r5. r6 and r7 are scratch.
5257 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5258 }
5259 default: break;
5260 }
5261 __ bind(&got_a_heap_number);
5262 // r2: Answer as signed int32.
5263 // r5: Heap number to write answer into.
5264
5265 // Nothing can go wrong now, so move the heap number to r0, which is the
5266 // result.
5267 __ mov(r0, Operand(r5));
5268
5269 // Tail call that writes the int32 in r2 to the heap number in r0, using
5270 // r3 as scratch. r0 is preserved and returned.
5271 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5272 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5273
5274 if (mode_ != NO_OVERWRITE) {
5275 __ bind(&have_to_allocate);
5276 // Get a new heap number in r5. r6 and r7 are scratch.
5277 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5278 __ jmp(&got_a_heap_number);
5279 }
5280
5281 // If all else failed then we go to the runtime system.
5282 __ bind(&slow);
5283 __ push(r1); // restore stack
5284 __ push(r0);
5285 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5286 switch (op_) {
5287 case Token::BIT_OR:
5288 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5289 break;
5290 case Token::BIT_AND:
5291 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5292 break;
5293 case Token::BIT_XOR:
5294 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5295 break;
5296 case Token::SAR:
5297 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5298 break;
5299 case Token::SHR:
5300 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5301 break;
5302 case Token::SHL:
5303 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5304 break;
5305 default:
5306 UNREACHABLE();
5307 }
5308}
5309
5310
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005311// Can we multiply by x with max two shifts and an add.
5312// This answers yes to all integers from 2 to 10.
5313static bool IsEasyToMultiplyBy(int x) {
5314 if (x < 2) return false; // Avoid special cases.
5315 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5316 if (IsPowerOf2(x)) return true; // Simple shift.
5317 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5318 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5319 return false;
5320}
5321
5322
5323// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5324// Source and destination may be the same register. This routine does
5325// not set carry and overflow the way a mul instruction would.
5326static void MultiplyByKnownInt(MacroAssembler* masm,
5327 Register source,
5328 Register destination,
5329 int known_int) {
5330 if (IsPowerOf2(known_int)) {
5331 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5332 } else if (PopCountLessThanEqual2(known_int)) {
5333 int first_bit = BitPosition(known_int);
5334 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5335 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5336 if (first_bit != 0) {
5337 __ mov(destination, Operand(destination, LSL, first_bit));
5338 }
5339 } else {
5340 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5341 int the_bit = BitPosition(known_int + 1);
5342 __ rsb(destination, source, Operand(source, LSL, the_bit));
5343 }
5344}
5345
5346
5347// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5348// a register for the cases where it doesn't know a good trick, and may deliver
5349// a result that needs shifting.
5350static void MultiplyByKnownInt2(
5351 MacroAssembler* masm,
5352 Register result,
5353 Register source,
5354 Register known_int_register, // Smi tagged.
5355 int known_int,
5356 int* required_shift) { // Including Smi tag shift
5357 switch (known_int) {
5358 case 3:
5359 __ add(result, source, Operand(source, LSL, 1));
5360 *required_shift = 1;
5361 break;
5362 case 5:
5363 __ add(result, source, Operand(source, LSL, 2));
5364 *required_shift = 1;
5365 break;
5366 case 6:
5367 __ add(result, source, Operand(source, LSL, 1));
5368 *required_shift = 2;
5369 break;
5370 case 7:
5371 __ rsb(result, source, Operand(source, LSL, 3));
5372 *required_shift = 1;
5373 break;
5374 case 9:
5375 __ add(result, source, Operand(source, LSL, 3));
5376 *required_shift = 1;
5377 break;
5378 case 10:
5379 __ add(result, source, Operand(source, LSL, 2));
5380 *required_shift = 2;
5381 break;
5382 default:
5383 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5384 __ mul(result, source, known_int_register);
5385 *required_shift = 0;
5386 }
5387}
5388
5389
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005390void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5391 // r1 : x
5392 // r0 : y
5393 // result : r0
5394
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005395 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5396 // tell us that.
5397 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5398
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005399 switch (op_) {
5400 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005401 Label not_smi;
5402 // Fast path.
5403 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005404 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005405 __ b(ne, &not_smi);
5406 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5407 // Return if no overflow.
5408 __ Ret(vc);
5409 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5410
5411 HandleBinaryOpSlowCases(masm,
5412 &not_smi,
5413 Builtins::ADD,
5414 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005415 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005416 break;
5417 }
5418
5419 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005420 Label not_smi;
5421 // Fast path.
5422 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005423 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005424 __ b(ne, &not_smi);
5425 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5426 // Return if no overflow.
5427 __ Ret(vc);
5428 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5429
5430 HandleBinaryOpSlowCases(masm,
5431 &not_smi,
5432 Builtins::SUB,
5433 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005434 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005435 break;
5436 }
5437
5438 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005439 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005440 ASSERT(kSmiTag == 0); // adjust code below
5441 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005442 __ b(ne, &not_smi);
5443 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005444 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005445 // Do multiplication
5446 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5447 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005448 __ mov(ip, Operand(r3, ASR, 31));
5449 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5450 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005451 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005452 __ tst(r3, Operand(r3));
5453 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005454 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005455 // We need -0 if we were multiplying a negative number with 0 to get 0.
5456 // We know one of them was zero.
5457 __ add(r2, r0, Operand(r1), SetCC);
5458 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5459 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5460 // Slow case. We fall through here if we multiplied a negative number
5461 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005462 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005463
5464 HandleBinaryOpSlowCases(masm,
5465 &not_smi,
5466 Builtins::MUL,
5467 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005468 mode_);
5469 break;
5470 }
5471
5472 case Token::DIV:
5473 case Token::MOD: {
5474 Label not_smi;
5475 if (specialized_on_rhs_) {
5476 Label smi_is_unsuitable;
5477 __ BranchOnNotSmi(r1, &not_smi);
5478 if (IsPowerOf2(constant_rhs_)) {
5479 if (op_ == Token::MOD) {
5480 __ and_(r0,
5481 r1,
5482 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5483 SetCC);
5484 // We now have the answer, but if the input was negative we also
5485 // have the sign bit. Our work is done if the result is
5486 // positive or zero:
5487 __ Ret(pl);
5488 // A mod of a negative left hand side must return a negative number.
5489 // Unfortunately if the answer is 0 then we must return -0. And we
5490 // already optimistically trashed r0 so we may need to restore it.
5491 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5492 // Next two instructions are conditional on the answer being -0.
5493 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5494 __ b(eq, &smi_is_unsuitable);
5495 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5496 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5497 } else {
5498 ASSERT(op_ == Token::DIV);
5499 __ tst(r1,
5500 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5501 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5502 int shift = 0;
5503 int d = constant_rhs_;
5504 while ((d & 1) == 0) {
5505 d >>= 1;
5506 shift++;
5507 }
5508 __ mov(r0, Operand(r1, LSR, shift));
5509 __ bic(r0, r0, Operand(kSmiTagMask));
5510 }
5511 } else {
5512 // Not a power of 2.
5513 __ tst(r1, Operand(0x80000000u));
5514 __ b(ne, &smi_is_unsuitable);
5515 // Find a fixed point reciprocal of the divisor so we can divide by
5516 // multiplying.
5517 double divisor = 1.0 / constant_rhs_;
5518 int shift = 32;
5519 double scale = 4294967296.0; // 1 << 32.
5520 uint32_t mul;
5521 // Maximise the precision of the fixed point reciprocal.
5522 while (true) {
5523 mul = static_cast<uint32_t>(scale * divisor);
5524 if (mul >= 0x7fffffff) break;
5525 scale *= 2.0;
5526 shift++;
5527 }
5528 mul++;
5529 __ mov(r2, Operand(mul));
5530 __ umull(r3, r2, r2, r1);
5531 __ mov(r2, Operand(r2, LSR, shift - 31));
5532 // r2 is r1 / rhs. r2 is not Smi tagged.
5533 // r0 is still the known rhs. r0 is Smi tagged.
5534 // r1 is still the unkown lhs. r1 is Smi tagged.
5535 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5536 // r4 = r2 * r0.
5537 MultiplyByKnownInt2(masm,
5538 r4,
5539 r2,
5540 r0,
5541 constant_rhs_,
5542 &required_r4_shift);
5543 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5544 if (op_ == Token::DIV) {
5545 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5546 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5547 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5548 } else {
5549 ASSERT(op_ == Token::MOD);
5550 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5551 }
5552 }
5553 __ Ret();
5554 __ bind(&smi_is_unsuitable);
5555 } else {
5556 __ jmp(&not_smi);
5557 }
5558 HandleBinaryOpSlowCases(masm,
5559 &not_smi,
5560 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5561 op_,
5562 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005563 break;
5564 }
5565
5566 case Token::BIT_OR:
5567 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005568 case Token::BIT_XOR:
5569 case Token::SAR:
5570 case Token::SHR:
5571 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005572 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005573 ASSERT(kSmiTag == 0); // adjust code below
5574 __ tst(r2, Operand(kSmiTagMask));
5575 __ b(ne, &slow);
5576 switch (op_) {
5577 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5578 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5579 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005580 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005581 // Remove tags from right operand.
5582 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5583 // Use only the 5 least significant bits of the shift count.
5584 __ and_(r2, r2, Operand(0x1f));
5585 __ mov(r0, Operand(r1, ASR, r2));
5586 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005587 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005588 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005589 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005590 // Remove tags from operands. We can't do this on a 31 bit number
5591 // because then the 0s get shifted into bit 30 instead of bit 31.
5592 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5593 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5594 // Use only the 5 least significant bits of the shift count.
5595 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005596 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005597 // Unsigned shift is not allowed to produce a negative number, so
5598 // check the sign bit and the sign bit after Smi tagging.
5599 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005600 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005601 // Smi tag result.
5602 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005603 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005604 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005605 // Remove tags from operands.
5606 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5607 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5608 // Use only the 5 least significant bits of the shift count.
5609 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005610 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005611 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005612 __ add(r2, r3, Operand(0x40000000), SetCC);
5613 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005614 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005615 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005616 default: UNREACHABLE();
5617 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005618 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005619 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005620 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005621 break;
5622 }
5623
5624 default: UNREACHABLE();
5625 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005626 // This code should be unreachable.
5627 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005628}
5629
5630
5631void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005632 // Do tail-call to runtime routine. Runtime routines expect at least one
5633 // argument, so give it a Smi.
5634 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005635 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00005636 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005637
5638 __ StubReturn(1);
5639}
5640
5641
5642void UnarySubStub::Generate(MacroAssembler* masm) {
5643 Label undo;
5644 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005645 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005646
5647 // Enter runtime system if the value is not a smi.
5648 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005649 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005650
5651 // Enter runtime system if the value of the expression is zero
5652 // to make sure that we switch between 0 and -0.
5653 __ cmp(r0, Operand(0));
5654 __ b(eq, &slow);
5655
5656 // The value of the expression is a smi that is not zero. Try
5657 // optimistic subtraction '0 - value'.
5658 __ rsb(r1, r0, Operand(0), SetCC);
5659 __ b(vs, &slow);
5660
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005661 __ mov(r0, Operand(r1)); // Set r0 to result.
5662 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005663
5664 // Enter runtime system.
5665 __ bind(&slow);
5666 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005667 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005668 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5669
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005670 __ bind(&not_smi);
5671 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5672 __ b(ne, &slow);
5673 // r0 is a heap number. Get a new heap number in r1.
5674 if (overwrite_) {
5675 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5676 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5677 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5678 } else {
5679 AllocateHeapNumber(masm, &slow, r1, r2, r3);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005680 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005681 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005682 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005683 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5684 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5685 __ mov(r0, Operand(r1));
5686 }
5687 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005688}
5689
5690
ager@chromium.orga1645e22009-09-09 19:27:10 +00005691int CEntryStub::MinorKey() {
5692 ASSERT(result_size_ <= 2);
5693 // Result returned in r0 or r0+r1 by default.
5694 return 0;
5695}
5696
5697
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005698void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005699 // r0 holds the exception.
5700
5701 // Adjust this code if not the case.
5702 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5703
5704 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005705 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5706 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005707
5708 // Restore the next handler and frame pointer, discard handler state.
5709 ASSERT(StackHandlerConstants::kNextOffset == 0);
5710 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005711 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005712 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5713 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5714
5715 // Before returning we restore the context from the frame pointer if
5716 // not NULL. The frame pointer is NULL in the exception handler of a
5717 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005718 __ cmp(fp, Operand(0));
5719 // Set cp to NULL if fp is NULL.
5720 __ mov(cp, Operand(0), LeaveCC, eq);
5721 // Restore cp otherwise.
5722 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005723#ifdef DEBUG
5724 if (FLAG_debug_code) {
5725 __ mov(lr, Operand(pc));
5726 }
5727#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005728 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005729 __ pop(pc);
5730}
5731
5732
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005733void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
5734 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005735 // Adjust this code if not the case.
5736 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5737
5738 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005739 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005740 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005741
5742 // Unwind the handlers until the ENTRY handler is found.
5743 Label loop, done;
5744 __ bind(&loop);
5745 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005746 const int kStateOffset = StackHandlerConstants::kStateOffset;
5747 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005748 __ cmp(r2, Operand(StackHandler::ENTRY));
5749 __ b(eq, &done);
5750 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005751 const int kNextOffset = StackHandlerConstants::kNextOffset;
5752 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005753 __ jmp(&loop);
5754 __ bind(&done);
5755
5756 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005757 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005758 __ pop(r2);
5759 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005760
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005761 if (type == OUT_OF_MEMORY) {
5762 // Set external caught exception to false.
5763 ExternalReference external_caught(Top::k_external_caught_exception_address);
5764 __ mov(r0, Operand(false));
5765 __ mov(r2, Operand(external_caught));
5766 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005767
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005768 // Set pending exception and r0 to out of memory exception.
5769 Failure* out_of_memory = Failure::OutOfMemoryException();
5770 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5771 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5772 __ str(r0, MemOperand(r2));
5773 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005774
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005775 // Stack layout at this point. See also StackHandlerConstants.
5776 // sp -> state (ENTRY)
5777 // fp
5778 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005779
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005780 // Discard handler state (r2 is not used) and restore frame pointer.
5781 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5782 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5783 // Before returning we restore the context from the frame pointer if
5784 // not NULL. The frame pointer is NULL in the exception handler of a
5785 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005786 __ cmp(fp, Operand(0));
5787 // Set cp to NULL if fp is NULL.
5788 __ mov(cp, Operand(0), LeaveCC, eq);
5789 // Restore cp otherwise.
5790 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005791#ifdef DEBUG
5792 if (FLAG_debug_code) {
5793 __ mov(lr, Operand(pc));
5794 }
5795#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005796 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005797 __ pop(pc);
5798}
5799
5800
5801void CEntryStub::GenerateCore(MacroAssembler* masm,
5802 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005803 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005804 Label* throw_out_of_memory_exception,
5805 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005806 bool do_gc,
5807 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005808 // r0: result parameter for PerformGC, if any
5809 // r4: number of arguments including receiver (C callee-saved)
5810 // r5: pointer to builtin function (C callee-saved)
5811 // r6: pointer to the first argument (C callee-saved)
5812
5813 if (do_gc) {
5814 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005815 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5816 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005817 }
5818
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005819 ExternalReference scope_depth =
5820 ExternalReference::heap_always_allocate_scope_depth();
5821 if (always_allocate) {
5822 __ mov(r0, Operand(scope_depth));
5823 __ ldr(r1, MemOperand(r0));
5824 __ add(r1, r1, Operand(1));
5825 __ str(r1, MemOperand(r0));
5826 }
5827
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005828 // Call C built-in.
5829 // r0 = argc, r1 = argv
5830 __ mov(r0, Operand(r4));
5831 __ mov(r1, Operand(r6));
5832
5833 // TODO(1242173): To let the GC traverse the return address of the exit
5834 // frames, we need to know where the return address is. Right now,
5835 // we push it on the stack to be able to find it again, but we never
5836 // restore from it in case of changes, which makes it impossible to
5837 // support moving the C entry code stub. This should be fixed, but currently
5838 // this is OK because the CEntryStub gets generated so early in the V8 boot
5839 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005840 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5841 masm->push(lr);
5842 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005843
5844 if (always_allocate) {
5845 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5846 // though (contain the result).
5847 __ mov(r2, Operand(scope_depth));
5848 __ ldr(r3, MemOperand(r2));
5849 __ sub(r3, r3, Operand(1));
5850 __ str(r3, MemOperand(r2));
5851 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005852
5853 // check for failure result
5854 Label failure_returned;
5855 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5856 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5857 __ add(r2, r0, Operand(1));
5858 __ tst(r2, Operand(kFailureTagMask));
5859 __ b(eq, &failure_returned);
5860
5861 // Exit C frame and return.
5862 // r0:r1: result
5863 // sp: stack pointer
5864 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005865 __ LeaveExitFrame(frame_type);
5866
5867 // check if we should retry or throw exception
5868 Label retry;
5869 __ bind(&failure_returned);
5870 ASSERT(Failure::RETRY_AFTER_GC == 0);
5871 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5872 __ b(eq, &retry);
5873
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005874 // Special handling of out of memory exceptions.
5875 Failure* out_of_memory = Failure::OutOfMemoryException();
5876 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5877 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005878
5879 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005880 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005881 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005882 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005883 __ ldr(r0, MemOperand(ip));
5884 __ str(r3, MemOperand(ip));
5885
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005886 // Special handling of termination exceptions which are uncatchable
5887 // by javascript code.
5888 __ cmp(r0, Operand(Factory::termination_exception()));
5889 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005890
5891 // Handle normal exception.
5892 __ jmp(throw_normal_exception);
5893
5894 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5895}
5896
5897
5898void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5899 // Called from JavaScript; parameters are on stack as if calling JS function
5900 // r0: number of arguments including receiver
5901 // r1: pointer to builtin function
5902 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005903 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005904 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005905
5906 // NOTE: Invocations of builtins may return failure objects
5907 // instead of a proper result. The builtin entry handles
5908 // this by performing a garbage collection and retrying the
5909 // builtin once.
5910
5911 StackFrame::Type frame_type = is_debug_break
5912 ? StackFrame::EXIT_DEBUG
5913 : StackFrame::EXIT;
5914
5915 // Enter the exit frame that transitions from JavaScript to C++.
5916 __ EnterExitFrame(frame_type);
5917
5918 // r4: number of arguments (C callee-saved)
5919 // r5: pointer to builtin function (C callee-saved)
5920 // r6: pointer to first argument (C callee-saved)
5921
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005922 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005923 Label throw_termination_exception;
5924 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005925
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005926 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005927 GenerateCore(masm,
5928 &throw_normal_exception,
5929 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005930 &throw_out_of_memory_exception,
5931 frame_type,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005932 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005933 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005934
5935 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005936 GenerateCore(masm,
5937 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005938 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005939 &throw_out_of_memory_exception,
5940 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005941 true,
5942 false);
5943
5944 // Do full GC and retry runtime call one final time.
5945 Failure* failure = Failure::InternalError();
5946 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5947 GenerateCore(masm,
5948 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005949 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005950 &throw_out_of_memory_exception,
5951 frame_type,
5952 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005953 true);
5954
5955 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005956 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
5957
5958 __ bind(&throw_termination_exception);
5959 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005960
5961 __ bind(&throw_normal_exception);
5962 GenerateThrowTOS(masm);
5963}
5964
5965
5966void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5967 // r0: code entry
5968 // r1: function
5969 // r2: receiver
5970 // r3: argc
5971 // [sp+0]: argv
5972
5973 Label invoke, exit;
5974
5975 // Called from C, so do not pop argc and args on exit (preserve sp)
5976 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005977 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005978 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5979
5980 // Get address of argv, see stm above.
5981 // r0: code entry
5982 // r1: function
5983 // r2: receiver
5984 // r3: argc
5985 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5986 __ ldr(r4, MemOperand(r4)); // argv
5987
5988 // Push a frame with special values setup to mark it as an entry frame.
5989 // r0: code entry
5990 // r1: function
5991 // r2: receiver
5992 // r3: argc
5993 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005994 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005995 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5996 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005997 __ mov(r6, Operand(Smi::FromInt(marker)));
5998 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5999 __ ldr(r5, MemOperand(r5));
6000 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6001
6002 // Setup frame pointer for the frame to be pushed.
6003 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6004
6005 // Call a faked try-block that does the invoke.
6006 __ bl(&invoke);
6007
6008 // Caught exception: Store result (exception) in the pending
6009 // exception field in the JSEnv and return a failure sentinel.
6010 // Coming in here the fp will be invalid because the PushTryHandler below
6011 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006012 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006013 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006014 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006015 __ b(&exit);
6016
6017 // Invoke: Link this frame into the handler chain.
6018 __ bind(&invoke);
6019 // Must preserve r0-r4, r5-r7 are available.
6020 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006021 // If an exception not caught by another handler occurs, this handler
6022 // returns control to the code after the bl(&invoke) above, which
6023 // restores all kCalleeSaved registers (including cp and fp) to their
6024 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006025
6026 // Clear any pending exceptions.
6027 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6028 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006029 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006030 __ str(r5, MemOperand(ip));
6031
6032 // Invoke the function by calling through JS entry trampoline builtin.
6033 // Notice that we cannot store a reference to the trampoline code directly in
6034 // this stub, because runtime stubs are not traversed when doing GC.
6035
6036 // Expected registers by Builtins::JSEntryTrampoline
6037 // r0: code entry
6038 // r1: function
6039 // r2: receiver
6040 // r3: argc
6041 // r4: argv
6042 if (is_construct) {
6043 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6044 __ mov(ip, Operand(construct_entry));
6045 } else {
6046 ExternalReference entry(Builtins::JSEntryTrampoline);
6047 __ mov(ip, Operand(entry));
6048 }
6049 __ ldr(ip, MemOperand(ip)); // deref address
6050
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006051 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6052 // macro for the add instruction because we don't want the coverage tool
6053 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006054 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006055 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006056
6057 // Unlink this frame from the handler chain. When reading the
6058 // address of the next handler, there is no need to use the address
6059 // displacement since the current stack pointer (sp) points directly
6060 // to the stack handler.
6061 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6062 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6063 __ str(r3, MemOperand(ip));
6064 // No need to restore registers
6065 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6066
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006067
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006068 __ bind(&exit); // r0 holds result
6069 // Restore the top frame descriptors from the stack.
6070 __ pop(r3);
6071 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6072 __ str(r3, MemOperand(ip));
6073
6074 // Reset the stack to the callee saved registers.
6075 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6076
6077 // Restore callee-saved registers and return.
6078#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006079 if (FLAG_debug_code) {
6080 __ mov(lr, Operand(pc));
6081 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006082#endif
6083 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6084}
6085
6086
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006087// This stub performs an instanceof, calling the builtin function if
6088// necessary. Uses r1 for the object, r0 for the function that it may
6089// be an instance of (these are fetched from the stack).
6090void InstanceofStub::Generate(MacroAssembler* masm) {
6091 // Get the object - slow case for smis (we may need to throw an exception
6092 // depending on the rhs).
6093 Label slow, loop, is_instance, is_not_instance;
6094 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6095 __ BranchOnSmi(r0, &slow);
6096
6097 // Check that the left hand is a JS object and put map in r3.
6098 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6099 __ b(lt, &slow);
6100 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6101 __ b(gt, &slow);
6102
6103 // Get the prototype of the function (r4 is result, r2 is scratch).
6104 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6105 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6106
6107 // Check that the function prototype is a JS object.
6108 __ BranchOnSmi(r4, &slow);
6109 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6110 __ b(lt, &slow);
6111 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6112 __ b(gt, &slow);
6113
6114 // Register mapping: r3 is object map and r4 is function prototype.
6115 // Get prototype of object into r2.
6116 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6117
6118 // Loop through the prototype chain looking for the function prototype.
6119 __ bind(&loop);
6120 __ cmp(r2, Operand(r4));
6121 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006122 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6123 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006124 __ b(eq, &is_not_instance);
6125 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6126 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6127 __ jmp(&loop);
6128
6129 __ bind(&is_instance);
6130 __ mov(r0, Operand(Smi::FromInt(0)));
6131 __ pop();
6132 __ pop();
6133 __ mov(pc, Operand(lr)); // Return.
6134
6135 __ bind(&is_not_instance);
6136 __ mov(r0, Operand(Smi::FromInt(1)));
6137 __ pop();
6138 __ pop();
6139 __ mov(pc, Operand(lr)); // Return.
6140
6141 // Slow-case. Tail call builtin.
6142 __ bind(&slow);
6143 __ mov(r0, Operand(1)); // Arg count without receiver.
6144 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6145}
6146
6147
ager@chromium.org7c537e22008-10-16 08:43:32 +00006148void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006149 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006150 Label adaptor;
6151 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6152 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006153 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006154 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155
ager@chromium.org7c537e22008-10-16 08:43:32 +00006156 // Nothing to do: The formal number of parameters has already been
6157 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006158 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006159
ager@chromium.org7c537e22008-10-16 08:43:32 +00006160 // Arguments adaptor case: Read the arguments length from the
6161 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006162 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006163 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006164 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006165}
6166
6167
ager@chromium.org7c537e22008-10-16 08:43:32 +00006168void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6169 // The displacement is the offset of the last parameter (if any)
6170 // relative to the frame pointer.
6171 static const int kDisplacement =
6172 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006173
ager@chromium.org7c537e22008-10-16 08:43:32 +00006174 // Check that the key is a smi.
6175 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006176 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006177
ager@chromium.org7c537e22008-10-16 08:43:32 +00006178 // Check if the calling frame is an arguments adaptor frame.
6179 Label adaptor;
6180 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6181 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006182 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006183 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006184
ager@chromium.org7c537e22008-10-16 08:43:32 +00006185 // Check index against formal parameters count limit passed in
6186 // through register eax. Use unsigned comparison to get negative
6187 // check for free.
6188 __ cmp(r1, r0);
6189 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006190
ager@chromium.org7c537e22008-10-16 08:43:32 +00006191 // Read the argument from the stack and return it.
6192 __ sub(r3, r0, r1);
6193 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6194 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006195 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006196
6197 // Arguments adaptor case: Check index against actual arguments
6198 // limit found in the arguments adaptor frame. Use unsigned
6199 // comparison to get negative check for free.
6200 __ bind(&adaptor);
6201 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6202 __ cmp(r1, r0);
6203 __ b(cs, &slow);
6204
6205 // Read the argument from the adaptor frame and return it.
6206 __ sub(r3, r0, r1);
6207 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6208 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006209 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006210
6211 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6212 // by calling the runtime system.
6213 __ bind(&slow);
6214 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006215 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006216}
6217
6218
6219void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6220 // Check if the calling frame is an arguments adaptor frame.
6221 Label runtime;
6222 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6223 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006224 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006225 __ b(ne, &runtime);
6226
6227 // Patch the arguments.length and the parameters pointer.
6228 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6229 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6230 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6231 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6232 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6233
6234 // Do the runtime call to allocate the arguments object.
6235 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006236 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006237}
6238
6239
6240void CallFunctionStub::Generate(MacroAssembler* masm) {
6241 Label slow;
6242 // Get the function to call from the stack.
6243 // function, receiver [, arguments]
6244 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6245
6246 // Check that the function is really a JavaScript function.
6247 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006248 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006249 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006250 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006251 __ b(ne, &slow);
6252
6253 // Fast-case: Invoke the function now.
6254 // r1: pushed function
6255 ParameterCount actual(argc_);
6256 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6257
6258 // Slow-case: Non-function called.
6259 __ bind(&slow);
6260 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006261 __ mov(r2, Operand(0));
6262 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6263 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6264 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006265}
6266
6267
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006268int CompareStub::MinorKey() {
6269 // Encode the two parameters in a unique 16 bit value.
6270 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6271 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6272}
6273
6274
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006275#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006277} } // namespace v8::internal