blob: 5cc4824d537bca3ba550734655fe4a38da49270f [file] [log] [blame]
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000033#include "parser.h"
34#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "scopes.h"
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#define __ ACCESS_MASM(masm_)
43
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000044static void EmitIdenticalObjectComparison(MacroAssembler* masm,
45 Label* slow,
46 Condition cc);
47static void EmitSmiNonsmiComparison(MacroAssembler* masm,
48 Label* rhs_not_nan,
49 Label* slow,
50 bool strict);
51static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
52static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
53
54
55
ager@chromium.orge2902be2009-06-08 12:21:35 +000056// -------------------------------------------------------------------------
57// Platform-specific DeferredCode functions.
58
59void DeferredCode::SaveRegisters() {
60 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
61 int action = registers_[i];
62 if (action == kPush) {
63 __ push(RegisterAllocator::ToRegister(i));
64 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
65 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
66 }
67 }
68}
69
70
71void DeferredCode::RestoreRegisters() {
72 // Restore registers in reverse order due to the stack.
73 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
74 int action = registers_[i];
75 if (action == kPush) {
76 __ pop(RegisterAllocator::ToRegister(i));
77 } else if (action != kIgnore) {
78 action &= ~kSyncedFlag;
79 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
80 }
81 }
82}
83
ager@chromium.org3bf7b912008-11-17 09:09:45 +000084
85// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000086// CodeGenState implementation.
87
ager@chromium.org7c537e22008-10-16 08:43:32 +000088CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000089 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000090 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000091 true_target_(NULL),
92 false_target_(NULL),
93 previous_(NULL) {
94 owner_->set_state(this);
95}
96
97
ager@chromium.org7c537e22008-10-16 08:43:32 +000098CodeGenState::CodeGenState(CodeGenerator* owner,
99 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000100 JumpTarget* true_target,
101 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000102 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000103 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000104 true_target_(true_target),
105 false_target_(false_target),
106 previous_(owner->state()) {
107 owner_->set_state(this);
108}
109
110
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000111CodeGenState::~CodeGenState() {
112 ASSERT(owner_->state() == this);
113 owner_->set_state(previous_);
114}
115
116
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000117// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000118// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
ager@chromium.org7c537e22008-10-16 08:43:32 +0000120CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
121 bool is_eval)
122 : is_eval_(is_eval),
123 script_(script),
124 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 masm_(new MacroAssembler(NULL, buffer_size)),
126 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000127 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000128 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129 cc_reg_(al),
130 state_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000131 function_return_is_shadowed_(false),
132 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135
136// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000139// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140// cp: callee's context
141
ager@chromium.org7c537e22008-10-16 08:43:32 +0000142void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 ZoneList<Statement*>* body = fun->body();
144
145 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000146 ASSERT(scope_ == NULL);
147 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000148 ASSERT(allocator_ == NULL);
149 RegisterAllocator register_allocator(this);
150 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000151 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000152 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000153 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000155 {
156 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000158 // Entry:
159 // Stack: receiver, arguments
160 // lr: return address
161 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000163 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000165 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000166 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // tos: code slot
168#ifdef DEBUG
169 if (strlen(FLAG_stop_at) > 0 &&
170 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000172 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 }
174#endif
175
176 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000177 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000178 // Initialize the function return target after the locals are set
179 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000183 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000184 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 // Allocate local context.
186 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000187 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000188 frame_->EmitPush(r0);
189 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000190
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000191#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000192 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000193 __ cmp(r0, Operand(cp));
194 verified_true.Branch(eq);
195 __ stop("NewContext: r0 is expected to be the same as cp");
196 verified_true.Bind();
197#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000199 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 }
201
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000202 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 // 1) only needed if we have a context
204 // 2) no need to recompute context ptr every single time
205 // 3) don't copy parameter operand code from SlotOperand!
206 {
207 Comment cmnt2(masm_, "[ copy context parameters into .context");
208
209 // Note that iteration order is relevant here! If we have the same
210 // parameter twice (e.g., function (x, y, x)), and that parameter
211 // needs to be copied into the context, it must be the last argument
212 // passed to the parameter that needs to be copied. This is a rare
213 // case so we don't check for it, instead we rely on the copying
214 // order: such a parameter is copied repeatedly into the same
215 // context location and thus the last value is what is seen inside
216 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000217 for (int i = 0; i < scope_->num_parameters(); i++) {
218 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 Slot* slot = par->slot();
220 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000221 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000222 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 // Loads r2 with context; used below in RecordWrite.
224 __ str(r1, SlotOperand(slot, r2));
225 // Load the offset into r3.
226 int slot_offset =
227 FixedArray::kHeaderSize + slot->index() * kPointerSize;
228 __ mov(r3, Operand(slot_offset));
229 __ RecordWrite(r2, r3, r1);
230 }
231 }
232 }
233
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000234 // Store the arguments object. This must happen after context
235 // initialization because the arguments object may be stored in the
236 // context.
237 if (scope_->arguments() != NULL) {
238 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000240 { Reference shadow_ref(this, scope_->arguments_shadow());
241 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000242 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000243 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000244 // The receiver is below the arguments, the return address,
245 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000247 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000248 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000249 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000250 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000251 frame_->CallStub(&stub, 3);
252 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000253 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000254 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000255 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000257 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 }
259
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000260 // Generate code to 'execute' declarations and initialize functions
261 // (source elements). In case of an illegal redeclaration we need to
262 // handle that instead of processing the declarations.
263 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000265 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 } else {
267 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000268 ProcessDeclarations(scope_->declarations());
269 // Bail out if a stack-overflow exception occurred when processing
270 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000271 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 }
273
mads.s.ager31e71382008-08-13 09:32:07 +0000274 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000275 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000276 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000277 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 CheckStack();
279
280 // Compile the body of the function in a vanilla state. Don't
281 // bother compiling all the code if the scope has an illegal
282 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000283 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 Comment cmnt(masm_, "[ function body");
285#ifdef DEBUG
286 bool is_builtin = Bootstrapper::IsActive();
287 bool should_trace =
288 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000289 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000290 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000291 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000292 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000294 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 }
297
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000298 // Generate the return sequence if necessary.
299 if (frame_ != NULL || function_return_.is_linked()) {
300 // exit
301 // r0: result
302 // sp: stack pointer
303 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000304 // cp: callee's context
305 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000306
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 function_return_.Bind();
308 if (FLAG_trace) {
309 // Push the return value on the stack as the parameter.
310 // Runtime::TraceExit returns the parameter as it is.
311 frame_->EmitPush(r0);
312 frame_->CallRuntime(Runtime::kTraceExit, 1);
313 }
314
315 // Tear down the frame which will restore the caller's frame pointer and
316 // the link register.
317 frame_->Exit();
318
319 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000320 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000321 }
322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 ASSERT(!has_cc());
325 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000326 ASSERT(!function_return_is_shadowed_);
327 function_return_.Unuse();
328 DeleteFrame();
329
330 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000331 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000332 ProcessDeferred();
333 }
334
335 allocator_ = NULL;
336 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337}
338
339
ager@chromium.org7c537e22008-10-16 08:43:32 +0000340MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
341 // Currently, this assertion will fail if we try to assign to
342 // a constant variable that is constant because it is read-only
343 // (such as the variable referring to a named function expression).
344 // We need to implement assignments to read-only variables.
345 // Ideally, we should do this during AST generation (by converting
346 // such assignments into expression statements); however, in general
347 // we may not be able to make the decision until past AST generation,
348 // that is when the entire program is known.
349 ASSERT(slot != NULL);
350 int index = slot->index();
351 switch (slot->type()) {
352 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000353 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000354
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000355 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000356 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357
358 case Slot::CONTEXT: {
359 // Follow the context chain if necessary.
360 ASSERT(!tmp.is(cp)); // do not overwrite context register
361 Register context = cp;
362 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000363 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000364 // Load the closure.
365 // (All contexts, even 'with' contexts, have a closure,
366 // and it is the same for all contexts inside a function.
367 // There is no need to go to the function context first.)
368 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
369 // Load the function context (which is the incoming, outer context).
370 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
371 context = tmp;
372 }
373 // We may have a 'with' context now. Get the function context.
374 // (In fact this mov may never be the needed, since the scope analysis
375 // may not permit a direct context access in this case and thus we are
376 // always at a function context. However it is safe to dereference be-
377 // cause the function context of a function context is itself. Before
378 // deleting this mov we should try to create a counter-example first,
379 // though...)
380 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
381 return ContextOperand(tmp, index);
382 }
383
384 default:
385 UNREACHABLE();
386 return MemOperand(r0, 0);
387 }
388}
389
390
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000391MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
392 Slot* slot,
393 Register tmp,
394 Register tmp2,
395 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000396 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000397 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000398
ager@chromium.org381abbb2009-02-25 13:23:22 +0000399 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
400 if (s->num_heap_slots() > 0) {
401 if (s->calls_eval()) {
402 // Check that extension is NULL.
403 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
404 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000405 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000406 }
407 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
408 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
409 context = tmp;
410 }
411 }
412 // Check that last extension is NULL.
413 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
414 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000416 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000417 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000418}
419
420
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000421void CodeGenerator::LoadConditionAndSpill(Expression* expression,
422 TypeofState typeof_state,
423 JumpTarget* true_target,
424 JumpTarget* false_target,
425 bool force_control) {
426 ASSERT(in_spilled_code());
427 set_in_spilled_code(false);
428 LoadCondition(expression, typeof_state, true_target, false_target,
429 force_control);
430 if (frame_ != NULL) {
431 frame_->SpillAll();
432 }
433 set_in_spilled_code(true);
434}
435
436
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000437// Loads a value on TOS. If it is a boolean value, the result may have been
438// (partially) translated into branches, or it may have set the condition
439// code register. If force_cc is set, the value is forced to set the
440// condition code register and no value is pushed. If the condition code
441// register was set, has_cc() is true and cc_reg_ contains the condition to
442// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000443void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000444 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000445 JumpTarget* true_target,
446 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000447 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000448 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000449 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000450 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451
ager@chromium.org7c537e22008-10-16 08:43:32 +0000452 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000453 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454
455 // If we hit a stack overflow, we may not have actually visited
456 // the expression. In that case, we ensure that we have a
457 // valid-looking frame state because we will continue to generate
458 // code as we unwind the C++ stack.
459 //
460 // It's possible to have both a stack overflow and a valid frame
461 // state (eg, a subexpression overflowed, visiting it returned
462 // with a dummied frame state, and visiting this expression
463 // returned with a normal-looking state).
464 if (HasStackOverflow() &&
465 has_valid_frame() &&
466 !has_cc() &&
467 frame_->height() == original_height) {
468 true_target->Jump();
469 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000470 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000471 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000472 // Convert the TOS value to a boolean in the condition code register.
473 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000475 ASSERT(!force_cc || !has_valid_frame() || has_cc());
476 ASSERT(!has_valid_frame() ||
477 (has_cc() && frame_->height() == original_height) ||
478 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000482void CodeGenerator::LoadAndSpill(Expression* expression,
483 TypeofState typeof_state) {
484 ASSERT(in_spilled_code());
485 set_in_spilled_code(false);
486 Load(expression, typeof_state);
487 frame_->SpillAll();
488 set_in_spilled_code(true);
489}
490
491
ager@chromium.org7c537e22008-10-16 08:43:32 +0000492void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000493#ifdef DEBUG
494 int original_height = frame_->height();
495#endif
496 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000497 JumpTarget true_target;
498 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000499 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500
501 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000503 JumpTarget loaded;
504 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000506 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507 frame_->EmitPush(r0);
508 loaded.Jump();
509 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000510 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000511 frame_->EmitPush(r0);
512 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 cc_reg_ = al;
514 }
515
516 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000517 // We have at least one condition value that has been "translated"
518 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000519 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000520 if (frame_ != NULL) {
521 loaded.Jump(); // Don't lose the current TOS.
522 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000524 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000527 __ mov(r0, Operand(Factory::true_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 // If both "true" and "false" need to be loaded jump across the code for
531 // "false".
532 if (both) {
533 loaded.Jump();
534 }
535 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000538 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000539 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 // A value is loaded on all paths reaching this point.
542 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547}
548
549
ager@chromium.org7c537e22008-10-16 08:43:32 +0000550void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000551 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000552 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554}
555
556
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000557void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000558 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000559 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
560 __ ldr(scratch,
561 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000562 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000563}
564
565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000567// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
568// variables w/o reference errors elsewhere.
569void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000570 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 Variable* variable = x->AsVariableProxy()->AsVariable();
572 if (variable != NULL && !variable->is_this() && variable->is_global()) {
573 // NOTE: This is somewhat nasty. We force the compiler to load
574 // the variable as if through '<global>.<variable>' to make sure we
575 // do not get reference errors.
576 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
577 Literal key(variable->name());
578 // TODO(1241834): Fetch the position from the variable instead of using
579 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000580 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000581 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000583 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 }
585}
586
587
ager@chromium.org7c537e22008-10-16 08:43:32 +0000588Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
590 cgen->LoadReference(this);
591}
592
593
594Reference::~Reference() {
595 cgen_->UnloadReference(this);
596}
597
598
ager@chromium.org7c537e22008-10-16 08:43:32 +0000599void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000600 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000601 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 Expression* e = ref->expression();
603 Property* property = e->AsProperty();
604 Variable* var = e->AsVariableProxy()->AsVariable();
605
606 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000607 // The expression is either a property or a variable proxy that rewrites
608 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000609 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000610 // We use a named reference if the key is a literal symbol, unless it is
611 // a string that can be legally parsed as an integer. This is because
612 // otherwise we will not get into the slow case code that handles [] on
613 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 Literal* literal = property->key()->AsLiteral();
615 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000616 if (literal != NULL &&
617 literal->handle()->IsSymbol() &&
618 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 ref->set_type(Reference::NAMED);
620 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000621 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 ref->set_type(Reference::KEYED);
623 }
624 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000625 // The expression is a variable proxy that does not rewrite to a
626 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 LoadGlobal();
629 ref->set_type(Reference::NAMED);
630 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000631 ASSERT(var->slot() != NULL);
632 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 }
634 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000635 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000636 LoadAndSpill(e);
637 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 }
639}
640
641
ager@chromium.org7c537e22008-10-16 08:43:32 +0000642void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000643 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000644 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000645 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000647 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000649 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000650 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 }
652}
653
654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
656// register to a boolean in the condition code register. The code
657// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000658void CodeGenerator::ToBoolean(JumpTarget* true_target,
659 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000660 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000661 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000663 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664
665 // Fast case checks
666
mads.s.ager31e71382008-08-13 09:32:07 +0000667 // Check if the value is 'false'.
668 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
mads.s.ager31e71382008-08-13 09:32:07 +0000671 // Check if the value is 'true'.
672 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000673 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674
mads.s.ager31e71382008-08-13 09:32:07 +0000675 // Check if the value is 'undefined'.
676 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000677 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
mads.s.ager31e71382008-08-13 09:32:07 +0000679 // Check if the value is a smi.
680 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000681 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000682 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000683 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684
685 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000686 frame_->EmitPush(r0);
687 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000688 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
691 cc_reg_ = ne;
692}
693
694
kasper.lund7276f142008-07-30 08:49:36 +0000695class GenericBinaryOpStub : public CodeStub {
696 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000697 GenericBinaryOpStub(Token::Value op,
698 OverwriteMode mode)
699 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000700
701 private:
702 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000703 OverwriteMode mode_;
704
705 // Minor key encoding in 16 bits.
706 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
707 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000708
709 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000710 int MinorKey() {
711 // Encode the parameters in a unique 16 bit value.
712 return OpBits::encode(op_)
713 | ModeBits::encode(mode_);
714 }
715
kasper.lund7276f142008-07-30 08:49:36 +0000716 void Generate(MacroAssembler* masm);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000717 void HandleNonSmiBitwiseOp(MacroAssembler* masm);
kasper.lund7276f142008-07-30 08:49:36 +0000718
719 const char* GetName() {
720 switch (op_) {
721 case Token::ADD: return "GenericBinaryOpStub_ADD";
722 case Token::SUB: return "GenericBinaryOpStub_SUB";
723 case Token::MUL: return "GenericBinaryOpStub_MUL";
724 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000725 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
726 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
727 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
728 case Token::SAR: return "GenericBinaryOpStub_SAR";
729 case Token::SHL: return "GenericBinaryOpStub_SHL";
730 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000731 default: return "GenericBinaryOpStub";
732 }
733 }
734
735#ifdef DEBUG
736 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
737#endif
738};
739
740
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000741void CodeGenerator::GenericBinaryOperation(Token::Value op,
742 OverwriteMode overwrite_mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000743 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000744 // sp[0] : y
745 // sp[1] : x
746 // result : r0
747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 // Stub is entered with a call: 'return address' is in lr.
749 switch (op) {
750 case Token::ADD: // fall through.
751 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000752 case Token::MUL:
753 case Token::BIT_OR:
754 case Token::BIT_AND:
755 case Token::BIT_XOR:
756 case Token::SHL:
757 case Token::SHR:
758 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000759 frame_->EmitPop(r0); // r0 : y
760 frame_->EmitPop(r1); // r1 : x
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000761 GenericBinaryOpStub stub(op, overwrite_mode);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000762 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 break;
764 }
765
766 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000767 Result arg_count = allocator_->Allocate(r0);
768 ASSERT(arg_count.is_valid());
769 __ mov(arg_count.reg(), Operand(1));
770 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771 break;
772 }
773
774 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000775 Result arg_count = allocator_->Allocate(r0);
776 ASSERT(arg_count.is_valid());
777 __ mov(arg_count.reg(), Operand(1));
778 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 break;
780 }
781
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000785 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 break;
787
788 default:
789 // Other cases should have been handled before this point.
790 UNREACHABLE();
791 break;
792 }
793}
794
795
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000796class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000797 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000798 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000799 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000800 bool reversed,
801 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000802 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000803 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000804 reversed_(reversed),
805 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000806 set_comment("[ DeferredInlinedSmiOperation");
807 }
808
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000809 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000810
811 private:
812 Token::Value op_;
813 int value_;
814 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000815 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000816};
817
818
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000819void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000820 switch (op_) {
821 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000822 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000823 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000824 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
825 __ mov(r1, Operand(Smi::FromInt(value_)));
826 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000827 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
828 __ mov(r0, Operand(Smi::FromInt(value_)));
829 }
830 break;
831 }
832
833 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000834 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000835 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000836 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
837 __ mov(r1, Operand(Smi::FromInt(value_)));
838 } else {
839 __ add(r1, r0, Operand(Smi::FromInt(value_)));
840 __ mov(r0, Operand(Smi::FromInt(value_)));
841 }
842 break;
843 }
844
845 case Token::BIT_OR:
846 case Token::BIT_XOR:
847 case Token::BIT_AND: {
848 if (reversed_) {
849 __ mov(r1, Operand(Smi::FromInt(value_)));
850 } else {
851 __ mov(r1, Operand(r0));
852 __ mov(r0, Operand(Smi::FromInt(value_)));
853 }
854 break;
855 }
856
857 case Token::SHL:
858 case Token::SHR:
859 case Token::SAR: {
860 if (!reversed_) {
861 __ mov(r1, Operand(r0));
862 __ mov(r0, Operand(Smi::FromInt(value_)));
863 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000864 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000865 }
866 break;
867 }
868
869 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000870 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000871 UNREACHABLE();
872 break;
873 }
874
ager@chromium.orge2902be2009-06-08 12:21:35 +0000875 GenericBinaryOpStub stub(op_, overwrite_mode_);
876 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000877}
878
879
ager@chromium.org7c537e22008-10-16 08:43:32 +0000880void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000881 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000882 bool reversed,
883 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000884 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 // NOTE: This is an attempt to inline (a bit) more of the code for
886 // some possible smi operations (like + and -) when (at least) one
887 // of the operands is a literal smi. With this optimization, the
888 // performance of the system is increased by ~15%, and the generated
889 // code size is increased by ~1% (measured on a combination of
890 // different benchmarks).
891
mads.s.ager31e71382008-08-13 09:32:07 +0000892 // sp[0] : operand
893
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000894 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000896 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000897 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899 switch (op) {
900 case Token::ADD: {
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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000904 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000905 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000907 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000908 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 break;
910 }
911
912 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000913 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000914 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
ager@chromium.orge2902be2009-06-08 12:21:35 +0000916 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000917 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000918 } else {
919 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000921 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000922 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000923 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000924 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000925 break;
926 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000928 case Token::BIT_OR:
929 case Token::BIT_XOR:
930 case Token::BIT_AND: {
931 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000932 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000933 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000934 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935 switch (op) {
936 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
937 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
938 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
939 default: UNREACHABLE();
940 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000941 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000942 break;
943 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000945 case Token::SHL:
946 case Token::SHR:
947 case Token::SAR: {
948 if (reversed) {
949 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000950 frame_->EmitPush(ip);
951 frame_->EmitPush(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000952 GenericBinaryOperation(op, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000953
954 } else {
955 int shift_value = int_value & 0x1f; // least significant 5 bits
956 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000957 new DeferredInlineSmiOperation(op, shift_value, false, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000958 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000959 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000960 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
961 switch (op) {
962 case Token::SHL: {
963 __ mov(r2, Operand(r2, LSL, shift_value));
964 // check that the *unsigned* result fits in a smi
965 __ add(r3, r2, Operand(0x40000000), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000966 deferred->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000967 break;
968 }
969 case Token::SHR: {
970 // LSR by immediate 0 means shifting 32 bits.
971 if (shift_value != 0) {
972 __ mov(r2, Operand(r2, LSR, shift_value));
973 }
974 // check that the *unsigned* result fits in a smi
975 // neither of the two high-order bits can be set:
976 // - 0x80000000: high bit would be lost when smi tagging
977 // - 0x40000000: this number would convert to negative when
978 // smi tagging these two cases can only happen with shifts
979 // by 0 or 1 when handed a valid smi
980 __ and_(r3, r2, Operand(0xc0000000), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000981 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000982 break;
983 }
984 case Token::SAR: {
985 if (shift_value != 0) {
986 // ASR by immediate 0 means shifting 32 bits.
987 __ mov(r2, Operand(r2, ASR, shift_value));
988 }
989 break;
990 }
991 default: UNREACHABLE();
992 }
993 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000994 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000995 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 break;
997 }
998
999 default:
1000 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001001 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001002 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001003 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 } else {
1005 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001006 frame_->EmitPush(ip);
1007 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001009 GenericBinaryOperation(op, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 break;
1011 }
1012
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001013 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014}
1015
1016
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001017void CodeGenerator::Comparison(Condition cc,
1018 Expression* left,
1019 Expression* right,
1020 bool strict) {
1021 if (left != NULL) LoadAndSpill(left);
1022 if (right != NULL) LoadAndSpill(right);
1023
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001024 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001025 // sp[0] : y
1026 // sp[1] : x
1027 // result : cc register
1028
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 // Strict only makes sense for equality comparisons.
1030 ASSERT(!strict || cc == eq);
1031
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001032 JumpTarget exit;
1033 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001034 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1035 if (cc == gt || cc == le) {
1036 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001037 frame_->EmitPop(r1);
1038 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001039 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001040 frame_->EmitPop(r0);
1041 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001042 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 __ orr(r2, r0, Operand(r1));
1044 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001045 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001047 // Perform non-smi comparison by stub.
1048 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1049 // We call with 0 args because there are 0 on the stack.
1050 CompareStub stub(cc, strict);
1051 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001053 Result result = allocator_->Allocate(r0);
1054 ASSERT(result.is_valid());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001055 __ cmp(result.reg(), Operand(0));
1056 result.Unuse();
1057 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001059 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001060 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 __ cmp(r1, Operand(r0));
1062
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001063 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 cc_reg_ = cc;
1065}
1066
1067
kasper.lund7276f142008-07-30 08:49:36 +00001068class CallFunctionStub: public CodeStub {
1069 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001070 CallFunctionStub(int argc, InLoopFlag in_loop)
1071 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001072
1073 void Generate(MacroAssembler* masm);
1074
1075 private:
1076 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001077 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001078
kasper.lund7276f142008-07-30 08:49:36 +00001079#if defined(DEBUG)
1080 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1081#endif // defined(DEBUG)
1082
1083 Major MajorKey() { return CallFunction; }
1084 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001085 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001086};
1087
1088
mads.s.ager31e71382008-08-13 09:32:07 +00001089// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001090void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001092 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001094 int arg_count = args->length();
1095 for (int i = 0; i < arg_count; i++) {
1096 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001097 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098
kasper.lund7276f142008-07-30 08:49:36 +00001099 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001100 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101
kasper.lund7276f142008-07-30 08:49:36 +00001102 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001103 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1104 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001105 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106
1107 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001108 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001109 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110}
1111
1112
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001113void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001114 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 ASSERT(has_cc());
1116 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001117 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 cc_reg_ = al;
1119}
1120
1121
ager@chromium.org7c537e22008-10-16 08:43:32 +00001122void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001123 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 if (FLAG_check_stack) {
1125 Comment cmnt(masm_, "[ check stack");
1126 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001127 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 }
1129}
1130
1131
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001132void CodeGenerator::VisitAndSpill(Statement* statement) {
1133 ASSERT(in_spilled_code());
1134 set_in_spilled_code(false);
1135 Visit(statement);
1136 if (frame_ != NULL) {
1137 frame_->SpillAll();
1138 }
1139 set_in_spilled_code(true);
1140}
1141
1142
1143void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1144 ASSERT(in_spilled_code());
1145 set_in_spilled_code(false);
1146 VisitStatements(statements);
1147 if (frame_ != NULL) {
1148 frame_->SpillAll();
1149 }
1150 set_in_spilled_code(true);
1151}
1152
1153
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001154void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1155#ifdef DEBUG
1156 int original_height = frame_->height();
1157#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001158 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001159 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1160 VisitAndSpill(statements->at(i));
1161 }
1162 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1163}
1164
1165
ager@chromium.org7c537e22008-10-16 08:43:32 +00001166void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001167#ifdef DEBUG
1168 int original_height = frame_->height();
1169#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001170 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001172 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001173 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001174 VisitStatementsAndSpill(node->statements());
1175 if (node->break_target()->is_linked()) {
1176 node->break_target()->Bind();
1177 }
1178 node->break_target()->Unuse();
1179 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180}
1181
1182
ager@chromium.org7c537e22008-10-16 08:43:32 +00001183void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001184 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001185 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001186 frame_->EmitPush(r0);
1187 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001188 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001189 frame_->EmitPush(r0);
1190 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001191 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192}
1193
1194
ager@chromium.org7c537e22008-10-16 08:43:32 +00001195void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001196#ifdef DEBUG
1197 int original_height = frame_->height();
1198#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001199 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001201 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 Variable* var = node->proxy()->var();
1203 ASSERT(var != NULL); // must have been resolved
1204 Slot* slot = var->slot();
1205
1206 // If it was not possible to allocate the variable at compile time,
1207 // we need to "declare" it at runtime to make sure it actually
1208 // exists in the local context.
1209 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1210 // Variables with a "LOOKUP" slot were introduced as non-locals
1211 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001212 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001214 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001215 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001216 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 // Declaration nodes are always declared in only two modes.
1218 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1219 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001220 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001221 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 // Push initial value, if any.
1223 // Note: For variables we must not push an initial value (such as
1224 // 'undefined') because we may have a (legal) redeclaration and we
1225 // must not destroy the current value.
1226 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001227 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001228 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001230 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001232 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001235 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001236 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001237 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 return;
1239 }
1240
1241 ASSERT(!var->is_global());
1242
1243 // If we have a function or a constant, we need to initialize the variable.
1244 Expression* val = NULL;
1245 if (node->mode() == Variable::CONST) {
1246 val = new Literal(Factory::the_hole_value());
1247 } else {
1248 val = node->fun(); // NULL if we don't have a function
1249 }
1250
1251 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001252 {
1253 // Set initial value.
1254 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001255 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001256 target.SetValue(NOT_CONST_INIT);
1257 // The reference is removed from the stack (preserving TOS) when
1258 // it goes out of scope.
1259 }
1260 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001261 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001263 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264}
1265
1266
ager@chromium.org7c537e22008-10-16 08:43:32 +00001267void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001268#ifdef DEBUG
1269 int original_height = frame_->height();
1270#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001271 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001273 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 Expression* expression = node->expression();
1275 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001276 LoadAndSpill(expression);
1277 frame_->Drop();
1278 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279}
1280
1281
ager@chromium.org7c537e22008-10-16 08:43:32 +00001282void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001283#ifdef DEBUG
1284 int original_height = frame_->height();
1285#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001286 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001288 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001290 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291}
1292
1293
ager@chromium.org7c537e22008-10-16 08:43:32 +00001294void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001295#ifdef DEBUG
1296 int original_height = frame_->height();
1297#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001298 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001300 // Generate different code depending on which parts of the if statement
1301 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 bool has_then_stm = node->HasThenStatement();
1303 bool has_else_stm = node->HasElseStatement();
1304
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001305 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001307 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001309 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001310 JumpTarget then;
1311 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001313 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1314 &then, &else_, true);
1315 if (frame_ != NULL) {
1316 Branch(false, &else_);
1317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001319 if (frame_ != NULL || then.is_linked()) {
1320 then.Bind();
1321 VisitAndSpill(node->then_statement());
1322 }
1323 if (frame_ != NULL) {
1324 exit.Jump();
1325 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001327 if (else_.is_linked()) {
1328 else_.Bind();
1329 VisitAndSpill(node->else_statement());
1330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331
1332 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001333 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001335 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001337 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1338 &then, &exit, true);
1339 if (frame_ != NULL) {
1340 Branch(false, &exit);
1341 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001343 if (frame_ != NULL || then.is_linked()) {
1344 then.Bind();
1345 VisitAndSpill(node->then_statement());
1346 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347
1348 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001349 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001351 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001353 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1354 &exit, &else_, true);
1355 if (frame_ != NULL) {
1356 Branch(true, &exit);
1357 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 if (frame_ != NULL || else_.is_linked()) {
1360 else_.Bind();
1361 VisitAndSpill(node->else_statement());
1362 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363
1364 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001365 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 ASSERT(!has_then_stm && !has_else_stm);
1367 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001368 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1369 &exit, &exit, false);
1370 if (frame_ != NULL) {
1371 if (has_cc()) {
1372 cc_reg_ = al;
1373 } else {
1374 frame_->Drop();
1375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 }
1377 }
1378
1379 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001380 if (exit.is_linked()) {
1381 exit.Bind();
1382 }
1383 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384}
1385
1386
ager@chromium.org7c537e22008-10-16 08:43:32 +00001387void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001388 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390 CodeForStatementPosition(node);
1391 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392}
1393
1394
ager@chromium.org7c537e22008-10-16 08:43:32 +00001395void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001396 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 CodeForStatementPosition(node);
1399 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400}
1401
1402
ager@chromium.org7c537e22008-10-16 08:43:32 +00001403void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001404 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001406
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407 if (function_return_is_shadowed_) {
1408 CodeForStatementPosition(node);
1409 LoadAndSpill(node->expression());
1410 frame_->EmitPop(r0);
1411 function_return_.Jump();
1412 } else {
1413 // Load the returned value.
1414 CodeForStatementPosition(node);
1415 LoadAndSpill(node->expression());
1416
1417 // Pop the result from the frame and prepare the frame for
1418 // returning thus making it easier to merge.
1419 frame_->EmitPop(r0);
1420 frame_->PrepareForReturn();
1421
1422 function_return_.Jump();
1423 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424}
1425
1426
ager@chromium.org7c537e22008-10-16 08:43:32 +00001427void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001428#ifdef DEBUG
1429 int original_height = frame_->height();
1430#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001431 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001433 CodeForStatementPosition(node);
1434 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001435 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001436 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001437 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001438 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001439 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001440#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001441 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001442 __ cmp(r0, Operand(cp));
1443 verified_true.Branch(eq);
1444 __ stop("PushContext: r0 is expected to be the same as cp");
1445 verified_true.Bind();
1446#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001448 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001449 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450}
1451
1452
ager@chromium.org7c537e22008-10-16 08:43:32 +00001453void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001454#ifdef DEBUG
1455 int original_height = frame_->height();
1456#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001457 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 // Pop context.
1461 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1462 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001463 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001464 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465}
1466
1467
ager@chromium.org7c537e22008-10-16 08:43:32 +00001468void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001469#ifdef DEBUG
1470 int original_height = frame_->height();
1471#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001472 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001474 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001475 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001477 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001478
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001479 JumpTarget next_test;
1480 JumpTarget fall_through;
1481 JumpTarget default_entry;
1482 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 ZoneList<CaseClause*>* cases = node->cases();
1484 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001485 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486
1487 for (int i = 0; i < length; i++) {
1488 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001490 // Remember the default clause and compile it at the end.
1491 default_clause = clause;
1492 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 }
1494
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001495 Comment cmnt(masm_, "[ Case clause");
1496 // Compile the test.
1497 next_test.Bind();
1498 next_test.Unuse();
1499 // Duplicate TOS.
1500 __ ldr(r0, frame_->Top());
1501 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001502 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001503 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001504
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505 // Before entering the body from the test, remove the switch value from
1506 // the stack.
1507 frame_->Drop();
1508
1509 // Label the body so that fall through is enabled.
1510 if (i > 0 && cases->at(i - 1)->is_default()) {
1511 default_exit.Bind();
1512 } else {
1513 fall_through.Bind();
1514 fall_through.Unuse();
1515 }
1516 VisitStatementsAndSpill(clause->statements());
1517
1518 // If control flow can fall through from the body, jump to the next body
1519 // or the end of the statement.
1520 if (frame_ != NULL) {
1521 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1522 default_entry.Jump();
1523 } else {
1524 fall_through.Jump();
1525 }
1526 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 }
1528
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001529 // The final "test" removes the switch value.
1530 next_test.Bind();
1531 frame_->Drop();
1532
1533 // If there is a default clause, compile it.
1534 if (default_clause != NULL) {
1535 Comment cmnt(masm_, "[ Default clause");
1536 default_entry.Bind();
1537 VisitStatementsAndSpill(default_clause->statements());
1538 // If control flow can fall out of the default and there is a case after
1539 // it, jup to that case's body.
1540 if (frame_ != NULL && default_exit.is_bound()) {
1541 default_exit.Jump();
1542 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001543 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001545 if (fall_through.is_linked()) {
1546 fall_through.Bind();
1547 }
1548
1549 if (node->break_target()->is_linked()) {
1550 node->break_target()->Bind();
1551 }
1552 node->break_target()->Unuse();
1553 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554}
1555
1556
ager@chromium.org7c537e22008-10-16 08:43:32 +00001557void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001558#ifdef DEBUG
1559 int original_height = frame_->height();
1560#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001561 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001563 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001564 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1567 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1569 if (node->cond() == NULL) {
1570 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1571 info = ALWAYS_TRUE;
1572 } else {
1573 Literal* lit = node->cond()->AsLiteral();
1574 if (lit != NULL) {
1575 if (lit->IsTrue()) {
1576 info = ALWAYS_TRUE;
1577 } else if (lit->IsFalse()) {
1578 info = ALWAYS_FALSE;
1579 }
1580 }
1581 }
1582
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001583 switch (node->type()) {
1584 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001585 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001587 // Label the top of the loop for the backward CFG edge. If the test
1588 // is always true we can use the continue target, and if the test is
1589 // always false there is no need.
1590 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001591 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001592 node->continue_target()->Bind();
1593 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001594 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 } else {
1596 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001597 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001598 body.Bind();
1599 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001602 VisitAndSpill(node->body());
1603
1604 // Compile the test.
1605 if (info == ALWAYS_TRUE) {
1606 if (has_valid_frame()) {
1607 // If control can fall off the end of the body, jump back to the
1608 // top.
1609 node->continue_target()->Jump();
1610 }
1611 } else if (info == ALWAYS_FALSE) {
1612 // If we have a continue in the body, we only have to bind its jump
1613 // target.
1614 if (node->continue_target()->is_linked()) {
1615 node->continue_target()->Bind();
1616 }
1617 } else {
1618 ASSERT(info == DONT_KNOW);
1619 // We have to compile the test expression if it can be reached by
1620 // control flow falling out of the body or via continue.
1621 if (node->continue_target()->is_linked()) {
1622 node->continue_target()->Bind();
1623 }
1624 if (has_valid_frame()) {
1625 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1626 &body, node->break_target(), true);
1627 if (has_valid_frame()) {
1628 // A invalid frame here indicates that control did not
1629 // fall out of the test expression.
1630 Branch(true, &body);
1631 }
1632 }
1633 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635 }
1636
1637 case LoopStatement::WHILE_LOOP: {
1638 // If the test is never true and has no side effects there is no need
1639 // to compile the test or body.
1640 if (info == ALWAYS_FALSE) break;
1641
1642 // Label the top of the loop with the continue target for the backward
1643 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001644 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001645 node->continue_target()->Bind();
1646
1647 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001648 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001649 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1650 &body, node->break_target(), true);
1651 if (has_valid_frame()) {
1652 // A NULL frame indicates that control did not fall out of the
1653 // test expression.
1654 Branch(false, node->break_target());
1655 }
1656 if (has_valid_frame() || body.is_linked()) {
1657 body.Bind();
1658 }
1659 }
1660
1661 if (has_valid_frame()) {
1662 CheckStack(); // TODO(1222600): ignore if body contains calls.
1663 VisitAndSpill(node->body());
1664
1665 // If control flow can fall out of the body, jump back to the top.
1666 if (has_valid_frame()) {
1667 node->continue_target()->Jump();
1668 }
1669 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001671 }
1672
1673 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001674 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001675
1676 if (node->init() != NULL) {
1677 VisitAndSpill(node->init());
1678 }
1679
1680 // There is no need to compile the test or body.
1681 if (info == ALWAYS_FALSE) break;
1682
1683 // If there is no update statement, label the top of the loop with the
1684 // continue target, otherwise with the loop target.
1685 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001686 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 node->continue_target()->Bind();
1688 } else {
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 loop.Bind();
1691 }
1692
1693 // If the test is always true, there is no need to compile it.
1694 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001695 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001696 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1697 &body, node->break_target(), true);
1698 if (has_valid_frame()) {
1699 Branch(false, node->break_target());
1700 }
1701 if (has_valid_frame() || body.is_linked()) {
1702 body.Bind();
1703 }
1704 }
1705
1706 if (has_valid_frame()) {
1707 CheckStack(); // TODO(1222600): ignore if body contains calls.
1708 VisitAndSpill(node->body());
1709
1710 if (node->next() == NULL) {
1711 // If there is no update statement and control flow can fall out
1712 // of the loop, jump directly to the continue label.
1713 if (has_valid_frame()) {
1714 node->continue_target()->Jump();
1715 }
1716 } else {
1717 // If there is an update statement and control flow can reach it
1718 // via falling out of the body of the loop or continuing, we
1719 // compile the update statement.
1720 if (node->continue_target()->is_linked()) {
1721 node->continue_target()->Bind();
1722 }
1723 if (has_valid_frame()) {
1724 // Record source position of the statement as this code which is
1725 // after the code for the body actually belongs to the loop
1726 // statement and not the body.
1727 CodeForStatementPosition(node);
1728 VisitAndSpill(node->next());
1729 loop.Jump();
1730 }
1731 }
1732 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001734 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 }
1736
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001737 if (node->break_target()->is_linked()) {
1738 node->break_target()->Bind();
1739 }
1740 node->continue_target()->Unuse();
1741 node->break_target()->Unuse();
1742 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743}
1744
1745
ager@chromium.org7c537e22008-10-16 08:43:32 +00001746void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001747#ifdef DEBUG
1748 int original_height = frame_->height();
1749#endif
1750 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001751 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001753 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001755 JumpTarget primitive;
1756 JumpTarget jsobject;
1757 JumpTarget fixed_array;
1758 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1759 JumpTarget end_del_check;
1760 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761
1762 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001763 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764
1765 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1766 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001769 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001771 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772
1773 // Stack layout in body:
1774 // [iteration counter (Smi)]
1775 // [length of array]
1776 // [FixedArray]
1777 // [Map or 0]
1778 // [Object]
1779
1780 // Check if enumerable is already a JSObject
1781 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001783 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001784 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001786 primitive.Bind();
1787 frame_->EmitPush(r0);
1788 Result arg_count = allocator_->Allocate(r0);
1789 ASSERT(arg_count.is_valid());
1790 __ mov(arg_count.reg(), Operand(0));
1791 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001795 frame_->EmitPush(r0); // duplicate the object being enumerated
1796 frame_->EmitPush(r0);
1797 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798
1799 // If we got a Map, we can do a fast modification check.
1800 // Otherwise, we got a FixedArray, and we have to do a slow check.
1801 __ mov(r2, Operand(r0));
1802 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1803 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001804 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805
1806 // Get enum cache
1807 __ mov(r1, Operand(r0));
1808 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1809 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1810 __ ldr(r2,
1811 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1812
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001813 frame_->EmitPush(r0); // map
1814 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001815 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001818 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 frame_->EmitPush(r0);
1820 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 frame_->EmitPush(r1); // insert 0 in place of Map
1825 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826
1827 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001828 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001831 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001836 // sp[0] : index
1837 // sp[1] : array/enum cache length
1838 // sp[2] : array or enum cache
1839 // sp[3] : 0 or map
1840 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 // Grab the current frame's height for the break and continue
1842 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001843 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1844 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1847 __ ldr(r1, frame_->ElementAt(1)); // load the length
1848 __ cmp(r0, Operand(r1)); // compare to the array length
1849 node->break_target()->Branch(hs);
1850
1851 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1856 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1857
1858 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 // Check if this (still) matches the map of the enumerable.
1861 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1864 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866
1867 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1869 frame_->EmitPush(r0);
1870 frame_->EmitPush(r3); // push entry
1871 Result arg_count_register = allocator_->Allocate(r0);
1872 ASSERT(arg_count_register.is_valid());
1873 __ mov(arg_count_register.reg(), Operand(1));
1874 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1875 CALL_JS,
1876 &arg_count_register,
1877 2);
1878 __ mov(r3, Operand(result.reg()));
1879 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880
1881 // If the property has been removed while iterating, we just skip it.
1882 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001885 end_del_check.Bind();
1886 // Store the entry in the 'each' expression and take another spin in the
1887 // loop. r3: i'th entry of the enum cache (or string there of)
1888 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 { Reference each(this, node->each());
1890 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001891 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001892 __ ldr(r0, frame_->ElementAt(each.size()));
1893 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001894 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001895 // If the reference was to a slot we rely on the convenient property
1896 // that it doesn't matter whether a value (eg, r3 pushed above) is
1897 // right on top of or right underneath a zero-sized reference.
1898 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001899 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001900 // It's safe to pop the value lying on top of the reference before
1901 // unloading the reference itself (which preserves the top of stack,
1902 // ie, now the topmost value of the non-zero sized reference), since
1903 // we will discard the top of stack after unloading the reference
1904 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001905 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001906 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 }
1908 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001909 // Discard the i'th entry pushed above or else the remainder of the
1910 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 frame_->Drop();
1912
1913 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001915 VisitAndSpill(node->body());
1916
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001917 // Next. Reestablish a spilled frame in case we are coming here via
1918 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001919 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001920 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001921 frame_->EmitPop(r0);
1922 __ add(r0, r0, Operand(Smi::FromInt(1)));
1923 frame_->EmitPush(r0);
1924 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001926 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1927 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001929 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930
1931 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001932 exit.Bind();
1933 node->continue_target()->Unuse();
1934 node->break_target()->Unuse();
1935 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936}
1937
1938
ager@chromium.org7c537e22008-10-16 08:43:32 +00001939void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001940#ifdef DEBUG
1941 int original_height = frame_->height();
1942#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001943 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001945 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001947 JumpTarget try_block;
1948 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001950 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001952 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953
1954 // Store the caught exception in the catch variable.
1955 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001956 ASSERT(ref.is_slot());
1957 // Here we make use of the convenient property that it doesn't matter
1958 // whether a value is immediately on top of or underneath a zero-sized
1959 // reference.
1960 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 }
1962
1963 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 VisitStatementsAndSpill(node->catch_block()->statements());
1967 if (frame_ != NULL) {
1968 exit.Jump();
1969 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970
1971
1972 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001973 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001975 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1976 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001978 // Shadow the labels for all escapes from the try block, including
1979 // returns. During shadowing, the original label is hidden as the
1980 // LabelShadow and operations on the original actually affect the
1981 // shadowing label.
1982 //
1983 // We should probably try to unify the escaping labels and the return
1984 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 int nof_escapes = node->escaping_targets()->length();
1986 List<ShadowTarget*> shadows(1 + nof_escapes);
1987
1988 // Add the shadow target for the function return.
1989 static const int kReturnShadowIndex = 0;
1990 shadows.Add(new ShadowTarget(&function_return_));
1991 bool function_return_was_shadowed = function_return_is_shadowed_;
1992 function_return_is_shadowed_ = true;
1993 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1994
1995 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 }
1999
2000 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002001 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
2003 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002004 // After shadowing stops, the original labels are unshadowed and the
2005 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002006 bool has_unlinks = false;
2007 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 has_unlinks = has_unlinks || shadows[i]->is_linked();
2010 }
2011 function_return_is_shadowed_ = function_return_was_shadowed;
2012
2013 // Get an external reference to the handler address.
2014 ExternalReference handler_address(Top::k_handler_address);
2015
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002016 // If we can fall off the end of the try block, unlink from try chain.
2017 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002018 // The next handler address is on top of the frame. Unlink from
2019 // the handler list and drop the rest of this handler from the
2020 // frame.
2021 ASSERT(StackHandlerConstants::kNextOffset == 0);
2022 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023 __ mov(r3, Operand(handler_address));
2024 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002025 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 if (has_unlinks) {
2027 exit.Jump();
2028 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 }
2030
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002031 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002032 // jumped to. Deallocate each shadow target.
2033 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002035 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002036 shadows[i]->Bind();
2037 // Because we can be jumping here (to spilled code) from unspilled
2038 // code, we need to reestablish a spilled frame at this block.
2039 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 // Reload sp from the top handler, because some statements that we
2042 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002045 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002047 ASSERT(StackHandlerConstants::kNextOffset == 0);
2048 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002050 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2053 frame_->PrepareForReturn();
2054 }
2055 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 }
2057 }
2058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 exit.Bind();
2060 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061}
2062
2063
ager@chromium.org7c537e22008-10-16 08:43:32 +00002064void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065#ifdef DEBUG
2066 int original_height = frame_->height();
2067#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002068 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002070 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071
2072 // State: Used to keep track of reason for entering the finally
2073 // block. Should probably be extended to hold information for
2074 // break/continue from within the try block.
2075 enum { FALLING, THROWING, JUMPING };
2076
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002077 JumpTarget try_block;
2078 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002080 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 // In case of thrown exceptions, this is where we continue.
2084 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
2087 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2091 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002093 // Shadow the labels for all escapes from the try block, including
2094 // returns. Shadowing hides the original label as the LabelShadow and
2095 // operations on the original actually affect the shadowing label.
2096 //
2097 // We should probably try to unify the escaping labels and the return
2098 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 int nof_escapes = node->escaping_targets()->length();
2100 List<ShadowTarget*> shadows(1 + nof_escapes);
2101
2102 // Add the shadow target for the function return.
2103 static const int kReturnShadowIndex = 0;
2104 shadows.Add(new ShadowTarget(&function_return_));
2105 bool function_return_was_shadowed = function_return_is_shadowed_;
2106 function_return_is_shadowed_ = true;
2107 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2108
2109 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002111 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 }
2113
2114 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002115 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002117 // Stop the introduced shadowing and count the number of required unlinks.
2118 // After shadowing stops, the original labels are unshadowed and the
2119 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 shadows[i]->StopShadowing();
2123 if (shadows[i]->is_linked()) nof_unlinks++;
2124 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 // Get an external reference to the handler address.
2128 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 // If we can fall off the end of the try block, unlink from the try
2131 // chain and set the state on the frame to FALLING.
2132 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002133 // The next handler address is on top of the frame.
2134 ASSERT(StackHandlerConstants::kNextOffset == 0);
2135 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002136 __ mov(r3, Operand(handler_address));
2137 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002138 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139
2140 // Fake a top of stack value (unneeded when FALLING) and set the
2141 // state in r2, then jump around the unlink blocks if any.
2142 __ mov(r0, Operand(Factory::undefined_value()));
2143 frame_->EmitPush(r0);
2144 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2145 if (nof_unlinks > 0) {
2146 finally_block.Jump();
2147 }
2148 }
2149
2150 // Generate code to unlink and set the state for the (formerly)
2151 // shadowing targets that have been jumped to.
2152 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002154 // If we have come from the shadowed return, the return value is
2155 // in (a non-refcounted reference to) r0. We must preserve it
2156 // until it is pushed.
2157 //
2158 // Because we can be jumping here (to spilled code) from
2159 // unspilled code, we need to reestablish a spilled frame at
2160 // this block.
2161 shadows[i]->Bind();
2162 frame_->SpillAll();
2163
2164 // Reload sp from the top handler, because some statements that
2165 // we break from (eg, for...in) may have left stuff on the
2166 // stack.
2167 __ mov(r3, Operand(handler_address));
2168 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002169 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170
2171 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002172 // handler address is currently on top of the frame.
2173 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 frame_->EmitPop(r1);
2175 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002176 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177
2178 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002179 // If this label shadowed the function return, materialize the
2180 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002181 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002182 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002184 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 }
2187 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002188 if (--nof_unlinks > 0) {
2189 // If this is not the last unlink block, jump around the next.
2190 finally_block.Jump();
2191 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 }
2193 }
2194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002198 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002199 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002200
2201 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002202 // and the state - while evaluating the finally block.
2203 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002205 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002207 if (has_valid_frame()) {
2208 // Restore state and return value or faked TOS.
2209 frame_->EmitPop(r2);
2210 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 }
2212
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213 // Generate code to jump to the right destination for all used
2214 // formerly shadowing targets. Deallocate each shadow target.
2215 for (int i = 0; i < shadows.length(); i++) {
2216 if (has_valid_frame() && shadows[i]->is_bound()) {
2217 JumpTarget* original = shadows[i]->other_target();
2218 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2219 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002220 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 skip.Branch(ne);
2222 frame_->PrepareForReturn();
2223 original->Jump();
2224 skip.Bind();
2225 } else {
2226 original->Branch(eq);
2227 }
2228 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002229 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002231 if (has_valid_frame()) {
2232 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002233 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2235 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002237 // Rethrow exception.
2238 frame_->EmitPush(r0);
2239 frame_->CallRuntime(Runtime::kReThrow, 1);
2240
2241 // Done.
2242 exit.Bind();
2243 }
2244 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245}
2246
2247
ager@chromium.org7c537e22008-10-16 08:43:32 +00002248void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249#ifdef DEBUG
2250 int original_height = frame_->height();
2251#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002252 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002254 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002255#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002256 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002257#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002258 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260}
2261
2262
ager@chromium.org7c537e22008-10-16 08:43:32 +00002263void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002264 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 ASSERT(boilerplate->IsBoilerplate());
2266
2267 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002268 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002269 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270
2271 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272 frame_->EmitPush(cp);
2273 frame_->CallRuntime(Runtime::kNewClosure, 2);
2274 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275}
2276
2277
ager@chromium.org7c537e22008-10-16 08:43:32 +00002278void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002279#ifdef DEBUG
2280 int original_height = frame_->height();
2281#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002282 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 Comment cmnt(masm_, "[ FunctionLiteral");
2284
2285 // Build the function boilerplate and instantiate it.
2286 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002287 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002288 if (HasStackOverflow()) {
2289 ASSERT(frame_->height() == original_height);
2290 return;
2291 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002293 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294}
2295
2296
ager@chromium.org7c537e22008-10-16 08:43:32 +00002297void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002299#ifdef DEBUG
2300 int original_height = frame_->height();
2301#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002302 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2304 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002305 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306}
2307
2308
ager@chromium.org7c537e22008-10-16 08:43:32 +00002309void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310#ifdef DEBUG
2311 int original_height = frame_->height();
2312#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002313 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002315 JumpTarget then;
2316 JumpTarget else_;
2317 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002318 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2319 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 then.Bind();
2322 LoadAndSpill(node->then_expression(), typeof_state());
2323 exit.Jump();
2324 else_.Bind();
2325 LoadAndSpill(node->else_expression(), typeof_state());
2326 exit.Bind();
2327 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328}
2329
2330
ager@chromium.org7c537e22008-10-16 08:43:32 +00002331void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002332 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002333 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002334 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002336 JumpTarget slow;
2337 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002338
2339 // Generate fast-case code for variables that might be shadowed by
2340 // eval-introduced variables. Eval is used a lot without
2341 // introducing variables. In those cases, we do not want to
2342 // perform a runtime call for all variables in the scope
2343 // containing the eval.
2344 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2345 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002346 // If there was no control flow to slow, we can exit early.
2347 if (!slow.is_linked()) {
2348 frame_->EmitPush(r0);
2349 return;
2350 }
2351
2352 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002353
2354 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2355 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2356 // Only generate the fast case for locals that rewrite to slots.
2357 // This rules out argument loads.
2358 if (potential_slot != NULL) {
2359 __ ldr(r0,
2360 ContextSlotOperandCheckExtensions(potential_slot,
2361 r1,
2362 r2,
2363 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002364 if (potential_slot->var()->mode() == Variable::CONST) {
2365 __ cmp(r0, Operand(Factory::the_hole_value()));
2366 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2367 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002369 // ContextSlotOperandCheckExtensions so we have to jump around
2370 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002372 }
2373 }
2374
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 slow.Bind();
2376 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002377 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002378 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379
ager@chromium.org7c537e22008-10-16 08:43:32 +00002380 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002381 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002382 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002383 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002385
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 done.Bind();
2387 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388
2389 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002390 // Note: We would like to keep the assert below, but it fires because of
2391 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002392 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002394 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002395 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002396 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002397 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002398 // Const slots may contain 'the hole' value (the constant hasn't been
2399 // initialized yet) which needs to be converted into the 'undefined'
2400 // value.
2401 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002402 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002403 __ cmp(r0, Operand(Factory::the_hole_value()));
2404 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002405 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 }
2407 }
2408}
2409
2410
ager@chromium.org381abbb2009-02-25 13:23:22 +00002411void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2412 TypeofState typeof_state,
2413 Register tmp,
2414 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002415 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002416 // Check that no extension objects have been created by calls to
2417 // eval from the current scope to the global scope.
2418 Register context = cp;
2419 Scope* s = scope();
2420 while (s != NULL) {
2421 if (s->num_heap_slots() > 0) {
2422 if (s->calls_eval()) {
2423 // Check that extension is NULL.
2424 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2425 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002426 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002427 }
2428 // Load next context in chain.
2429 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2430 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2431 context = tmp;
2432 }
2433 // If no outer scope calls eval, we do not need to check more
2434 // context extensions.
2435 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2436 s = s->outer_scope();
2437 }
2438
2439 if (s->is_eval_scope()) {
2440 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002441 if (!context.is(tmp)) {
2442 __ mov(tmp, Operand(context));
2443 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002444 __ bind(&next);
2445 // Terminate at global context.
2446 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2447 __ cmp(tmp2, Operand(Factory::global_context_map()));
2448 __ b(eq, &fast);
2449 // Check that extension is NULL.
2450 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2451 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002453 // Load next context in chain.
2454 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2455 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2456 __ b(&next);
2457 __ bind(&fast);
2458 }
2459
2460 // All extension objects were empty and it is safe to use a global
2461 // load IC call.
2462 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2463 // Load the global object.
2464 LoadGlobal();
2465 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 Result name = allocator_->Allocate(r2);
2467 ASSERT(name.is_valid()); // We are in spilled code.
2468 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002469 // Call IC stub.
2470 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002471 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002472 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002474 }
2475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 // Drop the global object. The result is in r0.
2477 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002478}
2479
2480
ager@chromium.org7c537e22008-10-16 08:43:32 +00002481void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482#ifdef DEBUG
2483 int original_height = frame_->height();
2484#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002485 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002486 Comment cmnt(masm_, "[ Slot");
2487 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002489}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490
ager@chromium.org7c537e22008-10-16 08:43:32 +00002491
2492void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493#ifdef DEBUG
2494 int original_height = frame_->height();
2495#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002496 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002497 Comment cmnt(masm_, "[ VariableProxy");
2498
2499 Variable* var = node->var();
2500 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002501 if (expr != NULL) {
2502 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002504 ASSERT(var->is_global());
2505 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002506 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002508 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509}
2510
2511
ager@chromium.org7c537e22008-10-16 08:43:32 +00002512void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002513#ifdef DEBUG
2514 int original_height = frame_->height();
2515#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002516 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002518 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002519 frame_->EmitPush(r0);
2520 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521}
2522
2523
ager@chromium.org7c537e22008-10-16 08:43:32 +00002524void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002525#ifdef DEBUG
2526 int original_height = frame_->height();
2527#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002528 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529 Comment cmnt(masm_, "[ RexExp Literal");
2530
2531 // Retrieve the literal array and check the allocated entry.
2532
2533 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002534 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535
2536 // Load the literals array of the function.
2537 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2538
2539 // Load the literal at the ast saved index.
2540 int literal_offset =
2541 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2542 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2543
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002544 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002546 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
2548 // If the entry is undefined we call the runtime system to computed
2549 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002551 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002552 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002553 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002555 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 frame_->EmitPush(r0);
2557 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002558 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002561 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 frame_->EmitPush(r2);
2563 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564}
2565
2566
2567// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002568// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569// Each created boilerplate is stored in the JSFunction and they are
2570// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002573 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002574 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 private:
2580 ObjectLiteral* node_;
2581};
2582
2583
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002584void DeferredObjectLiteral::Generate() {
2585 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002586
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002587 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002590 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002592 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002593 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002595 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002596 __ push(r0);
2597 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2598 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600}
2601
2602
ager@chromium.org7c537e22008-10-16 08:43:32 +00002603void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604#ifdef DEBUG
2605 int original_height = frame_->height();
2606#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002607 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 Comment cmnt(masm_, "[ ObjectLiteral");
2609
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002610 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611
2612 // Retrieve the literal array and check the allocated entry.
2613
2614 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002615 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616
2617 // Load the literals array of the function.
2618 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2619
2620 // Load the literal at the ast saved index.
2621 int literal_offset =
2622 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2623 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2624
2625 // Check whether we need to materialize the object literal boilerplate.
2626 // If so, jump to the deferred code.
2627 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002628 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
2631 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002632 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002633
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002635 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2636 if (node->depth() == 1) {
2637 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2638 }
2639 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002640 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002641 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642
2643 for (int i = 0; i < node->properties()->length(); i++) {
2644 ObjectLiteral::Property* property = node->properties()->at(i);
2645 Literal* key = property->key();
2646 Expression* value = property->value();
2647 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648 case ObjectLiteral::Property::CONSTANT:
2649 break;
2650 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2651 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2652 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 case ObjectLiteral::Property::COMPUTED: // fall through
2654 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655 frame_->EmitPush(r0); // dup the result
2656 LoadAndSpill(key);
2657 LoadAndSpill(value);
2658 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002659 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002660 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 break;
2662 }
2663 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 frame_->EmitPush(r0);
2665 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002666 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667 frame_->EmitPush(r0);
2668 LoadAndSpill(value);
2669 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002670 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 break;
2672 }
2673 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002674 frame_->EmitPush(r0);
2675 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002676 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 frame_->EmitPush(r0);
2678 LoadAndSpill(value);
2679 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002680 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 break;
2682 }
2683 }
2684 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 ASSERT(frame_->height() == original_height + 1);
2686}
2687
2688
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002689// This deferred code stub will be used for creating the boilerplate
2690// by calling Runtime_CreateArrayLiteralBoilerplate.
2691// Each created boilerplate is stored in the JSFunction and they are
2692// therefore context dependent.
2693class DeferredArrayLiteral: public DeferredCode {
2694 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002695 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002696 set_comment("[ DeferredArrayLiteral");
2697 }
2698
2699 virtual void Generate();
2700
2701 private:
2702 ArrayLiteral* node_;
2703};
2704
2705
2706void DeferredArrayLiteral::Generate() {
2707 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002708
2709 // If the entry is undefined we call the runtime system to computed
2710 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002711 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002712 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002713 // Literal index (1).
2714 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002715 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002716 // Constant properties (2).
2717 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002718 __ push(r0);
2719 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2720 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002721 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002722}
2723
2724
ager@chromium.org7c537e22008-10-16 08:43:32 +00002725void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002726#ifdef DEBUG
2727 int original_height = frame_->height();
2728#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002729 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002731
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002732 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002733
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002734 // Retrieve the literal array and check the allocated entry.
2735
2736 // Load the function of this activation.
2737 __ ldr(r1, frame_->Function());
2738
2739 // Load the literals array of the function.
2740 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2741
2742 // Load the literal at the ast saved index.
2743 int literal_offset =
2744 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2745 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2746
2747 // Check whether we need to materialize the object literal boilerplate.
2748 // If so, jump to the deferred code.
2749 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002750 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002751 deferred->BindExit();
2752
2753 // Push the object literal boilerplate.
2754 frame_->EmitPush(r2);
2755
2756 // Clone the boilerplate object.
2757 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2758 if (node->depth() == 1) {
2759 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2760 }
2761 frame_->CallRuntime(clone_function_id, 1);
2762 frame_->EmitPush(r0); // save the result
2763 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002764
2765 // Generate code to set the elements in the array that are not
2766 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 for (int i = 0; i < node->values()->length(); i++) {
2768 Expression* value = node->values()->at(i);
2769
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002770 // If value is a literal the property value is already set in the
2771 // boilerplate object.
2772 if (value->AsLiteral() != NULL) continue;
2773 // If value is a materialized literal the property value is already set
2774 // in the boilerplate object if it is simple.
2775 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002777 // The property must be set by generated code.
2778 LoadAndSpill(value);
2779 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002781 // Fetch the object literal.
2782 __ ldr(r1, frame_->Top());
2783 // Get the elements array.
2784 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002786 // Write to the indexed properties array.
2787 int offset = i * kPointerSize + Array::kHeaderSize;
2788 __ str(r0, FieldMemOperand(r1, offset));
2789
2790 // Update the write barrier for the array address.
2791 __ mov(r3, Operand(offset));
2792 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002794 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795}
2796
2797
ager@chromium.org32912102009-01-16 10:38:43 +00002798void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002799#ifdef DEBUG
2800 int original_height = frame_->height();
2801#endif
2802 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002803 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002804 // Call runtime routine to allocate the catch extension object and
2805 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002806 Comment cmnt(masm_, "[ CatchExtensionObject");
2807 LoadAndSpill(node->key());
2808 LoadAndSpill(node->value());
2809 Result result =
2810 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2811 frame_->EmitPush(result.reg());
2812 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002813}
2814
2815
ager@chromium.org7c537e22008-10-16 08:43:32 +00002816void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002817#ifdef DEBUG
2818 int original_height = frame_->height();
2819#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002820 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002822 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002823
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824 { Reference target(this, node->target());
2825 if (target.is_illegal()) {
2826 // Fool the virtual frame into thinking that we left the assignment's
2827 // value on the frame.
2828 __ mov(r0, Operand(Smi::FromInt(0)));
2829 frame_->EmitPush(r0);
2830 ASSERT(frame_->height() == original_height + 1);
2831 return;
2832 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002834 if (node->op() == Token::ASSIGN ||
2835 node->op() == Token::INIT_VAR ||
2836 node->op() == Token::INIT_CONST) {
2837 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002840 // +=, *= and similar binary assignments.
2841 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002842 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2843 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002844 bool overwrite =
2845 (node->value()->AsBinaryOperation() != NULL &&
2846 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002848 SmiOperation(node->binary_op(),
2849 literal->handle(),
2850 false,
2851 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002852 frame_->EmitPush(r0);
2853
2854 } else {
2855 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002856 GenericBinaryOperation(node->binary_op(),
2857 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002858 frame_->EmitPush(r0);
2859 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002862 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2863 if (var != NULL &&
2864 (var->mode() == Variable::CONST) &&
2865 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2866 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002869 CodeForSourcePosition(node->position());
2870 if (node->op() == Token::INIT_CONST) {
2871 // Dynamic constant initializations must use the function context
2872 // and initialize the actual constant declared. Dynamic variable
2873 // initializations are simply assignments and use SetValue.
2874 target.SetValue(CONST_INIT);
2875 } else {
2876 target.SetValue(NOT_CONST_INIT);
2877 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878 }
2879 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002880 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881}
2882
2883
ager@chromium.org7c537e22008-10-16 08:43:32 +00002884void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002885#ifdef DEBUG
2886 int original_height = frame_->height();
2887#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002888 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889 Comment cmnt(masm_, "[ Throw");
2890
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002891 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002892 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 frame_->CallRuntime(Runtime::kThrow, 1);
2894 frame_->EmitPush(r0);
2895 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896}
2897
2898
ager@chromium.org7c537e22008-10-16 08:43:32 +00002899void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002900#ifdef DEBUG
2901 int original_height = frame_->height();
2902#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002903 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002905
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002906 { Reference property(this, node);
2907 property.GetValueAndSpill(typeof_state());
2908 }
2909 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910}
2911
2912
ager@chromium.org7c537e22008-10-16 08:43:32 +00002913void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002914#ifdef DEBUG
2915 int original_height = frame_->height();
2916#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002917 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 Comment cmnt(masm_, "[ Call");
2919
2920 ZoneList<Expression*>* args = node->arguments();
2921
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002922 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 // Standard function call.
2924
2925 // Check if the function is a variable or a property.
2926 Expression* function = node->expression();
2927 Variable* var = function->AsVariableProxy()->AsVariable();
2928 Property* property = function->AsProperty();
2929
2930 // ------------------------------------------------------------------------
2931 // Fast-case: Use inline caching.
2932 // ---
2933 // According to ECMA-262, section 11.2.3, page 44, the function to call
2934 // must be resolved after the arguments have been evaluated. The IC code
2935 // automatically handles this by loading the arguments before the function
2936 // is resolved in cache misses (this also holds for megamorphic calls).
2937 // ------------------------------------------------------------------------
2938
2939 if (var != NULL && !var->is_this() && var->is_global()) {
2940 // ----------------------------------
2941 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2942 // ----------------------------------
2943
2944 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002945 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002947
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002948 // Pass the global object as the receiver and let the IC stub
2949 // patch the stack to use the global proxy as 'this' in the
2950 // invoked function.
2951 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952
2953 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954 int arg_count = args->length();
2955 for (int i = 0; i < arg_count; i++) {
2956 LoadAndSpill(args->at(i));
2957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958
2959 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002960 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2961 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002962 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002963 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
2964 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002965 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002967 frame_->Drop();
2968 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969
2970 } else if (var != NULL && var->slot() != NULL &&
2971 var->slot()->type() == Slot::LOOKUP) {
2972 // ----------------------------------
2973 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2974 // ----------------------------------
2975
2976 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002977 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002978 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002979 frame_->EmitPush(r0);
2980 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 // r0: slot value; r1: receiver
2982
2983 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002984 frame_->EmitPush(r0); // function
2985 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986
2987 // Call the function.
2988 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002989 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002990
2991 } else if (property != NULL) {
2992 // Check if the key is a literal string.
2993 Literal* literal = property->key()->AsLiteral();
2994
2995 if (literal != NULL && literal->handle()->IsSymbol()) {
2996 // ------------------------------------------------------------------
2997 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2998 // ------------------------------------------------------------------
2999
3000 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003001 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003002 frame_->EmitPush(r0);
3003 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003004
3005 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003006 int arg_count = args->length();
3007 for (int i = 0; i < arg_count; i++) {
3008 LoadAndSpill(args->at(i));
3009 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003010
3011 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003012 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3013 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003014 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003015 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003016 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017
3018 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003021 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022
3023 } else {
3024 // -------------------------------------------
3025 // JavaScript example: 'array[index](1, 2, 3)'
3026 // -------------------------------------------
3027
3028 // Load the function to call from the property through a reference.
3029 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003030 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031
3032 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003033 if (property->is_synthetic()) {
3034 LoadGlobalReceiver(r0);
3035 } else {
3036 __ ldr(r0, frame_->ElementAt(ref.size()));
3037 frame_->EmitPush(r0);
3038 }
3039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040 // Call the function.
3041 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003042 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003043 }
3044
3045 } else {
3046 // ----------------------------------
3047 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3048 // ----------------------------------
3049
3050 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003052
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003053 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003054 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056 // Call the function.
3057 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003058 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003060 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061}
3062
3063
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003064void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003065#ifdef DEBUG
3066 int original_height = frame_->height();
3067#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003068 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003069 Comment cmnt(masm_, "[ CallEval");
3070
3071 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3072 // the function we need to call and the receiver of the call.
3073 // Then we call the resolved function using the given arguments.
3074
3075 ZoneList<Expression*>* args = node->arguments();
3076 Expression* function = node->expression();
3077
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003078 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003079
3080 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003081 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003082 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003083 frame_->EmitPush(r2); // Slot for receiver
3084 int arg_count = args->length();
3085 for (int i = 0; i < arg_count; i++) {
3086 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003087 }
3088
3089 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3091 frame_->EmitPush(r1);
3092 if (arg_count > 0) {
3093 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3094 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003095 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003097 }
3098
3099 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003100 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003101
3102 // Touch up stack with the right values for the function and the receiver.
3103 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003105 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003106 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003107
3108 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003109 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003110
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003111 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3112 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003114
3115 __ ldr(cp, frame_->Context());
3116 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003117 frame_->Drop();
3118 frame_->EmitPush(r0);
3119 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003120}
3121
3122
ager@chromium.org7c537e22008-10-16 08:43:32 +00003123void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003124#ifdef DEBUG
3125 int original_height = frame_->height();
3126#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003127 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003128 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003129 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130
3131 // According to ECMA-262, section 11.2.2, page 44, the function
3132 // expression in new calls must be evaluated before the
3133 // arguments. This is different from ordinary calls, where the
3134 // actual function to call is resolved after the arguments have been
3135 // evaluated.
3136
3137 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003138 // receiver. There is no need to use the global proxy here because
3139 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003141 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142
3143 // Push the arguments ("left-to-right") on the stack.
3144 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003145 int arg_count = args->length();
3146 for (int i = 0; i < arg_count; i++) {
3147 LoadAndSpill(args->at(i));
3148 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149
mads.s.ager31e71382008-08-13 09:32:07 +00003150 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003151 Result num_args = allocator_->Allocate(r0);
3152 ASSERT(num_args.is_valid());
3153 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003154
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003155 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156 Result function = allocator_->Allocate(r1);
3157 ASSERT(function.is_valid());
3158 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 // Call the construct call builtin that handles allocation and
3161 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003162 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003163 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3164 Result result = frame_->CallCodeObject(ic,
3165 RelocInfo::CONSTRUCT_CALL,
3166 &num_args,
3167 &function,
3168 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003169
3170 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003171 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003172 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173}
3174
3175
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003176void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3177 VirtualFrame::SpilledScope spilled_scope;
3178 ASSERT(args->length() == 1);
3179 LoadAndSpill(args->at(0)); // Load the object.
3180 frame_->CallRuntime(Runtime::kClassOf, 1);
3181 frame_->EmitPush(r0);
3182}
3183
3184
ager@chromium.org7c537e22008-10-16 08:43:32 +00003185void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003186 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003188 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003189 LoadAndSpill(args->at(0));
3190 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003191 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003192 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003193 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003194 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3195 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003196 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003197 // Load the value.
3198 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003199 leave.Bind();
3200 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003201}
3202
3203
ager@chromium.org7c537e22008-10-16 08:43:32 +00003204void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003205 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003206 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003207 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003208 LoadAndSpill(args->at(0)); // Load the object.
3209 LoadAndSpill(args->at(1)); // Load the value.
3210 frame_->EmitPop(r0); // r0 contains value
3211 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003212 // if (object->IsSmi()) return object.
3213 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003214 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003215 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3216 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003217 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 // Store the value.
3219 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3220 // Update the write barrier.
3221 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3222 __ RecordWrite(r1, r2, r3);
3223 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003224 leave.Bind();
3225 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226}
3227
3228
ager@chromium.org7c537e22008-10-16 08:43:32 +00003229void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003230 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003231 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003232 LoadAndSpill(args->at(0));
3233 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003234 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235 cc_reg_ = eq;
3236}
3237
3238
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003239void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003240 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003241 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3242 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003243#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003244 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003245 LoadAndSpill(args->at(1));
3246 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003247 __ CallRuntime(Runtime::kLog, 2);
3248 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003249#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003250 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003251 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003252}
3253
3254
ager@chromium.org7c537e22008-10-16 08:43:32 +00003255void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003256 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003257 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 LoadAndSpill(args->at(0));
3259 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003260 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3261 cc_reg_ = eq;
3262}
3263
3264
kasper.lund7276f142008-07-30 08:49:36 +00003265// This should generate code that performs a charCodeAt() call or returns
3266// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3267// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003268void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003269 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003270 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003271 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003272 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003273}
3274
3275
ager@chromium.org7c537e22008-10-16 08:43:32 +00003276void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003277 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003278 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003280 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003281 // We need the CC bits to come out as not_equal in the case where the
3282 // object is a smi. This can't be done with the usual test opcode so
3283 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003285 __ and_(r1, r0, Operand(kSmiTagMask));
3286 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003287 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003288 // It is a heap object - get the map. Check if the object is a JS array.
3289 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003290 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 cc_reg_ = eq;
3292}
3293
3294
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003295void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3296 VirtualFrame::SpilledScope spilled_scope;
3297 ASSERT(args->length() == 0);
3298 frame_->CallRuntime(Runtime::kIsConstructCall, 0);
3299 frame_->EmitPush(r0);
3300}
3301
3302
ager@chromium.org7c537e22008-10-16 08:43:32 +00003303void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003304 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 ASSERT(args->length() == 0);
3306
mads.s.ager31e71382008-08-13 09:32:07 +00003307 // Seed the result with the formal parameters count, which will be used
3308 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003309 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3310
3311 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003312 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 frame_->CallStub(&stub, 0);
3314 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003315}
3316
3317
ager@chromium.org7c537e22008-10-16 08:43:32 +00003318void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003319 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320 ASSERT(args->length() == 1);
3321
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003322 // Satisfy contract with ArgumentsAccessStub:
3323 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003324 LoadAndSpill(args->at(0));
3325 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003326 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327
3328 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003329 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003330 frame_->CallStub(&stub, 0);
3331 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332}
3333
3334
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003335void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3336 VirtualFrame::SpilledScope spilled_scope;
3337 ASSERT(args->length() == 0);
3338 __ Call(ExternalReference::random_positive_smi_function().address(),
3339 RelocInfo::RUNTIME_ENTRY);
3340 frame_->EmitPush(r0);
3341}
3342
3343
3344void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3345 VirtualFrame::SpilledScope spilled_scope;
3346 LoadAndSpill(args->at(0));
3347 switch (op) {
3348 case SIN:
3349 frame_->CallRuntime(Runtime::kMath_sin, 1);
3350 break;
3351 case COS:
3352 frame_->CallRuntime(Runtime::kMath_cos, 1);
3353 break;
3354 }
3355 frame_->EmitPush(r0);
3356}
3357
3358
ager@chromium.org7c537e22008-10-16 08:43:32 +00003359void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003360 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003361 ASSERT(args->length() == 2);
3362
3363 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003364 LoadAndSpill(args->at(0));
3365 LoadAndSpill(args->at(1));
3366 frame_->EmitPop(r0);
3367 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003368 __ cmp(r0, Operand(r1));
3369 cc_reg_ = eq;
3370}
3371
3372
ager@chromium.org7c537e22008-10-16 08:43:32 +00003373void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003374#ifdef DEBUG
3375 int original_height = frame_->height();
3376#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003377 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003378 if (CheckForInlineRuntimeCall(node)) {
3379 ASSERT((has_cc() && frame_->height() == original_height) ||
3380 (!has_cc() && frame_->height() == original_height + 1));
3381 return;
3382 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383
3384 ZoneList<Expression*>* args = node->arguments();
3385 Comment cmnt(masm_, "[ CallRuntime");
3386 Runtime::Function* function = node->function();
3387
ager@chromium.org41826e72009-03-30 13:30:57 +00003388 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003389 // Prepare stack for calling JS runtime function.
3390 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003391 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003392 // Push the builtins object found in the current global object.
3393 __ ldr(r1, GlobalObject());
3394 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003395 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003396 }
mads.s.ager31e71382008-08-13 09:32:07 +00003397
ager@chromium.org41826e72009-03-30 13:30:57 +00003398 // Push the arguments ("left-to-right").
3399 int arg_count = args->length();
3400 for (int i = 0; i < arg_count; i++) {
3401 LoadAndSpill(args->at(i));
3402 }
mads.s.ager31e71382008-08-13 09:32:07 +00003403
ager@chromium.org41826e72009-03-30 13:30:57 +00003404 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003406 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3407 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003408 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003409 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003410 frame_->Drop();
3411 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003412 } else {
3413 // Call the C runtime function.
3414 frame_->CallRuntime(function, arg_count);
3415 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003416 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003417 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418}
3419
3420
ager@chromium.org7c537e22008-10-16 08:43:32 +00003421void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003422#ifdef DEBUG
3423 int original_height = frame_->height();
3424#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003425 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003426 Comment cmnt(masm_, "[ UnaryOperation");
3427
3428 Token::Value op = node->op();
3429
3430 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003431 LoadConditionAndSpill(node->expression(),
3432 NOT_INSIDE_TYPEOF,
3433 false_target(),
3434 true_target(),
3435 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003436 cc_reg_ = NegateCondition(cc_reg_);
3437
3438 } else if (op == Token::DELETE) {
3439 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003440 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003442 LoadAndSpill(property->obj());
3443 LoadAndSpill(property->key());
3444 Result arg_count = allocator_->Allocate(r0);
3445 ASSERT(arg_count.is_valid());
3446 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3447 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448
mads.s.ager31e71382008-08-13 09:32:07 +00003449 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450 Slot* slot = variable->slot();
3451 if (variable->is_global()) {
3452 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003453 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003454 frame_->EmitPush(r0);
3455 Result arg_count = allocator_->Allocate(r0);
3456 ASSERT(arg_count.is_valid());
3457 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3458 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459
3460 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3461 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003462 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003463 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003464 frame_->EmitPush(r0);
3465 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003467 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003468 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003469 frame_->EmitPush(r0);
3470 Result arg_count = allocator_->Allocate(r0);
3471 ASSERT(arg_count.is_valid());
3472 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3473 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003474
mads.s.ager31e71382008-08-13 09:32:07 +00003475 } else {
3476 // Default: Result of deleting non-global, not dynamically
3477 // introduced variables is false.
3478 __ mov(r0, Operand(Factory::false_value()));
3479 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003480
3481 } else {
3482 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483 LoadAndSpill(node->expression()); // may have side-effects
3484 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485 __ mov(r0, Operand(Factory::true_value()));
3486 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003487 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488
3489 } else if (op == Token::TYPEOF) {
3490 // Special case for loading the typeof expression; see comment on
3491 // LoadTypeofExpression().
3492 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003493 frame_->CallRuntime(Runtime::kTypeof, 1);
3494 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003495
3496 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003497 LoadAndSpill(node->expression());
3498 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499 switch (op) {
3500 case Token::NOT:
3501 case Token::DELETE:
3502 case Token::TYPEOF:
3503 UNREACHABLE(); // handled above
3504 break;
3505
3506 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003507 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003508 (node->expression()->AsBinaryOperation() != NULL &&
3509 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003510 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003511 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512 break;
3513 }
3514
3515 case Token::BIT_NOT: {
3516 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003517 JumpTarget smi_label;
3518 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003520 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003522 frame_->EmitPush(r0);
3523 Result arg_count = allocator_->Allocate(r0);
3524 ASSERT(arg_count.is_valid());
3525 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3526 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003528 continue_label.Jump();
3529 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530 __ mvn(r0, Operand(r0));
3531 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003532 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533 break;
3534 }
3535
3536 case Token::VOID:
3537 // since the stack top is cached in r0, popping and then
3538 // pushing a value can be done by just writing to r0.
3539 __ mov(r0, Operand(Factory::undefined_value()));
3540 break;
3541
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003542 case Token::ADD: {
3543 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003544 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003545 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003546 continue_label.Branch(eq);
3547 frame_->EmitPush(r0);
3548 Result arg_count = allocator_->Allocate(r0);
3549 ASSERT(arg_count.is_valid());
3550 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3551 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3552 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555 default:
3556 UNREACHABLE();
3557 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003558 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003559 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003560 ASSERT((has_cc() && frame_->height() == original_height) ||
3561 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562}
3563
3564
ager@chromium.org7c537e22008-10-16 08:43:32 +00003565void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003566#ifdef DEBUG
3567 int original_height = frame_->height();
3568#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003569 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 Comment cmnt(masm_, "[ CountOperation");
3571
3572 bool is_postfix = node->is_postfix();
3573 bool is_increment = node->op() == Token::INC;
3574
3575 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3576 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3577
3578 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003579 if (is_postfix) {
3580 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003581 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003582 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583
3584 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003585 if (target.is_illegal()) {
3586 // Spoof the virtual frame to have the expected height (one higher
3587 // than on entry).
3588 if (!is_postfix) {
3589 __ mov(r0, Operand(Smi::FromInt(0)));
3590 frame_->EmitPush(r0);
3591 }
3592 ASSERT(frame_->height() == original_height + 1);
3593 return;
3594 }
3595 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3596 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003597
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003598 JumpTarget slow;
3599 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003600
3601 // Load the value (1) into register r1.
3602 __ mov(r1, Operand(Smi::FromInt(1)));
3603
3604 // Check for smi operand.
3605 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003606 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607
3608 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003609 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003610 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003611 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612
3613 // Perform optimistic increment/decrement.
3614 if (is_increment) {
3615 __ add(r0, r0, Operand(r1), SetCC);
3616 } else {
3617 __ sub(r0, r0, Operand(r1), SetCC);
3618 }
3619
3620 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003621 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622
3623 // Revert optimistic increment/decrement.
3624 if (is_increment) {
3625 __ sub(r0, r0, Operand(r1));
3626 } else {
3627 __ add(r0, r0, Operand(r1));
3628 }
3629
3630 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003631 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003632 {
3633 // Convert the operand to a number.
3634 frame_->EmitPush(r0);
3635 Result arg_count = allocator_->Allocate(r0);
3636 ASSERT(arg_count.is_valid());
3637 __ mov(arg_count.reg(), Operand(0));
3638 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3639 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003641 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643 }
3644
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003645 // Compute the new value.
3646 __ mov(r1, Operand(Smi::FromInt(1)));
3647 frame_->EmitPush(r0);
3648 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003649 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003650 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003652 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653 }
3654
3655 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003656 exit.Bind();
3657 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003658 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003659 }
3660
3661 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003662 if (is_postfix) frame_->EmitPop(r0);
3663 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003664}
3665
3666
ager@chromium.org7c537e22008-10-16 08:43:32 +00003667void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003668#ifdef DEBUG
3669 int original_height = frame_->height();
3670#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003671 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 Comment cmnt(masm_, "[ BinaryOperation");
3673 Token::Value op = node->op();
3674
3675 // According to ECMA-262 section 11.11, page 58, the binary logical
3676 // operators must yield the result of one of the two expressions
3677 // before any ToBoolean() conversions. This means that the value
3678 // produced by a && or || operator is not necessarily a boolean.
3679
3680 // NOTE: If the left hand side produces a materialized value (not in
3681 // the CC register), we force the right hand side to do the
3682 // same. This is necessary because we may have to branch to the exit
3683 // after evaluating the left hand side (due to the shortcut
3684 // semantics), but the compiler must (statically) know if the result
3685 // of compiling the binary operation is materialized or not.
3686
3687 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003688 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003689 LoadConditionAndSpill(node->left(),
3690 NOT_INSIDE_TYPEOF,
3691 &is_true,
3692 false_target(),
3693 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003694 if (has_cc()) {
3695 Branch(false, false_target());
3696
3697 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 is_true.Bind();
3699 LoadConditionAndSpill(node->right(),
3700 NOT_INSIDE_TYPEOF,
3701 true_target(),
3702 false_target(),
3703 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003704
3705 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003706 JumpTarget pop_and_continue;
3707 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003709 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003710 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 // Avoid popping the result if it converts to 'false' using the
3712 // standard ToBoolean() conversion as described in ECMA-262,
3713 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003714 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715 Branch(false, &exit);
3716
3717 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003718 pop_and_continue.Bind();
3719 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720
3721 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003722 is_true.Bind();
3723 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724
3725 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 }
3728
3729 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003730 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 LoadConditionAndSpill(node->left(),
3732 NOT_INSIDE_TYPEOF,
3733 true_target(),
3734 &is_false,
3735 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 if (has_cc()) {
3737 Branch(true, true_target());
3738
3739 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003740 is_false.Bind();
3741 LoadConditionAndSpill(node->right(),
3742 NOT_INSIDE_TYPEOF,
3743 true_target(),
3744 false_target(),
3745 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003746
3747 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003748 JumpTarget pop_and_continue;
3749 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003751 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003753 // Avoid popping the result if it converts to 'true' using the
3754 // standard ToBoolean() conversion as described in ECMA-262,
3755 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003756 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757 Branch(true, &exit);
3758
3759 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003760 pop_and_continue.Bind();
3761 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762
3763 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 is_false.Bind();
3765 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
3767 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769 }
3770
3771 } else {
3772 // Optimize for the case where (at least) one of the expressions
3773 // is a literal small integer.
3774 Literal* lliteral = node->left()->AsLiteral();
3775 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003776 // NOTE: The code below assumes that the slow cases (calls to runtime)
3777 // never return a constant/immutable object.
3778 bool overwrite_left =
3779 (node->left()->AsBinaryOperation() != NULL &&
3780 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3781 bool overwrite_right =
3782 (node->right()->AsBinaryOperation() != NULL &&
3783 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784
3785 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003786 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003787 SmiOperation(node->op(),
3788 rliteral->handle(),
3789 false,
3790 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003791
3792 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003793 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003794 SmiOperation(node->op(),
3795 lliteral->handle(),
3796 true,
3797 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798
3799 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003800 OverwriteMode overwrite_mode = NO_OVERWRITE;
3801 if (overwrite_left) {
3802 overwrite_mode = OVERWRITE_LEFT;
3803 } else if (overwrite_right) {
3804 overwrite_mode = OVERWRITE_RIGHT;
3805 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 LoadAndSpill(node->left());
3807 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003808 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003812 ASSERT((has_cc() && frame_->height() == original_height) ||
3813 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814}
3815
3816
ager@chromium.org7c537e22008-10-16 08:43:32 +00003817void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818#ifdef DEBUG
3819 int original_height = frame_->height();
3820#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003821 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003822 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823 frame_->EmitPush(r0);
3824 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825}
3826
3827
ager@chromium.org7c537e22008-10-16 08:43:32 +00003828void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003829#ifdef DEBUG
3830 int original_height = frame_->height();
3831#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003832 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003833 Comment cmnt(masm_, "[ CompareOperation");
3834
3835 // Get the expressions from the node.
3836 Expression* left = node->left();
3837 Expression* right = node->right();
3838 Token::Value op = node->op();
3839
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003840 // To make null checks efficient, we check if either left or right is the
3841 // literal 'null'. If so, we optimize the code by inlining a null check
3842 // instead of calling the (very) general runtime routine for checking
3843 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003845 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003846 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003847 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003848 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3849 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003851 LoadAndSpill(left_is_null ? right : left);
3852 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 __ cmp(r0, Operand(Factory::null_value()));
3854
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003855 // The 'null' value is only equal to 'undefined' if using non-strict
3856 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003858 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003859
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003861 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003866 // It can be an undetectable object.
3867 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3868 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3869 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3870 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 }
3872
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003874 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875 return;
3876 }
3877 }
3878
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003879 // To make typeof testing for natives implemented in JavaScript really
3880 // efficient, we generate special code for expressions of the form:
3881 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 UnaryOperation* operation = left->AsUnaryOperation();
3883 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3884 (operation != NULL && operation->op() == Token::TYPEOF) &&
3885 (right->AsLiteral() != NULL &&
3886 right->AsLiteral()->handle()->IsString())) {
3887 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3888
mads.s.ager31e71382008-08-13 09:32:07 +00003889 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003891 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892
3893 if (check->Equals(Heap::number_symbol())) {
3894 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003895 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3897 __ cmp(r1, Operand(Factory::heap_number_map()));
3898 cc_reg_ = eq;
3899
3900 } else if (check->Equals(Heap::string_symbol())) {
3901 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003902 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903
3904 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3905
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003906 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3908 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3909 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003910 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911
3912 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3913 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3914 cc_reg_ = lt;
3915
3916 } else if (check->Equals(Heap::boolean_symbol())) {
3917 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 __ cmp(r1, Operand(Factory::false_value()));
3920 cc_reg_ = eq;
3921
3922 } else if (check->Equals(Heap::undefined_symbol())) {
3923 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003924 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925
3926 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003927 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003929 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3931 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3932 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3933 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3934
3935 cc_reg_ = eq;
3936
3937 } else if (check->Equals(Heap::function_symbol())) {
3938 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003939 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003940 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941 cc_reg_ = eq;
3942
3943 } else if (check->Equals(Heap::object_symbol())) {
3944 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003946
3947 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3948 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003949 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003951 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003952 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3953 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3954 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003955 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956
3957 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3958 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003959 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3961 cc_reg_ = le;
3962
3963 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003964 // Uncommon case: typeof testing against a string literal that is
3965 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003966 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 ASSERT(!has_valid_frame() ||
3969 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003970 return;
3971 }
3972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003973 switch (op) {
3974 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003975 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976 break;
3977
3978 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003979 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 break;
3981
3982 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003983 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 break;
3985
3986 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003987 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003988 break;
3989
3990 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003991 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992 break;
3993
3994 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003995 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996 break;
3997
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003998 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003999 LoadAndSpill(left);
4000 LoadAndSpill(right);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004001 Result arg_count = allocator_->Allocate(r0);
4002 ASSERT(arg_count.is_valid());
4003 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4004 Result result = frame_->InvokeBuiltin(Builtins::IN,
4005 CALL_JS,
4006 &arg_count,
4007 2);
4008 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004009 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004012 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004013 LoadAndSpill(left);
4014 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004015 InstanceofStub stub;
4016 Result result = frame_->CallStub(&stub, 2);
4017 // At this point if instanceof succeeded then r0 == 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004018 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004019 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004021 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004022
4023 default:
4024 UNREACHABLE();
4025 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004026 ASSERT((has_cc() && frame_->height() == original_height) ||
4027 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004028}
4029
4030
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031#ifdef DEBUG
4032bool CodeGenerator::HasValidEntryRegisters() { return true; }
4033#endif
4034
4035
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004037#define __ ACCESS_MASM(masm)
4038
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039
ager@chromium.org7c537e22008-10-16 08:43:32 +00004040Handle<String> Reference::GetName() {
4041 ASSERT(type_ == NAMED);
4042 Property* property = expression_->AsProperty();
4043 if (property == NULL) {
4044 // Global variable reference treated as a named property reference.
4045 VariableProxy* proxy = expression_->AsVariableProxy();
4046 ASSERT(proxy->AsVariable() != NULL);
4047 ASSERT(proxy->AsVariable()->is_global());
4048 return proxy->name();
4049 } else {
4050 Literal* raw_name = property->key()->AsLiteral();
4051 ASSERT(raw_name != NULL);
4052 return Handle<String>(String::cast(*raw_name->handle()));
4053 }
4054}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004055
ager@chromium.org7c537e22008-10-16 08:43:32 +00004056
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004057void Reference::GetValueAndSpill(TypeofState typeof_state) {
4058 ASSERT(cgen_->in_spilled_code());
4059 cgen_->set_in_spilled_code(false);
4060 GetValue(typeof_state);
4061 cgen_->frame()->SpillAll();
4062 cgen_->set_in_spilled_code(true);
4063}
4064
4065
ager@chromium.org7c537e22008-10-16 08:43:32 +00004066void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004067 ASSERT(!cgen_->in_spilled_code());
4068 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004069 ASSERT(!is_illegal());
4070 ASSERT(!cgen_->has_cc());
4071 MacroAssembler* masm = cgen_->masm();
4072 Property* property = expression_->AsProperty();
4073 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004074 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004075 }
4076
4077 switch (type_) {
4078 case SLOT: {
4079 Comment cmnt(masm, "[ Load from Slot");
4080 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4081 ASSERT(slot != NULL);
4082 cgen_->LoadFromSlot(slot, typeof_state);
4083 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004084 }
4085
ager@chromium.org7c537e22008-10-16 08:43:32 +00004086 case NAMED: {
4087 // TODO(1241834): Make sure that this it is safe to ignore the
4088 // distinction between expressions in a typeof and not in a typeof. If
4089 // there is a chance that reference errors can be thrown below, we
4090 // must distinguish between the two kinds of loads (typeof expression
4091 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004092 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004093 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004094 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004095 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004096 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4097 // Setup the name register.
4098 Result name_reg = cgen_->allocator()->Allocate(r2);
4099 ASSERT(name_reg.is_valid());
4100 __ mov(name_reg.reg(), Operand(name));
4101 ASSERT(var == NULL || var->is_global());
4102 RelocInfo::Mode rmode = (var == NULL)
4103 ? RelocInfo::CODE_TARGET
4104 : RelocInfo::CODE_TARGET_CONTEXT;
4105 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4106 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004107 break;
4108 }
4109
4110 case KEYED: {
4111 // TODO(1241834): Make sure that this it is safe to ignore the
4112 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004113
4114 // TODO(181): Implement inlined version of array indexing once
4115 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004117 Comment cmnt(masm, "[ Load from keyed Property");
4118 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004119 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004120 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004121 ASSERT(var == NULL || var->is_global());
4122 RelocInfo::Mode rmode = (var == NULL)
4123 ? RelocInfo::CODE_TARGET
4124 : RelocInfo::CODE_TARGET_CONTEXT;
4125 Result answer = frame->CallCodeObject(ic, rmode, 0);
4126 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004127 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004128 }
4129
4130 default:
4131 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004132 }
4133}
4134
4135
ager@chromium.org7c537e22008-10-16 08:43:32 +00004136void Reference::SetValue(InitState init_state) {
4137 ASSERT(!is_illegal());
4138 ASSERT(!cgen_->has_cc());
4139 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004140 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004141 Property* property = expression_->AsProperty();
4142 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004143 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004144 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004145
ager@chromium.org7c537e22008-10-16 08:43:32 +00004146 switch (type_) {
4147 case SLOT: {
4148 Comment cmnt(masm, "[ Store to Slot");
4149 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4150 ASSERT(slot != NULL);
4151 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004152 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004153
ager@chromium.org7c537e22008-10-16 08:43:32 +00004154 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004155 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004156 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004157 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004158
ager@chromium.org7c537e22008-10-16 08:43:32 +00004159 if (init_state == CONST_INIT) {
4160 // Same as the case for a normal store, but ignores attribute
4161 // (e.g. READ_ONLY) of context slot so that we can initialize
4162 // const properties (introduced via eval("const foo = (some
4163 // expr);")). Also, uses the current function context instead of
4164 // the top context.
4165 //
4166 // Note that we must declare the foo upon entry of eval(), via a
4167 // context slot declaration, but we cannot initialize it at the
4168 // same time, because the const declaration may be at the end of
4169 // the eval code (sigh...) and the const variable may have been
4170 // used before (where its value is 'undefined'). Thus, we can only
4171 // do the initialization when we actually encounter the expression
4172 // and when the expression operands are defined and valid, and
4173 // thus we need the split into 2 operations: declaration of the
4174 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004175 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004176 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004177 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004178 }
4179 // Storing a variable must keep the (new) value on the expression
4180 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004181 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004182
ager@chromium.org7c537e22008-10-16 08:43:32 +00004183 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004184 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004185
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004186 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004187 if (init_state == CONST_INIT) {
4188 ASSERT(slot->var()->mode() == Variable::CONST);
4189 // Only the first const initialization must be executed (the slot
4190 // still contains 'the hole' value). When the assignment is
4191 // executed, the code is identical to a normal store (see below).
4192 Comment cmnt(masm, "[ Init const");
4193 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4194 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004195 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004196 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004197
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198 // We must execute the store. Storing a variable must keep the
4199 // (new) value on the stack. This is necessary for compiling
4200 // assignment expressions.
4201 //
4202 // Note: We will reach here even with slot->var()->mode() ==
4203 // Variable::CONST because of const declarations which will
4204 // initialize consts to 'the hole' value and by doing so, end up
4205 // calling this code. r2 may be loaded with context; used below in
4206 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004207 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004208 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004209 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004210 if (slot->type() == Slot::CONTEXT) {
4211 // Skip write barrier if the written value is a smi.
4212 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004213 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004214 // r2 is loaded with context when calling SlotOperand above.
4215 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4216 __ mov(r3, Operand(offset));
4217 __ RecordWrite(r2, r3, r1);
4218 }
4219 // If we definitely did not jump over the assignment, we do not need
4220 // to bind the exit label. Doing so can defeat peephole
4221 // optimization.
4222 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004223 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224 }
4225 }
4226 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004227 }
4228
ager@chromium.org7c537e22008-10-16 08:43:32 +00004229 case NAMED: {
4230 Comment cmnt(masm, "[ Store to named Property");
4231 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004232 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004233 Handle<String> name(GetName());
4234
4235 Result value = cgen_->allocator()->Allocate(r0);
4236 ASSERT(value.is_valid());
4237 frame->EmitPop(value.reg());
4238
4239 // Setup the name register.
4240 Result property_name = cgen_->allocator()->Allocate(r2);
4241 ASSERT(property_name.is_valid());
4242 __ mov(property_name.reg(), Operand(name));
4243 Result answer = frame->CallCodeObject(ic,
4244 RelocInfo::CODE_TARGET,
4245 &value,
4246 &property_name,
4247 0);
4248 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004249 break;
4250 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004251
ager@chromium.org7c537e22008-10-16 08:43:32 +00004252 case KEYED: {
4253 Comment cmnt(masm, "[ Store to keyed Property");
4254 Property* property = expression_->AsProperty();
4255 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004256 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004257
4258 // Call IC code.
4259 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4260 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004261 Result value = cgen_->allocator()->Allocate(r0);
4262 ASSERT(value.is_valid());
4263 frame->EmitPop(value.reg()); // value
4264 Result result =
4265 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4266 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004267 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004268 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269
4270 default:
4271 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004272 }
4273}
4274
4275
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004276// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4277// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4278// (31 instead of 32).
4279static void CountLeadingZeros(
4280 MacroAssembler* masm,
4281 Register source,
4282 Register scratch,
4283 Register zeros) {
4284#ifdef __ARM_ARCH_5__
4285 __ clz(zeros, source); // This instruction is only supported after ARM5.
4286#else
4287 __ mov(zeros, Operand(0));
4288 __ mov(scratch, source);
4289 // Top 16.
4290 __ tst(scratch, Operand(0xffff0000));
4291 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4292 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4293 // Top 8.
4294 __ tst(scratch, Operand(0xff000000));
4295 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4296 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4297 // Top 4.
4298 __ tst(scratch, Operand(0xf0000000));
4299 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4300 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4301 // Top 2.
4302 __ tst(scratch, Operand(0xc0000000));
4303 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4304 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4305 // Top bit.
4306 __ tst(scratch, Operand(0x80000000));
4307 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4308#endif
4309}
4310
4311
4312// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4313// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4314// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4315// scratch register. Destroys the source register. No GC occurs during this
4316// stub so you don't have to set up the frame.
4317class ConvertToDoubleStub : public CodeStub {
4318 public:
4319 ConvertToDoubleStub(Register result_reg_1,
4320 Register result_reg_2,
4321 Register source_reg,
4322 Register scratch_reg)
4323 : result1_(result_reg_1),
4324 result2_(result_reg_2),
4325 source_(source_reg),
4326 zeros_(scratch_reg) { }
4327
4328 private:
4329 Register result1_;
4330 Register result2_;
4331 Register source_;
4332 Register zeros_;
4333
4334 // Minor key encoding in 16 bits.
4335 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4336 class OpBits: public BitField<Token::Value, 2, 14> {};
4337
4338 Major MajorKey() { return ConvertToDouble; }
4339 int MinorKey() {
4340 // Encode the parameters in a unique 16 bit value.
4341 return result1_.code() +
4342 (result2_.code() << 4) +
4343 (source_.code() << 8) +
4344 (zeros_.code() << 12);
4345 }
4346
4347 void Generate(MacroAssembler* masm);
4348
4349 const char* GetName() { return "ConvertToDoubleStub"; }
4350
4351#ifdef DEBUG
4352 void Print() { PrintF("ConvertToDoubleStub\n"); }
4353#endif
4354};
4355
4356
4357void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4358#ifndef BIG_ENDIAN_FLOATING_POINT
4359 Register exponent = result1_;
4360 Register mantissa = result2_;
4361#else
4362 Register exponent = result2_;
4363 Register mantissa = result1_;
4364#endif
4365 Label not_special;
4366 // Convert from Smi to integer.
4367 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4368 // Move sign bit from source to destination. This works because the sign bit
4369 // in the exponent word of the double has the same position and polarity as
4370 // the 2's complement sign bit in a Smi.
4371 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4372 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4373 // Subtract from 0 if source was negative.
4374 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4375 __ cmp(source_, Operand(1));
4376 __ b(gt, &not_special);
4377
4378 // We have -1, 0 or 1, which we treat specially.
4379 __ cmp(source_, Operand(0));
4380 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4381 static const uint32_t exponent_word_for_1 =
4382 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4383 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4384 // 1, 0 and -1 all have 0 for the second word.
4385 __ mov(mantissa, Operand(0));
4386 __ Ret();
4387
4388 __ bind(&not_special);
4389 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4390 // Gets the wrong answer for 0, but we already checked for that case above.
4391 CountLeadingZeros(masm, source_, mantissa, zeros_);
4392 // Compute exponent and or it into the exponent register.
4393 // We use result2 as a scratch register here.
4394 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4395 __ orr(exponent,
4396 exponent,
4397 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4398 // Shift up the source chopping the top bit off.
4399 __ add(zeros_, zeros_, Operand(1));
4400 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4401 __ mov(source_, Operand(source_, LSL, zeros_));
4402 // Compute lower part of fraction (last 12 bits).
4403 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4404 // And the top (top 20 bits).
4405 __ orr(exponent,
4406 exponent,
4407 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4408 __ Ret();
4409}
4410
4411
4412// This stub can convert a signed int32 to a heap number (double). It does
4413// not work for int32s that are in Smi range! No GC occurs during this stub
4414// so you don't have to set up the frame.
4415class WriteInt32ToHeapNumberStub : public CodeStub {
4416 public:
4417 WriteInt32ToHeapNumberStub(Register the_int,
4418 Register the_heap_number,
4419 Register scratch)
4420 : the_int_(the_int),
4421 the_heap_number_(the_heap_number),
4422 scratch_(scratch) { }
4423
4424 private:
4425 Register the_int_;
4426 Register the_heap_number_;
4427 Register scratch_;
4428
4429 // Minor key encoding in 16 bits.
4430 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4431 class OpBits: public BitField<Token::Value, 2, 14> {};
4432
4433 Major MajorKey() { return WriteInt32ToHeapNumber; }
4434 int MinorKey() {
4435 // Encode the parameters in a unique 16 bit value.
4436 return the_int_.code() +
4437 (the_heap_number_.code() << 4) +
4438 (scratch_.code() << 8);
4439 }
4440
4441 void Generate(MacroAssembler* masm);
4442
4443 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4444
4445#ifdef DEBUG
4446 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4447#endif
4448};
4449
4450
4451// See comment for class.
4452void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4453 Label max_negative_int;
4454 // the_int_ has the answer which is a signed int32 but not a Smi.
4455 // We test for the special value that has a different exponent. This test
4456 // has the neat side effect of setting the flags according to the sign.
4457 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4458 __ cmp(the_int_, Operand(0x80000000));
4459 __ b(eq, &max_negative_int);
4460 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4461 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4462 uint32_t non_smi_exponent =
4463 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4464 __ mov(scratch_, Operand(non_smi_exponent));
4465 // Set the sign bit in scratch_ if the value was negative.
4466 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4467 // Subtract from 0 if the value was negative.
4468 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4469 // We should be masking the implict first digit of the mantissa away here,
4470 // but it just ends up combining harmlessly with the last digit of the
4471 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4472 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4473 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4474 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4475 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4476 __ str(scratch_, FieldMemOperand(the_heap_number_,
4477 HeapNumber::kExponentOffset));
4478 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4479 __ str(scratch_, FieldMemOperand(the_heap_number_,
4480 HeapNumber::kMantissaOffset));
4481 __ Ret();
4482
4483 __ bind(&max_negative_int);
4484 // The max negative int32 is stored as a positive number in the mantissa of
4485 // a double because it uses a sign bit instead of using two's complement.
4486 // The actual mantissa bits stored are all 0 because the implicit most
4487 // significant 1 bit is not stored.
4488 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4489 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4490 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4491 __ mov(ip, Operand(0));
4492 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4493 __ Ret();
4494}
4495
4496
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004497// Handle the case where the lhs and rhs are the same object.
4498// Equality is almost reflexive (everything but NaN), so this is a test
4499// for "identity and not NaN".
4500static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4501 Label* slow,
4502 Condition cc) {
4503 Label not_identical;
4504 __ cmp(r0, Operand(r1));
4505 __ b(ne, &not_identical);
4506
4507 Register exp_mask_reg = r5;
4508 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4509
4510 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4511 // so we do the second best thing - test it ourselves.
4512 Label heap_number, return_equal;
4513 // They are both equal and they are not both Smis so both of them are not
4514 // Smis. If it's not a heap number, then return equal.
4515 if (cc == lt || cc == gt) {
4516 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4517 __ b(ge, slow);
4518 } else {
4519 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4520 __ b(eq, &heap_number);
4521 // Comparing JS objects with <=, >= is complicated.
4522 if (cc != eq) {
4523 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4524 __ b(ge, slow);
4525 }
4526 }
4527 __ bind(&return_equal);
4528 if (cc == lt) {
4529 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4530 } else if (cc == gt) {
4531 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4532 } else {
4533 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4534 }
4535 __ mov(pc, Operand(lr)); // Return.
4536
4537 // For less and greater we don't have to check for NaN since the result of
4538 // x < x is false regardless. For the others here is some code to check
4539 // for NaN.
4540 if (cc != lt && cc != gt) {
4541 __ bind(&heap_number);
4542 // It is a heap number, so return non-equal if it's NaN and equal if it's
4543 // not NaN.
4544 // The representation of NaN values has all exponent bits (52..62) set,
4545 // and not all mantissa bits (0..51) clear.
4546 // Read top bits of double representation (second word of value).
4547 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4548 // Test that exponent bits are all set.
4549 __ and_(r3, r2, Operand(exp_mask_reg));
4550 __ cmp(r3, Operand(exp_mask_reg));
4551 __ b(ne, &return_equal);
4552
4553 // Shift out flag and all exponent bits, retaining only mantissa.
4554 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4555 // Or with all low-bits of mantissa.
4556 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4557 __ orr(r0, r3, Operand(r2), SetCC);
4558 // For equal we already have the right value in r0: Return zero (equal)
4559 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4560 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4561 // if it's a NaN.
4562 if (cc != eq) {
4563 // All-zero means Infinity means equal.
4564 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4565 if (cc == le) {
4566 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4567 } else {
4568 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4569 }
4570 }
4571 __ mov(pc, Operand(lr)); // Return.
4572 }
4573 // No fall through here.
4574
4575 __ bind(&not_identical);
4576}
4577
4578
4579// See comment at call site.
4580static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4581 Label* rhs_not_nan,
4582 Label* slow,
4583 bool strict) {
4584 Label lhs_is_smi;
4585 __ tst(r0, Operand(kSmiTagMask));
4586 __ b(eq, &lhs_is_smi);
4587
4588 // Rhs is a Smi. Check whether the non-smi is a heap number.
4589 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4590 if (strict) {
4591 // If lhs was not a number and rhs was a Smi then strict equality cannot
4592 // succeed. Return non-equal (r0 is already not zero)
4593 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4594 } else {
4595 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4596 // the runtime.
4597 __ b(ne, slow);
4598 }
4599
4600 // Rhs is a smi, lhs is a number.
4601 __ push(lr);
4602 __ mov(r7, Operand(r1));
4603 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4604 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4605 // r3 and r2 are rhs as double.
4606 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4607 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4608 // We now have both loaded as doubles but we can skip the lhs nan check
4609 // since it's a Smi.
4610 __ pop(lr);
4611 __ jmp(rhs_not_nan);
4612
4613 __ bind(&lhs_is_smi);
4614 // Lhs is a Smi. Check whether the non-smi is a heap number.
4615 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4616 if (strict) {
4617 // If lhs was not a number and rhs was a Smi then strict equality cannot
4618 // succeed. Return non-equal.
4619 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4620 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4621 } else {
4622 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4623 // the runtime.
4624 __ b(ne, slow);
4625 }
4626
4627 // Lhs is a smi, rhs is a number.
4628 // r0 is Smi and r1 is heap number.
4629 __ push(lr);
4630 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4631 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4632 __ mov(r7, Operand(r0));
4633 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4634 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4635 __ pop(lr);
4636 // Fall through to both_loaded_as_doubles.
4637}
4638
4639
4640void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4641 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4642 Register lhs_exponent = exp_first ? r0 : r1;
4643 Register rhs_exponent = exp_first ? r2 : r3;
4644 Register lhs_mantissa = exp_first ? r1 : r0;
4645 Register rhs_mantissa = exp_first ? r3 : r2;
4646 Label one_is_nan, neither_is_nan;
4647
4648 Register exp_mask_reg = r5;
4649
4650 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4651 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4652 __ cmp(r4, Operand(exp_mask_reg));
4653 __ b(ne, rhs_not_nan);
4654 __ mov(r4,
4655 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4656 SetCC);
4657 __ b(ne, &one_is_nan);
4658 __ cmp(rhs_mantissa, Operand(0));
4659 __ b(ne, &one_is_nan);
4660
4661 __ bind(rhs_not_nan);
4662 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4663 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4664 __ cmp(r4, Operand(exp_mask_reg));
4665 __ b(ne, &neither_is_nan);
4666 __ mov(r4,
4667 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4668 SetCC);
4669 __ b(ne, &one_is_nan);
4670 __ cmp(lhs_mantissa, Operand(0));
4671 __ b(eq, &neither_is_nan);
4672
4673 __ bind(&one_is_nan);
4674 // NaN comparisons always fail.
4675 // Load whatever we need in r0 to make the comparison fail.
4676 if (cc == lt || cc == le) {
4677 __ mov(r0, Operand(GREATER));
4678 } else {
4679 __ mov(r0, Operand(LESS));
4680 }
4681 __ mov(pc, Operand(lr)); // Return.
4682
4683 __ bind(&neither_is_nan);
4684}
4685
4686
4687// See comment at call site.
4688static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4689 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4690 Register lhs_exponent = exp_first ? r0 : r1;
4691 Register rhs_exponent = exp_first ? r2 : r3;
4692 Register lhs_mantissa = exp_first ? r1 : r0;
4693 Register rhs_mantissa = exp_first ? r3 : r2;
4694
4695 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4696 if (cc == eq) {
4697 // Doubles are not equal unless they have the same bit pattern.
4698 // Exception: 0 and -0.
4699 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4700 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4701 // Return non-zero if the numbers are unequal.
4702 __ mov(pc, Operand(lr), LeaveCC, ne);
4703
4704 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4705 // If exponents are equal then return 0.
4706 __ mov(pc, Operand(lr), LeaveCC, eq);
4707
4708 // Exponents are unequal. The only way we can return that the numbers
4709 // are equal is if one is -0 and the other is 0. We already dealt
4710 // with the case where both are -0 or both are 0.
4711 // We start by seeing if the mantissas (that are equal) or the bottom
4712 // 31 bits of the rhs exponent are non-zero. If so we return not
4713 // equal.
4714 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4715 __ mov(r0, Operand(r4), LeaveCC, ne);
4716 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4717 // Now they are equal if and only if the lhs exponent is zero in its
4718 // low 31 bits.
4719 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4720 __ mov(pc, Operand(lr));
4721 } else {
4722 // Call a native function to do a comparison between two non-NaNs.
4723 // Call C routine that may not cause GC or other trouble.
4724 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4725 __ Jump(r5); // Tail call.
4726 }
4727}
4728
4729
4730// See comment at call site.
4731static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4732 // If either operand is a JSObject or an oddball value, then they are
4733 // not equal since their pointers are different.
4734 // There is no test for undetectability in strict equality.
4735 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4736 Label first_non_object;
4737 // Get the type of the first operand into r2 and compare it with
4738 // FIRST_JS_OBJECT_TYPE.
4739 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4740 __ b(lt, &first_non_object);
4741
4742 // Return non-zero (r0 is not zero)
4743 Label return_not_equal;
4744 __ bind(&return_not_equal);
4745 __ mov(pc, Operand(lr)); // Return.
4746
4747 __ bind(&first_non_object);
4748 // Check for oddballs: true, false, null, undefined.
4749 __ cmp(r2, Operand(ODDBALL_TYPE));
4750 __ b(eq, &return_not_equal);
4751
4752 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4753 __ b(ge, &return_not_equal);
4754
4755 // Check for oddballs: true, false, null, undefined.
4756 __ cmp(r3, Operand(ODDBALL_TYPE));
4757 __ b(eq, &return_not_equal);
4758}
4759
4760
4761// See comment at call site.
4762static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4763 Label* both_loaded_as_doubles,
4764 Label* not_heap_numbers,
4765 Label* slow) {
4766 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4767 __ b(ne, not_heap_numbers);
4768 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4769 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4770
4771 // Both are heap numbers. Load them up then jump to the code we have
4772 // for that.
4773 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4774 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4775 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4776 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4777 __ jmp(both_loaded_as_doubles);
4778}
4779
4780
4781// Fast negative check for symbol-to-symbol equality.
4782static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4783 // r2 is object type of r0.
4784 __ tst(r2, Operand(kIsNotStringMask));
4785 __ b(ne, slow);
4786 __ tst(r2, Operand(kIsSymbolMask));
4787 __ b(eq, slow);
4788 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4789 __ b(ge, slow);
4790 __ tst(r3, Operand(kIsSymbolMask));
4791 __ b(eq, slow);
4792
4793 // Both are symbols. We already checked they weren't the same pointer
4794 // so they are not equal.
4795 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4796 __ mov(pc, Operand(lr)); // Return.
4797}
4798
4799
4800// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4801// positive or negative to indicate the result of the comparison.
4802void CompareStub::Generate(MacroAssembler* masm) {
4803 Label slow; // Call builtin.
4804 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4805
4806 // NOTICE! This code is only reached after a smi-fast-case check, so
4807 // it is certain that at least one operand isn't a smi.
4808
4809 // Handle the case where the objects are identical. Either returns the answer
4810 // or goes to slow. Only falls through if the objects were not identical.
4811 EmitIdenticalObjectComparison(masm, &slow, cc_);
4812
4813 // If either is a Smi (we know that not both are), then they can only
4814 // be strictly equal if the other is a HeapNumber.
4815 ASSERT_EQ(0, kSmiTag);
4816 ASSERT_EQ(0, Smi::FromInt(0));
4817 __ and_(r2, r0, Operand(r1));
4818 __ tst(r2, Operand(kSmiTagMask));
4819 __ b(ne, &not_smis);
4820 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4821 // 1) Return the answer.
4822 // 2) Go to slow.
4823 // 3) Fall through to both_loaded_as_doubles.
4824 // 4) Jump to rhs_not_nan.
4825 // In cases 3 and 4 we have found out we were dealing with a number-number
4826 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4827 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4828
4829 __ bind(&both_loaded_as_doubles);
4830 // r0, r1, r2, r3 are the double representations of the left hand side
4831 // and the right hand side.
4832
4833 // Checks for NaN in the doubles we have loaded. Can return the answer or
4834 // fall through if neither is a NaN. Also binds rhs_not_nan.
4835 EmitNanCheck(masm, &rhs_not_nan, cc_);
4836
4837 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4838 // answer. Never falls through.
4839 EmitTwoNonNanDoubleComparison(masm, cc_);
4840
4841 __ bind(&not_smis);
4842 // At this point we know we are dealing with two different objects,
4843 // and neither of them is a Smi. The objects are in r0 and r1.
4844 if (strict_) {
4845 // This returns non-equal for some object types, or falls through if it
4846 // was not lucky.
4847 EmitStrictTwoHeapObjectCompare(masm);
4848 }
4849
4850 Label check_for_symbols;
4851 // Check for heap-number-heap-number comparison. Can jump to slow case,
4852 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4853 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4854 // In this case r2 will contain the type of r0.
4855 EmitCheckForTwoHeapNumbers(masm,
4856 &both_loaded_as_doubles,
4857 &check_for_symbols,
4858 &slow);
4859
4860 __ bind(&check_for_symbols);
4861 if (cc_ == eq) {
4862 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4863 // of r0 on entry.
4864 EmitCheckForSymbols(masm, &slow);
4865 }
4866
4867 __ bind(&slow);
4868 __ push(lr);
4869 __ push(r1);
4870 __ push(r0);
4871 // Figure out which native to call and setup the arguments.
4872 Builtins::JavaScript native;
4873 int arg_count = 1; // Not counting receiver.
4874 if (cc_ == eq) {
4875 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4876 } else {
4877 native = Builtins::COMPARE;
4878 int ncr; // NaN compare result
4879 if (cc_ == lt || cc_ == le) {
4880 ncr = GREATER;
4881 } else {
4882 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4883 ncr = LESS;
4884 }
4885 arg_count++;
4886 __ mov(r0, Operand(Smi::FromInt(ncr)));
4887 __ push(r0);
4888 }
4889
4890 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4891 // tagged as a small integer.
4892 __ mov(r0, Operand(arg_count));
4893 __ InvokeBuiltin(native, CALL_JS);
4894 __ cmp(r0, Operand(0));
4895 __ pop(pc);
4896}
4897
4898
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004899// Allocates a heap number or jumps to the label if the young space is full and
4900// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004901static void AllocateHeapNumber(
4902 MacroAssembler* masm,
4903 Label* need_gc, // Jump here if young space is full.
4904 Register result_reg, // The tagged address of the new heap number.
4905 Register allocation_top_addr_reg, // A scratch register.
4906 Register scratch2) { // Another scratch register.
4907 ExternalReference allocation_top =
4908 ExternalReference::new_space_allocation_top_address();
4909 ExternalReference allocation_limit =
4910 ExternalReference::new_space_allocation_limit_address();
4911
4912 // allocat := the address of the allocation top variable.
4913 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4914 // result_reg := the old allocation top.
4915 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4916 // scratch2 := the address of the allocation limit.
4917 __ mov(scratch2, Operand(allocation_limit));
4918 // scratch2 := the allocation limit.
4919 __ ldr(scratch2, MemOperand(scratch2));
4920 // result_reg := the new allocation top.
4921 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4922 // Compare new new allocation top and limit.
4923 __ cmp(result_reg, Operand(scratch2));
4924 // Branch if out of space in young generation.
4925 __ b(hi, need_gc);
4926 // Store new allocation top.
4927 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4928 // Tag and adjust back to start of new object.
4929 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4930 // Get heap number map into scratch2.
4931 __ mov(scratch2, Operand(Factory::heap_number_map()));
4932 // Store heap number map in new object.
4933 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4934}
4935
4936
4937// We fall into this code if the operands were Smis, but the result was
4938// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004939// the operands were not both Smi. The operands are in r0 and r1. In order
4940// to call the C-implemented binary fp operation routines we need to end up
4941// with the double precision floating point operands in r0 and r1 (for the
4942// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004943static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4944 Label* not_smi,
4945 const Builtins::JavaScript& builtin,
4946 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004947 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004948 Label slow, slow_pop_2_first, do_the_call;
4949 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4950 // Smi-smi case (overflow).
4951 // Since both are Smis there is no heap number to overwrite, so allocate.
4952 // The new heap number is in r5. r6 and r7 are scratch.
4953 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4954 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004955 __ mov(r7, Operand(r0));
4956 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004957 __ push(lr);
4958 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4959 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4960 __ mov(r7, Operand(r1));
4961 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4962 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4963 __ pop(lr);
4964 __ jmp(&do_the_call); // Tail call. No return.
4965
4966 // We jump to here if something goes wrong (one param is not a number of any
4967 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004968 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004969 __ push(r1);
4970 __ push(r0);
4971 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004972 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004973
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004974 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004975 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004976 if (mode == NO_OVERWRITE) {
4977 // In the case where there is no chance of an overwritable float we may as
4978 // well do the allocation immediately while r0 and r1 are untouched.
4979 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4980 }
4981
4982 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004983 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004984 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
4985 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004986 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004987 if (mode == OVERWRITE_RIGHT) {
4988 __ mov(r5, Operand(r0)); // Overwrite this heap number.
4989 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004990 // Calling convention says that second double is in r2 and r3.
4991 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004992 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
4993 __ jmp(&finished_loading_r0);
4994 __ bind(&r0_is_smi);
4995 if (mode == OVERWRITE_RIGHT) {
4996 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004997 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004998 }
4999 // Write Smi from r0 to r3 and r2 in double format.
5000 __ mov(r7, Operand(r0));
5001 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5002 __ push(lr);
5003 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5004 __ pop(lr);
5005 __ bind(&finished_loading_r0);
5006
5007 // Move r1 to a double in r0-r1.
5008 __ tst(r1, Operand(kSmiTagMask));
5009 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5010 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5011 __ b(ne, &slow);
5012 if (mode == OVERWRITE_LEFT) {
5013 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005014 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005015 // Calling convention says that first double is in r0 and r1.
5016 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005017 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5018 __ jmp(&finished_loading_r1);
5019 __ bind(&r1_is_smi);
5020 if (mode == OVERWRITE_LEFT) {
5021 // We can't overwrite a Smi so get address of new heap number into r5.
5022 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5023 }
5024 // Write Smi from r1 to r1 and r0 in double format.
5025 __ mov(r7, Operand(r1));
5026 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5027 __ push(lr);
5028 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5029 __ pop(lr);
5030 __ bind(&finished_loading_r1);
5031
5032 __ bind(&do_the_call);
5033 // r0: Left value (least significant part of mantissa).
5034 // r1: Left value (sign, exponent, top of mantissa).
5035 // r2: Right value (least significant part of mantissa).
5036 // r3: Right value (sign, exponent, top of mantissa).
5037 // r5: Address of heap number for result.
5038 __ push(lr); // For later.
5039 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005040 // Call C routine that may not cause GC or other trouble.
5041 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005042 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005043 // Store answer in the overwritable heap number.
5044 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005045#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005046 // Double returned in fp coprocessor register 0 and 1, encoded as register
5047 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5048 // substract the tag from r4.
5049 __ sub(r5, r4, Operand(kHeapObjectTag));
5050 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5051#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005052 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005053 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005054 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005055#endif
5056 __ mov(r0, Operand(r4));
5057 // And we are done.
5058 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005059}
5060
5061
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005062// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005063// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005064// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5065// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005066// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5067// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005068static void GetInt32(MacroAssembler* masm,
5069 Register source,
5070 Register dest,
5071 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005072 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005073 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005074 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005075 // Get exponent word.
5076 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5077 // Get exponent alone in scratch2.
5078 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005079 // Load dest with zero. We use this either for the final shift or
5080 // for the answer.
5081 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005082 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005083 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5084 // the exponent that we are fastest at and also the highest exponent we can
5085 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005086 const uint32_t non_smi_exponent =
5087 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5088 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005089 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5090 __ b(eq, &right_exponent);
5091 // If the exponent is higher than that then go to slow case. This catches
5092 // numbers that don't fit in a signed int32, infinities and NaNs.
5093 __ b(gt, slow);
5094
5095 // We know the exponent is smaller than 30 (biased). If it is less than
5096 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5097 // it rounds to zero.
5098 const uint32_t zero_exponent =
5099 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5100 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5101 // Dest already has a Smi zero.
5102 __ b(lt, &done);
5103 // We have a shifted exponent between 0 and 30 in scratch2.
5104 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5105 // We now have the exponent in dest. Subtract from 30 to get
5106 // how much to shift down.
5107 __ rsb(dest, dest, Operand(30));
5108
5109 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005110 // Get the top bits of the mantissa.
5111 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5112 // Put back the implicit 1.
5113 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5114 // Shift up the mantissa bits to take up the space the exponent used to take.
5115 // We just orred in the implicit bit so that took care of one and we want to
5116 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5117 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5118 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5119 // Put sign in zero flag.
5120 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005121 // Get the second half of the double. For some exponents we don't actually
5122 // need this because the bits get shifted out again, but it's probably slower
5123 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005124 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5125 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005126 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5127 // Move down according to the exponent.
5128 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005129 // Fix sign if sign bit was set.
5130 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005131 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005132}
5133
5134
5135// For bitwise ops where the inputs are not both Smis we here try to determine
5136// whether both inputs are either Smis or at least heap numbers that can be
5137// represented by a 32 bit signed value. We truncate towards zero as required
5138// by the ES spec. If this is the case we do the bitwise op and see if the
5139// result is a Smi. If so, great, otherwise we try to find a heap number to
5140// write the answer into (either by allocating or by overwriting).
5141// On entry the operands are in r0 and r1. On exit the answer is in r0.
5142void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5143 Label slow, result_not_a_smi;
5144 Label r0_is_smi, r1_is_smi;
5145 Label done_checking_r0, done_checking_r1;
5146
5147 __ tst(r1, Operand(kSmiTagMask));
5148 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5149 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5150 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005151 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005152 __ jmp(&done_checking_r1);
5153 __ bind(&r1_is_smi);
5154 __ mov(r3, Operand(r1, ASR, 1));
5155 __ bind(&done_checking_r1);
5156
5157 __ tst(r0, Operand(kSmiTagMask));
5158 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5159 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5160 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005161 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005162 __ jmp(&done_checking_r0);
5163 __ bind(&r0_is_smi);
5164 __ mov(r2, Operand(r0, ASR, 1));
5165 __ bind(&done_checking_r0);
5166
5167 // r0 and r1: Original operands (Smi or heap numbers).
5168 // r2 and r3: Signed int32 operands.
5169 switch (op_) {
5170 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5171 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5172 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5173 case Token::SAR:
5174 // Use only the 5 least significant bits of the shift count.
5175 __ and_(r2, r2, Operand(0x1f));
5176 __ mov(r2, Operand(r3, ASR, r2));
5177 break;
5178 case Token::SHR:
5179 // Use only the 5 least significant bits of the shift count.
5180 __ and_(r2, r2, Operand(0x1f));
5181 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5182 // SHR is special because it is required to produce a positive answer.
5183 // The code below for writing into heap numbers isn't capable of writing
5184 // the register as an unsigned int so we go to slow case if we hit this
5185 // case.
5186 __ b(mi, &slow);
5187 break;
5188 case Token::SHL:
5189 // Use only the 5 least significant bits of the shift count.
5190 __ and_(r2, r2, Operand(0x1f));
5191 __ mov(r2, Operand(r3, LSL, r2));
5192 break;
5193 default: UNREACHABLE();
5194 }
5195 // check that the *signed* result fits in a smi
5196 __ add(r3, r2, Operand(0x40000000), SetCC);
5197 __ b(mi, &result_not_a_smi);
5198 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5199 __ Ret();
5200
5201 Label have_to_allocate, got_a_heap_number;
5202 __ bind(&result_not_a_smi);
5203 switch (mode_) {
5204 case OVERWRITE_RIGHT: {
5205 __ tst(r0, Operand(kSmiTagMask));
5206 __ b(eq, &have_to_allocate);
5207 __ mov(r5, Operand(r0));
5208 break;
5209 }
5210 case OVERWRITE_LEFT: {
5211 __ tst(r1, Operand(kSmiTagMask));
5212 __ b(eq, &have_to_allocate);
5213 __ mov(r5, Operand(r1));
5214 break;
5215 }
5216 case NO_OVERWRITE: {
5217 // Get a new heap number in r5. r6 and r7 are scratch.
5218 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5219 }
5220 default: break;
5221 }
5222 __ bind(&got_a_heap_number);
5223 // r2: Answer as signed int32.
5224 // r5: Heap number to write answer into.
5225
5226 // Nothing can go wrong now, so move the heap number to r0, which is the
5227 // result.
5228 __ mov(r0, Operand(r5));
5229
5230 // Tail call that writes the int32 in r2 to the heap number in r0, using
5231 // r3 as scratch. r0 is preserved and returned.
5232 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5233 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5234
5235 if (mode_ != NO_OVERWRITE) {
5236 __ bind(&have_to_allocate);
5237 // Get a new heap number in r5. r6 and r7 are scratch.
5238 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5239 __ jmp(&got_a_heap_number);
5240 }
5241
5242 // If all else failed then we go to the runtime system.
5243 __ bind(&slow);
5244 __ push(r1); // restore stack
5245 __ push(r0);
5246 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5247 switch (op_) {
5248 case Token::BIT_OR:
5249 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5250 break;
5251 case Token::BIT_AND:
5252 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5253 break;
5254 case Token::BIT_XOR:
5255 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5256 break;
5257 case Token::SAR:
5258 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5259 break;
5260 case Token::SHR:
5261 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5262 break;
5263 case Token::SHL:
5264 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5265 break;
5266 default:
5267 UNREACHABLE();
5268 }
5269}
5270
5271
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005272void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5273 // r1 : x
5274 // r0 : y
5275 // result : r0
5276
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005277 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5278 // tell us that.
5279 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5280
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005281 switch (op_) {
5282 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005283 Label not_smi;
5284 // Fast path.
5285 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005286 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005287 __ b(ne, &not_smi);
5288 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5289 // Return if no overflow.
5290 __ Ret(vc);
5291 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5292
5293 HandleBinaryOpSlowCases(masm,
5294 &not_smi,
5295 Builtins::ADD,
5296 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005297 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005298 break;
5299 }
5300
5301 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005302 Label not_smi;
5303 // Fast path.
5304 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005305 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005306 __ b(ne, &not_smi);
5307 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5308 // Return if no overflow.
5309 __ Ret(vc);
5310 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5311
5312 HandleBinaryOpSlowCases(masm,
5313 &not_smi,
5314 Builtins::SUB,
5315 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005316 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005317 break;
5318 }
5319
5320 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005321 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005322 ASSERT(kSmiTag == 0); // adjust code below
5323 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005324 __ b(ne, &not_smi);
5325 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005326 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005327 // Do multiplication
5328 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5329 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005330 __ mov(ip, Operand(r3, ASR, 31));
5331 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5332 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005333 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005334 __ tst(r3, Operand(r3));
5335 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005336 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005337 // We need -0 if we were multiplying a negative number with 0 to get 0.
5338 // We know one of them was zero.
5339 __ add(r2, r0, Operand(r1), SetCC);
5340 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5341 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5342 // Slow case. We fall through here if we multiplied a negative number
5343 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005344 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005345
5346 HandleBinaryOpSlowCases(masm,
5347 &not_smi,
5348 Builtins::MUL,
5349 Token::MUL,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005350 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005351 break;
5352 }
5353
5354 case Token::BIT_OR:
5355 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005356 case Token::BIT_XOR:
5357 case Token::SAR:
5358 case Token::SHR:
5359 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005360 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005361 ASSERT(kSmiTag == 0); // adjust code below
5362 __ tst(r2, Operand(kSmiTagMask));
5363 __ b(ne, &slow);
5364 switch (op_) {
5365 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5366 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5367 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005368 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005369 // Remove tags from right operand.
5370 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5371 // Use only the 5 least significant bits of the shift count.
5372 __ and_(r2, r2, Operand(0x1f));
5373 __ mov(r0, Operand(r1, ASR, r2));
5374 // Smi tag result.
5375 __ and_(r0, r0, Operand(~kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005376 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005377 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005378 // Remove tags from operands. We can't do this on a 31 bit number
5379 // because then the 0s get shifted into bit 30 instead of bit 31.
5380 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5381 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5382 // Use only the 5 least significant bits of the shift count.
5383 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005384 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005385 // Unsigned shift is not allowed to produce a negative number, so
5386 // check the sign bit and the sign bit after Smi tagging.
5387 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005388 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005389 // Smi tag result.
5390 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005391 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005392 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005393 // Remove tags from operands.
5394 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5395 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5396 // Use only the 5 least significant bits of the shift count.
5397 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005398 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005399 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005400 __ add(r2, r3, Operand(0x40000000), SetCC);
5401 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005402 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005403 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005404 default: UNREACHABLE();
5405 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005406 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005407 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005408 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005409 break;
5410 }
5411
5412 default: UNREACHABLE();
5413 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005414 // This code should be unreachable.
5415 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005416}
5417
5418
5419void StackCheckStub::Generate(MacroAssembler* masm) {
5420 Label within_limit;
5421 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5422 __ ldr(ip, MemOperand(ip));
5423 __ cmp(sp, Operand(ip));
5424 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005425 // Do tail-call to runtime routine. Runtime routines expect at least one
5426 // argument, so give it a Smi.
5427 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005428 __ push(r0);
5429 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5430 __ bind(&within_limit);
5431
5432 __ StubReturn(1);
5433}
5434
5435
5436void UnarySubStub::Generate(MacroAssembler* masm) {
5437 Label undo;
5438 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005439 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005440
5441 // Enter runtime system if the value is not a smi.
5442 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005443 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005444
5445 // Enter runtime system if the value of the expression is zero
5446 // to make sure that we switch between 0 and -0.
5447 __ cmp(r0, Operand(0));
5448 __ b(eq, &slow);
5449
5450 // The value of the expression is a smi that is not zero. Try
5451 // optimistic subtraction '0 - value'.
5452 __ rsb(r1, r0, Operand(0), SetCC);
5453 __ b(vs, &slow);
5454
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005455 __ mov(r0, Operand(r1)); // Set r0 to result.
5456 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005457
5458 // Enter runtime system.
5459 __ bind(&slow);
5460 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005461 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005462 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5463
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005464 __ bind(&not_smi);
5465 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5466 __ b(ne, &slow);
5467 // r0 is a heap number. Get a new heap number in r1.
5468 if (overwrite_) {
5469 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5470 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5471 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5472 } else {
5473 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5474 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5475 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5476 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5477 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5478 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5479 __ mov(r0, Operand(r1));
5480 }
5481 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005482}
5483
5484
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005485void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005486 // r0 holds the exception.
5487
5488 // Adjust this code if not the case.
5489 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5490
5491 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005492 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5493 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005494
5495 // Restore the next handler and frame pointer, discard handler state.
5496 ASSERT(StackHandlerConstants::kNextOffset == 0);
5497 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005498 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005499 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5500 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5501
5502 // Before returning we restore the context from the frame pointer if
5503 // not NULL. The frame pointer is NULL in the exception handler of a
5504 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005505 __ cmp(fp, Operand(0));
5506 // Set cp to NULL if fp is NULL.
5507 __ mov(cp, Operand(0), LeaveCC, eq);
5508 // Restore cp otherwise.
5509 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005510#ifdef DEBUG
5511 if (FLAG_debug_code) {
5512 __ mov(lr, Operand(pc));
5513 }
5514#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005515 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005516 __ pop(pc);
5517}
5518
5519
5520void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005521 // Adjust this code if not the case.
5522 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5523
5524 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005525 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005526 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005527
5528 // Unwind the handlers until the ENTRY handler is found.
5529 Label loop, done;
5530 __ bind(&loop);
5531 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005532 const int kStateOffset = StackHandlerConstants::kStateOffset;
5533 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005534 __ cmp(r2, Operand(StackHandler::ENTRY));
5535 __ b(eq, &done);
5536 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005537 const int kNextOffset = StackHandlerConstants::kNextOffset;
5538 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005539 __ jmp(&loop);
5540 __ bind(&done);
5541
5542 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005543 ASSERT(StackHandlerConstants::kNextOffset == 0);
5544 __ pop(r0);
5545 __ str(r0, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005546
5547 // Set external caught exception to false.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005548 ExternalReference external_caught(Top::k_external_caught_exception_address);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005549 __ mov(r0, Operand(false));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005550 __ mov(r2, Operand(external_caught));
5551 __ str(r0, MemOperand(r2));
5552
5553 // Set pending exception and r0 to out of memory exception.
5554 Failure* out_of_memory = Failure::OutOfMemoryException();
5555 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5556 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5557 __ str(r0, MemOperand(r2));
5558
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005559 // Stack layout at this point. See also StackHandlerConstants.
5560 // sp -> state (ENTRY)
5561 // fp
5562 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005563
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005564 // Discard handler state (r2 is not used) and restore frame pointer.
5565 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5566 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5567 // Before returning we restore the context from the frame pointer if
5568 // not NULL. The frame pointer is NULL in the exception handler of a
5569 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005570 __ cmp(fp, Operand(0));
5571 // Set cp to NULL if fp is NULL.
5572 __ mov(cp, Operand(0), LeaveCC, eq);
5573 // Restore cp otherwise.
5574 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005575#ifdef DEBUG
5576 if (FLAG_debug_code) {
5577 __ mov(lr, Operand(pc));
5578 }
5579#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005580 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005581 __ pop(pc);
5582}
5583
5584
5585void CEntryStub::GenerateCore(MacroAssembler* masm,
5586 Label* throw_normal_exception,
5587 Label* throw_out_of_memory_exception,
5588 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005589 bool do_gc,
5590 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005591 // r0: result parameter for PerformGC, if any
5592 // r4: number of arguments including receiver (C callee-saved)
5593 // r5: pointer to builtin function (C callee-saved)
5594 // r6: pointer to the first argument (C callee-saved)
5595
5596 if (do_gc) {
5597 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005598 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5599 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005600 }
5601
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005602 ExternalReference scope_depth =
5603 ExternalReference::heap_always_allocate_scope_depth();
5604 if (always_allocate) {
5605 __ mov(r0, Operand(scope_depth));
5606 __ ldr(r1, MemOperand(r0));
5607 __ add(r1, r1, Operand(1));
5608 __ str(r1, MemOperand(r0));
5609 }
5610
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005611 // Call C built-in.
5612 // r0 = argc, r1 = argv
5613 __ mov(r0, Operand(r4));
5614 __ mov(r1, Operand(r6));
5615
5616 // TODO(1242173): To let the GC traverse the return address of the exit
5617 // frames, we need to know where the return address is. Right now,
5618 // we push it on the stack to be able to find it again, but we never
5619 // restore from it in case of changes, which makes it impossible to
5620 // support moving the C entry code stub. This should be fixed, but currently
5621 // this is OK because the CEntryStub gets generated so early in the V8 boot
5622 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005623 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5624 masm->push(lr);
5625 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005626
5627 if (always_allocate) {
5628 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5629 // though (contain the result).
5630 __ mov(r2, Operand(scope_depth));
5631 __ ldr(r3, MemOperand(r2));
5632 __ sub(r3, r3, Operand(1));
5633 __ str(r3, MemOperand(r2));
5634 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005635
5636 // check for failure result
5637 Label failure_returned;
5638 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5639 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5640 __ add(r2, r0, Operand(1));
5641 __ tst(r2, Operand(kFailureTagMask));
5642 __ b(eq, &failure_returned);
5643
5644 // Exit C frame and return.
5645 // r0:r1: result
5646 // sp: stack pointer
5647 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005648 __ LeaveExitFrame(frame_type);
5649
5650 // check if we should retry or throw exception
5651 Label retry;
5652 __ bind(&failure_returned);
5653 ASSERT(Failure::RETRY_AFTER_GC == 0);
5654 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5655 __ b(eq, &retry);
5656
5657 Label continue_exception;
5658 // If the returned failure is EXCEPTION then promote Top::pending_exception().
5659 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
5660 __ b(ne, &continue_exception);
5661
5662 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005663 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005664 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005665 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005666 __ ldr(r0, MemOperand(ip));
5667 __ str(r3, MemOperand(ip));
5668
5669 __ bind(&continue_exception);
5670 // Special handling of out of memory exception.
5671 Failure* out_of_memory = Failure::OutOfMemoryException();
5672 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5673 __ b(eq, throw_out_of_memory_exception);
5674
5675 // Handle normal exception.
5676 __ jmp(throw_normal_exception);
5677
5678 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5679}
5680
5681
5682void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5683 // Called from JavaScript; parameters are on stack as if calling JS function
5684 // r0: number of arguments including receiver
5685 // r1: pointer to builtin function
5686 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005687 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005688 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005689
5690 // NOTE: Invocations of builtins may return failure objects
5691 // instead of a proper result. The builtin entry handles
5692 // this by performing a garbage collection and retrying the
5693 // builtin once.
5694
5695 StackFrame::Type frame_type = is_debug_break
5696 ? StackFrame::EXIT_DEBUG
5697 : StackFrame::EXIT;
5698
5699 // Enter the exit frame that transitions from JavaScript to C++.
5700 __ EnterExitFrame(frame_type);
5701
5702 // r4: number of arguments (C callee-saved)
5703 // r5: pointer to builtin function (C callee-saved)
5704 // r6: pointer to first argument (C callee-saved)
5705
5706 Label throw_out_of_memory_exception;
5707 Label throw_normal_exception;
5708
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005709 // Call into the runtime system. Collect garbage before the call if
5710 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005711 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005712 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005713 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
5714 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005715 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005716 &throw_out_of_memory_exception,
5717 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005718 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005719 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005720
5721 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005722 GenerateCore(masm,
5723 &throw_normal_exception,
5724 &throw_out_of_memory_exception,
5725 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005726 true,
5727 false);
5728
5729 // Do full GC and retry runtime call one final time.
5730 Failure* failure = Failure::InternalError();
5731 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5732 GenerateCore(masm,
5733 &throw_normal_exception,
5734 &throw_out_of_memory_exception,
5735 frame_type,
5736 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005737 true);
5738
5739 __ bind(&throw_out_of_memory_exception);
5740 GenerateThrowOutOfMemory(masm);
5741 // control flow for generated will not return.
5742
5743 __ bind(&throw_normal_exception);
5744 GenerateThrowTOS(masm);
5745}
5746
5747
5748void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5749 // r0: code entry
5750 // r1: function
5751 // r2: receiver
5752 // r3: argc
5753 // [sp+0]: argv
5754
5755 Label invoke, exit;
5756
5757 // Called from C, so do not pop argc and args on exit (preserve sp)
5758 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005759 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005760 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5761
5762 // Get address of argv, see stm above.
5763 // r0: code entry
5764 // r1: function
5765 // r2: receiver
5766 // r3: argc
5767 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5768 __ ldr(r4, MemOperand(r4)); // argv
5769
5770 // Push a frame with special values setup to mark it as an entry frame.
5771 // r0: code entry
5772 // r1: function
5773 // r2: receiver
5774 // r3: argc
5775 // r4: argv
5776 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5777 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
5778 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
5779 __ mov(r6, Operand(Smi::FromInt(marker)));
5780 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5781 __ ldr(r5, MemOperand(r5));
5782 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5783
5784 // Setup frame pointer for the frame to be pushed.
5785 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5786
5787 // Call a faked try-block that does the invoke.
5788 __ bl(&invoke);
5789
5790 // Caught exception: Store result (exception) in the pending
5791 // exception field in the JSEnv and return a failure sentinel.
5792 // Coming in here the fp will be invalid because the PushTryHandler below
5793 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00005794 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005795 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005796 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005797 __ b(&exit);
5798
5799 // Invoke: Link this frame into the handler chain.
5800 __ bind(&invoke);
5801 // Must preserve r0-r4, r5-r7 are available.
5802 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005803 // If an exception not caught by another handler occurs, this handler
5804 // returns control to the code after the bl(&invoke) above, which
5805 // restores all kCalleeSaved registers (including cp and fp) to their
5806 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005807
5808 // Clear any pending exceptions.
5809 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5810 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005811 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005812 __ str(r5, MemOperand(ip));
5813
5814 // Invoke the function by calling through JS entry trampoline builtin.
5815 // Notice that we cannot store a reference to the trampoline code directly in
5816 // this stub, because runtime stubs are not traversed when doing GC.
5817
5818 // Expected registers by Builtins::JSEntryTrampoline
5819 // r0: code entry
5820 // r1: function
5821 // r2: receiver
5822 // r3: argc
5823 // r4: argv
5824 if (is_construct) {
5825 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
5826 __ mov(ip, Operand(construct_entry));
5827 } else {
5828 ExternalReference entry(Builtins::JSEntryTrampoline);
5829 __ mov(ip, Operand(entry));
5830 }
5831 __ ldr(ip, MemOperand(ip)); // deref address
5832
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005833 // Branch and link to JSEntryTrampoline. We don't use the double underscore
5834 // macro for the add instruction because we don't want the coverage tool
5835 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005836 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005837 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005838
5839 // Unlink this frame from the handler chain. When reading the
5840 // address of the next handler, there is no need to use the address
5841 // displacement since the current stack pointer (sp) points directly
5842 // to the stack handler.
5843 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5844 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5845 __ str(r3, MemOperand(ip));
5846 // No need to restore registers
5847 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5848
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005849
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005850 __ bind(&exit); // r0 holds result
5851 // Restore the top frame descriptors from the stack.
5852 __ pop(r3);
5853 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5854 __ str(r3, MemOperand(ip));
5855
5856 // Reset the stack to the callee saved registers.
5857 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5858
5859 // Restore callee-saved registers and return.
5860#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005861 if (FLAG_debug_code) {
5862 __ mov(lr, Operand(pc));
5863 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005864#endif
5865 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5866}
5867
5868
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005869// This stub performs an instanceof, calling the builtin function if
5870// necessary. Uses r1 for the object, r0 for the function that it may
5871// be an instance of (these are fetched from the stack).
5872void InstanceofStub::Generate(MacroAssembler* masm) {
5873 // Get the object - slow case for smis (we may need to throw an exception
5874 // depending on the rhs).
5875 Label slow, loop, is_instance, is_not_instance;
5876 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5877 __ BranchOnSmi(r0, &slow);
5878
5879 // Check that the left hand is a JS object and put map in r3.
5880 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
5881 __ b(lt, &slow);
5882 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
5883 __ b(gt, &slow);
5884
5885 // Get the prototype of the function (r4 is result, r2 is scratch).
5886 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
5887 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
5888
5889 // Check that the function prototype is a JS object.
5890 __ BranchOnSmi(r4, &slow);
5891 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
5892 __ b(lt, &slow);
5893 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
5894 __ b(gt, &slow);
5895
5896 // Register mapping: r3 is object map and r4 is function prototype.
5897 // Get prototype of object into r2.
5898 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
5899
5900 // Loop through the prototype chain looking for the function prototype.
5901 __ bind(&loop);
5902 __ cmp(r2, Operand(r4));
5903 __ b(eq, &is_instance);
5904 __ cmp(r2, Operand(Factory::null_value()));
5905 __ b(eq, &is_not_instance);
5906 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
5907 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
5908 __ jmp(&loop);
5909
5910 __ bind(&is_instance);
5911 __ mov(r0, Operand(Smi::FromInt(0)));
5912 __ pop();
5913 __ pop();
5914 __ mov(pc, Operand(lr)); // Return.
5915
5916 __ bind(&is_not_instance);
5917 __ mov(r0, Operand(Smi::FromInt(1)));
5918 __ pop();
5919 __ pop();
5920 __ mov(pc, Operand(lr)); // Return.
5921
5922 // Slow-case. Tail call builtin.
5923 __ bind(&slow);
5924 __ mov(r0, Operand(1)); // Arg count without receiver.
5925 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
5926}
5927
5928
ager@chromium.org7c537e22008-10-16 08:43:32 +00005929void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005930 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005931 Label adaptor;
5932 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5933 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5934 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005935 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005936
ager@chromium.org7c537e22008-10-16 08:43:32 +00005937 // Nothing to do: The formal number of parameters has already been
5938 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00005939 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005940
ager@chromium.org7c537e22008-10-16 08:43:32 +00005941 // Arguments adaptor case: Read the arguments length from the
5942 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005943 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005944 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00005945 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005946}
5947
5948
ager@chromium.org7c537e22008-10-16 08:43:32 +00005949void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5950 // The displacement is the offset of the last parameter (if any)
5951 // relative to the frame pointer.
5952 static const int kDisplacement =
5953 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005954
ager@chromium.org7c537e22008-10-16 08:43:32 +00005955 // Check that the key is a smi.
5956 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005957 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005958
ager@chromium.org7c537e22008-10-16 08:43:32 +00005959 // Check if the calling frame is an arguments adaptor frame.
5960 Label adaptor;
5961 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5962 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5963 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5964 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005965
ager@chromium.org7c537e22008-10-16 08:43:32 +00005966 // Check index against formal parameters count limit passed in
5967 // through register eax. Use unsigned comparison to get negative
5968 // check for free.
5969 __ cmp(r1, r0);
5970 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005971
ager@chromium.org7c537e22008-10-16 08:43:32 +00005972 // Read the argument from the stack and return it.
5973 __ sub(r3, r0, r1);
5974 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5975 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005976 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005977
5978 // Arguments adaptor case: Check index against actual arguments
5979 // limit found in the arguments adaptor frame. Use unsigned
5980 // comparison to get negative check for free.
5981 __ bind(&adaptor);
5982 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5983 __ cmp(r1, r0);
5984 __ b(cs, &slow);
5985
5986 // Read the argument from the adaptor frame and return it.
5987 __ sub(r3, r0, r1);
5988 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5989 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005990 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005991
5992 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5993 // by calling the runtime system.
5994 __ bind(&slow);
5995 __ push(r1);
5996 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5997}
5998
5999
6000void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6001 // Check if the calling frame is an arguments adaptor frame.
6002 Label runtime;
6003 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6004 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6005 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6006 __ b(ne, &runtime);
6007
6008 // Patch the arguments.length and the parameters pointer.
6009 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6010 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6011 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6012 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6013 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6014
6015 // Do the runtime call to allocate the arguments object.
6016 __ bind(&runtime);
6017 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006018}
6019
6020
6021void CallFunctionStub::Generate(MacroAssembler* masm) {
6022 Label slow;
6023 // Get the function to call from the stack.
6024 // function, receiver [, arguments]
6025 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6026
6027 // Check that the function is really a JavaScript function.
6028 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006029 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006030 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006031 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006032 __ b(ne, &slow);
6033
6034 // Fast-case: Invoke the function now.
6035 // r1: pushed function
6036 ParameterCount actual(argc_);
6037 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6038
6039 // Slow-case: Non-function called.
6040 __ bind(&slow);
6041 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006042 __ mov(r2, Operand(0));
6043 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6044 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6045 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006046}
6047
6048
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006049int CompareStub::MinorKey() {
6050 // Encode the two parameters in a unique 16 bit value.
6051 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6052 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6053}
6054
6055
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006056#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006058} } // namespace v8::internal