blob: d3507ec0e370af73ed96e4590d0b8f9fa5fe96e2 [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
179 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 // Initialize the function return target after the locals are set
182 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000183 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000186 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000187 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 // Allocate local context.
189 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000190 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 frame_->EmitPush(r0);
192 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000193
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000194#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000195 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000196 __ cmp(r0, Operand(cp));
197 verified_true.Branch(eq);
198 __ stop("NewContext: r0 is expected to be the same as cp");
199 verified_true.Bind();
200#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000202 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 }
204
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 // 1) only needed if we have a context
207 // 2) no need to recompute context ptr every single time
208 // 3) don't copy parameter operand code from SlotOperand!
209 {
210 Comment cmnt2(masm_, "[ copy context parameters into .context");
211
212 // Note that iteration order is relevant here! If we have the same
213 // parameter twice (e.g., function (x, y, x)), and that parameter
214 // needs to be copied into the context, it must be the last argument
215 // passed to the parameter that needs to be copied. This is a rare
216 // case so we don't check for it, instead we rely on the copying
217 // order: such a parameter is copied repeatedly into the same
218 // context location and thus the last value is what is seen inside
219 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 for (int i = 0; i < scope_->num_parameters(); i++) {
221 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 Slot* slot = par->slot();
223 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000224 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000225 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 // Loads r2 with context; used below in RecordWrite.
227 __ str(r1, SlotOperand(slot, r2));
228 // Load the offset into r3.
229 int slot_offset =
230 FixedArray::kHeaderSize + slot->index() * kPointerSize;
231 __ mov(r3, Operand(slot_offset));
232 __ RecordWrite(r2, r3, r1);
233 }
234 }
235 }
236
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000237 // Store the arguments object. This must happen after context
238 // initialization because the arguments object may be stored in the
239 // context.
240 if (scope_->arguments() != NULL) {
241 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000243 { Reference shadow_ref(this, scope_->arguments_shadow());
244 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000245 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000247 // The receiver is below the arguments, the return address,
248 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000249 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000250 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000251 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000252 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000253 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254 frame_->CallStub(&stub, 3);
255 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000256 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000257 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000258 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000260 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 }
262
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000263 // Generate code to 'execute' declarations and initialize functions
264 // (source elements). In case of an illegal redeclaration we need to
265 // handle that instead of processing the declarations.
266 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000268 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 } else {
270 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000271 ProcessDeclarations(scope_->declarations());
272 // Bail out if a stack-overflow exception occurred when processing
273 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000274 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 }
276
mads.s.ager31e71382008-08-13 09:32:07 +0000277 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000278 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000279 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000280 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 CheckStack();
282
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.
302 if (frame_ != NULL || function_return_.is_linked()) {
303 // exit
304 // r0: result
305 // sp: stack pointer
306 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 // cp: callee's context
308 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000309
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 function_return_.Bind();
311 if (FLAG_trace) {
312 // Push the return value on the stack as the parameter.
313 // Runtime::TraceExit returns the parameter as it is.
314 frame_->EmitPush(r0);
315 frame_->CallRuntime(Runtime::kTraceExit, 1);
316 }
317
318 // Tear down the frame which will restore the caller's frame pointer and
319 // the link register.
320 frame_->Exit();
321
322 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000323 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000324 }
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 ASSERT(!has_cc());
328 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 ASSERT(!function_return_is_shadowed_);
330 function_return_.Unuse();
331 DeleteFrame();
332
333 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000334 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000335 ProcessDeferred();
336 }
337
338 allocator_ = NULL;
339 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340}
341
342
ager@chromium.org7c537e22008-10-16 08:43:32 +0000343MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
344 // Currently, this assertion will fail if we try to assign to
345 // a constant variable that is constant because it is read-only
346 // (such as the variable referring to a named function expression).
347 // We need to implement assignments to read-only variables.
348 // Ideally, we should do this during AST generation (by converting
349 // such assignments into expression statements); however, in general
350 // we may not be able to make the decision until past AST generation,
351 // that is when the entire program is known.
352 ASSERT(slot != NULL);
353 int index = slot->index();
354 switch (slot->type()) {
355 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000356 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000358 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000359 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000360
361 case Slot::CONTEXT: {
362 // Follow the context chain if necessary.
363 ASSERT(!tmp.is(cp)); // do not overwrite context register
364 Register context = cp;
365 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000366 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000367 // Load the closure.
368 // (All contexts, even 'with' contexts, have a closure,
369 // and it is the same for all contexts inside a function.
370 // There is no need to go to the function context first.)
371 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
372 // Load the function context (which is the incoming, outer context).
373 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
374 context = tmp;
375 }
376 // We may have a 'with' context now. Get the function context.
377 // (In fact this mov may never be the needed, since the scope analysis
378 // may not permit a direct context access in this case and thus we are
379 // always at a function context. However it is safe to dereference be-
380 // cause the function context of a function context is itself. Before
381 // deleting this mov we should try to create a counter-example first,
382 // though...)
383 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
384 return ContextOperand(tmp, index);
385 }
386
387 default:
388 UNREACHABLE();
389 return MemOperand(r0, 0);
390 }
391}
392
393
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
395 Slot* slot,
396 Register tmp,
397 Register tmp2,
398 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000399 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000400 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000401
ager@chromium.org381abbb2009-02-25 13:23:22 +0000402 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
403 if (s->num_heap_slots() > 0) {
404 if (s->calls_eval()) {
405 // Check that extension is NULL.
406 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
407 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000408 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000409 }
410 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
411 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
412 context = tmp;
413 }
414 }
415 // Check that last extension is NULL.
416 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
417 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000418 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000419 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000420 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000421}
422
423
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000424// Loads a value on TOS. If it is a boolean value, the result may have been
425// (partially) translated into branches, or it may have set the condition
426// code register. If force_cc is set, the value is forced to set the
427// condition code register and no value is pushed. If the condition code
428// register was set, has_cc() is true and cc_reg_ contains the condition to
429// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000430void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000431 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432 JumpTarget* true_target,
433 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000434 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000435 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
ager@chromium.org7c537e22008-10-16 08:43:32 +0000438 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000439 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000440
441 // If we hit a stack overflow, we may not have actually visited
442 // the expression. In that case, we ensure that we have a
443 // valid-looking frame state because we will continue to generate
444 // code as we unwind the C++ stack.
445 //
446 // It's possible to have both a stack overflow and a valid frame
447 // state (eg, a subexpression overflowed, visiting it returned
448 // with a dummied frame state, and visiting this expression
449 // returned with a normal-looking state).
450 if (HasStackOverflow() &&
451 has_valid_frame() &&
452 !has_cc() &&
453 frame_->height() == original_height) {
454 true_target->Jump();
455 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000456 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000457 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000458 // Convert the TOS value to a boolean in the condition code register.
459 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461 ASSERT(!force_cc || !has_valid_frame() || has_cc());
462 ASSERT(!has_valid_frame() ||
463 (has_cc() && frame_->height() == original_height) ||
464 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465}
466
467
ager@chromium.org7c537e22008-10-16 08:43:32 +0000468void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469#ifdef DEBUG
470 int original_height = frame_->height();
471#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000472 JumpTarget true_target;
473 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000474 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000477 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000478 JumpTarget loaded;
479 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000481 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000482 frame_->EmitPush(r0);
483 loaded.Jump();
484 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000485 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000486 frame_->EmitPush(r0);
487 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 cc_reg_ = al;
489 }
490
491 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000492 // We have at least one condition value that has been "translated"
493 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000494 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495 if (frame_ != NULL) {
496 loaded.Jump(); // Don't lose the current TOS.
497 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000501 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000502 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000503 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505 // If both "true" and "false" need to be loaded jump across the code for
506 // "false".
507 if (both) {
508 loaded.Jump();
509 }
510 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000512 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000513 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 // A value is loaded on all paths reaching this point.
517 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522}
523
524
ager@chromium.org7c537e22008-10-16 08:43:32 +0000525void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000526 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000527 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529}
530
531
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000532void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000534 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
535 __ ldr(scratch,
536 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000538}
539
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
543// variables w/o reference errors elsewhere.
544void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000545 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 Variable* variable = x->AsVariableProxy()->AsVariable();
547 if (variable != NULL && !variable->is_this() && variable->is_global()) {
548 // NOTE: This is somewhat nasty. We force the compiler to load
549 // the variable as if through '<global>.<variable>' to make sure we
550 // do not get reference errors.
551 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
552 Literal key(variable->name());
553 // TODO(1241834): Fetch the position from the variable instead of using
554 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000555 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
560}
561
562
ager@chromium.org7c537e22008-10-16 08:43:32 +0000563Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
565 cgen->LoadReference(this);
566}
567
568
569Reference::~Reference() {
570 cgen_->UnloadReference(this);
571}
572
573
ager@chromium.org7c537e22008-10-16 08:43:32 +0000574void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000575 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000576 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 Expression* e = ref->expression();
578 Property* property = e->AsProperty();
579 Variable* var = e->AsVariableProxy()->AsVariable();
580
581 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000582 // The expression is either a property or a variable proxy that rewrites
583 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000585 // We use a named reference if the key is a literal symbol, unless it is
586 // a string that can be legally parsed as an integer. This is because
587 // otherwise we will not get into the slow case code that handles [] on
588 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 Literal* literal = property->key()->AsLiteral();
590 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000591 if (literal != NULL &&
592 literal->handle()->IsSymbol() &&
593 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 ref->set_type(Reference::NAMED);
595 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000596 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 ref->set_type(Reference::KEYED);
598 }
599 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000600 // The expression is a variable proxy that does not rewrite to a
601 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 LoadGlobal();
604 ref->set_type(Reference::NAMED);
605 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000606 ASSERT(var->slot() != NULL);
607 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 }
609 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000610 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000611 LoadAndSpill(e);
612 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 }
614}
615
616
ager@chromium.org7c537e22008-10-16 08:43:32 +0000617void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000618 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000619 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000620 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000622 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000623 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000624 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000625 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 }
627}
628
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
631// register to a boolean in the condition code register. The code
632// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000633void CodeGenerator::ToBoolean(JumpTarget* true_target,
634 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000635 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000636 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000638 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639
640 // Fast case checks
641
mads.s.ager31e71382008-08-13 09:32:07 +0000642 // Check if the value is 'false'.
643 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000644 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
mads.s.ager31e71382008-08-13 09:32:07 +0000646 // Check if the value is 'true'.
647 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649
mads.s.ager31e71382008-08-13 09:32:07 +0000650 // Check if the value is 'undefined'.
651 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000652 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
mads.s.ager31e71382008-08-13 09:32:07 +0000654 // Check if the value is a smi.
655 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000656 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000657 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000658 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
660 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000661 frame_->EmitPush(r0);
662 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000663 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
666 cc_reg_ = ne;
667}
668
669
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000670void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000671 OverwriteMode overwrite_mode,
672 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000673 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000674 // sp[0] : y
675 // sp[1] : x
676 // result : r0
677
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 // Stub is entered with a call: 'return address' is in lr.
679 switch (op) {
680 case Token::ADD: // fall through.
681 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000682 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000683 case Token::DIV:
684 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000685 case Token::BIT_OR:
686 case Token::BIT_AND:
687 case Token::BIT_XOR:
688 case Token::SHL:
689 case Token::SHR:
690 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000691 frame_->EmitPop(r0); // r0 : y
692 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000693 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000694 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695 break;
696 }
697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000699 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000701 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 break;
703
704 default:
705 // Other cases should have been handled before this point.
706 UNREACHABLE();
707 break;
708 }
709}
710
711
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000712class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000713 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000714 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000715 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000716 bool reversed,
717 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000718 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000720 reversed_(reversed),
721 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000722 set_comment("[ DeferredInlinedSmiOperation");
723 }
724
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000725 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000726
727 private:
728 Token::Value op_;
729 int value_;
730 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000731 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000732};
733
734
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000735void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000736 switch (op_) {
737 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000738 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000739 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000740 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
741 __ mov(r1, Operand(Smi::FromInt(value_)));
742 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
744 __ mov(r0, Operand(Smi::FromInt(value_)));
745 }
746 break;
747 }
748
749 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000750 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000751 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000752 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
753 __ mov(r1, Operand(Smi::FromInt(value_)));
754 } else {
755 __ add(r1, r0, Operand(Smi::FromInt(value_)));
756 __ mov(r0, Operand(Smi::FromInt(value_)));
757 }
758 break;
759 }
760
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000761 // For these operations there is no optimistic operation that needs to be
762 // reverted.
763 case Token::MUL:
764 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000765 case Token::BIT_OR:
766 case Token::BIT_XOR:
767 case Token::BIT_AND: {
768 if (reversed_) {
769 __ mov(r1, Operand(Smi::FromInt(value_)));
770 } else {
771 __ mov(r1, Operand(r0));
772 __ mov(r0, Operand(Smi::FromInt(value_)));
773 }
774 break;
775 }
776
777 case Token::SHL:
778 case Token::SHR:
779 case Token::SAR: {
780 if (!reversed_) {
781 __ mov(r1, Operand(r0));
782 __ mov(r0, Operand(Smi::FromInt(value_)));
783 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000784 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000785 }
786 break;
787 }
788
789 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000790 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000791 UNREACHABLE();
792 break;
793 }
794
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000795 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000796 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000797}
798
799
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000800static bool PopCountLessThanEqual2(unsigned int x) {
801 x &= x - 1;
802 return (x & (x - 1)) == 0;
803}
804
805
806// Returns the index of the lowest bit set.
807static int BitPosition(unsigned x) {
808 int bit_posn = 0;
809 while ((x & 0xf) == 0) {
810 bit_posn += 4;
811 x >>= 4;
812 }
813 while ((x & 1) == 0) {
814 bit_posn++;
815 x >>= 1;
816 }
817 return bit_posn;
818}
819
820
ager@chromium.org7c537e22008-10-16 08:43:32 +0000821void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000822 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000823 bool reversed,
824 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000825 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 // NOTE: This is an attempt to inline (a bit) more of the code for
827 // some possible smi operations (like + and -) when (at least) one
828 // of the operands is a literal smi. With this optimization, the
829 // performance of the system is increased by ~15%, and the generated
830 // code size is increased by ~1% (measured on a combination of
831 // different benchmarks).
832
mads.s.ager31e71382008-08-13 09:32:07 +0000833 // sp[0] : operand
834
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000835 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000837 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000838 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000840 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 switch (op) {
842 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000843 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000844 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000846 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000847 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000849 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 break;
852 }
853
854 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000855 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000856 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
ager@chromium.orge2902be2009-06-08 12:21:35 +0000858 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000859 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000860 } else {
861 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000863 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000864 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000865 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000866 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000867 break;
868 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000870
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000871 case Token::BIT_OR:
872 case Token::BIT_XOR:
873 case Token::BIT_AND: {
874 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000875 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000876 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000877 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000878 switch (op) {
879 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
880 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
881 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
882 default: UNREACHABLE();
883 }
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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000888 case Token::SHL:
889 case Token::SHR:
890 case Token::SAR: {
891 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000892 something_to_inline = false;
893 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000894 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000895 int shift_value = int_value & 0x1f; // least significant 5 bits
896 DeferredCode* deferred =
897 new DeferredInlineSmiOperation(op, shift_value, false, mode);
898 __ tst(r0, Operand(kSmiTagMask));
899 deferred->Branch(ne);
900 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
901 switch (op) {
902 case Token::SHL: {
903 if (shift_value != 0) {
904 __ mov(r2, Operand(r2, LSL, shift_value));
905 }
906 // check that the *unsigned* result fits in a smi
907 __ add(r3, r2, Operand(0x40000000), SetCC);
908 deferred->Branch(mi);
909 break;
910 }
911 case Token::SHR: {
912 // LSR by immediate 0 means shifting 32 bits.
913 if (shift_value != 0) {
914 __ mov(r2, Operand(r2, LSR, shift_value));
915 }
916 // check that the *unsigned* result fits in a smi
917 // neither of the two high-order bits can be set:
918 // - 0x80000000: high bit would be lost when smi tagging
919 // - 0x40000000: this number would convert to negative when
920 // smi tagging these two cases can only happen with shifts
921 // by 0 or 1 when handed a valid smi
922 __ and_(r3, r2, Operand(0xc0000000), SetCC);
923 deferred->Branch(ne);
924 break;
925 }
926 case Token::SAR: {
927 if (shift_value != 0) {
928 // ASR by immediate 0 means shifting 32 bits.
929 __ mov(r2, Operand(r2, ASR, shift_value));
930 }
931 break;
932 }
933 default: UNREACHABLE();
934 }
935 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
936 deferred->BindExit();
937 break;
938 }
939
940 case Token::MOD: {
941 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
942 something_to_inline = false;
943 break;
944 }
945 DeferredCode* deferred =
946 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
947 unsigned mask = (0x80000000u | kSmiTagMask);
948 __ tst(r0, Operand(mask));
949 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
950 mask = (int_value << kSmiTagSize) - 1;
951 __ and_(r0, r0, Operand(mask));
952 deferred->BindExit();
953 break;
954 }
955
956 case Token::MUL: {
957 if (!IsEasyToMultiplyBy(int_value)) {
958 something_to_inline = false;
959 break;
960 }
961 DeferredCode* deferred =
962 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
963 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
964 max_smi_that_wont_overflow <<= kSmiTagSize;
965 unsigned mask = 0x80000000u;
966 while ((mask & max_smi_that_wont_overflow) == 0) {
967 mask |= mask >> 1;
968 }
969 mask |= kSmiTagMask;
970 // This does a single mask that checks for a too high value in a
971 // conservative way and for a non-Smi. It also filters out negative
972 // numbers, unfortunately, but since this code is inline we prefer
973 // brevity to comprehensiveness.
974 __ tst(r0, Operand(mask));
975 deferred->Branch(ne);
976 MultiplyByKnownInt(masm_, r0, r0, int_value);
977 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 break;
979 }
980
981 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000982 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 break;
984 }
985
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000986 if (!something_to_inline) {
987 if (!reversed) {
988 frame_->EmitPush(r0);
989 __ mov(r0, Operand(value));
990 frame_->EmitPush(r0);
991 GenericBinaryOperation(op, mode, int_value);
992 } else {
993 __ mov(ip, Operand(value));
994 frame_->EmitPush(ip);
995 frame_->EmitPush(r0);
996 GenericBinaryOperation(op, mode, kUnknownIntValue);
997 }
998 }
999
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001000 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001}
1002
1003
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001004void CodeGenerator::Comparison(Condition cc,
1005 Expression* left,
1006 Expression* right,
1007 bool strict) {
1008 if (left != NULL) LoadAndSpill(left);
1009 if (right != NULL) LoadAndSpill(right);
1010
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001011 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001012 // sp[0] : y
1013 // sp[1] : x
1014 // result : cc register
1015
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 // Strict only makes sense for equality comparisons.
1017 ASSERT(!strict || cc == eq);
1018
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001019 JumpTarget exit;
1020 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001021 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1022 if (cc == gt || cc == le) {
1023 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001024 frame_->EmitPop(r1);
1025 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001026 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001027 frame_->EmitPop(r0);
1028 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001029 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 __ orr(r2, r0, Operand(r1));
1031 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001032 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001034 // Perform non-smi comparison by stub.
1035 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1036 // We call with 0 args because there are 0 on the stack.
1037 CompareStub stub(cc, strict);
1038 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001039 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001040 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001042 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001043 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 __ cmp(r1, Operand(r0));
1045
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001046 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 cc_reg_ = cc;
1048}
1049
1050
kasper.lund7276f142008-07-30 08:49:36 +00001051class CallFunctionStub: public CodeStub {
1052 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001053 CallFunctionStub(int argc, InLoopFlag in_loop)
1054 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001055
1056 void Generate(MacroAssembler* masm);
1057
1058 private:
1059 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001060 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001061
kasper.lund7276f142008-07-30 08:49:36 +00001062#if defined(DEBUG)
1063 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1064#endif // defined(DEBUG)
1065
1066 Major MajorKey() { return CallFunction; }
1067 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001068 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001069};
1070
1071
mads.s.ager31e71382008-08-13 09:32:07 +00001072// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001073void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001075 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001077 int arg_count = args->length();
1078 for (int i = 0; i < arg_count; i++) {
1079 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001080 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081
kasper.lund7276f142008-07-30 08:49:36 +00001082 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001083 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
kasper.lund7276f142008-07-30 08:49:36 +00001085 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001086 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1087 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001088 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
1090 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001091 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001092 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093}
1094
1095
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001096void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001097 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 ASSERT(has_cc());
1099 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 cc_reg_ = al;
1102}
1103
1104
ager@chromium.org7c537e22008-10-16 08:43:32 +00001105void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001106 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 if (FLAG_check_stack) {
1108 Comment cmnt(masm_, "[ check stack");
1109 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001110 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 }
1112}
1113
1114
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001115void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1116#ifdef DEBUG
1117 int original_height = frame_->height();
1118#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001119 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001120 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1121 VisitAndSpill(statements->at(i));
1122 }
1123 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1124}
1125
1126
ager@chromium.org7c537e22008-10-16 08:43:32 +00001127void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001128#ifdef DEBUG
1129 int original_height = frame_->height();
1130#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001131 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001133 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001134 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001135 VisitStatementsAndSpill(node->statements());
1136 if (node->break_target()->is_linked()) {
1137 node->break_target()->Bind();
1138 }
1139 node->break_target()->Unuse();
1140 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141}
1142
1143
ager@chromium.org7c537e22008-10-16 08:43:32 +00001144void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001145 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001146 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001147 frame_->EmitPush(r0);
1148 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001149 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001150 frame_->EmitPush(r0);
1151 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001152 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153}
1154
1155
ager@chromium.org7c537e22008-10-16 08:43:32 +00001156void CodeGenerator::VisitDeclaration(Declaration* 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_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 Variable* var = node->proxy()->var();
1164 ASSERT(var != NULL); // must have been resolved
1165 Slot* slot = var->slot();
1166
1167 // If it was not possible to allocate the variable at compile time,
1168 // we need to "declare" it at runtime to make sure it actually
1169 // exists in the local context.
1170 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1171 // Variables with a "LOOKUP" slot were introduced as non-locals
1172 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001173 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001175 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001176 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001177 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 // Declaration nodes are always declared in only two modes.
1179 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1180 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001181 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001182 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 // Push initial value, if any.
1184 // Note: For variables we must not push an initial value (such as
1185 // 'undefined') because we may have a (legal) redeclaration and we
1186 // must not destroy the current value.
1187 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001188 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001189 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001191 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001193 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001194 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001196 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001197 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001198 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 return;
1200 }
1201
1202 ASSERT(!var->is_global());
1203
1204 // If we have a function or a constant, we need to initialize the variable.
1205 Expression* val = NULL;
1206 if (node->mode() == Variable::CONST) {
1207 val = new Literal(Factory::the_hole_value());
1208 } else {
1209 val = node->fun(); // NULL if we don't have a function
1210 }
1211
1212 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001213 {
1214 // Set initial value.
1215 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001216 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001217 target.SetValue(NOT_CONST_INIT);
1218 // The reference is removed from the stack (preserving TOS) when
1219 // it goes out of scope.
1220 }
1221 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225}
1226
1227
ager@chromium.org7c537e22008-10-16 08:43:32 +00001228void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001229#ifdef DEBUG
1230 int original_height = frame_->height();
1231#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001232 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001234 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 Expression* expression = node->expression();
1236 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001237 LoadAndSpill(expression);
1238 frame_->Drop();
1239 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240}
1241
1242
ager@chromium.org7c537e22008-10-16 08:43:32 +00001243void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001244#ifdef DEBUG
1245 int original_height = frame_->height();
1246#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001247 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001249 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001251 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252}
1253
1254
ager@chromium.org7c537e22008-10-16 08:43:32 +00001255void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001256#ifdef DEBUG
1257 int original_height = frame_->height();
1258#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001259 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001261 // Generate different code depending on which parts of the if statement
1262 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 bool has_then_stm = node->HasThenStatement();
1264 bool has_else_stm = node->HasElseStatement();
1265
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001266 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001268 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001270 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001271 JumpTarget then;
1272 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1275 &then, &else_, true);
1276 if (frame_ != NULL) {
1277 Branch(false, &else_);
1278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001280 if (frame_ != NULL || then.is_linked()) {
1281 then.Bind();
1282 VisitAndSpill(node->then_statement());
1283 }
1284 if (frame_ != NULL) {
1285 exit.Jump();
1286 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001288 if (else_.is_linked()) {
1289 else_.Bind();
1290 VisitAndSpill(node->else_statement());
1291 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
1293 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001294 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001296 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001298 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1299 &then, &exit, true);
1300 if (frame_ != NULL) {
1301 Branch(false, &exit);
1302 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001304 if (frame_ != NULL || then.is_linked()) {
1305 then.Bind();
1306 VisitAndSpill(node->then_statement());
1307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
1309 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001310 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001312 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001314 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1315 &exit, &else_, true);
1316 if (frame_ != NULL) {
1317 Branch(true, &exit);
1318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001320 if (frame_ != NULL || else_.is_linked()) {
1321 else_.Bind();
1322 VisitAndSpill(node->else_statement());
1323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324
1325 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001326 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 ASSERT(!has_then_stm && !has_else_stm);
1328 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001329 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1330 &exit, &exit, false);
1331 if (frame_ != NULL) {
1332 if (has_cc()) {
1333 cc_reg_ = al;
1334 } else {
1335 frame_->Drop();
1336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 }
1338 }
1339
1340 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001341 if (exit.is_linked()) {
1342 exit.Bind();
1343 }
1344 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345}
1346
1347
ager@chromium.org7c537e22008-10-16 08:43:32 +00001348void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001349 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001351 CodeForStatementPosition(node);
1352 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353}
1354
1355
ager@chromium.org7c537e22008-10-16 08:43:32 +00001356void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001357 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 CodeForStatementPosition(node);
1360 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361}
1362
1363
ager@chromium.org7c537e22008-10-16 08:43:32 +00001364void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001365 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001367
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001368 if (function_return_is_shadowed_) {
1369 CodeForStatementPosition(node);
1370 LoadAndSpill(node->expression());
1371 frame_->EmitPop(r0);
1372 function_return_.Jump();
1373 } else {
1374 // Load the returned value.
1375 CodeForStatementPosition(node);
1376 LoadAndSpill(node->expression());
1377
1378 // Pop the result from the frame and prepare the frame for
1379 // returning thus making it easier to merge.
1380 frame_->EmitPop(r0);
1381 frame_->PrepareForReturn();
1382
1383 function_return_.Jump();
1384 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385}
1386
1387
ager@chromium.org7c537e22008-10-16 08:43:32 +00001388void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001389#ifdef DEBUG
1390 int original_height = frame_->height();
1391#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001392 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001394 CodeForStatementPosition(node);
1395 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001396 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001397 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001398 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001399 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001400 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001401#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001402 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001403 __ cmp(r0, Operand(cp));
1404 verified_true.Branch(eq);
1405 __ stop("PushContext: r0 is expected to be the same as cp");
1406 verified_true.Bind();
1407#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001409 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001410 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411}
1412
1413
ager@chromium.org7c537e22008-10-16 08:43:32 +00001414void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001415#ifdef DEBUG
1416 int original_height = frame_->height();
1417#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001418 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001420 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 // Pop context.
1422 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1423 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001424 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001425 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426}
1427
1428
ager@chromium.org7c537e22008-10-16 08:43:32 +00001429void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001430#ifdef DEBUG
1431 int original_height = frame_->height();
1432#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001433 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001436 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001438 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001439
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001440 JumpTarget next_test;
1441 JumpTarget fall_through;
1442 JumpTarget default_entry;
1443 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 ZoneList<CaseClause*>* cases = node->cases();
1445 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001446 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447
1448 for (int i = 0; i < length; i++) {
1449 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001451 // Remember the default clause and compile it at the end.
1452 default_clause = clause;
1453 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 }
1455
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001456 Comment cmnt(masm_, "[ Case clause");
1457 // Compile the test.
1458 next_test.Bind();
1459 next_test.Unuse();
1460 // Duplicate TOS.
1461 __ ldr(r0, frame_->Top());
1462 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001463 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001464 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001465
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001466 // Before entering the body from the test, remove the switch value from
1467 // the stack.
1468 frame_->Drop();
1469
1470 // Label the body so that fall through is enabled.
1471 if (i > 0 && cases->at(i - 1)->is_default()) {
1472 default_exit.Bind();
1473 } else {
1474 fall_through.Bind();
1475 fall_through.Unuse();
1476 }
1477 VisitStatementsAndSpill(clause->statements());
1478
1479 // If control flow can fall through from the body, jump to the next body
1480 // or the end of the statement.
1481 if (frame_ != NULL) {
1482 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1483 default_entry.Jump();
1484 } else {
1485 fall_through.Jump();
1486 }
1487 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 }
1489
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001490 // The final "test" removes the switch value.
1491 next_test.Bind();
1492 frame_->Drop();
1493
1494 // If there is a default clause, compile it.
1495 if (default_clause != NULL) {
1496 Comment cmnt(masm_, "[ Default clause");
1497 default_entry.Bind();
1498 VisitStatementsAndSpill(default_clause->statements());
1499 // If control flow can fall out of the default and there is a case after
1500 // it, jup to that case's body.
1501 if (frame_ != NULL && default_exit.is_bound()) {
1502 default_exit.Jump();
1503 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001504 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001506 if (fall_through.is_linked()) {
1507 fall_through.Bind();
1508 }
1509
1510 if (node->break_target()->is_linked()) {
1511 node->break_target()->Bind();
1512 }
1513 node->break_target()->Unuse();
1514 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515}
1516
1517
ager@chromium.org7c537e22008-10-16 08:43:32 +00001518void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001519#ifdef DEBUG
1520 int original_height = frame_->height();
1521#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001522 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001524 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001525 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001527 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1528 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1530 if (node->cond() == NULL) {
1531 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1532 info = ALWAYS_TRUE;
1533 } else {
1534 Literal* lit = node->cond()->AsLiteral();
1535 if (lit != NULL) {
1536 if (lit->IsTrue()) {
1537 info = ALWAYS_TRUE;
1538 } else if (lit->IsFalse()) {
1539 info = ALWAYS_FALSE;
1540 }
1541 }
1542 }
1543
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544 switch (node->type()) {
1545 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001546 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001548 // Label the top of the loop for the backward CFG edge. If the test
1549 // is always true we can use the continue target, and if the test is
1550 // always false there is no need.
1551 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001552 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001553 node->continue_target()->Bind();
1554 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001555 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 } else {
1557 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001558 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001559 body.Bind();
1560 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001563 VisitAndSpill(node->body());
1564
1565 // Compile the test.
1566 if (info == ALWAYS_TRUE) {
1567 if (has_valid_frame()) {
1568 // If control can fall off the end of the body, jump back to the
1569 // top.
1570 node->continue_target()->Jump();
1571 }
1572 } else if (info == ALWAYS_FALSE) {
1573 // If we have a continue in the body, we only have to bind its jump
1574 // target.
1575 if (node->continue_target()->is_linked()) {
1576 node->continue_target()->Bind();
1577 }
1578 } else {
1579 ASSERT(info == DONT_KNOW);
1580 // We have to compile the test expression if it can be reached by
1581 // control flow falling out of the body or via continue.
1582 if (node->continue_target()->is_linked()) {
1583 node->continue_target()->Bind();
1584 }
1585 if (has_valid_frame()) {
1586 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1587 &body, node->break_target(), true);
1588 if (has_valid_frame()) {
1589 // A invalid frame here indicates that control did not
1590 // fall out of the test expression.
1591 Branch(true, &body);
1592 }
1593 }
1594 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001596 }
1597
1598 case LoopStatement::WHILE_LOOP: {
1599 // If the test is never true and has no side effects there is no need
1600 // to compile the test or body.
1601 if (info == ALWAYS_FALSE) break;
1602
1603 // Label the top of the loop with the continue target for the backward
1604 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001605 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001606 node->continue_target()->Bind();
1607
1608 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001609 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1611 &body, node->break_target(), true);
1612 if (has_valid_frame()) {
1613 // A NULL frame indicates that control did not fall out of the
1614 // test expression.
1615 Branch(false, node->break_target());
1616 }
1617 if (has_valid_frame() || body.is_linked()) {
1618 body.Bind();
1619 }
1620 }
1621
1622 if (has_valid_frame()) {
1623 CheckStack(); // TODO(1222600): ignore if body contains calls.
1624 VisitAndSpill(node->body());
1625
1626 // If control flow can fall out of the body, jump back to the top.
1627 if (has_valid_frame()) {
1628 node->continue_target()->Jump();
1629 }
1630 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001632 }
1633
1634 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001635 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001636
1637 if (node->init() != NULL) {
1638 VisitAndSpill(node->init());
1639 }
1640
1641 // There is no need to compile the test or body.
1642 if (info == ALWAYS_FALSE) break;
1643
1644 // If there is no update statement, label the top of the loop with the
1645 // continue target, otherwise with the loop target.
1646 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001647 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001648 node->continue_target()->Bind();
1649 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001650 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001651 loop.Bind();
1652 }
1653
1654 // If the test is always true, there is no need to compile it.
1655 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001656 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001657 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1658 &body, node->break_target(), true);
1659 if (has_valid_frame()) {
1660 Branch(false, node->break_target());
1661 }
1662 if (has_valid_frame() || body.is_linked()) {
1663 body.Bind();
1664 }
1665 }
1666
1667 if (has_valid_frame()) {
1668 CheckStack(); // TODO(1222600): ignore if body contains calls.
1669 VisitAndSpill(node->body());
1670
1671 if (node->next() == NULL) {
1672 // If there is no update statement and control flow can fall out
1673 // of the loop, jump directly to the continue label.
1674 if (has_valid_frame()) {
1675 node->continue_target()->Jump();
1676 }
1677 } else {
1678 // If there is an update statement and control flow can reach it
1679 // via falling out of the body of the loop or continuing, we
1680 // compile the update statement.
1681 if (node->continue_target()->is_linked()) {
1682 node->continue_target()->Bind();
1683 }
1684 if (has_valid_frame()) {
1685 // Record source position of the statement as this code which is
1686 // after the code for the body actually belongs to the loop
1687 // statement and not the body.
1688 CodeForStatementPosition(node);
1689 VisitAndSpill(node->next());
1690 loop.Jump();
1691 }
1692 }
1693 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001695 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 }
1697
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001698 if (node->break_target()->is_linked()) {
1699 node->break_target()->Bind();
1700 }
1701 node->continue_target()->Unuse();
1702 node->break_target()->Unuse();
1703 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704}
1705
1706
ager@chromium.org7c537e22008-10-16 08:43:32 +00001707void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001708#ifdef DEBUG
1709 int original_height = frame_->height();
1710#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001711 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001713 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001715 JumpTarget primitive;
1716 JumpTarget jsobject;
1717 JumpTarget fixed_array;
1718 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1719 JumpTarget end_del_check;
1720 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721
1722 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001723 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
1725 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1726 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001727 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001729 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001731 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732
1733 // Stack layout in body:
1734 // [iteration counter (Smi)]
1735 // [length of array]
1736 // [FixedArray]
1737 // [Map or 0]
1738 // [Object]
1739
1740 // Check if enumerable is already a JSObject
1741 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001742 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001743 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001744 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746 primitive.Bind();
1747 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001748 Result arg_count(r0);
1749 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001750 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001754 frame_->EmitPush(r0); // duplicate the object being enumerated
1755 frame_->EmitPush(r0);
1756 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757
1758 // If we got a Map, we can do a fast modification check.
1759 // Otherwise, we got a FixedArray, and we have to do a slow check.
1760 __ mov(r2, Operand(r0));
1761 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1762 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001763 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764
1765 // Get enum cache
1766 __ mov(r1, Operand(r0));
1767 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1768 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1769 __ ldr(r2,
1770 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1771
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001772 frame_->EmitPush(r0); // map
1773 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001774 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001776 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001777 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 frame_->EmitPush(r0);
1779 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 frame_->EmitPush(r1); // insert 0 in place of Map
1784 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785
1786 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001787 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001790 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
1793 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001795 // sp[0] : index
1796 // sp[1] : array/enum cache length
1797 // sp[2] : array or enum cache
1798 // sp[3] : 0 or map
1799 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001800 // Grab the current frame's height for the break and continue
1801 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001802 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1803 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001805 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1806 __ ldr(r1, frame_->ElementAt(1)); // load the length
1807 __ cmp(r0, Operand(r1)); // compare to the array length
1808 node->break_target()->Branch(hs);
1809
1810 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001813 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1815 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1816
1817 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001818 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 // Check if this (still) matches the map of the enumerable.
1820 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1823 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825
1826 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001827 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1828 frame_->EmitPush(r0);
1829 frame_->EmitPush(r3); // push entry
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001830 Result arg_count_reg(r0);
1831 __ mov(r0, Operand(1));
1832 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2);
1833 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834
1835 // If the property has been removed while iterating, we just skip it.
1836 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001837 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839 end_del_check.Bind();
1840 // Store the entry in the 'each' expression and take another spin in the
1841 // loop. r3: i'th entry of the enum cache (or string there of)
1842 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 { Reference each(this, node->each());
1844 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001845 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 __ ldr(r0, frame_->ElementAt(each.size()));
1847 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001848 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001849 // If the reference was to a slot we rely on the convenient property
1850 // that it doesn't matter whether a value (eg, r3 pushed above) is
1851 // right on top of or right underneath a zero-sized reference.
1852 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001853 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001854 // It's safe to pop the value lying on top of the reference before
1855 // unloading the reference itself (which preserves the top of stack,
1856 // ie, now the topmost value of the non-zero sized reference), since
1857 // we will discard the top of stack after unloading the reference
1858 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001860 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 }
1862 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001863 // Discard the i'th entry pushed above or else the remainder of the
1864 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 frame_->Drop();
1866
1867 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 VisitAndSpill(node->body());
1870
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001871 // Next. Reestablish a spilled frame in case we are coming here via
1872 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001873 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001874 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001875 frame_->EmitPop(r0);
1876 __ add(r0, r0, Operand(Smi::FromInt(1)));
1877 frame_->EmitPush(r0);
1878 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001880 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1881 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001883 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
1885 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886 exit.Bind();
1887 node->continue_target()->Unuse();
1888 node->break_target()->Unuse();
1889 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890}
1891
1892
ager@chromium.org7c537e22008-10-16 08:43:32 +00001893void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894#ifdef DEBUG
1895 int original_height = frame_->height();
1896#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001897 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001899 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001901 JumpTarget try_block;
1902 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907
1908 // Store the caught exception in the catch variable.
1909 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001910 ASSERT(ref.is_slot());
1911 // Here we make use of the convenient property that it doesn't matter
1912 // whether a value is immediately on top of or underneath a zero-sized
1913 // reference.
1914 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 }
1916
1917 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001918 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 VisitStatementsAndSpill(node->catch_block()->statements());
1921 if (frame_ != NULL) {
1922 exit.Jump();
1923 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924
1925
1926 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001927 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1930 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001932 // Shadow the labels for all escapes from the try block, including
1933 // returns. During shadowing, the original label is hidden as the
1934 // LabelShadow and operations on the original actually affect the
1935 // shadowing label.
1936 //
1937 // We should probably try to unify the escaping labels and the return
1938 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 int nof_escapes = node->escaping_targets()->length();
1940 List<ShadowTarget*> shadows(1 + nof_escapes);
1941
1942 // Add the shadow target for the function return.
1943 static const int kReturnShadowIndex = 0;
1944 shadows.Add(new ShadowTarget(&function_return_));
1945 bool function_return_was_shadowed = function_return_is_shadowed_;
1946 function_return_is_shadowed_ = true;
1947 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1948
1949 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001951 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 }
1953
1954 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956
1957 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001958 // After shadowing stops, the original labels are unshadowed and the
1959 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001960 bool has_unlinks = false;
1961 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001963 has_unlinks = has_unlinks || shadows[i]->is_linked();
1964 }
1965 function_return_is_shadowed_ = function_return_was_shadowed;
1966
1967 // Get an external reference to the handler address.
1968 ExternalReference handler_address(Top::k_handler_address);
1969
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001970 // If we can fall off the end of the try block, unlink from try chain.
1971 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001972 // The next handler address is on top of the frame. Unlink from
1973 // the handler list and drop the rest of this handler from the
1974 // frame.
1975 ASSERT(StackHandlerConstants::kNextOffset == 0);
1976 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 __ mov(r3, Operand(handler_address));
1978 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001979 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001980 if (has_unlinks) {
1981 exit.Jump();
1982 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 }
1984
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001985 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986 // jumped to. Deallocate each shadow target.
1987 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001989 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 shadows[i]->Bind();
1991 // Because we can be jumping here (to spilled code) from unspilled
1992 // code, we need to reestablish a spilled frame at this block.
1993 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 // Reload sp from the top handler, because some statements that we
1996 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001999 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002001 ASSERT(StackHandlerConstants::kNextOffset == 0);
2002 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002004 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002006 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2007 frame_->PrepareForReturn();
2008 }
2009 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 }
2011 }
2012
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 exit.Bind();
2014 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015}
2016
2017
ager@chromium.org7c537e22008-10-16 08:43:32 +00002018void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002019#ifdef DEBUG
2020 int original_height = frame_->height();
2021#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002022 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
2026 // State: Used to keep track of reason for entering the finally
2027 // block. Should probably be extended to hold information for
2028 // break/continue from within the try block.
2029 enum { FALLING, THROWING, JUMPING };
2030
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002031 JumpTarget try_block;
2032 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002036 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 // In case of thrown exceptions, this is where we continue.
2038 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
2041 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2045 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002047 // Shadow the labels for all escapes from the try block, including
2048 // returns. Shadowing hides the original label as the LabelShadow and
2049 // operations on the original actually affect the shadowing label.
2050 //
2051 // We should probably try to unify the escaping labels and the return
2052 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002053 int nof_escapes = node->escaping_targets()->length();
2054 List<ShadowTarget*> shadows(1 + nof_escapes);
2055
2056 // Add the shadow target for the function return.
2057 static const int kReturnShadowIndex = 0;
2058 shadows.Add(new ShadowTarget(&function_return_));
2059 bool function_return_was_shadowed = function_return_is_shadowed_;
2060 function_return_is_shadowed_ = true;
2061 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2062
2063 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 }
2067
2068 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002071 // Stop the introduced shadowing and count the number of required unlinks.
2072 // After shadowing stops, the original labels are unshadowed and the
2073 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 shadows[i]->StopShadowing();
2077 if (shadows[i]->is_linked()) nof_unlinks++;
2078 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081 // Get an external reference to the handler address.
2082 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002084 // If we can fall off the end of the try block, unlink from the try
2085 // chain and set the state on the frame to FALLING.
2086 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002087 // The next handler address is on top of the frame.
2088 ASSERT(StackHandlerConstants::kNextOffset == 0);
2089 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 __ mov(r3, Operand(handler_address));
2091 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002092 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002093
2094 // Fake a top of stack value (unneeded when FALLING) and set the
2095 // state in r2, then jump around the unlink blocks if any.
2096 __ mov(r0, Operand(Factory::undefined_value()));
2097 frame_->EmitPush(r0);
2098 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2099 if (nof_unlinks > 0) {
2100 finally_block.Jump();
2101 }
2102 }
2103
2104 // Generate code to unlink and set the state for the (formerly)
2105 // shadowing targets that have been jumped to.
2106 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 // If we have come from the shadowed return, the return value is
2109 // in (a non-refcounted reference to) r0. We must preserve it
2110 // until it is pushed.
2111 //
2112 // Because we can be jumping here (to spilled code) from
2113 // unspilled code, we need to reestablish a spilled frame at
2114 // this block.
2115 shadows[i]->Bind();
2116 frame_->SpillAll();
2117
2118 // Reload sp from the top handler, because some statements that
2119 // we break from (eg, for...in) may have left stuff on the
2120 // stack.
2121 __ mov(r3, Operand(handler_address));
2122 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002123 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002124
2125 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002126 // handler address is currently on top of the frame.
2127 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 frame_->EmitPop(r1);
2129 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002130 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131
2132 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002133 // If this label shadowed the function return, materialize the
2134 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002136 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002138 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 }
2141 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142 if (--nof_unlinks > 0) {
2143 // If this is not the last unlink block, jump around the next.
2144 finally_block.Jump();
2145 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 }
2147 }
2148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002152 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002153 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002154
2155 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 // and the state - while evaluating the finally block.
2157 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 if (has_valid_frame()) {
2162 // Restore state and return value or faked TOS.
2163 frame_->EmitPop(r2);
2164 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 }
2166
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 // Generate code to jump to the right destination for all used
2168 // formerly shadowing targets. Deallocate each shadow target.
2169 for (int i = 0; i < shadows.length(); i++) {
2170 if (has_valid_frame() && shadows[i]->is_bound()) {
2171 JumpTarget* original = shadows[i]->other_target();
2172 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2173 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002174 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175 skip.Branch(ne);
2176 frame_->PrepareForReturn();
2177 original->Jump();
2178 skip.Bind();
2179 } else {
2180 original->Branch(eq);
2181 }
2182 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 if (has_valid_frame()) {
2186 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002187 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2189 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002191 // Rethrow exception.
2192 frame_->EmitPush(r0);
2193 frame_->CallRuntime(Runtime::kReThrow, 1);
2194
2195 // Done.
2196 exit.Bind();
2197 }
2198 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199}
2200
2201
ager@chromium.org7c537e22008-10-16 08:43:32 +00002202void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002203#ifdef DEBUG
2204 int original_height = frame_->height();
2205#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002206 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002208 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002209#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002210 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002211#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002212 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214}
2215
2216
ager@chromium.org7c537e22008-10-16 08:43:32 +00002217void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002218 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 ASSERT(boilerplate->IsBoilerplate());
2220
2221 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002222 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224
2225 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 frame_->EmitPush(cp);
2227 frame_->CallRuntime(Runtime::kNewClosure, 2);
2228 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229}
2230
2231
ager@chromium.org7c537e22008-10-16 08:43:32 +00002232void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002233#ifdef DEBUG
2234 int original_height = frame_->height();
2235#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002236 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 Comment cmnt(masm_, "[ FunctionLiteral");
2238
2239 // Build the function boilerplate and instantiate it.
2240 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002241 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 if (HasStackOverflow()) {
2243 ASSERT(frame_->height() == original_height);
2244 return;
2245 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002247 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248}
2249
2250
ager@chromium.org7c537e22008-10-16 08:43:32 +00002251void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002253#ifdef DEBUG
2254 int original_height = frame_->height();
2255#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002256 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2258 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260}
2261
2262
ager@chromium.org7c537e22008-10-16 08:43:32 +00002263void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002264#ifdef DEBUG
2265 int original_height = frame_->height();
2266#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002267 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002269 JumpTarget then;
2270 JumpTarget else_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2272 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002273 if (has_valid_frame()) {
2274 Branch(false, &else_);
2275 }
2276 if (has_valid_frame() || then.is_linked()) {
2277 then.Bind();
2278 LoadAndSpill(node->then_expression(), typeof_state());
2279 }
2280 if (else_.is_linked()) {
2281 JumpTarget exit;
2282 if (has_valid_frame()) exit.Jump();
2283 else_.Bind();
2284 LoadAndSpill(node->else_expression(), typeof_state());
2285 if (exit.is_linked()) exit.Bind();
2286 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288}
2289
2290
ager@chromium.org7c537e22008-10-16 08:43:32 +00002291void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002292 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002293 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002294 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002296 JumpTarget slow;
2297 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002298
2299 // Generate fast-case code for variables that might be shadowed by
2300 // eval-introduced variables. Eval is used a lot without
2301 // introducing variables. In those cases, we do not want to
2302 // perform a runtime call for all variables in the scope
2303 // containing the eval.
2304 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2305 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002306 // If there was no control flow to slow, we can exit early.
2307 if (!slow.is_linked()) {
2308 frame_->EmitPush(r0);
2309 return;
2310 }
2311
2312 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002313
2314 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2315 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2316 // Only generate the fast case for locals that rewrite to slots.
2317 // This rules out argument loads.
2318 if (potential_slot != NULL) {
2319 __ ldr(r0,
2320 ContextSlotOperandCheckExtensions(potential_slot,
2321 r1,
2322 r2,
2323 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002324 if (potential_slot->var()->mode() == Variable::CONST) {
2325 __ cmp(r0, Operand(Factory::the_hole_value()));
2326 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2327 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002329 // ContextSlotOperandCheckExtensions so we have to jump around
2330 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002331 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002332 }
2333 }
2334
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 slow.Bind();
2336 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002337 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002338 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339
ager@chromium.org7c537e22008-10-16 08:43:32 +00002340 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002342 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002345
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 done.Bind();
2347 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
2349 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002350 // Note: We would like to keep the assert below, but it fires because of
2351 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002352 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002354 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002355 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002356 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002357 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002358 // Const slots may contain 'the hole' value (the constant hasn't been
2359 // initialized yet) which needs to be converted into the 'undefined'
2360 // value.
2361 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002363 __ cmp(r0, Operand(Factory::the_hole_value()));
2364 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 }
2367 }
2368}
2369
2370
ager@chromium.org381abbb2009-02-25 13:23:22 +00002371void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2372 TypeofState typeof_state,
2373 Register tmp,
2374 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002376 // Check that no extension objects have been created by calls to
2377 // eval from the current scope to the global scope.
2378 Register context = cp;
2379 Scope* s = scope();
2380 while (s != NULL) {
2381 if (s->num_heap_slots() > 0) {
2382 if (s->calls_eval()) {
2383 // Check that extension is NULL.
2384 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2385 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002387 }
2388 // Load next context in chain.
2389 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2390 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2391 context = tmp;
2392 }
2393 // If no outer scope calls eval, we do not need to check more
2394 // context extensions.
2395 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2396 s = s->outer_scope();
2397 }
2398
2399 if (s->is_eval_scope()) {
2400 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002401 if (!context.is(tmp)) {
2402 __ mov(tmp, Operand(context));
2403 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002404 __ bind(&next);
2405 // Terminate at global context.
2406 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2407 __ cmp(tmp2, Operand(Factory::global_context_map()));
2408 __ b(eq, &fast);
2409 // Check that extension is NULL.
2410 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2411 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002412 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002413 // Load next context in chain.
2414 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2415 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2416 __ b(&next);
2417 __ bind(&fast);
2418 }
2419
2420 // All extension objects were empty and it is safe to use a global
2421 // load IC call.
2422 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2423 // Load the global object.
2424 LoadGlobal();
2425 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002426 Result name(r2);
2427 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002428 // Call IC stub.
2429 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002430 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002431 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002433 }
2434
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 // Drop the global object. The result is in r0.
2436 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002437}
2438
2439
ager@chromium.org7c537e22008-10-16 08:43:32 +00002440void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002441#ifdef DEBUG
2442 int original_height = frame_->height();
2443#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002444 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002445 Comment cmnt(masm_, "[ Slot");
2446 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002447 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002448}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449
ager@chromium.org7c537e22008-10-16 08:43:32 +00002450
2451void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452#ifdef DEBUG
2453 int original_height = frame_->height();
2454#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002455 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002456 Comment cmnt(masm_, "[ VariableProxy");
2457
2458 Variable* var = node->var();
2459 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002460 if (expr != NULL) {
2461 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002463 ASSERT(var->is_global());
2464 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468}
2469
2470
ager@chromium.org7c537e22008-10-16 08:43:32 +00002471void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002472#ifdef DEBUG
2473 int original_height = frame_->height();
2474#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002475 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002477 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002478 frame_->EmitPush(r0);
2479 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480}
2481
2482
ager@chromium.org7c537e22008-10-16 08:43:32 +00002483void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* 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;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 Comment cmnt(masm_, "[ RexExp Literal");
2489
2490 // Retrieve the literal array and check the allocated entry.
2491
2492 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002493 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494
2495 // Load the literals array of the function.
2496 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2497
2498 // Load the literal at the ast saved index.
2499 int literal_offset =
2500 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2501 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2502
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002503 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002505 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
2507 // If the entry is undefined we call the runtime system to computed
2508 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002510 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002511 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002512 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002513 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002514 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 frame_->EmitPush(r0);
2516 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002517 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002519 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002521 frame_->EmitPush(r2);
2522 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523}
2524
2525
2526// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002527// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528// Each created boilerplate is stored in the JSFunction and they are
2529// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002530class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002532 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002535
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 private:
2539 ObjectLiteral* node_;
2540};
2541
2542
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002543void DeferredObjectLiteral::Generate() {
2544 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002545
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002546 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002549 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002551 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002552 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002554 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002555 __ push(r0);
2556 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2557 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002558 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559}
2560
2561
ager@chromium.org7c537e22008-10-16 08:43:32 +00002562void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563#ifdef DEBUG
2564 int original_height = frame_->height();
2565#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002566 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 Comment cmnt(masm_, "[ ObjectLiteral");
2568
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002569 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570
2571 // Retrieve the literal array and check the allocated entry.
2572
2573 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002574 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
2576 // Load the literals array of the function.
2577 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2578
2579 // Load the literal at the ast saved index.
2580 int literal_offset =
2581 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2582 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2583
2584 // Check whether we need to materialize the object literal boilerplate.
2585 // If so, jump to the deferred code.
2586 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002587 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
2590 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002594 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2595 if (node->depth() == 1) {
2596 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2597 }
2598 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002600 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601
2602 for (int i = 0; i < node->properties()->length(); i++) {
2603 ObjectLiteral::Property* property = node->properties()->at(i);
2604 Literal* key = property->key();
2605 Expression* value = property->value();
2606 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002607 case ObjectLiteral::Property::CONSTANT:
2608 break;
2609 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2610 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2611 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 case ObjectLiteral::Property::COMPUTED: // fall through
2613 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002614 frame_->EmitPush(r0); // dup the result
2615 LoadAndSpill(key);
2616 LoadAndSpill(value);
2617 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002618 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002619 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 break;
2621 }
2622 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002623 frame_->EmitPush(r0);
2624 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002625 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626 frame_->EmitPush(r0);
2627 LoadAndSpill(value);
2628 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002629 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 break;
2631 }
2632 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002633 frame_->EmitPush(r0);
2634 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002635 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002636 frame_->EmitPush(r0);
2637 LoadAndSpill(value);
2638 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002639 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 break;
2641 }
2642 }
2643 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 ASSERT(frame_->height() == original_height + 1);
2645}
2646
2647
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648// This deferred code stub will be used for creating the boilerplate
2649// by calling Runtime_CreateArrayLiteralBoilerplate.
2650// Each created boilerplate is stored in the JSFunction and they are
2651// therefore context dependent.
2652class DeferredArrayLiteral: public DeferredCode {
2653 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002654 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002655 set_comment("[ DeferredArrayLiteral");
2656 }
2657
2658 virtual void Generate();
2659
2660 private:
2661 ArrayLiteral* node_;
2662};
2663
2664
2665void DeferredArrayLiteral::Generate() {
2666 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002667
2668 // If the entry is undefined we call the runtime system to computed
2669 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002670 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002671 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002672 // Literal index (1).
2673 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002674 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002675 // Constant properties (2).
2676 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002677 __ push(r0);
2678 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2679 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002680 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002681}
2682
2683
ager@chromium.org7c537e22008-10-16 08:43:32 +00002684void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685#ifdef DEBUG
2686 int original_height = frame_->height();
2687#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002688 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002690
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002691 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002692
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002693 // Retrieve the literal array and check the allocated entry.
2694
2695 // Load the function of this activation.
2696 __ ldr(r1, frame_->Function());
2697
2698 // Load the literals array of the function.
2699 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2700
2701 // Load the literal at the ast saved index.
2702 int literal_offset =
2703 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2704 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2705
2706 // Check whether we need to materialize the object literal boilerplate.
2707 // If so, jump to the deferred code.
2708 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002709 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002710 deferred->BindExit();
2711
2712 // Push the object literal boilerplate.
2713 frame_->EmitPush(r2);
2714
2715 // Clone the boilerplate object.
2716 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2717 if (node->depth() == 1) {
2718 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2719 }
2720 frame_->CallRuntime(clone_function_id, 1);
2721 frame_->EmitPush(r0); // save the result
2722 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002723
2724 // Generate code to set the elements in the array that are not
2725 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 for (int i = 0; i < node->values()->length(); i++) {
2727 Expression* value = node->values()->at(i);
2728
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002729 // If value is a literal the property value is already set in the
2730 // boilerplate object.
2731 if (value->AsLiteral() != NULL) continue;
2732 // If value is a materialized literal the property value is already set
2733 // in the boilerplate object if it is simple.
2734 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002736 // The property must be set by generated code.
2737 LoadAndSpill(value);
2738 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002739
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002740 // Fetch the object literal.
2741 __ ldr(r1, frame_->Top());
2742 // Get the elements array.
2743 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002745 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002746 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002747 __ str(r0, FieldMemOperand(r1, offset));
2748
2749 // Update the write barrier for the array address.
2750 __ mov(r3, Operand(offset));
2751 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002753 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002754}
2755
2756
ager@chromium.org32912102009-01-16 10:38:43 +00002757void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002758#ifdef DEBUG
2759 int original_height = frame_->height();
2760#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002761 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002762 // Call runtime routine to allocate the catch extension object and
2763 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764 Comment cmnt(masm_, "[ CatchExtensionObject");
2765 LoadAndSpill(node->key());
2766 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002767 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2768 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002770}
2771
2772
ager@chromium.org7c537e22008-10-16 08:43:32 +00002773void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002774#ifdef DEBUG
2775 int original_height = frame_->height();
2776#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002777 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002779 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002780
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002781 { Reference target(this, node->target());
2782 if (target.is_illegal()) {
2783 // Fool the virtual frame into thinking that we left the assignment's
2784 // value on the frame.
2785 __ mov(r0, Operand(Smi::FromInt(0)));
2786 frame_->EmitPush(r0);
2787 ASSERT(frame_->height() == original_height + 1);
2788 return;
2789 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002791 if (node->op() == Token::ASSIGN ||
2792 node->op() == Token::INIT_VAR ||
2793 node->op() == Token::INIT_CONST) {
2794 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002797 // +=, *= and similar binary assignments.
2798 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002799 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2800 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002801 bool overwrite =
2802 (node->value()->AsBinaryOperation() != NULL &&
2803 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002804 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002805 SmiOperation(node->binary_op(),
2806 literal->handle(),
2807 false,
2808 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002809 frame_->EmitPush(r0);
2810
2811 } else {
2812 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002813 GenericBinaryOperation(node->binary_op(),
2814 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002815 frame_->EmitPush(r0);
2816 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002817 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002819 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2820 if (var != NULL &&
2821 (var->mode() == Variable::CONST) &&
2822 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2823 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002826 CodeForSourcePosition(node->position());
2827 if (node->op() == Token::INIT_CONST) {
2828 // Dynamic constant initializations must use the function context
2829 // and initialize the actual constant declared. Dynamic variable
2830 // initializations are simply assignments and use SetValue.
2831 target.SetValue(CONST_INIT);
2832 } else {
2833 target.SetValue(NOT_CONST_INIT);
2834 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 }
2836 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002837 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838}
2839
2840
ager@chromium.org7c537e22008-10-16 08:43:32 +00002841void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002842#ifdef DEBUG
2843 int original_height = frame_->height();
2844#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002845 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002846 Comment cmnt(masm_, "[ Throw");
2847
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002848 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002849 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002850 frame_->CallRuntime(Runtime::kThrow, 1);
2851 frame_->EmitPush(r0);
2852 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853}
2854
2855
ager@chromium.org7c537e22008-10-16 08:43:32 +00002856void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857#ifdef DEBUG
2858 int original_height = frame_->height();
2859#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002860 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002863 { Reference property(this, node);
2864 property.GetValueAndSpill(typeof_state());
2865 }
2866 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867}
2868
2869
ager@chromium.org7c537e22008-10-16 08:43:32 +00002870void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002871#ifdef DEBUG
2872 int original_height = frame_->height();
2873#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002874 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 Comment cmnt(masm_, "[ Call");
2876
2877 ZoneList<Expression*>* args = node->arguments();
2878
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002879 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 // Standard function call.
2881
2882 // Check if the function is a variable or a property.
2883 Expression* function = node->expression();
2884 Variable* var = function->AsVariableProxy()->AsVariable();
2885 Property* property = function->AsProperty();
2886
2887 // ------------------------------------------------------------------------
2888 // Fast-case: Use inline caching.
2889 // ---
2890 // According to ECMA-262, section 11.2.3, page 44, the function to call
2891 // must be resolved after the arguments have been evaluated. The IC code
2892 // automatically handles this by loading the arguments before the function
2893 // is resolved in cache misses (this also holds for megamorphic calls).
2894 // ------------------------------------------------------------------------
2895
2896 if (var != NULL && !var->is_this() && var->is_global()) {
2897 // ----------------------------------
2898 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2899 // ----------------------------------
2900
2901 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002902 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002903 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002904
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002905 // Pass the global object as the receiver and let the IC stub
2906 // patch the stack to use the global proxy as 'this' in the
2907 // invoked function.
2908 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909
2910 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002911 int arg_count = args->length();
2912 for (int i = 0; i < arg_count; i++) {
2913 LoadAndSpill(args->at(i));
2914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915
2916 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002917 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2918 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002919 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002920 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
2921 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002922 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924 frame_->Drop();
2925 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926
2927 } else if (var != NULL && var->slot() != NULL &&
2928 var->slot()->type() == Slot::LOOKUP) {
2929 // ----------------------------------
2930 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2931 // ----------------------------------
2932
2933 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002934 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002935 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002936 frame_->EmitPush(r0);
2937 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 // r0: slot value; r1: receiver
2939
2940 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 frame_->EmitPush(r0); // function
2942 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943
2944 // Call the function.
2945 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947
2948 } else if (property != NULL) {
2949 // Check if the key is a literal string.
2950 Literal* literal = property->key()->AsLiteral();
2951
2952 if (literal != NULL && literal->handle()->IsSymbol()) {
2953 // ------------------------------------------------------------------
2954 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2955 // ------------------------------------------------------------------
2956
2957 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002958 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 frame_->EmitPush(r0);
2960 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961
2962 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002963 int arg_count = args->length();
2964 for (int i = 0; i < arg_count; i++) {
2965 LoadAndSpill(args->at(i));
2966 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967
2968 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002969 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2970 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002971 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002972 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002973 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002974
2975 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002976 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002977
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002978 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979
2980 } else {
2981 // -------------------------------------------
2982 // JavaScript example: 'array[index](1, 2, 3)'
2983 // -------------------------------------------
2984
2985 // Load the function to call from the property through a reference.
2986 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002987 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988
2989 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002990 if (property->is_synthetic()) {
2991 LoadGlobalReceiver(r0);
2992 } else {
2993 __ ldr(r0, frame_->ElementAt(ref.size()));
2994 frame_->EmitPush(r0);
2995 }
2996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997 // Call the function.
2998 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002999 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000 }
3001
3002 } else {
3003 // ----------------------------------
3004 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3005 // ----------------------------------
3006
3007 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003008 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003009
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003010 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003011 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003012
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013 // Call the function.
3014 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003015 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003017 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018}
3019
3020
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003021void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003022#ifdef DEBUG
3023 int original_height = frame_->height();
3024#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003025 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003026 Comment cmnt(masm_, "[ CallEval");
3027
3028 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3029 // the function we need to call and the receiver of the call.
3030 // Then we call the resolved function using the given arguments.
3031
3032 ZoneList<Expression*>* args = node->arguments();
3033 Expression* function = node->expression();
3034
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003036
3037 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003038 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003039 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003040 frame_->EmitPush(r2); // Slot for receiver
3041 int arg_count = args->length();
3042 for (int i = 0; i < arg_count; i++) {
3043 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003044 }
3045
3046 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003047 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3048 frame_->EmitPush(r1);
3049 if (arg_count > 0) {
3050 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3051 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003052 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003053 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003054 }
3055
3056 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003058
3059 // Touch up stack with the right values for the function and the receiver.
3060 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003062 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003063 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003064
3065 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003066 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003067
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003068 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3069 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003070 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003071
3072 __ ldr(cp, frame_->Context());
3073 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003074 frame_->Drop();
3075 frame_->EmitPush(r0);
3076 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003077}
3078
3079
ager@chromium.org7c537e22008-10-16 08:43:32 +00003080void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003081#ifdef DEBUG
3082 int original_height = frame_->height();
3083#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003084 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003086 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087
3088 // According to ECMA-262, section 11.2.2, page 44, the function
3089 // expression in new calls must be evaluated before the
3090 // arguments. This is different from ordinary calls, where the
3091 // actual function to call is resolved after the arguments have been
3092 // evaluated.
3093
3094 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003095 // receiver. There is no need to use the global proxy here because
3096 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003098 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099
3100 // Push the arguments ("left-to-right") on the stack.
3101 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003102 int arg_count = args->length();
3103 for (int i = 0; i < arg_count; i++) {
3104 LoadAndSpill(args->at(i));
3105 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106
mads.s.ager31e71382008-08-13 09:32:07 +00003107 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003108 Result num_args(r0);
3109 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003111 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003112 Result function(r1);
3113 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 // Call the construct call builtin that handles allocation and
3116 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003117 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003118 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003119 frame_->CallCodeObject(ic,
3120 RelocInfo::CONSTRUCT_CALL,
3121 &num_args,
3122 &function,
3123 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003124
3125 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003126 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003127 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128}
3129
3130
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003131void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3132 VirtualFrame::SpilledScope spilled_scope;
3133 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003134 JumpTarget leave, null, function, non_function_constructor;
3135
3136 // Load the object into r0.
3137 LoadAndSpill(args->at(0));
3138 frame_->EmitPop(r0);
3139
3140 // If the object is a smi, we return null.
3141 __ tst(r0, Operand(kSmiTagMask));
3142 null.Branch(eq);
3143
3144 // Check that the object is a JS object but take special care of JS
3145 // functions to make sure they have 'Function' as their class.
3146 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3147 null.Branch(lt);
3148
3149 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3150 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3151 // LAST_JS_OBJECT_TYPE.
3152 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3153 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3154 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3155 function.Branch(eq);
3156
3157 // Check if the constructor in the map is a function.
3158 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3159 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3160 non_function_constructor.Branch(ne);
3161
3162 // The r0 register now contains the constructor function. Grab the
3163 // instance class name from there.
3164 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3165 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003166 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003167 leave.Jump();
3168
3169 // Functions have class 'Function'.
3170 function.Bind();
3171 __ mov(r0, Operand(Factory::function_class_symbol()));
3172 frame_->EmitPush(r0);
3173 leave.Jump();
3174
3175 // Objects with a non-function constructor have class 'Object'.
3176 non_function_constructor.Bind();
3177 __ mov(r0, Operand(Factory::Object_symbol()));
3178 frame_->EmitPush(r0);
3179 leave.Jump();
3180
3181 // Non-JS objects have class null.
3182 null.Bind();
3183 __ mov(r0, Operand(Factory::null_value()));
3184 frame_->EmitPush(r0);
3185
3186 // All done.
3187 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003188}
3189
3190
ager@chromium.org7c537e22008-10-16 08:43:32 +00003191void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003192 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003194 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003195 LoadAndSpill(args->at(0));
3196 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003197 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003199 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003200 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3201 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003202 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203 // Load the value.
3204 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003205 leave.Bind();
3206 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207}
3208
3209
ager@chromium.org7c537e22008-10-16 08:43:32 +00003210void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003211 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003212 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003213 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003214 LoadAndSpill(args->at(0)); // Load the object.
3215 LoadAndSpill(args->at(1)); // Load the value.
3216 frame_->EmitPop(r0); // r0 contains value
3217 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 // if (object->IsSmi()) return object.
3219 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003220 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003221 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3222 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003224 // Store the value.
3225 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3226 // Update the write barrier.
3227 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3228 __ RecordWrite(r1, r2, r3);
3229 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003230 leave.Bind();
3231 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232}
3233
3234
ager@chromium.org7c537e22008-10-16 08:43:32 +00003235void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003236 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003238 LoadAndSpill(args->at(0));
3239 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003240 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 cc_reg_ = eq;
3242}
3243
3244
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003245void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003246 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003247 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3248 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003249#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003250 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003251 LoadAndSpill(args->at(1));
3252 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003253 __ CallRuntime(Runtime::kLog, 2);
3254 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003255#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003256 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003257 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003258}
3259
3260
ager@chromium.org7c537e22008-10-16 08:43:32 +00003261void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003262 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003263 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003264 LoadAndSpill(args->at(0));
3265 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003266 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003267 cc_reg_ = eq;
3268}
3269
3270
kasper.lund7276f142008-07-30 08:49:36 +00003271// This should generate code that performs a charCodeAt() call or returns
3272// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3273// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003274void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003275 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003276 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003277 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003278 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003279}
3280
3281
ager@chromium.org7c537e22008-10-16 08:43:32 +00003282void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003283 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003284 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003285 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003286 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003287 // We need the CC bits to come out as not_equal in the case where the
3288 // object is a smi. This can't be done with the usual test opcode so
3289 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003290 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003291 __ and_(r1, r0, Operand(kSmiTagMask));
3292 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003293 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003294 // It is a heap object - get the map. Check if the object is a JS array.
3295 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003296 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 cc_reg_ = eq;
3298}
3299
3300
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003301void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3302 VirtualFrame::SpilledScope spilled_scope;
3303 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003304
3305 // Get the frame pointer for the calling frame.
3306 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3307
3308 // Skip the arguments adaptor frame if it exists.
3309 Label check_frame_marker;
3310 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
3311 __ cmp(r1, Operand(ArgumentsAdaptorFrame::SENTINEL));
3312 __ b(ne, &check_frame_marker);
3313 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3314
3315 // Check the marker in the calling frame.
3316 __ bind(&check_frame_marker);
3317 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3318 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3319 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003320}
3321
3322
ager@chromium.org7c537e22008-10-16 08:43:32 +00003323void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003324 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003325 ASSERT(args->length() == 0);
3326
mads.s.ager31e71382008-08-13 09:32:07 +00003327 // Seed the result with the formal parameters count, which will be used
3328 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3330
3331 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003332 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003333 frame_->CallStub(&stub, 0);
3334 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335}
3336
3337
ager@chromium.org7c537e22008-10-16 08:43:32 +00003338void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003339 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340 ASSERT(args->length() == 1);
3341
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003342 // Satisfy contract with ArgumentsAccessStub:
3343 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003344 LoadAndSpill(args->at(0));
3345 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003346 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347
3348 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003349 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003350 frame_->CallStub(&stub, 0);
3351 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352}
3353
3354
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003355void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3356 VirtualFrame::SpilledScope spilled_scope;
3357 ASSERT(args->length() == 0);
3358 __ Call(ExternalReference::random_positive_smi_function().address(),
3359 RelocInfo::RUNTIME_ENTRY);
3360 frame_->EmitPush(r0);
3361}
3362
3363
3364void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3365 VirtualFrame::SpilledScope spilled_scope;
3366 LoadAndSpill(args->at(0));
3367 switch (op) {
3368 case SIN:
3369 frame_->CallRuntime(Runtime::kMath_sin, 1);
3370 break;
3371 case COS:
3372 frame_->CallRuntime(Runtime::kMath_cos, 1);
3373 break;
3374 }
3375 frame_->EmitPush(r0);
3376}
3377
3378
ager@chromium.org7c537e22008-10-16 08:43:32 +00003379void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003380 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003381 ASSERT(args->length() == 2);
3382
3383 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003384 LoadAndSpill(args->at(0));
3385 LoadAndSpill(args->at(1));
3386 frame_->EmitPop(r0);
3387 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003388 __ cmp(r0, Operand(r1));
3389 cc_reg_ = eq;
3390}
3391
3392
ager@chromium.org7c537e22008-10-16 08:43:32 +00003393void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003394#ifdef DEBUG
3395 int original_height = frame_->height();
3396#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003397 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003398 if (CheckForInlineRuntimeCall(node)) {
3399 ASSERT((has_cc() && frame_->height() == original_height) ||
3400 (!has_cc() && frame_->height() == original_height + 1));
3401 return;
3402 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003403
3404 ZoneList<Expression*>* args = node->arguments();
3405 Comment cmnt(masm_, "[ CallRuntime");
3406 Runtime::Function* function = node->function();
3407
ager@chromium.org41826e72009-03-30 13:30:57 +00003408 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003409 // Prepare stack for calling JS runtime function.
3410 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003411 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003412 // Push the builtins object found in the current global object.
3413 __ ldr(r1, GlobalObject());
3414 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003415 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003416 }
mads.s.ager31e71382008-08-13 09:32:07 +00003417
ager@chromium.org41826e72009-03-30 13:30:57 +00003418 // Push the arguments ("left-to-right").
3419 int arg_count = args->length();
3420 for (int i = 0; i < arg_count; i++) {
3421 LoadAndSpill(args->at(i));
3422 }
mads.s.ager31e71382008-08-13 09:32:07 +00003423
ager@chromium.org41826e72009-03-30 13:30:57 +00003424 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003426 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3427 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003428 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003429 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430 frame_->Drop();
3431 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003432 } else {
3433 // Call the C runtime function.
3434 frame_->CallRuntime(function, arg_count);
3435 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003437 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003438}
3439
3440
ager@chromium.org7c537e22008-10-16 08:43:32 +00003441void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003442#ifdef DEBUG
3443 int original_height = frame_->height();
3444#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003445 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 Comment cmnt(masm_, "[ UnaryOperation");
3447
3448 Token::Value op = node->op();
3449
3450 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 LoadConditionAndSpill(node->expression(),
3452 NOT_INSIDE_TYPEOF,
3453 false_target(),
3454 true_target(),
3455 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003456 // LoadCondition may (and usually does) leave a test and branch to
3457 // be emitted by the caller. In that case, negate the condition.
3458 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459
3460 } else if (op == Token::DELETE) {
3461 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003462 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003464 LoadAndSpill(property->obj());
3465 LoadAndSpill(property->key());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003466 Result arg_count(r0);
3467 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003468 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003469
mads.s.ager31e71382008-08-13 09:32:07 +00003470 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471 Slot* slot = variable->slot();
3472 if (variable->is_global()) {
3473 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003474 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003475 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003476 Result arg_count(r0);
3477 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003479
3480 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3481 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003482 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003483 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003484 frame_->EmitPush(r0);
3485 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003487 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003488 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003489 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003490 Result arg_count(r0);
3491 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493
mads.s.ager31e71382008-08-13 09:32:07 +00003494 } else {
3495 // Default: Result of deleting non-global, not dynamically
3496 // introduced variables is false.
3497 __ mov(r0, Operand(Factory::false_value()));
3498 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499
3500 } else {
3501 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003502 LoadAndSpill(node->expression()); // may have side-effects
3503 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504 __ mov(r0, Operand(Factory::true_value()));
3505 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003506 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507
3508 } else if (op == Token::TYPEOF) {
3509 // Special case for loading the typeof expression; see comment on
3510 // LoadTypeofExpression().
3511 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003512 frame_->CallRuntime(Runtime::kTypeof, 1);
3513 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514
3515 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003516 LoadAndSpill(node->expression());
3517 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 switch (op) {
3519 case Token::NOT:
3520 case Token::DELETE:
3521 case Token::TYPEOF:
3522 UNREACHABLE(); // handled above
3523 break;
3524
3525 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003526 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003527 (node->expression()->AsBinaryOperation() != NULL &&
3528 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003529 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003530 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003531 break;
3532 }
3533
3534 case Token::BIT_NOT: {
3535 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003536 JumpTarget smi_label;
3537 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003539 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003541 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003542 Result arg_count(r0);
3543 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003544 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003546 continue_label.Jump();
3547 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548 __ mvn(r0, Operand(r0));
3549 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003550 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 break;
3552 }
3553
3554 case Token::VOID:
3555 // since the stack top is cached in r0, popping and then
3556 // pushing a value can be done by just writing to r0.
3557 __ mov(r0, Operand(Factory::undefined_value()));
3558 break;
3559
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003560 case Token::ADD: {
3561 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003562 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003563 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 continue_label.Branch(eq);
3565 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003566 Result arg_count(r0);
3567 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003568 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3569 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003571 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 default:
3573 UNREACHABLE();
3574 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003577 ASSERT(!has_valid_frame() ||
3578 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003579 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580}
3581
3582
ager@chromium.org7c537e22008-10-16 08:43:32 +00003583void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584#ifdef DEBUG
3585 int original_height = frame_->height();
3586#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003587 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 Comment cmnt(masm_, "[ CountOperation");
3589
3590 bool is_postfix = node->is_postfix();
3591 bool is_increment = node->op() == Token::INC;
3592
3593 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3594 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3595
3596 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003597 if (is_postfix) {
3598 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003599 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601
3602 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003603 if (target.is_illegal()) {
3604 // Spoof the virtual frame to have the expected height (one higher
3605 // than on entry).
3606 if (!is_postfix) {
3607 __ mov(r0, Operand(Smi::FromInt(0)));
3608 frame_->EmitPush(r0);
3609 }
3610 ASSERT(frame_->height() == original_height + 1);
3611 return;
3612 }
3613 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3614 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003616 JumpTarget slow;
3617 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618
3619 // Load the value (1) into register r1.
3620 __ mov(r1, Operand(Smi::FromInt(1)));
3621
3622 // Check for smi operand.
3623 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003624 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625
3626 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003627 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003628 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630
3631 // Perform optimistic increment/decrement.
3632 if (is_increment) {
3633 __ add(r0, r0, Operand(r1), SetCC);
3634 } else {
3635 __ sub(r0, r0, Operand(r1), SetCC);
3636 }
3637
3638 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003639 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640
3641 // Revert optimistic increment/decrement.
3642 if (is_increment) {
3643 __ sub(r0, r0, Operand(r1));
3644 } else {
3645 __ add(r0, r0, Operand(r1));
3646 }
3647
3648 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003649 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003650 {
3651 // Convert the operand to a number.
3652 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003653 Result arg_count(r0);
3654 __ mov(r0, Operand(0));
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003655 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3656 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003658 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003659 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 }
3661
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003662 // Compute the new value.
3663 __ mov(r1, Operand(Smi::FromInt(1)));
3664 frame_->EmitPush(r0);
3665 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003666 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003667 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003668 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003669 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670 }
3671
3672 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003673 exit.Bind();
3674 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003675 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003676 }
3677
3678 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003679 if (is_postfix) frame_->EmitPop(r0);
3680 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681}
3682
3683
ager@chromium.org7c537e22008-10-16 08:43:32 +00003684void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003685#ifdef DEBUG
3686 int original_height = frame_->height();
3687#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003688 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689 Comment cmnt(masm_, "[ BinaryOperation");
3690 Token::Value op = node->op();
3691
3692 // According to ECMA-262 section 11.11, page 58, the binary logical
3693 // operators must yield the result of one of the two expressions
3694 // before any ToBoolean() conversions. This means that the value
3695 // produced by a && or || operator is not necessarily a boolean.
3696
3697 // NOTE: If the left hand side produces a materialized value (not in
3698 // the CC register), we force the right hand side to do the
3699 // same. This is necessary because we may have to branch to the exit
3700 // after evaluating the left hand side (due to the shortcut
3701 // semantics), but the compiler must (statically) know if the result
3702 // of compiling the binary operation is materialized or not.
3703
3704 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003705 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003706 LoadConditionAndSpill(node->left(),
3707 NOT_INSIDE_TYPEOF,
3708 &is_true,
3709 false_target(),
3710 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003711 if (has_valid_frame() && !has_cc()) {
3712 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003713 JumpTarget pop_and_continue;
3714 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003716 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003717 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718 // Avoid popping the result if it converts to 'false' using the
3719 // standard ToBoolean() conversion as described in ECMA-262,
3720 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003721 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722 Branch(false, &exit);
3723
3724 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003725 pop_and_continue.Bind();
3726 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727
3728 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729 is_true.Bind();
3730 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003731
3732 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003733 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003734 } else if (has_cc() || is_true.is_linked()) {
3735 // The left-hand side is either (a) partially compiled to
3736 // control flow with a final branch left to emit or (b) fully
3737 // compiled to control flow and possibly true.
3738 if (has_cc()) {
3739 Branch(false, false_target());
3740 }
3741 is_true.Bind();
3742 LoadConditionAndSpill(node->right(),
3743 NOT_INSIDE_TYPEOF,
3744 true_target(),
3745 false_target(),
3746 false);
3747 } else {
3748 // Nothing to do.
3749 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750 }
3751
3752 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003753 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003754 LoadConditionAndSpill(node->left(),
3755 NOT_INSIDE_TYPEOF,
3756 true_target(),
3757 &is_false,
3758 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003759 if (has_valid_frame() && !has_cc()) {
3760 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003761 JumpTarget pop_and_continue;
3762 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003763
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003764 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003765 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766 // Avoid popping the result if it converts to 'true' using the
3767 // standard ToBoolean() conversion as described in ECMA-262,
3768 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003769 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770 Branch(true, &exit);
3771
3772 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003773 pop_and_continue.Bind();
3774 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775
3776 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003777 is_false.Bind();
3778 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779
3780 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003781 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003782 } else if (has_cc() || is_false.is_linked()) {
3783 // The left-hand side is either (a) partially compiled to
3784 // control flow with a final branch left to emit or (b) fully
3785 // compiled to control flow and possibly false.
3786 if (has_cc()) {
3787 Branch(true, true_target());
3788 }
3789 is_false.Bind();
3790 LoadConditionAndSpill(node->right(),
3791 NOT_INSIDE_TYPEOF,
3792 true_target(),
3793 false_target(),
3794 false);
3795 } else {
3796 // Nothing to do.
3797 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 }
3799
3800 } else {
3801 // Optimize for the case where (at least) one of the expressions
3802 // is a literal small integer.
3803 Literal* lliteral = node->left()->AsLiteral();
3804 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003805 // NOTE: The code below assumes that the slow cases (calls to runtime)
3806 // never return a constant/immutable object.
3807 bool overwrite_left =
3808 (node->left()->AsBinaryOperation() != NULL &&
3809 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3810 bool overwrite_right =
3811 (node->right()->AsBinaryOperation() != NULL &&
3812 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813
3814 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003815 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003816 SmiOperation(node->op(),
3817 rliteral->handle(),
3818 false,
3819 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820
3821 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003822 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003823 SmiOperation(node->op(),
3824 lliteral->handle(),
3825 true,
3826 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827
3828 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003829 OverwriteMode overwrite_mode = NO_OVERWRITE;
3830 if (overwrite_left) {
3831 overwrite_mode = OVERWRITE_LEFT;
3832 } else if (overwrite_right) {
3833 overwrite_mode = OVERWRITE_RIGHT;
3834 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003835 LoadAndSpill(node->left());
3836 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003837 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003838 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003839 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003841 ASSERT(!has_valid_frame() ||
3842 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003843 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844}
3845
3846
ager@chromium.org7c537e22008-10-16 08:43:32 +00003847void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003848#ifdef DEBUG
3849 int original_height = frame_->height();
3850#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003851 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003852 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003853 frame_->EmitPush(r0);
3854 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855}
3856
3857
ager@chromium.org7c537e22008-10-16 08:43:32 +00003858void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003859#ifdef DEBUG
3860 int original_height = frame_->height();
3861#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003862 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863 Comment cmnt(masm_, "[ CompareOperation");
3864
3865 // Get the expressions from the node.
3866 Expression* left = node->left();
3867 Expression* right = node->right();
3868 Token::Value op = node->op();
3869
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003870 // To make null checks efficient, we check if either left or right is the
3871 // literal 'null'. If so, we optimize the code by inlining a null check
3872 // instead of calling the (very) general runtime routine for checking
3873 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003875 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003876 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003877 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003878 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3879 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881 LoadAndSpill(left_is_null ? right : left);
3882 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883 __ cmp(r0, Operand(Factory::null_value()));
3884
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003885 // The 'null' value is only equal to 'undefined' if using non-strict
3886 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003888 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003889
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003891 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003893 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003896 // It can be an undetectable object.
3897 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3898 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3899 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3900 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003901 }
3902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003904 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003905 return;
3906 }
3907 }
3908
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003909 // To make typeof testing for natives implemented in JavaScript really
3910 // efficient, we generate special code for expressions of the form:
3911 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003912 UnaryOperation* operation = left->AsUnaryOperation();
3913 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3914 (operation != NULL && operation->op() == Token::TYPEOF) &&
3915 (right->AsLiteral() != NULL &&
3916 right->AsLiteral()->handle()->IsString())) {
3917 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3918
mads.s.ager31e71382008-08-13 09:32:07 +00003919 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003922
3923 if (check->Equals(Heap::number_symbol())) {
3924 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003925 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3927 __ cmp(r1, Operand(Factory::heap_number_map()));
3928 cc_reg_ = eq;
3929
3930 } else if (check->Equals(Heap::string_symbol())) {
3931 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003932 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933
3934 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3935
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003936 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3938 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3939 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003940 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941
3942 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3943 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3944 cc_reg_ = lt;
3945
3946 } else if (check->Equals(Heap::boolean_symbol())) {
3947 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003948 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949 __ cmp(r1, Operand(Factory::false_value()));
3950 cc_reg_ = eq;
3951
3952 } else if (check->Equals(Heap::undefined_symbol())) {
3953 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003954 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955
3956 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003957 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003959 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3961 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3962 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3963 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3964
3965 cc_reg_ = eq;
3966
3967 } else if (check->Equals(Heap::function_symbol())) {
3968 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003969 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003970 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971 cc_reg_ = eq;
3972
3973 } else if (check->Equals(Heap::object_symbol())) {
3974 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003975 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976
3977 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3978 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003979 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003981 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3983 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3984 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003985 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986
3987 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3988 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3991 cc_reg_ = le;
3992
3993 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003994 // Uncommon case: typeof testing against a string literal that is
3995 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003996 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003997 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003998 ASSERT(!has_valid_frame() ||
3999 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004000 return;
4001 }
4002
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 switch (op) {
4004 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004005 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 break;
4007
4008 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004009 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004010 break;
4011
4012 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004013 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014 break;
4015
4016 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004017 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 break;
4019
4020 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004021 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022 break;
4023
4024 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004025 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026 break;
4027
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004028 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004029 LoadAndSpill(left);
4030 LoadAndSpill(right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004031 Result arg_count(r0);
4032 __ mov(r0, Operand(1)); // not counting receiver
4033 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2);
4034 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004036 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004038 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004039 LoadAndSpill(left);
4040 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004041 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004042 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004043 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004044 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004045 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004047 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048
4049 default:
4050 UNREACHABLE();
4051 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004052 ASSERT((has_cc() && frame_->height() == original_height) ||
4053 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054}
4055
4056
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004057#ifdef DEBUG
4058bool CodeGenerator::HasValidEntryRegisters() { return true; }
4059#endif
4060
4061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004063#define __ ACCESS_MASM(masm)
4064
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065
ager@chromium.org7c537e22008-10-16 08:43:32 +00004066Handle<String> Reference::GetName() {
4067 ASSERT(type_ == NAMED);
4068 Property* property = expression_->AsProperty();
4069 if (property == NULL) {
4070 // Global variable reference treated as a named property reference.
4071 VariableProxy* proxy = expression_->AsVariableProxy();
4072 ASSERT(proxy->AsVariable() != NULL);
4073 ASSERT(proxy->AsVariable()->is_global());
4074 return proxy->name();
4075 } else {
4076 Literal* raw_name = property->key()->AsLiteral();
4077 ASSERT(raw_name != NULL);
4078 return Handle<String>(String::cast(*raw_name->handle()));
4079 }
4080}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004081
ager@chromium.org7c537e22008-10-16 08:43:32 +00004082
4083void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004084 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004085 ASSERT(!is_illegal());
4086 ASSERT(!cgen_->has_cc());
4087 MacroAssembler* masm = cgen_->masm();
4088 Property* property = expression_->AsProperty();
4089 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004090 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004091 }
4092
4093 switch (type_) {
4094 case SLOT: {
4095 Comment cmnt(masm, "[ Load from Slot");
4096 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4097 ASSERT(slot != NULL);
4098 cgen_->LoadFromSlot(slot, typeof_state);
4099 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004100 }
4101
ager@chromium.org7c537e22008-10-16 08:43:32 +00004102 case NAMED: {
4103 // TODO(1241834): Make sure that this it is safe to ignore the
4104 // distinction between expressions in a typeof and not in a typeof. If
4105 // there is a chance that reference errors can be thrown below, we
4106 // must distinguish between the two kinds of loads (typeof expression
4107 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004108 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004109 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004110 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004111 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004112 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4113 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004114 Result name_reg(r2);
4115 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 ASSERT(var == NULL || var->is_global());
4117 RelocInfo::Mode rmode = (var == NULL)
4118 ? RelocInfo::CODE_TARGET
4119 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004120 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4121 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004122 break;
4123 }
4124
4125 case KEYED: {
4126 // TODO(1241834): Make sure that this it is safe to ignore the
4127 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004128
4129 // TODO(181): Implement inlined version of array indexing once
4130 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004131 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004132 Comment cmnt(masm, "[ Load from keyed Property");
4133 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004134 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004135 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004136 ASSERT(var == NULL || var->is_global());
4137 RelocInfo::Mode rmode = (var == NULL)
4138 ? RelocInfo::CODE_TARGET
4139 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004140 frame->CallCodeObject(ic, rmode, 0);
4141 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004142 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004143 }
4144
4145 default:
4146 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004147 }
4148}
4149
4150
ager@chromium.org7c537e22008-10-16 08:43:32 +00004151void Reference::SetValue(InitState init_state) {
4152 ASSERT(!is_illegal());
4153 ASSERT(!cgen_->has_cc());
4154 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004155 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004156 Property* property = expression_->AsProperty();
4157 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004158 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004159 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004160
ager@chromium.org7c537e22008-10-16 08:43:32 +00004161 switch (type_) {
4162 case SLOT: {
4163 Comment cmnt(masm, "[ Store to Slot");
4164 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4165 ASSERT(slot != NULL);
4166 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004167 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004168
ager@chromium.org7c537e22008-10-16 08:43:32 +00004169 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004170 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004171 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004172 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004173
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174 if (init_state == CONST_INIT) {
4175 // Same as the case for a normal store, but ignores attribute
4176 // (e.g. READ_ONLY) of context slot so that we can initialize
4177 // const properties (introduced via eval("const foo = (some
4178 // expr);")). Also, uses the current function context instead of
4179 // the top context.
4180 //
4181 // Note that we must declare the foo upon entry of eval(), via a
4182 // context slot declaration, but we cannot initialize it at the
4183 // same time, because the const declaration may be at the end of
4184 // the eval code (sigh...) and the const variable may have been
4185 // used before (where its value is 'undefined'). Thus, we can only
4186 // do the initialization when we actually encounter the expression
4187 // and when the expression operands are defined and valid, and
4188 // thus we need the split into 2 operations: declaration of the
4189 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004190 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004191 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004192 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004193 }
4194 // Storing a variable must keep the (new) value on the expression
4195 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004196 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004197
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004199 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004200
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004201 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004202 if (init_state == CONST_INIT) {
4203 ASSERT(slot->var()->mode() == Variable::CONST);
4204 // Only the first const initialization must be executed (the slot
4205 // still contains 'the hole' value). When the assignment is
4206 // executed, the code is identical to a normal store (see below).
4207 Comment cmnt(masm, "[ Init const");
4208 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4209 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004210 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004211 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004212
ager@chromium.org7c537e22008-10-16 08:43:32 +00004213 // We must execute the store. Storing a variable must keep the
4214 // (new) value on the stack. This is necessary for compiling
4215 // assignment expressions.
4216 //
4217 // Note: We will reach here even with slot->var()->mode() ==
4218 // Variable::CONST because of const declarations which will
4219 // initialize consts to 'the hole' value and by doing so, end up
4220 // calling this code. r2 may be loaded with context; used below in
4221 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004222 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004223 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004224 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004225 if (slot->type() == Slot::CONTEXT) {
4226 // Skip write barrier if the written value is a smi.
4227 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004228 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004229 // r2 is loaded with context when calling SlotOperand above.
4230 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4231 __ mov(r3, Operand(offset));
4232 __ RecordWrite(r2, r3, r1);
4233 }
4234 // If we definitely did not jump over the assignment, we do not need
4235 // to bind the exit label. Doing so can defeat peephole
4236 // optimization.
4237 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004238 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004239 }
4240 }
4241 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004242 }
4243
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 case NAMED: {
4245 Comment cmnt(masm, "[ Store to named Property");
4246 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004247 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 Handle<String> name(GetName());
4249
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004250 Result value(r0);
4251 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004252
4253 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004254 Result property_name(r2);
4255 __ mov(r2, Operand(name));
4256 frame->CallCodeObject(ic,
4257 RelocInfo::CODE_TARGET,
4258 &value,
4259 &property_name,
4260 0);
4261 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004262 break;
4263 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004264
ager@chromium.org7c537e22008-10-16 08:43:32 +00004265 case KEYED: {
4266 Comment cmnt(masm, "[ Store to keyed Property");
4267 Property* property = expression_->AsProperty();
4268 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004269 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004270
4271 // Call IC code.
4272 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4273 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004274 Result value(r0);
4275 frame->EmitPop(r0); // value
4276 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4277 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004278 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004279 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004280
4281 default:
4282 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004283 }
4284}
4285
4286
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004287// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4288// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4289// (31 instead of 32).
4290static void CountLeadingZeros(
4291 MacroAssembler* masm,
4292 Register source,
4293 Register scratch,
4294 Register zeros) {
4295#ifdef __ARM_ARCH_5__
4296 __ clz(zeros, source); // This instruction is only supported after ARM5.
4297#else
4298 __ mov(zeros, Operand(0));
4299 __ mov(scratch, source);
4300 // Top 16.
4301 __ tst(scratch, Operand(0xffff0000));
4302 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4303 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4304 // Top 8.
4305 __ tst(scratch, Operand(0xff000000));
4306 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4307 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4308 // Top 4.
4309 __ tst(scratch, Operand(0xf0000000));
4310 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4311 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4312 // Top 2.
4313 __ tst(scratch, Operand(0xc0000000));
4314 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4315 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4316 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004317 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004318 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4319#endif
4320}
4321
4322
4323// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4324// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4325// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4326// scratch register. Destroys the source register. No GC occurs during this
4327// stub so you don't have to set up the frame.
4328class ConvertToDoubleStub : public CodeStub {
4329 public:
4330 ConvertToDoubleStub(Register result_reg_1,
4331 Register result_reg_2,
4332 Register source_reg,
4333 Register scratch_reg)
4334 : result1_(result_reg_1),
4335 result2_(result_reg_2),
4336 source_(source_reg),
4337 zeros_(scratch_reg) { }
4338
4339 private:
4340 Register result1_;
4341 Register result2_;
4342 Register source_;
4343 Register zeros_;
4344
4345 // Minor key encoding in 16 bits.
4346 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4347 class OpBits: public BitField<Token::Value, 2, 14> {};
4348
4349 Major MajorKey() { return ConvertToDouble; }
4350 int MinorKey() {
4351 // Encode the parameters in a unique 16 bit value.
4352 return result1_.code() +
4353 (result2_.code() << 4) +
4354 (source_.code() << 8) +
4355 (zeros_.code() << 12);
4356 }
4357
4358 void Generate(MacroAssembler* masm);
4359
4360 const char* GetName() { return "ConvertToDoubleStub"; }
4361
4362#ifdef DEBUG
4363 void Print() { PrintF("ConvertToDoubleStub\n"); }
4364#endif
4365};
4366
4367
4368void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4369#ifndef BIG_ENDIAN_FLOATING_POINT
4370 Register exponent = result1_;
4371 Register mantissa = result2_;
4372#else
4373 Register exponent = result2_;
4374 Register mantissa = result1_;
4375#endif
4376 Label not_special;
4377 // Convert from Smi to integer.
4378 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4379 // Move sign bit from source to destination. This works because the sign bit
4380 // in the exponent word of the double has the same position and polarity as
4381 // the 2's complement sign bit in a Smi.
4382 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4383 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4384 // Subtract from 0 if source was negative.
4385 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4386 __ cmp(source_, Operand(1));
4387 __ b(gt, &not_special);
4388
4389 // We have -1, 0 or 1, which we treat specially.
4390 __ cmp(source_, Operand(0));
4391 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4392 static const uint32_t exponent_word_for_1 =
4393 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4394 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4395 // 1, 0 and -1 all have 0 for the second word.
4396 __ mov(mantissa, Operand(0));
4397 __ Ret();
4398
4399 __ bind(&not_special);
4400 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4401 // Gets the wrong answer for 0, but we already checked for that case above.
4402 CountLeadingZeros(masm, source_, mantissa, zeros_);
4403 // Compute exponent and or it into the exponent register.
4404 // We use result2 as a scratch register here.
4405 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4406 __ orr(exponent,
4407 exponent,
4408 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4409 // Shift up the source chopping the top bit off.
4410 __ add(zeros_, zeros_, Operand(1));
4411 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4412 __ mov(source_, Operand(source_, LSL, zeros_));
4413 // Compute lower part of fraction (last 12 bits).
4414 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4415 // And the top (top 20 bits).
4416 __ orr(exponent,
4417 exponent,
4418 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4419 __ Ret();
4420}
4421
4422
4423// This stub can convert a signed int32 to a heap number (double). It does
4424// not work for int32s that are in Smi range! No GC occurs during this stub
4425// so you don't have to set up the frame.
4426class WriteInt32ToHeapNumberStub : public CodeStub {
4427 public:
4428 WriteInt32ToHeapNumberStub(Register the_int,
4429 Register the_heap_number,
4430 Register scratch)
4431 : the_int_(the_int),
4432 the_heap_number_(the_heap_number),
4433 scratch_(scratch) { }
4434
4435 private:
4436 Register the_int_;
4437 Register the_heap_number_;
4438 Register scratch_;
4439
4440 // Minor key encoding in 16 bits.
4441 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4442 class OpBits: public BitField<Token::Value, 2, 14> {};
4443
4444 Major MajorKey() { return WriteInt32ToHeapNumber; }
4445 int MinorKey() {
4446 // Encode the parameters in a unique 16 bit value.
4447 return the_int_.code() +
4448 (the_heap_number_.code() << 4) +
4449 (scratch_.code() << 8);
4450 }
4451
4452 void Generate(MacroAssembler* masm);
4453
4454 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4455
4456#ifdef DEBUG
4457 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4458#endif
4459};
4460
4461
4462// See comment for class.
4463void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4464 Label max_negative_int;
4465 // the_int_ has the answer which is a signed int32 but not a Smi.
4466 // We test for the special value that has a different exponent. This test
4467 // has the neat side effect of setting the flags according to the sign.
4468 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004469 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004470 __ b(eq, &max_negative_int);
4471 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4472 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4473 uint32_t non_smi_exponent =
4474 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4475 __ mov(scratch_, Operand(non_smi_exponent));
4476 // Set the sign bit in scratch_ if the value was negative.
4477 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4478 // Subtract from 0 if the value was negative.
4479 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4480 // We should be masking the implict first digit of the mantissa away here,
4481 // but it just ends up combining harmlessly with the last digit of the
4482 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4483 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4484 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4485 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4486 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4487 __ str(scratch_, FieldMemOperand(the_heap_number_,
4488 HeapNumber::kExponentOffset));
4489 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4490 __ str(scratch_, FieldMemOperand(the_heap_number_,
4491 HeapNumber::kMantissaOffset));
4492 __ Ret();
4493
4494 __ bind(&max_negative_int);
4495 // The max negative int32 is stored as a positive number in the mantissa of
4496 // a double because it uses a sign bit instead of using two's complement.
4497 // The actual mantissa bits stored are all 0 because the implicit most
4498 // significant 1 bit is not stored.
4499 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4500 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4501 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4502 __ mov(ip, Operand(0));
4503 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4504 __ Ret();
4505}
4506
4507
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004508// Handle the case where the lhs and rhs are the same object.
4509// Equality is almost reflexive (everything but NaN), so this is a test
4510// for "identity and not NaN".
4511static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4512 Label* slow,
4513 Condition cc) {
4514 Label not_identical;
4515 __ cmp(r0, Operand(r1));
4516 __ b(ne, &not_identical);
4517
4518 Register exp_mask_reg = r5;
4519 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4520
4521 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4522 // so we do the second best thing - test it ourselves.
4523 Label heap_number, return_equal;
4524 // They are both equal and they are not both Smis so both of them are not
4525 // Smis. If it's not a heap number, then return equal.
4526 if (cc == lt || cc == gt) {
4527 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4528 __ b(ge, slow);
4529 } else {
4530 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4531 __ b(eq, &heap_number);
4532 // Comparing JS objects with <=, >= is complicated.
4533 if (cc != eq) {
4534 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4535 __ b(ge, slow);
4536 }
4537 }
4538 __ bind(&return_equal);
4539 if (cc == lt) {
4540 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4541 } else if (cc == gt) {
4542 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4543 } else {
4544 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4545 }
4546 __ mov(pc, Operand(lr)); // Return.
4547
4548 // For less and greater we don't have to check for NaN since the result of
4549 // x < x is false regardless. For the others here is some code to check
4550 // for NaN.
4551 if (cc != lt && cc != gt) {
4552 __ bind(&heap_number);
4553 // It is a heap number, so return non-equal if it's NaN and equal if it's
4554 // not NaN.
4555 // The representation of NaN values has all exponent bits (52..62) set,
4556 // and not all mantissa bits (0..51) clear.
4557 // Read top bits of double representation (second word of value).
4558 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4559 // Test that exponent bits are all set.
4560 __ and_(r3, r2, Operand(exp_mask_reg));
4561 __ cmp(r3, Operand(exp_mask_reg));
4562 __ b(ne, &return_equal);
4563
4564 // Shift out flag and all exponent bits, retaining only mantissa.
4565 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4566 // Or with all low-bits of mantissa.
4567 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4568 __ orr(r0, r3, Operand(r2), SetCC);
4569 // For equal we already have the right value in r0: Return zero (equal)
4570 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4571 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4572 // if it's a NaN.
4573 if (cc != eq) {
4574 // All-zero means Infinity means equal.
4575 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4576 if (cc == le) {
4577 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4578 } else {
4579 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4580 }
4581 }
4582 __ mov(pc, Operand(lr)); // Return.
4583 }
4584 // No fall through here.
4585
4586 __ bind(&not_identical);
4587}
4588
4589
4590// See comment at call site.
4591static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4592 Label* rhs_not_nan,
4593 Label* slow,
4594 bool strict) {
4595 Label lhs_is_smi;
4596 __ tst(r0, Operand(kSmiTagMask));
4597 __ b(eq, &lhs_is_smi);
4598
4599 // Rhs is a Smi. Check whether the non-smi is a heap number.
4600 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4601 if (strict) {
4602 // If lhs was not a number and rhs was a Smi then strict equality cannot
4603 // succeed. Return non-equal (r0 is already not zero)
4604 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4605 } else {
4606 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4607 // the runtime.
4608 __ b(ne, slow);
4609 }
4610
4611 // Rhs is a smi, lhs is a number.
4612 __ push(lr);
4613 __ mov(r7, Operand(r1));
4614 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4615 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4616 // r3 and r2 are rhs as double.
4617 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4618 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4619 // We now have both loaded as doubles but we can skip the lhs nan check
4620 // since it's a Smi.
4621 __ pop(lr);
4622 __ jmp(rhs_not_nan);
4623
4624 __ bind(&lhs_is_smi);
4625 // Lhs is a Smi. Check whether the non-smi is a heap number.
4626 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4627 if (strict) {
4628 // If lhs was not a number and rhs was a Smi then strict equality cannot
4629 // succeed. Return non-equal.
4630 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4631 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4632 } else {
4633 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4634 // the runtime.
4635 __ b(ne, slow);
4636 }
4637
4638 // Lhs is a smi, rhs is a number.
4639 // r0 is Smi and r1 is heap number.
4640 __ push(lr);
4641 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4642 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4643 __ mov(r7, Operand(r0));
4644 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4645 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4646 __ pop(lr);
4647 // Fall through to both_loaded_as_doubles.
4648}
4649
4650
4651void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4652 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4653 Register lhs_exponent = exp_first ? r0 : r1;
4654 Register rhs_exponent = exp_first ? r2 : r3;
4655 Register lhs_mantissa = exp_first ? r1 : r0;
4656 Register rhs_mantissa = exp_first ? r3 : r2;
4657 Label one_is_nan, neither_is_nan;
4658
4659 Register exp_mask_reg = r5;
4660
4661 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4662 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4663 __ cmp(r4, Operand(exp_mask_reg));
4664 __ b(ne, rhs_not_nan);
4665 __ mov(r4,
4666 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4667 SetCC);
4668 __ b(ne, &one_is_nan);
4669 __ cmp(rhs_mantissa, Operand(0));
4670 __ b(ne, &one_is_nan);
4671
4672 __ bind(rhs_not_nan);
4673 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4674 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4675 __ cmp(r4, Operand(exp_mask_reg));
4676 __ b(ne, &neither_is_nan);
4677 __ mov(r4,
4678 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4679 SetCC);
4680 __ b(ne, &one_is_nan);
4681 __ cmp(lhs_mantissa, Operand(0));
4682 __ b(eq, &neither_is_nan);
4683
4684 __ bind(&one_is_nan);
4685 // NaN comparisons always fail.
4686 // Load whatever we need in r0 to make the comparison fail.
4687 if (cc == lt || cc == le) {
4688 __ mov(r0, Operand(GREATER));
4689 } else {
4690 __ mov(r0, Operand(LESS));
4691 }
4692 __ mov(pc, Operand(lr)); // Return.
4693
4694 __ bind(&neither_is_nan);
4695}
4696
4697
4698// See comment at call site.
4699static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4700 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4701 Register lhs_exponent = exp_first ? r0 : r1;
4702 Register rhs_exponent = exp_first ? r2 : r3;
4703 Register lhs_mantissa = exp_first ? r1 : r0;
4704 Register rhs_mantissa = exp_first ? r3 : r2;
4705
4706 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4707 if (cc == eq) {
4708 // Doubles are not equal unless they have the same bit pattern.
4709 // Exception: 0 and -0.
4710 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4711 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4712 // Return non-zero if the numbers are unequal.
4713 __ mov(pc, Operand(lr), LeaveCC, ne);
4714
4715 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4716 // If exponents are equal then return 0.
4717 __ mov(pc, Operand(lr), LeaveCC, eq);
4718
4719 // Exponents are unequal. The only way we can return that the numbers
4720 // are equal is if one is -0 and the other is 0. We already dealt
4721 // with the case where both are -0 or both are 0.
4722 // We start by seeing if the mantissas (that are equal) or the bottom
4723 // 31 bits of the rhs exponent are non-zero. If so we return not
4724 // equal.
4725 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4726 __ mov(r0, Operand(r4), LeaveCC, ne);
4727 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4728 // Now they are equal if and only if the lhs exponent is zero in its
4729 // low 31 bits.
4730 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4731 __ mov(pc, Operand(lr));
4732 } else {
4733 // Call a native function to do a comparison between two non-NaNs.
4734 // Call C routine that may not cause GC or other trouble.
4735 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4736 __ Jump(r5); // Tail call.
4737 }
4738}
4739
4740
4741// See comment at call site.
4742static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4743 // If either operand is a JSObject or an oddball value, then they are
4744 // not equal since their pointers are different.
4745 // There is no test for undetectability in strict equality.
4746 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4747 Label first_non_object;
4748 // Get the type of the first operand into r2 and compare it with
4749 // FIRST_JS_OBJECT_TYPE.
4750 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4751 __ b(lt, &first_non_object);
4752
4753 // Return non-zero (r0 is not zero)
4754 Label return_not_equal;
4755 __ bind(&return_not_equal);
4756 __ mov(pc, Operand(lr)); // Return.
4757
4758 __ bind(&first_non_object);
4759 // Check for oddballs: true, false, null, undefined.
4760 __ cmp(r2, Operand(ODDBALL_TYPE));
4761 __ b(eq, &return_not_equal);
4762
4763 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4764 __ b(ge, &return_not_equal);
4765
4766 // Check for oddballs: true, false, null, undefined.
4767 __ cmp(r3, Operand(ODDBALL_TYPE));
4768 __ b(eq, &return_not_equal);
4769}
4770
4771
4772// See comment at call site.
4773static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4774 Label* both_loaded_as_doubles,
4775 Label* not_heap_numbers,
4776 Label* slow) {
4777 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4778 __ b(ne, not_heap_numbers);
4779 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4780 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4781
4782 // Both are heap numbers. Load them up then jump to the code we have
4783 // for that.
4784 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4785 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4786 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4787 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4788 __ jmp(both_loaded_as_doubles);
4789}
4790
4791
4792// Fast negative check for symbol-to-symbol equality.
4793static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4794 // r2 is object type of r0.
4795 __ tst(r2, Operand(kIsNotStringMask));
4796 __ b(ne, slow);
4797 __ tst(r2, Operand(kIsSymbolMask));
4798 __ b(eq, slow);
4799 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4800 __ b(ge, slow);
4801 __ tst(r3, Operand(kIsSymbolMask));
4802 __ b(eq, slow);
4803
4804 // Both are symbols. We already checked they weren't the same pointer
4805 // so they are not equal.
4806 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4807 __ mov(pc, Operand(lr)); // Return.
4808}
4809
4810
4811// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4812// positive or negative to indicate the result of the comparison.
4813void CompareStub::Generate(MacroAssembler* masm) {
4814 Label slow; // Call builtin.
4815 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4816
4817 // NOTICE! This code is only reached after a smi-fast-case check, so
4818 // it is certain that at least one operand isn't a smi.
4819
4820 // Handle the case where the objects are identical. Either returns the answer
4821 // or goes to slow. Only falls through if the objects were not identical.
4822 EmitIdenticalObjectComparison(masm, &slow, cc_);
4823
4824 // If either is a Smi (we know that not both are), then they can only
4825 // be strictly equal if the other is a HeapNumber.
4826 ASSERT_EQ(0, kSmiTag);
4827 ASSERT_EQ(0, Smi::FromInt(0));
4828 __ and_(r2, r0, Operand(r1));
4829 __ tst(r2, Operand(kSmiTagMask));
4830 __ b(ne, &not_smis);
4831 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4832 // 1) Return the answer.
4833 // 2) Go to slow.
4834 // 3) Fall through to both_loaded_as_doubles.
4835 // 4) Jump to rhs_not_nan.
4836 // In cases 3 and 4 we have found out we were dealing with a number-number
4837 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4838 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4839
4840 __ bind(&both_loaded_as_doubles);
4841 // r0, r1, r2, r3 are the double representations of the left hand side
4842 // and the right hand side.
4843
4844 // Checks for NaN in the doubles we have loaded. Can return the answer or
4845 // fall through if neither is a NaN. Also binds rhs_not_nan.
4846 EmitNanCheck(masm, &rhs_not_nan, cc_);
4847
4848 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4849 // answer. Never falls through.
4850 EmitTwoNonNanDoubleComparison(masm, cc_);
4851
4852 __ bind(&not_smis);
4853 // At this point we know we are dealing with two different objects,
4854 // and neither of them is a Smi. The objects are in r0 and r1.
4855 if (strict_) {
4856 // This returns non-equal for some object types, or falls through if it
4857 // was not lucky.
4858 EmitStrictTwoHeapObjectCompare(masm);
4859 }
4860
4861 Label check_for_symbols;
4862 // Check for heap-number-heap-number comparison. Can jump to slow case,
4863 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4864 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4865 // In this case r2 will contain the type of r0.
4866 EmitCheckForTwoHeapNumbers(masm,
4867 &both_loaded_as_doubles,
4868 &check_for_symbols,
4869 &slow);
4870
4871 __ bind(&check_for_symbols);
4872 if (cc_ == eq) {
4873 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4874 // of r0 on entry.
4875 EmitCheckForSymbols(masm, &slow);
4876 }
4877
4878 __ bind(&slow);
4879 __ push(lr);
4880 __ push(r1);
4881 __ push(r0);
4882 // Figure out which native to call and setup the arguments.
4883 Builtins::JavaScript native;
4884 int arg_count = 1; // Not counting receiver.
4885 if (cc_ == eq) {
4886 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4887 } else {
4888 native = Builtins::COMPARE;
4889 int ncr; // NaN compare result
4890 if (cc_ == lt || cc_ == le) {
4891 ncr = GREATER;
4892 } else {
4893 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4894 ncr = LESS;
4895 }
4896 arg_count++;
4897 __ mov(r0, Operand(Smi::FromInt(ncr)));
4898 __ push(r0);
4899 }
4900
4901 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4902 // tagged as a small integer.
4903 __ mov(r0, Operand(arg_count));
4904 __ InvokeBuiltin(native, CALL_JS);
4905 __ cmp(r0, Operand(0));
4906 __ pop(pc);
4907}
4908
4909
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004910// Allocates a heap number or jumps to the label if the young space is full and
4911// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004912static void AllocateHeapNumber(
4913 MacroAssembler* masm,
4914 Label* need_gc, // Jump here if young space is full.
4915 Register result_reg, // The tagged address of the new heap number.
4916 Register allocation_top_addr_reg, // A scratch register.
4917 Register scratch2) { // Another scratch register.
4918 ExternalReference allocation_top =
4919 ExternalReference::new_space_allocation_top_address();
4920 ExternalReference allocation_limit =
4921 ExternalReference::new_space_allocation_limit_address();
4922
4923 // allocat := the address of the allocation top variable.
4924 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4925 // result_reg := the old allocation top.
4926 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4927 // scratch2 := the address of the allocation limit.
4928 __ mov(scratch2, Operand(allocation_limit));
4929 // scratch2 := the allocation limit.
4930 __ ldr(scratch2, MemOperand(scratch2));
4931 // result_reg := the new allocation top.
4932 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4933 // Compare new new allocation top and limit.
4934 __ cmp(result_reg, Operand(scratch2));
4935 // Branch if out of space in young generation.
4936 __ b(hi, need_gc);
4937 // Store new allocation top.
4938 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4939 // Tag and adjust back to start of new object.
4940 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4941 // Get heap number map into scratch2.
4942 __ mov(scratch2, Operand(Factory::heap_number_map()));
4943 // Store heap number map in new object.
4944 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4945}
4946
4947
4948// We fall into this code if the operands were Smis, but the result was
4949// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004950// the operands were not both Smi. The operands are in r0 and r1. In order
4951// to call the C-implemented binary fp operation routines we need to end up
4952// with the double precision floating point operands in r0 and r1 (for the
4953// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004954static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4955 Label* not_smi,
4956 const Builtins::JavaScript& builtin,
4957 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004958 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004959 Label slow, slow_pop_2_first, do_the_call;
4960 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4961 // Smi-smi case (overflow).
4962 // Since both are Smis there is no heap number to overwrite, so allocate.
4963 // The new heap number is in r5. r6 and r7 are scratch.
4964 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4965 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004966 __ mov(r7, Operand(r0));
4967 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004968 __ push(lr);
4969 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4970 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4971 __ mov(r7, Operand(r1));
4972 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4973 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4974 __ pop(lr);
4975 __ jmp(&do_the_call); // Tail call. No return.
4976
4977 // We jump to here if something goes wrong (one param is not a number of any
4978 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004979 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004980 __ push(r1);
4981 __ push(r0);
4982 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004983 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004984
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004985 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004986 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004987 if (mode == NO_OVERWRITE) {
4988 // In the case where there is no chance of an overwritable float we may as
4989 // well do the allocation immediately while r0 and r1 are untouched.
4990 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4991 }
4992
4993 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004994 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004995 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
4996 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004997 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004998 if (mode == OVERWRITE_RIGHT) {
4999 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5000 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005001 // Calling convention says that second double is in r2 and r3.
5002 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005003 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5004 __ jmp(&finished_loading_r0);
5005 __ bind(&r0_is_smi);
5006 if (mode == OVERWRITE_RIGHT) {
5007 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005008 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005009 }
5010 // Write Smi from r0 to r3 and r2 in double format.
5011 __ mov(r7, Operand(r0));
5012 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5013 __ push(lr);
5014 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5015 __ pop(lr);
5016 __ bind(&finished_loading_r0);
5017
5018 // Move r1 to a double in r0-r1.
5019 __ tst(r1, Operand(kSmiTagMask));
5020 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5021 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5022 __ b(ne, &slow);
5023 if (mode == OVERWRITE_LEFT) {
5024 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005025 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005026 // Calling convention says that first double is in r0 and r1.
5027 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005028 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5029 __ jmp(&finished_loading_r1);
5030 __ bind(&r1_is_smi);
5031 if (mode == OVERWRITE_LEFT) {
5032 // We can't overwrite a Smi so get address of new heap number into r5.
5033 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5034 }
5035 // Write Smi from r1 to r1 and r0 in double format.
5036 __ mov(r7, Operand(r1));
5037 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5038 __ push(lr);
5039 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5040 __ pop(lr);
5041 __ bind(&finished_loading_r1);
5042
5043 __ bind(&do_the_call);
5044 // r0: Left value (least significant part of mantissa).
5045 // r1: Left value (sign, exponent, top of mantissa).
5046 // r2: Right value (least significant part of mantissa).
5047 // r3: Right value (sign, exponent, top of mantissa).
5048 // r5: Address of heap number for result.
5049 __ push(lr); // For later.
5050 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005051 // Call C routine that may not cause GC or other trouble.
5052 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005053 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005054 // Store answer in the overwritable heap number.
5055 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005056#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005057 // Double returned in fp coprocessor register 0 and 1, encoded as register
5058 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5059 // substract the tag from r4.
5060 __ sub(r5, r4, Operand(kHeapObjectTag));
5061 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5062#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005063 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005064 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005065 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005066#endif
5067 __ mov(r0, Operand(r4));
5068 // And we are done.
5069 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005070}
5071
5072
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005073// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005074// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005075// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5076// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005077// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5078// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005079static void GetInt32(MacroAssembler* masm,
5080 Register source,
5081 Register dest,
5082 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005083 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005084 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005085 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005086 // Get exponent word.
5087 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5088 // Get exponent alone in scratch2.
5089 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005090 // Load dest with zero. We use this either for the final shift or
5091 // for the answer.
5092 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005093 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005094 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5095 // the exponent that we are fastest at and also the highest exponent we can
5096 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005097 const uint32_t non_smi_exponent =
5098 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5099 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005100 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5101 __ b(eq, &right_exponent);
5102 // If the exponent is higher than that then go to slow case. This catches
5103 // numbers that don't fit in a signed int32, infinities and NaNs.
5104 __ b(gt, slow);
5105
5106 // We know the exponent is smaller than 30 (biased). If it is less than
5107 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5108 // it rounds to zero.
5109 const uint32_t zero_exponent =
5110 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5111 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5112 // Dest already has a Smi zero.
5113 __ b(lt, &done);
5114 // We have a shifted exponent between 0 and 30 in scratch2.
5115 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5116 // We now have the exponent in dest. Subtract from 30 to get
5117 // how much to shift down.
5118 __ rsb(dest, dest, Operand(30));
5119
5120 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005121 // Get the top bits of the mantissa.
5122 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5123 // Put back the implicit 1.
5124 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5125 // Shift up the mantissa bits to take up the space the exponent used to take.
5126 // We just orred in the implicit bit so that took care of one and we want to
5127 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5128 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5129 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5130 // Put sign in zero flag.
5131 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005132 // Get the second half of the double. For some exponents we don't actually
5133 // need this because the bits get shifted out again, but it's probably slower
5134 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005135 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5136 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005137 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5138 // Move down according to the exponent.
5139 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005140 // Fix sign if sign bit was set.
5141 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005142 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005143}
5144
5145
5146// For bitwise ops where the inputs are not both Smis we here try to determine
5147// whether both inputs are either Smis or at least heap numbers that can be
5148// represented by a 32 bit signed value. We truncate towards zero as required
5149// by the ES spec. If this is the case we do the bitwise op and see if the
5150// result is a Smi. If so, great, otherwise we try to find a heap number to
5151// write the answer into (either by allocating or by overwriting).
5152// On entry the operands are in r0 and r1. On exit the answer is in r0.
5153void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5154 Label slow, result_not_a_smi;
5155 Label r0_is_smi, r1_is_smi;
5156 Label done_checking_r0, done_checking_r1;
5157
5158 __ tst(r1, Operand(kSmiTagMask));
5159 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5160 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5161 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005162 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005163 __ jmp(&done_checking_r1);
5164 __ bind(&r1_is_smi);
5165 __ mov(r3, Operand(r1, ASR, 1));
5166 __ bind(&done_checking_r1);
5167
5168 __ tst(r0, Operand(kSmiTagMask));
5169 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5170 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5171 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005172 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005173 __ jmp(&done_checking_r0);
5174 __ bind(&r0_is_smi);
5175 __ mov(r2, Operand(r0, ASR, 1));
5176 __ bind(&done_checking_r0);
5177
5178 // r0 and r1: Original operands (Smi or heap numbers).
5179 // r2 and r3: Signed int32 operands.
5180 switch (op_) {
5181 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5182 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5183 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5184 case Token::SAR:
5185 // Use only the 5 least significant bits of the shift count.
5186 __ and_(r2, r2, Operand(0x1f));
5187 __ mov(r2, Operand(r3, ASR, r2));
5188 break;
5189 case Token::SHR:
5190 // Use only the 5 least significant bits of the shift count.
5191 __ and_(r2, r2, Operand(0x1f));
5192 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5193 // SHR is special because it is required to produce a positive answer.
5194 // The code below for writing into heap numbers isn't capable of writing
5195 // the register as an unsigned int so we go to slow case if we hit this
5196 // case.
5197 __ b(mi, &slow);
5198 break;
5199 case Token::SHL:
5200 // Use only the 5 least significant bits of the shift count.
5201 __ and_(r2, r2, Operand(0x1f));
5202 __ mov(r2, Operand(r3, LSL, r2));
5203 break;
5204 default: UNREACHABLE();
5205 }
5206 // check that the *signed* result fits in a smi
5207 __ add(r3, r2, Operand(0x40000000), SetCC);
5208 __ b(mi, &result_not_a_smi);
5209 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5210 __ Ret();
5211
5212 Label have_to_allocate, got_a_heap_number;
5213 __ bind(&result_not_a_smi);
5214 switch (mode_) {
5215 case OVERWRITE_RIGHT: {
5216 __ tst(r0, Operand(kSmiTagMask));
5217 __ b(eq, &have_to_allocate);
5218 __ mov(r5, Operand(r0));
5219 break;
5220 }
5221 case OVERWRITE_LEFT: {
5222 __ tst(r1, Operand(kSmiTagMask));
5223 __ b(eq, &have_to_allocate);
5224 __ mov(r5, Operand(r1));
5225 break;
5226 }
5227 case NO_OVERWRITE: {
5228 // Get a new heap number in r5. r6 and r7 are scratch.
5229 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5230 }
5231 default: break;
5232 }
5233 __ bind(&got_a_heap_number);
5234 // r2: Answer as signed int32.
5235 // r5: Heap number to write answer into.
5236
5237 // Nothing can go wrong now, so move the heap number to r0, which is the
5238 // result.
5239 __ mov(r0, Operand(r5));
5240
5241 // Tail call that writes the int32 in r2 to the heap number in r0, using
5242 // r3 as scratch. r0 is preserved and returned.
5243 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5244 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5245
5246 if (mode_ != NO_OVERWRITE) {
5247 __ bind(&have_to_allocate);
5248 // Get a new heap number in r5. r6 and r7 are scratch.
5249 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5250 __ jmp(&got_a_heap_number);
5251 }
5252
5253 // If all else failed then we go to the runtime system.
5254 __ bind(&slow);
5255 __ push(r1); // restore stack
5256 __ push(r0);
5257 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5258 switch (op_) {
5259 case Token::BIT_OR:
5260 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5261 break;
5262 case Token::BIT_AND:
5263 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5264 break;
5265 case Token::BIT_XOR:
5266 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5267 break;
5268 case Token::SAR:
5269 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5270 break;
5271 case Token::SHR:
5272 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5273 break;
5274 case Token::SHL:
5275 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5276 break;
5277 default:
5278 UNREACHABLE();
5279 }
5280}
5281
5282
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005283// Can we multiply by x with max two shifts and an add.
5284// This answers yes to all integers from 2 to 10.
5285static bool IsEasyToMultiplyBy(int x) {
5286 if (x < 2) return false; // Avoid special cases.
5287 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5288 if (IsPowerOf2(x)) return true; // Simple shift.
5289 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5290 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5291 return false;
5292}
5293
5294
5295// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5296// Source and destination may be the same register. This routine does
5297// not set carry and overflow the way a mul instruction would.
5298static void MultiplyByKnownInt(MacroAssembler* masm,
5299 Register source,
5300 Register destination,
5301 int known_int) {
5302 if (IsPowerOf2(known_int)) {
5303 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5304 } else if (PopCountLessThanEqual2(known_int)) {
5305 int first_bit = BitPosition(known_int);
5306 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5307 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5308 if (first_bit != 0) {
5309 __ mov(destination, Operand(destination, LSL, first_bit));
5310 }
5311 } else {
5312 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5313 int the_bit = BitPosition(known_int + 1);
5314 __ rsb(destination, source, Operand(source, LSL, the_bit));
5315 }
5316}
5317
5318
5319// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5320// a register for the cases where it doesn't know a good trick, and may deliver
5321// a result that needs shifting.
5322static void MultiplyByKnownInt2(
5323 MacroAssembler* masm,
5324 Register result,
5325 Register source,
5326 Register known_int_register, // Smi tagged.
5327 int known_int,
5328 int* required_shift) { // Including Smi tag shift
5329 switch (known_int) {
5330 case 3:
5331 __ add(result, source, Operand(source, LSL, 1));
5332 *required_shift = 1;
5333 break;
5334 case 5:
5335 __ add(result, source, Operand(source, LSL, 2));
5336 *required_shift = 1;
5337 break;
5338 case 6:
5339 __ add(result, source, Operand(source, LSL, 1));
5340 *required_shift = 2;
5341 break;
5342 case 7:
5343 __ rsb(result, source, Operand(source, LSL, 3));
5344 *required_shift = 1;
5345 break;
5346 case 9:
5347 __ add(result, source, Operand(source, LSL, 3));
5348 *required_shift = 1;
5349 break;
5350 case 10:
5351 __ add(result, source, Operand(source, LSL, 2));
5352 *required_shift = 2;
5353 break;
5354 default:
5355 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5356 __ mul(result, source, known_int_register);
5357 *required_shift = 0;
5358 }
5359}
5360
5361
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005362void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5363 // r1 : x
5364 // r0 : y
5365 // result : r0
5366
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005367 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5368 // tell us that.
5369 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5370
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005371 switch (op_) {
5372 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005373 Label not_smi;
5374 // Fast path.
5375 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005376 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005377 __ b(ne, &not_smi);
5378 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5379 // Return if no overflow.
5380 __ Ret(vc);
5381 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5382
5383 HandleBinaryOpSlowCases(masm,
5384 &not_smi,
5385 Builtins::ADD,
5386 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005387 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005388 break;
5389 }
5390
5391 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005392 Label not_smi;
5393 // Fast path.
5394 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005395 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005396 __ b(ne, &not_smi);
5397 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5398 // Return if no overflow.
5399 __ Ret(vc);
5400 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5401
5402 HandleBinaryOpSlowCases(masm,
5403 &not_smi,
5404 Builtins::SUB,
5405 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005406 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005407 break;
5408 }
5409
5410 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005411 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005412 ASSERT(kSmiTag == 0); // adjust code below
5413 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005414 __ b(ne, &not_smi);
5415 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005416 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005417 // Do multiplication
5418 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5419 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005420 __ mov(ip, Operand(r3, ASR, 31));
5421 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5422 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005423 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005424 __ tst(r3, Operand(r3));
5425 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005426 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005427 // We need -0 if we were multiplying a negative number with 0 to get 0.
5428 // We know one of them was zero.
5429 __ add(r2, r0, Operand(r1), SetCC);
5430 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5431 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5432 // Slow case. We fall through here if we multiplied a negative number
5433 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005434 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005435
5436 HandleBinaryOpSlowCases(masm,
5437 &not_smi,
5438 Builtins::MUL,
5439 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005440 mode_);
5441 break;
5442 }
5443
5444 case Token::DIV:
5445 case Token::MOD: {
5446 Label not_smi;
5447 if (specialized_on_rhs_) {
5448 Label smi_is_unsuitable;
5449 __ BranchOnNotSmi(r1, &not_smi);
5450 if (IsPowerOf2(constant_rhs_)) {
5451 if (op_ == Token::MOD) {
5452 __ and_(r0,
5453 r1,
5454 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5455 SetCC);
5456 // We now have the answer, but if the input was negative we also
5457 // have the sign bit. Our work is done if the result is
5458 // positive or zero:
5459 __ Ret(pl);
5460 // A mod of a negative left hand side must return a negative number.
5461 // Unfortunately if the answer is 0 then we must return -0. And we
5462 // already optimistically trashed r0 so we may need to restore it.
5463 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5464 // Next two instructions are conditional on the answer being -0.
5465 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5466 __ b(eq, &smi_is_unsuitable);
5467 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5468 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5469 } else {
5470 ASSERT(op_ == Token::DIV);
5471 __ tst(r1,
5472 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5473 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5474 int shift = 0;
5475 int d = constant_rhs_;
5476 while ((d & 1) == 0) {
5477 d >>= 1;
5478 shift++;
5479 }
5480 __ mov(r0, Operand(r1, LSR, shift));
5481 __ bic(r0, r0, Operand(kSmiTagMask));
5482 }
5483 } else {
5484 // Not a power of 2.
5485 __ tst(r1, Operand(0x80000000u));
5486 __ b(ne, &smi_is_unsuitable);
5487 // Find a fixed point reciprocal of the divisor so we can divide by
5488 // multiplying.
5489 double divisor = 1.0 / constant_rhs_;
5490 int shift = 32;
5491 double scale = 4294967296.0; // 1 << 32.
5492 uint32_t mul;
5493 // Maximise the precision of the fixed point reciprocal.
5494 while (true) {
5495 mul = static_cast<uint32_t>(scale * divisor);
5496 if (mul >= 0x7fffffff) break;
5497 scale *= 2.0;
5498 shift++;
5499 }
5500 mul++;
5501 __ mov(r2, Operand(mul));
5502 __ umull(r3, r2, r2, r1);
5503 __ mov(r2, Operand(r2, LSR, shift - 31));
5504 // r2 is r1 / rhs. r2 is not Smi tagged.
5505 // r0 is still the known rhs. r0 is Smi tagged.
5506 // r1 is still the unkown lhs. r1 is Smi tagged.
5507 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5508 // r4 = r2 * r0.
5509 MultiplyByKnownInt2(masm,
5510 r4,
5511 r2,
5512 r0,
5513 constant_rhs_,
5514 &required_r4_shift);
5515 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5516 if (op_ == Token::DIV) {
5517 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5518 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5519 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5520 } else {
5521 ASSERT(op_ == Token::MOD);
5522 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5523 }
5524 }
5525 __ Ret();
5526 __ bind(&smi_is_unsuitable);
5527 } else {
5528 __ jmp(&not_smi);
5529 }
5530 HandleBinaryOpSlowCases(masm,
5531 &not_smi,
5532 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5533 op_,
5534 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005535 break;
5536 }
5537
5538 case Token::BIT_OR:
5539 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005540 case Token::BIT_XOR:
5541 case Token::SAR:
5542 case Token::SHR:
5543 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005544 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005545 ASSERT(kSmiTag == 0); // adjust code below
5546 __ tst(r2, Operand(kSmiTagMask));
5547 __ b(ne, &slow);
5548 switch (op_) {
5549 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5550 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5551 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005552 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005553 // Remove tags from right operand.
5554 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5555 // Use only the 5 least significant bits of the shift count.
5556 __ and_(r2, r2, Operand(0x1f));
5557 __ mov(r0, Operand(r1, ASR, r2));
5558 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005559 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005560 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005561 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005562 // Remove tags from operands. We can't do this on a 31 bit number
5563 // because then the 0s get shifted into bit 30 instead of bit 31.
5564 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5565 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5566 // Use only the 5 least significant bits of the shift count.
5567 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005568 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005569 // Unsigned shift is not allowed to produce a negative number, so
5570 // check the sign bit and the sign bit after Smi tagging.
5571 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005572 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005573 // Smi tag result.
5574 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005575 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005576 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005577 // Remove tags from operands.
5578 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5579 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5580 // Use only the 5 least significant bits of the shift count.
5581 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005582 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005583 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005584 __ add(r2, r3, Operand(0x40000000), SetCC);
5585 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005586 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005587 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005588 default: UNREACHABLE();
5589 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005590 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005591 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005592 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005593 break;
5594 }
5595
5596 default: UNREACHABLE();
5597 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005598 // This code should be unreachable.
5599 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005600}
5601
5602
5603void StackCheckStub::Generate(MacroAssembler* masm) {
5604 Label within_limit;
5605 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5606 __ ldr(ip, MemOperand(ip));
5607 __ cmp(sp, Operand(ip));
5608 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005609 // Do tail-call to runtime routine. Runtime routines expect at least one
5610 // argument, so give it a Smi.
5611 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005612 __ push(r0);
5613 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5614 __ bind(&within_limit);
5615
5616 __ StubReturn(1);
5617}
5618
5619
5620void UnarySubStub::Generate(MacroAssembler* masm) {
5621 Label undo;
5622 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005623 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005624
5625 // Enter runtime system if the value is not a smi.
5626 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005627 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005628
5629 // Enter runtime system if the value of the expression is zero
5630 // to make sure that we switch between 0 and -0.
5631 __ cmp(r0, Operand(0));
5632 __ b(eq, &slow);
5633
5634 // The value of the expression is a smi that is not zero. Try
5635 // optimistic subtraction '0 - value'.
5636 __ rsb(r1, r0, Operand(0), SetCC);
5637 __ b(vs, &slow);
5638
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005639 __ mov(r0, Operand(r1)); // Set r0 to result.
5640 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005641
5642 // Enter runtime system.
5643 __ bind(&slow);
5644 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005645 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005646 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5647
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005648 __ bind(&not_smi);
5649 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5650 __ b(ne, &slow);
5651 // r0 is a heap number. Get a new heap number in r1.
5652 if (overwrite_) {
5653 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5654 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5655 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5656 } else {
5657 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5658 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5659 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5660 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5661 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5662 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5663 __ mov(r0, Operand(r1));
5664 }
5665 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005666}
5667
5668
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005669void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005670 // r0 holds the exception.
5671
5672 // Adjust this code if not the case.
5673 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5674
5675 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005676 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5677 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005678
5679 // Restore the next handler and frame pointer, discard handler state.
5680 ASSERT(StackHandlerConstants::kNextOffset == 0);
5681 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005682 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005683 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5684 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5685
5686 // Before returning we restore the context from the frame pointer if
5687 // not NULL. The frame pointer is NULL in the exception handler of a
5688 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005689 __ cmp(fp, Operand(0));
5690 // Set cp to NULL if fp is NULL.
5691 __ mov(cp, Operand(0), LeaveCC, eq);
5692 // Restore cp otherwise.
5693 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005694#ifdef DEBUG
5695 if (FLAG_debug_code) {
5696 __ mov(lr, Operand(pc));
5697 }
5698#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005699 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005700 __ pop(pc);
5701}
5702
5703
5704void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005705 // Adjust this code if not the case.
5706 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5707
5708 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005709 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005710 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005711
5712 // Unwind the handlers until the ENTRY handler is found.
5713 Label loop, done;
5714 __ bind(&loop);
5715 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005716 const int kStateOffset = StackHandlerConstants::kStateOffset;
5717 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005718 __ cmp(r2, Operand(StackHandler::ENTRY));
5719 __ b(eq, &done);
5720 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005721 const int kNextOffset = StackHandlerConstants::kNextOffset;
5722 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005723 __ jmp(&loop);
5724 __ bind(&done);
5725
5726 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005727 ASSERT(StackHandlerConstants::kNextOffset == 0);
5728 __ pop(r0);
5729 __ str(r0, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005730
5731 // Set external caught exception to false.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005732 ExternalReference external_caught(Top::k_external_caught_exception_address);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005733 __ mov(r0, Operand(false));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005734 __ mov(r2, Operand(external_caught));
5735 __ str(r0, MemOperand(r2));
5736
5737 // Set pending exception and r0 to out of memory exception.
5738 Failure* out_of_memory = Failure::OutOfMemoryException();
5739 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5740 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5741 __ str(r0, MemOperand(r2));
5742
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005743 // Stack layout at this point. See also StackHandlerConstants.
5744 // sp -> state (ENTRY)
5745 // fp
5746 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005747
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005748 // Discard handler state (r2 is not used) and restore frame pointer.
5749 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5750 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5751 // Before returning we restore the context from the frame pointer if
5752 // not NULL. The frame pointer is NULL in the exception handler of a
5753 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005754 __ cmp(fp, Operand(0));
5755 // Set cp to NULL if fp is NULL.
5756 __ mov(cp, Operand(0), LeaveCC, eq);
5757 // Restore cp otherwise.
5758 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005759#ifdef DEBUG
5760 if (FLAG_debug_code) {
5761 __ mov(lr, Operand(pc));
5762 }
5763#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005764 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005765 __ pop(pc);
5766}
5767
5768
5769void CEntryStub::GenerateCore(MacroAssembler* masm,
5770 Label* throw_normal_exception,
5771 Label* throw_out_of_memory_exception,
5772 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005773 bool do_gc,
5774 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005775 // r0: result parameter for PerformGC, if any
5776 // r4: number of arguments including receiver (C callee-saved)
5777 // r5: pointer to builtin function (C callee-saved)
5778 // r6: pointer to the first argument (C callee-saved)
5779
5780 if (do_gc) {
5781 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005782 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5783 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005784 }
5785
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005786 ExternalReference scope_depth =
5787 ExternalReference::heap_always_allocate_scope_depth();
5788 if (always_allocate) {
5789 __ mov(r0, Operand(scope_depth));
5790 __ ldr(r1, MemOperand(r0));
5791 __ add(r1, r1, Operand(1));
5792 __ str(r1, MemOperand(r0));
5793 }
5794
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005795 // Call C built-in.
5796 // r0 = argc, r1 = argv
5797 __ mov(r0, Operand(r4));
5798 __ mov(r1, Operand(r6));
5799
5800 // TODO(1242173): To let the GC traverse the return address of the exit
5801 // frames, we need to know where the return address is. Right now,
5802 // we push it on the stack to be able to find it again, but we never
5803 // restore from it in case of changes, which makes it impossible to
5804 // support moving the C entry code stub. This should be fixed, but currently
5805 // this is OK because the CEntryStub gets generated so early in the V8 boot
5806 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005807 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5808 masm->push(lr);
5809 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005810
5811 if (always_allocate) {
5812 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5813 // though (contain the result).
5814 __ mov(r2, Operand(scope_depth));
5815 __ ldr(r3, MemOperand(r2));
5816 __ sub(r3, r3, Operand(1));
5817 __ str(r3, MemOperand(r2));
5818 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005819
5820 // check for failure result
5821 Label failure_returned;
5822 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5823 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5824 __ add(r2, r0, Operand(1));
5825 __ tst(r2, Operand(kFailureTagMask));
5826 __ b(eq, &failure_returned);
5827
5828 // Exit C frame and return.
5829 // r0:r1: result
5830 // sp: stack pointer
5831 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005832 __ LeaveExitFrame(frame_type);
5833
5834 // check if we should retry or throw exception
5835 Label retry;
5836 __ bind(&failure_returned);
5837 ASSERT(Failure::RETRY_AFTER_GC == 0);
5838 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5839 __ b(eq, &retry);
5840
5841 Label continue_exception;
5842 // If the returned failure is EXCEPTION then promote Top::pending_exception().
5843 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
5844 __ b(ne, &continue_exception);
5845
5846 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005847 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005848 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005849 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005850 __ ldr(r0, MemOperand(ip));
5851 __ str(r3, MemOperand(ip));
5852
5853 __ bind(&continue_exception);
5854 // Special handling of out of memory exception.
5855 Failure* out_of_memory = Failure::OutOfMemoryException();
5856 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5857 __ b(eq, throw_out_of_memory_exception);
5858
5859 // Handle normal exception.
5860 __ jmp(throw_normal_exception);
5861
5862 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5863}
5864
5865
5866void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5867 // Called from JavaScript; parameters are on stack as if calling JS function
5868 // r0: number of arguments including receiver
5869 // r1: pointer to builtin function
5870 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005871 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005872 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005873
5874 // NOTE: Invocations of builtins may return failure objects
5875 // instead of a proper result. The builtin entry handles
5876 // this by performing a garbage collection and retrying the
5877 // builtin once.
5878
5879 StackFrame::Type frame_type = is_debug_break
5880 ? StackFrame::EXIT_DEBUG
5881 : StackFrame::EXIT;
5882
5883 // Enter the exit frame that transitions from JavaScript to C++.
5884 __ EnterExitFrame(frame_type);
5885
5886 // r4: number of arguments (C callee-saved)
5887 // r5: pointer to builtin function (C callee-saved)
5888 // r6: pointer to first argument (C callee-saved)
5889
5890 Label throw_out_of_memory_exception;
5891 Label throw_normal_exception;
5892
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005893 // Call into the runtime system. Collect garbage before the call if
5894 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005895 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005896 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005897 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
5898 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005899 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005900 &throw_out_of_memory_exception,
5901 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005902 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005903 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005904
5905 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005906 GenerateCore(masm,
5907 &throw_normal_exception,
5908 &throw_out_of_memory_exception,
5909 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005910 true,
5911 false);
5912
5913 // Do full GC and retry runtime call one final time.
5914 Failure* failure = Failure::InternalError();
5915 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5916 GenerateCore(masm,
5917 &throw_normal_exception,
5918 &throw_out_of_memory_exception,
5919 frame_type,
5920 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005921 true);
5922
5923 __ bind(&throw_out_of_memory_exception);
5924 GenerateThrowOutOfMemory(masm);
5925 // control flow for generated will not return.
5926
5927 __ bind(&throw_normal_exception);
5928 GenerateThrowTOS(masm);
5929}
5930
5931
5932void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5933 // r0: code entry
5934 // r1: function
5935 // r2: receiver
5936 // r3: argc
5937 // [sp+0]: argv
5938
5939 Label invoke, exit;
5940
5941 // Called from C, so do not pop argc and args on exit (preserve sp)
5942 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005943 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005944 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5945
5946 // Get address of argv, see stm above.
5947 // r0: code entry
5948 // r1: function
5949 // r2: receiver
5950 // r3: argc
5951 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5952 __ ldr(r4, MemOperand(r4)); // argv
5953
5954 // Push a frame with special values setup to mark it as an entry frame.
5955 // r0: code entry
5956 // r1: function
5957 // r2: receiver
5958 // r3: argc
5959 // r4: argv
5960 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5961 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
5962 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
5963 __ mov(r6, Operand(Smi::FromInt(marker)));
5964 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5965 __ ldr(r5, MemOperand(r5));
5966 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5967
5968 // Setup frame pointer for the frame to be pushed.
5969 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5970
5971 // Call a faked try-block that does the invoke.
5972 __ bl(&invoke);
5973
5974 // Caught exception: Store result (exception) in the pending
5975 // exception field in the JSEnv and return a failure sentinel.
5976 // Coming in here the fp will be invalid because the PushTryHandler below
5977 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00005978 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005979 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005980 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005981 __ b(&exit);
5982
5983 // Invoke: Link this frame into the handler chain.
5984 __ bind(&invoke);
5985 // Must preserve r0-r4, r5-r7 are available.
5986 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005987 // If an exception not caught by another handler occurs, this handler
5988 // returns control to the code after the bl(&invoke) above, which
5989 // restores all kCalleeSaved registers (including cp and fp) to their
5990 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005991
5992 // Clear any pending exceptions.
5993 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5994 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005995 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005996 __ str(r5, MemOperand(ip));
5997
5998 // Invoke the function by calling through JS entry trampoline builtin.
5999 // Notice that we cannot store a reference to the trampoline code directly in
6000 // this stub, because runtime stubs are not traversed when doing GC.
6001
6002 // Expected registers by Builtins::JSEntryTrampoline
6003 // r0: code entry
6004 // r1: function
6005 // r2: receiver
6006 // r3: argc
6007 // r4: argv
6008 if (is_construct) {
6009 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6010 __ mov(ip, Operand(construct_entry));
6011 } else {
6012 ExternalReference entry(Builtins::JSEntryTrampoline);
6013 __ mov(ip, Operand(entry));
6014 }
6015 __ ldr(ip, MemOperand(ip)); // deref address
6016
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006017 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6018 // macro for the add instruction because we don't want the coverage tool
6019 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006020 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006021 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006022
6023 // Unlink this frame from the handler chain. When reading the
6024 // address of the next handler, there is no need to use the address
6025 // displacement since the current stack pointer (sp) points directly
6026 // to the stack handler.
6027 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6028 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6029 __ str(r3, MemOperand(ip));
6030 // No need to restore registers
6031 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6032
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006033
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006034 __ bind(&exit); // r0 holds result
6035 // Restore the top frame descriptors from the stack.
6036 __ pop(r3);
6037 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6038 __ str(r3, MemOperand(ip));
6039
6040 // Reset the stack to the callee saved registers.
6041 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6042
6043 // Restore callee-saved registers and return.
6044#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006045 if (FLAG_debug_code) {
6046 __ mov(lr, Operand(pc));
6047 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006048#endif
6049 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6050}
6051
6052
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006053// This stub performs an instanceof, calling the builtin function if
6054// necessary. Uses r1 for the object, r0 for the function that it may
6055// be an instance of (these are fetched from the stack).
6056void InstanceofStub::Generate(MacroAssembler* masm) {
6057 // Get the object - slow case for smis (we may need to throw an exception
6058 // depending on the rhs).
6059 Label slow, loop, is_instance, is_not_instance;
6060 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6061 __ BranchOnSmi(r0, &slow);
6062
6063 // Check that the left hand is a JS object and put map in r3.
6064 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6065 __ b(lt, &slow);
6066 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6067 __ b(gt, &slow);
6068
6069 // Get the prototype of the function (r4 is result, r2 is scratch).
6070 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6071 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6072
6073 // Check that the function prototype is a JS object.
6074 __ BranchOnSmi(r4, &slow);
6075 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6076 __ b(lt, &slow);
6077 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6078 __ b(gt, &slow);
6079
6080 // Register mapping: r3 is object map and r4 is function prototype.
6081 // Get prototype of object into r2.
6082 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6083
6084 // Loop through the prototype chain looking for the function prototype.
6085 __ bind(&loop);
6086 __ cmp(r2, Operand(r4));
6087 __ b(eq, &is_instance);
6088 __ cmp(r2, Operand(Factory::null_value()));
6089 __ b(eq, &is_not_instance);
6090 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6091 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6092 __ jmp(&loop);
6093
6094 __ bind(&is_instance);
6095 __ mov(r0, Operand(Smi::FromInt(0)));
6096 __ pop();
6097 __ pop();
6098 __ mov(pc, Operand(lr)); // Return.
6099
6100 __ bind(&is_not_instance);
6101 __ mov(r0, Operand(Smi::FromInt(1)));
6102 __ pop();
6103 __ pop();
6104 __ mov(pc, Operand(lr)); // Return.
6105
6106 // Slow-case. Tail call builtin.
6107 __ bind(&slow);
6108 __ mov(r0, Operand(1)); // Arg count without receiver.
6109 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6110}
6111
6112
ager@chromium.org7c537e22008-10-16 08:43:32 +00006113void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006114 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006115 Label adaptor;
6116 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6117 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6118 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006119 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006120
ager@chromium.org7c537e22008-10-16 08:43:32 +00006121 // Nothing to do: The formal number of parameters has already been
6122 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006123 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006124
ager@chromium.org7c537e22008-10-16 08:43:32 +00006125 // Arguments adaptor case: Read the arguments length from the
6126 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006127 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006128 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006129 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006130}
6131
6132
ager@chromium.org7c537e22008-10-16 08:43:32 +00006133void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6134 // The displacement is the offset of the last parameter (if any)
6135 // relative to the frame pointer.
6136 static const int kDisplacement =
6137 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006138
ager@chromium.org7c537e22008-10-16 08:43:32 +00006139 // Check that the key is a smi.
6140 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006141 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006142
ager@chromium.org7c537e22008-10-16 08:43:32 +00006143 // Check if the calling frame is an arguments adaptor frame.
6144 Label adaptor;
6145 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6146 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6147 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6148 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006149
ager@chromium.org7c537e22008-10-16 08:43:32 +00006150 // Check index against formal parameters count limit passed in
6151 // through register eax. Use unsigned comparison to get negative
6152 // check for free.
6153 __ cmp(r1, r0);
6154 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155
ager@chromium.org7c537e22008-10-16 08:43:32 +00006156 // Read the argument from the stack and return it.
6157 __ sub(r3, r0, r1);
6158 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6159 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006160 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006161
6162 // Arguments adaptor case: Check index against actual arguments
6163 // limit found in the arguments adaptor frame. Use unsigned
6164 // comparison to get negative check for free.
6165 __ bind(&adaptor);
6166 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6167 __ cmp(r1, r0);
6168 __ b(cs, &slow);
6169
6170 // Read the argument from the adaptor frame and return it.
6171 __ sub(r3, r0, r1);
6172 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6173 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006174 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006175
6176 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6177 // by calling the runtime system.
6178 __ bind(&slow);
6179 __ push(r1);
6180 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
6181}
6182
6183
6184void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6185 // Check if the calling frame is an arguments adaptor frame.
6186 Label runtime;
6187 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6188 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6189 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6190 __ b(ne, &runtime);
6191
6192 // Patch the arguments.length and the parameters pointer.
6193 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6194 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6195 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6196 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6197 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6198
6199 // Do the runtime call to allocate the arguments object.
6200 __ bind(&runtime);
6201 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006202}
6203
6204
6205void CallFunctionStub::Generate(MacroAssembler* masm) {
6206 Label slow;
6207 // Get the function to call from the stack.
6208 // function, receiver [, arguments]
6209 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6210
6211 // Check that the function is really a JavaScript function.
6212 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006213 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006214 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006215 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006216 __ b(ne, &slow);
6217
6218 // Fast-case: Invoke the function now.
6219 // r1: pushed function
6220 ParameterCount actual(argc_);
6221 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6222
6223 // Slow-case: Non-function called.
6224 __ bind(&slow);
6225 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006226 __ mov(r2, Operand(0));
6227 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6228 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6229 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006230}
6231
6232
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006233int CompareStub::MinorKey() {
6234 // Encode the two parameters in a unique 16 bit value.
6235 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6236 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6237}
6238
6239
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006240#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006241
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006242} } // namespace v8::internal