blob: 8c28b24347b221575cdc12aeb402b1d69be8224f [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
ager@chromium.orge2902be2009-06-08 12:21:35 +000044// -------------------------------------------------------------------------
45// Platform-specific DeferredCode functions.
46
47void DeferredCode::SaveRegisters() {
48 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
49 int action = registers_[i];
50 if (action == kPush) {
51 __ push(RegisterAllocator::ToRegister(i));
52 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
53 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
54 }
55 }
56}
57
58
59void DeferredCode::RestoreRegisters() {
60 // Restore registers in reverse order due to the stack.
61 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
62 int action = registers_[i];
63 if (action == kPush) {
64 __ pop(RegisterAllocator::ToRegister(i));
65 } else if (action != kIgnore) {
66 action &= ~kSyncedFlag;
67 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
68 }
69 }
70}
71
ager@chromium.org3bf7b912008-11-17 09:09:45 +000072
73// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000074// CodeGenState implementation.
75
ager@chromium.org7c537e22008-10-16 08:43:32 +000076CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000077 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000078 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000079 true_target_(NULL),
80 false_target_(NULL),
81 previous_(NULL) {
82 owner_->set_state(this);
83}
84
85
ager@chromium.org7c537e22008-10-16 08:43:32 +000086CodeGenState::CodeGenState(CodeGenerator* owner,
87 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000088 JumpTarget* true_target,
89 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000090 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000091 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000092 true_target_(true_target),
93 false_target_(false_target),
94 previous_(owner->state()) {
95 owner_->set_state(this);
96}
97
98
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000099CodeGenState::~CodeGenState() {
100 ASSERT(owner_->state() == this);
101 owner_->set_state(previous_);
102}
103
104
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000105// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000106// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
109 bool is_eval)
110 : is_eval_(is_eval),
111 script_(script),
112 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 masm_(new MacroAssembler(NULL, buffer_size)),
114 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000115 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000116 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 cc_reg_(al),
118 state_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000119 function_return_is_shadowed_(false),
120 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121}
122
123
124// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000125// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000127// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128// cp: callee's context
129
ager@chromium.org7c537e22008-10-16 08:43:32 +0000130void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 ZoneList<Statement*>* body = fun->body();
132
133 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000134 ASSERT(scope_ == NULL);
135 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000136 ASSERT(allocator_ == NULL);
137 RegisterAllocator register_allocator(this);
138 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000139 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000140 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000141 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000143 {
144 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000146 // Entry:
147 // Stack: receiver, arguments
148 // lr: return address
149 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000151 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000153 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000154 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 // tos: code slot
156#ifdef DEBUG
157 if (strlen(FLAG_stop_at) > 0 &&
158 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000159 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000160 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 }
162#endif
163
164 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000165 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 // Initialize the function return target after the locals are set
167 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000168 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000169 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000171 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000172 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 // Allocate local context.
174 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000175 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176 frame_->EmitPush(r0);
177 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000178
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000179#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000181 __ cmp(r0, Operand(cp));
182 verified_true.Branch(eq);
183 __ stop("NewContext: r0 is expected to be the same as cp");
184 verified_true.Bind();
185#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000187 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 }
189
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000190 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191 // 1) only needed if we have a context
192 // 2) no need to recompute context ptr every single time
193 // 3) don't copy parameter operand code from SlotOperand!
194 {
195 Comment cmnt2(masm_, "[ copy context parameters into .context");
196
197 // Note that iteration order is relevant here! If we have the same
198 // parameter twice (e.g., function (x, y, x)), and that parameter
199 // needs to be copied into the context, it must be the last argument
200 // passed to the parameter that needs to be copied. This is a rare
201 // case so we don't check for it, instead we rely on the copying
202 // order: such a parameter is copied repeatedly into the same
203 // context location and thus the last value is what is seen inside
204 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 for (int i = 0; i < scope_->num_parameters(); i++) {
206 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 Slot* slot = par->slot();
208 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000209 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000210 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 // Loads r2 with context; used below in RecordWrite.
212 __ str(r1, SlotOperand(slot, r2));
213 // Load the offset into r3.
214 int slot_offset =
215 FixedArray::kHeaderSize + slot->index() * kPointerSize;
216 __ mov(r3, Operand(slot_offset));
217 __ RecordWrite(r2, r3, r1);
218 }
219 }
220 }
221
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000222 // Store the arguments object. This must happen after context
223 // initialization because the arguments object may be stored in the
224 // context.
225 if (scope_->arguments() != NULL) {
226 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000228 { Reference shadow_ref(this, scope_->arguments_shadow());
229 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000230 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000231 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000232 // The receiver is below the arguments, the return address,
233 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000234 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000235 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000236 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000237 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000238 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000239 frame_->CallStub(&stub, 3);
240 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000241 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000242 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000243 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000245 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 }
247
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000248 // Generate code to 'execute' declarations and initialize functions
249 // (source elements). In case of an illegal redeclaration we need to
250 // handle that instead of processing the declarations.
251 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000253 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 } else {
255 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000256 ProcessDeclarations(scope_->declarations());
257 // Bail out if a stack-overflow exception occurred when processing
258 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000259 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 }
261
mads.s.ager31e71382008-08-13 09:32:07 +0000262 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000263 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000264 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000265 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 CheckStack();
267
268 // Compile the body of the function in a vanilla state. Don't
269 // bother compiling all the code if the scope has an illegal
270 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000271 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 Comment cmnt(masm_, "[ function body");
273#ifdef DEBUG
274 bool is_builtin = Bootstrapper::IsActive();
275 bool should_trace =
276 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000277 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000278 frame_->CallRuntime(Runtime::kDebugTrace, 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#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000282 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 }
285
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000286 // Generate the return sequence if necessary.
287 if (frame_ != NULL || function_return_.is_linked()) {
288 // exit
289 // r0: result
290 // sp: stack pointer
291 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000292 // cp: callee's context
293 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 function_return_.Bind();
296 if (FLAG_trace) {
297 // Push the return value on the stack as the parameter.
298 // Runtime::TraceExit returns the parameter as it is.
299 frame_->EmitPush(r0);
300 frame_->CallRuntime(Runtime::kTraceExit, 1);
301 }
302
303 // Tear down the frame which will restore the caller's frame pointer and
304 // the link register.
305 frame_->Exit();
306
307 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000308 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000309 }
310
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 ASSERT(!has_cc());
313 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 ASSERT(!function_return_is_shadowed_);
315 function_return_.Unuse();
316 DeleteFrame();
317
318 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000319 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000320 ProcessDeferred();
321 }
322
323 allocator_ = NULL;
324 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325}
326
327
ager@chromium.org7c537e22008-10-16 08:43:32 +0000328MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
329 // Currently, this assertion will fail if we try to assign to
330 // a constant variable that is constant because it is read-only
331 // (such as the variable referring to a named function expression).
332 // We need to implement assignments to read-only variables.
333 // Ideally, we should do this during AST generation (by converting
334 // such assignments into expression statements); however, in general
335 // we may not be able to make the decision until past AST generation,
336 // that is when the entire program is known.
337 ASSERT(slot != NULL);
338 int index = slot->index();
339 switch (slot->type()) {
340 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000342
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000343 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000344 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000345
346 case Slot::CONTEXT: {
347 // Follow the context chain if necessary.
348 ASSERT(!tmp.is(cp)); // do not overwrite context register
349 Register context = cp;
350 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000351 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000352 // Load the closure.
353 // (All contexts, even 'with' contexts, have a closure,
354 // and it is the same for all contexts inside a function.
355 // There is no need to go to the function context first.)
356 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
357 // Load the function context (which is the incoming, outer context).
358 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
359 context = tmp;
360 }
361 // We may have a 'with' context now. Get the function context.
362 // (In fact this mov may never be the needed, since the scope analysis
363 // may not permit a direct context access in this case and thus we are
364 // always at a function context. However it is safe to dereference be-
365 // cause the function context of a function context is itself. Before
366 // deleting this mov we should try to create a counter-example first,
367 // though...)
368 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
369 return ContextOperand(tmp, index);
370 }
371
372 default:
373 UNREACHABLE();
374 return MemOperand(r0, 0);
375 }
376}
377
378
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000379MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
380 Slot* slot,
381 Register tmp,
382 Register tmp2,
383 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000384 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000385 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000386
ager@chromium.org381abbb2009-02-25 13:23:22 +0000387 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
388 if (s->num_heap_slots() > 0) {
389 if (s->calls_eval()) {
390 // Check that extension is NULL.
391 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
392 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000393 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000394 }
395 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
396 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
397 context = tmp;
398 }
399 }
400 // Check that last extension is NULL.
401 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
402 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000403 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000404 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000405 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000406}
407
408
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000409void CodeGenerator::LoadConditionAndSpill(Expression* expression,
410 TypeofState typeof_state,
411 JumpTarget* true_target,
412 JumpTarget* false_target,
413 bool force_control) {
414 ASSERT(in_spilled_code());
415 set_in_spilled_code(false);
416 LoadCondition(expression, typeof_state, true_target, false_target,
417 force_control);
418 if (frame_ != NULL) {
419 frame_->SpillAll();
420 }
421 set_in_spilled_code(true);
422}
423
424
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000425// Loads a value on TOS. If it is a boolean value, the result may have been
426// (partially) translated into branches, or it may have set the condition
427// code register. If force_cc is set, the value is forced to set the
428// condition code register and no value is pushed. If the condition code
429// register was set, has_cc() is true and cc_reg_ contains the condition to
430// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000431void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000432 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000433 JumpTarget* true_target,
434 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000435 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000437 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000438 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
ager@chromium.org7c537e22008-10-16 08:43:32 +0000440 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000441 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000442
443 // If we hit a stack overflow, we may not have actually visited
444 // the expression. In that case, we ensure that we have a
445 // valid-looking frame state because we will continue to generate
446 // code as we unwind the C++ stack.
447 //
448 // It's possible to have both a stack overflow and a valid frame
449 // state (eg, a subexpression overflowed, visiting it returned
450 // with a dummied frame state, and visiting this expression
451 // returned with a normal-looking state).
452 if (HasStackOverflow() &&
453 has_valid_frame() &&
454 !has_cc() &&
455 frame_->height() == original_height) {
456 true_target->Jump();
457 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000458 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000459 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000460 // Convert the TOS value to a boolean in the condition code register.
461 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000463 ASSERT(!force_cc || !has_valid_frame() || has_cc());
464 ASSERT(!has_valid_frame() ||
465 (has_cc() && frame_->height() == original_height) ||
466 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467}
468
469
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000470void CodeGenerator::LoadAndSpill(Expression* expression,
471 TypeofState typeof_state) {
472 ASSERT(in_spilled_code());
473 set_in_spilled_code(false);
474 Load(expression, typeof_state);
475 frame_->SpillAll();
476 set_in_spilled_code(true);
477}
478
479
ager@chromium.org7c537e22008-10-16 08:43:32 +0000480void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000481#ifdef DEBUG
482 int original_height = frame_->height();
483#endif
484 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000485 JumpTarget true_target;
486 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000487 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
489 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000491 JumpTarget loaded;
492 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000493 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000494 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495 frame_->EmitPush(r0);
496 loaded.Jump();
497 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000498 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 frame_->EmitPush(r0);
500 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 cc_reg_ = al;
502 }
503
504 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505 // We have at least one condition value that has been "translated"
506 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000507 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000508 if (frame_ != NULL) {
509 loaded.Jump(); // Don't lose the current TOS.
510 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000512 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000515 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000518 // If both "true" and "false" need to be loaded jump across the code for
519 // "false".
520 if (both) {
521 loaded.Jump();
522 }
523 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000525 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000526 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000529 // A value is loaded on all paths reaching this point.
530 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535}
536
537
ager@chromium.org7c537e22008-10-16 08:43:32 +0000538void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000539 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000540 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542}
543
544
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000545void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000546 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000547 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
548 __ ldr(scratch,
549 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000551}
552
553
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000555// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
556// variables w/o reference errors elsewhere.
557void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000558 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 Variable* variable = x->AsVariableProxy()->AsVariable();
560 if (variable != NULL && !variable->is_this() && variable->is_global()) {
561 // NOTE: This is somewhat nasty. We force the compiler to load
562 // the variable as if through '<global>.<variable>' to make sure we
563 // do not get reference errors.
564 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
565 Literal key(variable->name());
566 // TODO(1241834): Fetch the position from the variable instead of using
567 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000568 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 }
573}
574
575
ager@chromium.org7c537e22008-10-16 08:43:32 +0000576Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
578 cgen->LoadReference(this);
579}
580
581
582Reference::~Reference() {
583 cgen_->UnloadReference(this);
584}
585
586
ager@chromium.org7c537e22008-10-16 08:43:32 +0000587void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000588 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000589 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 Expression* e = ref->expression();
591 Property* property = e->AsProperty();
592 Variable* var = e->AsVariableProxy()->AsVariable();
593
594 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000595 // The expression is either a property or a variable proxy that rewrites
596 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000597 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000598 // We use a named reference if the key is a literal symbol, unless it is
599 // a string that can be legally parsed as an integer. This is because
600 // otherwise we will not get into the slow case code that handles [] on
601 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 Literal* literal = property->key()->AsLiteral();
603 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000604 if (literal != NULL &&
605 literal->handle()->IsSymbol() &&
606 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 ref->set_type(Reference::NAMED);
608 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000609 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 ref->set_type(Reference::KEYED);
611 }
612 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000613 // The expression is a variable proxy that does not rewrite to a
614 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 LoadGlobal();
617 ref->set_type(Reference::NAMED);
618 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000619 ASSERT(var->slot() != NULL);
620 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 }
622 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000623 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000624 LoadAndSpill(e);
625 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 }
627}
628
629
ager@chromium.org7c537e22008-10-16 08:43:32 +0000630void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000631 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000632 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000633 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000635 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000636 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000637 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000638 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 }
640}
641
642
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
644// register to a boolean in the condition code register. The code
645// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000646void CodeGenerator::ToBoolean(JumpTarget* true_target,
647 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000648 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000649 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000651 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652
653 // Fast case checks
654
mads.s.ager31e71382008-08-13 09:32:07 +0000655 // Check if the value is 'false'.
656 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000657 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658
mads.s.ager31e71382008-08-13 09:32:07 +0000659 // Check if the value is 'true'.
660 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000661 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
mads.s.ager31e71382008-08-13 09:32:07 +0000663 // Check if the value is 'undefined'.
664 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000665 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666
mads.s.ager31e71382008-08-13 09:32:07 +0000667 // Check if the value is a smi.
668 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000670 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000671 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
673 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000674 frame_->EmitPush(r0);
675 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000676 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 cc_reg_ = ne;
680}
681
682
kasper.lund7276f142008-07-30 08:49:36 +0000683class GenericBinaryOpStub : public CodeStub {
684 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000685 GenericBinaryOpStub(Token::Value op,
686 OverwriteMode mode)
687 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000688
689 private:
690 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000691 OverwriteMode mode_;
692
693 // Minor key encoding in 16 bits.
694 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
695 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000696
697 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000698 int MinorKey() {
699 // Encode the parameters in a unique 16 bit value.
700 return OpBits::encode(op_)
701 | ModeBits::encode(mode_);
702 }
703
kasper.lund7276f142008-07-30 08:49:36 +0000704 void Generate(MacroAssembler* masm);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000705 void HandleNonSmiBitwiseOp(MacroAssembler* masm);
kasper.lund7276f142008-07-30 08:49:36 +0000706
707 const char* GetName() {
708 switch (op_) {
709 case Token::ADD: return "GenericBinaryOpStub_ADD";
710 case Token::SUB: return "GenericBinaryOpStub_SUB";
711 case Token::MUL: return "GenericBinaryOpStub_MUL";
712 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000713 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
714 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
715 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
716 case Token::SAR: return "GenericBinaryOpStub_SAR";
717 case Token::SHL: return "GenericBinaryOpStub_SHL";
718 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000719 default: return "GenericBinaryOpStub";
720 }
721 }
722
723#ifdef DEBUG
724 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
725#endif
726};
727
728
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000729void CodeGenerator::GenericBinaryOperation(Token::Value op,
730 OverwriteMode overwrite_mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000731 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000732 // sp[0] : y
733 // sp[1] : x
734 // result : r0
735
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 // Stub is entered with a call: 'return address' is in lr.
737 switch (op) {
738 case Token::ADD: // fall through.
739 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000740 case Token::MUL:
741 case Token::BIT_OR:
742 case Token::BIT_AND:
743 case Token::BIT_XOR:
744 case Token::SHL:
745 case Token::SHR:
746 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000747 frame_->EmitPop(r0); // r0 : y
748 frame_->EmitPop(r1); // r1 : x
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000749 GenericBinaryOpStub stub(op, overwrite_mode);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000750 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751 break;
752 }
753
754 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755 Result arg_count = allocator_->Allocate(r0);
756 ASSERT(arg_count.is_valid());
757 __ mov(arg_count.reg(), Operand(1));
758 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 break;
760 }
761
762 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000763 Result arg_count = allocator_->Allocate(r0);
764 ASSERT(arg_count.is_valid());
765 __ mov(arg_count.reg(), Operand(1));
766 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 break;
768 }
769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000771 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000773 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 break;
775
776 default:
777 // Other cases should have been handled before this point.
778 UNREACHABLE();
779 break;
780 }
781}
782
783
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000784class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000785 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000786 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000787 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000788 bool reversed,
789 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000790 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000791 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000792 reversed_(reversed),
793 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000794 set_comment("[ DeferredInlinedSmiOperation");
795 }
796
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000797 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000798
799 private:
800 Token::Value op_;
801 int value_;
802 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000803 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000804};
805
806
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000807void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000808 switch (op_) {
809 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000810 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000811 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000812 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
813 __ mov(r1, Operand(Smi::FromInt(value_)));
814 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000815 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
816 __ mov(r0, Operand(Smi::FromInt(value_)));
817 }
818 break;
819 }
820
821 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000822 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000823 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000824 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
825 __ mov(r1, Operand(Smi::FromInt(value_)));
826 } else {
827 __ add(r1, r0, Operand(Smi::FromInt(value_)));
828 __ mov(r0, Operand(Smi::FromInt(value_)));
829 }
830 break;
831 }
832
833 case Token::BIT_OR:
834 case Token::BIT_XOR:
835 case Token::BIT_AND: {
836 if (reversed_) {
837 __ mov(r1, Operand(Smi::FromInt(value_)));
838 } else {
839 __ mov(r1, Operand(r0));
840 __ mov(r0, Operand(Smi::FromInt(value_)));
841 }
842 break;
843 }
844
845 case Token::SHL:
846 case Token::SHR:
847 case Token::SAR: {
848 if (!reversed_) {
849 __ mov(r1, Operand(r0));
850 __ mov(r0, Operand(Smi::FromInt(value_)));
851 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000852 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000853 }
854 break;
855 }
856
857 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000858 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000859 UNREACHABLE();
860 break;
861 }
862
ager@chromium.orge2902be2009-06-08 12:21:35 +0000863 GenericBinaryOpStub stub(op_, overwrite_mode_);
864 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000865}
866
867
ager@chromium.org7c537e22008-10-16 08:43:32 +0000868void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000869 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000870 bool reversed,
871 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000872 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 // NOTE: This is an attempt to inline (a bit) more of the code for
874 // some possible smi operations (like + and -) when (at least) one
875 // of the operands is a literal smi. With this optimization, the
876 // performance of the system is increased by ~15%, and the generated
877 // code size is increased by ~1% (measured on a combination of
878 // different benchmarks).
879
mads.s.ager31e71382008-08-13 09:32:07 +0000880 // sp[0] : operand
881
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000884 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000885 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
887 switch (op) {
888 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000890 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000893 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000895 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000896 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 break;
898 }
899
900 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000901 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000902 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000906 } else {
907 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000909 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000910 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000911 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000912 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000913 break;
914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000916 case Token::BIT_OR:
917 case Token::BIT_XOR:
918 case Token::BIT_AND: {
919 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000920 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000921 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000922 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000923 switch (op) {
924 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
925 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
926 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
927 default: UNREACHABLE();
928 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000929 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 break;
931 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000933 case Token::SHL:
934 case Token::SHR:
935 case Token::SAR: {
936 if (reversed) {
937 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000938 frame_->EmitPush(ip);
939 frame_->EmitPush(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000940 GenericBinaryOperation(op, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000941
942 } else {
943 int shift_value = int_value & 0x1f; // least significant 5 bits
944 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000945 new DeferredInlineSmiOperation(op, shift_value, false, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000946 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000947 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000948 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
949 switch (op) {
950 case Token::SHL: {
951 __ mov(r2, Operand(r2, LSL, shift_value));
952 // check that the *unsigned* result fits in a smi
953 __ add(r3, r2, Operand(0x40000000), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000954 deferred->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000955 break;
956 }
957 case Token::SHR: {
958 // LSR by immediate 0 means shifting 32 bits.
959 if (shift_value != 0) {
960 __ mov(r2, Operand(r2, LSR, shift_value));
961 }
962 // check that the *unsigned* result fits in a smi
963 // neither of the two high-order bits can be set:
964 // - 0x80000000: high bit would be lost when smi tagging
965 // - 0x40000000: this number would convert to negative when
966 // smi tagging these two cases can only happen with shifts
967 // by 0 or 1 when handed a valid smi
968 __ and_(r3, r2, Operand(0xc0000000), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000969 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000970 break;
971 }
972 case Token::SAR: {
973 if (shift_value != 0) {
974 // ASR by immediate 0 means shifting 32 bits.
975 __ mov(r2, Operand(r2, ASR, shift_value));
976 }
977 break;
978 }
979 default: UNREACHABLE();
980 }
981 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000982 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000983 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 break;
985 }
986
987 default:
988 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000989 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000990 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000991 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 } else {
993 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000994 frame_->EmitPush(ip);
995 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000997 GenericBinaryOperation(op, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 break;
999 }
1000
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001001 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002}
1003
1004
ager@chromium.org7c537e22008-10-16 08:43:32 +00001005void CodeGenerator::Comparison(Condition cc, bool strict) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001006 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001007 // sp[0] : y
1008 // sp[1] : x
1009 // result : cc register
1010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 // Strict only makes sense for equality comparisons.
1012 ASSERT(!strict || cc == eq);
1013
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001014 JumpTarget exit;
1015 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001016 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1017 if (cc == gt || cc == le) {
1018 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001019 frame_->EmitPop(r1);
1020 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001021 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001022 frame_->EmitPop(r0);
1023 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001024 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 __ orr(r2, r0, Operand(r1));
1026 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001027 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001028
1029 // Perform non-smi comparison by runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001030 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031
1032 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001033 Builtins::JavaScript native;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001034 int arg_count = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001036 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001038 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 int ncr; // NaN compare result
1040 if (cc == lt || cc == le) {
1041 ncr = GREATER;
1042 } else {
1043 ASSERT(cc == gt || cc == ge); // remaining cases
1044 ncr = LESS;
1045 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001046 frame_->EmitPush(r0);
1047 arg_count++;
mads.s.ager31e71382008-08-13 09:32:07 +00001048 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 }
1050
1051 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1052 // tagged as a small integer.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001053 frame_->EmitPush(r0);
1054 Result arg_count_register = allocator_->Allocate(r0);
1055 ASSERT(arg_count_register.is_valid());
1056 __ mov(arg_count_register.reg(), Operand(arg_count));
1057 Result result = frame_->InvokeBuiltin(native,
1058 CALL_JS,
1059 &arg_count_register,
1060 arg_count + 1);
1061 __ cmp(result.reg(), Operand(0));
1062 result.Unuse();
1063 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064
1065 // test smi equality by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001066 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 __ cmp(r1, Operand(r0));
1068
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001069 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 cc_reg_ = cc;
1071}
1072
1073
kasper.lund7276f142008-07-30 08:49:36 +00001074class CallFunctionStub: public CodeStub {
1075 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001076 CallFunctionStub(int argc, InLoopFlag in_loop)
1077 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001078
1079 void Generate(MacroAssembler* masm);
1080
1081 private:
1082 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001083 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001084
kasper.lund7276f142008-07-30 08:49:36 +00001085#if defined(DEBUG)
1086 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1087#endif // defined(DEBUG)
1088
1089 Major MajorKey() { return CallFunction; }
1090 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001091 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001092};
1093
1094
mads.s.ager31e71382008-08-13 09:32:07 +00001095// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001096void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001098 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 int arg_count = args->length();
1101 for (int i = 0; i < arg_count; i++) {
1102 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001103 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104
kasper.lund7276f142008-07-30 08:49:36 +00001105 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001106 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107
kasper.lund7276f142008-07-30 08:49:36 +00001108 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001109 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1110 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001111 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112
1113 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001114 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001115 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116}
1117
1118
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001119void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001120 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 ASSERT(has_cc());
1122 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001123 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 cc_reg_ = al;
1125}
1126
1127
ager@chromium.org7c537e22008-10-16 08:43:32 +00001128void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001129 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 if (FLAG_check_stack) {
1131 Comment cmnt(masm_, "[ check stack");
1132 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001133 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134 }
1135}
1136
1137
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001138void CodeGenerator::VisitAndSpill(Statement* statement) {
1139 ASSERT(in_spilled_code());
1140 set_in_spilled_code(false);
1141 Visit(statement);
1142 if (frame_ != NULL) {
1143 frame_->SpillAll();
1144 }
1145 set_in_spilled_code(true);
1146}
1147
1148
1149void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1150 ASSERT(in_spilled_code());
1151 set_in_spilled_code(false);
1152 VisitStatements(statements);
1153 if (frame_ != NULL) {
1154 frame_->SpillAll();
1155 }
1156 set_in_spilled_code(true);
1157}
1158
1159
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001160void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1161#ifdef DEBUG
1162 int original_height = frame_->height();
1163#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001164 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001165 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1166 VisitAndSpill(statements->at(i));
1167 }
1168 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1169}
1170
1171
ager@chromium.org7c537e22008-10-16 08:43:32 +00001172void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001173#ifdef DEBUG
1174 int original_height = frame_->height();
1175#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001176 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001178 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001179 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001180 VisitStatementsAndSpill(node->statements());
1181 if (node->break_target()->is_linked()) {
1182 node->break_target()->Bind();
1183 }
1184 node->break_target()->Unuse();
1185 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186}
1187
1188
ager@chromium.org7c537e22008-10-16 08:43:32 +00001189void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001190 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001191 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001192 frame_->EmitPush(r0);
1193 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001194 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001195 frame_->EmitPush(r0);
1196 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001197 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198}
1199
1200
ager@chromium.org7c537e22008-10-16 08:43:32 +00001201void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202#ifdef DEBUG
1203 int original_height = frame_->height();
1204#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001205 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001207 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 Variable* var = node->proxy()->var();
1209 ASSERT(var != NULL); // must have been resolved
1210 Slot* slot = var->slot();
1211
1212 // If it was not possible to allocate the variable at compile time,
1213 // we need to "declare" it at runtime to make sure it actually
1214 // exists in the local context.
1215 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1216 // Variables with a "LOOKUP" slot were introduced as non-locals
1217 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001218 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001221 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 // Declaration nodes are always declared in only two modes.
1224 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1225 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001226 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001227 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 // Push initial value, if any.
1229 // Note: For variables we must not push an initial value (such as
1230 // 'undefined') because we may have a (legal) redeclaration and we
1231 // must not destroy the current value.
1232 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001233 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001234 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001236 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001238 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001239 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001242 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001243 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 return;
1245 }
1246
1247 ASSERT(!var->is_global());
1248
1249 // If we have a function or a constant, we need to initialize the variable.
1250 Expression* val = NULL;
1251 if (node->mode() == Variable::CONST) {
1252 val = new Literal(Factory::the_hole_value());
1253 } else {
1254 val = node->fun(); // NULL if we don't have a function
1255 }
1256
1257 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001258 {
1259 // Set initial value.
1260 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001261 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001262 target.SetValue(NOT_CONST_INIT);
1263 // The reference is removed from the stack (preserving TOS) when
1264 // it goes out of scope.
1265 }
1266 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270}
1271
1272
ager@chromium.org7c537e22008-10-16 08:43:32 +00001273void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274#ifdef DEBUG
1275 int original_height = frame_->height();
1276#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001277 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 Expression* expression = node->expression();
1281 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001282 LoadAndSpill(expression);
1283 frame_->Drop();
1284 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285}
1286
1287
ager@chromium.org7c537e22008-10-16 08:43:32 +00001288void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289#ifdef DEBUG
1290 int original_height = frame_->height();
1291#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001292 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001294 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297}
1298
1299
ager@chromium.org7c537e22008-10-16 08:43:32 +00001300void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001301#ifdef DEBUG
1302 int original_height = frame_->height();
1303#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001304 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 // Generate different code depending on which parts of the if statement
1307 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 bool has_then_stm = node->HasThenStatement();
1309 bool has_else_stm = node->HasElseStatement();
1310
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001311 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001313 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001315 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001316 JumpTarget then;
1317 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001319 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1320 &then, &else_, true);
1321 if (frame_ != NULL) {
1322 Branch(false, &else_);
1323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001325 if (frame_ != NULL || then.is_linked()) {
1326 then.Bind();
1327 VisitAndSpill(node->then_statement());
1328 }
1329 if (frame_ != NULL) {
1330 exit.Jump();
1331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001333 if (else_.is_linked()) {
1334 else_.Bind();
1335 VisitAndSpill(node->else_statement());
1336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337
1338 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001339 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001341 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1344 &then, &exit, true);
1345 if (frame_ != NULL) {
1346 Branch(false, &exit);
1347 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001349 if (frame_ != NULL || then.is_linked()) {
1350 then.Bind();
1351 VisitAndSpill(node->then_statement());
1352 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353
1354 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001355 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001357 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1360 &exit, &else_, true);
1361 if (frame_ != NULL) {
1362 Branch(true, &exit);
1363 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001365 if (frame_ != NULL || else_.is_linked()) {
1366 else_.Bind();
1367 VisitAndSpill(node->else_statement());
1368 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369
1370 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001371 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 ASSERT(!has_then_stm && !has_else_stm);
1373 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001374 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1375 &exit, &exit, false);
1376 if (frame_ != NULL) {
1377 if (has_cc()) {
1378 cc_reg_ = al;
1379 } else {
1380 frame_->Drop();
1381 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 }
1383 }
1384
1385 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001386 if (exit.is_linked()) {
1387 exit.Bind();
1388 }
1389 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390}
1391
1392
ager@chromium.org7c537e22008-10-16 08:43:32 +00001393void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001394 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001396 CodeForStatementPosition(node);
1397 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398}
1399
1400
ager@chromium.org7c537e22008-10-16 08:43:32 +00001401void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001402 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001404 CodeForStatementPosition(node);
1405 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406}
1407
1408
ager@chromium.org7c537e22008-10-16 08:43:32 +00001409void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001410 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001412
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001413 if (function_return_is_shadowed_) {
1414 CodeForStatementPosition(node);
1415 LoadAndSpill(node->expression());
1416 frame_->EmitPop(r0);
1417 function_return_.Jump();
1418 } else {
1419 // Load the returned value.
1420 CodeForStatementPosition(node);
1421 LoadAndSpill(node->expression());
1422
1423 // Pop the result from the frame and prepare the frame for
1424 // returning thus making it easier to merge.
1425 frame_->EmitPop(r0);
1426 frame_->PrepareForReturn();
1427
1428 function_return_.Jump();
1429 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430}
1431
1432
ager@chromium.org7c537e22008-10-16 08:43:32 +00001433void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434#ifdef DEBUG
1435 int original_height = frame_->height();
1436#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001437 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001439 CodeForStatementPosition(node);
1440 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001441 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001442 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001443 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001444 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001445 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001446#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001447 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001448 __ cmp(r0, Operand(cp));
1449 verified_true.Branch(eq);
1450 __ stop("PushContext: r0 is expected to be the same as cp");
1451 verified_true.Bind();
1452#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001454 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456}
1457
1458
ager@chromium.org7c537e22008-10-16 08:43:32 +00001459void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460#ifdef DEBUG
1461 int original_height = frame_->height();
1462#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001463 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 // Pop context.
1467 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1468 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001469 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471}
1472
1473
ager@chromium.org7c537e22008-10-16 08:43:32 +00001474int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001476}
1477
ager@chromium.org7c537e22008-10-16 08:43:32 +00001478int CodeGenerator::FastCaseSwitchMinCaseCount() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001479 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001480}
1481
1482
ager@chromium.org7c537e22008-10-16 08:43:32 +00001483void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001484 SwitchStatement* node,
1485 int min_index,
1486 int range,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001487 Label* default_label,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001488 Vector<Label*> case_targets,
1489 Vector<Label> case_labels) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001490 VirtualFrame::SpilledScope spilled_scope;
1491 JumpTarget setup_default;
1492 JumpTarget is_smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001493
1494 // A non-null default label pointer indicates a default case among
1495 // the case labels. Otherwise we use the break target as a
1496 // "default" for failure to hit the jump table.
1497 JumpTarget* default_target =
1498 (default_label == NULL) ? node->break_target() : &setup_default;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001499
1500 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001501 frame_->EmitPop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001502
1503 // Test for a Smi value in a HeapNumber.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001504 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505 is_smi.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001506 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001507 default_target->Branch(ne);
1508 frame_->EmitPush(r0);
1509 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1510 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001511
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001512 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001513 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001514 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001515 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1516 if (Smi::IsValid(-min_index)) {
1517 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1518 } else {
1519 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1520 __ add(r0, r0, Operand(Smi::FromInt(1)));
1521 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001522 } else {
1523 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1524 }
1525 }
1526 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001527 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001528 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001529 default_target->Branch(ge);
1530 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001531 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001532
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001533 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1534
1535 // If there was a default case among the case labels, we need to
1536 // emit code to jump to it from the default target used for failure
1537 // to hit the jump table.
1538 if (default_label != NULL) {
1539 if (has_valid_frame()) {
1540 node->break_target()->Jump();
1541 }
1542 setup_default.Bind();
1543 frame_->MergeTo(start_frame);
1544 __ b(default_label);
1545 DeleteFrame();
1546 }
1547 if (node->break_target()->is_linked()) {
1548 node->break_target()->Bind();
1549 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001550}
1551
1552
ager@chromium.org7c537e22008-10-16 08:43:32 +00001553void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001554#ifdef DEBUG
1555 int original_height = frame_->height();
1556#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001557 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001559 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001560 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001562 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001563 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001564 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1565 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001566 }
1567
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001568 JumpTarget next_test;
1569 JumpTarget fall_through;
1570 JumpTarget default_entry;
1571 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 ZoneList<CaseClause*>* cases = node->cases();
1573 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001574 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575
1576 for (int i = 0; i < length; i++) {
1577 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001579 // Remember the default clause and compile it at the end.
1580 default_clause = clause;
1581 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 }
1583
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001584 Comment cmnt(masm_, "[ Case clause");
1585 // Compile the test.
1586 next_test.Bind();
1587 next_test.Unuse();
1588 // Duplicate TOS.
1589 __ ldr(r0, frame_->Top());
1590 frame_->EmitPush(r0);
1591 LoadAndSpill(clause->label());
1592 Comparison(eq, true);
1593 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 // Before entering the body from the test, remove the switch value from
1596 // the stack.
1597 frame_->Drop();
1598
1599 // Label the body so that fall through is enabled.
1600 if (i > 0 && cases->at(i - 1)->is_default()) {
1601 default_exit.Bind();
1602 } else {
1603 fall_through.Bind();
1604 fall_through.Unuse();
1605 }
1606 VisitStatementsAndSpill(clause->statements());
1607
1608 // If control flow can fall through from the body, jump to the next body
1609 // or the end of the statement.
1610 if (frame_ != NULL) {
1611 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1612 default_entry.Jump();
1613 } else {
1614 fall_through.Jump();
1615 }
1616 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 }
1618
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001619 // The final "test" removes the switch value.
1620 next_test.Bind();
1621 frame_->Drop();
1622
1623 // If there is a default clause, compile it.
1624 if (default_clause != NULL) {
1625 Comment cmnt(masm_, "[ Default clause");
1626 default_entry.Bind();
1627 VisitStatementsAndSpill(default_clause->statements());
1628 // If control flow can fall out of the default and there is a case after
1629 // it, jup to that case's body.
1630 if (frame_ != NULL && default_exit.is_bound()) {
1631 default_exit.Jump();
1632 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001633 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635 if (fall_through.is_linked()) {
1636 fall_through.Bind();
1637 }
1638
1639 if (node->break_target()->is_linked()) {
1640 node->break_target()->Bind();
1641 }
1642 node->break_target()->Unuse();
1643 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644}
1645
1646
ager@chromium.org7c537e22008-10-16 08:43:32 +00001647void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001648#ifdef DEBUG
1649 int original_height = frame_->height();
1650#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001651 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001653 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001654 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001656 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1657 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1659 if (node->cond() == NULL) {
1660 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1661 info = ALWAYS_TRUE;
1662 } else {
1663 Literal* lit = node->cond()->AsLiteral();
1664 if (lit != NULL) {
1665 if (lit->IsTrue()) {
1666 info = ALWAYS_TRUE;
1667 } else if (lit->IsFalse()) {
1668 info = ALWAYS_FALSE;
1669 }
1670 }
1671 }
1672
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001673 switch (node->type()) {
1674 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001675 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001677 // Label the top of the loop for the backward CFG edge. If the test
1678 // is always true we can use the continue target, and if the test is
1679 // always false there is no need.
1680 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001681 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001682 node->continue_target()->Bind();
1683 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001684 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001685 } else {
1686 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001687 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001688 body.Bind();
1689 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001692 VisitAndSpill(node->body());
1693
1694 // Compile the test.
1695 if (info == ALWAYS_TRUE) {
1696 if (has_valid_frame()) {
1697 // If control can fall off the end of the body, jump back to the
1698 // top.
1699 node->continue_target()->Jump();
1700 }
1701 } else if (info == ALWAYS_FALSE) {
1702 // If we have a continue in the body, we only have to bind its jump
1703 // target.
1704 if (node->continue_target()->is_linked()) {
1705 node->continue_target()->Bind();
1706 }
1707 } else {
1708 ASSERT(info == DONT_KNOW);
1709 // We have to compile the test expression if it can be reached by
1710 // control flow falling out of the body or via continue.
1711 if (node->continue_target()->is_linked()) {
1712 node->continue_target()->Bind();
1713 }
1714 if (has_valid_frame()) {
1715 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1716 &body, node->break_target(), true);
1717 if (has_valid_frame()) {
1718 // A invalid frame here indicates that control did not
1719 // fall out of the test expression.
1720 Branch(true, &body);
1721 }
1722 }
1723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001725 }
1726
1727 case LoopStatement::WHILE_LOOP: {
1728 // If the test is never true and has no side effects there is no need
1729 // to compile the test or body.
1730 if (info == ALWAYS_FALSE) break;
1731
1732 // Label the top of the loop with the continue target for the backward
1733 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001734 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001735 node->continue_target()->Bind();
1736
1737 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001738 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1740 &body, node->break_target(), true);
1741 if (has_valid_frame()) {
1742 // A NULL frame indicates that control did not fall out of the
1743 // test expression.
1744 Branch(false, node->break_target());
1745 }
1746 if (has_valid_frame() || body.is_linked()) {
1747 body.Bind();
1748 }
1749 }
1750
1751 if (has_valid_frame()) {
1752 CheckStack(); // TODO(1222600): ignore if body contains calls.
1753 VisitAndSpill(node->body());
1754
1755 // If control flow can fall out of the body, jump back to the top.
1756 if (has_valid_frame()) {
1757 node->continue_target()->Jump();
1758 }
1759 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001761 }
1762
1763 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001764 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001765
1766 if (node->init() != NULL) {
1767 VisitAndSpill(node->init());
1768 }
1769
1770 // There is no need to compile the test or body.
1771 if (info == ALWAYS_FALSE) break;
1772
1773 // If there is no update statement, label the top of the loop with the
1774 // continue target, otherwise with the loop target.
1775 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001776 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 node->continue_target()->Bind();
1778 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001779 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001780 loop.Bind();
1781 }
1782
1783 // If the test is always true, there is no need to compile it.
1784 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001785 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001786 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1787 &body, node->break_target(), true);
1788 if (has_valid_frame()) {
1789 Branch(false, node->break_target());
1790 }
1791 if (has_valid_frame() || body.is_linked()) {
1792 body.Bind();
1793 }
1794 }
1795
1796 if (has_valid_frame()) {
1797 CheckStack(); // TODO(1222600): ignore if body contains calls.
1798 VisitAndSpill(node->body());
1799
1800 if (node->next() == NULL) {
1801 // If there is no update statement and control flow can fall out
1802 // of the loop, jump directly to the continue label.
1803 if (has_valid_frame()) {
1804 node->continue_target()->Jump();
1805 }
1806 } else {
1807 // If there is an update statement and control flow can reach it
1808 // via falling out of the body of the loop or continuing, we
1809 // compile the update statement.
1810 if (node->continue_target()->is_linked()) {
1811 node->continue_target()->Bind();
1812 }
1813 if (has_valid_frame()) {
1814 // Record source position of the statement as this code which is
1815 // after the code for the body actually belongs to the loop
1816 // statement and not the body.
1817 CodeForStatementPosition(node);
1818 VisitAndSpill(node->next());
1819 loop.Jump();
1820 }
1821 }
1822 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 }
1826
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001827 if (node->break_target()->is_linked()) {
1828 node->break_target()->Bind();
1829 }
1830 node->continue_target()->Unuse();
1831 node->break_target()->Unuse();
1832 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833}
1834
1835
ager@chromium.org7c537e22008-10-16 08:43:32 +00001836void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001837#ifdef DEBUG
1838 int original_height = frame_->height();
1839#endif
1840 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001841 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001843 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001845 JumpTarget primitive;
1846 JumpTarget jsobject;
1847 JumpTarget fixed_array;
1848 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1849 JumpTarget end_del_check;
1850 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851
1852 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001853 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
1855 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1856 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001857 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
1863 // Stack layout in body:
1864 // [iteration counter (Smi)]
1865 // [length of array]
1866 // [FixedArray]
1867 // [Map or 0]
1868 // [Object]
1869
1870 // Check if enumerable is already a JSObject
1871 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001872 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001873 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 primitive.Bind();
1877 frame_->EmitPush(r0);
1878 Result arg_count = allocator_->Allocate(r0);
1879 ASSERT(arg_count.is_valid());
1880 __ mov(arg_count.reg(), Operand(0));
1881 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001885 frame_->EmitPush(r0); // duplicate the object being enumerated
1886 frame_->EmitPush(r0);
1887 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888
1889 // If we got a Map, we can do a fast modification check.
1890 // Otherwise, we got a FixedArray, and we have to do a slow check.
1891 __ mov(r2, Operand(r0));
1892 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1893 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895
1896 // Get enum cache
1897 __ mov(r1, Operand(r0));
1898 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1899 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1900 __ ldr(r2,
1901 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1902
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001903 frame_->EmitPush(r0); // map
1904 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001905 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001908 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 frame_->EmitPush(r0);
1910 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 frame_->EmitPush(r1); // insert 0 in place of Map
1915 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916
1917 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001918 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001921 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001922 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923
1924 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001926 // sp[0] : index
1927 // sp[1] : array/enum cache length
1928 // sp[2] : array or enum cache
1929 // sp[3] : 0 or map
1930 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001931 // Grab the current frame's height for the break and continue
1932 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001933 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1934 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001936 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1937 __ ldr(r1, frame_->ElementAt(1)); // load the length
1938 __ cmp(r0, Operand(r1)); // compare to the array length
1939 node->break_target()->Branch(hs);
1940
1941 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001942
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1946 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1947
1948 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 // Check if this (still) matches the map of the enumerable.
1951 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001952 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1954 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956
1957 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001958 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1959 frame_->EmitPush(r0);
1960 frame_->EmitPush(r3); // push entry
1961 Result arg_count_register = allocator_->Allocate(r0);
1962 ASSERT(arg_count_register.is_valid());
1963 __ mov(arg_count_register.reg(), Operand(1));
1964 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1965 CALL_JS,
1966 &arg_count_register,
1967 2);
1968 __ mov(r3, Operand(result.reg()));
1969 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970
1971 // If the property has been removed while iterating, we just skip it.
1972 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001973 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001975 end_del_check.Bind();
1976 // Store the entry in the 'each' expression and take another spin in the
1977 // loop. r3: i'th entry of the enum cache (or string there of)
1978 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 { Reference each(this, node->each());
1980 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001981 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982 __ ldr(r0, frame_->ElementAt(each.size()));
1983 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001984 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001985 // If the reference was to a slot we rely on the convenient property
1986 // that it doesn't matter whether a value (eg, r3 pushed above) is
1987 // right on top of or right underneath a zero-sized reference.
1988 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001989 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001990 // It's safe to pop the value lying on top of the reference before
1991 // unloading the reference itself (which preserves the top of stack,
1992 // ie, now the topmost value of the non-zero sized reference), since
1993 // we will discard the top of stack after unloading the reference
1994 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 }
1998 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001999 // Discard the i'th entry pushed above or else the remainder of the
2000 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002001 frame_->Drop();
2002
2003 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 VisitAndSpill(node->body());
2006
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002007 // Next. Reestablish a spilled frame in case we are coming here via
2008 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002010 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002011 frame_->EmitPop(r0);
2012 __ add(r0, r0, Operand(Smi::FromInt(1)));
2013 frame_->EmitPush(r0);
2014 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002016 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2017 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002019 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020
2021 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 exit.Bind();
2023 node->continue_target()->Unuse();
2024 node->break_target()->Unuse();
2025 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026}
2027
2028
ager@chromium.org7c537e22008-10-16 08:43:32 +00002029void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002030#ifdef DEBUG
2031 int original_height = frame_->height();
2032#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002033 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002037 JumpTarget try_block;
2038 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
2044 // Store the caught exception in the catch variable.
2045 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002046 ASSERT(ref.is_slot());
2047 // Here we make use of the convenient property that it doesn't matter
2048 // whether a value is immediately on top of or underneath a zero-sized
2049 // reference.
2050 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 }
2052
2053 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056 VisitStatementsAndSpill(node->catch_block()->statements());
2057 if (frame_ != NULL) {
2058 exit.Jump();
2059 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060
2061
2062 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002063 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2066 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002068 // Shadow the labels for all escapes from the try block, including
2069 // returns. During shadowing, the original label is hidden as the
2070 // LabelShadow and operations on the original actually affect the
2071 // shadowing label.
2072 //
2073 // We should probably try to unify the escaping labels and the return
2074 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 int nof_escapes = node->escaping_targets()->length();
2076 List<ShadowTarget*> shadows(1 + nof_escapes);
2077
2078 // Add the shadow target for the function return.
2079 static const int kReturnShadowIndex = 0;
2080 shadows.Add(new ShadowTarget(&function_return_));
2081 bool function_return_was_shadowed = function_return_is_shadowed_;
2082 function_return_is_shadowed_ = true;
2083 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2084
2085 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 }
2089
2090 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002091 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092
2093 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002094 // After shadowing stops, the original labels are unshadowed and the
2095 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096 bool has_unlinks = false;
2097 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 has_unlinks = has_unlinks || shadows[i]->is_linked();
2100 }
2101 function_return_is_shadowed_ = function_return_was_shadowed;
2102
2103 // Get an external reference to the handler address.
2104 ExternalReference handler_address(Top::k_handler_address);
2105
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002106 // If we can fall off the end of the try block, unlink from try chain.
2107 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002108 // The next handler address is on top of the frame. Unlink from
2109 // the handler list and drop the rest of this handler from the
2110 // frame.
2111 ASSERT(StackHandlerConstants::kNextOffset == 0);
2112 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 __ mov(r3, Operand(handler_address));
2114 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002115 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 if (has_unlinks) {
2117 exit.Jump();
2118 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 }
2120
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002121 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 // jumped to. Deallocate each shadow target.
2123 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002125 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 shadows[i]->Bind();
2127 // Because we can be jumping here (to spilled code) from unspilled
2128 // code, we need to reestablish a spilled frame at this block.
2129 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 // Reload sp from the top handler, because some statements that we
2132 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002135 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002137 ASSERT(StackHandlerConstants::kNextOffset == 0);
2138 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002140 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2143 frame_->PrepareForReturn();
2144 }
2145 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 }
2147 }
2148
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002149 exit.Bind();
2150 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151}
2152
2153
ager@chromium.org7c537e22008-10-16 08:43:32 +00002154void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002155#ifdef DEBUG
2156 int original_height = frame_->height();
2157#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002158 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002160 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161
2162 // State: Used to keep track of reason for entering the finally
2163 // block. Should probably be extended to hold information for
2164 // break/continue from within the try block.
2165 enum { FALLING, THROWING, JUMPING };
2166
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002167 JumpTarget try_block;
2168 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 // In case of thrown exceptions, this is where we continue.
2174 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176
2177 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002178 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002180 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2181 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002183 // Shadow the labels for all escapes from the try block, including
2184 // returns. Shadowing hides the original label as the LabelShadow and
2185 // operations on the original actually affect the shadowing label.
2186 //
2187 // We should probably try to unify the escaping labels and the return
2188 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002189 int nof_escapes = node->escaping_targets()->length();
2190 List<ShadowTarget*> shadows(1 + nof_escapes);
2191
2192 // Add the shadow target for the function return.
2193 static const int kReturnShadowIndex = 0;
2194 shadows.Add(new ShadowTarget(&function_return_));
2195 bool function_return_was_shadowed = function_return_is_shadowed_;
2196 function_return_is_shadowed_ = true;
2197 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2198
2199 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002201 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 }
2203
2204 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002205 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002207 // Stop the introduced shadowing and count the number of required unlinks.
2208 // After shadowing stops, the original labels are unshadowed and the
2209 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 shadows[i]->StopShadowing();
2213 if (shadows[i]->is_linked()) nof_unlinks++;
2214 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002215 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 // Get an external reference to the handler address.
2218 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 // If we can fall off the end of the try block, unlink from the try
2221 // chain and set the state on the frame to FALLING.
2222 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002223 // The next handler address is on top of the frame.
2224 ASSERT(StackHandlerConstants::kNextOffset == 0);
2225 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 __ mov(r3, Operand(handler_address));
2227 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002228 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002229
2230 // Fake a top of stack value (unneeded when FALLING) and set the
2231 // state in r2, then jump around the unlink blocks if any.
2232 __ mov(r0, Operand(Factory::undefined_value()));
2233 frame_->EmitPush(r0);
2234 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2235 if (nof_unlinks > 0) {
2236 finally_block.Jump();
2237 }
2238 }
2239
2240 // Generate code to unlink and set the state for the (formerly)
2241 // shadowing targets that have been jumped to.
2242 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002244 // If we have come from the shadowed return, the return value is
2245 // in (a non-refcounted reference to) r0. We must preserve it
2246 // until it is pushed.
2247 //
2248 // Because we can be jumping here (to spilled code) from
2249 // unspilled code, we need to reestablish a spilled frame at
2250 // this block.
2251 shadows[i]->Bind();
2252 frame_->SpillAll();
2253
2254 // Reload sp from the top handler, because some statements that
2255 // we break from (eg, for...in) may have left stuff on the
2256 // stack.
2257 __ mov(r3, Operand(handler_address));
2258 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002259 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260
2261 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002262 // handler address is currently on top of the frame.
2263 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002264 frame_->EmitPop(r1);
2265 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002266 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267
2268 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002269 // If this label shadowed the function return, materialize the
2270 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002272 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002273 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002274 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002275 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 }
2277 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002278 if (--nof_unlinks > 0) {
2279 // If this is not the last unlink block, jump around the next.
2280 finally_block.Jump();
2281 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 }
2283 }
2284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002286 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002288 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002290
2291 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292 // and the state - while evaluating the finally block.
2293 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002297 if (has_valid_frame()) {
2298 // Restore state and return value or faked TOS.
2299 frame_->EmitPop(r2);
2300 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 }
2302
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002303 // Generate code to jump to the right destination for all used
2304 // formerly shadowing targets. Deallocate each shadow target.
2305 for (int i = 0; i < shadows.length(); i++) {
2306 if (has_valid_frame() && shadows[i]->is_bound()) {
2307 JumpTarget* original = shadows[i]->other_target();
2308 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2309 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002310 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002311 skip.Branch(ne);
2312 frame_->PrepareForReturn();
2313 original->Jump();
2314 skip.Bind();
2315 } else {
2316 original->Branch(eq);
2317 }
2318 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 if (has_valid_frame()) {
2322 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002323 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002324 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2325 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002327 // Rethrow exception.
2328 frame_->EmitPush(r0);
2329 frame_->CallRuntime(Runtime::kReThrow, 1);
2330
2331 // Done.
2332 exit.Bind();
2333 }
2334 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335}
2336
2337
ager@chromium.org7c537e22008-10-16 08:43:32 +00002338void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339#ifdef DEBUG
2340 int original_height = frame_->height();
2341#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002342 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002344 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002345#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002347#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002348 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350}
2351
2352
ager@chromium.org7c537e22008-10-16 08:43:32 +00002353void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002354 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 ASSERT(boilerplate->IsBoilerplate());
2356
2357 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002358 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360
2361 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362 frame_->EmitPush(cp);
2363 frame_->CallRuntime(Runtime::kNewClosure, 2);
2364 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365}
2366
2367
ager@chromium.org7c537e22008-10-16 08:43:32 +00002368void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369#ifdef DEBUG
2370 int original_height = frame_->height();
2371#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002372 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 Comment cmnt(masm_, "[ FunctionLiteral");
2374
2375 // Build the function boilerplate and instantiate it.
2376 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002377 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002378 if (HasStackOverflow()) {
2379 ASSERT(frame_->height() == original_height);
2380 return;
2381 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002383 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384}
2385
2386
ager@chromium.org7c537e22008-10-16 08:43:32 +00002387void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002389#ifdef DEBUG
2390 int original_height = frame_->height();
2391#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002392 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2394 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002395 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396}
2397
2398
ager@chromium.org7c537e22008-10-16 08:43:32 +00002399void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002400#ifdef DEBUG
2401 int original_height = frame_->height();
2402#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002403 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002405 JumpTarget then;
2406 JumpTarget else_;
2407 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002408 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2409 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002411 then.Bind();
2412 LoadAndSpill(node->then_expression(), typeof_state());
2413 exit.Jump();
2414 else_.Bind();
2415 LoadAndSpill(node->else_expression(), typeof_state());
2416 exit.Bind();
2417 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418}
2419
2420
ager@chromium.org7c537e22008-10-16 08:43:32 +00002421void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002422 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002423 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002424 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002426 JumpTarget slow;
2427 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002428
2429 // Generate fast-case code for variables that might be shadowed by
2430 // eval-introduced variables. Eval is used a lot without
2431 // introducing variables. In those cases, we do not want to
2432 // perform a runtime call for all variables in the scope
2433 // containing the eval.
2434 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2435 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002436 // If there was no control flow to slow, we can exit early.
2437 if (!slow.is_linked()) {
2438 frame_->EmitPush(r0);
2439 return;
2440 }
2441
2442 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002443
2444 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2445 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2446 // Only generate the fast case for locals that rewrite to slots.
2447 // This rules out argument loads.
2448 if (potential_slot != NULL) {
2449 __ ldr(r0,
2450 ContextSlotOperandCheckExtensions(potential_slot,
2451 r1,
2452 r2,
2453 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002454 if (potential_slot->var()->mode() == Variable::CONST) {
2455 __ cmp(r0, Operand(Factory::the_hole_value()));
2456 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2457 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002459 // ContextSlotOperandCheckExtensions so we have to jump around
2460 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002462 }
2463 }
2464
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 slow.Bind();
2466 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002467 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002468 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469
ager@chromium.org7c537e22008-10-16 08:43:32 +00002470 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002472 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 done.Bind();
2477 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478
2479 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002480 // Note: We would like to keep the assert below, but it fires because of
2481 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002482 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002484 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002485 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002486 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002487 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002488 // Const slots may contain 'the hole' value (the constant hasn't been
2489 // initialized yet) which needs to be converted into the 'undefined'
2490 // value.
2491 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002492 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002493 __ cmp(r0, Operand(Factory::the_hole_value()));
2494 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002495 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 }
2497 }
2498}
2499
2500
ager@chromium.org381abbb2009-02-25 13:23:22 +00002501void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2502 TypeofState typeof_state,
2503 Register tmp,
2504 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002505 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002506 // Check that no extension objects have been created by calls to
2507 // eval from the current scope to the global scope.
2508 Register context = cp;
2509 Scope* s = scope();
2510 while (s != NULL) {
2511 if (s->num_heap_slots() > 0) {
2512 if (s->calls_eval()) {
2513 // Check that extension is NULL.
2514 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2515 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002516 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002517 }
2518 // Load next context in chain.
2519 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2520 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2521 context = tmp;
2522 }
2523 // If no outer scope calls eval, we do not need to check more
2524 // context extensions.
2525 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2526 s = s->outer_scope();
2527 }
2528
2529 if (s->is_eval_scope()) {
2530 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002531 if (!context.is(tmp)) {
2532 __ mov(tmp, Operand(context));
2533 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002534 __ bind(&next);
2535 // Terminate at global context.
2536 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2537 __ cmp(tmp2, Operand(Factory::global_context_map()));
2538 __ b(eq, &fast);
2539 // Check that extension is NULL.
2540 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2541 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002543 // Load next context in chain.
2544 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2545 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2546 __ b(&next);
2547 __ bind(&fast);
2548 }
2549
2550 // All extension objects were empty and it is safe to use a global
2551 // load IC call.
2552 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2553 // Load the global object.
2554 LoadGlobal();
2555 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 Result name = allocator_->Allocate(r2);
2557 ASSERT(name.is_valid()); // We are in spilled code.
2558 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002559 // Call IC stub.
2560 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002562 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002563 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002564 }
2565
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 // Drop the global object. The result is in r0.
2567 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002568}
2569
2570
ager@chromium.org7c537e22008-10-16 08:43:32 +00002571void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572#ifdef DEBUG
2573 int original_height = frame_->height();
2574#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002575 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002576 Comment cmnt(masm_, "[ Slot");
2577 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002579}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580
ager@chromium.org7c537e22008-10-16 08:43:32 +00002581
2582void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002583#ifdef DEBUG
2584 int original_height = frame_->height();
2585#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002586 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002587 Comment cmnt(masm_, "[ VariableProxy");
2588
2589 Variable* var = node->var();
2590 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002591 if (expr != NULL) {
2592 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002594 ASSERT(var->is_global());
2595 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002596 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002598 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599}
2600
2601
ager@chromium.org7c537e22008-10-16 08:43:32 +00002602void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603#ifdef DEBUG
2604 int original_height = frame_->height();
2605#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002606 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002608 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002609 frame_->EmitPush(r0);
2610 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611}
2612
2613
ager@chromium.org7c537e22008-10-16 08:43:32 +00002614void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002615#ifdef DEBUG
2616 int original_height = frame_->height();
2617#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002618 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 Comment cmnt(masm_, "[ RexExp Literal");
2620
2621 // Retrieve the literal array and check the allocated entry.
2622
2623 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002624 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625
2626 // Load the literals array of the function.
2627 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2628
2629 // Load the literal at the ast saved index.
2630 int literal_offset =
2631 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2632 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2633
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002634 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002636 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637
2638 // If the entry is undefined we call the runtime system to computed
2639 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002640 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002641 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002642 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002643 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002645 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002646 frame_->EmitPush(r0);
2647 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002648 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002650 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002652 frame_->EmitPush(r2);
2653 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654}
2655
2656
2657// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002658// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659// Each created boilerplate is stored in the JSFunction and they are
2660// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002663 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669 private:
2670 ObjectLiteral* node_;
2671};
2672
2673
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002674void DeferredObjectLiteral::Generate() {
2675 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002676
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002677 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002680 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002682 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002683 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002685 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002686 __ push(r0);
2687 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2688 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690}
2691
2692
ager@chromium.org7c537e22008-10-16 08:43:32 +00002693void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694#ifdef DEBUG
2695 int original_height = frame_->height();
2696#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002697 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 Comment cmnt(masm_, "[ ObjectLiteral");
2699
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002700 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
2702 // Retrieve the literal array and check the allocated entry.
2703
2704 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002705 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706
2707 // Load the literals array of the function.
2708 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2709
2710 // Load the literal at the ast saved index.
2711 int literal_offset =
2712 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2713 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2714
2715 // Check whether we need to materialize the object literal boilerplate.
2716 // If so, jump to the deferred code.
2717 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002718 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002719 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
2721 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002722 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2726 if (node->depth() == 1) {
2727 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2728 }
2729 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002731 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732
2733 for (int i = 0; i < node->properties()->length(); i++) {
2734 ObjectLiteral::Property* property = node->properties()->at(i);
2735 Literal* key = property->key();
2736 Expression* value = property->value();
2737 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002738 case ObjectLiteral::Property::CONSTANT:
2739 break;
2740 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2741 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2742 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 case ObjectLiteral::Property::COMPUTED: // fall through
2744 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 frame_->EmitPush(r0); // dup the result
2746 LoadAndSpill(key);
2747 LoadAndSpill(value);
2748 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002749 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002750 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 break;
2752 }
2753 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0);
2755 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002756 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
2758 LoadAndSpill(value);
2759 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002760 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 break;
2762 }
2763 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764 frame_->EmitPush(r0);
2765 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 LoadAndSpill(value);
2769 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002770 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 break;
2772 }
2773 }
2774 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002775 ASSERT(frame_->height() == original_height + 1);
2776}
2777
2778
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002779// This deferred code stub will be used for creating the boilerplate
2780// by calling Runtime_CreateArrayLiteralBoilerplate.
2781// Each created boilerplate is stored in the JSFunction and they are
2782// therefore context dependent.
2783class DeferredArrayLiteral: public DeferredCode {
2784 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002785 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786 set_comment("[ DeferredArrayLiteral");
2787 }
2788
2789 virtual void Generate();
2790
2791 private:
2792 ArrayLiteral* node_;
2793};
2794
2795
2796void DeferredArrayLiteral::Generate() {
2797 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002798
2799 // If the entry is undefined we call the runtime system to computed
2800 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002801 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002802 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002803 // Literal index (1).
2804 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002805 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002806 // Constant properties (2).
2807 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002808 __ push(r0);
2809 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2810 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002811 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002812}
2813
2814
ager@chromium.org7c537e22008-10-16 08:43:32 +00002815void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002816#ifdef DEBUG
2817 int original_height = frame_->height();
2818#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002819 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002821
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002822 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002823
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002824 // Retrieve the literal array and check the allocated entry.
2825
2826 // Load the function of this activation.
2827 __ ldr(r1, frame_->Function());
2828
2829 // Load the literals array of the function.
2830 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2831
2832 // Load the literal at the ast saved index.
2833 int literal_offset =
2834 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2835 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2836
2837 // Check whether we need to materialize the object literal boilerplate.
2838 // If so, jump to the deferred code.
2839 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002840 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002841 deferred->BindExit();
2842
2843 // Push the object literal boilerplate.
2844 frame_->EmitPush(r2);
2845
2846 // Clone the boilerplate object.
2847 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2848 if (node->depth() == 1) {
2849 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2850 }
2851 frame_->CallRuntime(clone_function_id, 1);
2852 frame_->EmitPush(r0); // save the result
2853 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002854
2855 // Generate code to set the elements in the array that are not
2856 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857 for (int i = 0; i < node->values()->length(); i++) {
2858 Expression* value = node->values()->at(i);
2859
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002860 // If value is a literal the property value is already set in the
2861 // boilerplate object.
2862 if (value->AsLiteral() != NULL) continue;
2863 // If value is a materialized literal the property value is already set
2864 // in the boilerplate object if it is simple.
2865 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002867 // The property must be set by generated code.
2868 LoadAndSpill(value);
2869 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002871 // Fetch the object literal.
2872 __ ldr(r1, frame_->Top());
2873 // Get the elements array.
2874 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002876 // Write to the indexed properties array.
2877 int offset = i * kPointerSize + Array::kHeaderSize;
2878 __ str(r0, FieldMemOperand(r1, offset));
2879
2880 // Update the write barrier for the array address.
2881 __ mov(r3, Operand(offset));
2882 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002885}
2886
2887
ager@chromium.org32912102009-01-16 10:38:43 +00002888void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002889#ifdef DEBUG
2890 int original_height = frame_->height();
2891#endif
2892 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002893 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002894 // Call runtime routine to allocate the catch extension object and
2895 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002896 Comment cmnt(masm_, "[ CatchExtensionObject");
2897 LoadAndSpill(node->key());
2898 LoadAndSpill(node->value());
2899 Result result =
2900 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2901 frame_->EmitPush(result.reg());
2902 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002903}
2904
2905
ager@chromium.org7c537e22008-10-16 08:43:32 +00002906void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907#ifdef DEBUG
2908 int original_height = frame_->height();
2909#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002910 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002912 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002913
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002914 { Reference target(this, node->target());
2915 if (target.is_illegal()) {
2916 // Fool the virtual frame into thinking that we left the assignment's
2917 // value on the frame.
2918 __ mov(r0, Operand(Smi::FromInt(0)));
2919 frame_->EmitPush(r0);
2920 ASSERT(frame_->height() == original_height + 1);
2921 return;
2922 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924 if (node->op() == Token::ASSIGN ||
2925 node->op() == Token::INIT_VAR ||
2926 node->op() == Token::INIT_CONST) {
2927 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002930 // +=, *= and similar binary assignments.
2931 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002932 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2933 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002934 bool overwrite =
2935 (node->value()->AsBinaryOperation() != NULL &&
2936 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002937 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002938 SmiOperation(node->binary_op(),
2939 literal->handle(),
2940 false,
2941 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002942 frame_->EmitPush(r0);
2943
2944 } else {
2945 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002946 GenericBinaryOperation(node->binary_op(),
2947 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948 frame_->EmitPush(r0);
2949 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002952 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2953 if (var != NULL &&
2954 (var->mode() == Variable::CONST) &&
2955 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2956 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 CodeForSourcePosition(node->position());
2960 if (node->op() == Token::INIT_CONST) {
2961 // Dynamic constant initializations must use the function context
2962 // and initialize the actual constant declared. Dynamic variable
2963 // initializations are simply assignments and use SetValue.
2964 target.SetValue(CONST_INIT);
2965 } else {
2966 target.SetValue(NOT_CONST_INIT);
2967 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968 }
2969 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002970 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971}
2972
2973
ager@chromium.org7c537e22008-10-16 08:43:32 +00002974void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002975#ifdef DEBUG
2976 int original_height = frame_->height();
2977#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002978 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979 Comment cmnt(masm_, "[ Throw");
2980
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002981 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002982 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002983 frame_->CallRuntime(Runtime::kThrow, 1);
2984 frame_->EmitPush(r0);
2985 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986}
2987
2988
ager@chromium.org7c537e22008-10-16 08:43:32 +00002989void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002990#ifdef DEBUG
2991 int original_height = frame_->height();
2992#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002993 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002995
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002996 { Reference property(this, node);
2997 property.GetValueAndSpill(typeof_state());
2998 }
2999 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000}
3001
3002
ager@chromium.org7c537e22008-10-16 08:43:32 +00003003void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004#ifdef DEBUG
3005 int original_height = frame_->height();
3006#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003007 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 Comment cmnt(masm_, "[ Call");
3009
3010 ZoneList<Expression*>* args = node->arguments();
3011
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003012 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013 // Standard function call.
3014
3015 // Check if the function is a variable or a property.
3016 Expression* function = node->expression();
3017 Variable* var = function->AsVariableProxy()->AsVariable();
3018 Property* property = function->AsProperty();
3019
3020 // ------------------------------------------------------------------------
3021 // Fast-case: Use inline caching.
3022 // ---
3023 // According to ECMA-262, section 11.2.3, page 44, the function to call
3024 // must be resolved after the arguments have been evaluated. The IC code
3025 // automatically handles this by loading the arguments before the function
3026 // is resolved in cache misses (this also holds for megamorphic calls).
3027 // ------------------------------------------------------------------------
3028
3029 if (var != NULL && !var->is_this() && var->is_global()) {
3030 // ----------------------------------
3031 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3032 // ----------------------------------
3033
3034 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003035 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003036 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003037
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003038 // Pass the global object as the receiver and let the IC stub
3039 // patch the stack to use the global proxy as 'this' in the
3040 // invoked function.
3041 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042
3043 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003044 int arg_count = args->length();
3045 for (int i = 0; i < arg_count; i++) {
3046 LoadAndSpill(args->at(i));
3047 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048
3049 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003050 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3051 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003052 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003053 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3054 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003055 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057 frame_->Drop();
3058 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059
3060 } else if (var != NULL && var->slot() != NULL &&
3061 var->slot()->type() == Slot::LOOKUP) {
3062 // ----------------------------------
3063 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3064 // ----------------------------------
3065
3066 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003068 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003069 frame_->EmitPush(r0);
3070 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 // r0: slot value; r1: receiver
3072
3073 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003074 frame_->EmitPush(r0); // function
3075 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003076
3077 // Call the function.
3078 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003079 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080
3081 } else if (property != NULL) {
3082 // Check if the key is a literal string.
3083 Literal* literal = property->key()->AsLiteral();
3084
3085 if (literal != NULL && literal->handle()->IsSymbol()) {
3086 // ------------------------------------------------------------------
3087 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3088 // ------------------------------------------------------------------
3089
3090 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003091 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->EmitPush(r0);
3093 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094
3095 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 int arg_count = args->length();
3097 for (int i = 0; i < arg_count; i++) {
3098 LoadAndSpill(args->at(i));
3099 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100
3101 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003102 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3103 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003104 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003105 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003106 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107
3108 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003110
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003111 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112
3113 } else {
3114 // -------------------------------------------
3115 // JavaScript example: 'array[index](1, 2, 3)'
3116 // -------------------------------------------
3117
3118 // Load the function to call from the property through a reference.
3119 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003120 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121
3122 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003123 if (property->is_synthetic()) {
3124 LoadGlobalReceiver(r0);
3125 } else {
3126 __ ldr(r0, frame_->ElementAt(ref.size()));
3127 frame_->EmitPush(r0);
3128 }
3129
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130 // Call the function.
3131 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 }
3134
3135 } else {
3136 // ----------------------------------
3137 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3138 // ----------------------------------
3139
3140 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003141 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003142
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003143 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003144 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 // Call the function.
3147 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003148 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003150 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151}
3152
3153
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003154void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003155#ifdef DEBUG
3156 int original_height = frame_->height();
3157#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003158 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003159 Comment cmnt(masm_, "[ CallEval");
3160
3161 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3162 // the function we need to call and the receiver of the call.
3163 // Then we call the resolved function using the given arguments.
3164
3165 ZoneList<Expression*>* args = node->arguments();
3166 Expression* function = node->expression();
3167
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003168 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003169
3170 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003171 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003172 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003173 frame_->EmitPush(r2); // Slot for receiver
3174 int arg_count = args->length();
3175 for (int i = 0; i < arg_count; i++) {
3176 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003177 }
3178
3179 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003180 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3181 frame_->EmitPush(r1);
3182 if (arg_count > 0) {
3183 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3184 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003185 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003186 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003187 }
3188
3189 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003190 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003191
3192 // Touch up stack with the right values for the function and the receiver.
3193 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003195 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003196 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003197
3198 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003199 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003200
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003201 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3202 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003203 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003204
3205 __ ldr(cp, frame_->Context());
3206 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003207 frame_->Drop();
3208 frame_->EmitPush(r0);
3209 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003210}
3211
3212
ager@chromium.org7c537e22008-10-16 08:43:32 +00003213void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003214#ifdef DEBUG
3215 int original_height = frame_->height();
3216#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003217 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220
3221 // According to ECMA-262, section 11.2.2, page 44, the function
3222 // expression in new calls must be evaluated before the
3223 // arguments. This is different from ordinary calls, where the
3224 // actual function to call is resolved after the arguments have been
3225 // evaluated.
3226
3227 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003228 // receiver. There is no need to use the global proxy here because
3229 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003230 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003231 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232
3233 // Push the arguments ("left-to-right") on the stack.
3234 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003235 int arg_count = args->length();
3236 for (int i = 0; i < arg_count; i++) {
3237 LoadAndSpill(args->at(i));
3238 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003239
mads.s.ager31e71382008-08-13 09:32:07 +00003240 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003241 Result num_args = allocator_->Allocate(r0);
3242 ASSERT(num_args.is_valid());
3243 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003245 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003246 Result function = allocator_->Allocate(r1);
3247 ASSERT(function.is_valid());
3248 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250 // Call the construct call builtin that handles allocation and
3251 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003252 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003253 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3254 Result result = frame_->CallCodeObject(ic,
3255 RelocInfo::CONSTRUCT_CALL,
3256 &num_args,
3257 &function,
3258 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003259
3260 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003261 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003262 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263}
3264
3265
ager@chromium.org7c537e22008-10-16 08:43:32 +00003266void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003267 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003269 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 LoadAndSpill(args->at(0));
3271 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003272 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003274 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003275 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3276 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003277 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003278 // Load the value.
3279 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003280 leave.Bind();
3281 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282}
3283
3284
ager@chromium.org7c537e22008-10-16 08:43:32 +00003285void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003286 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003288 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003289 LoadAndSpill(args->at(0)); // Load the object.
3290 LoadAndSpill(args->at(1)); // Load the value.
3291 frame_->EmitPop(r0); // r0 contains value
3292 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 // if (object->IsSmi()) return object.
3294 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003295 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003296 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3297 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003298 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 // Store the value.
3300 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3301 // Update the write barrier.
3302 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3303 __ RecordWrite(r1, r2, r3);
3304 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003305 leave.Bind();
3306 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307}
3308
3309
ager@chromium.org7c537e22008-10-16 08:43:32 +00003310void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003311 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003312 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 LoadAndSpill(args->at(0));
3314 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003315 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316 cc_reg_ = eq;
3317}
3318
3319
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003320void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003321 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003322 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3323 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003324#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003325 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003326 LoadAndSpill(args->at(1));
3327 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003328 __ CallRuntime(Runtime::kLog, 2);
3329 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003330#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003331 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003332 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003333}
3334
3335
ager@chromium.org7c537e22008-10-16 08:43:32 +00003336void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003337 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003338 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003339 LoadAndSpill(args->at(0));
3340 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003341 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3342 cc_reg_ = eq;
3343}
3344
3345
kasper.lund7276f142008-07-30 08:49:36 +00003346// This should generate code that performs a charCodeAt() call or returns
3347// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3348// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003349void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003350 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003351 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003352 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003353 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003354}
3355
3356
ager@chromium.org7c537e22008-10-16 08:43:32 +00003357void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003358 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003359 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003360 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003361 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003362 // We need the CC bits to come out as not_equal in the case where the
3363 // object is a smi. This can't be done with the usual test opcode so
3364 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003365 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003366 __ and_(r1, r0, Operand(kSmiTagMask));
3367 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003368 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003369 // It is a heap object - get the map. Check if the object is a JS array.
3370 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003371 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372 cc_reg_ = eq;
3373}
3374
3375
ager@chromium.org7c537e22008-10-16 08:43:32 +00003376void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003377 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378 ASSERT(args->length() == 0);
3379
mads.s.ager31e71382008-08-13 09:32:07 +00003380 // Seed the result with the formal parameters count, which will be used
3381 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3383
3384 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003385 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003386 frame_->CallStub(&stub, 0);
3387 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388}
3389
3390
ager@chromium.org7c537e22008-10-16 08:43:32 +00003391void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003392 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003393 ASSERT(args->length() == 1);
3394
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003395 // Satisfy contract with ArgumentsAccessStub:
3396 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003397 LoadAndSpill(args->at(0));
3398 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003399 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003400
3401 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003402 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 frame_->CallStub(&stub, 0);
3404 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405}
3406
3407
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003408void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3409 VirtualFrame::SpilledScope spilled_scope;
3410 ASSERT(args->length() == 0);
3411 __ Call(ExternalReference::random_positive_smi_function().address(),
3412 RelocInfo::RUNTIME_ENTRY);
3413 frame_->EmitPush(r0);
3414}
3415
3416
3417void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3418 VirtualFrame::SpilledScope spilled_scope;
3419 LoadAndSpill(args->at(0));
3420 switch (op) {
3421 case SIN:
3422 frame_->CallRuntime(Runtime::kMath_sin, 1);
3423 break;
3424 case COS:
3425 frame_->CallRuntime(Runtime::kMath_cos, 1);
3426 break;
3427 }
3428 frame_->EmitPush(r0);
3429}
3430
3431
ager@chromium.org7c537e22008-10-16 08:43:32 +00003432void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003433 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003434 ASSERT(args->length() == 2);
3435
3436 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003437 LoadAndSpill(args->at(0));
3438 LoadAndSpill(args->at(1));
3439 frame_->EmitPop(r0);
3440 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003441 __ cmp(r0, Operand(r1));
3442 cc_reg_ = eq;
3443}
3444
3445
ager@chromium.org7c537e22008-10-16 08:43:32 +00003446void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003447#ifdef DEBUG
3448 int original_height = frame_->height();
3449#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003450 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 if (CheckForInlineRuntimeCall(node)) {
3452 ASSERT((has_cc() && frame_->height() == original_height) ||
3453 (!has_cc() && frame_->height() == original_height + 1));
3454 return;
3455 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003456
3457 ZoneList<Expression*>* args = node->arguments();
3458 Comment cmnt(masm_, "[ CallRuntime");
3459 Runtime::Function* function = node->function();
3460
ager@chromium.org41826e72009-03-30 13:30:57 +00003461 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003462 // Prepare stack for calling JS runtime function.
3463 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003464 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003465 // Push the builtins object found in the current global object.
3466 __ ldr(r1, GlobalObject());
3467 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003468 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003469 }
mads.s.ager31e71382008-08-13 09:32:07 +00003470
ager@chromium.org41826e72009-03-30 13:30:57 +00003471 // Push the arguments ("left-to-right").
3472 int arg_count = args->length();
3473 for (int i = 0; i < arg_count; i++) {
3474 LoadAndSpill(args->at(i));
3475 }
mads.s.ager31e71382008-08-13 09:32:07 +00003476
ager@chromium.org41826e72009-03-30 13:30:57 +00003477 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003479 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3480 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003481 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003482 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483 frame_->Drop();
3484 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003485 } else {
3486 // Call the C runtime function.
3487 frame_->CallRuntime(function, arg_count);
3488 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003489 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003490 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491}
3492
3493
ager@chromium.org7c537e22008-10-16 08:43:32 +00003494void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003495#ifdef DEBUG
3496 int original_height = frame_->height();
3497#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003498 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499 Comment cmnt(masm_, "[ UnaryOperation");
3500
3501 Token::Value op = node->op();
3502
3503 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003504 LoadConditionAndSpill(node->expression(),
3505 NOT_INSIDE_TYPEOF,
3506 false_target(),
3507 true_target(),
3508 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509 cc_reg_ = NegateCondition(cc_reg_);
3510
3511 } else if (op == Token::DELETE) {
3512 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003513 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003515 LoadAndSpill(property->obj());
3516 LoadAndSpill(property->key());
3517 Result arg_count = allocator_->Allocate(r0);
3518 ASSERT(arg_count.is_valid());
3519 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3520 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521
mads.s.ager31e71382008-08-13 09:32:07 +00003522 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003523 Slot* slot = variable->slot();
3524 if (variable->is_global()) {
3525 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003526 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003527 frame_->EmitPush(r0);
3528 Result arg_count = allocator_->Allocate(r0);
3529 ASSERT(arg_count.is_valid());
3530 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3531 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532
3533 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3534 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003535 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003536 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003537 frame_->EmitPush(r0);
3538 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003540 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003541 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003542 frame_->EmitPush(r0);
3543 Result arg_count = allocator_->Allocate(r0);
3544 ASSERT(arg_count.is_valid());
3545 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3546 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547
mads.s.ager31e71382008-08-13 09:32:07 +00003548 } else {
3549 // Default: Result of deleting non-global, not dynamically
3550 // introduced variables is false.
3551 __ mov(r0, Operand(Factory::false_value()));
3552 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553
3554 } else {
3555 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003556 LoadAndSpill(node->expression()); // may have side-effects
3557 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003558 __ mov(r0, Operand(Factory::true_value()));
3559 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003560 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561
3562 } else if (op == Token::TYPEOF) {
3563 // Special case for loading the typeof expression; see comment on
3564 // LoadTypeofExpression().
3565 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003566 frame_->CallRuntime(Runtime::kTypeof, 1);
3567 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003568
3569 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003570 LoadAndSpill(node->expression());
3571 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 switch (op) {
3573 case Token::NOT:
3574 case Token::DELETE:
3575 case Token::TYPEOF:
3576 UNREACHABLE(); // handled above
3577 break;
3578
3579 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003580 bool overwrite =
3581 (node->AsBinaryOperation() != NULL &&
3582 node->AsBinaryOperation()->ResultOverwriteAllowed());
3583 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 break;
3586 }
3587
3588 case Token::BIT_NOT: {
3589 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003590 JumpTarget smi_label;
3591 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003592 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003595 frame_->EmitPush(r0);
3596 Result arg_count = allocator_->Allocate(r0);
3597 ASSERT(arg_count.is_valid());
3598 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3599 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003600
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003601 continue_label.Jump();
3602 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 __ mvn(r0, Operand(r0));
3604 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003605 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003606 break;
3607 }
3608
3609 case Token::VOID:
3610 // since the stack top is cached in r0, popping and then
3611 // pushing a value can be done by just writing to r0.
3612 __ mov(r0, Operand(Factory::undefined_value()));
3613 break;
3614
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003615 case Token::ADD: {
3616 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003617 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003618 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003619 continue_label.Branch(eq);
3620 frame_->EmitPush(r0);
3621 Result arg_count = allocator_->Allocate(r0);
3622 ASSERT(arg_count.is_valid());
3623 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3624 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3625 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003627 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 default:
3629 UNREACHABLE();
3630 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003631 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003633 ASSERT((has_cc() && frame_->height() == original_height) ||
3634 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635}
3636
3637
ager@chromium.org7c537e22008-10-16 08:43:32 +00003638void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003639#ifdef DEBUG
3640 int original_height = frame_->height();
3641#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003642 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643 Comment cmnt(masm_, "[ CountOperation");
3644
3645 bool is_postfix = node->is_postfix();
3646 bool is_increment = node->op() == Token::INC;
3647
3648 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3649 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3650
3651 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003652 if (is_postfix) {
3653 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003654 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003655 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656
3657 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003658 if (target.is_illegal()) {
3659 // Spoof the virtual frame to have the expected height (one higher
3660 // than on entry).
3661 if (!is_postfix) {
3662 __ mov(r0, Operand(Smi::FromInt(0)));
3663 frame_->EmitPush(r0);
3664 }
3665 ASSERT(frame_->height() == original_height + 1);
3666 return;
3667 }
3668 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3669 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003671 JumpTarget slow;
3672 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673
3674 // Load the value (1) into register r1.
3675 __ mov(r1, Operand(Smi::FromInt(1)));
3676
3677 // Check for smi operand.
3678 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003679 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680
3681 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003682 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003683 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003684 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685
3686 // Perform optimistic increment/decrement.
3687 if (is_increment) {
3688 __ add(r0, r0, Operand(r1), SetCC);
3689 } else {
3690 __ sub(r0, r0, Operand(r1), SetCC);
3691 }
3692
3693 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003694 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695
3696 // Revert optimistic increment/decrement.
3697 if (is_increment) {
3698 __ sub(r0, r0, Operand(r1));
3699 } else {
3700 __ add(r0, r0, Operand(r1));
3701 }
3702
3703 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003704 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003705 {
3706 // Convert the operand to a number.
3707 frame_->EmitPush(r0);
3708 Result arg_count = allocator_->Allocate(r0);
3709 ASSERT(arg_count.is_valid());
3710 __ mov(arg_count.reg(), Operand(0));
3711 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003714 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003715 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716 }
3717
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003718 // Compute the new value.
3719 __ mov(r1, Operand(Smi::FromInt(1)));
3720 frame_->EmitPush(r0);
3721 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003723 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003725 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003726 }
3727
3728 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729 exit.Bind();
3730 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003731 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003732 }
3733
3734 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003735 if (is_postfix) frame_->EmitPop(r0);
3736 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737}
3738
3739
ager@chromium.org7c537e22008-10-16 08:43:32 +00003740void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003741#ifdef DEBUG
3742 int original_height = frame_->height();
3743#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003744 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003745 Comment cmnt(masm_, "[ BinaryOperation");
3746 Token::Value op = node->op();
3747
3748 // According to ECMA-262 section 11.11, page 58, the binary logical
3749 // operators must yield the result of one of the two expressions
3750 // before any ToBoolean() conversions. This means that the value
3751 // produced by a && or || operator is not necessarily a boolean.
3752
3753 // NOTE: If the left hand side produces a materialized value (not in
3754 // the CC register), we force the right hand side to do the
3755 // same. This is necessary because we may have to branch to the exit
3756 // after evaluating the left hand side (due to the shortcut
3757 // semantics), but the compiler must (statically) know if the result
3758 // of compiling the binary operation is materialized or not.
3759
3760 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003761 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 LoadConditionAndSpill(node->left(),
3763 NOT_INSIDE_TYPEOF,
3764 &is_true,
3765 false_target(),
3766 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767 if (has_cc()) {
3768 Branch(false, false_target());
3769
3770 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003771 is_true.Bind();
3772 LoadConditionAndSpill(node->right(),
3773 NOT_INSIDE_TYPEOF,
3774 true_target(),
3775 false_target(),
3776 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777
3778 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003779 JumpTarget pop_and_continue;
3780 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003782 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 // Avoid popping the result if it converts to 'false' using the
3785 // standard ToBoolean() conversion as described in ECMA-262,
3786 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003787 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788 Branch(false, &exit);
3789
3790 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 pop_and_continue.Bind();
3792 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793
3794 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003795 is_true.Bind();
3796 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003797
3798 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003799 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800 }
3801
3802 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003803 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003804 LoadConditionAndSpill(node->left(),
3805 NOT_INSIDE_TYPEOF,
3806 true_target(),
3807 &is_false,
3808 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 if (has_cc()) {
3810 Branch(true, true_target());
3811
3812 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003813 is_false.Bind();
3814 LoadConditionAndSpill(node->right(),
3815 NOT_INSIDE_TYPEOF,
3816 true_target(),
3817 false_target(),
3818 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003819
3820 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003821 JumpTarget pop_and_continue;
3822 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003823
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003824 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003825 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003826 // Avoid popping the result if it converts to 'true' using the
3827 // standard ToBoolean() conversion as described in ECMA-262,
3828 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003829 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830 Branch(true, &exit);
3831
3832 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003833 pop_and_continue.Bind();
3834 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003835
3836 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003837 is_false.Bind();
3838 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003839
3840 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003841 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842 }
3843
3844 } else {
3845 // Optimize for the case where (at least) one of the expressions
3846 // is a literal small integer.
3847 Literal* lliteral = node->left()->AsLiteral();
3848 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003849 // NOTE: The code below assumes that the slow cases (calls to runtime)
3850 // never return a constant/immutable object.
3851 bool overwrite_left =
3852 (node->left()->AsBinaryOperation() != NULL &&
3853 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3854 bool overwrite_right =
3855 (node->right()->AsBinaryOperation() != NULL &&
3856 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857
3858 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003859 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003860 SmiOperation(node->op(),
3861 rliteral->handle(),
3862 false,
3863 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003864
3865 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003866 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003867 SmiOperation(node->op(),
3868 lliteral->handle(),
3869 true,
3870 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871
3872 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003873 OverwriteMode overwrite_mode = NO_OVERWRITE;
3874 if (overwrite_left) {
3875 overwrite_mode = OVERWRITE_LEFT;
3876 } else if (overwrite_right) {
3877 overwrite_mode = OVERWRITE_RIGHT;
3878 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003879 LoadAndSpill(node->left());
3880 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003881 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003883 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003885 ASSERT((has_cc() && frame_->height() == original_height) ||
3886 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003887}
3888
3889
ager@chromium.org7c537e22008-10-16 08:43:32 +00003890void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003891#ifdef DEBUG
3892 int original_height = frame_->height();
3893#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003894 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003895 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003896 frame_->EmitPush(r0);
3897 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003898}
3899
3900
ager@chromium.org7c537e22008-10-16 08:43:32 +00003901void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003902#ifdef DEBUG
3903 int original_height = frame_->height();
3904#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003905 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003906 Comment cmnt(masm_, "[ CompareOperation");
3907
3908 // Get the expressions from the node.
3909 Expression* left = node->left();
3910 Expression* right = node->right();
3911 Token::Value op = node->op();
3912
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003913 // To make null checks efficient, we check if either left or right is the
3914 // literal 'null'. If so, we optimize the code by inlining a null check
3915 // instead of calling the (very) general runtime routine for checking
3916 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003918 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003919 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003920 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003921 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3922 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003924 LoadAndSpill(left_is_null ? right : left);
3925 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 __ cmp(r0, Operand(Factory::null_value()));
3927
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003928 // The 'null' value is only equal to 'undefined' if using non-strict
3929 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003931 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003932
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003934 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003937 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003939 // It can be an undetectable object.
3940 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3941 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3942 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3943 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003944 }
3945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003946 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003947 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948 return;
3949 }
3950 }
3951
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003952 // To make typeof testing for natives implemented in JavaScript really
3953 // efficient, we generate special code for expressions of the form:
3954 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955 UnaryOperation* operation = left->AsUnaryOperation();
3956 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3957 (operation != NULL && operation->op() == Token::TYPEOF) &&
3958 (right->AsLiteral() != NULL &&
3959 right->AsLiteral()->handle()->IsString())) {
3960 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3961
mads.s.ager31e71382008-08-13 09:32:07 +00003962 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
3966 if (check->Equals(Heap::number_symbol())) {
3967 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3970 __ cmp(r1, Operand(Factory::heap_number_map()));
3971 cc_reg_ = eq;
3972
3973 } else if (check->Equals(Heap::string_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(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3978
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003979 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3981 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3982 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003983 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984
3985 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3986 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3987 cc_reg_ = lt;
3988
3989 } else if (check->Equals(Heap::boolean_symbol())) {
3990 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003991 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992 __ cmp(r1, Operand(Factory::false_value()));
3993 cc_reg_ = eq;
3994
3995 } else if (check->Equals(Heap::undefined_symbol())) {
3996 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003997 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003998
3999 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004000 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004001
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004002 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4004 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4005 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4006 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4007
4008 cc_reg_ = eq;
4009
4010 } else if (check->Equals(Heap::function_symbol())) {
4011 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004012 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004013 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014 cc_reg_ = eq;
4015
4016 } else if (check->Equals(Heap::object_symbol())) {
4017 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004018 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004019
4020 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4021 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004022 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004024 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004025 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4026 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4027 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004028 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029
4030 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4031 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4034 cc_reg_ = le;
4035
4036 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004037 // Uncommon case: typeof testing against a string literal that is
4038 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004039 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004041 ASSERT(!has_valid_frame() ||
4042 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043 return;
4044 }
4045
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004046 LoadAndSpill(left);
4047 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048 switch (op) {
4049 case Token::EQ:
4050 Comparison(eq, false);
4051 break;
4052
4053 case Token::LT:
4054 Comparison(lt);
4055 break;
4056
4057 case Token::GT:
4058 Comparison(gt);
4059 break;
4060
4061 case Token::LTE:
4062 Comparison(le);
4063 break;
4064
4065 case Token::GTE:
4066 Comparison(ge);
4067 break;
4068
4069 case Token::EQ_STRICT:
4070 Comparison(eq, true);
4071 break;
4072
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004073 case Token::IN: {
4074 Result arg_count = allocator_->Allocate(r0);
4075 ASSERT(arg_count.is_valid());
4076 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4077 Result result = frame_->InvokeBuiltin(Builtins::IN,
4078 CALL_JS,
4079 &arg_count,
4080 2);
4081 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004085 case Token::INSTANCEOF: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004086 InstanceofStub stub;
4087 Result result = frame_->CallStub(&stub, 2);
4088 // At this point if instanceof succeeded then r0 == 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004090 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004091 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004093
4094 default:
4095 UNREACHABLE();
4096 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004097 ASSERT((has_cc() && frame_->height() == original_height) ||
4098 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099}
4100
4101
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004102#ifdef DEBUG
4103bool CodeGenerator::HasValidEntryRegisters() { return true; }
4104#endif
4105
4106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004108#define __ ACCESS_MASM(masm)
4109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110
ager@chromium.org7c537e22008-10-16 08:43:32 +00004111Handle<String> Reference::GetName() {
4112 ASSERT(type_ == NAMED);
4113 Property* property = expression_->AsProperty();
4114 if (property == NULL) {
4115 // Global variable reference treated as a named property reference.
4116 VariableProxy* proxy = expression_->AsVariableProxy();
4117 ASSERT(proxy->AsVariable() != NULL);
4118 ASSERT(proxy->AsVariable()->is_global());
4119 return proxy->name();
4120 } else {
4121 Literal* raw_name = property->key()->AsLiteral();
4122 ASSERT(raw_name != NULL);
4123 return Handle<String>(String::cast(*raw_name->handle()));
4124 }
4125}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004126
ager@chromium.org7c537e22008-10-16 08:43:32 +00004127
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004128void Reference::GetValueAndSpill(TypeofState typeof_state) {
4129 ASSERT(cgen_->in_spilled_code());
4130 cgen_->set_in_spilled_code(false);
4131 GetValue(typeof_state);
4132 cgen_->frame()->SpillAll();
4133 cgen_->set_in_spilled_code(true);
4134}
4135
4136
ager@chromium.org7c537e22008-10-16 08:43:32 +00004137void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 ASSERT(!cgen_->in_spilled_code());
4139 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004140 ASSERT(!is_illegal());
4141 ASSERT(!cgen_->has_cc());
4142 MacroAssembler* masm = cgen_->masm();
4143 Property* property = expression_->AsProperty();
4144 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004145 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004146 }
4147
4148 switch (type_) {
4149 case SLOT: {
4150 Comment cmnt(masm, "[ Load from Slot");
4151 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4152 ASSERT(slot != NULL);
4153 cgen_->LoadFromSlot(slot, typeof_state);
4154 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004155 }
4156
ager@chromium.org7c537e22008-10-16 08:43:32 +00004157 case NAMED: {
4158 // TODO(1241834): Make sure that this it is safe to ignore the
4159 // distinction between expressions in a typeof and not in a typeof. If
4160 // there is a chance that reference errors can be thrown below, we
4161 // must distinguish between the two kinds of loads (typeof expression
4162 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004163 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004164 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004165 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004166 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004167 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4168 // Setup the name register.
4169 Result name_reg = cgen_->allocator()->Allocate(r2);
4170 ASSERT(name_reg.is_valid());
4171 __ mov(name_reg.reg(), Operand(name));
4172 ASSERT(var == NULL || var->is_global());
4173 RelocInfo::Mode rmode = (var == NULL)
4174 ? RelocInfo::CODE_TARGET
4175 : RelocInfo::CODE_TARGET_CONTEXT;
4176 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4177 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004178 break;
4179 }
4180
4181 case KEYED: {
4182 // TODO(1241834): Make sure that this it is safe to ignore the
4183 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004184
4185 // TODO(181): Implement inlined version of array indexing once
4186 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004187 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004188 Comment cmnt(masm, "[ Load from keyed Property");
4189 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004190 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004191 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004192 ASSERT(var == NULL || var->is_global());
4193 RelocInfo::Mode rmode = (var == NULL)
4194 ? RelocInfo::CODE_TARGET
4195 : RelocInfo::CODE_TARGET_CONTEXT;
4196 Result answer = frame->CallCodeObject(ic, rmode, 0);
4197 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004199 }
4200
4201 default:
4202 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004203 }
4204}
4205
4206
ager@chromium.org7c537e22008-10-16 08:43:32 +00004207void Reference::SetValue(InitState init_state) {
4208 ASSERT(!is_illegal());
4209 ASSERT(!cgen_->has_cc());
4210 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004211 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004212 Property* property = expression_->AsProperty();
4213 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004214 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004215 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004216
ager@chromium.org7c537e22008-10-16 08:43:32 +00004217 switch (type_) {
4218 case SLOT: {
4219 Comment cmnt(masm, "[ Store to Slot");
4220 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4221 ASSERT(slot != NULL);
4222 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004223 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004224
ager@chromium.org7c537e22008-10-16 08:43:32 +00004225 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004226 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004227 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004228 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004229
ager@chromium.org7c537e22008-10-16 08:43:32 +00004230 if (init_state == CONST_INIT) {
4231 // Same as the case for a normal store, but ignores attribute
4232 // (e.g. READ_ONLY) of context slot so that we can initialize
4233 // const properties (introduced via eval("const foo = (some
4234 // expr);")). Also, uses the current function context instead of
4235 // the top context.
4236 //
4237 // Note that we must declare the foo upon entry of eval(), via a
4238 // context slot declaration, but we cannot initialize it at the
4239 // same time, because the const declaration may be at the end of
4240 // the eval code (sigh...) and the const variable may have been
4241 // used before (where its value is 'undefined'). Thus, we can only
4242 // do the initialization when we actually encounter the expression
4243 // and when the expression operands are defined and valid, and
4244 // thus we need the split into 2 operations: declaration of the
4245 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004246 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004247 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004249 }
4250 // Storing a variable must keep the (new) value on the expression
4251 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004252 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004253
ager@chromium.org7c537e22008-10-16 08:43:32 +00004254 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004255 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004256
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004257 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004258 if (init_state == CONST_INIT) {
4259 ASSERT(slot->var()->mode() == Variable::CONST);
4260 // Only the first const initialization must be executed (the slot
4261 // still contains 'the hole' value). When the assignment is
4262 // executed, the code is identical to a normal store (see below).
4263 Comment cmnt(masm, "[ Init const");
4264 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4265 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004266 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004267 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004268
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269 // We must execute the store. Storing a variable must keep the
4270 // (new) value on the stack. This is necessary for compiling
4271 // assignment expressions.
4272 //
4273 // Note: We will reach here even with slot->var()->mode() ==
4274 // Variable::CONST because of const declarations which will
4275 // initialize consts to 'the hole' value and by doing so, end up
4276 // calling this code. r2 may be loaded with context; used below in
4277 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004278 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004279 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004280 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004281 if (slot->type() == Slot::CONTEXT) {
4282 // Skip write barrier if the written value is a smi.
4283 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004284 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004285 // r2 is loaded with context when calling SlotOperand above.
4286 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4287 __ mov(r3, Operand(offset));
4288 __ RecordWrite(r2, r3, r1);
4289 }
4290 // If we definitely did not jump over the assignment, we do not need
4291 // to bind the exit label. Doing so can defeat peephole
4292 // optimization.
4293 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004294 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004295 }
4296 }
4297 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004298 }
4299
ager@chromium.org7c537e22008-10-16 08:43:32 +00004300 case NAMED: {
4301 Comment cmnt(masm, "[ Store to named Property");
4302 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004303 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004304 Handle<String> name(GetName());
4305
4306 Result value = cgen_->allocator()->Allocate(r0);
4307 ASSERT(value.is_valid());
4308 frame->EmitPop(value.reg());
4309
4310 // Setup the name register.
4311 Result property_name = cgen_->allocator()->Allocate(r2);
4312 ASSERT(property_name.is_valid());
4313 __ mov(property_name.reg(), Operand(name));
4314 Result answer = frame->CallCodeObject(ic,
4315 RelocInfo::CODE_TARGET,
4316 &value,
4317 &property_name,
4318 0);
4319 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004320 break;
4321 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004322
ager@chromium.org7c537e22008-10-16 08:43:32 +00004323 case KEYED: {
4324 Comment cmnt(masm, "[ Store to keyed Property");
4325 Property* property = expression_->AsProperty();
4326 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004327 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004328
4329 // Call IC code.
4330 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4331 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004332 Result value = cgen_->allocator()->Allocate(r0);
4333 ASSERT(value.is_valid());
4334 frame->EmitPop(value.reg()); // value
4335 Result result =
4336 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4337 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004338 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004339 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004340
4341 default:
4342 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004343 }
4344}
4345
4346
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004347// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4348// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4349// (31 instead of 32).
4350static void CountLeadingZeros(
4351 MacroAssembler* masm,
4352 Register source,
4353 Register scratch,
4354 Register zeros) {
4355#ifdef __ARM_ARCH_5__
4356 __ clz(zeros, source); // This instruction is only supported after ARM5.
4357#else
4358 __ mov(zeros, Operand(0));
4359 __ mov(scratch, source);
4360 // Top 16.
4361 __ tst(scratch, Operand(0xffff0000));
4362 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4363 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4364 // Top 8.
4365 __ tst(scratch, Operand(0xff000000));
4366 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4367 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4368 // Top 4.
4369 __ tst(scratch, Operand(0xf0000000));
4370 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4371 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4372 // Top 2.
4373 __ tst(scratch, Operand(0xc0000000));
4374 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4375 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4376 // Top bit.
4377 __ tst(scratch, Operand(0x80000000));
4378 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4379#endif
4380}
4381
4382
4383// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4384// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4385// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4386// scratch register. Destroys the source register. No GC occurs during this
4387// stub so you don't have to set up the frame.
4388class ConvertToDoubleStub : public CodeStub {
4389 public:
4390 ConvertToDoubleStub(Register result_reg_1,
4391 Register result_reg_2,
4392 Register source_reg,
4393 Register scratch_reg)
4394 : result1_(result_reg_1),
4395 result2_(result_reg_2),
4396 source_(source_reg),
4397 zeros_(scratch_reg) { }
4398
4399 private:
4400 Register result1_;
4401 Register result2_;
4402 Register source_;
4403 Register zeros_;
4404
4405 // Minor key encoding in 16 bits.
4406 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4407 class OpBits: public BitField<Token::Value, 2, 14> {};
4408
4409 Major MajorKey() { return ConvertToDouble; }
4410 int MinorKey() {
4411 // Encode the parameters in a unique 16 bit value.
4412 return result1_.code() +
4413 (result2_.code() << 4) +
4414 (source_.code() << 8) +
4415 (zeros_.code() << 12);
4416 }
4417
4418 void Generate(MacroAssembler* masm);
4419
4420 const char* GetName() { return "ConvertToDoubleStub"; }
4421
4422#ifdef DEBUG
4423 void Print() { PrintF("ConvertToDoubleStub\n"); }
4424#endif
4425};
4426
4427
4428void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4429#ifndef BIG_ENDIAN_FLOATING_POINT
4430 Register exponent = result1_;
4431 Register mantissa = result2_;
4432#else
4433 Register exponent = result2_;
4434 Register mantissa = result1_;
4435#endif
4436 Label not_special;
4437 // Convert from Smi to integer.
4438 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4439 // Move sign bit from source to destination. This works because the sign bit
4440 // in the exponent word of the double has the same position and polarity as
4441 // the 2's complement sign bit in a Smi.
4442 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4443 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4444 // Subtract from 0 if source was negative.
4445 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4446 __ cmp(source_, Operand(1));
4447 __ b(gt, &not_special);
4448
4449 // We have -1, 0 or 1, which we treat specially.
4450 __ cmp(source_, Operand(0));
4451 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4452 static const uint32_t exponent_word_for_1 =
4453 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4454 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4455 // 1, 0 and -1 all have 0 for the second word.
4456 __ mov(mantissa, Operand(0));
4457 __ Ret();
4458
4459 __ bind(&not_special);
4460 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4461 // Gets the wrong answer for 0, but we already checked for that case above.
4462 CountLeadingZeros(masm, source_, mantissa, zeros_);
4463 // Compute exponent and or it into the exponent register.
4464 // We use result2 as a scratch register here.
4465 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4466 __ orr(exponent,
4467 exponent,
4468 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4469 // Shift up the source chopping the top bit off.
4470 __ add(zeros_, zeros_, Operand(1));
4471 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4472 __ mov(source_, Operand(source_, LSL, zeros_));
4473 // Compute lower part of fraction (last 12 bits).
4474 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4475 // And the top (top 20 bits).
4476 __ orr(exponent,
4477 exponent,
4478 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4479 __ Ret();
4480}
4481
4482
4483// This stub can convert a signed int32 to a heap number (double). It does
4484// not work for int32s that are in Smi range! No GC occurs during this stub
4485// so you don't have to set up the frame.
4486class WriteInt32ToHeapNumberStub : public CodeStub {
4487 public:
4488 WriteInt32ToHeapNumberStub(Register the_int,
4489 Register the_heap_number,
4490 Register scratch)
4491 : the_int_(the_int),
4492 the_heap_number_(the_heap_number),
4493 scratch_(scratch) { }
4494
4495 private:
4496 Register the_int_;
4497 Register the_heap_number_;
4498 Register scratch_;
4499
4500 // Minor key encoding in 16 bits.
4501 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4502 class OpBits: public BitField<Token::Value, 2, 14> {};
4503
4504 Major MajorKey() { return WriteInt32ToHeapNumber; }
4505 int MinorKey() {
4506 // Encode the parameters in a unique 16 bit value.
4507 return the_int_.code() +
4508 (the_heap_number_.code() << 4) +
4509 (scratch_.code() << 8);
4510 }
4511
4512 void Generate(MacroAssembler* masm);
4513
4514 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4515
4516#ifdef DEBUG
4517 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4518#endif
4519};
4520
4521
4522// See comment for class.
4523void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4524 Label max_negative_int;
4525 // the_int_ has the answer which is a signed int32 but not a Smi.
4526 // We test for the special value that has a different exponent. This test
4527 // has the neat side effect of setting the flags according to the sign.
4528 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4529 __ cmp(the_int_, Operand(0x80000000));
4530 __ b(eq, &max_negative_int);
4531 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4532 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4533 uint32_t non_smi_exponent =
4534 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4535 __ mov(scratch_, Operand(non_smi_exponent));
4536 // Set the sign bit in scratch_ if the value was negative.
4537 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4538 // Subtract from 0 if the value was negative.
4539 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4540 // We should be masking the implict first digit of the mantissa away here,
4541 // but it just ends up combining harmlessly with the last digit of the
4542 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4543 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4544 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4545 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4546 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4547 __ str(scratch_, FieldMemOperand(the_heap_number_,
4548 HeapNumber::kExponentOffset));
4549 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4550 __ str(scratch_, FieldMemOperand(the_heap_number_,
4551 HeapNumber::kMantissaOffset));
4552 __ Ret();
4553
4554 __ bind(&max_negative_int);
4555 // The max negative int32 is stored as a positive number in the mantissa of
4556 // a double because it uses a sign bit instead of using two's complement.
4557 // The actual mantissa bits stored are all 0 because the implicit most
4558 // significant 1 bit is not stored.
4559 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4560 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4561 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4562 __ mov(ip, Operand(0));
4563 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4564 __ Ret();
4565}
4566
4567
4568// Allocates a heap number or jumps to the label if the young space is full and
4569// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004570static void AllocateHeapNumber(
4571 MacroAssembler* masm,
4572 Label* need_gc, // Jump here if young space is full.
4573 Register result_reg, // The tagged address of the new heap number.
4574 Register allocation_top_addr_reg, // A scratch register.
4575 Register scratch2) { // Another scratch register.
4576 ExternalReference allocation_top =
4577 ExternalReference::new_space_allocation_top_address();
4578 ExternalReference allocation_limit =
4579 ExternalReference::new_space_allocation_limit_address();
4580
4581 // allocat := the address of the allocation top variable.
4582 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4583 // result_reg := the old allocation top.
4584 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4585 // scratch2 := the address of the allocation limit.
4586 __ mov(scratch2, Operand(allocation_limit));
4587 // scratch2 := the allocation limit.
4588 __ ldr(scratch2, MemOperand(scratch2));
4589 // result_reg := the new allocation top.
4590 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4591 // Compare new new allocation top and limit.
4592 __ cmp(result_reg, Operand(scratch2));
4593 // Branch if out of space in young generation.
4594 __ b(hi, need_gc);
4595 // Store new allocation top.
4596 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4597 // Tag and adjust back to start of new object.
4598 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4599 // Get heap number map into scratch2.
4600 __ mov(scratch2, Operand(Factory::heap_number_map()));
4601 // Store heap number map in new object.
4602 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4603}
4604
4605
4606// We fall into this code if the operands were Smis, but the result was
4607// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004608// the operands were not both Smi. The operands are in r0 and r1. In order
4609// to call the C-implemented binary fp operation routines we need to end up
4610// with the double precision floating point operands in r0 and r1 (for the
4611// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004612static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4613 Label* not_smi,
4614 const Builtins::JavaScript& builtin,
4615 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004616 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004617 Label slow, slow_pop_2_first, do_the_call;
4618 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4619 // Smi-smi case (overflow).
4620 // Since both are Smis there is no heap number to overwrite, so allocate.
4621 // The new heap number is in r5. r6 and r7 are scratch.
4622 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4623 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
4624 ConvertToDoubleStub stub1(r3, r2, r0, r6);
4625 __ push(lr);
4626 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4627 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4628 __ mov(r7, Operand(r1));
4629 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4630 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4631 __ pop(lr);
4632 __ jmp(&do_the_call); // Tail call. No return.
4633
4634 // We jump to here if something goes wrong (one param is not a number of any
4635 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004636 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004637 __ push(r1);
4638 __ push(r0);
4639 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004640 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004641
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004642 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004643 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004644 if (mode == NO_OVERWRITE) {
4645 // In the case where there is no chance of an overwritable float we may as
4646 // well do the allocation immediately while r0 and r1 are untouched.
4647 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4648 }
4649
4650 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004651 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004652 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
4653 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004654 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004655 if (mode == OVERWRITE_RIGHT) {
4656 __ mov(r5, Operand(r0)); // Overwrite this heap number.
4657 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004658 // Calling convention says that second double is in r2 and r3.
4659 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004660 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
4661 __ jmp(&finished_loading_r0);
4662 __ bind(&r0_is_smi);
4663 if (mode == OVERWRITE_RIGHT) {
4664 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004665 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004666 }
4667 // Write Smi from r0 to r3 and r2 in double format.
4668 __ mov(r7, Operand(r0));
4669 ConvertToDoubleStub stub3(r3, r2, r7, r6);
4670 __ push(lr);
4671 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
4672 __ pop(lr);
4673 __ bind(&finished_loading_r0);
4674
4675 // Move r1 to a double in r0-r1.
4676 __ tst(r1, Operand(kSmiTagMask));
4677 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
4678 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4679 __ b(ne, &slow);
4680 if (mode == OVERWRITE_LEFT) {
4681 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004682 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004683 // Calling convention says that first double is in r0 and r1.
4684 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004685 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
4686 __ jmp(&finished_loading_r1);
4687 __ bind(&r1_is_smi);
4688 if (mode == OVERWRITE_LEFT) {
4689 // We can't overwrite a Smi so get address of new heap number into r5.
4690 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4691 }
4692 // Write Smi from r1 to r1 and r0 in double format.
4693 __ mov(r7, Operand(r1));
4694 ConvertToDoubleStub stub4(r1, r0, r7, r6);
4695 __ push(lr);
4696 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
4697 __ pop(lr);
4698 __ bind(&finished_loading_r1);
4699
4700 __ bind(&do_the_call);
4701 // r0: Left value (least significant part of mantissa).
4702 // r1: Left value (sign, exponent, top of mantissa).
4703 // r2: Right value (least significant part of mantissa).
4704 // r3: Right value (sign, exponent, top of mantissa).
4705 // r5: Address of heap number for result.
4706 __ push(lr); // For later.
4707 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004708 // Call C routine that may not cause GC or other trouble.
4709 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004710 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004711 // Store answer in the overwritable heap number.
4712 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004713#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004714 // Double returned in fp coprocessor register 0 and 1, encoded as register
4715 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
4716 // substract the tag from r4.
4717 __ sub(r5, r4, Operand(kHeapObjectTag));
4718 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
4719#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004720 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004721 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004722 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004723#endif
4724 __ mov(r0, Operand(r4));
4725 // And we are done.
4726 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004727}
4728
4729
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004730// Tries to get a signed int32 out of a double precision floating point heap
4731// number. Rounds towards 0. Only succeeds for doubles that are in the ranges
4732// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
4733// almost to the range of signed int32 values that are not Smis. Jumps to the
4734// label if the double isn't in the range it can cope with.
4735static void GetInt32(MacroAssembler* masm,
4736 Register source,
4737 Register dest,
4738 Register scratch,
4739 Label* slow) {
4740 Register scratch2 = dest;
4741 // Get exponent word.
4742 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
4743 // Get exponent alone in scratch2.
4744 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
4745 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
4746 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4747 const uint32_t non_smi_exponent =
4748 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4749 __ cmp(scratch2, Operand(non_smi_exponent));
4750 // If not, then we go slow.
4751 __ b(ne, slow);
4752 // Get the top bits of the mantissa.
4753 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
4754 // Put back the implicit 1.
4755 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
4756 // Shift up the mantissa bits to take up the space the exponent used to take.
4757 // We just orred in the implicit bit so that took care of one and we want to
4758 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
4759 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4760 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
4761 // Put sign in zero flag.
4762 __ tst(scratch, Operand(HeapNumber::kSignMask));
4763 // Get the second half of the double.
4764 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
4765 // Shift down 22 bits to get the last 10 bits.
4766 __ orr(dest, scratch2, Operand(scratch, LSR, 32 - shift_distance));
4767 // Fix sign if sign bit was set.
4768 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
4769}
4770
4771
4772// For bitwise ops where the inputs are not both Smis we here try to determine
4773// whether both inputs are either Smis or at least heap numbers that can be
4774// represented by a 32 bit signed value. We truncate towards zero as required
4775// by the ES spec. If this is the case we do the bitwise op and see if the
4776// result is a Smi. If so, great, otherwise we try to find a heap number to
4777// write the answer into (either by allocating or by overwriting).
4778// On entry the operands are in r0 and r1. On exit the answer is in r0.
4779void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
4780 Label slow, result_not_a_smi;
4781 Label r0_is_smi, r1_is_smi;
4782 Label done_checking_r0, done_checking_r1;
4783
4784 __ tst(r1, Operand(kSmiTagMask));
4785 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
4786 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4787 __ b(ne, &slow);
4788 GetInt32(masm, r1, r3, r4, &slow);
4789 __ jmp(&done_checking_r1);
4790 __ bind(&r1_is_smi);
4791 __ mov(r3, Operand(r1, ASR, 1));
4792 __ bind(&done_checking_r1);
4793
4794 __ tst(r0, Operand(kSmiTagMask));
4795 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
4796 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4797 __ b(ne, &slow);
4798 GetInt32(masm, r0, r2, r4, &slow);
4799 __ jmp(&done_checking_r0);
4800 __ bind(&r0_is_smi);
4801 __ mov(r2, Operand(r0, ASR, 1));
4802 __ bind(&done_checking_r0);
4803
4804 // r0 and r1: Original operands (Smi or heap numbers).
4805 // r2 and r3: Signed int32 operands.
4806 switch (op_) {
4807 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
4808 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
4809 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
4810 case Token::SAR:
4811 // Use only the 5 least significant bits of the shift count.
4812 __ and_(r2, r2, Operand(0x1f));
4813 __ mov(r2, Operand(r3, ASR, r2));
4814 break;
4815 case Token::SHR:
4816 // Use only the 5 least significant bits of the shift count.
4817 __ and_(r2, r2, Operand(0x1f));
4818 __ mov(r2, Operand(r3, LSR, r2), SetCC);
4819 // SHR is special because it is required to produce a positive answer.
4820 // The code below for writing into heap numbers isn't capable of writing
4821 // the register as an unsigned int so we go to slow case if we hit this
4822 // case.
4823 __ b(mi, &slow);
4824 break;
4825 case Token::SHL:
4826 // Use only the 5 least significant bits of the shift count.
4827 __ and_(r2, r2, Operand(0x1f));
4828 __ mov(r2, Operand(r3, LSL, r2));
4829 break;
4830 default: UNREACHABLE();
4831 }
4832 // check that the *signed* result fits in a smi
4833 __ add(r3, r2, Operand(0x40000000), SetCC);
4834 __ b(mi, &result_not_a_smi);
4835 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
4836 __ Ret();
4837
4838 Label have_to_allocate, got_a_heap_number;
4839 __ bind(&result_not_a_smi);
4840 switch (mode_) {
4841 case OVERWRITE_RIGHT: {
4842 __ tst(r0, Operand(kSmiTagMask));
4843 __ b(eq, &have_to_allocate);
4844 __ mov(r5, Operand(r0));
4845 break;
4846 }
4847 case OVERWRITE_LEFT: {
4848 __ tst(r1, Operand(kSmiTagMask));
4849 __ b(eq, &have_to_allocate);
4850 __ mov(r5, Operand(r1));
4851 break;
4852 }
4853 case NO_OVERWRITE: {
4854 // Get a new heap number in r5. r6 and r7 are scratch.
4855 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4856 }
4857 default: break;
4858 }
4859 __ bind(&got_a_heap_number);
4860 // r2: Answer as signed int32.
4861 // r5: Heap number to write answer into.
4862
4863 // Nothing can go wrong now, so move the heap number to r0, which is the
4864 // result.
4865 __ mov(r0, Operand(r5));
4866
4867 // Tail call that writes the int32 in r2 to the heap number in r0, using
4868 // r3 as scratch. r0 is preserved and returned.
4869 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
4870 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4871
4872 if (mode_ != NO_OVERWRITE) {
4873 __ bind(&have_to_allocate);
4874 // Get a new heap number in r5. r6 and r7 are scratch.
4875 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4876 __ jmp(&got_a_heap_number);
4877 }
4878
4879 // If all else failed then we go to the runtime system.
4880 __ bind(&slow);
4881 __ push(r1); // restore stack
4882 __ push(r0);
4883 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4884 switch (op_) {
4885 case Token::BIT_OR:
4886 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4887 break;
4888 case Token::BIT_AND:
4889 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4890 break;
4891 case Token::BIT_XOR:
4892 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4893 break;
4894 case Token::SAR:
4895 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
4896 break;
4897 case Token::SHR:
4898 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
4899 break;
4900 case Token::SHL:
4901 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
4902 break;
4903 default:
4904 UNREACHABLE();
4905 }
4906}
4907
4908
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004909void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4910 // r1 : x
4911 // r0 : y
4912 // result : r0
4913
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004914 // All ops need to know whether we are dealing with two Smis. Set up r2 to
4915 // tell us that.
4916 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4917
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004918 switch (op_) {
4919 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004920 Label not_smi;
4921 // Fast path.
4922 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004923 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004924 __ b(ne, &not_smi);
4925 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
4926 // Return if no overflow.
4927 __ Ret(vc);
4928 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
4929
4930 HandleBinaryOpSlowCases(masm,
4931 &not_smi,
4932 Builtins::ADD,
4933 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004934 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004935 break;
4936 }
4937
4938 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004939 Label not_smi;
4940 // Fast path.
4941 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004942 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004943 __ b(ne, &not_smi);
4944 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
4945 // Return if no overflow.
4946 __ Ret(vc);
4947 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
4948
4949 HandleBinaryOpSlowCases(masm,
4950 &not_smi,
4951 Builtins::SUB,
4952 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004953 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004954 break;
4955 }
4956
4957 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004958 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004959 ASSERT(kSmiTag == 0); // adjust code below
4960 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004961 __ b(ne, &not_smi);
4962 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004963 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004964 // Do multiplication
4965 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
4966 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004967 __ mov(ip, Operand(r3, ASR, 31));
4968 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4969 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004970 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004971 __ tst(r3, Operand(r3));
4972 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004973 __ Ret(ne);
4974 // Slow case.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004975 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004976
4977 HandleBinaryOpSlowCases(masm,
4978 &not_smi,
4979 Builtins::MUL,
4980 Token::MUL,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004981 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004982 break;
4983 }
4984
4985 case Token::BIT_OR:
4986 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004987 case Token::BIT_XOR:
4988 case Token::SAR:
4989 case Token::SHR:
4990 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004991 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004992 ASSERT(kSmiTag == 0); // adjust code below
4993 __ tst(r2, Operand(kSmiTagMask));
4994 __ b(ne, &slow);
4995 switch (op_) {
4996 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4997 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4998 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004999 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005000 // Remove tags from right operand.
5001 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5002 // Use only the 5 least significant bits of the shift count.
5003 __ and_(r2, r2, Operand(0x1f));
5004 __ mov(r0, Operand(r1, ASR, r2));
5005 // Smi tag result.
5006 __ and_(r0, r0, Operand(~kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005007 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005008 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005009 // Remove tags from operands. We can't do this on a 31 bit number
5010 // because then the 0s get shifted into bit 30 instead of bit 31.
5011 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5012 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5013 // Use only the 5 least significant bits of the shift count.
5014 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005015 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005016 // Unsigned shift is not allowed to produce a negative number, so
5017 // check the sign bit and the sign bit after Smi tagging.
5018 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005019 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005020 // Smi tag result.
5021 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005022 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005023 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005024 // Remove tags from operands.
5025 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5026 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5027 // Use only the 5 least significant bits of the shift count.
5028 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005029 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005030 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005031 __ add(r2, r3, Operand(0x40000000), SetCC);
5032 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005033 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005034 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005035 default: UNREACHABLE();
5036 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005037 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005038 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005039 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005040 break;
5041 }
5042
5043 default: UNREACHABLE();
5044 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005045 // This code should be unreachable.
5046 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005047}
5048
5049
5050void StackCheckStub::Generate(MacroAssembler* masm) {
5051 Label within_limit;
5052 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5053 __ ldr(ip, MemOperand(ip));
5054 __ cmp(sp, Operand(ip));
5055 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005056 // Do tail-call to runtime routine. Runtime routines expect at least one
5057 // argument, so give it a Smi.
5058 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005059 __ push(r0);
5060 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5061 __ bind(&within_limit);
5062
5063 __ StubReturn(1);
5064}
5065
5066
5067void UnarySubStub::Generate(MacroAssembler* masm) {
5068 Label undo;
5069 Label slow;
5070 Label done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005071 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005072
5073 // Enter runtime system if the value is not a smi.
5074 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005075 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005076
5077 // Enter runtime system if the value of the expression is zero
5078 // to make sure that we switch between 0 and -0.
5079 __ cmp(r0, Operand(0));
5080 __ b(eq, &slow);
5081
5082 // The value of the expression is a smi that is not zero. Try
5083 // optimistic subtraction '0 - value'.
5084 __ rsb(r1, r0, Operand(0), SetCC);
5085 __ b(vs, &slow);
5086
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005087 __ mov(r0, Operand(r1)); // Set r0 to result.
5088 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005089
5090 // Enter runtime system.
5091 __ bind(&slow);
5092 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005093 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005094 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5095
5096 __ bind(&done);
5097 __ StubReturn(1);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005098
5099 __ bind(&not_smi);
5100 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5101 __ b(ne, &slow);
5102 // r0 is a heap number. Get a new heap number in r1.
5103 if (overwrite_) {
5104 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5105 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5106 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5107 } else {
5108 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5109 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5110 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5111 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5112 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5113 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5114 __ mov(r0, Operand(r1));
5115 }
5116 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005117}
5118
5119
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005120void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005121 // r0 holds the exception.
5122
5123 // Adjust this code if not the case.
5124 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5125
5126 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005127 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5128 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005129
5130 // Restore the next handler and frame pointer, discard handler state.
5131 ASSERT(StackHandlerConstants::kNextOffset == 0);
5132 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005133 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005134 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5135 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5136
5137 // Before returning we restore the context from the frame pointer if
5138 // not NULL. The frame pointer is NULL in the exception handler of a
5139 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005140 __ cmp(fp, Operand(0));
5141 // Set cp to NULL if fp is NULL.
5142 __ mov(cp, Operand(0), LeaveCC, eq);
5143 // Restore cp otherwise.
5144 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005145#ifdef DEBUG
5146 if (FLAG_debug_code) {
5147 __ mov(lr, Operand(pc));
5148 }
5149#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005150 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005151 __ pop(pc);
5152}
5153
5154
5155void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005156 // Adjust this code if not the case.
5157 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5158
5159 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005160 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005161 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005162
5163 // Unwind the handlers until the ENTRY handler is found.
5164 Label loop, done;
5165 __ bind(&loop);
5166 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005167 const int kStateOffset = StackHandlerConstants::kStateOffset;
5168 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005169 __ cmp(r2, Operand(StackHandler::ENTRY));
5170 __ b(eq, &done);
5171 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005172 const int kNextOffset = StackHandlerConstants::kNextOffset;
5173 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005174 __ jmp(&loop);
5175 __ bind(&done);
5176
5177 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005178 ASSERT(StackHandlerConstants::kNextOffset == 0);
5179 __ pop(r0);
5180 __ str(r0, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005181
5182 // Set external caught exception to false.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005183 ExternalReference external_caught(Top::k_external_caught_exception_address);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005184 __ mov(r0, Operand(false));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005185 __ mov(r2, Operand(external_caught));
5186 __ str(r0, MemOperand(r2));
5187
5188 // Set pending exception and r0 to out of memory exception.
5189 Failure* out_of_memory = Failure::OutOfMemoryException();
5190 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5191 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5192 __ str(r0, MemOperand(r2));
5193
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005194 // Stack layout at this point. See also StackHandlerConstants.
5195 // sp -> state (ENTRY)
5196 // fp
5197 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005198
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005199 // Discard handler state (r2 is not used) and restore frame pointer.
5200 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5201 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5202 // Before returning we restore the context from the frame pointer if
5203 // not NULL. The frame pointer is NULL in the exception handler of a
5204 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005205 __ cmp(fp, Operand(0));
5206 // Set cp to NULL if fp is NULL.
5207 __ mov(cp, Operand(0), LeaveCC, eq);
5208 // Restore cp otherwise.
5209 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005210#ifdef DEBUG
5211 if (FLAG_debug_code) {
5212 __ mov(lr, Operand(pc));
5213 }
5214#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005215 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005216 __ pop(pc);
5217}
5218
5219
5220void CEntryStub::GenerateCore(MacroAssembler* masm,
5221 Label* throw_normal_exception,
5222 Label* throw_out_of_memory_exception,
5223 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005224 bool do_gc,
5225 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005226 // r0: result parameter for PerformGC, if any
5227 // r4: number of arguments including receiver (C callee-saved)
5228 // r5: pointer to builtin function (C callee-saved)
5229 // r6: pointer to the first argument (C callee-saved)
5230
5231 if (do_gc) {
5232 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005233 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5234 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005235 }
5236
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005237 ExternalReference scope_depth =
5238 ExternalReference::heap_always_allocate_scope_depth();
5239 if (always_allocate) {
5240 __ mov(r0, Operand(scope_depth));
5241 __ ldr(r1, MemOperand(r0));
5242 __ add(r1, r1, Operand(1));
5243 __ str(r1, MemOperand(r0));
5244 }
5245
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005246 // Call C built-in.
5247 // r0 = argc, r1 = argv
5248 __ mov(r0, Operand(r4));
5249 __ mov(r1, Operand(r6));
5250
5251 // TODO(1242173): To let the GC traverse the return address of the exit
5252 // frames, we need to know where the return address is. Right now,
5253 // we push it on the stack to be able to find it again, but we never
5254 // restore from it in case of changes, which makes it impossible to
5255 // support moving the C entry code stub. This should be fixed, but currently
5256 // this is OK because the CEntryStub gets generated so early in the V8 boot
5257 // sequence that it is not moving ever.
5258 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5259 __ push(lr);
ager@chromium.org41826e72009-03-30 13:30:57 +00005260 __ Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005261
5262 if (always_allocate) {
5263 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5264 // though (contain the result).
5265 __ mov(r2, Operand(scope_depth));
5266 __ ldr(r3, MemOperand(r2));
5267 __ sub(r3, r3, Operand(1));
5268 __ str(r3, MemOperand(r2));
5269 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005270
5271 // check for failure result
5272 Label failure_returned;
5273 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5274 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5275 __ add(r2, r0, Operand(1));
5276 __ tst(r2, Operand(kFailureTagMask));
5277 __ b(eq, &failure_returned);
5278
5279 // Exit C frame and return.
5280 // r0:r1: result
5281 // sp: stack pointer
5282 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005283 __ LeaveExitFrame(frame_type);
5284
5285 // check if we should retry or throw exception
5286 Label retry;
5287 __ bind(&failure_returned);
5288 ASSERT(Failure::RETRY_AFTER_GC == 0);
5289 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5290 __ b(eq, &retry);
5291
5292 Label continue_exception;
5293 // If the returned failure is EXCEPTION then promote Top::pending_exception().
5294 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
5295 __ b(ne, &continue_exception);
5296
5297 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005298 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005299 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005300 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005301 __ ldr(r0, MemOperand(ip));
5302 __ str(r3, MemOperand(ip));
5303
5304 __ bind(&continue_exception);
5305 // Special handling of out of memory exception.
5306 Failure* out_of_memory = Failure::OutOfMemoryException();
5307 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5308 __ b(eq, throw_out_of_memory_exception);
5309
5310 // Handle normal exception.
5311 __ jmp(throw_normal_exception);
5312
5313 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5314}
5315
5316
5317void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5318 // Called from JavaScript; parameters are on stack as if calling JS function
5319 // r0: number of arguments including receiver
5320 // r1: pointer to builtin function
5321 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005322 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005323 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005324
5325 // NOTE: Invocations of builtins may return failure objects
5326 // instead of a proper result. The builtin entry handles
5327 // this by performing a garbage collection and retrying the
5328 // builtin once.
5329
5330 StackFrame::Type frame_type = is_debug_break
5331 ? StackFrame::EXIT_DEBUG
5332 : StackFrame::EXIT;
5333
5334 // Enter the exit frame that transitions from JavaScript to C++.
5335 __ EnterExitFrame(frame_type);
5336
5337 // r4: number of arguments (C callee-saved)
5338 // r5: pointer to builtin function (C callee-saved)
5339 // r6: pointer to first argument (C callee-saved)
5340
5341 Label throw_out_of_memory_exception;
5342 Label throw_normal_exception;
5343
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005344 // Call into the runtime system. Collect garbage before the call if
5345 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005346 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005347 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005348 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
5349 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005350 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005351 &throw_out_of_memory_exception,
5352 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005353 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005354 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005355
5356 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005357 GenerateCore(masm,
5358 &throw_normal_exception,
5359 &throw_out_of_memory_exception,
5360 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005361 true,
5362 false);
5363
5364 // Do full GC and retry runtime call one final time.
5365 Failure* failure = Failure::InternalError();
5366 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5367 GenerateCore(masm,
5368 &throw_normal_exception,
5369 &throw_out_of_memory_exception,
5370 frame_type,
5371 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005372 true);
5373
5374 __ bind(&throw_out_of_memory_exception);
5375 GenerateThrowOutOfMemory(masm);
5376 // control flow for generated will not return.
5377
5378 __ bind(&throw_normal_exception);
5379 GenerateThrowTOS(masm);
5380}
5381
5382
5383void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5384 // r0: code entry
5385 // r1: function
5386 // r2: receiver
5387 // r3: argc
5388 // [sp+0]: argv
5389
5390 Label invoke, exit;
5391
5392 // Called from C, so do not pop argc and args on exit (preserve sp)
5393 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005394 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005395 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5396
5397 // Get address of argv, see stm above.
5398 // r0: code entry
5399 // r1: function
5400 // r2: receiver
5401 // r3: argc
5402 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5403 __ ldr(r4, MemOperand(r4)); // argv
5404
5405 // Push a frame with special values setup to mark it as an entry frame.
5406 // r0: code entry
5407 // r1: function
5408 // r2: receiver
5409 // r3: argc
5410 // r4: argv
5411 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5412 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
5413 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
5414 __ mov(r6, Operand(Smi::FromInt(marker)));
5415 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5416 __ ldr(r5, MemOperand(r5));
5417 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5418
5419 // Setup frame pointer for the frame to be pushed.
5420 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5421
5422 // Call a faked try-block that does the invoke.
5423 __ bl(&invoke);
5424
5425 // Caught exception: Store result (exception) in the pending
5426 // exception field in the JSEnv and return a failure sentinel.
5427 // Coming in here the fp will be invalid because the PushTryHandler below
5428 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00005429 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005430 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005431 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005432 __ b(&exit);
5433
5434 // Invoke: Link this frame into the handler chain.
5435 __ bind(&invoke);
5436 // Must preserve r0-r4, r5-r7 are available.
5437 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005438 // If an exception not caught by another handler occurs, this handler
5439 // returns control to the code after the bl(&invoke) above, which
5440 // restores all kCalleeSaved registers (including cp and fp) to their
5441 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005442
5443 // Clear any pending exceptions.
5444 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5445 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005446 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005447 __ str(r5, MemOperand(ip));
5448
5449 // Invoke the function by calling through JS entry trampoline builtin.
5450 // Notice that we cannot store a reference to the trampoline code directly in
5451 // this stub, because runtime stubs are not traversed when doing GC.
5452
5453 // Expected registers by Builtins::JSEntryTrampoline
5454 // r0: code entry
5455 // r1: function
5456 // r2: receiver
5457 // r3: argc
5458 // r4: argv
5459 if (is_construct) {
5460 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
5461 __ mov(ip, Operand(construct_entry));
5462 } else {
5463 ExternalReference entry(Builtins::JSEntryTrampoline);
5464 __ mov(ip, Operand(entry));
5465 }
5466 __ ldr(ip, MemOperand(ip)); // deref address
5467
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005468 // Branch and link to JSEntryTrampoline. We don't use the double underscore
5469 // macro for the add instruction because we don't want the coverage tool
5470 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005471 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005472 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005473
5474 // Unlink this frame from the handler chain. When reading the
5475 // address of the next handler, there is no need to use the address
5476 // displacement since the current stack pointer (sp) points directly
5477 // to the stack handler.
5478 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5479 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5480 __ str(r3, MemOperand(ip));
5481 // No need to restore registers
5482 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5483
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005484
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005485 __ bind(&exit); // r0 holds result
5486 // Restore the top frame descriptors from the stack.
5487 __ pop(r3);
5488 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5489 __ str(r3, MemOperand(ip));
5490
5491 // Reset the stack to the callee saved registers.
5492 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5493
5494 // Restore callee-saved registers and return.
5495#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005496 if (FLAG_debug_code) {
5497 __ mov(lr, Operand(pc));
5498 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005499#endif
5500 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5501}
5502
5503
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005504// This stub performs an instanceof, calling the builtin function if
5505// necessary. Uses r1 for the object, r0 for the function that it may
5506// be an instance of (these are fetched from the stack).
5507void InstanceofStub::Generate(MacroAssembler* masm) {
5508 // Get the object - slow case for smis (we may need to throw an exception
5509 // depending on the rhs).
5510 Label slow, loop, is_instance, is_not_instance;
5511 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5512 __ BranchOnSmi(r0, &slow);
5513
5514 // Check that the left hand is a JS object and put map in r3.
5515 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
5516 __ b(lt, &slow);
5517 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
5518 __ b(gt, &slow);
5519
5520 // Get the prototype of the function (r4 is result, r2 is scratch).
5521 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
5522 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
5523
5524 // Check that the function prototype is a JS object.
5525 __ BranchOnSmi(r4, &slow);
5526 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
5527 __ b(lt, &slow);
5528 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
5529 __ b(gt, &slow);
5530
5531 // Register mapping: r3 is object map and r4 is function prototype.
5532 // Get prototype of object into r2.
5533 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
5534
5535 // Loop through the prototype chain looking for the function prototype.
5536 __ bind(&loop);
5537 __ cmp(r2, Operand(r4));
5538 __ b(eq, &is_instance);
5539 __ cmp(r2, Operand(Factory::null_value()));
5540 __ b(eq, &is_not_instance);
5541 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
5542 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
5543 __ jmp(&loop);
5544
5545 __ bind(&is_instance);
5546 __ mov(r0, Operand(Smi::FromInt(0)));
5547 __ pop();
5548 __ pop();
5549 __ mov(pc, Operand(lr)); // Return.
5550
5551 __ bind(&is_not_instance);
5552 __ mov(r0, Operand(Smi::FromInt(1)));
5553 __ pop();
5554 __ pop();
5555 __ mov(pc, Operand(lr)); // Return.
5556
5557 // Slow-case. Tail call builtin.
5558 __ bind(&slow);
5559 __ mov(r0, Operand(1)); // Arg count without receiver.
5560 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
5561}
5562
5563
ager@chromium.org7c537e22008-10-16 08:43:32 +00005564void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005565 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005566 Label adaptor;
5567 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5568 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5569 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005570 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005571
ager@chromium.org7c537e22008-10-16 08:43:32 +00005572 // Nothing to do: The formal number of parameters has already been
5573 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00005574 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005575
ager@chromium.org7c537e22008-10-16 08:43:32 +00005576 // Arguments adaptor case: Read the arguments length from the
5577 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005578 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005579 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00005580 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005581}
5582
5583
ager@chromium.org7c537e22008-10-16 08:43:32 +00005584void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5585 // The displacement is the offset of the last parameter (if any)
5586 // relative to the frame pointer.
5587 static const int kDisplacement =
5588 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005589
ager@chromium.org7c537e22008-10-16 08:43:32 +00005590 // Check that the key is a smi.
5591 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005592 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005593
ager@chromium.org7c537e22008-10-16 08:43:32 +00005594 // Check if the calling frame is an arguments adaptor frame.
5595 Label adaptor;
5596 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5597 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5598 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5599 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005600
ager@chromium.org7c537e22008-10-16 08:43:32 +00005601 // Check index against formal parameters count limit passed in
5602 // through register eax. Use unsigned comparison to get negative
5603 // check for free.
5604 __ cmp(r1, r0);
5605 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005606
ager@chromium.org7c537e22008-10-16 08:43:32 +00005607 // Read the argument from the stack and return it.
5608 __ sub(r3, r0, r1);
5609 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5610 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005611 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005612
5613 // Arguments adaptor case: Check index against actual arguments
5614 // limit found in the arguments adaptor frame. Use unsigned
5615 // comparison to get negative check for free.
5616 __ bind(&adaptor);
5617 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5618 __ cmp(r1, r0);
5619 __ b(cs, &slow);
5620
5621 // Read the argument from the adaptor frame and return it.
5622 __ sub(r3, r0, r1);
5623 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5624 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005625 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005626
5627 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5628 // by calling the runtime system.
5629 __ bind(&slow);
5630 __ push(r1);
5631 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5632}
5633
5634
5635void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5636 // Check if the calling frame is an arguments adaptor frame.
5637 Label runtime;
5638 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5639 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5640 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5641 __ b(ne, &runtime);
5642
5643 // Patch the arguments.length and the parameters pointer.
5644 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5645 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5646 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5647 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5648 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5649
5650 // Do the runtime call to allocate the arguments object.
5651 __ bind(&runtime);
5652 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005653}
5654
5655
5656void CallFunctionStub::Generate(MacroAssembler* masm) {
5657 Label slow;
5658 // Get the function to call from the stack.
5659 // function, receiver [, arguments]
5660 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5661
5662 // Check that the function is really a JavaScript function.
5663 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005664 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005665 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005666 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005667 __ b(ne, &slow);
5668
5669 // Fast-case: Invoke the function now.
5670 // r1: pushed function
5671 ParameterCount actual(argc_);
5672 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5673
5674 // Slow-case: Non-function called.
5675 __ bind(&slow);
5676 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005677 __ mov(r2, Operand(0));
5678 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5679 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5680 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005681}
5682
5683
5684#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686} } // namespace v8::internal