blob: 47da62de7240632468022e0c6a15d5ff29aec415 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "scopes.h"
34#include "runtime.h"
35
36namespace v8 { namespace internal {
37
ager@chromium.org3bf7b912008-11-17 09:09:45 +000038#define __ masm_->
39
40// -------------------------------------------------------------------------
41// VirtualFrame implementation.
42
43VirtualFrame::VirtualFrame(CodeGenerator* cgen) {
44 ASSERT(cgen->scope() != NULL);
45
46 masm_ = cgen->masm();
47 frame_local_count_ = cgen->scope()->num_stack_slots();
48 parameter_count_ = cgen->scope()->num_parameters();
49}
50
51
52void VirtualFrame::Enter() {
53 Comment cmnt(masm_, "[ Enter JS frame");
54#ifdef DEBUG
55 { Label done, fail;
56 __ tst(r1, Operand(kSmiTagMask));
57 __ b(eq, &fail);
58 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
59 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
60 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
61 __ b(eq, &done);
62 __ bind(&fail);
63 __ stop("CodeGenerator::EnterJSFrame - r1 not a function");
64 __ bind(&done);
65 }
66#endif // DEBUG
67
68 __ stm(db_w, sp, r1.bit() | cp.bit() | fp.bit() | lr.bit());
69 // Adjust FP to point to saved FP.
70 __ add(fp, sp, Operand(2 * kPointerSize));
71}
72
73
74void VirtualFrame::Exit() {
75 Comment cmnt(masm_, "[ Exit JS frame");
76 // Drop the execution stack down to the frame pointer and restore the caller
77 // frame pointer and return address.
78 __ mov(sp, fp);
79 __ ldm(ia_w, sp, fp.bit() | lr.bit());
80}
81
82
83void VirtualFrame::AllocateLocals() {
84 if (frame_local_count_ > 0) {
85 Comment cmnt(masm_, "[ Allocate space for locals");
86 // Initialize stack slots with 'undefined' value.
87 __ mov(ip, Operand(Factory::undefined_value()));
88 for (int i = 0; i < frame_local_count_; i++) {
89 __ push(ip);
90 }
91 }
92}
93
94
95void VirtualFrame::Drop(int count) {
96 ASSERT(count >= 0);
97 if (count > 0) {
98 __ add(sp, sp, Operand(count * kPointerSize));
99 }
100}
101
102
103void VirtualFrame::Pop() { Drop(1); }
104
105
106void VirtualFrame::Pop(Register reg) {
107 __ pop(reg);
108}
109
110
111void VirtualFrame::Push(Register reg) {
112 __ push(reg);
113}
114
115
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000116// -------------------------------------------------------------------------
117// CodeGenState implementation.
118
ager@chromium.org7c537e22008-10-16 08:43:32 +0000119CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000120 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000121 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000122 true_target_(NULL),
123 false_target_(NULL),
124 previous_(NULL) {
125 owner_->set_state(this);
126}
127
128
ager@chromium.org7c537e22008-10-16 08:43:32 +0000129CodeGenState::CodeGenState(CodeGenerator* owner,
130 TypeofState typeof_state,
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000131 Label* true_target,
132 Label* false_target)
133 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000134 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000135 true_target_(true_target),
136 false_target_(false_target),
137 previous_(owner->state()) {
138 owner_->set_state(this);
139}
140
141
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000142CodeGenState::~CodeGenState() {
143 ASSERT(owner_->state() == this);
144 owner_->set_state(previous_);
145}
146
147
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000148// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000149// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
ager@chromium.org7c537e22008-10-16 08:43:32 +0000151CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
152 bool is_eval)
153 : is_eval_(is_eval),
154 script_(script),
155 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 masm_(new MacroAssembler(NULL, buffer_size)),
157 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000158 frame_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 cc_reg_(al),
160 state_(NULL),
161 break_stack_height_(0) {
162}
163
164
165// Calling conventions:
mads.s.ager31e71382008-08-13 09:32:07 +0000166// r0: the number of arguments
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167// fp: frame pointer
168// sp: stack pointer
169// pp: caller's parameter pointer
170// cp: callee's context
171
ager@chromium.org7c537e22008-10-16 08:43:32 +0000172void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 ZoneList<Statement*>* body = fun->body();
174
175 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000176 ASSERT(scope_ == NULL);
177 scope_ = fun->scope();
178 ASSERT(frame_ == NULL);
179 VirtualFrame virtual_frame(this);
180 frame_ = &virtual_frame;
181 cc_reg_ = al;
182 {
183 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184
185 // Entry
186 // stack: function, receiver, arguments, return address
187 // r0: number of arguments
188 // sp: stack pointer
189 // fp: frame pointer
190 // pp: caller's parameter pointer
191 // cp: callee's context
192
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000193 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 // tos: code slot
195#ifdef DEBUG
196 if (strlen(FLAG_stop_at) > 0 &&
197 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasper.lund7276f142008-07-30 08:49:36 +0000198 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 }
200#endif
201
202 // Allocate space for locals and initialize them.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203 frame_->AllocateLocals();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 // Allocate local context.
207 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000208 __ ldr(r0, frame_->Function());
209 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +0000210 __ CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
211
212 if (kDebug) {
213 Label verified_true;
214 __ cmp(r0, Operand(cp));
215 __ b(eq, &verified_true);
216 __ stop("NewContext: r0 is expected to be the same as cp");
217 __ bind(&verified_true);
218 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 }
222
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000223 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 // 1) only needed if we have a context
225 // 2) no need to recompute context ptr every single time
226 // 3) don't copy parameter operand code from SlotOperand!
227 {
228 Comment cmnt2(masm_, "[ copy context parameters into .context");
229
230 // Note that iteration order is relevant here! If we have the same
231 // parameter twice (e.g., function (x, y, x)), and that parameter
232 // needs to be copied into the context, it must be the last argument
233 // passed to the parameter that needs to be copied. This is a rare
234 // case so we don't check for it, instead we rely on the copying
235 // order: such a parameter is copied repeatedly into the same
236 // context location and thus the last value is what is seen inside
237 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000238 for (int i = 0; i < scope_->num_parameters(); i++) {
239 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 Slot* slot = par->slot();
241 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000242 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
243 __ ldr(r1, frame_->Parameter(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 // Loads r2 with context; used below in RecordWrite.
245 __ str(r1, SlotOperand(slot, r2));
246 // Load the offset into r3.
247 int slot_offset =
248 FixedArray::kHeaderSize + slot->index() * kPointerSize;
249 __ mov(r3, Operand(slot_offset));
250 __ RecordWrite(r2, r3, r1);
251 }
252 }
253 }
254
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000255 // Store the arguments object. This must happen after context
256 // initialization because the arguments object may be stored in the
257 // context.
258 if (scope_->arguments() != NULL) {
259 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000261 { Reference shadow_ref(this, scope_->arguments_shadow());
262 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000263 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000264 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000265 // The receiver is below the arguments, the return address,
266 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000267 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000268 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000269 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000270 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
271 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000272 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000273 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000274 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000275 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000277 frame_->Pop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 }
279
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000280 // Generate code to 'execute' declarations and initialize functions
281 // (source elements). In case of an illegal redeclaration we need to
282 // handle that instead of processing the declarations.
283 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000285 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 } else {
287 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000288 ProcessDeclarations(scope_->declarations());
289 // Bail out if a stack-overflow exception occurred when processing
290 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000291 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 }
293
mads.s.ager31e71382008-08-13 09:32:07 +0000294 if (FLAG_trace) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000295 __ CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000296 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 CheckStack();
299
300 // Compile the body of the function in a vanilla state. Don't
301 // bother compiling all the code if the scope has an illegal
302 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000303 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 Comment cmnt(masm_, "[ function body");
305#ifdef DEBUG
306 bool is_builtin = Bootstrapper::IsActive();
307 bool should_trace =
308 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000309 if (should_trace) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000310 __ CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000311 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313#endif
314 VisitStatements(body);
315 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 }
317
318 // exit
319 // r0: result
320 // sp: stack pointer
321 // fp: frame pointer
322 // pp: parameter pointer
323 // cp: callee's context
mads.s.ager31e71382008-08-13 09:32:07 +0000324 __ mov(r0, Operand(Factory::undefined_value()));
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 __ bind(&function_return_);
mads.s.ager31e71382008-08-13 09:32:07 +0000327 if (FLAG_trace) {
328 // Push the return value on the stack as the parameter.
329 // Runtime::TraceExit returns the parameter as it is.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000330 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000331 __ CallRuntime(Runtime::kTraceExit, 1);
332 }
333
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000334 // Tear down the frame which will restore the caller's frame pointer and the
335 // link register.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000336 frame_->Exit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000338 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
339 __ mov(pc, lr);
340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Code generation state must be reset.
342 scope_ = NULL;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000343 frame_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 ASSERT(!has_cc());
345 ASSERT(state_ == NULL);
346}
347
348
ager@chromium.org7c537e22008-10-16 08:43:32 +0000349MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
350 // Currently, this assertion will fail if we try to assign to
351 // a constant variable that is constant because it is read-only
352 // (such as the variable referring to a named function expression).
353 // We need to implement assignments to read-only variables.
354 // Ideally, we should do this during AST generation (by converting
355 // such assignments into expression statements); however, in general
356 // we may not be able to make the decision until past AST generation,
357 // that is when the entire program is known.
358 ASSERT(slot != NULL);
359 int index = slot->index();
360 switch (slot->type()) {
361 case Slot::PARAMETER:
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000362 return frame_->Parameter(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000363
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000364 case Slot::LOCAL:
365 return frame_->Local(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000366
367 case Slot::CONTEXT: {
368 // Follow the context chain if necessary.
369 ASSERT(!tmp.is(cp)); // do not overwrite context register
370 Register context = cp;
371 int chain_length = scope()->ContextChainLength(slot->var()->scope());
372 for (int i = chain_length; i-- > 0;) {
373 // Load the closure.
374 // (All contexts, even 'with' contexts, have a closure,
375 // and it is the same for all contexts inside a function.
376 // There is no need to go to the function context first.)
377 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
378 // Load the function context (which is the incoming, outer context).
379 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
380 context = tmp;
381 }
382 // We may have a 'with' context now. Get the function context.
383 // (In fact this mov may never be the needed, since the scope analysis
384 // may not permit a direct context access in this case and thus we are
385 // always at a function context. However it is safe to dereference be-
386 // cause the function context of a function context is itself. Before
387 // deleting this mov we should try to create a counter-example first,
388 // though...)
389 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
390 return ContextOperand(tmp, index);
391 }
392
393 default:
394 UNREACHABLE();
395 return MemOperand(r0, 0);
396 }
397}
398
399
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000400// Loads a value on TOS. If it is a boolean value, the result may have been
401// (partially) translated into branches, or it may have set the condition
402// code register. If force_cc is set, the value is forced to set the
403// condition code register and no value is pushed. If the condition code
404// register was set, has_cc() is true and cc_reg_ contains the condition to
405// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000406void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000407 TypeofState typeof_state,
408 Label* true_target,
409 Label* false_target,
410 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000411 ASSERT(!has_cc());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000414 Visit(x);
415 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 if (force_cc && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000417 // Convert the TOS value to a boolean in the condition code register.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000418 // Visiting an expression may possibly choose neither (a) to leave a
419 // value in the condition code register nor (b) to leave a value in TOS
420 // (eg, by compiling to only jumps to the targets). In that case the
421 // code generated by ToBoolean is wrong because it assumes the value of
422 // the expression in TOS. So long as there is always a value in TOS or
423 // the condition code register when control falls through to here (there
424 // is), the code generated by ToBoolean is dead and therefore safe.
mads.s.ager31e71382008-08-13 09:32:07 +0000425 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 }
427 ASSERT(has_cc() || !force_cc);
428}
429
430
ager@chromium.org7c537e22008-10-16 08:43:32 +0000431void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 Label true_target;
433 Label false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000434 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
436 if (has_cc()) {
437 // convert cc_reg_ into a bool
438 Label loaded, materialize_true;
439 __ b(cc_reg_, &materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000440 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000441 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 __ b(&loaded);
443 __ bind(&materialize_true);
mads.s.ager31e71382008-08-13 09:32:07 +0000444 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000445 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446 __ bind(&loaded);
447 cc_reg_ = al;
448 }
449
450 if (true_target.is_linked() || false_target.is_linked()) {
451 // we have at least one condition value
452 // that has been "translated" into a branch,
453 // thus it needs to be loaded explicitly again
454 Label loaded;
455 __ b(&loaded); // don't lose current TOS
456 bool both = true_target.is_linked() && false_target.is_linked();
457 // reincarnate "true", if necessary
458 if (true_target.is_linked()) {
459 __ bind(&true_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000460 __ mov(r0, Operand(Factory::true_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000461 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 }
463 // if both "true" and "false" need to be reincarnated,
464 // jump across code for "false"
465 if (both)
466 __ b(&loaded);
467 // reincarnate "false", if necessary
468 if (false_target.is_linked()) {
469 __ bind(&false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000470 __ mov(r0, Operand(Factory::false_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000471 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 }
473 // everything is loaded at this point
474 __ bind(&loaded);
475 }
476 ASSERT(!has_cc());
477}
478
479
ager@chromium.org7c537e22008-10-16 08:43:32 +0000480void CodeGenerator::LoadGlobal() {
mads.s.ager31e71382008-08-13 09:32:07 +0000481 __ ldr(r0, GlobalObject());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000482 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483}
484
485
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000486void CodeGenerator::LoadGlobalReceiver(Register scratch) {
487 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
488 __ ldr(scratch,
489 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
490 frame_->Push(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000491}
492
493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000495// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
496// variables w/o reference errors elsewhere.
497void CodeGenerator::LoadTypeofExpression(Expression* x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 Variable* variable = x->AsVariableProxy()->AsVariable();
499 if (variable != NULL && !variable->is_this() && variable->is_global()) {
500 // NOTE: This is somewhat nasty. We force the compiler to load
501 // the variable as if through '<global>.<variable>' to make sure we
502 // do not get reference errors.
503 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
504 Literal key(variable->name());
505 // TODO(1241834): Fetch the position from the variable instead of using
506 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000507 Property property(&global, &key, RelocInfo::kNoPosition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 Load(&property);
509 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000510 Load(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 }
512}
513
514
ager@chromium.org7c537e22008-10-16 08:43:32 +0000515Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
517 cgen->LoadReference(this);
518}
519
520
521Reference::~Reference() {
522 cgen_->UnloadReference(this);
523}
524
525
ager@chromium.org7c537e22008-10-16 08:43:32 +0000526void CodeGenerator::LoadReference(Reference* ref) {
527 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 Expression* e = ref->expression();
529 Property* property = e->AsProperty();
530 Variable* var = e->AsVariableProxy()->AsVariable();
531
532 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000533 // The expression is either a property or a variable proxy that rewrites
534 // to a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 Load(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000536 // We use a named reference if the key is a literal symbol, unless it is
537 // a string that can be legally parsed as an integer. This is because
538 // otherwise we will not get into the slow case code that handles [] on
539 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 Literal* literal = property->key()->AsLiteral();
541 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542 if (literal != NULL &&
543 literal->handle()->IsSymbol() &&
544 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 ref->set_type(Reference::NAMED);
546 } else {
547 Load(property->key());
548 ref->set_type(Reference::KEYED);
549 }
550 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000551 // The expression is a variable proxy that does not rewrite to a
552 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 LoadGlobal();
555 ref->set_type(Reference::NAMED);
556 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000557 ASSERT(var->slot() != NULL);
558 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
560 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000561 // Anything else is a runtime error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562 Load(e);
563 __ CallRuntime(Runtime::kThrowReferenceError, 1);
564 }
565}
566
567
ager@chromium.org7c537e22008-10-16 08:43:32 +0000568void CodeGenerator::UnloadReference(Reference* ref) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000569 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000570 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000572 if (size > 0) {
573 frame_->Pop(r0);
574 frame_->Drop(size);
575 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 }
577}
578
579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
581// register to a boolean in the condition code register. The code
582// may jump to 'false_target' in case the register converts to 'false'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000583void CodeGenerator::ToBoolean(Label* true_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000584 Label* false_target) {
mads.s.ager31e71382008-08-13 09:32:07 +0000585 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 // Only the condition code should be set.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000587 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588
589 // Fast case checks
590
mads.s.ager31e71382008-08-13 09:32:07 +0000591 // Check if the value is 'false'.
592 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 __ b(eq, false_target);
594
mads.s.ager31e71382008-08-13 09:32:07 +0000595 // Check if the value is 'true'.
596 __ cmp(r0, Operand(Factory::true_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 __ b(eq, true_target);
598
mads.s.ager31e71382008-08-13 09:32:07 +0000599 // Check if the value is 'undefined'.
600 __ cmp(r0, Operand(Factory::undefined_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 __ b(eq, false_target);
602
mads.s.ager31e71382008-08-13 09:32:07 +0000603 // Check if the value is a smi.
604 __ cmp(r0, Operand(Smi::FromInt(0)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 __ b(eq, false_target);
mads.s.ager31e71382008-08-13 09:32:07 +0000606 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 __ b(eq, true_target);
608
609 // Slow case: call the runtime.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000610 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000611 __ CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000612 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614
615 cc_reg_ = ne;
616}
617
618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619class GetPropertyStub : public CodeStub {
620 public:
621 GetPropertyStub() { }
622
623 private:
624 Major MajorKey() { return GetProperty; }
625 int MinorKey() { return 0; }
626 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627};
628
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630class SetPropertyStub : public CodeStub {
631 public:
632 SetPropertyStub() { }
633
634 private:
635 Major MajorKey() { return SetProperty; }
636 int MinorKey() { return 0; }
637 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638};
639
640
kasper.lund7276f142008-07-30 08:49:36 +0000641class GenericBinaryOpStub : public CodeStub {
642 public:
643 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
644
645 private:
646 Token::Value op_;
647
648 Major MajorKey() { return GenericBinaryOp; }
649 int MinorKey() { return static_cast<int>(op_); }
650 void Generate(MacroAssembler* masm);
651
652 const char* GetName() {
653 switch (op_) {
654 case Token::ADD: return "GenericBinaryOpStub_ADD";
655 case Token::SUB: return "GenericBinaryOpStub_SUB";
656 case Token::MUL: return "GenericBinaryOpStub_MUL";
657 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000658 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
659 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
660 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
661 case Token::SAR: return "GenericBinaryOpStub_SAR";
662 case Token::SHL: return "GenericBinaryOpStub_SHL";
663 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000664 default: return "GenericBinaryOpStub";
665 }
666 }
667
668#ifdef DEBUG
669 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
670#endif
671};
672
673
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674class InvokeBuiltinStub : public CodeStub {
675 public:
676 enum Kind { Inc, Dec, ToNumber };
677 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
678
679 private:
680 Kind kind_;
681 int argc_;
682
683 Major MajorKey() { return InvokeBuiltin; }
684 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
685 void Generate(MacroAssembler* masm);
686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687#ifdef DEBUG
688 void Print() {
689 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
690 static_cast<int>(kind_),
691 argc_);
692 }
693#endif
694};
695
696
ager@chromium.org7c537e22008-10-16 08:43:32 +0000697void CodeGenerator::GenericBinaryOperation(Token::Value op) {
mads.s.ager31e71382008-08-13 09:32:07 +0000698 // sp[0] : y
699 // sp[1] : x
700 // result : r0
701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 // Stub is entered with a call: 'return address' is in lr.
703 switch (op) {
704 case Token::ADD: // fall through.
705 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000706 case Token::MUL:
707 case Token::BIT_OR:
708 case Token::BIT_AND:
709 case Token::BIT_XOR:
710 case Token::SHL:
711 case Token::SHR:
712 case Token::SAR: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000713 frame_->Pop(r0); // r0 : y
714 frame_->Pop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +0000715 GenericBinaryOpStub stub(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 __ CallStub(&stub);
717 break;
718 }
719
720 case Token::DIV: {
mads.s.ager31e71382008-08-13 09:32:07 +0000721 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000722 __ InvokeBuiltin(Builtins::DIV, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 break;
724 }
725
726 case Token::MOD: {
mads.s.ager31e71382008-08-13 09:32:07 +0000727 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000728 __ InvokeBuiltin(Builtins::MOD, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729 break;
730 }
731
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 case Token::COMMA:
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000733 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 // simply discard left value
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000735 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 break;
737
738 default:
739 // Other cases should have been handled before this point.
740 UNREACHABLE();
741 break;
742 }
743}
744
745
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000746class DeferredInlinedSmiOperation: public DeferredCode {
747 public:
748 DeferredInlinedSmiOperation(CodeGenerator* generator, Token::Value op,
749 int value, bool reversed) :
750 DeferredCode(generator), op_(op), value_(value), reversed_(reversed) {
751 set_comment("[ DeferredInlinedSmiOperation");
752 }
753
754 virtual void Generate() {
755 switch (op_) {
756 case Token::ADD: {
757 if (reversed_) {
758 // revert optimistic add
759 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
760 __ mov(r1, Operand(Smi::FromInt(value_))); // x
761 } else {
762 // revert optimistic add
763 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
764 __ mov(r0, Operand(Smi::FromInt(value_)));
765 }
766 break;
767 }
768
769 case Token::SUB: {
770 if (reversed_) {
771 // revert optimistic sub
772 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
773 __ mov(r1, Operand(Smi::FromInt(value_)));
774 } else {
775 __ add(r1, r0, Operand(Smi::FromInt(value_)));
776 __ mov(r0, Operand(Smi::FromInt(value_)));
777 }
778 break;
779 }
780
781 case Token::BIT_OR:
782 case Token::BIT_XOR:
783 case Token::BIT_AND: {
784 if (reversed_) {
785 __ mov(r1, Operand(Smi::FromInt(value_)));
786 } else {
787 __ mov(r1, Operand(r0));
788 __ mov(r0, Operand(Smi::FromInt(value_)));
789 }
790 break;
791 }
792
793 case Token::SHL:
794 case Token::SHR:
795 case Token::SAR: {
796 if (!reversed_) {
797 __ mov(r1, Operand(r0));
798 __ mov(r0, Operand(Smi::FromInt(value_)));
799 } else {
800 UNREACHABLE(); // should have been handled in SmiOperation
801 }
802 break;
803 }
804
805 default:
806 // other cases should have been handled before this point.
807 UNREACHABLE();
808 break;
809 }
810
811 GenericBinaryOpStub igostub(op_);
812 __ CallStub(&igostub);
813 }
814
815 private:
816 Token::Value op_;
817 int value_;
818 bool reversed_;
819};
820
821
ager@chromium.org7c537e22008-10-16 08:43:32 +0000822void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000823 Handle<Object> value,
824 bool reversed) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 // NOTE: This is an attempt to inline (a bit) more of the code for
826 // some possible smi operations (like + and -) when (at least) one
827 // of the operands is a literal smi. With this optimization, the
828 // performance of the system is increased by ~15%, and the generated
829 // code size is increased by ~1% (measured on a combination of
830 // different benchmarks).
831
mads.s.ager31e71382008-08-13 09:32:07 +0000832 // sp[0] : operand
833
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000834 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835
836 Label exit;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000837 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
839 switch (op) {
840 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000841 DeferredCode* deferred =
842 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000844 __ add(r0, r0, Operand(value), SetCC);
845 __ b(vs, deferred->enter());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 __ tst(r0, Operand(kSmiTagMask));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000847 __ b(ne, deferred->enter());
848 __ bind(deferred->exit());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 break;
850 }
851
852 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000853 DeferredCode* deferred =
854 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000857 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000859 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000861 __ b(vs, deferred->enter());
862 __ tst(r0, Operand(kSmiTagMask));
863 __ b(ne, deferred->enter());
864 __ bind(deferred->exit());
865 break;
866 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000868 case Token::BIT_OR:
869 case Token::BIT_XOR:
870 case Token::BIT_AND: {
871 DeferredCode* deferred =
872 new DeferredInlinedSmiOperation(this, op, int_value, reversed);
873 __ tst(r0, Operand(kSmiTagMask));
874 __ b(ne, deferred->enter());
875 switch (op) {
876 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
877 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
878 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
879 default: UNREACHABLE();
880 }
881 __ bind(deferred->exit());
882 break;
883 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 case Token::SHL:
886 case Token::SHR:
887 case Token::SAR: {
888 if (reversed) {
889 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000890 frame_->Push(ip);
891 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 GenericBinaryOperation(op);
893
894 } else {
895 int shift_value = int_value & 0x1f; // least significant 5 bits
896 DeferredCode* deferred =
897 new DeferredInlinedSmiOperation(this, op, shift_value, false);
898 __ tst(r0, Operand(kSmiTagMask));
899 __ b(ne, deferred->enter());
900 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
901 switch (op) {
902 case Token::SHL: {
903 __ mov(r2, Operand(r2, LSL, shift_value));
904 // check that the *unsigned* result fits in a smi
905 __ add(r3, r2, Operand(0x40000000), SetCC);
906 __ b(mi, deferred->enter());
907 break;
908 }
909 case Token::SHR: {
910 // LSR by immediate 0 means shifting 32 bits.
911 if (shift_value != 0) {
912 __ mov(r2, Operand(r2, LSR, shift_value));
913 }
914 // check that the *unsigned* result fits in a smi
915 // neither of the two high-order bits can be set:
916 // - 0x80000000: high bit would be lost when smi tagging
917 // - 0x40000000: this number would convert to negative when
918 // smi tagging these two cases can only happen with shifts
919 // by 0 or 1 when handed a valid smi
920 __ and_(r3, r2, Operand(0xc0000000), SetCC);
921 __ b(ne, deferred->enter());
922 break;
923 }
924 case Token::SAR: {
925 if (shift_value != 0) {
926 // ASR by immediate 0 means shifting 32 bits.
927 __ mov(r2, Operand(r2, ASR, shift_value));
928 }
929 break;
930 }
931 default: UNREACHABLE();
932 }
933 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
934 __ bind(deferred->exit());
935 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 break;
937 }
938
939 default:
940 if (!reversed) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000941 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000942 __ mov(r0, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000943 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 } else {
945 __ mov(ip, Operand(value));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000946 frame_->Push(ip);
947 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 }
kasper.lund7276f142008-07-30 08:49:36 +0000949 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 break;
951 }
952
953 __ bind(&exit);
954}
955
956
ager@chromium.org7c537e22008-10-16 08:43:32 +0000957void CodeGenerator::Comparison(Condition cc, bool strict) {
mads.s.ager31e71382008-08-13 09:32:07 +0000958 // sp[0] : y
959 // sp[1] : x
960 // result : cc register
961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 // Strict only makes sense for equality comparisons.
963 ASSERT(!strict || cc == eq);
964
965 Label exit, smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000966 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
967 if (cc == gt || cc == le) {
968 cc = ReverseCondition(cc);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000969 frame_->Pop(r1);
970 frame_->Pop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000971 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000972 frame_->Pop(r0);
973 frame_->Pop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000974 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 __ orr(r2, r0, Operand(r1));
976 __ tst(r2, Operand(kSmiTagMask));
977 __ b(eq, &smi);
978
979 // Perform non-smi comparison by runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000980 frame_->Push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
982 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000983 Builtins::JavaScript native;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 int argc;
985 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000986 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 argc = 1;
988 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000989 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 int ncr; // NaN compare result
991 if (cc == lt || cc == le) {
992 ncr = GREATER;
993 } else {
994 ASSERT(cc == gt || cc == ge); // remaining cases
995 ncr = LESS;
996 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000997 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000998 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 argc = 2;
1000 }
1001
1002 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1003 // tagged as a small integer.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001004 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001005 __ mov(r0, Operand(argc));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001006 __ InvokeBuiltin(native, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 __ cmp(r0, Operand(0));
1008 __ b(&exit);
1009
1010 // test smi equality by pointer comparison.
1011 __ bind(&smi);
1012 __ cmp(r1, Operand(r0));
1013
1014 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 cc_reg_ = cc;
1016}
1017
1018
kasper.lund7276f142008-07-30 08:49:36 +00001019class CallFunctionStub: public CodeStub {
1020 public:
1021 explicit CallFunctionStub(int argc) : argc_(argc) {}
1022
1023 void Generate(MacroAssembler* masm);
1024
1025 private:
1026 int argc_;
1027
kasper.lund7276f142008-07-30 08:49:36 +00001028#if defined(DEBUG)
1029 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1030#endif // defined(DEBUG)
1031
1032 Major MajorKey() { return CallFunction; }
1033 int MinorKey() { return argc_; }
1034};
1035
1036
mads.s.ager31e71382008-08-13 09:32:07 +00001037// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001038void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 // Push the arguments ("left-to-right") on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001041 for (int i = 0; i < args->length(); i++) {
1042 Load(args->at(i));
1043 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044
kasper.lund7276f142008-07-30 08:49:36 +00001045 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001046 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047
kasper.lund7276f142008-07-30 08:49:36 +00001048 // Use the shared code stub to call the function.
1049 CallFunctionStub call_function(args->length());
1050 __ CallStub(&call_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051
1052 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001053 __ ldr(cp, frame_->Context());
1054 frame_->Pop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055}
1056
1057
ager@chromium.org7c537e22008-10-16 08:43:32 +00001058void CodeGenerator::Branch(bool if_true, Label* L) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 ASSERT(has_cc());
1060 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
1061 __ b(cc, L);
1062 cc_reg_ = al;
1063}
1064
1065
ager@chromium.org7c537e22008-10-16 08:43:32 +00001066void CodeGenerator::CheckStack() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 if (FLAG_check_stack) {
1068 Comment cmnt(masm_, "[ check stack");
1069 StackCheckStub stub;
1070 __ CallStub(&stub);
1071 }
1072}
1073
1074
ager@chromium.org7c537e22008-10-16 08:43:32 +00001075void CodeGenerator::VisitBlock(Block* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 Comment cmnt(masm_, "[ Block");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001077 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 node->set_break_stack_height(break_stack_height_);
1079 VisitStatements(node->statements());
1080 __ bind(node->break_target());
1081}
1082
1083
ager@chromium.org7c537e22008-10-16 08:43:32 +00001084void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
mads.s.ager31e71382008-08-13 09:32:07 +00001085 __ mov(r0, Operand(pairs));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001086 frame_->Push(r0);
1087 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001088 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001089 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 __ CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001091 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092}
1093
1094
ager@chromium.org7c537e22008-10-16 08:43:32 +00001095void CodeGenerator::VisitDeclaration(Declaration* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 Comment cmnt(masm_, "[ Declaration");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001097 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 Variable* var = node->proxy()->var();
1099 ASSERT(var != NULL); // must have been resolved
1100 Slot* slot = var->slot();
1101
1102 // If it was not possible to allocate the variable at compile time,
1103 // we need to "declare" it at runtime to make sure it actually
1104 // exists in the local context.
1105 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1106 // Variables with a "LOOKUP" slot were introduced as non-locals
1107 // during variable resolution and must have mode DYNAMIC.
1108 ASSERT(var->mode() == Variable::DYNAMIC);
1109 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001110 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001111 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001112 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 // Declaration nodes are always declared in only two modes.
1114 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1115 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001116 __ mov(r0, Operand(Smi::FromInt(attr)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001117 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 // Push initial value, if any.
1119 // Note: For variables we must not push an initial value (such as
1120 // 'undefined') because we may have a (legal) redeclaration and we
1121 // must not destroy the current value.
1122 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001123 __ mov(r0, Operand(Factory::the_hole_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001124 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 } else if (node->fun() != NULL) {
1126 Load(node->fun());
1127 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001128 __ mov(r0, Operand(0)); // no initial value!
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001129 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001131 __ CallRuntime(Runtime::kDeclareContextSlot, 4);
1132 // Ignore the return value (declarations are statements).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 return;
1134 }
1135
1136 ASSERT(!var->is_global());
1137
1138 // If we have a function or a constant, we need to initialize the variable.
1139 Expression* val = NULL;
1140 if (node->mode() == Variable::CONST) {
1141 val = new Literal(Factory::the_hole_value());
1142 } else {
1143 val = node->fun(); // NULL if we don't have a function
1144 }
1145
1146 if (val != NULL) {
1147 // Set initial value.
1148 Reference target(this, node->proxy());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001149 ASSERT(target.is_slot());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 Load(val);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001151 target.SetValue(NOT_CONST_INIT);
1152 // Get rid of the assigned value (declarations are statements). It's
1153 // safe to pop the value lying on top of the reference before unloading
1154 // the reference itself (which preserves the top of stack) because we
1155 // know it is a zero-sized reference.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001156 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 }
1158}
1159
1160
ager@chromium.org7c537e22008-10-16 08:43:32 +00001161void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 Comment cmnt(masm_, "[ ExpressionStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001163 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 Expression* expression = node->expression();
1165 expression->MarkAsStatement();
1166 Load(expression);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001167 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168}
1169
1170
ager@chromium.org7c537e22008-10-16 08:43:32 +00001171void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 Comment cmnt(masm_, "// EmptyStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001173 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 // nothing to do
1175}
1176
1177
ager@chromium.org7c537e22008-10-16 08:43:32 +00001178void CodeGenerator::VisitIfStatement(IfStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 Comment cmnt(masm_, "[ IfStatement");
1180 // Generate different code depending on which
1181 // parts of the if statement are present or not.
1182 bool has_then_stm = node->HasThenStatement();
1183 bool has_else_stm = node->HasElseStatement();
1184
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001185 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186
1187 Label exit;
1188 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001189 Comment cmnt(masm_, "[ IfThenElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 Label then;
1191 Label else_;
1192 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001193 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 Branch(false, &else_);
1195 // then
1196 __ bind(&then);
1197 Visit(node->then_statement());
1198 __ b(&exit);
1199 // else
1200 __ bind(&else_);
1201 Visit(node->else_statement());
1202
1203 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001204 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 ASSERT(!has_else_stm);
1206 Label then;
1207 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001208 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &exit, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209 Branch(false, &exit);
1210 // then
1211 __ bind(&then);
1212 Visit(node->then_statement());
1213
1214 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001215 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 ASSERT(!has_then_stm);
1217 Label else_;
1218 // if (!cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001219 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 Branch(true, &exit);
1221 // else
1222 __ bind(&else_);
1223 Visit(node->else_statement());
1224
1225 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001226 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 ASSERT(!has_then_stm && !has_else_stm);
1228 // if (cond)
ager@chromium.org7c537e22008-10-16 08:43:32 +00001229 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &exit, &exit, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 if (has_cc()) {
1231 cc_reg_ = al;
1232 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001233 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 }
1235 }
1236
1237 // end
1238 __ bind(&exit);
1239}
1240
1241
ager@chromium.org7c537e22008-10-16 08:43:32 +00001242void CodeGenerator::CleanStack(int num_bytes) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001243 ASSERT(num_bytes % kPointerSize == 0);
1244 frame_->Drop(num_bytes / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245}
1246
1247
ager@chromium.org7c537e22008-10-16 08:43:32 +00001248void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 Comment cmnt(masm_, "[ ContinueStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001250 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1252 __ b(node->target()->continue_target());
1253}
1254
1255
ager@chromium.org7c537e22008-10-16 08:43:32 +00001256void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 Comment cmnt(masm_, "[ BreakStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001258 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 CleanStack(break_stack_height_ - node->target()->break_stack_height());
1260 __ b(node->target()->break_target());
1261}
1262
1263
ager@chromium.org7c537e22008-10-16 08:43:32 +00001264void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 Comment cmnt(masm_, "[ ReturnStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001266 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 Load(node->expression());
mads.s.ager31e71382008-08-13 09:32:07 +00001268 // Move the function result into r0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001269 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001270
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 __ b(&function_return_);
1272}
1273
1274
ager@chromium.org7c537e22008-10-16 08:43:32 +00001275void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 Comment cmnt(masm_, "[ WithEnterStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001277 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Load(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001279 if (node->is_catch_block()) {
1280 __ CallRuntime(Runtime::kPushCatchContext, 1);
1281 } else {
1282 __ CallRuntime(Runtime::kPushContext, 1);
1283 }
kasper.lund7276f142008-07-30 08:49:36 +00001284 if (kDebug) {
1285 Label verified_true;
1286 __ cmp(r0, Operand(cp));
1287 __ b(eq, &verified_true);
1288 __ stop("PushContext: r0 is expected to be the same as cp");
1289 __ bind(&verified_true);
1290 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001292 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293}
1294
1295
ager@chromium.org7c537e22008-10-16 08:43:32 +00001296void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 Comment cmnt(masm_, "[ WithExitStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001298 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 // Pop context.
1300 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1301 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001302 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303}
1304
1305
ager@chromium.org7c537e22008-10-16 08:43:32 +00001306int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
1307 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001308}
1309
ager@chromium.org7c537e22008-10-16 08:43:32 +00001310int CodeGenerator::FastCaseSwitchMinCaseCount() {
1311 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001312}
1313
1314
ager@chromium.org7c537e22008-10-16 08:43:32 +00001315void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001316 SwitchStatement* node,
1317 int min_index,
1318 int range,
1319 Label* fail_label,
1320 Vector<Label*> case_targets,
1321 Vector<Label> case_labels) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001322
1323 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
1324
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001325 frame_->Pop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001326
1327 // Test for a Smi value in a HeapNumber.
1328 Label is_smi;
1329 __ tst(r0, Operand(kSmiTagMask));
1330 __ b(eq, &is_smi);
1331 __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag));
1332 __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag));
1333 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
1334 __ b(ne, fail_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001335 frame_->Push(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001336 __ CallRuntime(Runtime::kNumberToSmi, 1);
1337 __ bind(&is_smi);
1338
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001339 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001340 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001341 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001342 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1343 if (Smi::IsValid(-min_index)) {
1344 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1345 } else {
1346 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1347 __ add(r0, r0, Operand(Smi::FromInt(1)));
1348 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001349 } else {
1350 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1351 }
1352 }
1353 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
1354 __ b(ne, fail_label);
1355 __ cmp(r0, Operand(Smi::FromInt(range)));
1356 __ b(ge, fail_label);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001357 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001358
1359 GenerateFastCaseSwitchCases(node, case_labels);
1360}
1361
1362
ager@chromium.org7c537e22008-10-16 08:43:32 +00001363void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 Comment cmnt(masm_, "[ SwitchStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001365 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 node->set_break_stack_height(break_stack_height_);
1367
1368 Load(node->tag());
1369
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001370 if (TryGenerateFastCaseSwitchStatement(node)) {
1371 return;
1372 }
1373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 Label next, fall_through, default_case;
1375 ZoneList<CaseClause*>* cases = node->cases();
1376 int length = cases->length();
1377
1378 for (int i = 0; i < length; i++) {
1379 CaseClause* clause = cases->at(i);
1380
1381 Comment cmnt(masm_, "[ case clause");
1382
1383 if (clause->is_default()) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001384 // Continue matching cases. The program will execute the default case's
1385 // statements if it does not match any of the cases.
1386 __ b(&next);
1387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 // Bind the default case label, so we can branch to it when we
1389 // have compared against all other cases.
1390 ASSERT(default_case.is_unused()); // at most one default clause
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001391 __ bind(&default_case);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 } else {
1393 __ bind(&next);
1394 next.Unuse();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001395 __ ldr(r0, frame_->Top());
1396 frame_->Push(r0); // duplicate TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 Load(clause->label());
1398 Comparison(eq, true);
1399 Branch(false, &next);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 }
1401
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001402 // Entering the case statement for the first time. Remove the switch value
1403 // from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001404 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 // Generate code for the body.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001407 // This is also the target for the fall through from the previous case's
1408 // statements which has to skip over the matching code and the popping of
1409 // the switch value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 __ bind(&fall_through);
1411 fall_through.Unuse();
1412 VisitStatements(clause->statements());
1413 __ b(&fall_through);
1414 }
1415
1416 __ bind(&next);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001417 // Reached the end of the case statements without matching any of the cases.
1418 if (default_case.is_bound()) {
1419 // A default case exists -> execute its statements.
1420 __ b(&default_case);
1421 } else {
1422 // Remove the switch value from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001423 frame_->Pop();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001424 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425
1426 __ bind(&fall_through);
1427 __ bind(node->break_target());
1428}
1429
1430
ager@chromium.org7c537e22008-10-16 08:43:32 +00001431void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 Comment cmnt(masm_, "[ LoopStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001433 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 node->set_break_stack_height(break_stack_height_);
1435
1436 // simple condition analysis
1437 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1438 if (node->cond() == NULL) {
1439 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1440 info = ALWAYS_TRUE;
1441 } else {
1442 Literal* lit = node->cond()->AsLiteral();
1443 if (lit != NULL) {
1444 if (lit->IsTrue()) {
1445 info = ALWAYS_TRUE;
1446 } else if (lit->IsFalse()) {
1447 info = ALWAYS_FALSE;
1448 }
1449 }
1450 }
1451
1452 Label loop, entry;
1453
1454 // init
1455 if (node->init() != NULL) {
1456 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1457 Visit(node->init());
1458 }
1459 if (node->type() != LoopStatement::DO_LOOP && info != ALWAYS_TRUE) {
1460 __ b(&entry);
1461 }
1462
1463 // body
1464 __ bind(&loop);
1465 Visit(node->body());
1466
1467 // next
1468 __ bind(node->continue_target());
1469 if (node->next() != NULL) {
1470 // Record source position of the statement as this code which is after the
1471 // code for the body actually belongs to the loop statement and not the
1472 // body.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001473 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1475 Visit(node->next());
1476 }
1477
1478 // cond
1479 __ bind(&entry);
1480 switch (info) {
1481 case ALWAYS_TRUE:
1482 CheckStack(); // TODO(1222600): ignore if body contains calls.
1483 __ b(&loop);
1484 break;
1485 case ALWAYS_FALSE:
1486 break;
1487 case DONT_KNOW:
1488 CheckStack(); // TODO(1222600): ignore if body contains calls.
1489 LoadCondition(node->cond(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00001490 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 &loop,
1492 node->break_target(),
1493 true);
1494 Branch(true, &loop);
1495 break;
1496 }
1497
1498 // exit
1499 __ bind(node->break_target());
1500}
1501
1502
ager@chromium.org7c537e22008-10-16 08:43:32 +00001503void CodeGenerator::VisitForInStatement(ForInStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 Comment cmnt(masm_, "[ ForInStatement");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001505 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506
1507 // We keep stuff on the stack while the body is executing.
1508 // Record it, so that a break/continue crossing this statement
1509 // can restore the stack.
1510 const int kForInStackSize = 5 * kPointerSize;
1511 break_stack_height_ += kForInStackSize;
1512 node->set_break_stack_height(break_stack_height_);
1513
1514 Label loop, next, entry, cleanup, exit, primitive, jsobject;
1515 Label filter_key, end_del_check, fixed_array, non_string;
1516
1517 // Get the object to enumerate over (converted to JSObject).
1518 Load(node->enumerable());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001519 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520
1521 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1522 // to the specification. 12.6.4 mandates a call to ToObject.
1523 __ cmp(r0, Operand(Factory::undefined_value()));
1524 __ b(eq, &exit);
1525 __ cmp(r0, Operand(Factory::null_value()));
1526 __ b(eq, &exit);
1527
1528 // Stack layout in body:
1529 // [iteration counter (Smi)]
1530 // [length of array]
1531 // [FixedArray]
1532 // [Map or 0]
1533 // [Object]
1534
1535 // Check if enumerable is already a JSObject
1536 __ tst(r0, Operand(kSmiTagMask));
1537 __ b(eq, &primitive);
1538 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1539 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001540 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 __ b(hs, &jsobject);
1542
1543 __ bind(&primitive);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001544 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001545 __ mov(r0, Operand(0));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001546 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547
1548
1549 __ bind(&jsobject);
1550
1551 // Get the set of properties (as a FixedArray or Map).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001552 frame_->Push(r0); // duplicate the object being enumerated
1553 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 __ CallRuntime(Runtime::kGetPropertyNamesFast, 1);
1555
1556 // If we got a Map, we can do a fast modification check.
1557 // Otherwise, we got a FixedArray, and we have to do a slow check.
1558 __ mov(r2, Operand(r0));
1559 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1560 __ cmp(r1, Operand(Factory::meta_map()));
1561 __ b(ne, &fixed_array);
1562
1563 // Get enum cache
1564 __ mov(r1, Operand(r0));
1565 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1566 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1567 __ ldr(r2,
1568 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1569
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001570 frame_->Push(r0); // map
1571 frame_->Push(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001572 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001574 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001575 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001576 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 __ b(&entry);
1578
1579
1580 __ bind(&fixed_array);
1581
1582 __ mov(r1, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001583 frame_->Push(r1); // insert 0 in place of Map
1584 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585
1586 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001587 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001589 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001590 __ mov(r0, Operand(Smi::FromInt(0))); // init index
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001591 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 __ b(&entry);
1594
1595 // Body.
1596 __ bind(&loop);
1597 Visit(node->body());
1598
1599 // Next.
1600 __ bind(node->continue_target());
1601 __ bind(&next);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001602 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 __ add(r0, r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001604 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
1606 // Condition.
1607 __ bind(&entry);
1608
mads.s.ager31e71382008-08-13 09:32:07 +00001609 // sp[0] : index
1610 // sp[1] : array/enum cache length
1611 // sp[2] : array or enum cache
1612 // sp[3] : 0 or map
1613 // sp[4] : enumerable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001614 __ ldr(r0, frame_->Element(0)); // load the current count
1615 __ ldr(r1, frame_->Element(1)); // load the length
mads.s.ager31e71382008-08-13 09:32:07 +00001616 __ cmp(r0, Operand(r1)); // compare to the array length
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 __ b(hs, &cleanup);
1618
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001619 __ ldr(r0, frame_->Element(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001620
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 // Get the i'th entry of the array.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001622 __ ldr(r2, frame_->Element(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1624 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1625
1626 // Get Map or 0.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001627 __ ldr(r2, frame_->Element(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 // Check if this (still) matches the map of the enumerable.
1629 // If not, we have to filter the key.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001630 __ ldr(r1, frame_->Element(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1632 __ cmp(r1, Operand(r2));
1633 __ b(eq, &end_del_check);
1634
1635 // Convert the entry to a string (or null if it isn't a property anymore).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001636 __ ldr(r0, frame_->Element(4)); // push enumerable
1637 frame_->Push(r0);
1638 frame_->Push(r3); // push entry
mads.s.ager31e71382008-08-13 09:32:07 +00001639 __ mov(r0, Operand(1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001640 __ InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642
1643 // If the property has been removed while iterating, we just skip it.
1644 __ cmp(r3, Operand(Factory::null_value()));
1645 __ b(eq, &next);
1646
1647
1648 __ bind(&end_del_check);
1649
1650 // Store the entry in the 'each' expression and take another spin in the loop.
mads.s.ager31e71382008-08-13 09:32:07 +00001651 // r3: i'th entry of the enum cache (or string there of)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001652 frame_->Push(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 { Reference each(this, node->each());
1654 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001655 if (each.size() > 0) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001656 __ ldr(r0, frame_->Element(each.size()));
1657 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001658 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001659 // If the reference was to a slot we rely on the convenient property
1660 // that it doesn't matter whether a value (eg, r3 pushed above) is
1661 // right on top of or right underneath a zero-sized reference.
1662 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001663 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001664 // It's safe to pop the value lying on top of the reference before
1665 // unloading the reference itself (which preserves the top of stack,
1666 // ie, now the topmost value of the non-zero sized reference), since
1667 // we will discard the top of stack after unloading the reference
1668 // anyway.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001669 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001670 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 }
1672 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001673 // Discard the i'th entry pushed above or else the remainder of the
1674 // reference, whichever is currently on top of the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001675 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 CheckStack(); // TODO(1222600): ignore if body contains calls.
1677 __ jmp(&loop);
1678
1679 // Cleanup.
1680 __ bind(&cleanup);
1681 __ bind(node->break_target());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001682 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
1684 // Exit.
1685 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
1687 break_stack_height_ -= kForInStackSize;
1688}
1689
1690
ager@chromium.org7c537e22008-10-16 08:43:32 +00001691void CodeGenerator::VisitTryCatch(TryCatch* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 Comment cmnt(masm_, "[ TryCatch");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001693 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694
1695 Label try_block, exit;
1696
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 __ bl(&try_block);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // --- Catch block ---
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001699 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 // Store the caught exception in the catch variable.
1702 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001703 ASSERT(ref.is_slot());
1704 // Here we make use of the convenient property that it doesn't matter
1705 // whether a value is immediately on top of or underneath a zero-sized
1706 // reference.
1707 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 }
1709
1710 // Remove the exception from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001711 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712
1713 VisitStatements(node->catch_block()->statements());
1714 __ b(&exit);
1715
1716
1717 // --- Try block ---
1718 __ bind(&try_block);
1719
1720 __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
1721
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001722 // Shadow the labels for all escapes from the try block, including
1723 // returns. During shadowing, the original label is hidden as the
1724 // LabelShadow and operations on the original actually affect the
1725 // shadowing label.
1726 //
1727 // We should probably try to unify the escaping labels and the return
1728 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 int nof_escapes = node->escaping_labels()->length();
1730 List<LabelShadow*> shadows(1 + nof_escapes);
1731 shadows.Add(new LabelShadow(&function_return_));
1732 for (int i = 0; i < nof_escapes; i++) {
1733 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1734 }
1735
1736 // Generate code for the statements in the try block.
1737 VisitStatements(node->try_block()->statements());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001738 frame_->Pop(); // Discard the result.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
1740 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001741 // After shadowing stops, the original labels are unshadowed and the
1742 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 int nof_unlinks = 0;
1744 for (int i = 0; i <= nof_escapes; i++) {
1745 shadows[i]->StopShadowing();
1746 if (shadows[i]->is_linked()) nof_unlinks++;
1747 }
1748
1749 // Unlink from try chain.
1750 // TOS contains code slot
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001751 const int kNextIndex = (StackHandlerConstants::kNextOffset
1752 + StackHandlerConstants::kAddressDisplacement)
1753 / kPointerSize;
1754 __ ldr(r1, frame_->Element(kNextIndex)); // read next_sp
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1756 __ str(r1, MemOperand(r3));
1757 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001758 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // Code slot popped.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 if (nof_unlinks > 0) __ b(&exit);
1761
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001762 // Generate unlink code for the (formerly) shadowing labels that have been
1763 // jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 for (int i = 0; i <= nof_escapes; i++) {
1765 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001766 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 __ bind(shadows[i]);
1768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 // Reload sp from the top handler, because some statements that we
1770 // break from (eg, for...in) may have left stuff on the stack.
1771 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1772 __ ldr(sp, MemOperand(r3));
1773
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001774 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 __ str(r1, MemOperand(r3));
1776 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001777 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 // Code slot popped.
1779
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001780 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 }
1782 }
1783
1784 __ bind(&exit);
1785}
1786
1787
ager@chromium.org7c537e22008-10-16 08:43:32 +00001788void CodeGenerator::VisitTryFinally(TryFinally* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 Comment cmnt(masm_, "[ TryFinally");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001790 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791
1792 // State: Used to keep track of reason for entering the finally
1793 // block. Should probably be extended to hold information for
1794 // break/continue from within the try block.
1795 enum { FALLING, THROWING, JUMPING };
1796
1797 Label exit, unlink, try_block, finally_block;
1798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 __ bl(&try_block);
1800
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001801 frame_->Push(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 // In case of thrown exceptions, this is where we continue.
1803 __ mov(r2, Operand(Smi::FromInt(THROWING)));
1804 __ b(&finally_block);
1805
1806
1807 // --- Try block ---
1808 __ bind(&try_block);
1809
1810 __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
1811
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001812 // Shadow the labels for all escapes from the try block, including
1813 // returns. Shadowing hides the original label as the LabelShadow and
1814 // operations on the original actually affect the shadowing label.
1815 //
1816 // We should probably try to unify the escaping labels and the return
1817 // label.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 int nof_escapes = node->escaping_labels()->length();
1819 List<LabelShadow*> shadows(1 + nof_escapes);
1820 shadows.Add(new LabelShadow(&function_return_));
1821 for (int i = 0; i < nof_escapes; i++) {
1822 shadows.Add(new LabelShadow(node->escaping_labels()->at(i)));
1823 }
1824
1825 // Generate code for the statements in the try block.
1826 VisitStatements(node->try_block()->statements());
1827
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001828 // Stop the introduced shadowing and count the number of required unlinks.
1829 // After shadowing stops, the original labels are unshadowed and the
1830 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 int nof_unlinks = 0;
1832 for (int i = 0; i <= nof_escapes; i++) {
1833 shadows[i]->StopShadowing();
1834 if (shadows[i]->is_linked()) nof_unlinks++;
1835 }
1836
1837 // Set the state on the stack to FALLING.
mads.s.ager31e71382008-08-13 09:32:07 +00001838 __ mov(r0, Operand(Factory::undefined_value())); // fake TOS
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001839 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 __ mov(r2, Operand(Smi::FromInt(FALLING)));
1841 if (nof_unlinks > 0) __ b(&unlink);
1842
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001843 // Generate code to set the state for the (formerly) shadowing labels that
1844 // have been jumped to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 for (int i = 0; i <= nof_escapes; i++) {
1846 if (shadows[i]->is_linked()) {
1847 __ bind(shadows[i]);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001848 if (shadows[i]->original_label() == &function_return_) {
1849 // If this label shadowed the function return, materialize the
1850 // return value on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001851 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001852 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001853 // Fake TOS for labels that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00001854 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001855 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 }
1857 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
1858 __ b(&unlink);
1859 }
1860 }
1861
mads.s.ager31e71382008-08-13 09:32:07 +00001862 // Unlink from try chain;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 __ bind(&unlink);
1864
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001865 frame_->Pop(r0); // Store TOS in r0 across stack manipulation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // Reload sp from the top handler, because some statements that we
1867 // break from (eg, for...in) may have left stuff on the stack.
1868 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
1869 __ ldr(sp, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001870 const int kNextIndex = (StackHandlerConstants::kNextOffset
1871 + StackHandlerConstants::kAddressDisplacement)
1872 / kPointerSize;
1873 __ ldr(r1, frame_->Element(kNextIndex));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 __ str(r1, MemOperand(r3));
1875 ASSERT(StackHandlerConstants::kCodeOffset == 0); // first field is code
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001876 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 // Code slot popped.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001878 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
1880 // --- Finally block ---
1881 __ bind(&finally_block);
1882
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001883 // Push the state on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001884 frame_->Push(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001885
1886 // We keep two elements on the stack - the (possibly faked) result
1887 // and the state - while evaluating the finally block. Record it, so
1888 // that a break/continue crossing this statement can restore the
1889 // stack.
1890 const int kFinallyStackSize = 2 * kPointerSize;
1891 break_stack_height_ += kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
1893 // Generate code for the statements in the finally block.
1894 VisitStatements(node->finally_block()->statements());
1895
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001896 // Restore state and return value or faked TOS.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001897 frame_->Pop(r2);
1898 frame_->Pop(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001899 break_stack_height_ -= kFinallyStackSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001901 // Generate code to jump to the right destination for all used (formerly)
1902 // shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 for (int i = 0; i <= nof_escapes; i++) {
1904 if (shadows[i]->is_bound()) {
1905 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001906 if (shadows[i]->original_label() != &function_return_) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 Label next;
1908 __ b(ne, &next);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001909 __ b(shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 __ bind(&next);
1911 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001912 __ b(eq, shadows[i]->original_label());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 }
1914 }
1915 }
1916
1917 // Check if we need to rethrow the exception.
1918 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
1919 __ b(ne, &exit);
1920
1921 // Rethrow exception.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001922 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 __ CallRuntime(Runtime::kReThrow, 1);
1924
1925 // Done.
1926 __ bind(&exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927}
1928
1929
ager@chromium.org7c537e22008-10-16 08:43:32 +00001930void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 Comment cmnt(masm_, "[ DebuggerStatament");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001932 CodeForStatement(node);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001933 __ CallRuntime(Runtime::kDebugBreak, 0);
1934 // Ignore the return value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935}
1936
1937
ager@chromium.org7c537e22008-10-16 08:43:32 +00001938void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 ASSERT(boilerplate->IsBoilerplate());
1940
1941 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001942 __ mov(r0, Operand(boilerplate));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001943 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944
1945 // Create a new closure.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001946 frame_->Push(cp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 __ CallRuntime(Runtime::kNewClosure, 2);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001948 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949}
1950
1951
ager@chromium.org7c537e22008-10-16 08:43:32 +00001952void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 Comment cmnt(masm_, "[ FunctionLiteral");
1954
1955 // Build the function boilerplate and instantiate it.
1956 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00001957 // Check for stack-overflow exception.
1958 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 InstantiateBoilerplate(boilerplate);
1960}
1961
1962
ager@chromium.org7c537e22008-10-16 08:43:32 +00001963void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 FunctionBoilerplateLiteral* node) {
1965 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
1966 InstantiateBoilerplate(node->boilerplate());
1967}
1968
1969
ager@chromium.org7c537e22008-10-16 08:43:32 +00001970void CodeGenerator::VisitConditional(Conditional* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 Comment cmnt(masm_, "[ Conditional");
1972 Label then, else_, exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001973 LoadCondition(node->condition(), NOT_INSIDE_TYPEOF, &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 Branch(false, &else_);
1975 __ bind(&then);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001976 Load(node->then_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 __ b(&exit);
1978 __ bind(&else_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001979 Load(node->else_expression(), typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 __ bind(&exit);
1981}
1982
1983
ager@chromium.org7c537e22008-10-16 08:43:32 +00001984void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
1985 if (slot->type() == Slot::LOOKUP) {
1986 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987
1988 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001989 frame_->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001990 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001991 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992
ager@chromium.org7c537e22008-10-16 08:43:32 +00001993 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001994 __ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001995 } else {
1996 __ CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001998 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999
2000 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002001 // Note: We would like to keep the assert below, but it fires because of
2002 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org7c537e22008-10-16 08:43:32 +00002003 // ASSERT(slot->var()->mode() != Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002005 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002006 __ ldr(r0, SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002007 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002008 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002009 // Const slots may contain 'the hole' value (the constant hasn't been
2010 // initialized yet) which needs to be converted into the 'undefined'
2011 // value.
2012 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002013 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002014 __ cmp(r0, Operand(Factory::the_hole_value()));
2015 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002016 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 }
2018 }
2019}
2020
2021
ager@chromium.org7c537e22008-10-16 08:43:32 +00002022void CodeGenerator::VisitSlot(Slot* node) {
2023 Comment cmnt(masm_, "[ Slot");
2024 LoadFromSlot(node, typeof_state());
2025}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026
ager@chromium.org7c537e22008-10-16 08:43:32 +00002027
2028void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
2029 Comment cmnt(masm_, "[ VariableProxy");
2030
2031 Variable* var = node->var();
2032 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002033 if (expr != NULL) {
2034 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002036 ASSERT(var->is_global());
2037 Reference ref(this, node);
2038 ref.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 }
2040}
2041
2042
ager@chromium.org7c537e22008-10-16 08:43:32 +00002043void CodeGenerator::VisitLiteral(Literal* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002045 __ mov(r0, Operand(node->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002046 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047}
2048
2049
ager@chromium.org7c537e22008-10-16 08:43:32 +00002050void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 Comment cmnt(masm_, "[ RexExp Literal");
2052
2053 // Retrieve the literal array and check the allocated entry.
2054
2055 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002056 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057
2058 // Load the literals array of the function.
2059 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2060
2061 // Load the literal at the ast saved index.
2062 int literal_offset =
2063 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2064 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2065
2066 Label done;
2067 __ cmp(r2, Operand(Factory::undefined_value()));
2068 __ b(ne, &done);
2069
2070 // If the entry is undefined we call the runtime system to computed
2071 // the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002072 frame_->Push(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002073 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002074 frame_->Push(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002075 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002076 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002077 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002078 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 __ CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002080 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
mads.s.ager31e71382008-08-13 09:32:07 +00002082 __ bind(&done);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 // Push the literal.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002084 frame_->Push(r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085}
2086
2087
2088// This deferred code stub will be used for creating the boilerplate
2089// by calling Runtime_CreateObjectLiteral.
2090// Each created boilerplate is stored in the JSFunction and they are
2091// therefore context dependent.
2092class ObjectLiteralDeferred: public DeferredCode {
2093 public:
2094 ObjectLiteralDeferred(CodeGenerator* generator, ObjectLiteral* node)
2095 : DeferredCode(generator), node_(node) {
2096 set_comment("[ ObjectLiteralDeferred");
2097 }
2098 virtual void Generate();
2099 private:
2100 ObjectLiteral* node_;
2101};
2102
2103
2104void ObjectLiteralDeferred::Generate() {
2105 // If the entry is undefined we call the runtime system to computed
2106 // the literal.
2107
2108 // Literal array (0).
mads.s.ager31e71382008-08-13 09:32:07 +00002109 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002111 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2112 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002114 __ mov(r0, Operand(node_->constant_properties()));
2115 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002117 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118}
2119
2120
ager@chromium.org7c537e22008-10-16 08:43:32 +00002121void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 Comment cmnt(masm_, "[ ObjectLiteral");
2123
2124 ObjectLiteralDeferred* deferred = new ObjectLiteralDeferred(this, node);
2125
2126 // Retrieve the literal array and check the allocated entry.
2127
2128 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002129 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
2131 // Load the literals array of the function.
2132 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2133
2134 // Load the literal at the ast saved index.
2135 int literal_offset =
2136 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2137 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2138
2139 // Check whether we need to materialize the object literal boilerplate.
2140 // If so, jump to the deferred code.
2141 __ cmp(r2, Operand(Factory::undefined_value()));
2142 __ b(eq, deferred->enter());
2143 __ bind(deferred->exit());
2144
2145 // Push the object literal boilerplate.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002146 frame_->Push(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 // Clone the boilerplate object.
2149 __ CallRuntime(Runtime::kCloneObjectLiteralBoilerplate, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002150 frame_->Push(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002151 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152
2153 for (int i = 0; i < node->properties()->length(); i++) {
2154 ObjectLiteral::Property* property = node->properties()->at(i);
2155 Literal* key = property->key();
2156 Expression* value = property->value();
2157 switch (property->kind()) {
2158 case ObjectLiteral::Property::CONSTANT: break;
2159 case ObjectLiteral::Property::COMPUTED: // fall through
2160 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002161 frame_->Push(r0); // dup the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 Load(key);
2163 Load(value);
2164 __ CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002165 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002166 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 break;
2168 }
2169 case ObjectLiteral::Property::SETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002170 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002172 __ mov(r0, Operand(Smi::FromInt(1)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002173 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 Load(value);
2175 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002176 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 break;
2178 }
2179 case ObjectLiteral::Property::GETTER: {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002180 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 Load(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002182 __ mov(r0, Operand(Smi::FromInt(0)));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002183 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 Load(value);
2185 __ CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002186 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 break;
2188 }
2189 }
2190 }
2191}
2192
2193
ager@chromium.org7c537e22008-10-16 08:43:32 +00002194void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002196
2197 // Call runtime to create the array literal.
2198 __ mov(r0, Operand(node->literals()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002199 frame_->Push(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002200 // Load the function of this frame.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002201 __ ldr(r0, frame_->Function());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002202 __ ldr(r0, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002203 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002204 __ CallRuntime(Runtime::kCreateArrayLiteral, 2);
2205
2206 // Push the resulting array literal on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002207 frame_->Push(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002208
2209 // Generate code to set the elements in the array that are not
2210 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 for (int i = 0; i < node->values()->length(); i++) {
2212 Expression* value = node->values()->at(i);
2213
2214 // If value is literal the property value is already
2215 // set in the boilerplate object.
2216 if (value->AsLiteral() == NULL) {
2217 // The property must be set by generated code.
2218 Load(value);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002219 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220
2221 // Fetch the object literal
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002222 __ ldr(r1, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 // Get the elements array.
2224 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2225
2226 // Write to the indexed properties array.
2227 int offset = i * kPointerSize + Array::kHeaderSize;
2228 __ str(r0, FieldMemOperand(r1, offset));
2229
2230 // Update the write barrier for the array address.
2231 __ mov(r3, Operand(offset));
2232 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 }
2234 }
2235}
2236
2237
ager@chromium.org7c537e22008-10-16 08:43:32 +00002238void CodeGenerator::VisitAssignment(Assignment* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002240 CodeForStatement(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002241
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 Reference target(this, node->target());
2243 if (target.is_illegal()) return;
2244
2245 if (node->op() == Token::ASSIGN ||
2246 node->op() == Token::INIT_VAR ||
2247 node->op() == Token::INIT_CONST) {
2248 Load(node->value());
2249
2250 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002251 target.GetValue(NOT_INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 Literal* literal = node->value()->AsLiteral();
2253 if (literal != NULL && literal->handle()->IsSmi()) {
2254 SmiOperation(node->binary_op(), literal->handle(), false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002255 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 } else {
2258 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002259 GenericBinaryOperation(node->binary_op());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002260 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 }
2262 }
2263
2264 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2265 if (var != NULL &&
2266 (var->mode() == Variable::CONST) &&
2267 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2268 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002271 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 if (node->op() == Token::INIT_CONST) {
2273 // Dynamic constant initializations must use the function context
2274 // and initialize the actual constant declared. Dynamic variable
2275 // initializations are simply assignments and use SetValue.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002276 target.SetValue(CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002278 target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 }
2280 }
2281}
2282
2283
ager@chromium.org7c537e22008-10-16 08:43:32 +00002284void CodeGenerator::VisitThrow(Throw* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 Comment cmnt(masm_, "[ Throw");
2286
2287 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002288 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 __ CallRuntime(Runtime::kThrow, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002290 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291}
2292
2293
ager@chromium.org7c537e22008-10-16 08:43:32 +00002294void CodeGenerator::VisitProperty(Property* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002296
ager@chromium.org7c537e22008-10-16 08:43:32 +00002297 Reference property(this, node);
2298 property.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299}
2300
2301
ager@chromium.org7c537e22008-10-16 08:43:32 +00002302void CodeGenerator::VisitCall(Call* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 Comment cmnt(masm_, "[ Call");
2304
2305 ZoneList<Expression*>* args = node->arguments();
2306
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002307 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 // Standard function call.
2309
2310 // Check if the function is a variable or a property.
2311 Expression* function = node->expression();
2312 Variable* var = function->AsVariableProxy()->AsVariable();
2313 Property* property = function->AsProperty();
2314
2315 // ------------------------------------------------------------------------
2316 // Fast-case: Use inline caching.
2317 // ---
2318 // According to ECMA-262, section 11.2.3, page 44, the function to call
2319 // must be resolved after the arguments have been evaluated. The IC code
2320 // automatically handles this by loading the arguments before the function
2321 // is resolved in cache misses (this also holds for megamorphic calls).
2322 // ------------------------------------------------------------------------
2323
2324 if (var != NULL && !var->is_this() && var->is_global()) {
2325 // ----------------------------------
2326 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2327 // ----------------------------------
2328
2329 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002330 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002331 frame_->Push(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002332
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002333 // Pass the global object as the receiver and let the IC stub
2334 // patch the stack to use the global proxy as 'this' in the
2335 // invoked function.
2336 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337
2338 // Load the arguments.
2339 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340
2341 // Setup the receiver register and call the IC initialization code.
2342 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002343 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002344 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002345 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002347 frame_->Pop();
2348 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349
2350 } else if (var != NULL && var->slot() != NULL &&
2351 var->slot()->type() == Slot::LOOKUP) {
2352 // ----------------------------------
2353 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2354 // ----------------------------------
2355
2356 // Load the function
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002357 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002358 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002359 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2361 // r0: slot value; r1: receiver
2362
2363 // Load the receiver.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002364 frame_->Push(r0); // function
2365 frame_->Push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366
2367 // Call the function.
2368 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002369 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370
2371 } else if (property != NULL) {
2372 // Check if the key is a literal string.
2373 Literal* literal = property->key()->AsLiteral();
2374
2375 if (literal != NULL && literal->handle()->IsSymbol()) {
2376 // ------------------------------------------------------------------
2377 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2378 // ------------------------------------------------------------------
2379
2380 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002381 __ mov(r0, Operand(literal->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002382 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 Load(property->obj());
2384
2385 // Load the arguments.
2386 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
2388 // Set the receiver register and call the IC initialization code.
2389 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002390 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002391 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002392 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393
2394 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002395 frame_->Pop();
mads.s.ager31e71382008-08-13 09:32:07 +00002396
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002397 frame_->Push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398
2399 } else {
2400 // -------------------------------------------
2401 // JavaScript example: 'array[index](1, 2, 3)'
2402 // -------------------------------------------
2403
2404 // Load the function to call from the property through a reference.
2405 Reference ref(this, property);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002406 ref.GetValue(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407
2408 // Pass receiver to called function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002409 __ ldr(r0, frame_->Element(ref.size()));
2410 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 // Call the function.
2412 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002413 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 }
2415
2416 } else {
2417 // ----------------------------------
2418 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2419 // ----------------------------------
2420
2421 // Load the function.
2422 Load(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002423
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002424 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002425 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 // Call the function.
2428 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002429 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430 }
2431}
2432
2433
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002434void CodeGenerator::VisitCallEval(CallEval* node) {
2435 Comment cmnt(masm_, "[ CallEval");
2436
2437 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
2438 // the function we need to call and the receiver of the call.
2439 // Then we call the resolved function using the given arguments.
2440
2441 ZoneList<Expression*>* args = node->arguments();
2442 Expression* function = node->expression();
2443
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002444 CodeForStatement(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002445
2446 // Prepare stack for call to resolved function.
2447 Load(function);
2448 __ mov(r2, Operand(Factory::undefined_value()));
2449 __ push(r2); // Slot for receiver
2450 for (int i = 0; i < args->length(); i++) {
2451 Load(args->at(i));
2452 }
2453
2454 // Prepare stack for call to ResolvePossiblyDirectEval.
2455 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize + kPointerSize));
2456 __ push(r1);
2457 if (args->length() > 0) {
2458 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize));
2459 __ push(r1);
2460 } else {
2461 __ push(r2);
2462 }
2463
2464 // Resolve the call.
2465 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
2466
2467 // Touch up stack with the right values for the function and the receiver.
2468 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
2469 __ str(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
2470 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
2471 __ str(r1, MemOperand(sp, args->length() * kPointerSize));
2472
2473 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002474 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002475
2476 CallFunctionStub call_function(args->length());
2477 __ CallStub(&call_function);
2478
2479 __ ldr(cp, frame_->Context());
2480 // Remove the function from the stack.
2481 frame_->Pop();
2482 frame_->Push(r0);
2483}
2484
2485
ager@chromium.org7c537e22008-10-16 08:43:32 +00002486void CodeGenerator::VisitCallNew(CallNew* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 Comment cmnt(masm_, "[ CallNew");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002488 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489
2490 // According to ECMA-262, section 11.2.2, page 44, the function
2491 // expression in new calls must be evaluated before the
2492 // arguments. This is different from ordinary calls, where the
2493 // actual function to call is resolved after the arguments have been
2494 // evaluated.
2495
2496 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002497 // receiver. There is no need to use the global proxy here because
2498 // it will always be replaced with a newly allocated object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002500 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501
2502 // Push the arguments ("left-to-right") on the stack.
2503 ZoneList<Expression*>* args = node->arguments();
2504 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2505
mads.s.ager31e71382008-08-13 09:32:07 +00002506 // r0: the number of arguments.
2507 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002509 // Load the function into r1 as per calling convention.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002510 __ ldr(r1, frame_->Element(args->length() + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512 // Call the construct call builtin that handles allocation and
2513 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002514 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002516 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002517
2518 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002519 __ str(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520}
2521
2522
ager@chromium.org7c537e22008-10-16 08:43:32 +00002523void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 ASSERT(args->length() == 1);
2525 Label leave;
2526 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002527 frame_->Pop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00002528 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002529 __ tst(r0, Operand(kSmiTagMask));
2530 __ b(eq, &leave);
2531 // It is a heap object - get map.
2532 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2533 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002534 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 __ cmp(r1, Operand(JS_VALUE_TYPE));
2536 __ b(ne, &leave);
2537 // Load the value.
2538 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2539 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002540 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541}
2542
2543
ager@chromium.org7c537e22008-10-16 08:43:32 +00002544void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 ASSERT(args->length() == 2);
2546 Label leave;
2547 Load(args->at(0)); // Load the object.
2548 Load(args->at(1)); // Load the value.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002549 frame_->Pop(r0); // r0 contains value
2550 frame_->Pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551 // if (object->IsSmi()) return object.
2552 __ tst(r1, Operand(kSmiTagMask));
2553 __ b(eq, &leave);
2554 // It is a heap object - get map.
2555 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2556 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2557 // if (!object->IsJSValue()) return object.
2558 __ cmp(r2, Operand(JS_VALUE_TYPE));
2559 __ b(ne, &leave);
2560 // Store the value.
2561 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2562 // Update the write barrier.
2563 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2564 __ RecordWrite(r1, r2, r3);
2565 // Leave.
2566 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002567 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568}
2569
2570
ager@chromium.org7c537e22008-10-16 08:43:32 +00002571void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 ASSERT(args->length() == 1);
2573 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002574 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002575 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576 cc_reg_ = eq;
2577}
2578
2579
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002580void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
2581 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
2582 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002583#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002584 if (ShouldGenerateLog(args->at(0))) {
2585 Load(args->at(1));
2586 Load(args->at(2));
2587 __ CallRuntime(Runtime::kLog, 2);
2588 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002589#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002590 __ mov(r0, Operand(Factory::undefined_value()));
2591 frame_->Push(r0);
2592}
2593
2594
ager@chromium.org7c537e22008-10-16 08:43:32 +00002595void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002596 ASSERT(args->length() == 1);
2597 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002598 frame_->Pop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002599 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2600 cc_reg_ = eq;
2601}
2602
2603
kasper.lund7276f142008-07-30 08:49:36 +00002604// This should generate code that performs a charCodeAt() call or returns
2605// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2606// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002607void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002608 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002609 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002610 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002611}
2612
2613
ager@chromium.org7c537e22008-10-16 08:43:32 +00002614void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002615 ASSERT(args->length() == 1);
2616 Load(args->at(0));
2617 Label answer;
2618 // We need the CC bits to come out as not_equal in the case where the
2619 // object is a smi. This can't be done with the usual test opcode so
2620 // we use XOR to get the right CC bits.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002621 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002622 __ and_(r1, r0, Operand(kSmiTagMask));
2623 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2624 __ b(ne, &answer);
2625 // It is a heap object - get the map.
2626 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2627 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2628 // Check if the object is a JS array or not.
2629 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2630 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 cc_reg_ = eq;
2632}
2633
2634
ager@chromium.org7c537e22008-10-16 08:43:32 +00002635void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 ASSERT(args->length() == 0);
2637
mads.s.ager31e71382008-08-13 09:32:07 +00002638 // Seed the result with the formal parameters count, which will be used
2639 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2641
2642 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002643 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002645 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646}
2647
2648
ager@chromium.org7c537e22008-10-16 08:43:32 +00002649void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 ASSERT(args->length() == 1);
2651
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002652 // Satisfy contract with ArgumentsAccessStub:
2653 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002655 frame_->Pop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002656 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657
2658 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002659 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002661 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662}
2663
2664
ager@chromium.org7c537e22008-10-16 08:43:32 +00002665void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002666 ASSERT(args->length() == 2);
2667
2668 // Load the two objects into registers and perform the comparison.
2669 Load(args->at(0));
2670 Load(args->at(1));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002671 frame_->Pop(r0);
2672 frame_->Pop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002673 __ cmp(r0, Operand(r1));
2674 cc_reg_ = eq;
2675}
2676
2677
ager@chromium.org7c537e22008-10-16 08:43:32 +00002678void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002679 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680
2681 ZoneList<Expression*>* args = node->arguments();
2682 Comment cmnt(masm_, "[ CallRuntime");
2683 Runtime::Function* function = node->function();
2684
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002686 // Push the arguments ("left-to-right").
2687 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2688
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 // Call the C runtime function.
2690 __ CallRuntime(function, args->length());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002691 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002694 // Prepare stack for calling JS runtime function.
2695 __ mov(r0, Operand(node->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002696 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002697 // Push the builtins object found in the current global object.
2698 __ ldr(r1, GlobalObject());
2699 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002700 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002701
2702 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2703
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002706 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002707 __ ldr(cp, frame_->Context());
2708 frame_->Pop();
2709 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 }
2711}
2712
2713
ager@chromium.org7c537e22008-10-16 08:43:32 +00002714void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 Comment cmnt(masm_, "[ UnaryOperation");
2716
2717 Token::Value op = node->op();
2718
2719 if (op == Token::NOT) {
2720 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002721 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002722 false_target(),
2723 true_target(),
2724 true);
2725 cc_reg_ = NegateCondition(cc_reg_);
2726
2727 } else if (op == Token::DELETE) {
2728 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002729 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 if (property != NULL) {
2731 Load(property->obj());
2732 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002733 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002734 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735
mads.s.ager31e71382008-08-13 09:32:07 +00002736 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 Slot* slot = variable->slot();
2738 if (variable->is_global()) {
2739 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002740 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002741 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002742 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002743 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744
2745 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2746 // lookup the context holding the named variable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002747 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002748 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002749 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 __ CallRuntime(Runtime::kLookupContext, 2);
2751 // r0: context
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002752 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002753 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002754 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002755 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002756 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757
mads.s.ager31e71382008-08-13 09:32:07 +00002758 } else {
2759 // Default: Result of deleting non-global, not dynamically
2760 // introduced variables is false.
2761 __ mov(r0, Operand(Factory::false_value()));
2762 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763
2764 } else {
2765 // Default: Result of deleting expressions is true.
2766 Load(node->expression()); // may have side-effects
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002767 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 __ mov(r0, Operand(Factory::true_value()));
2769 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002770 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771
2772 } else if (op == Token::TYPEOF) {
2773 // Special case for loading the typeof expression; see comment on
2774 // LoadTypeofExpression().
2775 LoadTypeofExpression(node->expression());
2776 __ CallRuntime(Runtime::kTypeof, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002777 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778
2779 } else {
2780 Load(node->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002781 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782 switch (op) {
2783 case Token::NOT:
2784 case Token::DELETE:
2785 case Token::TYPEOF:
2786 UNREACHABLE(); // handled above
2787 break;
2788
2789 case Token::SUB: {
2790 UnarySubStub stub;
2791 __ CallStub(&stub);
2792 break;
2793 }
2794
2795 case Token::BIT_NOT: {
2796 // smi check
2797 Label smi_label;
2798 Label continue_label;
2799 __ tst(r0, Operand(kSmiTagMask));
2800 __ b(eq, &smi_label);
2801
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002802 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002803 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002804 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805
2806 __ b(&continue_label);
2807 __ bind(&smi_label);
2808 __ mvn(r0, Operand(r0));
2809 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2810 __ bind(&continue_label);
2811 break;
2812 }
2813
2814 case Token::VOID:
2815 // since the stack top is cached in r0, popping and then
2816 // pushing a value can be done by just writing to r0.
2817 __ mov(r0, Operand(Factory::undefined_value()));
2818 break;
2819
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002820 case Token::ADD: {
2821 // Smi check.
2822 Label continue_label;
2823 __ tst(r0, Operand(kSmiTagMask));
2824 __ b(eq, &continue_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002825 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002826 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002827 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002828 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002830 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 default:
2832 UNREACHABLE();
2833 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002834 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 }
2836}
2837
2838
ager@chromium.org7c537e22008-10-16 08:43:32 +00002839void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840 Comment cmnt(masm_, "[ CountOperation");
2841
2842 bool is_postfix = node->is_postfix();
2843 bool is_increment = node->op() == Token::INC;
2844
2845 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2846 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2847
2848 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002849 if (is_postfix) {
2850 __ mov(r0, Operand(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002851 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002852 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002853
2854 { Reference target(this, node->expression());
2855 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002856 target.GetValue(NOT_INSIDE_TYPEOF);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002857 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858
2859 Label slow, exit;
2860
2861 // Load the value (1) into register r1.
2862 __ mov(r1, Operand(Smi::FromInt(1)));
2863
2864 // Check for smi operand.
2865 __ tst(r0, Operand(kSmiTagMask));
2866 __ b(ne, &slow);
2867
2868 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002869 if (is_postfix) {
2870 __ str(r0, frame_->Element(target.size()));
2871 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872
2873 // Perform optimistic increment/decrement.
2874 if (is_increment) {
2875 __ add(r0, r0, Operand(r1), SetCC);
2876 } else {
2877 __ sub(r0, r0, Operand(r1), SetCC);
2878 }
2879
2880 // If the increment/decrement didn't overflow, we're done.
2881 __ b(vc, &exit);
2882
2883 // Revert optimistic increment/decrement.
2884 if (is_increment) {
2885 __ sub(r0, r0, Operand(r1));
2886 } else {
2887 __ add(r0, r0, Operand(r1));
2888 }
2889
2890 // Slow case: Convert to number.
2891 __ bind(&slow);
2892
2893 // Postfix: Convert the operand to a number and store it as the result.
2894 if (is_postfix) {
2895 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
2896 __ CallStub(&stub);
2897 // Store to result (on the stack).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002898 __ str(r0, frame_->Element(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899 }
2900
2901 // Compute the new value by calling the right JavaScript native.
2902 if (is_increment) {
2903 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
2904 __ CallStub(&stub);
2905 } else {
2906 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
2907 __ CallStub(&stub);
2908 }
2909
2910 // Store the new value in the target if not const.
2911 __ bind(&exit);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002912 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002913 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914 }
2915
2916 // Postfix: Discard the new value and use the old.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002917 if (is_postfix) frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918}
2919
2920
ager@chromium.org7c537e22008-10-16 08:43:32 +00002921void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 Comment cmnt(masm_, "[ BinaryOperation");
2923 Token::Value op = node->op();
2924
2925 // According to ECMA-262 section 11.11, page 58, the binary logical
2926 // operators must yield the result of one of the two expressions
2927 // before any ToBoolean() conversions. This means that the value
2928 // produced by a && or || operator is not necessarily a boolean.
2929
2930 // NOTE: If the left hand side produces a materialized value (not in
2931 // the CC register), we force the right hand side to do the
2932 // same. This is necessary because we may have to branch to the exit
2933 // after evaluating the left hand side (due to the shortcut
2934 // semantics), but the compiler must (statically) know if the result
2935 // of compiling the binary operation is materialized or not.
2936
2937 if (op == Token::AND) {
2938 Label is_true;
2939 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002940 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 &is_true,
2942 false_target(),
2943 false);
2944 if (has_cc()) {
2945 Branch(false, false_target());
2946
2947 // Evaluate right side expression.
2948 __ bind(&is_true);
2949 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002950 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 true_target(),
2952 false_target(),
2953 false);
2954
2955 } else {
2956 Label pop_and_continue, exit;
2957
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002958 __ ldr(r0, frame_->Top()); // dup the stack top
2959 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960 // Avoid popping the result if it converts to 'false' using the
2961 // standard ToBoolean() conversion as described in ECMA-262,
2962 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002963 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 Branch(false, &exit);
2965
2966 // Pop the result of evaluating the first part.
2967 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002968 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969
2970 // Evaluate right side expression.
2971 __ bind(&is_true);
2972 Load(node->right());
2973
2974 // Exit (always with a materialized value).
2975 __ bind(&exit);
2976 }
2977
2978 } else if (op == Token::OR) {
2979 Label is_false;
2980 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002981 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 true_target(),
2983 &is_false,
2984 false);
2985 if (has_cc()) {
2986 Branch(true, true_target());
2987
2988 // Evaluate right side expression.
2989 __ bind(&is_false);
2990 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002991 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 true_target(),
2993 false_target(),
2994 false);
2995
2996 } else {
2997 Label pop_and_continue, exit;
2998
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002999 __ ldr(r0, frame_->Top());
3000 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001 // Avoid popping the result if it converts to 'true' using the
3002 // standard ToBoolean() conversion as described in ECMA-262,
3003 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003004 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005 Branch(true, &exit);
3006
3007 // Pop the result of evaluating the first part.
3008 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003009 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003010
3011 // Evaluate right side expression.
3012 __ bind(&is_false);
3013 Load(node->right());
3014
3015 // Exit (always with a materialized value).
3016 __ bind(&exit);
3017 }
3018
3019 } else {
3020 // Optimize for the case where (at least) one of the expressions
3021 // is a literal small integer.
3022 Literal* lliteral = node->left()->AsLiteral();
3023 Literal* rliteral = node->right()->AsLiteral();
3024
3025 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
3026 Load(node->left());
3027 SmiOperation(node->op(), rliteral->handle(), false);
3028
3029 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
3030 Load(node->right());
3031 SmiOperation(node->op(), lliteral->handle(), true);
3032
3033 } else {
3034 Load(node->left());
3035 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003036 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003038 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039 }
3040}
3041
3042
ager@chromium.org7c537e22008-10-16 08:43:32 +00003043void CodeGenerator::VisitThisFunction(ThisFunction* node) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003044 __ ldr(r0, frame_->Function());
3045 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046}
3047
3048
ager@chromium.org7c537e22008-10-16 08:43:32 +00003049void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 Comment cmnt(masm_, "[ CompareOperation");
3051
3052 // Get the expressions from the node.
3053 Expression* left = node->left();
3054 Expression* right = node->right();
3055 Token::Value op = node->op();
3056
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003057 // To make null checks efficient, we check if either left or right is the
3058 // literal 'null'. If so, we optimize the code by inlining a null check
3059 // instead of calling the (very) general runtime routine for checking
3060 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003062 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003063 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003064 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003065 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3066 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067 if (left_is_null || right_is_null) {
3068 Load(left_is_null ? right : left);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003069 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070 __ cmp(r0, Operand(Factory::null_value()));
3071
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003072 // The 'null' value is only equal to 'undefined' if using non-strict
3073 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074 if (op != Token::EQ_STRICT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003075 __ b(eq, true_target());
3076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077 __ cmp(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003078 __ b(eq, true_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003081 __ b(eq, false_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003083 // It can be an undetectable object.
3084 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3085 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3086 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3087 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 }
3089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003090 cc_reg_ = eq;
3091 return;
3092 }
3093 }
3094
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003095 // To make typeof testing for natives implemented in JavaScript really
3096 // efficient, we generate special code for expressions of the form:
3097 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 UnaryOperation* operation = left->AsUnaryOperation();
3099 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3100 (operation != NULL && operation->op() == Token::TYPEOF) &&
3101 (right->AsLiteral() != NULL &&
3102 right->AsLiteral()->handle()->IsString())) {
3103 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3104
mads.s.ager31e71382008-08-13 09:32:07 +00003105 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106 LoadTypeofExpression(operation->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003107 frame_->Pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003108
3109 if (check->Equals(Heap::number_symbol())) {
3110 __ tst(r1, Operand(kSmiTagMask));
3111 __ b(eq, true_target());
3112 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3113 __ cmp(r1, Operand(Factory::heap_number_map()));
3114 cc_reg_ = eq;
3115
3116 } else if (check->Equals(Heap::string_symbol())) {
3117 __ tst(r1, Operand(kSmiTagMask));
3118 __ b(eq, false_target());
3119
3120 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3121
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003122 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3124 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3125 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3126 __ b(eq, false_target());
3127
3128 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3129 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3130 cc_reg_ = lt;
3131
3132 } else if (check->Equals(Heap::boolean_symbol())) {
3133 __ cmp(r1, Operand(Factory::true_value()));
3134 __ b(eq, true_target());
3135 __ cmp(r1, Operand(Factory::false_value()));
3136 cc_reg_ = eq;
3137
3138 } else if (check->Equals(Heap::undefined_symbol())) {
3139 __ cmp(r1, Operand(Factory::undefined_value()));
3140 __ b(eq, true_target());
3141
3142 __ tst(r1, Operand(kSmiTagMask));
3143 __ b(eq, false_target());
3144
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003145 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3147 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3148 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3149 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3150
3151 cc_reg_ = eq;
3152
3153 } else if (check->Equals(Heap::function_symbol())) {
3154 __ tst(r1, Operand(kSmiTagMask));
3155 __ b(eq, false_target());
3156 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3157 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3158 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3159 cc_reg_ = eq;
3160
3161 } else if (check->Equals(Heap::object_symbol())) {
3162 __ tst(r1, Operand(kSmiTagMask));
3163 __ b(eq, false_target());
3164
3165 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3166 __ cmp(r1, Operand(Factory::null_value()));
3167 __ b(eq, true_target());
3168
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003169 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003170 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3171 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3172 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3173 __ b(eq, false_target());
3174
3175 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3176 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3177 __ b(lt, false_target());
3178 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3179 cc_reg_ = le;
3180
3181 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003182 // Uncommon case: typeof testing against a string literal that is
3183 // never returned from the typeof operator.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184 __ b(false_target());
3185 }
3186 return;
3187 }
3188
3189 Load(left);
3190 Load(right);
3191 switch (op) {
3192 case Token::EQ:
3193 Comparison(eq, false);
3194 break;
3195
3196 case Token::LT:
3197 Comparison(lt);
3198 break;
3199
3200 case Token::GT:
3201 Comparison(gt);
3202 break;
3203
3204 case Token::LTE:
3205 Comparison(le);
3206 break;
3207
3208 case Token::GTE:
3209 Comparison(ge);
3210 break;
3211
3212 case Token::EQ_STRICT:
3213 Comparison(eq, true);
3214 break;
3215
3216 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003217 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003218 __ InvokeBuiltin(Builtins::IN, CALL_JS);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003219 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 break;
3221
3222 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003223 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003224 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003225 __ tst(r0, Operand(r0));
3226 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227 break;
3228
3229 default:
3230 UNREACHABLE();
3231 }
3232}
3233
3234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003236#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237
ager@chromium.org7c537e22008-10-16 08:43:32 +00003238Handle<String> Reference::GetName() {
3239 ASSERT(type_ == NAMED);
3240 Property* property = expression_->AsProperty();
3241 if (property == NULL) {
3242 // Global variable reference treated as a named property reference.
3243 VariableProxy* proxy = expression_->AsVariableProxy();
3244 ASSERT(proxy->AsVariable() != NULL);
3245 ASSERT(proxy->AsVariable()->is_global());
3246 return proxy->name();
3247 } else {
3248 Literal* raw_name = property->key()->AsLiteral();
3249 ASSERT(raw_name != NULL);
3250 return Handle<String>(String::cast(*raw_name->handle()));
3251 }
3252}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003253
ager@chromium.org7c537e22008-10-16 08:43:32 +00003254
3255void Reference::GetValue(TypeofState typeof_state) {
3256 ASSERT(!is_illegal());
3257 ASSERT(!cgen_->has_cc());
3258 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003259 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260 Property* property = expression_->AsProperty();
3261 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003262 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003263 }
3264
3265 switch (type_) {
3266 case SLOT: {
3267 Comment cmnt(masm, "[ Load from Slot");
3268 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3269 ASSERT(slot != NULL);
3270 cgen_->LoadFromSlot(slot, typeof_state);
3271 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003272 }
3273
ager@chromium.org7c537e22008-10-16 08:43:32 +00003274 case NAMED: {
3275 // TODO(1241834): Make sure that this it is safe to ignore the
3276 // distinction between expressions in a typeof and not in a typeof. If
3277 // there is a chance that reference errors can be thrown below, we
3278 // must distinguish between the two kinds of loads (typeof expression
3279 // loads must not throw a reference error).
3280 Comment cmnt(masm, "[ Load from named Property");
3281 // Setup the name register.
3282 Handle<String> name(GetName());
3283 __ mov(r2, Operand(name));
3284 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3285
3286 Variable* var = expression_->AsVariableProxy()->AsVariable();
3287 if (var != NULL) {
3288 ASSERT(var->is_global());
3289 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3290 } else {
3291 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003292 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003293 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003294 break;
3295 }
3296
3297 case KEYED: {
3298 // TODO(1241834): Make sure that this it is safe to ignore the
3299 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003300
3301 // TODO(181): Implement inlined version of array indexing once
3302 // loop nesting is properly tracked on ARM.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003303 Comment cmnt(masm, "[ Load from keyed Property");
3304 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003305 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
3306
3307 Variable* var = expression_->AsVariableProxy()->AsVariable();
3308 if (var != NULL) {
3309 ASSERT(var->is_global());
3310 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3311 } else {
3312 __ Call(ic, RelocInfo::CODE_TARGET);
3313 }
3314 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003315 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003316 }
3317
3318 default:
3319 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003320 }
3321}
3322
3323
ager@chromium.org7c537e22008-10-16 08:43:32 +00003324void Reference::SetValue(InitState init_state) {
3325 ASSERT(!is_illegal());
3326 ASSERT(!cgen_->has_cc());
3327 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003328 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003329 Property* property = expression_->AsProperty();
3330 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003331 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003332 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003333
ager@chromium.org7c537e22008-10-16 08:43:32 +00003334 switch (type_) {
3335 case SLOT: {
3336 Comment cmnt(masm, "[ Store to Slot");
3337 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3338 ASSERT(slot != NULL);
3339 if (slot->type() == Slot::LOOKUP) {
3340 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003341
ager@chromium.org7c537e22008-10-16 08:43:32 +00003342 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003343 frame->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003344 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003345 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003346
ager@chromium.org7c537e22008-10-16 08:43:32 +00003347 if (init_state == CONST_INIT) {
3348 // Same as the case for a normal store, but ignores attribute
3349 // (e.g. READ_ONLY) of context slot so that we can initialize
3350 // const properties (introduced via eval("const foo = (some
3351 // expr);")). Also, uses the current function context instead of
3352 // the top context.
3353 //
3354 // Note that we must declare the foo upon entry of eval(), via a
3355 // context slot declaration, but we cannot initialize it at the
3356 // same time, because the const declaration may be at the end of
3357 // the eval code (sigh...) and the const variable may have been
3358 // used before (where its value is 'undefined'). Thus, we can only
3359 // do the initialization when we actually encounter the expression
3360 // and when the expression operands are defined and valid, and
3361 // thus we need the split into 2 operations: declaration of the
3362 // context slot followed by initialization.
3363 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3364 } else {
3365 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3366 }
3367 // Storing a variable must keep the (new) value on the expression
3368 // stack. This is necessary for compiling assignment expressions.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003369 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003370
ager@chromium.org7c537e22008-10-16 08:43:32 +00003371 } else {
3372 ASSERT(slot->var()->mode() != Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003373
ager@chromium.org7c537e22008-10-16 08:43:32 +00003374 Label exit;
3375 if (init_state == CONST_INIT) {
3376 ASSERT(slot->var()->mode() == Variable::CONST);
3377 // Only the first const initialization must be executed (the slot
3378 // still contains 'the hole' value). When the assignment is
3379 // executed, the code is identical to a normal store (see below).
3380 Comment cmnt(masm, "[ Init const");
3381 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3382 __ cmp(r2, Operand(Factory::the_hole_value()));
3383 __ b(ne, &exit);
3384 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003385
ager@chromium.org7c537e22008-10-16 08:43:32 +00003386 // We must execute the store. Storing a variable must keep the
3387 // (new) value on the stack. This is necessary for compiling
3388 // assignment expressions.
3389 //
3390 // Note: We will reach here even with slot->var()->mode() ==
3391 // Variable::CONST because of const declarations which will
3392 // initialize consts to 'the hole' value and by doing so, end up
3393 // calling this code. r2 may be loaded with context; used below in
3394 // RecordWrite.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003395 frame->Pop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003396 __ str(r0, cgen_->SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003397 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003398 if (slot->type() == Slot::CONTEXT) {
3399 // Skip write barrier if the written value is a smi.
3400 __ tst(r0, Operand(kSmiTagMask));
3401 __ b(eq, &exit);
3402 // r2 is loaded with context when calling SlotOperand above.
3403 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3404 __ mov(r3, Operand(offset));
3405 __ RecordWrite(r2, r3, r1);
3406 }
3407 // If we definitely did not jump over the assignment, we do not need
3408 // to bind the exit label. Doing so can defeat peephole
3409 // optimization.
3410 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3411 __ bind(&exit);
3412 }
3413 }
3414 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003415 }
3416
ager@chromium.org7c537e22008-10-16 08:43:32 +00003417 case NAMED: {
3418 Comment cmnt(masm, "[ Store to named Property");
3419 // Call the appropriate IC code.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003420 frame->Pop(r0); // value
ager@chromium.org7c537e22008-10-16 08:43:32 +00003421 // Setup the name register.
3422 Handle<String> name(GetName());
3423 __ mov(r2, Operand(name));
3424 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3425 __ Call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003426 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003427 break;
3428 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003429
ager@chromium.org7c537e22008-10-16 08:43:32 +00003430 case KEYED: {
3431 Comment cmnt(masm, "[ Store to keyed Property");
3432 Property* property = expression_->AsProperty();
3433 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003434 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003435
3436 // Call IC code.
3437 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3438 // TODO(1222589): Make the IC grab the values from the stack.
3439 frame->Pop(r0); // value
3440 __ Call(ic, RelocInfo::CODE_TARGET);
3441 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003442 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003443 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003444
3445 default:
3446 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003447 }
3448}
3449
3450
3451void GetPropertyStub::Generate(MacroAssembler* masm) {
3452 // sp[0]: key
3453 // sp[1]: receiver
3454 Label slow, fast;
3455 // Get the key and receiver object from the stack.
3456 __ ldm(ia, sp, r0.bit() | r1.bit());
3457 // Check that the key is a smi.
3458 __ tst(r0, Operand(kSmiTagMask));
3459 __ b(ne, &slow);
3460 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3461 // Check that the object isn't a smi.
3462 __ tst(r1, Operand(kSmiTagMask));
3463 __ b(eq, &slow);
3464
3465 // Check that the object is some kind of JS object EXCEPT JS Value type.
3466 // In the case that the object is a value-wrapper object,
3467 // we enter the runtime system to make sure that indexing into string
3468 // objects work as intended.
3469 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3470 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3471 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3472 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3473 __ b(lt, &slow);
3474
3475 // Get the elements array of the object.
3476 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3477 // Check that the object is in fast mode (not dictionary).
3478 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3479 __ cmp(r3, Operand(Factory::hash_table_map()));
3480 __ b(eq, &slow);
3481 // Check that the key (index) is within bounds.
3482 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3483 __ cmp(r0, Operand(r3));
3484 __ b(lo, &fast);
3485
3486 // Slow case: Push extra copies of the arguments (2).
3487 __ bind(&slow);
3488 __ ldm(ia, sp, r0.bit() | r1.bit());
3489 __ stm(db_w, sp, r0.bit() | r1.bit());
3490 // Do tail-call to runtime routine.
3491 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3492
3493 // Fast case: Do the load.
3494 __ bind(&fast);
3495 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3496 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3497 __ cmp(r0, Operand(Factory::the_hole_value()));
3498 // In case the loaded value is the_hole we have to consult GetProperty
3499 // to ensure the prototype chain is searched.
3500 __ b(eq, &slow);
3501
3502 __ StubReturn(1);
3503}
3504
3505
3506void SetPropertyStub::Generate(MacroAssembler* masm) {
3507 // r0 : value
3508 // sp[0] : key
3509 // sp[1] : receiver
3510
3511 Label slow, fast, array, extra, exit;
3512 // Get the key and the object from the stack.
3513 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3514 // Check that the key is a smi.
3515 __ tst(r1, Operand(kSmiTagMask));
3516 __ b(ne, &slow);
3517 // Check that the object isn't a smi.
3518 __ tst(r3, Operand(kSmiTagMask));
3519 __ b(eq, &slow);
3520 // Get the type of the object from its map.
3521 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3522 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3523 // Check if the object is a JS array or not.
3524 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3525 __ b(eq, &array);
3526 // Check that the object is some kind of JS object.
3527 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3528 __ b(lt, &slow);
3529
3530
3531 // Object case: Check key against length in the elements array.
3532 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3533 // Check that the object is in fast mode (not dictionary).
3534 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3535 __ cmp(r2, Operand(Factory::hash_table_map()));
3536 __ b(eq, &slow);
3537 // Untag the key (for checking against untagged length in the fixed array).
3538 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3539 // Compute address to store into and check array bounds.
3540 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3541 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3542 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3543 __ cmp(r1, Operand(ip));
3544 __ b(lo, &fast);
3545
3546
3547 // Slow case: Push extra copies of the arguments (3).
3548 __ bind(&slow);
3549 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3550 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3551 // Do tail-call to runtime routine.
3552 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3553
3554
3555 // Extra capacity case: Check if there is extra capacity to
3556 // perform the store and update the length. Used for adding one
3557 // element to the array by writing to array[array.length].
3558 // r0 == value, r1 == key, r2 == elements, r3 == object
3559 __ bind(&extra);
3560 __ b(ne, &slow); // do not leave holes in the array
3561 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3562 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3563 __ cmp(r1, Operand(ip));
3564 __ b(hs, &slow);
3565 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3566 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3567 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3568 __ mov(r3, Operand(r2));
3569 // NOTE: Computing the address to store into must take the fact
3570 // that the key has been incremented into account.
3571 int displacement = Array::kHeaderSize - kHeapObjectTag -
3572 ((1 << kSmiTagSize) * 2);
3573 __ add(r2, r2, Operand(displacement));
3574 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3575 __ b(&fast);
3576
3577
3578 // Array case: Get the length and the elements array from the JS
3579 // array. Check that the array is in fast mode; if it is the
3580 // length is always a smi.
3581 // r0 == value, r3 == object
3582 __ bind(&array);
3583 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3584 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3585 __ cmp(r1, Operand(Factory::hash_table_map()));
3586 __ b(eq, &slow);
3587
3588 // Check the key against the length in the array, compute the
3589 // address to store into and fall through to fast case.
3590 __ ldr(r1, MemOperand(sp));
3591 // r0 == value, r1 == key, r2 == elements, r3 == object.
3592 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3593 __ cmp(r1, Operand(ip));
3594 __ b(hs, &extra);
3595 __ mov(r3, Operand(r2));
3596 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3597 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3598
3599
3600 // Fast case: Do the store.
3601 // r0 == value, r2 == address to store into, r3 == elements
3602 __ bind(&fast);
3603 __ str(r0, MemOperand(r2));
3604 // Skip write barrier if the written value is a smi.
3605 __ tst(r0, Operand(kSmiTagMask));
3606 __ b(eq, &exit);
3607 // Update write barrier for the elements array address.
3608 __ sub(r1, r2, Operand(r3));
3609 __ RecordWrite(r3, r1, r2);
3610 __ bind(&exit);
3611 __ StubReturn(1);
3612}
3613
3614
3615void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3616 // r1 : x
3617 // r0 : y
3618 // result : r0
3619
3620 switch (op_) {
3621 case Token::ADD: {
3622 Label slow, exit;
3623 // fast path
3624 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3625 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3626 // go slow-path in case of overflow
3627 __ b(vs, &slow);
3628 // go slow-path in case of non-smi operands
3629 ASSERT(kSmiTag == 0); // adjust code below
3630 __ tst(r2, Operand(kSmiTagMask));
3631 __ b(eq, &exit);
3632 // slow path
3633 __ bind(&slow);
3634 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3635 __ push(r1);
3636 __ push(r0);
3637 __ mov(r0, Operand(1)); // set number of arguments
3638 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3639 // done
3640 __ bind(&exit);
3641 break;
3642 }
3643
3644 case Token::SUB: {
3645 Label slow, exit;
3646 // fast path
3647 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3648 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3649 // go slow-path in case of overflow
3650 __ b(vs, &slow);
3651 // go slow-path in case of non-smi operands
3652 ASSERT(kSmiTag == 0); // adjust code below
3653 __ tst(r2, Operand(kSmiTagMask));
3654 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3655 __ b(eq, &exit);
3656 // slow path
3657 __ bind(&slow);
3658 __ push(r1);
3659 __ push(r0);
3660 __ mov(r0, Operand(1)); // set number of arguments
3661 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3662 // done
3663 __ bind(&exit);
3664 break;
3665 }
3666
3667 case Token::MUL: {
3668 Label slow, exit;
3669 // tag check
3670 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3671 ASSERT(kSmiTag == 0); // adjust code below
3672 __ tst(r2, Operand(kSmiTagMask));
3673 __ b(ne, &slow);
3674 // remove tag from one operand (but keep sign), so that result is smi
3675 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3676 // do multiplication
3677 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3678 // go slow on overflows (overflow bit is not set)
3679 __ mov(ip, Operand(r3, ASR, 31));
3680 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3681 __ b(ne, &slow);
3682 // go slow on zero result to handle -0
3683 __ tst(r3, Operand(r3));
3684 __ mov(r0, Operand(r3), LeaveCC, ne);
3685 __ b(ne, &exit);
3686 // slow case
3687 __ bind(&slow);
3688 __ push(r1);
3689 __ push(r0);
3690 __ mov(r0, Operand(1)); // set number of arguments
3691 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3692 // done
3693 __ bind(&exit);
3694 break;
3695 }
3696
3697 case Token::BIT_OR:
3698 case Token::BIT_AND:
3699 case Token::BIT_XOR: {
3700 Label slow, exit;
3701 // tag check
3702 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3703 ASSERT(kSmiTag == 0); // adjust code below
3704 __ tst(r2, Operand(kSmiTagMask));
3705 __ b(ne, &slow);
3706 switch (op_) {
3707 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3708 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3709 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3710 default: UNREACHABLE();
3711 }
3712 __ b(&exit);
3713 __ bind(&slow);
3714 __ push(r1); // restore stack
3715 __ push(r0);
3716 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3717 switch (op_) {
3718 case Token::BIT_OR:
3719 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3720 break;
3721 case Token::BIT_AND:
3722 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3723 break;
3724 case Token::BIT_XOR:
3725 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3726 break;
3727 default:
3728 UNREACHABLE();
3729 }
3730 __ bind(&exit);
3731 break;
3732 }
3733
3734 case Token::SHL:
3735 case Token::SHR:
3736 case Token::SAR: {
3737 Label slow, exit;
3738 // tag check
3739 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3740 ASSERT(kSmiTag == 0); // adjust code below
3741 __ tst(r2, Operand(kSmiTagMask));
3742 __ b(ne, &slow);
3743 // remove tags from operands (but keep sign)
3744 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3745 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3746 // use only the 5 least significant bits of the shift count
3747 __ and_(r2, r2, Operand(0x1f));
3748 // perform operation
3749 switch (op_) {
3750 case Token::SAR:
3751 __ mov(r3, Operand(r3, ASR, r2));
3752 // no checks of result necessary
3753 break;
3754
3755 case Token::SHR:
3756 __ mov(r3, Operand(r3, LSR, r2));
3757 // check that the *unsigned* result fits in a smi
3758 // neither of the two high-order bits can be set:
3759 // - 0x80000000: high bit would be lost when smi tagging
3760 // - 0x40000000: this number would convert to negative when
3761 // smi tagging these two cases can only happen with shifts
3762 // by 0 or 1 when handed a valid smi
3763 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3764 __ b(ne, &slow);
3765 break;
3766
3767 case Token::SHL:
3768 __ mov(r3, Operand(r3, LSL, r2));
3769 // check that the *signed* result fits in a smi
3770 __ add(r2, r3, Operand(0x40000000), SetCC);
3771 __ b(mi, &slow);
3772 break;
3773
3774 default: UNREACHABLE();
3775 }
3776 // tag result and store it in r0
3777 ASSERT(kSmiTag == 0); // adjust code below
3778 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3779 __ b(&exit);
3780 // slow case
3781 __ bind(&slow);
3782 __ push(r1); // restore stack
3783 __ push(r0);
3784 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3785 switch (op_) {
3786 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3787 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3788 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3789 default: UNREACHABLE();
3790 }
3791 __ bind(&exit);
3792 break;
3793 }
3794
3795 default: UNREACHABLE();
3796 }
3797 __ Ret();
3798}
3799
3800
3801void StackCheckStub::Generate(MacroAssembler* masm) {
3802 Label within_limit;
3803 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3804 __ ldr(ip, MemOperand(ip));
3805 __ cmp(sp, Operand(ip));
3806 __ b(hs, &within_limit);
3807 // Do tail-call to runtime routine.
3808 __ push(r0);
3809 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3810 __ bind(&within_limit);
3811
3812 __ StubReturn(1);
3813}
3814
3815
3816void UnarySubStub::Generate(MacroAssembler* masm) {
3817 Label undo;
3818 Label slow;
3819 Label done;
3820
3821 // Enter runtime system if the value is not a smi.
3822 __ tst(r0, Operand(kSmiTagMask));
3823 __ b(ne, &slow);
3824
3825 // Enter runtime system if the value of the expression is zero
3826 // to make sure that we switch between 0 and -0.
3827 __ cmp(r0, Operand(0));
3828 __ b(eq, &slow);
3829
3830 // The value of the expression is a smi that is not zero. Try
3831 // optimistic subtraction '0 - value'.
3832 __ rsb(r1, r0, Operand(0), SetCC);
3833 __ b(vs, &slow);
3834
3835 // If result is a smi we are done.
3836 __ tst(r1, Operand(kSmiTagMask));
3837 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3838 __ b(eq, &done);
3839
3840 // Enter runtime system.
3841 __ bind(&slow);
3842 __ push(r0);
3843 __ mov(r0, Operand(0)); // set number of arguments
3844 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3845
3846 __ bind(&done);
3847 __ StubReturn(1);
3848}
3849
3850
3851void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3852 __ push(r0);
3853 __ mov(r0, Operand(0)); // set number of arguments
3854 switch (kind_) {
3855 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3856 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3857 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3858 default: UNREACHABLE();
3859 }
3860 __ StubReturn(argc_);
3861}
3862
3863
3864void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3865 // r0 holds exception
3866 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3867 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3868 __ ldr(sp, MemOperand(r3));
3869 __ pop(r2); // pop next in chain
3870 __ str(r2, MemOperand(r3));
3871 // restore parameter- and frame-pointer and pop state.
3872 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3873 // Before returning we restore the context from the frame pointer if not NULL.
3874 // The frame pointer is NULL in the exception handler of a JS entry frame.
3875 __ cmp(fp, Operand(0));
3876 // Set cp to NULL if fp is NULL.
3877 __ mov(cp, Operand(0), LeaveCC, eq);
3878 // Restore cp otherwise.
3879 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3880 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3881 __ pop(pc);
3882}
3883
3884
3885void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
3886 // Fetch top stack handler.
3887 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3888 __ ldr(r3, MemOperand(r3));
3889
3890 // Unwind the handlers until the ENTRY handler is found.
3891 Label loop, done;
3892 __ bind(&loop);
3893 // Load the type of the current stack handler.
3894 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
3895 StackHandlerConstants::kStateOffset;
3896 __ ldr(r2, MemOperand(r3, kStateOffset));
3897 __ cmp(r2, Operand(StackHandler::ENTRY));
3898 __ b(eq, &done);
3899 // Fetch the next handler in the list.
3900 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
3901 StackHandlerConstants::kNextOffset;
3902 __ ldr(r3, MemOperand(r3, kNextOffset));
3903 __ jmp(&loop);
3904 __ bind(&done);
3905
3906 // Set the top handler address to next handler past the current ENTRY handler.
3907 __ ldr(r0, MemOperand(r3, kNextOffset));
3908 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
3909 __ str(r0, MemOperand(r2));
3910
3911 // Set external caught exception to false.
3912 __ mov(r0, Operand(false));
3913 ExternalReference external_caught(Top::k_external_caught_exception_address);
3914 __ mov(r2, Operand(external_caught));
3915 __ str(r0, MemOperand(r2));
3916
3917 // Set pending exception and r0 to out of memory exception.
3918 Failure* out_of_memory = Failure::OutOfMemoryException();
3919 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3920 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
3921 __ str(r0, MemOperand(r2));
3922
3923 // Restore the stack to the address of the ENTRY handler
3924 __ mov(sp, Operand(r3));
3925
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003926 // Stack layout at this point. See also PushTryHandler
3927 // r3, sp -> next handler
3928 // state (ENTRY)
3929 // pp
3930 // fp
3931 // lr
3932
3933 // Discard ENTRY state (r2 is not used), and restore parameter-
3934 // and frame-pointer and pop state.
3935 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003936 // Before returning we restore the context from the frame pointer if not NULL.
3937 // The frame pointer is NULL in the exception handler of a JS entry frame.
3938 __ cmp(fp, Operand(0));
3939 // Set cp to NULL if fp is NULL.
3940 __ mov(cp, Operand(0), LeaveCC, eq);
3941 // Restore cp otherwise.
3942 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3943 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3944 __ pop(pc);
3945}
3946
3947
3948void CEntryStub::GenerateCore(MacroAssembler* masm,
3949 Label* throw_normal_exception,
3950 Label* throw_out_of_memory_exception,
3951 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003952 bool do_gc,
3953 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003954 // r0: result parameter for PerformGC, if any
3955 // r4: number of arguments including receiver (C callee-saved)
3956 // r5: pointer to builtin function (C callee-saved)
3957 // r6: pointer to the first argument (C callee-saved)
3958
3959 if (do_gc) {
3960 // Passing r0.
3961 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3962 }
3963
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003964 ExternalReference scope_depth =
3965 ExternalReference::heap_always_allocate_scope_depth();
3966 if (always_allocate) {
3967 __ mov(r0, Operand(scope_depth));
3968 __ ldr(r1, MemOperand(r0));
3969 __ add(r1, r1, Operand(1));
3970 __ str(r1, MemOperand(r0));
3971 }
3972
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003973 // Call C built-in.
3974 // r0 = argc, r1 = argv
3975 __ mov(r0, Operand(r4));
3976 __ mov(r1, Operand(r6));
3977
3978 // TODO(1242173): To let the GC traverse the return address of the exit
3979 // frames, we need to know where the return address is. Right now,
3980 // we push it on the stack to be able to find it again, but we never
3981 // restore from it in case of changes, which makes it impossible to
3982 // support moving the C entry code stub. This should be fixed, but currently
3983 // this is OK because the CEntryStub gets generated so early in the V8 boot
3984 // sequence that it is not moving ever.
3985 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
3986 __ push(lr);
3987#if !defined(__arm__)
3988 // Notify the simulator of the transition to C code.
3989 __ swi(assembler::arm::call_rt_r5);
3990#else /* !defined(__arm__) */
3991 __ mov(pc, Operand(r5));
3992#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003993
3994 if (always_allocate) {
3995 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3996 // though (contain the result).
3997 __ mov(r2, Operand(scope_depth));
3998 __ ldr(r3, MemOperand(r2));
3999 __ sub(r3, r3, Operand(1));
4000 __ str(r3, MemOperand(r2));
4001 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004002
4003 // check for failure result
4004 Label failure_returned;
4005 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4006 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4007 __ add(r2, r0, Operand(1));
4008 __ tst(r2, Operand(kFailureTagMask));
4009 __ b(eq, &failure_returned);
4010
4011 // Exit C frame and return.
4012 // r0:r1: result
4013 // sp: stack pointer
4014 // fp: frame pointer
4015 // pp: caller's parameter pointer pp (restored as C callee-saved)
4016 __ LeaveExitFrame(frame_type);
4017
4018 // check if we should retry or throw exception
4019 Label retry;
4020 __ bind(&failure_returned);
4021 ASSERT(Failure::RETRY_AFTER_GC == 0);
4022 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4023 __ b(eq, &retry);
4024
4025 Label continue_exception;
4026 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4027 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4028 __ b(ne, &continue_exception);
4029
4030 // Retrieve the pending exception and clear the variable.
4031 __ mov(ip, Operand(Factory::the_hole_value().location()));
4032 __ ldr(r3, MemOperand(ip));
4033 __ mov(ip, Operand(Top::pending_exception_address()));
4034 __ ldr(r0, MemOperand(ip));
4035 __ str(r3, MemOperand(ip));
4036
4037 __ bind(&continue_exception);
4038 // Special handling of out of memory exception.
4039 Failure* out_of_memory = Failure::OutOfMemoryException();
4040 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4041 __ b(eq, throw_out_of_memory_exception);
4042
4043 // Handle normal exception.
4044 __ jmp(throw_normal_exception);
4045
4046 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4047}
4048
4049
4050void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4051 // Called from JavaScript; parameters are on stack as if calling JS function
4052 // r0: number of arguments including receiver
4053 // r1: pointer to builtin function
4054 // fp: frame pointer (restored after C call)
4055 // sp: stack pointer (restored as callee's pp after C call)
4056 // cp: current context (C callee-saved)
4057 // pp: caller's parameter pointer pp (C callee-saved)
4058
4059 // NOTE: Invocations of builtins may return failure objects
4060 // instead of a proper result. The builtin entry handles
4061 // this by performing a garbage collection and retrying the
4062 // builtin once.
4063
4064 StackFrame::Type frame_type = is_debug_break
4065 ? StackFrame::EXIT_DEBUG
4066 : StackFrame::EXIT;
4067
4068 // Enter the exit frame that transitions from JavaScript to C++.
4069 __ EnterExitFrame(frame_type);
4070
4071 // r4: number of arguments (C callee-saved)
4072 // r5: pointer to builtin function (C callee-saved)
4073 // r6: pointer to first argument (C callee-saved)
4074
4075 Label throw_out_of_memory_exception;
4076 Label throw_normal_exception;
4077
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004078 // Call into the runtime system. Collect garbage before the call if
4079 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004080 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004081 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004082 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4083 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004084 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004085 &throw_out_of_memory_exception,
4086 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004087 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004088 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004089
4090 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004091 GenerateCore(masm,
4092 &throw_normal_exception,
4093 &throw_out_of_memory_exception,
4094 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004095 true,
4096 false);
4097
4098 // Do full GC and retry runtime call one final time.
4099 Failure* failure = Failure::InternalError();
4100 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4101 GenerateCore(masm,
4102 &throw_normal_exception,
4103 &throw_out_of_memory_exception,
4104 frame_type,
4105 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004106 true);
4107
4108 __ bind(&throw_out_of_memory_exception);
4109 GenerateThrowOutOfMemory(masm);
4110 // control flow for generated will not return.
4111
4112 __ bind(&throw_normal_exception);
4113 GenerateThrowTOS(masm);
4114}
4115
4116
4117void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4118 // r0: code entry
4119 // r1: function
4120 // r2: receiver
4121 // r3: argc
4122 // [sp+0]: argv
4123
4124 Label invoke, exit;
4125
4126 // Called from C, so do not pop argc and args on exit (preserve sp)
4127 // No need to save register-passed args
4128 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4129 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4130
4131 // Get address of argv, see stm above.
4132 // r0: code entry
4133 // r1: function
4134 // r2: receiver
4135 // r3: argc
4136 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4137 __ ldr(r4, MemOperand(r4)); // argv
4138
4139 // Push a frame with special values setup to mark it as an entry frame.
4140 // r0: code entry
4141 // r1: function
4142 // r2: receiver
4143 // r3: argc
4144 // r4: argv
4145 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4146 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4147 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4148 __ mov(r6, Operand(Smi::FromInt(marker)));
4149 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4150 __ ldr(r5, MemOperand(r5));
4151 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4152
4153 // Setup frame pointer for the frame to be pushed.
4154 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4155
4156 // Call a faked try-block that does the invoke.
4157 __ bl(&invoke);
4158
4159 // Caught exception: Store result (exception) in the pending
4160 // exception field in the JSEnv and return a failure sentinel.
4161 // Coming in here the fp will be invalid because the PushTryHandler below
4162 // sets it to 0 to signal the existence of the JSEntry frame.
4163 __ mov(ip, Operand(Top::pending_exception_address()));
4164 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004165 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004166 __ b(&exit);
4167
4168 // Invoke: Link this frame into the handler chain.
4169 __ bind(&invoke);
4170 // Must preserve r0-r4, r5-r7 are available.
4171 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4172 // If an exception not caught by another handler occurs, this handler returns
4173 // control to the code after the bl(&invoke) above, which restores all
4174 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4175 // before returning a failure to C.
4176
4177 // Clear any pending exceptions.
4178 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4179 __ ldr(r5, MemOperand(ip));
4180 __ mov(ip, Operand(Top::pending_exception_address()));
4181 __ str(r5, MemOperand(ip));
4182
4183 // Invoke the function by calling through JS entry trampoline builtin.
4184 // Notice that we cannot store a reference to the trampoline code directly in
4185 // this stub, because runtime stubs are not traversed when doing GC.
4186
4187 // Expected registers by Builtins::JSEntryTrampoline
4188 // r0: code entry
4189 // r1: function
4190 // r2: receiver
4191 // r3: argc
4192 // r4: argv
4193 if (is_construct) {
4194 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4195 __ mov(ip, Operand(construct_entry));
4196 } else {
4197 ExternalReference entry(Builtins::JSEntryTrampoline);
4198 __ mov(ip, Operand(entry));
4199 }
4200 __ ldr(ip, MemOperand(ip)); // deref address
4201
4202 // Branch and link to JSEntryTrampoline
4203 __ mov(lr, Operand(pc));
4204 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4205
4206 // Unlink this frame from the handler chain. When reading the
4207 // address of the next handler, there is no need to use the address
4208 // displacement since the current stack pointer (sp) points directly
4209 // to the stack handler.
4210 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4211 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4212 __ str(r3, MemOperand(ip));
4213 // No need to restore registers
4214 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4215
4216 __ bind(&exit); // r0 holds result
4217 // Restore the top frame descriptors from the stack.
4218 __ pop(r3);
4219 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4220 __ str(r3, MemOperand(ip));
4221
4222 // Reset the stack to the callee saved registers.
4223 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4224
4225 // Restore callee-saved registers and return.
4226#ifdef DEBUG
4227 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4228#endif
4229 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4230}
4231
4232
ager@chromium.org7c537e22008-10-16 08:43:32 +00004233void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004234 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004235 Label adaptor;
4236 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4237 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4238 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004239 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004240
ager@chromium.org7c537e22008-10-16 08:43:32 +00004241 // Nothing to do: The formal number of parameters has already been
4242 // passed in register r0 by calling function. Just return it.
4243 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004244
ager@chromium.org7c537e22008-10-16 08:43:32 +00004245 // Arguments adaptor case: Read the arguments length from the
4246 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004247 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004248 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004249 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004250}
4251
4252
ager@chromium.org7c537e22008-10-16 08:43:32 +00004253void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4254 // The displacement is the offset of the last parameter (if any)
4255 // relative to the frame pointer.
4256 static const int kDisplacement =
4257 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004258
ager@chromium.org7c537e22008-10-16 08:43:32 +00004259 // Check that the key is a smi.
4260 Label slow;
4261 __ tst(r1, Operand(kSmiTagMask));
4262 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004263
ager@chromium.org7c537e22008-10-16 08:43:32 +00004264 // Check if the calling frame is an arguments adaptor frame.
4265 Label adaptor;
4266 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4267 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4268 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4269 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004270
ager@chromium.org7c537e22008-10-16 08:43:32 +00004271 // Check index against formal parameters count limit passed in
4272 // through register eax. Use unsigned comparison to get negative
4273 // check for free.
4274 __ cmp(r1, r0);
4275 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004276
ager@chromium.org7c537e22008-10-16 08:43:32 +00004277 // Read the argument from the stack and return it.
4278 __ sub(r3, r0, r1);
4279 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4280 __ ldr(r0, MemOperand(r3, kDisplacement));
4281 __ mov(pc, lr);
4282
4283 // Arguments adaptor case: Check index against actual arguments
4284 // limit found in the arguments adaptor frame. Use unsigned
4285 // comparison to get negative check for free.
4286 __ bind(&adaptor);
4287 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4288 __ cmp(r1, r0);
4289 __ b(cs, &slow);
4290
4291 // Read the argument from the adaptor frame and return it.
4292 __ sub(r3, r0, r1);
4293 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4294 __ ldr(r0, MemOperand(r3, kDisplacement));
4295 __ mov(pc, lr);
4296
4297 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4298 // by calling the runtime system.
4299 __ bind(&slow);
4300 __ push(r1);
4301 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4302}
4303
4304
4305void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4306 // Check if the calling frame is an arguments adaptor frame.
4307 Label runtime;
4308 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4309 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4310 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4311 __ b(ne, &runtime);
4312
4313 // Patch the arguments.length and the parameters pointer.
4314 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4315 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4316 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4317 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4318 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4319
4320 // Do the runtime call to allocate the arguments object.
4321 __ bind(&runtime);
4322 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004323}
4324
4325
4326void CallFunctionStub::Generate(MacroAssembler* masm) {
4327 Label slow;
4328 // Get the function to call from the stack.
4329 // function, receiver [, arguments]
4330 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4331
4332 // Check that the function is really a JavaScript function.
4333 // r1: pushed function (to be verified)
4334 __ tst(r1, Operand(kSmiTagMask));
4335 __ b(eq, &slow);
4336 // Get the map of the function object.
4337 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4338 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4339 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4340 __ b(ne, &slow);
4341
4342 // Fast-case: Invoke the function now.
4343 // r1: pushed function
4344 ParameterCount actual(argc_);
4345 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4346
4347 // Slow-case: Non-function called.
4348 __ bind(&slow);
4349 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004350 __ mov(r2, Operand(0));
4351 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
4352 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
4353 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004354}
4355
4356
4357#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004359} } // namespace v8::internal