blob: 7428d3b5974177c81875b4e51d4b8c8f15575c08 [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
292 // pp: parameter pointer
293 // cp: callee's context
294 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000296 function_return_.Bind();
297 if (FLAG_trace) {
298 // Push the return value on the stack as the parameter.
299 // Runtime::TraceExit returns the parameter as it is.
300 frame_->EmitPush(r0);
301 frame_->CallRuntime(Runtime::kTraceExit, 1);
302 }
303
304 // Tear down the frame which will restore the caller's frame pointer and
305 // the link register.
306 frame_->Exit();
307
308 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000309 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000310 }
311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 ASSERT(!has_cc());
314 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000315 ASSERT(!function_return_is_shadowed_);
316 function_return_.Unuse();
317 DeleteFrame();
318
319 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000320 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321 ProcessDeferred();
322 }
323
324 allocator_ = NULL;
325 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326}
327
328
ager@chromium.org7c537e22008-10-16 08:43:32 +0000329MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
330 // Currently, this assertion will fail if we try to assign to
331 // a constant variable that is constant because it is read-only
332 // (such as the variable referring to a named function expression).
333 // We need to implement assignments to read-only variables.
334 // Ideally, we should do this during AST generation (by converting
335 // such assignments into expression statements); however, in general
336 // we may not be able to make the decision until past AST generation,
337 // that is when the entire program is known.
338 ASSERT(slot != NULL);
339 int index = slot->index();
340 switch (slot->type()) {
341 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000342 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000343
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000344 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000345 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000346
347 case Slot::CONTEXT: {
348 // Follow the context chain if necessary.
349 ASSERT(!tmp.is(cp)); // do not overwrite context register
350 Register context = cp;
351 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000352 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000353 // Load the closure.
354 // (All contexts, even 'with' contexts, have a closure,
355 // and it is the same for all contexts inside a function.
356 // There is no need to go to the function context first.)
357 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
358 // Load the function context (which is the incoming, outer context).
359 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
360 context = tmp;
361 }
362 // We may have a 'with' context now. Get the function context.
363 // (In fact this mov may never be the needed, since the scope analysis
364 // may not permit a direct context access in this case and thus we are
365 // always at a function context. However it is safe to dereference be-
366 // cause the function context of a function context is itself. Before
367 // deleting this mov we should try to create a counter-example first,
368 // though...)
369 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
370 return ContextOperand(tmp, index);
371 }
372
373 default:
374 UNREACHABLE();
375 return MemOperand(r0, 0);
376 }
377}
378
379
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000380MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
381 Slot* slot,
382 Register tmp,
383 Register tmp2,
384 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000385 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000386 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000387
ager@chromium.org381abbb2009-02-25 13:23:22 +0000388 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
389 if (s->num_heap_slots() > 0) {
390 if (s->calls_eval()) {
391 // Check that extension is NULL.
392 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
393 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000395 }
396 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
397 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
398 context = tmp;
399 }
400 }
401 // Check that last extension is NULL.
402 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
403 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000404 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000405 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000407}
408
409
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000410void CodeGenerator::LoadConditionAndSpill(Expression* expression,
411 TypeofState typeof_state,
412 JumpTarget* true_target,
413 JumpTarget* false_target,
414 bool force_control) {
415 ASSERT(in_spilled_code());
416 set_in_spilled_code(false);
417 LoadCondition(expression, typeof_state, true_target, false_target,
418 force_control);
419 if (frame_ != NULL) {
420 frame_->SpillAll();
421 }
422 set_in_spilled_code(true);
423}
424
425
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000426// Loads a value on TOS. If it is a boolean value, the result may have been
427// (partially) translated into branches, or it may have set the condition
428// code register. If force_cc is set, the value is forced to set the
429// condition code register and no value is pushed. If the condition code
430// register was set, has_cc() is true and cc_reg_ contains the condition to
431// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000432void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000433 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000434 JumpTarget* true_target,
435 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000436 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000437 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000438 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000439 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
ager@chromium.org7c537e22008-10-16 08:43:32 +0000441 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000442 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000443
444 // If we hit a stack overflow, we may not have actually visited
445 // the expression. In that case, we ensure that we have a
446 // valid-looking frame state because we will continue to generate
447 // code as we unwind the C++ stack.
448 //
449 // It's possible to have both a stack overflow and a valid frame
450 // state (eg, a subexpression overflowed, visiting it returned
451 // with a dummied frame state, and visiting this expression
452 // returned with a normal-looking state).
453 if (HasStackOverflow() &&
454 has_valid_frame() &&
455 !has_cc() &&
456 frame_->height() == original_height) {
457 true_target->Jump();
458 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000459 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000460 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000461 // Convert the TOS value to a boolean in the condition code register.
462 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000464 ASSERT(!force_cc || !has_valid_frame() || has_cc());
465 ASSERT(!has_valid_frame() ||
466 (has_cc() && frame_->height() == original_height) ||
467 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468}
469
470
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000471void CodeGenerator::LoadAndSpill(Expression* expression,
472 TypeofState typeof_state) {
473 ASSERT(in_spilled_code());
474 set_in_spilled_code(false);
475 Load(expression, typeof_state);
476 frame_->SpillAll();
477 set_in_spilled_code(true);
478}
479
480
ager@chromium.org7c537e22008-10-16 08:43:32 +0000481void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000482#ifdef DEBUG
483 int original_height = frame_->height();
484#endif
485 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000486 JumpTarget true_target;
487 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000488 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489
490 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000492 JumpTarget loaded;
493 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000495 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000496 frame_->EmitPush(r0);
497 loaded.Jump();
498 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000499 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000500 frame_->EmitPush(r0);
501 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 cc_reg_ = al;
503 }
504
505 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506 // We have at least one condition value that has been "translated"
507 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000508 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000509 if (frame_ != NULL) {
510 loaded.Jump(); // Don't lose the current TOS.
511 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000513 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000516 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000517 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 // If both "true" and "false" need to be loaded jump across the code for
520 // "false".
521 if (both) {
522 loaded.Jump();
523 }
524 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000527 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000530 // A value is loaded on all paths reaching this point.
531 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000533 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536}
537
538
ager@chromium.org7c537e22008-10-16 08:43:32 +0000539void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000540 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000541 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543}
544
545
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000546void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000547 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000548 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
549 __ ldr(scratch,
550 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000551 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000552}
553
554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000556// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
557// variables w/o reference errors elsewhere.
558void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000559 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 Variable* variable = x->AsVariableProxy()->AsVariable();
561 if (variable != NULL && !variable->is_this() && variable->is_global()) {
562 // NOTE: This is somewhat nasty. We force the compiler to load
563 // the variable as if through '<global>.<variable>' to make sure we
564 // do not get reference errors.
565 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
566 Literal key(variable->name());
567 // TODO(1241834): Fetch the position from the variable instead of using
568 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000569 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000572 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
574}
575
576
ager@chromium.org7c537e22008-10-16 08:43:32 +0000577Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
579 cgen->LoadReference(this);
580}
581
582
583Reference::~Reference() {
584 cgen_->UnloadReference(this);
585}
586
587
ager@chromium.org7c537e22008-10-16 08:43:32 +0000588void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000589 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000590 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 Expression* e = ref->expression();
592 Property* property = e->AsProperty();
593 Variable* var = e->AsVariableProxy()->AsVariable();
594
595 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000596 // The expression is either a property or a variable proxy that rewrites
597 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000598 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000599 // We use a named reference if the key is a literal symbol, unless it is
600 // a string that can be legally parsed as an integer. This is because
601 // otherwise we will not get into the slow case code that handles [] on
602 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 Literal* literal = property->key()->AsLiteral();
604 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000605 if (literal != NULL &&
606 literal->handle()->IsSymbol() &&
607 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 ref->set_type(Reference::NAMED);
609 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000610 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 ref->set_type(Reference::KEYED);
612 }
613 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000614 // The expression is a variable proxy that does not rewrite to a
615 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 LoadGlobal();
618 ref->set_type(Reference::NAMED);
619 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000620 ASSERT(var->slot() != NULL);
621 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 }
623 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000624 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000625 LoadAndSpill(e);
626 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 }
628}
629
630
ager@chromium.org7c537e22008-10-16 08:43:32 +0000631void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000632 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000633 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000634 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000636 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000637 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000638 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000639 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 }
641}
642
643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
645// register to a boolean in the condition code register. The code
646// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647void CodeGenerator::ToBoolean(JumpTarget* true_target,
648 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000649 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000650 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000652 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 // Fast case checks
655
mads.s.ager31e71382008-08-13 09:32:07 +0000656 // Check if the value is 'false'.
657 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000658 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
mads.s.ager31e71382008-08-13 09:32:07 +0000660 // Check if the value is 'true'.
661 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000662 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663
mads.s.ager31e71382008-08-13 09:32:07 +0000664 // Check if the value is 'undefined'.
665 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000666 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667
mads.s.ager31e71382008-08-13 09:32:07 +0000668 // Check if the value is a smi.
669 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000670 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000671 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000672 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
674 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000675 frame_->EmitPush(r0);
676 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000677 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
680 cc_reg_ = ne;
681}
682
683
kasper.lund7276f142008-07-30 08:49:36 +0000684class GenericBinaryOpStub : public CodeStub {
685 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000686 GenericBinaryOpStub(Token::Value op,
687 OverwriteMode mode)
688 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000689
690 private:
691 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000692 OverwriteMode mode_;
693
694 // Minor key encoding in 16 bits.
695 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
696 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000697
698 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699 int MinorKey() {
700 // Encode the parameters in a unique 16 bit value.
701 return OpBits::encode(op_)
702 | ModeBits::encode(mode_);
703 }
704
kasper.lund7276f142008-07-30 08:49:36 +0000705 void Generate(MacroAssembler* masm);
706
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.org65dad4b2009-04-23 08:48:43 +00001506 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1507 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.org870a0b62008-11-04 11:43:05 +00001508 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001509 default_target->Branch(ne);
1510 frame_->EmitPush(r0);
1511 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1512 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001513
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001514 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001515 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001516 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001517 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1518 if (Smi::IsValid(-min_index)) {
1519 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1520 } else {
1521 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1522 __ add(r0, r0, Operand(Smi::FromInt(1)));
1523 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001524 } else {
1525 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1526 }
1527 }
1528 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001529 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001530 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001531 default_target->Branch(ge);
1532 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001533 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001534
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001535 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1536
1537 // If there was a default case among the case labels, we need to
1538 // emit code to jump to it from the default target used for failure
1539 // to hit the jump table.
1540 if (default_label != NULL) {
1541 if (has_valid_frame()) {
1542 node->break_target()->Jump();
1543 }
1544 setup_default.Bind();
1545 frame_->MergeTo(start_frame);
1546 __ b(default_label);
1547 DeleteFrame();
1548 }
1549 if (node->break_target()->is_linked()) {
1550 node->break_target()->Bind();
1551 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001552}
1553
1554
ager@chromium.org7c537e22008-10-16 08:43:32 +00001555void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556#ifdef DEBUG
1557 int original_height = frame_->height();
1558#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001559 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001561 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001562 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001564 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001565 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1567 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001568 }
1569
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001570 JumpTarget next_test;
1571 JumpTarget fall_through;
1572 JumpTarget default_entry;
1573 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 ZoneList<CaseClause*>* cases = node->cases();
1575 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577
1578 for (int i = 0; i < length; i++) {
1579 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001581 // Remember the default clause and compile it at the end.
1582 default_clause = clause;
1583 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 }
1585
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586 Comment cmnt(masm_, "[ Case clause");
1587 // Compile the test.
1588 next_test.Bind();
1589 next_test.Unuse();
1590 // Duplicate TOS.
1591 __ ldr(r0, frame_->Top());
1592 frame_->EmitPush(r0);
1593 LoadAndSpill(clause->label());
1594 Comparison(eq, true);
1595 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001596
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 // Before entering the body from the test, remove the switch value from
1598 // the stack.
1599 frame_->Drop();
1600
1601 // Label the body so that fall through is enabled.
1602 if (i > 0 && cases->at(i - 1)->is_default()) {
1603 default_exit.Bind();
1604 } else {
1605 fall_through.Bind();
1606 fall_through.Unuse();
1607 }
1608 VisitStatementsAndSpill(clause->statements());
1609
1610 // If control flow can fall through from the body, jump to the next body
1611 // or the end of the statement.
1612 if (frame_ != NULL) {
1613 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1614 default_entry.Jump();
1615 } else {
1616 fall_through.Jump();
1617 }
1618 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 }
1620
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001621 // The final "test" removes the switch value.
1622 next_test.Bind();
1623 frame_->Drop();
1624
1625 // If there is a default clause, compile it.
1626 if (default_clause != NULL) {
1627 Comment cmnt(masm_, "[ Default clause");
1628 default_entry.Bind();
1629 VisitStatementsAndSpill(default_clause->statements());
1630 // If control flow can fall out of the default and there is a case after
1631 // it, jup to that case's body.
1632 if (frame_ != NULL && default_exit.is_bound()) {
1633 default_exit.Jump();
1634 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001635 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001637 if (fall_through.is_linked()) {
1638 fall_through.Bind();
1639 }
1640
1641 if (node->break_target()->is_linked()) {
1642 node->break_target()->Bind();
1643 }
1644 node->break_target()->Unuse();
1645 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646}
1647
1648
ager@chromium.org7c537e22008-10-16 08:43:32 +00001649void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001650#ifdef DEBUG
1651 int original_height = frame_->height();
1652#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001653 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001655 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001656 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001658 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1659 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1661 if (node->cond() == NULL) {
1662 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1663 info = ALWAYS_TRUE;
1664 } else {
1665 Literal* lit = node->cond()->AsLiteral();
1666 if (lit != NULL) {
1667 if (lit->IsTrue()) {
1668 info = ALWAYS_TRUE;
1669 } else if (lit->IsFalse()) {
1670 info = ALWAYS_FALSE;
1671 }
1672 }
1673 }
1674
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001675 switch (node->type()) {
1676 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001677 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679 // Label the top of the loop for the backward CFG edge. If the test
1680 // is always true we can use the continue target, and if the test is
1681 // always false there is no need.
1682 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001683 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001684 node->continue_target()->Bind();
1685 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001686 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 } else {
1688 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001689 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 body.Bind();
1691 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001694 VisitAndSpill(node->body());
1695
1696 // Compile the test.
1697 if (info == ALWAYS_TRUE) {
1698 if (has_valid_frame()) {
1699 // If control can fall off the end of the body, jump back to the
1700 // top.
1701 node->continue_target()->Jump();
1702 }
1703 } else if (info == ALWAYS_FALSE) {
1704 // If we have a continue in the body, we only have to bind its jump
1705 // target.
1706 if (node->continue_target()->is_linked()) {
1707 node->continue_target()->Bind();
1708 }
1709 } else {
1710 ASSERT(info == DONT_KNOW);
1711 // We have to compile the test expression if it can be reached by
1712 // control flow falling out of the body or via continue.
1713 if (node->continue_target()->is_linked()) {
1714 node->continue_target()->Bind();
1715 }
1716 if (has_valid_frame()) {
1717 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1718 &body, node->break_target(), true);
1719 if (has_valid_frame()) {
1720 // A invalid frame here indicates that control did not
1721 // fall out of the test expression.
1722 Branch(true, &body);
1723 }
1724 }
1725 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001727 }
1728
1729 case LoopStatement::WHILE_LOOP: {
1730 // If the test is never true and has no side effects there is no need
1731 // to compile the test or body.
1732 if (info == ALWAYS_FALSE) break;
1733
1734 // Label the top of the loop with the continue target for the backward
1735 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001736 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001737 node->continue_target()->Bind();
1738
1739 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001740 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001741 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1742 &body, node->break_target(), true);
1743 if (has_valid_frame()) {
1744 // A NULL frame indicates that control did not fall out of the
1745 // test expression.
1746 Branch(false, node->break_target());
1747 }
1748 if (has_valid_frame() || body.is_linked()) {
1749 body.Bind();
1750 }
1751 }
1752
1753 if (has_valid_frame()) {
1754 CheckStack(); // TODO(1222600): ignore if body contains calls.
1755 VisitAndSpill(node->body());
1756
1757 // If control flow can fall out of the body, jump back to the top.
1758 if (has_valid_frame()) {
1759 node->continue_target()->Jump();
1760 }
1761 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001763 }
1764
1765 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001766 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767
1768 if (node->init() != NULL) {
1769 VisitAndSpill(node->init());
1770 }
1771
1772 // There is no need to compile the test or body.
1773 if (info == ALWAYS_FALSE) break;
1774
1775 // If there is no update statement, label the top of the loop with the
1776 // continue target, otherwise with the loop target.
1777 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001778 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 node->continue_target()->Bind();
1780 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001781 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 loop.Bind();
1783 }
1784
1785 // If the test is always true, there is no need to compile it.
1786 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001787 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1789 &body, node->break_target(), true);
1790 if (has_valid_frame()) {
1791 Branch(false, node->break_target());
1792 }
1793 if (has_valid_frame() || body.is_linked()) {
1794 body.Bind();
1795 }
1796 }
1797
1798 if (has_valid_frame()) {
1799 CheckStack(); // TODO(1222600): ignore if body contains calls.
1800 VisitAndSpill(node->body());
1801
1802 if (node->next() == NULL) {
1803 // If there is no update statement and control flow can fall out
1804 // of the loop, jump directly to the continue label.
1805 if (has_valid_frame()) {
1806 node->continue_target()->Jump();
1807 }
1808 } else {
1809 // If there is an update statement and control flow can reach it
1810 // via falling out of the body of the loop or continuing, we
1811 // compile the update statement.
1812 if (node->continue_target()->is_linked()) {
1813 node->continue_target()->Bind();
1814 }
1815 if (has_valid_frame()) {
1816 // Record source position of the statement as this code which is
1817 // after the code for the body actually belongs to the loop
1818 // statement and not the body.
1819 CodeForStatementPosition(node);
1820 VisitAndSpill(node->next());
1821 loop.Jump();
1822 }
1823 }
1824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001826 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 }
1828
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829 if (node->break_target()->is_linked()) {
1830 node->break_target()->Bind();
1831 }
1832 node->continue_target()->Unuse();
1833 node->break_target()->Unuse();
1834 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835}
1836
1837
ager@chromium.org7c537e22008-10-16 08:43:32 +00001838void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001839#ifdef DEBUG
1840 int original_height = frame_->height();
1841#endif
1842 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001843 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001847 JumpTarget primitive;
1848 JumpTarget jsobject;
1849 JumpTarget fixed_array;
1850 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1851 JumpTarget end_del_check;
1852 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853
1854 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001855 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856
1857 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1858 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864
1865 // Stack layout in body:
1866 // [iteration counter (Smi)]
1867 // [length of array]
1868 // [FixedArray]
1869 // [Map or 0]
1870 // [Object]
1871
1872 // Check if enumerable is already a JSObject
1873 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 primitive.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1876 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001877 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 primitive.Bind();
1881 frame_->EmitPush(r0);
1882 Result arg_count = allocator_->Allocate(r0);
1883 ASSERT(arg_count.is_valid());
1884 __ mov(arg_count.reg(), Operand(0));
1885 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001887 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001889 frame_->EmitPush(r0); // duplicate the object being enumerated
1890 frame_->EmitPush(r0);
1891 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
1893 // If we got a Map, we can do a fast modification check.
1894 // Otherwise, we got a FixedArray, and we have to do a slow check.
1895 __ mov(r2, Operand(r0));
1896 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1897 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
1900 // Get enum cache
1901 __ mov(r1, Operand(r0));
1902 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1903 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1904 __ ldr(r2,
1905 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1906
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 frame_->EmitPush(r0); // map
1908 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001909 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001912 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001913 frame_->EmitPush(r0);
1914 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001916 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001918 frame_->EmitPush(r1); // insert 0 in place of Map
1919 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920
1921 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001922 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001924 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001925 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927
1928 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001930 // sp[0] : index
1931 // sp[1] : array/enum cache length
1932 // sp[2] : array or enum cache
1933 // sp[3] : 0 or map
1934 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 // Grab the current frame's height for the break and continue
1936 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001937 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1938 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001940 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1941 __ ldr(r1, frame_->ElementAt(1)); // load the length
1942 __ cmp(r0, Operand(r1)); // compare to the array length
1943 node->break_target()->Branch(hs);
1944
1945 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001948 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1950 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1951
1952 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 // Check if this (still) matches the map of the enumerable.
1955 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1958 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960
1961 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001962 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1963 frame_->EmitPush(r0);
1964 frame_->EmitPush(r3); // push entry
1965 Result arg_count_register = allocator_->Allocate(r0);
1966 ASSERT(arg_count_register.is_valid());
1967 __ mov(arg_count_register.reg(), Operand(1));
1968 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1969 CALL_JS,
1970 &arg_count_register,
1971 2);
1972 __ mov(r3, Operand(result.reg()));
1973 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974
1975 // If the property has been removed while iterating, we just skip it.
1976 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 end_del_check.Bind();
1980 // Store the entry in the 'each' expression and take another spin in the
1981 // loop. r3: i'th entry of the enum cache (or string there of)
1982 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 { Reference each(this, node->each());
1984 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001985 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986 __ ldr(r0, frame_->ElementAt(each.size()));
1987 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001988 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001989 // If the reference was to a slot we rely on the convenient property
1990 // that it doesn't matter whether a value (eg, r3 pushed above) is
1991 // right on top of or right underneath a zero-sized reference.
1992 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001993 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001994 // It's safe to pop the value lying on top of the reference before
1995 // unloading the reference itself (which preserves the top of stack,
1996 // ie, now the topmost value of the non-zero sized reference), since
1997 // we will discard the top of stack after unloading the reference
1998 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002000 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 }
2002 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00002003 // Discard the i'th entry pushed above or else the remainder of the
2004 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 frame_->Drop();
2006
2007 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 VisitAndSpill(node->body());
2010
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002011 // Next. Reestablish a spilled frame in case we are coming here via
2012 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002014 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 frame_->EmitPop(r0);
2016 __ add(r0, r0, Operand(Smi::FromInt(1)));
2017 frame_->EmitPush(r0);
2018 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002020 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2021 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002023 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024
2025 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 exit.Bind();
2027 node->continue_target()->Unuse();
2028 node->break_target()->Unuse();
2029 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030}
2031
2032
ager@chromium.org7c537e22008-10-16 08:43:32 +00002033void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034#ifdef DEBUG
2035 int original_height = frame_->height();
2036#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002037 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002041 JumpTarget try_block;
2042 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
2048 // Store the caught exception in the catch variable.
2049 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002050 ASSERT(ref.is_slot());
2051 // Here we make use of the convenient property that it doesn't matter
2052 // whether a value is immediately on top of or underneath a zero-sized
2053 // reference.
2054 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 }
2056
2057 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002058 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002060 VisitStatementsAndSpill(node->catch_block()->statements());
2061 if (frame_ != NULL) {
2062 exit.Jump();
2063 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064
2065
2066 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002067 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2070 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002072 // Shadow the labels for all escapes from the try block, including
2073 // returns. During shadowing, the original label is hidden as the
2074 // LabelShadow and operations on the original actually affect the
2075 // shadowing label.
2076 //
2077 // We should probably try to unify the escaping labels and the return
2078 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 int nof_escapes = node->escaping_targets()->length();
2080 List<ShadowTarget*> shadows(1 + nof_escapes);
2081
2082 // Add the shadow target for the function return.
2083 static const int kReturnShadowIndex = 0;
2084 shadows.Add(new ShadowTarget(&function_return_));
2085 bool function_return_was_shadowed = function_return_is_shadowed_;
2086 function_return_is_shadowed_ = true;
2087 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2088
2089 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002091 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 }
2093
2094 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002095 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096
2097 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002098 // After shadowing stops, the original labels are unshadowed and the
2099 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002100 bool has_unlinks = false;
2101 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002103 has_unlinks = has_unlinks || shadows[i]->is_linked();
2104 }
2105 function_return_is_shadowed_ = function_return_was_shadowed;
2106
2107 // Get an external reference to the handler address.
2108 ExternalReference handler_address(Top::k_handler_address);
2109
2110 // The next handler address is at kNextIndex in the stack.
2111 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2112 // If we can fall off the end of the try block, unlink from try chain.
2113 if (has_valid_frame()) {
2114 __ ldr(r1, frame_->ElementAt(kNextIndex));
2115 __ mov(r3, Operand(handler_address));
2116 __ str(r1, MemOperand(r3));
2117 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2118 if (has_unlinks) {
2119 exit.Jump();
2120 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 }
2122
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002123 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002124 // jumped to. Deallocate each shadow target.
2125 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002127 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 shadows[i]->Bind();
2129 // Because we can be jumping here (to spilled code) from unspilled
2130 // code, we need to reestablish a spilled frame at this block.
2131 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 // Reload sp from the top handler, because some statements that we
2134 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 // The stack pointer was restored to just below the code slot
2138 // (the topmost slot) in the handler.
2139 frame_->Forget(frame_->height() - handler_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002141 // kNextIndex is off by one because the code slot has already
2142 // been dropped.
2143 __ ldr(r1, frame_->ElementAt(kNextIndex - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002145 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002146 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002148 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2149 frame_->PrepareForReturn();
2150 }
2151 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 }
2153 }
2154
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002155 exit.Bind();
2156 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157}
2158
2159
ager@chromium.org7c537e22008-10-16 08:43:32 +00002160void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161#ifdef DEBUG
2162 int original_height = frame_->height();
2163#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002164 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
2168 // State: Used to keep track of reason for entering the finally
2169 // block. Should probably be extended to hold information for
2170 // break/continue from within the try block.
2171 enum { FALLING, THROWING, JUMPING };
2172
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002173 JumpTarget try_block;
2174 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002178 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 // In case of thrown exceptions, this is where we continue.
2180 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002181 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182
2183 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002184 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002186 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2187 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002189 // Shadow the labels for all escapes from the try block, including
2190 // returns. Shadowing hides the original label as the LabelShadow and
2191 // operations on the original actually affect the shadowing label.
2192 //
2193 // We should probably try to unify the escaping labels and the return
2194 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002195 int nof_escapes = node->escaping_targets()->length();
2196 List<ShadowTarget*> shadows(1 + nof_escapes);
2197
2198 // Add the shadow target for the function return.
2199 static const int kReturnShadowIndex = 0;
2200 shadows.Add(new ShadowTarget(&function_return_));
2201 bool function_return_was_shadowed = function_return_is_shadowed_;
2202 function_return_is_shadowed_ = true;
2203 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2204
2205 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002207 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 }
2209
2210 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002213 // Stop the introduced shadowing and count the number of required unlinks.
2214 // After shadowing stops, the original labels are unshadowed and the
2215 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 shadows[i]->StopShadowing();
2219 if (shadows[i]->is_linked()) nof_unlinks++;
2220 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 // Get an external reference to the handler address.
2224 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 // The next handler address is at kNextIndex in the stack.
2227 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2228 // If we can fall off the end of the try block, unlink from the try
2229 // chain and set the state on the frame to FALLING.
2230 if (has_valid_frame()) {
2231 __ ldr(r1, frame_->ElementAt(kNextIndex));
2232 __ mov(r3, Operand(handler_address));
2233 __ str(r1, MemOperand(r3));
2234 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2235
2236 // Fake a top of stack value (unneeded when FALLING) and set the
2237 // state in r2, then jump around the unlink blocks if any.
2238 __ mov(r0, Operand(Factory::undefined_value()));
2239 frame_->EmitPush(r0);
2240 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2241 if (nof_unlinks > 0) {
2242 finally_block.Jump();
2243 }
2244 }
2245
2246 // Generate code to unlink and set the state for the (formerly)
2247 // shadowing targets that have been jumped to.
2248 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 // If we have come from the shadowed return, the return value is
2251 // in (a non-refcounted reference to) r0. We must preserve it
2252 // until it is pushed.
2253 //
2254 // Because we can be jumping here (to spilled code) from
2255 // unspilled code, we need to reestablish a spilled frame at
2256 // this block.
2257 shadows[i]->Bind();
2258 frame_->SpillAll();
2259
2260 // Reload sp from the top handler, because some statements that
2261 // we break from (eg, for...in) may have left stuff on the
2262 // stack.
2263 __ mov(r3, Operand(handler_address));
2264 __ ldr(sp, MemOperand(r3));
2265 // The stack pointer was restored to the address slot in the handler.
2266 ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize);
2267 frame_->Forget(frame_->height() - handler_height + 1);
2268
2269 // Unlink this handler and drop it from the frame. The next
2270 // handler address is now on top of the frame.
2271 frame_->EmitPop(r1);
2272 __ str(r1, MemOperand(r3));
2273 // The top (code) and the second (handler) slot have both been
2274 // dropped already.
2275 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2);
2276
2277 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002278 // If this label shadowed the function return, materialize the
2279 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002281 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002282 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002283 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002284 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 }
2286 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 if (--nof_unlinks > 0) {
2288 // If this is not the last unlink block, jump around the next.
2289 finally_block.Jump();
2290 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 }
2292 }
2293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002297 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002298 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002299
2300 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301 // and the state - while evaluating the finally block.
2302 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002306 if (has_valid_frame()) {
2307 // Restore state and return value or faked TOS.
2308 frame_->EmitPop(r2);
2309 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 }
2311
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002312 // Generate code to jump to the right destination for all used
2313 // formerly shadowing targets. Deallocate each shadow target.
2314 for (int i = 0; i < shadows.length(); i++) {
2315 if (has_valid_frame() && shadows[i]->is_bound()) {
2316 JumpTarget* original = shadows[i]->other_target();
2317 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2318 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002319 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 skip.Branch(ne);
2321 frame_->PrepareForReturn();
2322 original->Jump();
2323 skip.Bind();
2324 } else {
2325 original->Branch(eq);
2326 }
2327 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002330 if (has_valid_frame()) {
2331 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002332 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002333 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2334 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002336 // Rethrow exception.
2337 frame_->EmitPush(r0);
2338 frame_->CallRuntime(Runtime::kReThrow, 1);
2339
2340 // Done.
2341 exit.Bind();
2342 }
2343 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344}
2345
2346
ager@chromium.org7c537e22008-10-16 08:43:32 +00002347void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002348#ifdef DEBUG
2349 int original_height = frame_->height();
2350#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002351 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002354#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002356#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002357 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359}
2360
2361
ager@chromium.org7c537e22008-10-16 08:43:32 +00002362void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002363 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 ASSERT(boilerplate->IsBoilerplate());
2365
2366 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002367 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369
2370 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 frame_->EmitPush(cp);
2372 frame_->CallRuntime(Runtime::kNewClosure, 2);
2373 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374}
2375
2376
ager@chromium.org7c537e22008-10-16 08:43:32 +00002377void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002378#ifdef DEBUG
2379 int original_height = frame_->height();
2380#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002381 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 Comment cmnt(masm_, "[ FunctionLiteral");
2383
2384 // Build the function boilerplate and instantiate it.
2385 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002386 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002387 if (HasStackOverflow()) {
2388 ASSERT(frame_->height() == original_height);
2389 return;
2390 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002392 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393}
2394
2395
ager@chromium.org7c537e22008-10-16 08:43:32 +00002396void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002398#ifdef DEBUG
2399 int original_height = frame_->height();
2400#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002401 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2403 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002404 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405}
2406
2407
ager@chromium.org7c537e22008-10-16 08:43:32 +00002408void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002409#ifdef DEBUG
2410 int original_height = frame_->height();
2411#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002412 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002414 JumpTarget then;
2415 JumpTarget else_;
2416 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002417 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2418 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002420 then.Bind();
2421 LoadAndSpill(node->then_expression(), typeof_state());
2422 exit.Jump();
2423 else_.Bind();
2424 LoadAndSpill(node->else_expression(), typeof_state());
2425 exit.Bind();
2426 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427}
2428
2429
ager@chromium.org7c537e22008-10-16 08:43:32 +00002430void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002431 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002432 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002433 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002435 JumpTarget slow;
2436 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002437
2438 // Generate fast-case code for variables that might be shadowed by
2439 // eval-introduced variables. Eval is used a lot without
2440 // introducing variables. In those cases, we do not want to
2441 // perform a runtime call for all variables in the scope
2442 // containing the eval.
2443 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2444 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002445 // If there was no control flow to slow, we can exit early.
2446 if (!slow.is_linked()) {
2447 frame_->EmitPush(r0);
2448 return;
2449 }
2450
2451 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002452
2453 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2454 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2455 // Only generate the fast case for locals that rewrite to slots.
2456 // This rules out argument loads.
2457 if (potential_slot != NULL) {
2458 __ ldr(r0,
2459 ContextSlotOperandCheckExtensions(potential_slot,
2460 r1,
2461 r2,
2462 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002463 if (potential_slot->var()->mode() == Variable::CONST) {
2464 __ cmp(r0, Operand(Factory::the_hole_value()));
2465 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2466 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002468 // ContextSlotOperandCheckExtensions so we have to jump around
2469 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002471 }
2472 }
2473
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002474 slow.Bind();
2475 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002476 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478
ager@chromium.org7c537e22008-10-16 08:43:32 +00002479 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002480 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002481 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002484
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002485 done.Bind();
2486 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487
2488 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002489 // Note: We would like to keep the assert below, but it fires because of
2490 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002491 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002493 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002494 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002495 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002496 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002497 // Const slots may contain 'the hole' value (the constant hasn't been
2498 // initialized yet) which needs to be converted into the 'undefined'
2499 // value.
2500 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002501 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002502 __ cmp(r0, Operand(Factory::the_hole_value()));
2503 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 }
2506 }
2507}
2508
2509
ager@chromium.org381abbb2009-02-25 13:23:22 +00002510void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2511 TypeofState typeof_state,
2512 Register tmp,
2513 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002514 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002515 // Check that no extension objects have been created by calls to
2516 // eval from the current scope to the global scope.
2517 Register context = cp;
2518 Scope* s = scope();
2519 while (s != NULL) {
2520 if (s->num_heap_slots() > 0) {
2521 if (s->calls_eval()) {
2522 // Check that extension is NULL.
2523 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2524 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002525 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002526 }
2527 // Load next context in chain.
2528 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2529 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2530 context = tmp;
2531 }
2532 // If no outer scope calls eval, we do not need to check more
2533 // context extensions.
2534 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2535 s = s->outer_scope();
2536 }
2537
2538 if (s->is_eval_scope()) {
2539 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002540 if (!context.is(tmp)) {
2541 __ mov(tmp, Operand(context));
2542 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002543 __ bind(&next);
2544 // Terminate at global context.
2545 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2546 __ cmp(tmp2, Operand(Factory::global_context_map()));
2547 __ b(eq, &fast);
2548 // Check that extension is NULL.
2549 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2550 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002551 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002552 // Load next context in chain.
2553 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2554 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2555 __ b(&next);
2556 __ bind(&fast);
2557 }
2558
2559 // All extension objects were empty and it is safe to use a global
2560 // load IC call.
2561 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2562 // Load the global object.
2563 LoadGlobal();
2564 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565 Result name = allocator_->Allocate(r2);
2566 ASSERT(name.is_valid()); // We are in spilled code.
2567 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002568 // Call IC stub.
2569 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002571 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002573 }
2574
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002575 // Drop the global object. The result is in r0.
2576 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002577}
2578
2579
ager@chromium.org7c537e22008-10-16 08:43:32 +00002580void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002581#ifdef DEBUG
2582 int original_height = frame_->height();
2583#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002584 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002585 Comment cmnt(masm_, "[ Slot");
2586 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002587 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002588}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
ager@chromium.org7c537e22008-10-16 08:43:32 +00002590
2591void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592#ifdef DEBUG
2593 int original_height = frame_->height();
2594#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002595 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002596 Comment cmnt(masm_, "[ VariableProxy");
2597
2598 Variable* var = node->var();
2599 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002600 if (expr != NULL) {
2601 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002603 ASSERT(var->is_global());
2604 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002605 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002607 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608}
2609
2610
ager@chromium.org7c537e22008-10-16 08:43:32 +00002611void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002612#ifdef DEBUG
2613 int original_height = frame_->height();
2614#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002615 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002617 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002618 frame_->EmitPush(r0);
2619 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620}
2621
2622
ager@chromium.org7c537e22008-10-16 08:43:32 +00002623void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002624#ifdef DEBUG
2625 int original_height = frame_->height();
2626#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002627 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 Comment cmnt(masm_, "[ RexExp Literal");
2629
2630 // Retrieve the literal array and check the allocated entry.
2631
2632 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002633 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634
2635 // Load the literals array of the function.
2636 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2637
2638 // Load the literal at the ast saved index.
2639 int literal_offset =
2640 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2641 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2642
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002643 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646
2647 // If the entry is undefined we call the runtime system to computed
2648 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002649 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002650 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002652 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002654 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655 frame_->EmitPush(r0);
2656 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002657 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 frame_->EmitPush(r2);
2662 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663}
2664
2665
2666// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002667// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668// Each created boilerplate is stored in the JSFunction and they are
2669// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002672 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002673 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 private:
2679 ObjectLiteral* node_;
2680};
2681
2682
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683void DeferredObjectLiteral::Generate() {
2684 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002686 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002689 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002691 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002692 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002694 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002695 __ push(r0);
2696 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2697 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699}
2700
2701
ager@chromium.org7c537e22008-10-16 08:43:32 +00002702void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002703#ifdef DEBUG
2704 int original_height = frame_->height();
2705#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002706 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 Comment cmnt(masm_, "[ ObjectLiteral");
2708
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002709 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710
2711 // Retrieve the literal array and check the allocated entry.
2712
2713 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002714 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715
2716 // Load the literals array of the function.
2717 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2718
2719 // Load the literal at the ast saved index.
2720 int literal_offset =
2721 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2722 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2723
2724 // Check whether we need to materialize the object literal boilerplate.
2725 // If so, jump to the deferred code.
2726 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002727 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002728 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729
2730 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002731 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002734 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2735 if (node->depth() == 1) {
2736 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2737 }
2738 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002739 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002740 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741
2742 for (int i = 0; i < node->properties()->length(); i++) {
2743 ObjectLiteral::Property* property = node->properties()->at(i);
2744 Literal* key = property->key();
2745 Expression* value = property->value();
2746 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002747 case ObjectLiteral::Property::CONSTANT:
2748 break;
2749 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2750 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2751 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 case ObjectLiteral::Property::COMPUTED: // fall through
2753 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0); // dup the result
2755 LoadAndSpill(key);
2756 LoadAndSpill(value);
2757 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002758 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002759 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 break;
2761 }
2762 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002763 frame_->EmitPush(r0);
2764 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002765 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002766 frame_->EmitPush(r0);
2767 LoadAndSpill(value);
2768 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002769 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770 break;
2771 }
2772 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 frame_->EmitPush(r0);
2774 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002775 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002776 frame_->EmitPush(r0);
2777 LoadAndSpill(value);
2778 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002779 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 break;
2781 }
2782 }
2783 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 ASSERT(frame_->height() == original_height + 1);
2785}
2786
2787
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002788// This deferred code stub will be used for creating the boilerplate
2789// by calling Runtime_CreateArrayLiteralBoilerplate.
2790// Each created boilerplate is stored in the JSFunction and they are
2791// therefore context dependent.
2792class DeferredArrayLiteral: public DeferredCode {
2793 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002794 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002795 set_comment("[ DeferredArrayLiteral");
2796 }
2797
2798 virtual void Generate();
2799
2800 private:
2801 ArrayLiteral* node_;
2802};
2803
2804
2805void DeferredArrayLiteral::Generate() {
2806 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002807
2808 // If the entry is undefined we call the runtime system to computed
2809 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002810 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002811 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002812 // Literal index (1).
2813 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002814 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002815 // Constant properties (2).
2816 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002817 __ push(r0);
2818 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2819 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002820 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002821}
2822
2823
ager@chromium.org7c537e22008-10-16 08:43:32 +00002824void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002825#ifdef DEBUG
2826 int original_height = frame_->height();
2827#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002828 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002830
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002831 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002832
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002833 // Retrieve the literal array and check the allocated entry.
2834
2835 // Load the function of this activation.
2836 __ ldr(r1, frame_->Function());
2837
2838 // Load the literals array of the function.
2839 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2840
2841 // Load the literal at the ast saved index.
2842 int literal_offset =
2843 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2844 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2845
2846 // Check whether we need to materialize the object literal boilerplate.
2847 // If so, jump to the deferred code.
2848 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002849 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002850 deferred->BindExit();
2851
2852 // Push the object literal boilerplate.
2853 frame_->EmitPush(r2);
2854
2855 // Clone the boilerplate object.
2856 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2857 if (node->depth() == 1) {
2858 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2859 }
2860 frame_->CallRuntime(clone_function_id, 1);
2861 frame_->EmitPush(r0); // save the result
2862 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002863
2864 // Generate code to set the elements in the array that are not
2865 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866 for (int i = 0; i < node->values()->length(); i++) {
2867 Expression* value = node->values()->at(i);
2868
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002869 // If value is a literal the property value is already set in the
2870 // boilerplate object.
2871 if (value->AsLiteral() != NULL) continue;
2872 // If value is a materialized literal the property value is already set
2873 // in the boilerplate object if it is simple.
2874 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002876 // The property must be set by generated code.
2877 LoadAndSpill(value);
2878 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002880 // Fetch the object literal.
2881 __ ldr(r1, frame_->Top());
2882 // Get the elements array.
2883 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002884
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002885 // Write to the indexed properties array.
2886 int offset = i * kPointerSize + Array::kHeaderSize;
2887 __ str(r0, FieldMemOperand(r1, offset));
2888
2889 // Update the write barrier for the array address.
2890 __ mov(r3, Operand(offset));
2891 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894}
2895
2896
ager@chromium.org32912102009-01-16 10:38:43 +00002897void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002898#ifdef DEBUG
2899 int original_height = frame_->height();
2900#endif
2901 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002902 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002903 // Call runtime routine to allocate the catch extension object and
2904 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002905 Comment cmnt(masm_, "[ CatchExtensionObject");
2906 LoadAndSpill(node->key());
2907 LoadAndSpill(node->value());
2908 Result result =
2909 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2910 frame_->EmitPush(result.reg());
2911 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002912}
2913
2914
ager@chromium.org7c537e22008-10-16 08:43:32 +00002915void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002916#ifdef DEBUG
2917 int original_height = frame_->height();
2918#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002919 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002921 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002922
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002923 { Reference target(this, node->target());
2924 if (target.is_illegal()) {
2925 // Fool the virtual frame into thinking that we left the assignment's
2926 // value on the frame.
2927 __ mov(r0, Operand(Smi::FromInt(0)));
2928 frame_->EmitPush(r0);
2929 ASSERT(frame_->height() == original_height + 1);
2930 return;
2931 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002932
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002933 if (node->op() == Token::ASSIGN ||
2934 node->op() == Token::INIT_VAR ||
2935 node->op() == Token::INIT_CONST) {
2936 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002939 // +=, *= and similar binary assignments.
2940 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2942 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002943 bool overwrite =
2944 (node->value()->AsBinaryOperation() != NULL &&
2945 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002947 SmiOperation(node->binary_op(),
2948 literal->handle(),
2949 false,
2950 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002951 frame_->EmitPush(r0);
2952
2953 } else {
2954 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002955 GenericBinaryOperation(node->binary_op(),
2956 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002957 frame_->EmitPush(r0);
2958 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002961 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2962 if (var != NULL &&
2963 (var->mode() == Variable::CONST) &&
2964 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2965 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002966
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002968 CodeForSourcePosition(node->position());
2969 if (node->op() == Token::INIT_CONST) {
2970 // Dynamic constant initializations must use the function context
2971 // and initialize the actual constant declared. Dynamic variable
2972 // initializations are simply assignments and use SetValue.
2973 target.SetValue(CONST_INIT);
2974 } else {
2975 target.SetValue(NOT_CONST_INIT);
2976 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 }
2978 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002979 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980}
2981
2982
ager@chromium.org7c537e22008-10-16 08:43:32 +00002983void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002984#ifdef DEBUG
2985 int original_height = frame_->height();
2986#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002987 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988 Comment cmnt(masm_, "[ Throw");
2989
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002990 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002991 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992 frame_->CallRuntime(Runtime::kThrow, 1);
2993 frame_->EmitPush(r0);
2994 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995}
2996
2997
ager@chromium.org7c537e22008-10-16 08:43:32 +00002998void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002999#ifdef DEBUG
3000 int original_height = frame_->height();
3001#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003002 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003004
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003005 { Reference property(this, node);
3006 property.GetValueAndSpill(typeof_state());
3007 }
3008 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009}
3010
3011
ager@chromium.org7c537e22008-10-16 08:43:32 +00003012void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013#ifdef DEBUG
3014 int original_height = frame_->height();
3015#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003016 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 Comment cmnt(masm_, "[ Call");
3018
3019 ZoneList<Expression*>* args = node->arguments();
3020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003021 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022 // Standard function call.
3023
3024 // Check if the function is a variable or a property.
3025 Expression* function = node->expression();
3026 Variable* var = function->AsVariableProxy()->AsVariable();
3027 Property* property = function->AsProperty();
3028
3029 // ------------------------------------------------------------------------
3030 // Fast-case: Use inline caching.
3031 // ---
3032 // According to ECMA-262, section 11.2.3, page 44, the function to call
3033 // must be resolved after the arguments have been evaluated. The IC code
3034 // automatically handles this by loading the arguments before the function
3035 // is resolved in cache misses (this also holds for megamorphic calls).
3036 // ------------------------------------------------------------------------
3037
3038 if (var != NULL && !var->is_this() && var->is_global()) {
3039 // ----------------------------------
3040 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3041 // ----------------------------------
3042
3043 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003044 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003045 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003046
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003047 // Pass the global object as the receiver and let the IC stub
3048 // patch the stack to use the global proxy as 'this' in the
3049 // invoked function.
3050 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003051
3052 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003053 int arg_count = args->length();
3054 for (int i = 0; i < arg_count; i++) {
3055 LoadAndSpill(args->at(i));
3056 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057
3058 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003059 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3060 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003061 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003062 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3063 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003064 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003066 frame_->Drop();
3067 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068
3069 } else if (var != NULL && var->slot() != NULL &&
3070 var->slot()->type() == Slot::LOOKUP) {
3071 // ----------------------------------
3072 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3073 // ----------------------------------
3074
3075 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003076 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003077 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003078 frame_->EmitPush(r0);
3079 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080 // r0: slot value; r1: receiver
3081
3082 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003083 frame_->EmitPush(r0); // function
3084 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085
3086 // Call the function.
3087 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089
3090 } else if (property != NULL) {
3091 // Check if the key is a literal string.
3092 Literal* literal = property->key()->AsLiteral();
3093
3094 if (literal != NULL && literal->handle()->IsSymbol()) {
3095 // ------------------------------------------------------------------
3096 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3097 // ------------------------------------------------------------------
3098
3099 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003100 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003101 frame_->EmitPush(r0);
3102 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103
3104 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003105 int arg_count = args->length();
3106 for (int i = 0; i < arg_count; i++) {
3107 LoadAndSpill(args->at(i));
3108 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109
3110 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003111 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3112 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003113 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003114 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003115 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003116
3117 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003118 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003119
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003120 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121
3122 } else {
3123 // -------------------------------------------
3124 // JavaScript example: 'array[index](1, 2, 3)'
3125 // -------------------------------------------
3126
3127 // Load the function to call from the property through a reference.
3128 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003129 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130
3131 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 if (property->is_synthetic()) {
3133 LoadGlobalReceiver(r0);
3134 } else {
3135 __ ldr(r0, frame_->ElementAt(ref.size()));
3136 frame_->EmitPush(r0);
3137 }
3138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003139 // Call the function.
3140 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003141 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142 }
3143
3144 } else {
3145 // ----------------------------------
3146 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3147 // ----------------------------------
3148
3149 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003150 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003151
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003152 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003153 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155 // Call the function.
3156 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003157 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003158 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003159 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160}
3161
3162
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003163void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003164#ifdef DEBUG
3165 int original_height = frame_->height();
3166#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003167 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003168 Comment cmnt(masm_, "[ CallEval");
3169
3170 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3171 // the function we need to call and the receiver of the call.
3172 // Then we call the resolved function using the given arguments.
3173
3174 ZoneList<Expression*>* args = node->arguments();
3175 Expression* function = node->expression();
3176
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003177 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003178
3179 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003180 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003181 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003182 frame_->EmitPush(r2); // Slot for receiver
3183 int arg_count = args->length();
3184 for (int i = 0; i < arg_count; i++) {
3185 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003186 }
3187
3188 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003189 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3190 frame_->EmitPush(r1);
3191 if (arg_count > 0) {
3192 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3193 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003194 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003195 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003196 }
3197
3198 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003199 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003200
3201 // Touch up stack with the right values for the function and the receiver.
3202 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003203 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003204 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003205 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003206
3207 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003208 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003209
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003210 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3211 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003212 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003213
3214 __ ldr(cp, frame_->Context());
3215 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003216 frame_->Drop();
3217 frame_->EmitPush(r0);
3218 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003219}
3220
3221
ager@chromium.org7c537e22008-10-16 08:43:32 +00003222void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223#ifdef DEBUG
3224 int original_height = frame_->height();
3225#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003226 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003228 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229
3230 // According to ECMA-262, section 11.2.2, page 44, the function
3231 // expression in new calls must be evaluated before the
3232 // arguments. This is different from ordinary calls, where the
3233 // actual function to call is resolved after the arguments have been
3234 // evaluated.
3235
3236 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003237 // receiver. There is no need to use the global proxy here because
3238 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003239 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003240 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241
3242 // Push the arguments ("left-to-right") on the stack.
3243 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003244 int arg_count = args->length();
3245 for (int i = 0; i < arg_count; i++) {
3246 LoadAndSpill(args->at(i));
3247 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248
mads.s.ager31e71382008-08-13 09:32:07 +00003249 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003250 Result num_args = allocator_->Allocate(r0);
3251 ASSERT(num_args.is_valid());
3252 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003254 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 Result function = allocator_->Allocate(r1);
3256 ASSERT(function.is_valid());
3257 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003258
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 // Call the construct call builtin that handles allocation and
3260 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003261 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003262 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3263 Result result = frame_->CallCodeObject(ic,
3264 RelocInfo::CONSTRUCT_CALL,
3265 &num_args,
3266 &function,
3267 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003268
3269 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003270 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003271 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272}
3273
3274
ager@chromium.org7c537e22008-10-16 08:43:32 +00003275void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003276 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003278 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279 LoadAndSpill(args->at(0));
3280 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003281 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003283 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003284 // It is a heap object - get map.
3285 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3286 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003287 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288 __ cmp(r1, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003289 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 // Load the value.
3291 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 leave.Bind();
3293 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294}
3295
3296
ager@chromium.org7c537e22008-10-16 08:43:32 +00003297void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003298 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003301 LoadAndSpill(args->at(0)); // Load the object.
3302 LoadAndSpill(args->at(1)); // Load the value.
3303 frame_->EmitPop(r0); // r0 contains value
3304 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 // if (object->IsSmi()) return object.
3306 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003308 // It is a heap object - get map.
3309 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3310 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3311 // if (!object->IsJSValue()) return object.
3312 __ cmp(r2, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314 // Store the value.
3315 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3316 // Update the write barrier.
3317 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3318 __ RecordWrite(r1, r2, r3);
3319 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003320 leave.Bind();
3321 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322}
3323
3324
ager@chromium.org7c537e22008-10-16 08:43:32 +00003325void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003326 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003328 LoadAndSpill(args->at(0));
3329 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003330 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003331 cc_reg_ = eq;
3332}
3333
3334
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003335void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003336 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003337 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3338 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003339#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003340 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003341 LoadAndSpill(args->at(1));
3342 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003343 __ CallRuntime(Runtime::kLog, 2);
3344 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003345#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003346 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003348}
3349
3350
ager@chromium.org7c537e22008-10-16 08:43:32 +00003351void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003352 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003353 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003354 LoadAndSpill(args->at(0));
3355 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003356 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3357 cc_reg_ = eq;
3358}
3359
3360
kasper.lund7276f142008-07-30 08:49:36 +00003361// This should generate code that performs a charCodeAt() call or returns
3362// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3363// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003364void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003365 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003366 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003367 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003368 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003369}
3370
3371
ager@chromium.org7c537e22008-10-16 08:43:32 +00003372void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003373 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003374 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003375 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003376 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003377 // We need the CC bits to come out as not_equal in the case where the
3378 // object is a smi. This can't be done with the usual test opcode so
3379 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003380 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003381 __ and_(r1, r0, Operand(kSmiTagMask));
3382 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003383 answer.Branch(ne);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003384 // It is a heap object - get the map.
3385 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3386 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3387 // Check if the object is a JS array or not.
3388 __ cmp(r1, Operand(JS_ARRAY_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003389 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390 cc_reg_ = eq;
3391}
3392
3393
ager@chromium.org7c537e22008-10-16 08:43:32 +00003394void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003395 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 ASSERT(args->length() == 0);
3397
mads.s.ager31e71382008-08-13 09:32:07 +00003398 // Seed the result with the formal parameters count, which will be used
3399 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003400 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3401
3402 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003403 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003404 frame_->CallStub(&stub, 0);
3405 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406}
3407
3408
ager@chromium.org7c537e22008-10-16 08:43:32 +00003409void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003410 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 ASSERT(args->length() == 1);
3412
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003413 // Satisfy contract with ArgumentsAccessStub:
3414 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003415 LoadAndSpill(args->at(0));
3416 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003417 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418
3419 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003420 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003421 frame_->CallStub(&stub, 0);
3422 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003423}
3424
3425
ager@chromium.org7c537e22008-10-16 08:43:32 +00003426void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003427 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003428 ASSERT(args->length() == 2);
3429
3430 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003431 LoadAndSpill(args->at(0));
3432 LoadAndSpill(args->at(1));
3433 frame_->EmitPop(r0);
3434 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003435 __ cmp(r0, Operand(r1));
3436 cc_reg_ = eq;
3437}
3438
3439
ager@chromium.org7c537e22008-10-16 08:43:32 +00003440void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003441#ifdef DEBUG
3442 int original_height = frame_->height();
3443#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003444 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003445 if (CheckForInlineRuntimeCall(node)) {
3446 ASSERT((has_cc() && frame_->height() == original_height) ||
3447 (!has_cc() && frame_->height() == original_height + 1));
3448 return;
3449 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450
3451 ZoneList<Expression*>* args = node->arguments();
3452 Comment cmnt(masm_, "[ CallRuntime");
3453 Runtime::Function* function = node->function();
3454
ager@chromium.org41826e72009-03-30 13:30:57 +00003455 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003456 // Prepare stack for calling JS runtime function.
3457 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003458 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003459 // Push the builtins object found in the current global object.
3460 __ ldr(r1, GlobalObject());
3461 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003462 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003463 }
mads.s.ager31e71382008-08-13 09:32:07 +00003464
ager@chromium.org41826e72009-03-30 13:30:57 +00003465 // Push the arguments ("left-to-right").
3466 int arg_count = args->length();
3467 for (int i = 0; i < arg_count; i++) {
3468 LoadAndSpill(args->at(i));
3469 }
mads.s.ager31e71382008-08-13 09:32:07 +00003470
ager@chromium.org41826e72009-03-30 13:30:57 +00003471 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003472 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003473 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3474 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003475 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003476 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003477 frame_->Drop();
3478 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003479 } else {
3480 // Call the C runtime function.
3481 frame_->CallRuntime(function, arg_count);
3482 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003484 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485}
3486
3487
ager@chromium.org7c537e22008-10-16 08:43:32 +00003488void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003489#ifdef DEBUG
3490 int original_height = frame_->height();
3491#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003492 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493 Comment cmnt(masm_, "[ UnaryOperation");
3494
3495 Token::Value op = node->op();
3496
3497 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003498 LoadConditionAndSpill(node->expression(),
3499 NOT_INSIDE_TYPEOF,
3500 false_target(),
3501 true_target(),
3502 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003503 cc_reg_ = NegateCondition(cc_reg_);
3504
3505 } else if (op == Token::DELETE) {
3506 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003507 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003509 LoadAndSpill(property->obj());
3510 LoadAndSpill(property->key());
3511 Result arg_count = allocator_->Allocate(r0);
3512 ASSERT(arg_count.is_valid());
3513 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3514 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003515
mads.s.ager31e71382008-08-13 09:32:07 +00003516 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003517 Slot* slot = variable->slot();
3518 if (variable->is_global()) {
3519 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003520 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003521 frame_->EmitPush(r0);
3522 Result arg_count = allocator_->Allocate(r0);
3523 ASSERT(arg_count.is_valid());
3524 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3525 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526
3527 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3528 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003529 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003530 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003531 frame_->EmitPush(r0);
3532 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003534 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003535 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003536 frame_->EmitPush(r0);
3537 Result arg_count = allocator_->Allocate(r0);
3538 ASSERT(arg_count.is_valid());
3539 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3540 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541
mads.s.ager31e71382008-08-13 09:32:07 +00003542 } else {
3543 // Default: Result of deleting non-global, not dynamically
3544 // introduced variables is false.
3545 __ mov(r0, Operand(Factory::false_value()));
3546 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547
3548 } else {
3549 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003550 LoadAndSpill(node->expression()); // may have side-effects
3551 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003552 __ mov(r0, Operand(Factory::true_value()));
3553 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003554 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555
3556 } else if (op == Token::TYPEOF) {
3557 // Special case for loading the typeof expression; see comment on
3558 // LoadTypeofExpression().
3559 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003560 frame_->CallRuntime(Runtime::kTypeof, 1);
3561 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562
3563 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 LoadAndSpill(node->expression());
3565 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003566 switch (op) {
3567 case Token::NOT:
3568 case Token::DELETE:
3569 case Token::TYPEOF:
3570 UNREACHABLE(); // handled above
3571 break;
3572
3573 case Token::SUB: {
3574 UnarySubStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576 break;
3577 }
3578
3579 case Token::BIT_NOT: {
3580 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003581 JumpTarget smi_label;
3582 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003586 frame_->EmitPush(r0);
3587 Result arg_count = allocator_->Allocate(r0);
3588 ASSERT(arg_count.is_valid());
3589 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3590 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003592 continue_label.Jump();
3593 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594 __ mvn(r0, Operand(r0));
3595 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003596 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003597 break;
3598 }
3599
3600 case Token::VOID:
3601 // since the stack top is cached in r0, popping and then
3602 // pushing a value can be done by just writing to r0.
3603 __ mov(r0, Operand(Factory::undefined_value()));
3604 break;
3605
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003606 case Token::ADD: {
3607 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003608 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003609 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003610 continue_label.Branch(eq);
3611 frame_->EmitPush(r0);
3612 Result arg_count = allocator_->Allocate(r0);
3613 ASSERT(arg_count.is_valid());
3614 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3615 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3616 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003618 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003619 default:
3620 UNREACHABLE();
3621 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003622 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003623 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003624 ASSERT((has_cc() && frame_->height() == original_height) ||
3625 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626}
3627
3628
ager@chromium.org7c537e22008-10-16 08:43:32 +00003629void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003630#ifdef DEBUG
3631 int original_height = frame_->height();
3632#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003633 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003634 Comment cmnt(masm_, "[ CountOperation");
3635
3636 bool is_postfix = node->is_postfix();
3637 bool is_increment = node->op() == Token::INC;
3638
3639 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3640 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3641
3642 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003643 if (is_postfix) {
3644 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003645 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003646 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003647
3648 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003649 if (target.is_illegal()) {
3650 // Spoof the virtual frame to have the expected height (one higher
3651 // than on entry).
3652 if (!is_postfix) {
3653 __ mov(r0, Operand(Smi::FromInt(0)));
3654 frame_->EmitPush(r0);
3655 }
3656 ASSERT(frame_->height() == original_height + 1);
3657 return;
3658 }
3659 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3660 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003662 JumpTarget slow;
3663 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664
3665 // Load the value (1) into register r1.
3666 __ mov(r1, Operand(Smi::FromInt(1)));
3667
3668 // Check for smi operand.
3669 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003670 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671
3672 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003673 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003674 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003675 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003676
3677 // Perform optimistic increment/decrement.
3678 if (is_increment) {
3679 __ add(r0, r0, Operand(r1), SetCC);
3680 } else {
3681 __ sub(r0, r0, Operand(r1), SetCC);
3682 }
3683
3684 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003685 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686
3687 // Revert optimistic increment/decrement.
3688 if (is_increment) {
3689 __ sub(r0, r0, Operand(r1));
3690 } else {
3691 __ add(r0, r0, Operand(r1));
3692 }
3693
3694 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003695 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003696 {
3697 // Convert the operand to a number.
3698 frame_->EmitPush(r0);
3699 Result arg_count = allocator_->Allocate(r0);
3700 ASSERT(arg_count.is_valid());
3701 __ mov(arg_count.reg(), Operand(0));
3702 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3703 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003705 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003706 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 }
3708
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003709 // Compute the new value.
3710 __ mov(r1, Operand(Smi::FromInt(1)));
3711 frame_->EmitPush(r0);
3712 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003714 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003716 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 }
3718
3719 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720 exit.Bind();
3721 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003722 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003723 }
3724
3725 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 if (is_postfix) frame_->EmitPop(r0);
3727 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003728}
3729
3730
ager@chromium.org7c537e22008-10-16 08:43:32 +00003731void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003732#ifdef DEBUG
3733 int original_height = frame_->height();
3734#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003735 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 Comment cmnt(masm_, "[ BinaryOperation");
3737 Token::Value op = node->op();
3738
3739 // According to ECMA-262 section 11.11, page 58, the binary logical
3740 // operators must yield the result of one of the two expressions
3741 // before any ToBoolean() conversions. This means that the value
3742 // produced by a && or || operator is not necessarily a boolean.
3743
3744 // NOTE: If the left hand side produces a materialized value (not in
3745 // the CC register), we force the right hand side to do the
3746 // same. This is necessary because we may have to branch to the exit
3747 // after evaluating the left hand side (due to the shortcut
3748 // semantics), but the compiler must (statically) know if the result
3749 // of compiling the binary operation is materialized or not.
3750
3751 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003752 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003753 LoadConditionAndSpill(node->left(),
3754 NOT_INSIDE_TYPEOF,
3755 &is_true,
3756 false_target(),
3757 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 if (has_cc()) {
3759 Branch(false, false_target());
3760
3761 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 is_true.Bind();
3763 LoadConditionAndSpill(node->right(),
3764 NOT_INSIDE_TYPEOF,
3765 true_target(),
3766 false_target(),
3767 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003768
3769 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003770 JumpTarget pop_and_continue;
3771 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003773 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003774 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003775 // Avoid popping the result if it converts to 'false' using the
3776 // standard ToBoolean() conversion as described in ECMA-262,
3777 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003778 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779 Branch(false, &exit);
3780
3781 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003782 pop_and_continue.Bind();
3783 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784
3785 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003786 is_true.Bind();
3787 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788
3789 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003790 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791 }
3792
3793 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003794 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003795 LoadConditionAndSpill(node->left(),
3796 NOT_INSIDE_TYPEOF,
3797 true_target(),
3798 &is_false,
3799 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800 if (has_cc()) {
3801 Branch(true, true_target());
3802
3803 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003804 is_false.Bind();
3805 LoadConditionAndSpill(node->right(),
3806 NOT_INSIDE_TYPEOF,
3807 true_target(),
3808 false_target(),
3809 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003810
3811 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003812 JumpTarget pop_and_continue;
3813 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003815 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003816 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 // Avoid popping the result if it converts to 'true' using the
3818 // standard ToBoolean() conversion as described in ECMA-262,
3819 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003820 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003821 Branch(true, &exit);
3822
3823 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003824 pop_and_continue.Bind();
3825 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003826
3827 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003828 is_false.Bind();
3829 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830
3831 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003832 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 }
3834
3835 } else {
3836 // Optimize for the case where (at least) one of the expressions
3837 // is a literal small integer.
3838 Literal* lliteral = node->left()->AsLiteral();
3839 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003840 // NOTE: The code below assumes that the slow cases (calls to runtime)
3841 // never return a constant/immutable object.
3842 bool overwrite_left =
3843 (node->left()->AsBinaryOperation() != NULL &&
3844 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3845 bool overwrite_right =
3846 (node->right()->AsBinaryOperation() != NULL &&
3847 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003848
3849 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003850 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003851 SmiOperation(node->op(),
3852 rliteral->handle(),
3853 false,
3854 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003855
3856 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003857 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003858 SmiOperation(node->op(),
3859 lliteral->handle(),
3860 true,
3861 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862
3863 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003864 OverwriteMode overwrite_mode = NO_OVERWRITE;
3865 if (overwrite_left) {
3866 overwrite_mode = OVERWRITE_LEFT;
3867 } else if (overwrite_right) {
3868 overwrite_mode = OVERWRITE_RIGHT;
3869 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003870 LoadAndSpill(node->left());
3871 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003872 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003874 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003876 ASSERT((has_cc() && frame_->height() == original_height) ||
3877 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878}
3879
3880
ager@chromium.org7c537e22008-10-16 08:43:32 +00003881void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003882#ifdef DEBUG
3883 int original_height = frame_->height();
3884#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003885 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003886 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003887 frame_->EmitPush(r0);
3888 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889}
3890
3891
ager@chromium.org7c537e22008-10-16 08:43:32 +00003892void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003893#ifdef DEBUG
3894 int original_height = frame_->height();
3895#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003896 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003897 Comment cmnt(masm_, "[ CompareOperation");
3898
3899 // Get the expressions from the node.
3900 Expression* left = node->left();
3901 Expression* right = node->right();
3902 Token::Value op = node->op();
3903
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003904 // To make null checks efficient, we check if either left or right is the
3905 // literal 'null'. If so, we optimize the code by inlining a null check
3906 // instead of calling the (very) general runtime routine for checking
3907 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003909 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003910 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003911 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003912 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3913 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003915 LoadAndSpill(left_is_null ? right : left);
3916 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 __ cmp(r0, Operand(Factory::null_value()));
3918
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003919 // The 'null' value is only equal to 'undefined' if using non-strict
3920 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003923
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003925 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003928 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003930 // It can be an undetectable object.
3931 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3932 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3933 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3934 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 }
3936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003938 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939 return;
3940 }
3941 }
3942
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003943 // To make typeof testing for natives implemented in JavaScript really
3944 // efficient, we generate special code for expressions of the form:
3945 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003946 UnaryOperation* operation = left->AsUnaryOperation();
3947 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3948 (operation != NULL && operation->op() == Token::TYPEOF) &&
3949 (right->AsLiteral() != NULL &&
3950 right->AsLiteral()->handle()->IsString())) {
3951 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3952
mads.s.ager31e71382008-08-13 09:32:07 +00003953 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003955 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956
3957 if (check->Equals(Heap::number_symbol())) {
3958 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003959 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3961 __ cmp(r1, Operand(Factory::heap_number_map()));
3962 cc_reg_ = eq;
3963
3964 } else if (check->Equals(Heap::string_symbol())) {
3965 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003966 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967
3968 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3969
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003970 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003971 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3972 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3973 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003974 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975
3976 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3977 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3978 cc_reg_ = lt;
3979
3980 } else if (check->Equals(Heap::boolean_symbol())) {
3981 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003982 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983 __ cmp(r1, Operand(Factory::false_value()));
3984 cc_reg_ = eq;
3985
3986 } else if (check->Equals(Heap::undefined_symbol())) {
3987 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003988 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989
3990 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003991 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003993 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003994 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3995 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3996 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3997 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3998
3999 cc_reg_ = eq;
4000
4001 } else if (check->Equals(Heap::function_symbol())) {
4002 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004003 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4005 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4006 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4007 cc_reg_ = eq;
4008
4009 } else if (check->Equals(Heap::object_symbol())) {
4010 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004011 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012
4013 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4014 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004015 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004017 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4019 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4020 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022
4023 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4024 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004025 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4027 cc_reg_ = le;
4028
4029 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004030 // Uncommon case: typeof testing against a string literal that is
4031 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004034 ASSERT(!has_valid_frame() ||
4035 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036 return;
4037 }
4038
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004039 LoadAndSpill(left);
4040 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004041 switch (op) {
4042 case Token::EQ:
4043 Comparison(eq, false);
4044 break;
4045
4046 case Token::LT:
4047 Comparison(lt);
4048 break;
4049
4050 case Token::GT:
4051 Comparison(gt);
4052 break;
4053
4054 case Token::LTE:
4055 Comparison(le);
4056 break;
4057
4058 case Token::GTE:
4059 Comparison(ge);
4060 break;
4061
4062 case Token::EQ_STRICT:
4063 Comparison(eq, true);
4064 break;
4065
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004066 case Token::IN: {
4067 Result arg_count = allocator_->Allocate(r0);
4068 ASSERT(arg_count.is_valid());
4069 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4070 Result result = frame_->InvokeBuiltin(Builtins::IN,
4071 CALL_JS,
4072 &arg_count,
4073 2);
4074 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004076 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078 case Token::INSTANCEOF: {
4079 Result arg_count = allocator_->Allocate(r0);
4080 ASSERT(arg_count.is_valid());
4081 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4082 Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF,
4083 CALL_JS,
4084 &arg_count,
4085 2);
4086 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004087 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004088 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090
4091 default:
4092 UNREACHABLE();
4093 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004094 ASSERT((has_cc() && frame_->height() == original_height) ||
4095 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004096}
4097
4098
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004099#ifdef DEBUG
4100bool CodeGenerator::HasValidEntryRegisters() { return true; }
4101#endif
4102
4103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004105#define __ ACCESS_MASM(masm)
4106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107
ager@chromium.org7c537e22008-10-16 08:43:32 +00004108Handle<String> Reference::GetName() {
4109 ASSERT(type_ == NAMED);
4110 Property* property = expression_->AsProperty();
4111 if (property == NULL) {
4112 // Global variable reference treated as a named property reference.
4113 VariableProxy* proxy = expression_->AsVariableProxy();
4114 ASSERT(proxy->AsVariable() != NULL);
4115 ASSERT(proxy->AsVariable()->is_global());
4116 return proxy->name();
4117 } else {
4118 Literal* raw_name = property->key()->AsLiteral();
4119 ASSERT(raw_name != NULL);
4120 return Handle<String>(String::cast(*raw_name->handle()));
4121 }
4122}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004123
ager@chromium.org7c537e22008-10-16 08:43:32 +00004124
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004125void Reference::GetValueAndSpill(TypeofState typeof_state) {
4126 ASSERT(cgen_->in_spilled_code());
4127 cgen_->set_in_spilled_code(false);
4128 GetValue(typeof_state);
4129 cgen_->frame()->SpillAll();
4130 cgen_->set_in_spilled_code(true);
4131}
4132
4133
ager@chromium.org7c537e22008-10-16 08:43:32 +00004134void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004135 ASSERT(!cgen_->in_spilled_code());
4136 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004137 ASSERT(!is_illegal());
4138 ASSERT(!cgen_->has_cc());
4139 MacroAssembler* masm = cgen_->masm();
4140 Property* property = expression_->AsProperty();
4141 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004142 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004143 }
4144
4145 switch (type_) {
4146 case SLOT: {
4147 Comment cmnt(masm, "[ Load from Slot");
4148 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4149 ASSERT(slot != NULL);
4150 cgen_->LoadFromSlot(slot, typeof_state);
4151 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004152 }
4153
ager@chromium.org7c537e22008-10-16 08:43:32 +00004154 case NAMED: {
4155 // TODO(1241834): Make sure that this it is safe to ignore the
4156 // distinction between expressions in a typeof and not in a typeof. If
4157 // there is a chance that reference errors can be thrown below, we
4158 // must distinguish between the two kinds of loads (typeof expression
4159 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004160 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004161 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004162 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004163 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004164 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4165 // Setup the name register.
4166 Result name_reg = cgen_->allocator()->Allocate(r2);
4167 ASSERT(name_reg.is_valid());
4168 __ mov(name_reg.reg(), Operand(name));
4169 ASSERT(var == NULL || var->is_global());
4170 RelocInfo::Mode rmode = (var == NULL)
4171 ? RelocInfo::CODE_TARGET
4172 : RelocInfo::CODE_TARGET_CONTEXT;
4173 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4174 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004175 break;
4176 }
4177
4178 case KEYED: {
4179 // TODO(1241834): Make sure that this it is safe to ignore the
4180 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004181
4182 // TODO(181): Implement inlined version of array indexing once
4183 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004185 Comment cmnt(masm, "[ Load from keyed Property");
4186 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004187 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004188 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004189 ASSERT(var == NULL || var->is_global());
4190 RelocInfo::Mode rmode = (var == NULL)
4191 ? RelocInfo::CODE_TARGET
4192 : RelocInfo::CODE_TARGET_CONTEXT;
4193 Result answer = frame->CallCodeObject(ic, rmode, 0);
4194 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004195 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004196 }
4197
4198 default:
4199 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004200 }
4201}
4202
4203
ager@chromium.org7c537e22008-10-16 08:43:32 +00004204void Reference::SetValue(InitState init_state) {
4205 ASSERT(!is_illegal());
4206 ASSERT(!cgen_->has_cc());
4207 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004208 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004209 Property* property = expression_->AsProperty();
4210 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004211 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004212 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004213
ager@chromium.org7c537e22008-10-16 08:43:32 +00004214 switch (type_) {
4215 case SLOT: {
4216 Comment cmnt(masm, "[ Store to Slot");
4217 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4218 ASSERT(slot != NULL);
4219 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004220 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004221
ager@chromium.org7c537e22008-10-16 08:43:32 +00004222 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004223 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004225 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004226
ager@chromium.org7c537e22008-10-16 08:43:32 +00004227 if (init_state == CONST_INIT) {
4228 // Same as the case for a normal store, but ignores attribute
4229 // (e.g. READ_ONLY) of context slot so that we can initialize
4230 // const properties (introduced via eval("const foo = (some
4231 // expr);")). Also, uses the current function context instead of
4232 // the top context.
4233 //
4234 // Note that we must declare the foo upon entry of eval(), via a
4235 // context slot declaration, but we cannot initialize it at the
4236 // same time, because the const declaration may be at the end of
4237 // the eval code (sigh...) and the const variable may have been
4238 // used before (where its value is 'undefined'). Thus, we can only
4239 // do the initialization when we actually encounter the expression
4240 // and when the expression operands are defined and valid, and
4241 // thus we need the split into 2 operations: declaration of the
4242 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004245 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004246 }
4247 // Storing a variable must keep the (new) value on the expression
4248 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004249 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004250
ager@chromium.org7c537e22008-10-16 08:43:32 +00004251 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004252 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004253
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004254 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004255 if (init_state == CONST_INIT) {
4256 ASSERT(slot->var()->mode() == Variable::CONST);
4257 // Only the first const initialization must be executed (the slot
4258 // still contains 'the hole' value). When the assignment is
4259 // executed, the code is identical to a normal store (see below).
4260 Comment cmnt(masm, "[ Init const");
4261 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4262 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004263 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004264 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004265
ager@chromium.org7c537e22008-10-16 08:43:32 +00004266 // We must execute the store. Storing a variable must keep the
4267 // (new) value on the stack. This is necessary for compiling
4268 // assignment expressions.
4269 //
4270 // Note: We will reach here even with slot->var()->mode() ==
4271 // Variable::CONST because of const declarations which will
4272 // initialize consts to 'the hole' value and by doing so, end up
4273 // calling this code. r2 may be loaded with context; used below in
4274 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004276 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004277 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004278 if (slot->type() == Slot::CONTEXT) {
4279 // Skip write barrier if the written value is a smi.
4280 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004281 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004282 // r2 is loaded with context when calling SlotOperand above.
4283 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4284 __ mov(r3, Operand(offset));
4285 __ RecordWrite(r2, r3, r1);
4286 }
4287 // If we definitely did not jump over the assignment, we do not need
4288 // to bind the exit label. Doing so can defeat peephole
4289 // optimization.
4290 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004291 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004292 }
4293 }
4294 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004295 }
4296
ager@chromium.org7c537e22008-10-16 08:43:32 +00004297 case NAMED: {
4298 Comment cmnt(masm, "[ Store to named Property");
4299 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004300 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 Handle<String> name(GetName());
4302
4303 Result value = cgen_->allocator()->Allocate(r0);
4304 ASSERT(value.is_valid());
4305 frame->EmitPop(value.reg());
4306
4307 // Setup the name register.
4308 Result property_name = cgen_->allocator()->Allocate(r2);
4309 ASSERT(property_name.is_valid());
4310 __ mov(property_name.reg(), Operand(name));
4311 Result answer = frame->CallCodeObject(ic,
4312 RelocInfo::CODE_TARGET,
4313 &value,
4314 &property_name,
4315 0);
4316 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004317 break;
4318 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004319
ager@chromium.org7c537e22008-10-16 08:43:32 +00004320 case KEYED: {
4321 Comment cmnt(masm, "[ Store to keyed Property");
4322 Property* property = expression_->AsProperty();
4323 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004324 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004325
4326 // Call IC code.
4327 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4328 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004329 Result value = cgen_->allocator()->Allocate(r0);
4330 ASSERT(value.is_valid());
4331 frame->EmitPop(value.reg()); // value
4332 Result result =
4333 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4334 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004335 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004336 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004337
4338 default:
4339 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004340 }
4341}
4342
4343
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004344static void AllocateHeapNumber(
4345 MacroAssembler* masm,
4346 Label* need_gc, // Jump here if young space is full.
4347 Register result_reg, // The tagged address of the new heap number.
4348 Register allocation_top_addr_reg, // A scratch register.
4349 Register scratch2) { // Another scratch register.
4350 ExternalReference allocation_top =
4351 ExternalReference::new_space_allocation_top_address();
4352 ExternalReference allocation_limit =
4353 ExternalReference::new_space_allocation_limit_address();
4354
4355 // allocat := the address of the allocation top variable.
4356 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4357 // result_reg := the old allocation top.
4358 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4359 // scratch2 := the address of the allocation limit.
4360 __ mov(scratch2, Operand(allocation_limit));
4361 // scratch2 := the allocation limit.
4362 __ ldr(scratch2, MemOperand(scratch2));
4363 // result_reg := the new allocation top.
4364 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4365 // Compare new new allocation top and limit.
4366 __ cmp(result_reg, Operand(scratch2));
4367 // Branch if out of space in young generation.
4368 __ b(hi, need_gc);
4369 // Store new allocation top.
4370 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4371 // Tag and adjust back to start of new object.
4372 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4373 // Get heap number map into scratch2.
4374 __ mov(scratch2, Operand(Factory::heap_number_map()));
4375 // Store heap number map in new object.
4376 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4377}
4378
4379
4380// We fall into this code if the operands were Smis, but the result was
4381// not (eg. overflow). We branch into this code (to the not_smi label) if
4382// the operands were not both Smi.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004383static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4384 Label* not_smi,
4385 const Builtins::JavaScript& builtin,
4386 Token::Value operation,
4387 int swi_number,
4388 OverwriteMode mode) {
4389 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004390 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004391 __ push(r1);
4392 __ push(r0);
4393 __ mov(r0, Operand(1)); // Set number of arguments.
4394 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004395
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004396 __ bind(not_smi);
4397 __ tst(r0, Operand(kSmiTagMask));
4398 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4399 __ tst(r1, Operand(kSmiTagMask));
4400 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4401 // Get map of r0 into r2.
4402 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4403 // Get type of r0 into r3.
4404 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4405 __ cmp(r3, Operand(HEAP_NUMBER_TYPE));
4406 __ b(ne, &slow);
4407 // Get type of r1 into r3.
4408 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
4409 // Check they are both the same map (heap number map).
4410 __ cmp(r2, r3);
4411 __ b(ne, &slow);
4412 // Both are doubles.
4413 // Calling convention says that second double is in r2 and r3.
4414 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
4415 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4416
4417 if (mode == NO_OVERWRITE) {
4418 // Get address of new heap number into r5.
4419 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004420 __ push(lr);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004421 __ push(r5);
4422 } else if (mode == OVERWRITE_LEFT) {
4423 __ push(lr);
4424 __ push(r1);
4425 } else {
4426 ASSERT(mode == OVERWRITE_RIGHT);
4427 __ push(lr);
4428 __ push(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004429 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004430 // Calling convention says that first double is in r0 and r1.
4431 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
4432 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4433 // Call C routine that may not cause GC or other trouble.
4434 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
4435#if !defined(__arm__)
4436 // Notify the simulator that we are calling an add routine in C.
4437 __ swi(swi_number);
4438#else
4439 // Actually call the add routine written in C.
4440 __ Call(r5);
4441#endif
4442 // Store answer in the overwritable heap number.
4443 __ pop(r4);
4444#if !defined(__ARM_EABI__) && defined(__arm__)
4445 // Double returned in fp coprocessor register 0 and 1, encoded as register
4446 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
4447 // substract the tag from r4.
4448 __ sub(r5, r4, Operand(kHeapObjectTag));
4449 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
4450#else
4451 // Double returned in fp coprocessor register 0 and 1.
4452 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
4453 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize));
4454#endif
4455 __ mov(r0, Operand(r4));
4456 // And we are done.
4457 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004458}
4459
4460
4461void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4462 // r1 : x
4463 // r0 : y
4464 // result : r0
4465
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004466 // All ops need to know whether we are dealing with two Smis. Set up r2 to
4467 // tell us that.
4468 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4469
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004470 switch (op_) {
4471 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004472 Label not_smi;
4473 // Fast path.
4474 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004475 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004476 __ b(ne, &not_smi);
4477 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
4478 // Return if no overflow.
4479 __ Ret(vc);
4480 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
4481
4482 HandleBinaryOpSlowCases(masm,
4483 &not_smi,
4484 Builtins::ADD,
4485 Token::ADD,
4486 assembler::arm::simulator_fp_add,
4487 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004488 break;
4489 }
4490
4491 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004492 Label not_smi;
4493 // Fast path.
4494 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004495 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004496 __ b(ne, &not_smi);
4497 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
4498 // Return if no overflow.
4499 __ Ret(vc);
4500 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
4501
4502 HandleBinaryOpSlowCases(masm,
4503 &not_smi,
4504 Builtins::SUB,
4505 Token::SUB,
4506 assembler::arm::simulator_fp_sub,
4507 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004508 break;
4509 }
4510
4511 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004512 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004513 ASSERT(kSmiTag == 0); // adjust code below
4514 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004515 __ b(ne, &not_smi);
4516 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004517 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004518 // Do multiplication
4519 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
4520 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004521 __ mov(ip, Operand(r3, ASR, 31));
4522 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4523 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004524 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004525 __ tst(r3, Operand(r3));
4526 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004527 __ Ret(ne);
4528 // Slow case.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004529 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004530
4531 HandleBinaryOpSlowCases(masm,
4532 &not_smi,
4533 Builtins::MUL,
4534 Token::MUL,
4535 assembler::arm::simulator_fp_mul,
4536 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004537 break;
4538 }
4539
4540 case Token::BIT_OR:
4541 case Token::BIT_AND:
4542 case Token::BIT_XOR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004543 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004544 ASSERT(kSmiTag == 0); // adjust code below
4545 __ tst(r2, Operand(kSmiTagMask));
4546 __ b(ne, &slow);
4547 switch (op_) {
4548 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4549 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4550 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
4551 default: UNREACHABLE();
4552 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004553 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004554 __ bind(&slow);
4555 __ push(r1); // restore stack
4556 __ push(r0);
4557 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4558 switch (op_) {
4559 case Token::BIT_OR:
4560 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4561 break;
4562 case Token::BIT_AND:
4563 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4564 break;
4565 case Token::BIT_XOR:
4566 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4567 break;
4568 default:
4569 UNREACHABLE();
4570 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004571 break;
4572 }
4573
4574 case Token::SHL:
4575 case Token::SHR:
4576 case Token::SAR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004577 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004578 ASSERT(kSmiTag == 0); // adjust code below
4579 __ tst(r2, Operand(kSmiTagMask));
4580 __ b(ne, &slow);
4581 // remove tags from operands (but keep sign)
4582 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
4583 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
4584 // use only the 5 least significant bits of the shift count
4585 __ and_(r2, r2, Operand(0x1f));
4586 // perform operation
4587 switch (op_) {
4588 case Token::SAR:
4589 __ mov(r3, Operand(r3, ASR, r2));
4590 // no checks of result necessary
4591 break;
4592
4593 case Token::SHR:
4594 __ mov(r3, Operand(r3, LSR, r2));
4595 // check that the *unsigned* result fits in a smi
4596 // neither of the two high-order bits can be set:
4597 // - 0x80000000: high bit would be lost when smi tagging
4598 // - 0x40000000: this number would convert to negative when
4599 // smi tagging these two cases can only happen with shifts
4600 // by 0 or 1 when handed a valid smi
4601 __ and_(r2, r3, Operand(0xc0000000), SetCC);
4602 __ b(ne, &slow);
4603 break;
4604
4605 case Token::SHL:
4606 __ mov(r3, Operand(r3, LSL, r2));
4607 // check that the *signed* result fits in a smi
4608 __ add(r2, r3, Operand(0x40000000), SetCC);
4609 __ b(mi, &slow);
4610 break;
4611
4612 default: UNREACHABLE();
4613 }
4614 // tag result and store it in r0
4615 ASSERT(kSmiTag == 0); // adjust code below
4616 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004617 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004618 // slow case
4619 __ bind(&slow);
4620 __ push(r1); // restore stack
4621 __ push(r0);
4622 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4623 switch (op_) {
4624 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
4625 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
4626 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
4627 default: UNREACHABLE();
4628 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004629 break;
4630 }
4631
4632 default: UNREACHABLE();
4633 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004634 // This code should be unreachable.
4635 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004636}
4637
4638
4639void StackCheckStub::Generate(MacroAssembler* masm) {
4640 Label within_limit;
4641 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4642 __ ldr(ip, MemOperand(ip));
4643 __ cmp(sp, Operand(ip));
4644 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004645 // Do tail-call to runtime routine. Runtime routines expect at least one
4646 // argument, so give it a Smi.
4647 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004648 __ push(r0);
4649 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4650 __ bind(&within_limit);
4651
4652 __ StubReturn(1);
4653}
4654
4655
4656void UnarySubStub::Generate(MacroAssembler* masm) {
4657 Label undo;
4658 Label slow;
4659 Label done;
4660
4661 // Enter runtime system if the value is not a smi.
4662 __ tst(r0, Operand(kSmiTagMask));
4663 __ b(ne, &slow);
4664
4665 // Enter runtime system if the value of the expression is zero
4666 // to make sure that we switch between 0 and -0.
4667 __ cmp(r0, Operand(0));
4668 __ b(eq, &slow);
4669
4670 // The value of the expression is a smi that is not zero. Try
4671 // optimistic subtraction '0 - value'.
4672 __ rsb(r1, r0, Operand(0), SetCC);
4673 __ b(vs, &slow);
4674
4675 // If result is a smi we are done.
4676 __ tst(r1, Operand(kSmiTagMask));
4677 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4678 __ b(eq, &done);
4679
4680 // Enter runtime system.
4681 __ bind(&slow);
4682 __ push(r0);
4683 __ mov(r0, Operand(0)); // set number of arguments
4684 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4685
4686 __ bind(&done);
4687 __ StubReturn(1);
4688}
4689
4690
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004691void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4692 // r0 holds exception
4693 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4694 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4695 __ ldr(sp, MemOperand(r3));
4696 __ pop(r2); // pop next in chain
4697 __ str(r2, MemOperand(r3));
4698 // restore parameter- and frame-pointer and pop state.
4699 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4700 // Before returning we restore the context from the frame pointer if not NULL.
4701 // The frame pointer is NULL in the exception handler of a JS entry frame.
4702 __ cmp(fp, Operand(0));
4703 // Set cp to NULL if fp is NULL.
4704 __ mov(cp, Operand(0), LeaveCC, eq);
4705 // Restore cp otherwise.
4706 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004707#ifdef DEBUG
4708 if (FLAG_debug_code) {
4709 __ mov(lr, Operand(pc));
4710 }
4711#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004712 __ pop(pc);
4713}
4714
4715
4716void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4717 // Fetch top stack handler.
4718 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4719 __ ldr(r3, MemOperand(r3));
4720
4721 // Unwind the handlers until the ENTRY handler is found.
4722 Label loop, done;
4723 __ bind(&loop);
4724 // Load the type of the current stack handler.
4725 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4726 StackHandlerConstants::kStateOffset;
4727 __ ldr(r2, MemOperand(r3, kStateOffset));
4728 __ cmp(r2, Operand(StackHandler::ENTRY));
4729 __ b(eq, &done);
4730 // Fetch the next handler in the list.
4731 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4732 StackHandlerConstants::kNextOffset;
4733 __ ldr(r3, MemOperand(r3, kNextOffset));
4734 __ jmp(&loop);
4735 __ bind(&done);
4736
4737 // Set the top handler address to next handler past the current ENTRY handler.
4738 __ ldr(r0, MemOperand(r3, kNextOffset));
4739 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4740 __ str(r0, MemOperand(r2));
4741
4742 // Set external caught exception to false.
4743 __ mov(r0, Operand(false));
4744 ExternalReference external_caught(Top::k_external_caught_exception_address);
4745 __ mov(r2, Operand(external_caught));
4746 __ str(r0, MemOperand(r2));
4747
4748 // Set pending exception and r0 to out of memory exception.
4749 Failure* out_of_memory = Failure::OutOfMemoryException();
4750 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4751 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4752 __ str(r0, MemOperand(r2));
4753
4754 // Restore the stack to the address of the ENTRY handler
4755 __ mov(sp, Operand(r3));
4756
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004757 // Stack layout at this point. See also PushTryHandler
4758 // r3, sp -> next handler
4759 // state (ENTRY)
4760 // pp
4761 // fp
4762 // lr
4763
4764 // Discard ENTRY state (r2 is not used), and restore parameter-
4765 // and frame-pointer and pop state.
4766 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004767 // Before returning we restore the context from the frame pointer if not NULL.
4768 // The frame pointer is NULL in the exception handler of a JS entry frame.
4769 __ cmp(fp, Operand(0));
4770 // Set cp to NULL if fp is NULL.
4771 __ mov(cp, Operand(0), LeaveCC, eq);
4772 // Restore cp otherwise.
4773 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004774#ifdef DEBUG
4775 if (FLAG_debug_code) {
4776 __ mov(lr, Operand(pc));
4777 }
4778#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004779 __ pop(pc);
4780}
4781
4782
4783void CEntryStub::GenerateCore(MacroAssembler* masm,
4784 Label* throw_normal_exception,
4785 Label* throw_out_of_memory_exception,
4786 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004787 bool do_gc,
4788 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004789 // r0: result parameter for PerformGC, if any
4790 // r4: number of arguments including receiver (C callee-saved)
4791 // r5: pointer to builtin function (C callee-saved)
4792 // r6: pointer to the first argument (C callee-saved)
4793
4794 if (do_gc) {
4795 // Passing r0.
4796 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4797 }
4798
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004799 ExternalReference scope_depth =
4800 ExternalReference::heap_always_allocate_scope_depth();
4801 if (always_allocate) {
4802 __ mov(r0, Operand(scope_depth));
4803 __ ldr(r1, MemOperand(r0));
4804 __ add(r1, r1, Operand(1));
4805 __ str(r1, MemOperand(r0));
4806 }
4807
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004808 // Call C built-in.
4809 // r0 = argc, r1 = argv
4810 __ mov(r0, Operand(r4));
4811 __ mov(r1, Operand(r6));
4812
4813 // TODO(1242173): To let the GC traverse the return address of the exit
4814 // frames, we need to know where the return address is. Right now,
4815 // we push it on the stack to be able to find it again, but we never
4816 // restore from it in case of changes, which makes it impossible to
4817 // support moving the C entry code stub. This should be fixed, but currently
4818 // this is OK because the CEntryStub gets generated so early in the V8 boot
4819 // sequence that it is not moving ever.
4820 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4821 __ push(lr);
4822#if !defined(__arm__)
4823 // Notify the simulator of the transition to C code.
4824 __ swi(assembler::arm::call_rt_r5);
4825#else /* !defined(__arm__) */
ager@chromium.org41826e72009-03-30 13:30:57 +00004826 __ Jump(r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004827#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004828
4829 if (always_allocate) {
4830 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4831 // though (contain the result).
4832 __ mov(r2, Operand(scope_depth));
4833 __ ldr(r3, MemOperand(r2));
4834 __ sub(r3, r3, Operand(1));
4835 __ str(r3, MemOperand(r2));
4836 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004837
4838 // check for failure result
4839 Label failure_returned;
4840 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4841 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4842 __ add(r2, r0, Operand(1));
4843 __ tst(r2, Operand(kFailureTagMask));
4844 __ b(eq, &failure_returned);
4845
4846 // Exit C frame and return.
4847 // r0:r1: result
4848 // sp: stack pointer
4849 // fp: frame pointer
4850 // pp: caller's parameter pointer pp (restored as C callee-saved)
4851 __ LeaveExitFrame(frame_type);
4852
4853 // check if we should retry or throw exception
4854 Label retry;
4855 __ bind(&failure_returned);
4856 ASSERT(Failure::RETRY_AFTER_GC == 0);
4857 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4858 __ b(eq, &retry);
4859
4860 Label continue_exception;
4861 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4862 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4863 __ b(ne, &continue_exception);
4864
4865 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004866 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004867 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004868 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004869 __ ldr(r0, MemOperand(ip));
4870 __ str(r3, MemOperand(ip));
4871
4872 __ bind(&continue_exception);
4873 // Special handling of out of memory exception.
4874 Failure* out_of_memory = Failure::OutOfMemoryException();
4875 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4876 __ b(eq, throw_out_of_memory_exception);
4877
4878 // Handle normal exception.
4879 __ jmp(throw_normal_exception);
4880
4881 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4882}
4883
4884
4885void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4886 // Called from JavaScript; parameters are on stack as if calling JS function
4887 // r0: number of arguments including receiver
4888 // r1: pointer to builtin function
4889 // fp: frame pointer (restored after C call)
4890 // sp: stack pointer (restored as callee's pp after C call)
4891 // cp: current context (C callee-saved)
4892 // pp: caller's parameter pointer pp (C callee-saved)
4893
4894 // NOTE: Invocations of builtins may return failure objects
4895 // instead of a proper result. The builtin entry handles
4896 // this by performing a garbage collection and retrying the
4897 // builtin once.
4898
4899 StackFrame::Type frame_type = is_debug_break
4900 ? StackFrame::EXIT_DEBUG
4901 : StackFrame::EXIT;
4902
4903 // Enter the exit frame that transitions from JavaScript to C++.
4904 __ EnterExitFrame(frame_type);
4905
4906 // r4: number of arguments (C callee-saved)
4907 // r5: pointer to builtin function (C callee-saved)
4908 // r6: pointer to first argument (C callee-saved)
4909
4910 Label throw_out_of_memory_exception;
4911 Label throw_normal_exception;
4912
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004913 // Call into the runtime system. Collect garbage before the call if
4914 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004915 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004916 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004917 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4918 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004919 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004920 &throw_out_of_memory_exception,
4921 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004922 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004923 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004924
4925 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004926 GenerateCore(masm,
4927 &throw_normal_exception,
4928 &throw_out_of_memory_exception,
4929 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004930 true,
4931 false);
4932
4933 // Do full GC and retry runtime call one final time.
4934 Failure* failure = Failure::InternalError();
4935 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4936 GenerateCore(masm,
4937 &throw_normal_exception,
4938 &throw_out_of_memory_exception,
4939 frame_type,
4940 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004941 true);
4942
4943 __ bind(&throw_out_of_memory_exception);
4944 GenerateThrowOutOfMemory(masm);
4945 // control flow for generated will not return.
4946
4947 __ bind(&throw_normal_exception);
4948 GenerateThrowTOS(masm);
4949}
4950
4951
4952void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4953 // r0: code entry
4954 // r1: function
4955 // r2: receiver
4956 // r3: argc
4957 // [sp+0]: argv
4958
4959 Label invoke, exit;
4960
4961 // Called from C, so do not pop argc and args on exit (preserve sp)
4962 // No need to save register-passed args
4963 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4964 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4965
4966 // Get address of argv, see stm above.
4967 // r0: code entry
4968 // r1: function
4969 // r2: receiver
4970 // r3: argc
4971 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4972 __ ldr(r4, MemOperand(r4)); // argv
4973
4974 // Push a frame with special values setup to mark it as an entry frame.
4975 // r0: code entry
4976 // r1: function
4977 // r2: receiver
4978 // r3: argc
4979 // r4: argv
4980 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4981 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4982 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4983 __ mov(r6, Operand(Smi::FromInt(marker)));
4984 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4985 __ ldr(r5, MemOperand(r5));
4986 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4987
4988 // Setup frame pointer for the frame to be pushed.
4989 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4990
4991 // Call a faked try-block that does the invoke.
4992 __ bl(&invoke);
4993
4994 // Caught exception: Store result (exception) in the pending
4995 // exception field in the JSEnv and return a failure sentinel.
4996 // Coming in here the fp will be invalid because the PushTryHandler below
4997 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004998 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004999 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005000 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005001 __ b(&exit);
5002
5003 // Invoke: Link this frame into the handler chain.
5004 __ bind(&invoke);
5005 // Must preserve r0-r4, r5-r7 are available.
5006 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
5007 // If an exception not caught by another handler occurs, this handler returns
5008 // control to the code after the bl(&invoke) above, which restores all
5009 // kCalleeSaved registers (including cp, pp and fp) to their saved values
5010 // before returning a failure to C.
5011
5012 // Clear any pending exceptions.
5013 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5014 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005015 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005016 __ str(r5, MemOperand(ip));
5017
5018 // Invoke the function by calling through JS entry trampoline builtin.
5019 // Notice that we cannot store a reference to the trampoline code directly in
5020 // this stub, because runtime stubs are not traversed when doing GC.
5021
5022 // Expected registers by Builtins::JSEntryTrampoline
5023 // r0: code entry
5024 // r1: function
5025 // r2: receiver
5026 // r3: argc
5027 // r4: argv
5028 if (is_construct) {
5029 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
5030 __ mov(ip, Operand(construct_entry));
5031 } else {
5032 ExternalReference entry(Builtins::JSEntryTrampoline);
5033 __ mov(ip, Operand(entry));
5034 }
5035 __ ldr(ip, MemOperand(ip)); // deref address
5036
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005037 // Branch and link to JSEntryTrampoline. We don't use the double underscore
5038 // macro for the add instruction because we don't want the coverage tool
5039 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005040 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005041 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005042
5043 // Unlink this frame from the handler chain. When reading the
5044 // address of the next handler, there is no need to use the address
5045 // displacement since the current stack pointer (sp) points directly
5046 // to the stack handler.
5047 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5048 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5049 __ str(r3, MemOperand(ip));
5050 // No need to restore registers
5051 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5052
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005053
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005054 __ bind(&exit); // r0 holds result
5055 // Restore the top frame descriptors from the stack.
5056 __ pop(r3);
5057 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5058 __ str(r3, MemOperand(ip));
5059
5060 // Reset the stack to the callee saved registers.
5061 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5062
5063 // Restore callee-saved registers and return.
5064#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005065 if (FLAG_debug_code) {
5066 __ mov(lr, Operand(pc));
5067 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005068#endif
5069 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5070}
5071
5072
ager@chromium.org7c537e22008-10-16 08:43:32 +00005073void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005074 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005075 Label adaptor;
5076 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5077 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5078 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005079 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005080
ager@chromium.org7c537e22008-10-16 08:43:32 +00005081 // Nothing to do: The formal number of parameters has already been
5082 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00005083 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005084
ager@chromium.org7c537e22008-10-16 08:43:32 +00005085 // Arguments adaptor case: Read the arguments length from the
5086 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005087 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005088 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00005089 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005090}
5091
5092
ager@chromium.org7c537e22008-10-16 08:43:32 +00005093void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5094 // The displacement is the offset of the last parameter (if any)
5095 // relative to the frame pointer.
5096 static const int kDisplacement =
5097 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005098
ager@chromium.org7c537e22008-10-16 08:43:32 +00005099 // Check that the key is a smi.
5100 Label slow;
5101 __ tst(r1, Operand(kSmiTagMask));
5102 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005103
ager@chromium.org7c537e22008-10-16 08:43:32 +00005104 // Check if the calling frame is an arguments adaptor frame.
5105 Label adaptor;
5106 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5107 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5108 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5109 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005110
ager@chromium.org7c537e22008-10-16 08:43:32 +00005111 // Check index against formal parameters count limit passed in
5112 // through register eax. Use unsigned comparison to get negative
5113 // check for free.
5114 __ cmp(r1, r0);
5115 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005116
ager@chromium.org7c537e22008-10-16 08:43:32 +00005117 // Read the argument from the stack and return it.
5118 __ sub(r3, r0, r1);
5119 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5120 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005121 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005122
5123 // Arguments adaptor case: Check index against actual arguments
5124 // limit found in the arguments adaptor frame. Use unsigned
5125 // comparison to get negative check for free.
5126 __ bind(&adaptor);
5127 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5128 __ cmp(r1, r0);
5129 __ b(cs, &slow);
5130
5131 // Read the argument from the adaptor frame and return it.
5132 __ sub(r3, r0, r1);
5133 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5134 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005135 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005136
5137 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5138 // by calling the runtime system.
5139 __ bind(&slow);
5140 __ push(r1);
5141 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5142}
5143
5144
5145void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5146 // Check if the calling frame is an arguments adaptor frame.
5147 Label runtime;
5148 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5149 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5150 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5151 __ b(ne, &runtime);
5152
5153 // Patch the arguments.length and the parameters pointer.
5154 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5155 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5156 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5157 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5158 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5159
5160 // Do the runtime call to allocate the arguments object.
5161 __ bind(&runtime);
5162 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005163}
5164
5165
5166void CallFunctionStub::Generate(MacroAssembler* masm) {
5167 Label slow;
5168 // Get the function to call from the stack.
5169 // function, receiver [, arguments]
5170 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5171
5172 // Check that the function is really a JavaScript function.
5173 // r1: pushed function (to be verified)
5174 __ tst(r1, Operand(kSmiTagMask));
5175 __ b(eq, &slow);
5176 // Get the map of the function object.
5177 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5178 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
5179 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
5180 __ b(ne, &slow);
5181
5182 // Fast-case: Invoke the function now.
5183 // r1: pushed function
5184 ParameterCount actual(argc_);
5185 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5186
5187 // Slow-case: Non-function called.
5188 __ bind(&slow);
5189 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005190 __ mov(r2, Operand(0));
5191 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5192 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5193 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005194}
5195
5196
5197#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005199} } // namespace v8::internal