blob: e0cae7f6e85d54cc0394c93ab4958fc5529bdcd9 [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);
2583 if (ShouldGenerateLog(args->at(0))) {
2584 Load(args->at(1));
2585 Load(args->at(2));
2586 __ CallRuntime(Runtime::kLog, 2);
2587 }
2588 __ mov(r0, Operand(Factory::undefined_value()));
2589 frame_->Push(r0);
2590}
2591
2592
ager@chromium.org7c537e22008-10-16 08:43:32 +00002593void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002594 ASSERT(args->length() == 1);
2595 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002596 frame_->Pop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002597 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2598 cc_reg_ = eq;
2599}
2600
2601
kasper.lund7276f142008-07-30 08:49:36 +00002602// This should generate code that performs a charCodeAt() call or returns
2603// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2604// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002605void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002606 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002607 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002608 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002609}
2610
2611
ager@chromium.org7c537e22008-10-16 08:43:32 +00002612void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002613 ASSERT(args->length() == 1);
2614 Load(args->at(0));
2615 Label answer;
2616 // We need the CC bits to come out as not_equal in the case where the
2617 // object is a smi. This can't be done with the usual test opcode so
2618 // we use XOR to get the right CC bits.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002619 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002620 __ and_(r1, r0, Operand(kSmiTagMask));
2621 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2622 __ b(ne, &answer);
2623 // It is a heap object - get the map.
2624 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2625 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2626 // Check if the object is a JS array or not.
2627 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2628 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 cc_reg_ = eq;
2630}
2631
2632
ager@chromium.org7c537e22008-10-16 08:43:32 +00002633void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 ASSERT(args->length() == 0);
2635
mads.s.ager31e71382008-08-13 09:32:07 +00002636 // Seed the result with the formal parameters count, which will be used
2637 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2639
2640 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002641 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002643 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644}
2645
2646
ager@chromium.org7c537e22008-10-16 08:43:32 +00002647void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 ASSERT(args->length() == 1);
2649
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002650 // Satisfy contract with ArgumentsAccessStub:
2651 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002653 frame_->Pop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002654 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655
2656 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002657 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002659 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660}
2661
2662
ager@chromium.org7c537e22008-10-16 08:43:32 +00002663void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002664 ASSERT(args->length() == 2);
2665
2666 // Load the two objects into registers and perform the comparison.
2667 Load(args->at(0));
2668 Load(args->at(1));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002669 frame_->Pop(r0);
2670 frame_->Pop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002671 __ cmp(r0, Operand(r1));
2672 cc_reg_ = eq;
2673}
2674
2675
ager@chromium.org7c537e22008-10-16 08:43:32 +00002676void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002677 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678
2679 ZoneList<Expression*>* args = node->arguments();
2680 Comment cmnt(masm_, "[ CallRuntime");
2681 Runtime::Function* function = node->function();
2682
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002684 // Push the arguments ("left-to-right").
2685 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 // Call the C runtime function.
2688 __ CallRuntime(function, args->length());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002689 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002692 // Prepare stack for calling JS runtime function.
2693 __ mov(r0, Operand(node->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002694 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002695 // Push the builtins object found in the current global object.
2696 __ ldr(r1, GlobalObject());
2697 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002698 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002699
2700 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002704 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002705 __ ldr(cp, frame_->Context());
2706 frame_->Pop();
2707 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 }
2709}
2710
2711
ager@chromium.org7c537e22008-10-16 08:43:32 +00002712void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 Comment cmnt(masm_, "[ UnaryOperation");
2714
2715 Token::Value op = node->op();
2716
2717 if (op == Token::NOT) {
2718 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002719 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 false_target(),
2721 true_target(),
2722 true);
2723 cc_reg_ = NegateCondition(cc_reg_);
2724
2725 } else if (op == Token::DELETE) {
2726 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002727 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 if (property != NULL) {
2729 Load(property->obj());
2730 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002731 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002732 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733
mads.s.ager31e71382008-08-13 09:32:07 +00002734 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735 Slot* slot = variable->slot();
2736 if (variable->is_global()) {
2737 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00002738 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002739 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002740 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002741 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742
2743 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2744 // lookup the context holding the named variable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002745 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002746 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002747 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 __ CallRuntime(Runtime::kLookupContext, 2);
2749 // r0: context
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002750 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002751 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002752 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002753 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002754 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755
mads.s.ager31e71382008-08-13 09:32:07 +00002756 } else {
2757 // Default: Result of deleting non-global, not dynamically
2758 // introduced variables is false.
2759 __ mov(r0, Operand(Factory::false_value()));
2760 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761
2762 } else {
2763 // Default: Result of deleting expressions is true.
2764 Load(node->expression()); // may have side-effects
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002765 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 __ mov(r0, Operand(Factory::true_value()));
2767 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002768 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769
2770 } else if (op == Token::TYPEOF) {
2771 // Special case for loading the typeof expression; see comment on
2772 // LoadTypeofExpression().
2773 LoadTypeofExpression(node->expression());
2774 __ CallRuntime(Runtime::kTypeof, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002775 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776
2777 } else {
2778 Load(node->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002779 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 switch (op) {
2781 case Token::NOT:
2782 case Token::DELETE:
2783 case Token::TYPEOF:
2784 UNREACHABLE(); // handled above
2785 break;
2786
2787 case Token::SUB: {
2788 UnarySubStub stub;
2789 __ CallStub(&stub);
2790 break;
2791 }
2792
2793 case Token::BIT_NOT: {
2794 // smi check
2795 Label smi_label;
2796 Label continue_label;
2797 __ tst(r0, Operand(kSmiTagMask));
2798 __ b(eq, &smi_label);
2799
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002800 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002801 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002802 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803
2804 __ b(&continue_label);
2805 __ bind(&smi_label);
2806 __ mvn(r0, Operand(r0));
2807 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2808 __ bind(&continue_label);
2809 break;
2810 }
2811
2812 case Token::VOID:
2813 // since the stack top is cached in r0, popping and then
2814 // pushing a value can be done by just writing to r0.
2815 __ mov(r0, Operand(Factory::undefined_value()));
2816 break;
2817
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002818 case Token::ADD: {
2819 // Smi check.
2820 Label continue_label;
2821 __ tst(r0, Operand(kSmiTagMask));
2822 __ b(eq, &continue_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002823 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002824 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002825 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002826 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002828 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 default:
2830 UNREACHABLE();
2831 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002832 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833 }
2834}
2835
2836
ager@chromium.org7c537e22008-10-16 08:43:32 +00002837void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 Comment cmnt(masm_, "[ CountOperation");
2839
2840 bool is_postfix = node->is_postfix();
2841 bool is_increment = node->op() == Token::INC;
2842
2843 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2844 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2845
2846 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002847 if (is_postfix) {
2848 __ mov(r0, Operand(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002849 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002850 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851
2852 { Reference target(this, node->expression());
2853 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002854 target.GetValue(NOT_INSIDE_TYPEOF);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002855 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002856
2857 Label slow, exit;
2858
2859 // Load the value (1) into register r1.
2860 __ mov(r1, Operand(Smi::FromInt(1)));
2861
2862 // Check for smi operand.
2863 __ tst(r0, Operand(kSmiTagMask));
2864 __ b(ne, &slow);
2865
2866 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002867 if (is_postfix) {
2868 __ str(r0, frame_->Element(target.size()));
2869 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002870
2871 // Perform optimistic increment/decrement.
2872 if (is_increment) {
2873 __ add(r0, r0, Operand(r1), SetCC);
2874 } else {
2875 __ sub(r0, r0, Operand(r1), SetCC);
2876 }
2877
2878 // If the increment/decrement didn't overflow, we're done.
2879 __ b(vc, &exit);
2880
2881 // Revert optimistic increment/decrement.
2882 if (is_increment) {
2883 __ sub(r0, r0, Operand(r1));
2884 } else {
2885 __ add(r0, r0, Operand(r1));
2886 }
2887
2888 // Slow case: Convert to number.
2889 __ bind(&slow);
2890
2891 // Postfix: Convert the operand to a number and store it as the result.
2892 if (is_postfix) {
2893 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
2894 __ CallStub(&stub);
2895 // Store to result (on the stack).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002896 __ str(r0, frame_->Element(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 }
2898
2899 // Compute the new value by calling the right JavaScript native.
2900 if (is_increment) {
2901 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
2902 __ CallStub(&stub);
2903 } else {
2904 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
2905 __ CallStub(&stub);
2906 }
2907
2908 // Store the new value in the target if not const.
2909 __ bind(&exit);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002910 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002911 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 }
2913
2914 // Postfix: Discard the new value and use the old.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002915 if (is_postfix) frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916}
2917
2918
ager@chromium.org7c537e22008-10-16 08:43:32 +00002919void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002920 Comment cmnt(masm_, "[ BinaryOperation");
2921 Token::Value op = node->op();
2922
2923 // According to ECMA-262 section 11.11, page 58, the binary logical
2924 // operators must yield the result of one of the two expressions
2925 // before any ToBoolean() conversions. This means that the value
2926 // produced by a && or || operator is not necessarily a boolean.
2927
2928 // NOTE: If the left hand side produces a materialized value (not in
2929 // the CC register), we force the right hand side to do the
2930 // same. This is necessary because we may have to branch to the exit
2931 // after evaluating the left hand side (due to the shortcut
2932 // semantics), but the compiler must (statically) know if the result
2933 // of compiling the binary operation is materialized or not.
2934
2935 if (op == Token::AND) {
2936 Label is_true;
2937 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002938 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002939 &is_true,
2940 false_target(),
2941 false);
2942 if (has_cc()) {
2943 Branch(false, false_target());
2944
2945 // Evaluate right side expression.
2946 __ bind(&is_true);
2947 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002948 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949 true_target(),
2950 false_target(),
2951 false);
2952
2953 } else {
2954 Label pop_and_continue, exit;
2955
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002956 __ ldr(r0, frame_->Top()); // dup the stack top
2957 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 // Avoid popping the result if it converts to 'false' using the
2959 // standard ToBoolean() conversion as described in ECMA-262,
2960 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002961 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 Branch(false, &exit);
2963
2964 // Pop the result of evaluating the first part.
2965 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002966 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967
2968 // Evaluate right side expression.
2969 __ bind(&is_true);
2970 Load(node->right());
2971
2972 // Exit (always with a materialized value).
2973 __ bind(&exit);
2974 }
2975
2976 } else if (op == Token::OR) {
2977 Label is_false;
2978 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002979 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980 true_target(),
2981 &is_false,
2982 false);
2983 if (has_cc()) {
2984 Branch(true, true_target());
2985
2986 // Evaluate right side expression.
2987 __ bind(&is_false);
2988 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002989 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002990 true_target(),
2991 false_target(),
2992 false);
2993
2994 } else {
2995 Label pop_and_continue, exit;
2996
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002997 __ ldr(r0, frame_->Top());
2998 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999 // Avoid popping the result if it converts to 'true' using the
3000 // standard ToBoolean() conversion as described in ECMA-262,
3001 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003002 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 Branch(true, &exit);
3004
3005 // Pop the result of evaluating the first part.
3006 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003007 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008
3009 // Evaluate right side expression.
3010 __ bind(&is_false);
3011 Load(node->right());
3012
3013 // Exit (always with a materialized value).
3014 __ bind(&exit);
3015 }
3016
3017 } else {
3018 // Optimize for the case where (at least) one of the expressions
3019 // is a literal small integer.
3020 Literal* lliteral = node->left()->AsLiteral();
3021 Literal* rliteral = node->right()->AsLiteral();
3022
3023 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
3024 Load(node->left());
3025 SmiOperation(node->op(), rliteral->handle(), false);
3026
3027 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
3028 Load(node->right());
3029 SmiOperation(node->op(), lliteral->handle(), true);
3030
3031 } else {
3032 Load(node->left());
3033 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003034 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003035 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003036 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 }
3038}
3039
3040
ager@chromium.org7c537e22008-10-16 08:43:32 +00003041void CodeGenerator::VisitThisFunction(ThisFunction* node) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003042 __ ldr(r0, frame_->Function());
3043 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044}
3045
3046
ager@chromium.org7c537e22008-10-16 08:43:32 +00003047void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 Comment cmnt(masm_, "[ CompareOperation");
3049
3050 // Get the expressions from the node.
3051 Expression* left = node->left();
3052 Expression* right = node->right();
3053 Token::Value op = node->op();
3054
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003055 // To make null checks efficient, we check if either left or right is the
3056 // literal 'null'. If so, we optimize the code by inlining a null check
3057 // instead of calling the (very) general runtime routine for checking
3058 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003060 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003061 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003062 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003063 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3064 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065 if (left_is_null || right_is_null) {
3066 Load(left_is_null ? right : left);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003067 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068 __ cmp(r0, Operand(Factory::null_value()));
3069
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003070 // The 'null' value is only equal to 'undefined' if using non-strict
3071 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 if (op != Token::EQ_STRICT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003073 __ b(eq, true_target());
3074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 __ cmp(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003076 __ b(eq, true_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003079 __ b(eq, false_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003080
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003081 // It can be an undetectable object.
3082 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3083 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3084 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3085 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086 }
3087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 cc_reg_ = eq;
3089 return;
3090 }
3091 }
3092
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003093 // To make typeof testing for natives implemented in JavaScript really
3094 // efficient, we generate special code for expressions of the form:
3095 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003096 UnaryOperation* operation = left->AsUnaryOperation();
3097 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3098 (operation != NULL && operation->op() == Token::TYPEOF) &&
3099 (right->AsLiteral() != NULL &&
3100 right->AsLiteral()->handle()->IsString())) {
3101 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3102
mads.s.ager31e71382008-08-13 09:32:07 +00003103 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003104 LoadTypeofExpression(operation->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003105 frame_->Pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106
3107 if (check->Equals(Heap::number_symbol())) {
3108 __ tst(r1, Operand(kSmiTagMask));
3109 __ b(eq, true_target());
3110 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3111 __ cmp(r1, Operand(Factory::heap_number_map()));
3112 cc_reg_ = eq;
3113
3114 } else if (check->Equals(Heap::string_symbol())) {
3115 __ tst(r1, Operand(kSmiTagMask));
3116 __ b(eq, false_target());
3117
3118 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3119
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003120 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3122 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3123 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3124 __ b(eq, false_target());
3125
3126 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3127 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3128 cc_reg_ = lt;
3129
3130 } else if (check->Equals(Heap::boolean_symbol())) {
3131 __ cmp(r1, Operand(Factory::true_value()));
3132 __ b(eq, true_target());
3133 __ cmp(r1, Operand(Factory::false_value()));
3134 cc_reg_ = eq;
3135
3136 } else if (check->Equals(Heap::undefined_symbol())) {
3137 __ cmp(r1, Operand(Factory::undefined_value()));
3138 __ b(eq, true_target());
3139
3140 __ tst(r1, Operand(kSmiTagMask));
3141 __ b(eq, false_target());
3142
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003143 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3145 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3146 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3147 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3148
3149 cc_reg_ = eq;
3150
3151 } else if (check->Equals(Heap::function_symbol())) {
3152 __ tst(r1, Operand(kSmiTagMask));
3153 __ b(eq, false_target());
3154 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3155 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3156 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3157 cc_reg_ = eq;
3158
3159 } else if (check->Equals(Heap::object_symbol())) {
3160 __ tst(r1, Operand(kSmiTagMask));
3161 __ b(eq, false_target());
3162
3163 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3164 __ cmp(r1, Operand(Factory::null_value()));
3165 __ b(eq, true_target());
3166
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003167 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3169 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3170 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3171 __ b(eq, false_target());
3172
3173 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3174 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3175 __ b(lt, false_target());
3176 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3177 cc_reg_ = le;
3178
3179 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003180 // Uncommon case: typeof testing against a string literal that is
3181 // never returned from the typeof operator.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182 __ b(false_target());
3183 }
3184 return;
3185 }
3186
3187 Load(left);
3188 Load(right);
3189 switch (op) {
3190 case Token::EQ:
3191 Comparison(eq, false);
3192 break;
3193
3194 case Token::LT:
3195 Comparison(lt);
3196 break;
3197
3198 case Token::GT:
3199 Comparison(gt);
3200 break;
3201
3202 case Token::LTE:
3203 Comparison(le);
3204 break;
3205
3206 case Token::GTE:
3207 Comparison(ge);
3208 break;
3209
3210 case Token::EQ_STRICT:
3211 Comparison(eq, true);
3212 break;
3213
3214 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003215 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003216 __ InvokeBuiltin(Builtins::IN, CALL_JS);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003217 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 break;
3219
3220 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003221 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003222 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003223 __ tst(r0, Operand(r0));
3224 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003225 break;
3226
3227 default:
3228 UNREACHABLE();
3229 }
3230}
3231
3232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003233#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003234#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235
ager@chromium.org7c537e22008-10-16 08:43:32 +00003236Handle<String> Reference::GetName() {
3237 ASSERT(type_ == NAMED);
3238 Property* property = expression_->AsProperty();
3239 if (property == NULL) {
3240 // Global variable reference treated as a named property reference.
3241 VariableProxy* proxy = expression_->AsVariableProxy();
3242 ASSERT(proxy->AsVariable() != NULL);
3243 ASSERT(proxy->AsVariable()->is_global());
3244 return proxy->name();
3245 } else {
3246 Literal* raw_name = property->key()->AsLiteral();
3247 ASSERT(raw_name != NULL);
3248 return Handle<String>(String::cast(*raw_name->handle()));
3249 }
3250}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003251
ager@chromium.org7c537e22008-10-16 08:43:32 +00003252
3253void Reference::GetValue(TypeofState typeof_state) {
3254 ASSERT(!is_illegal());
3255 ASSERT(!cgen_->has_cc());
3256 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003257 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003258 Property* property = expression_->AsProperty();
3259 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003260 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003261 }
3262
3263 switch (type_) {
3264 case SLOT: {
3265 Comment cmnt(masm, "[ Load from Slot");
3266 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3267 ASSERT(slot != NULL);
3268 cgen_->LoadFromSlot(slot, typeof_state);
3269 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003270 }
3271
ager@chromium.org7c537e22008-10-16 08:43:32 +00003272 case NAMED: {
3273 // TODO(1241834): Make sure that this it is safe to ignore the
3274 // distinction between expressions in a typeof and not in a typeof. If
3275 // there is a chance that reference errors can be thrown below, we
3276 // must distinguish between the two kinds of loads (typeof expression
3277 // loads must not throw a reference error).
3278 Comment cmnt(masm, "[ Load from named Property");
3279 // Setup the name register.
3280 Handle<String> name(GetName());
3281 __ mov(r2, Operand(name));
3282 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3283
3284 Variable* var = expression_->AsVariableProxy()->AsVariable();
3285 if (var != NULL) {
3286 ASSERT(var->is_global());
3287 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3288 } else {
3289 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003290 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003291 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003292 break;
3293 }
3294
3295 case KEYED: {
3296 // TODO(1241834): Make sure that this it is safe to ignore the
3297 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003298
3299 // TODO(181): Implement inlined version of array indexing once
3300 // loop nesting is properly tracked on ARM.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003301 Comment cmnt(masm, "[ Load from keyed Property");
3302 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003303 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
3304
3305 Variable* var = expression_->AsVariableProxy()->AsVariable();
3306 if (var != NULL) {
3307 ASSERT(var->is_global());
3308 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3309 } else {
3310 __ Call(ic, RelocInfo::CODE_TARGET);
3311 }
3312 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003313 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003314 }
3315
3316 default:
3317 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003318 }
3319}
3320
3321
ager@chromium.org7c537e22008-10-16 08:43:32 +00003322void Reference::SetValue(InitState init_state) {
3323 ASSERT(!is_illegal());
3324 ASSERT(!cgen_->has_cc());
3325 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003326 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003327 Property* property = expression_->AsProperty();
3328 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003329 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003330 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003331
ager@chromium.org7c537e22008-10-16 08:43:32 +00003332 switch (type_) {
3333 case SLOT: {
3334 Comment cmnt(masm, "[ Store to Slot");
3335 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3336 ASSERT(slot != NULL);
3337 if (slot->type() == Slot::LOOKUP) {
3338 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003339
ager@chromium.org7c537e22008-10-16 08:43:32 +00003340 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003341 frame->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003342 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003343 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003344
ager@chromium.org7c537e22008-10-16 08:43:32 +00003345 if (init_state == CONST_INIT) {
3346 // Same as the case for a normal store, but ignores attribute
3347 // (e.g. READ_ONLY) of context slot so that we can initialize
3348 // const properties (introduced via eval("const foo = (some
3349 // expr);")). Also, uses the current function context instead of
3350 // the top context.
3351 //
3352 // Note that we must declare the foo upon entry of eval(), via a
3353 // context slot declaration, but we cannot initialize it at the
3354 // same time, because the const declaration may be at the end of
3355 // the eval code (sigh...) and the const variable may have been
3356 // used before (where its value is 'undefined'). Thus, we can only
3357 // do the initialization when we actually encounter the expression
3358 // and when the expression operands are defined and valid, and
3359 // thus we need the split into 2 operations: declaration of the
3360 // context slot followed by initialization.
3361 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3362 } else {
3363 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3364 }
3365 // Storing a variable must keep the (new) value on the expression
3366 // stack. This is necessary for compiling assignment expressions.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003367 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003368
ager@chromium.org7c537e22008-10-16 08:43:32 +00003369 } else {
3370 ASSERT(slot->var()->mode() != Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003371
ager@chromium.org7c537e22008-10-16 08:43:32 +00003372 Label exit;
3373 if (init_state == CONST_INIT) {
3374 ASSERT(slot->var()->mode() == Variable::CONST);
3375 // Only the first const initialization must be executed (the slot
3376 // still contains 'the hole' value). When the assignment is
3377 // executed, the code is identical to a normal store (see below).
3378 Comment cmnt(masm, "[ Init const");
3379 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3380 __ cmp(r2, Operand(Factory::the_hole_value()));
3381 __ b(ne, &exit);
3382 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003383
ager@chromium.org7c537e22008-10-16 08:43:32 +00003384 // We must execute the store. Storing a variable must keep the
3385 // (new) value on the stack. This is necessary for compiling
3386 // assignment expressions.
3387 //
3388 // Note: We will reach here even with slot->var()->mode() ==
3389 // Variable::CONST because of const declarations which will
3390 // initialize consts to 'the hole' value and by doing so, end up
3391 // calling this code. r2 may be loaded with context; used below in
3392 // RecordWrite.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003393 frame->Pop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003394 __ str(r0, cgen_->SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003395 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003396 if (slot->type() == Slot::CONTEXT) {
3397 // Skip write barrier if the written value is a smi.
3398 __ tst(r0, Operand(kSmiTagMask));
3399 __ b(eq, &exit);
3400 // r2 is loaded with context when calling SlotOperand above.
3401 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3402 __ mov(r3, Operand(offset));
3403 __ RecordWrite(r2, r3, r1);
3404 }
3405 // If we definitely did not jump over the assignment, we do not need
3406 // to bind the exit label. Doing so can defeat peephole
3407 // optimization.
3408 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3409 __ bind(&exit);
3410 }
3411 }
3412 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003413 }
3414
ager@chromium.org7c537e22008-10-16 08:43:32 +00003415 case NAMED: {
3416 Comment cmnt(masm, "[ Store to named Property");
3417 // Call the appropriate IC code.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003418 frame->Pop(r0); // value
ager@chromium.org7c537e22008-10-16 08:43:32 +00003419 // Setup the name register.
3420 Handle<String> name(GetName());
3421 __ mov(r2, Operand(name));
3422 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3423 __ Call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003424 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003425 break;
3426 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003427
ager@chromium.org7c537e22008-10-16 08:43:32 +00003428 case KEYED: {
3429 Comment cmnt(masm, "[ Store to keyed Property");
3430 Property* property = expression_->AsProperty();
3431 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003432 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003433
3434 // Call IC code.
3435 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3436 // TODO(1222589): Make the IC grab the values from the stack.
3437 frame->Pop(r0); // value
3438 __ Call(ic, RelocInfo::CODE_TARGET);
3439 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003440 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003441 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003442
3443 default:
3444 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003445 }
3446}
3447
3448
3449void GetPropertyStub::Generate(MacroAssembler* masm) {
3450 // sp[0]: key
3451 // sp[1]: receiver
3452 Label slow, fast;
3453 // Get the key and receiver object from the stack.
3454 __ ldm(ia, sp, r0.bit() | r1.bit());
3455 // Check that the key is a smi.
3456 __ tst(r0, Operand(kSmiTagMask));
3457 __ b(ne, &slow);
3458 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3459 // Check that the object isn't a smi.
3460 __ tst(r1, Operand(kSmiTagMask));
3461 __ b(eq, &slow);
3462
3463 // Check that the object is some kind of JS object EXCEPT JS Value type.
3464 // In the case that the object is a value-wrapper object,
3465 // we enter the runtime system to make sure that indexing into string
3466 // objects work as intended.
3467 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3468 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3469 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3470 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3471 __ b(lt, &slow);
3472
3473 // Get the elements array of the object.
3474 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3475 // Check that the object is in fast mode (not dictionary).
3476 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3477 __ cmp(r3, Operand(Factory::hash_table_map()));
3478 __ b(eq, &slow);
3479 // Check that the key (index) is within bounds.
3480 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3481 __ cmp(r0, Operand(r3));
3482 __ b(lo, &fast);
3483
3484 // Slow case: Push extra copies of the arguments (2).
3485 __ bind(&slow);
3486 __ ldm(ia, sp, r0.bit() | r1.bit());
3487 __ stm(db_w, sp, r0.bit() | r1.bit());
3488 // Do tail-call to runtime routine.
3489 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3490
3491 // Fast case: Do the load.
3492 __ bind(&fast);
3493 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3494 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3495 __ cmp(r0, Operand(Factory::the_hole_value()));
3496 // In case the loaded value is the_hole we have to consult GetProperty
3497 // to ensure the prototype chain is searched.
3498 __ b(eq, &slow);
3499
3500 __ StubReturn(1);
3501}
3502
3503
3504void SetPropertyStub::Generate(MacroAssembler* masm) {
3505 // r0 : value
3506 // sp[0] : key
3507 // sp[1] : receiver
3508
3509 Label slow, fast, array, extra, exit;
3510 // Get the key and the object from the stack.
3511 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3512 // Check that the key is a smi.
3513 __ tst(r1, Operand(kSmiTagMask));
3514 __ b(ne, &slow);
3515 // Check that the object isn't a smi.
3516 __ tst(r3, Operand(kSmiTagMask));
3517 __ b(eq, &slow);
3518 // Get the type of the object from its map.
3519 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3520 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3521 // Check if the object is a JS array or not.
3522 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3523 __ b(eq, &array);
3524 // Check that the object is some kind of JS object.
3525 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3526 __ b(lt, &slow);
3527
3528
3529 // Object case: Check key against length in the elements array.
3530 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3531 // Check that the object is in fast mode (not dictionary).
3532 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3533 __ cmp(r2, Operand(Factory::hash_table_map()));
3534 __ b(eq, &slow);
3535 // Untag the key (for checking against untagged length in the fixed array).
3536 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3537 // Compute address to store into and check array bounds.
3538 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3539 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3540 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3541 __ cmp(r1, Operand(ip));
3542 __ b(lo, &fast);
3543
3544
3545 // Slow case: Push extra copies of the arguments (3).
3546 __ bind(&slow);
3547 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3548 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3549 // Do tail-call to runtime routine.
3550 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3551
3552
3553 // Extra capacity case: Check if there is extra capacity to
3554 // perform the store and update the length. Used for adding one
3555 // element to the array by writing to array[array.length].
3556 // r0 == value, r1 == key, r2 == elements, r3 == object
3557 __ bind(&extra);
3558 __ b(ne, &slow); // do not leave holes in the array
3559 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3560 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3561 __ cmp(r1, Operand(ip));
3562 __ b(hs, &slow);
3563 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3564 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3565 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3566 __ mov(r3, Operand(r2));
3567 // NOTE: Computing the address to store into must take the fact
3568 // that the key has been incremented into account.
3569 int displacement = Array::kHeaderSize - kHeapObjectTag -
3570 ((1 << kSmiTagSize) * 2);
3571 __ add(r2, r2, Operand(displacement));
3572 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3573 __ b(&fast);
3574
3575
3576 // Array case: Get the length and the elements array from the JS
3577 // array. Check that the array is in fast mode; if it is the
3578 // length is always a smi.
3579 // r0 == value, r3 == object
3580 __ bind(&array);
3581 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3582 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3583 __ cmp(r1, Operand(Factory::hash_table_map()));
3584 __ b(eq, &slow);
3585
3586 // Check the key against the length in the array, compute the
3587 // address to store into and fall through to fast case.
3588 __ ldr(r1, MemOperand(sp));
3589 // r0 == value, r1 == key, r2 == elements, r3 == object.
3590 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3591 __ cmp(r1, Operand(ip));
3592 __ b(hs, &extra);
3593 __ mov(r3, Operand(r2));
3594 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3595 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3596
3597
3598 // Fast case: Do the store.
3599 // r0 == value, r2 == address to store into, r3 == elements
3600 __ bind(&fast);
3601 __ str(r0, MemOperand(r2));
3602 // Skip write barrier if the written value is a smi.
3603 __ tst(r0, Operand(kSmiTagMask));
3604 __ b(eq, &exit);
3605 // Update write barrier for the elements array address.
3606 __ sub(r1, r2, Operand(r3));
3607 __ RecordWrite(r3, r1, r2);
3608 __ bind(&exit);
3609 __ StubReturn(1);
3610}
3611
3612
3613void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3614 // r1 : x
3615 // r0 : y
3616 // result : r0
3617
3618 switch (op_) {
3619 case Token::ADD: {
3620 Label slow, exit;
3621 // fast path
3622 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3623 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3624 // go slow-path in case of overflow
3625 __ b(vs, &slow);
3626 // go slow-path in case of non-smi operands
3627 ASSERT(kSmiTag == 0); // adjust code below
3628 __ tst(r2, Operand(kSmiTagMask));
3629 __ b(eq, &exit);
3630 // slow path
3631 __ bind(&slow);
3632 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3633 __ push(r1);
3634 __ push(r0);
3635 __ mov(r0, Operand(1)); // set number of arguments
3636 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3637 // done
3638 __ bind(&exit);
3639 break;
3640 }
3641
3642 case Token::SUB: {
3643 Label slow, exit;
3644 // fast path
3645 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3646 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3647 // go slow-path in case of overflow
3648 __ b(vs, &slow);
3649 // go slow-path in case of non-smi operands
3650 ASSERT(kSmiTag == 0); // adjust code below
3651 __ tst(r2, Operand(kSmiTagMask));
3652 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3653 __ b(eq, &exit);
3654 // slow path
3655 __ bind(&slow);
3656 __ push(r1);
3657 __ push(r0);
3658 __ mov(r0, Operand(1)); // set number of arguments
3659 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3660 // done
3661 __ bind(&exit);
3662 break;
3663 }
3664
3665 case Token::MUL: {
3666 Label slow, exit;
3667 // tag check
3668 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3669 ASSERT(kSmiTag == 0); // adjust code below
3670 __ tst(r2, Operand(kSmiTagMask));
3671 __ b(ne, &slow);
3672 // remove tag from one operand (but keep sign), so that result is smi
3673 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3674 // do multiplication
3675 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3676 // go slow on overflows (overflow bit is not set)
3677 __ mov(ip, Operand(r3, ASR, 31));
3678 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3679 __ b(ne, &slow);
3680 // go slow on zero result to handle -0
3681 __ tst(r3, Operand(r3));
3682 __ mov(r0, Operand(r3), LeaveCC, ne);
3683 __ b(ne, &exit);
3684 // slow case
3685 __ bind(&slow);
3686 __ push(r1);
3687 __ push(r0);
3688 __ mov(r0, Operand(1)); // set number of arguments
3689 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3690 // done
3691 __ bind(&exit);
3692 break;
3693 }
3694
3695 case Token::BIT_OR:
3696 case Token::BIT_AND:
3697 case Token::BIT_XOR: {
3698 Label slow, exit;
3699 // tag check
3700 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3701 ASSERT(kSmiTag == 0); // adjust code below
3702 __ tst(r2, Operand(kSmiTagMask));
3703 __ b(ne, &slow);
3704 switch (op_) {
3705 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3706 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3707 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3708 default: UNREACHABLE();
3709 }
3710 __ b(&exit);
3711 __ bind(&slow);
3712 __ push(r1); // restore stack
3713 __ push(r0);
3714 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3715 switch (op_) {
3716 case Token::BIT_OR:
3717 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3718 break;
3719 case Token::BIT_AND:
3720 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3721 break;
3722 case Token::BIT_XOR:
3723 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3724 break;
3725 default:
3726 UNREACHABLE();
3727 }
3728 __ bind(&exit);
3729 break;
3730 }
3731
3732 case Token::SHL:
3733 case Token::SHR:
3734 case Token::SAR: {
3735 Label slow, exit;
3736 // tag check
3737 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3738 ASSERT(kSmiTag == 0); // adjust code below
3739 __ tst(r2, Operand(kSmiTagMask));
3740 __ b(ne, &slow);
3741 // remove tags from operands (but keep sign)
3742 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3743 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3744 // use only the 5 least significant bits of the shift count
3745 __ and_(r2, r2, Operand(0x1f));
3746 // perform operation
3747 switch (op_) {
3748 case Token::SAR:
3749 __ mov(r3, Operand(r3, ASR, r2));
3750 // no checks of result necessary
3751 break;
3752
3753 case Token::SHR:
3754 __ mov(r3, Operand(r3, LSR, r2));
3755 // check that the *unsigned* result fits in a smi
3756 // neither of the two high-order bits can be set:
3757 // - 0x80000000: high bit would be lost when smi tagging
3758 // - 0x40000000: this number would convert to negative when
3759 // smi tagging these two cases can only happen with shifts
3760 // by 0 or 1 when handed a valid smi
3761 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3762 __ b(ne, &slow);
3763 break;
3764
3765 case Token::SHL:
3766 __ mov(r3, Operand(r3, LSL, r2));
3767 // check that the *signed* result fits in a smi
3768 __ add(r2, r3, Operand(0x40000000), SetCC);
3769 __ b(mi, &slow);
3770 break;
3771
3772 default: UNREACHABLE();
3773 }
3774 // tag result and store it in r0
3775 ASSERT(kSmiTag == 0); // adjust code below
3776 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3777 __ b(&exit);
3778 // slow case
3779 __ bind(&slow);
3780 __ push(r1); // restore stack
3781 __ push(r0);
3782 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3783 switch (op_) {
3784 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3785 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3786 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3787 default: UNREACHABLE();
3788 }
3789 __ bind(&exit);
3790 break;
3791 }
3792
3793 default: UNREACHABLE();
3794 }
3795 __ Ret();
3796}
3797
3798
3799void StackCheckStub::Generate(MacroAssembler* masm) {
3800 Label within_limit;
3801 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3802 __ ldr(ip, MemOperand(ip));
3803 __ cmp(sp, Operand(ip));
3804 __ b(hs, &within_limit);
3805 // Do tail-call to runtime routine.
3806 __ push(r0);
3807 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3808 __ bind(&within_limit);
3809
3810 __ StubReturn(1);
3811}
3812
3813
3814void UnarySubStub::Generate(MacroAssembler* masm) {
3815 Label undo;
3816 Label slow;
3817 Label done;
3818
3819 // Enter runtime system if the value is not a smi.
3820 __ tst(r0, Operand(kSmiTagMask));
3821 __ b(ne, &slow);
3822
3823 // Enter runtime system if the value of the expression is zero
3824 // to make sure that we switch between 0 and -0.
3825 __ cmp(r0, Operand(0));
3826 __ b(eq, &slow);
3827
3828 // The value of the expression is a smi that is not zero. Try
3829 // optimistic subtraction '0 - value'.
3830 __ rsb(r1, r0, Operand(0), SetCC);
3831 __ b(vs, &slow);
3832
3833 // If result is a smi we are done.
3834 __ tst(r1, Operand(kSmiTagMask));
3835 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3836 __ b(eq, &done);
3837
3838 // Enter runtime system.
3839 __ bind(&slow);
3840 __ push(r0);
3841 __ mov(r0, Operand(0)); // set number of arguments
3842 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3843
3844 __ bind(&done);
3845 __ StubReturn(1);
3846}
3847
3848
3849void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3850 __ push(r0);
3851 __ mov(r0, Operand(0)); // set number of arguments
3852 switch (kind_) {
3853 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3854 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3855 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3856 default: UNREACHABLE();
3857 }
3858 __ StubReturn(argc_);
3859}
3860
3861
3862void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3863 // r0 holds exception
3864 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3865 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3866 __ ldr(sp, MemOperand(r3));
3867 __ pop(r2); // pop next in chain
3868 __ str(r2, MemOperand(r3));
3869 // restore parameter- and frame-pointer and pop state.
3870 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3871 // Before returning we restore the context from the frame pointer if not NULL.
3872 // The frame pointer is NULL in the exception handler of a JS entry frame.
3873 __ cmp(fp, Operand(0));
3874 // Set cp to NULL if fp is NULL.
3875 __ mov(cp, Operand(0), LeaveCC, eq);
3876 // Restore cp otherwise.
3877 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3878 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3879 __ pop(pc);
3880}
3881
3882
3883void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
3884 // Fetch top stack handler.
3885 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3886 __ ldr(r3, MemOperand(r3));
3887
3888 // Unwind the handlers until the ENTRY handler is found.
3889 Label loop, done;
3890 __ bind(&loop);
3891 // Load the type of the current stack handler.
3892 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
3893 StackHandlerConstants::kStateOffset;
3894 __ ldr(r2, MemOperand(r3, kStateOffset));
3895 __ cmp(r2, Operand(StackHandler::ENTRY));
3896 __ b(eq, &done);
3897 // Fetch the next handler in the list.
3898 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
3899 StackHandlerConstants::kNextOffset;
3900 __ ldr(r3, MemOperand(r3, kNextOffset));
3901 __ jmp(&loop);
3902 __ bind(&done);
3903
3904 // Set the top handler address to next handler past the current ENTRY handler.
3905 __ ldr(r0, MemOperand(r3, kNextOffset));
3906 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
3907 __ str(r0, MemOperand(r2));
3908
3909 // Set external caught exception to false.
3910 __ mov(r0, Operand(false));
3911 ExternalReference external_caught(Top::k_external_caught_exception_address);
3912 __ mov(r2, Operand(external_caught));
3913 __ str(r0, MemOperand(r2));
3914
3915 // Set pending exception and r0 to out of memory exception.
3916 Failure* out_of_memory = Failure::OutOfMemoryException();
3917 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3918 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
3919 __ str(r0, MemOperand(r2));
3920
3921 // Restore the stack to the address of the ENTRY handler
3922 __ mov(sp, Operand(r3));
3923
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003924 // Stack layout at this point. See also PushTryHandler
3925 // r3, sp -> next handler
3926 // state (ENTRY)
3927 // pp
3928 // fp
3929 // lr
3930
3931 // Discard ENTRY state (r2 is not used), and restore parameter-
3932 // and frame-pointer and pop state.
3933 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003934 // Before returning we restore the context from the frame pointer if not NULL.
3935 // The frame pointer is NULL in the exception handler of a JS entry frame.
3936 __ cmp(fp, Operand(0));
3937 // Set cp to NULL if fp is NULL.
3938 __ mov(cp, Operand(0), LeaveCC, eq);
3939 // Restore cp otherwise.
3940 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3941 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3942 __ pop(pc);
3943}
3944
3945
3946void CEntryStub::GenerateCore(MacroAssembler* masm,
3947 Label* throw_normal_exception,
3948 Label* throw_out_of_memory_exception,
3949 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003950 bool do_gc,
3951 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003952 // r0: result parameter for PerformGC, if any
3953 // r4: number of arguments including receiver (C callee-saved)
3954 // r5: pointer to builtin function (C callee-saved)
3955 // r6: pointer to the first argument (C callee-saved)
3956
3957 if (do_gc) {
3958 // Passing r0.
3959 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3960 }
3961
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003962 ExternalReference scope_depth =
3963 ExternalReference::heap_always_allocate_scope_depth();
3964 if (always_allocate) {
3965 __ mov(r0, Operand(scope_depth));
3966 __ ldr(r1, MemOperand(r0));
3967 __ add(r1, r1, Operand(1));
3968 __ str(r1, MemOperand(r0));
3969 }
3970
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003971 // Call C built-in.
3972 // r0 = argc, r1 = argv
3973 __ mov(r0, Operand(r4));
3974 __ mov(r1, Operand(r6));
3975
3976 // TODO(1242173): To let the GC traverse the return address of the exit
3977 // frames, we need to know where the return address is. Right now,
3978 // we push it on the stack to be able to find it again, but we never
3979 // restore from it in case of changes, which makes it impossible to
3980 // support moving the C entry code stub. This should be fixed, but currently
3981 // this is OK because the CEntryStub gets generated so early in the V8 boot
3982 // sequence that it is not moving ever.
3983 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
3984 __ push(lr);
3985#if !defined(__arm__)
3986 // Notify the simulator of the transition to C code.
3987 __ swi(assembler::arm::call_rt_r5);
3988#else /* !defined(__arm__) */
3989 __ mov(pc, Operand(r5));
3990#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003991
3992 if (always_allocate) {
3993 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3994 // though (contain the result).
3995 __ mov(r2, Operand(scope_depth));
3996 __ ldr(r3, MemOperand(r2));
3997 __ sub(r3, r3, Operand(1));
3998 __ str(r3, MemOperand(r2));
3999 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004000
4001 // check for failure result
4002 Label failure_returned;
4003 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4004 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4005 __ add(r2, r0, Operand(1));
4006 __ tst(r2, Operand(kFailureTagMask));
4007 __ b(eq, &failure_returned);
4008
4009 // Exit C frame and return.
4010 // r0:r1: result
4011 // sp: stack pointer
4012 // fp: frame pointer
4013 // pp: caller's parameter pointer pp (restored as C callee-saved)
4014 __ LeaveExitFrame(frame_type);
4015
4016 // check if we should retry or throw exception
4017 Label retry;
4018 __ bind(&failure_returned);
4019 ASSERT(Failure::RETRY_AFTER_GC == 0);
4020 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4021 __ b(eq, &retry);
4022
4023 Label continue_exception;
4024 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4025 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4026 __ b(ne, &continue_exception);
4027
4028 // Retrieve the pending exception and clear the variable.
4029 __ mov(ip, Operand(Factory::the_hole_value().location()));
4030 __ ldr(r3, MemOperand(ip));
4031 __ mov(ip, Operand(Top::pending_exception_address()));
4032 __ ldr(r0, MemOperand(ip));
4033 __ str(r3, MemOperand(ip));
4034
4035 __ bind(&continue_exception);
4036 // Special handling of out of memory exception.
4037 Failure* out_of_memory = Failure::OutOfMemoryException();
4038 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4039 __ b(eq, throw_out_of_memory_exception);
4040
4041 // Handle normal exception.
4042 __ jmp(throw_normal_exception);
4043
4044 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4045}
4046
4047
4048void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4049 // Called from JavaScript; parameters are on stack as if calling JS function
4050 // r0: number of arguments including receiver
4051 // r1: pointer to builtin function
4052 // fp: frame pointer (restored after C call)
4053 // sp: stack pointer (restored as callee's pp after C call)
4054 // cp: current context (C callee-saved)
4055 // pp: caller's parameter pointer pp (C callee-saved)
4056
4057 // NOTE: Invocations of builtins may return failure objects
4058 // instead of a proper result. The builtin entry handles
4059 // this by performing a garbage collection and retrying the
4060 // builtin once.
4061
4062 StackFrame::Type frame_type = is_debug_break
4063 ? StackFrame::EXIT_DEBUG
4064 : StackFrame::EXIT;
4065
4066 // Enter the exit frame that transitions from JavaScript to C++.
4067 __ EnterExitFrame(frame_type);
4068
4069 // r4: number of arguments (C callee-saved)
4070 // r5: pointer to builtin function (C callee-saved)
4071 // r6: pointer to first argument (C callee-saved)
4072
4073 Label throw_out_of_memory_exception;
4074 Label throw_normal_exception;
4075
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004076 // Call into the runtime system. Collect garbage before the call if
4077 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004078 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004079 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004080 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4081 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004082 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004083 &throw_out_of_memory_exception,
4084 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004085 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004086 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004087
4088 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004089 GenerateCore(masm,
4090 &throw_normal_exception,
4091 &throw_out_of_memory_exception,
4092 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004093 true,
4094 false);
4095
4096 // Do full GC and retry runtime call one final time.
4097 Failure* failure = Failure::InternalError();
4098 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4099 GenerateCore(masm,
4100 &throw_normal_exception,
4101 &throw_out_of_memory_exception,
4102 frame_type,
4103 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004104 true);
4105
4106 __ bind(&throw_out_of_memory_exception);
4107 GenerateThrowOutOfMemory(masm);
4108 // control flow for generated will not return.
4109
4110 __ bind(&throw_normal_exception);
4111 GenerateThrowTOS(masm);
4112}
4113
4114
4115void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4116 // r0: code entry
4117 // r1: function
4118 // r2: receiver
4119 // r3: argc
4120 // [sp+0]: argv
4121
4122 Label invoke, exit;
4123
4124 // Called from C, so do not pop argc and args on exit (preserve sp)
4125 // No need to save register-passed args
4126 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4127 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4128
4129 // Get address of argv, see stm above.
4130 // r0: code entry
4131 // r1: function
4132 // r2: receiver
4133 // r3: argc
4134 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4135 __ ldr(r4, MemOperand(r4)); // argv
4136
4137 // Push a frame with special values setup to mark it as an entry frame.
4138 // r0: code entry
4139 // r1: function
4140 // r2: receiver
4141 // r3: argc
4142 // r4: argv
4143 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4144 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4145 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4146 __ mov(r6, Operand(Smi::FromInt(marker)));
4147 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4148 __ ldr(r5, MemOperand(r5));
4149 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4150
4151 // Setup frame pointer for the frame to be pushed.
4152 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4153
4154 // Call a faked try-block that does the invoke.
4155 __ bl(&invoke);
4156
4157 // Caught exception: Store result (exception) in the pending
4158 // exception field in the JSEnv and return a failure sentinel.
4159 // Coming in here the fp will be invalid because the PushTryHandler below
4160 // sets it to 0 to signal the existence of the JSEntry frame.
4161 __ mov(ip, Operand(Top::pending_exception_address()));
4162 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004163 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004164 __ b(&exit);
4165
4166 // Invoke: Link this frame into the handler chain.
4167 __ bind(&invoke);
4168 // Must preserve r0-r4, r5-r7 are available.
4169 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4170 // If an exception not caught by another handler occurs, this handler returns
4171 // control to the code after the bl(&invoke) above, which restores all
4172 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4173 // before returning a failure to C.
4174
4175 // Clear any pending exceptions.
4176 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4177 __ ldr(r5, MemOperand(ip));
4178 __ mov(ip, Operand(Top::pending_exception_address()));
4179 __ str(r5, MemOperand(ip));
4180
4181 // Invoke the function by calling through JS entry trampoline builtin.
4182 // Notice that we cannot store a reference to the trampoline code directly in
4183 // this stub, because runtime stubs are not traversed when doing GC.
4184
4185 // Expected registers by Builtins::JSEntryTrampoline
4186 // r0: code entry
4187 // r1: function
4188 // r2: receiver
4189 // r3: argc
4190 // r4: argv
4191 if (is_construct) {
4192 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4193 __ mov(ip, Operand(construct_entry));
4194 } else {
4195 ExternalReference entry(Builtins::JSEntryTrampoline);
4196 __ mov(ip, Operand(entry));
4197 }
4198 __ ldr(ip, MemOperand(ip)); // deref address
4199
4200 // Branch and link to JSEntryTrampoline
4201 __ mov(lr, Operand(pc));
4202 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4203
4204 // Unlink this frame from the handler chain. When reading the
4205 // address of the next handler, there is no need to use the address
4206 // displacement since the current stack pointer (sp) points directly
4207 // to the stack handler.
4208 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4209 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4210 __ str(r3, MemOperand(ip));
4211 // No need to restore registers
4212 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4213
4214 __ bind(&exit); // r0 holds result
4215 // Restore the top frame descriptors from the stack.
4216 __ pop(r3);
4217 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4218 __ str(r3, MemOperand(ip));
4219
4220 // Reset the stack to the callee saved registers.
4221 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4222
4223 // Restore callee-saved registers and return.
4224#ifdef DEBUG
4225 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4226#endif
4227 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4228}
4229
4230
ager@chromium.org7c537e22008-10-16 08:43:32 +00004231void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004232 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004233 Label adaptor;
4234 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4235 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4236 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004237 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004238
ager@chromium.org7c537e22008-10-16 08:43:32 +00004239 // Nothing to do: The formal number of parameters has already been
4240 // passed in register r0 by calling function. Just return it.
4241 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004242
ager@chromium.org7c537e22008-10-16 08:43:32 +00004243 // Arguments adaptor case: Read the arguments length from the
4244 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004245 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004246 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004247 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004248}
4249
4250
ager@chromium.org7c537e22008-10-16 08:43:32 +00004251void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4252 // The displacement is the offset of the last parameter (if any)
4253 // relative to the frame pointer.
4254 static const int kDisplacement =
4255 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004256
ager@chromium.org7c537e22008-10-16 08:43:32 +00004257 // Check that the key is a smi.
4258 Label slow;
4259 __ tst(r1, Operand(kSmiTagMask));
4260 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004261
ager@chromium.org7c537e22008-10-16 08:43:32 +00004262 // Check if the calling frame is an arguments adaptor frame.
4263 Label adaptor;
4264 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4265 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4266 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4267 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004268
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269 // Check index against formal parameters count limit passed in
4270 // through register eax. Use unsigned comparison to get negative
4271 // check for free.
4272 __ cmp(r1, r0);
4273 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004274
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275 // Read the argument from the stack and return it.
4276 __ sub(r3, r0, r1);
4277 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4278 __ ldr(r0, MemOperand(r3, kDisplacement));
4279 __ mov(pc, lr);
4280
4281 // Arguments adaptor case: Check index against actual arguments
4282 // limit found in the arguments adaptor frame. Use unsigned
4283 // comparison to get negative check for free.
4284 __ bind(&adaptor);
4285 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4286 __ cmp(r1, r0);
4287 __ b(cs, &slow);
4288
4289 // Read the argument from the adaptor frame and return it.
4290 __ sub(r3, r0, r1);
4291 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4292 __ ldr(r0, MemOperand(r3, kDisplacement));
4293 __ mov(pc, lr);
4294
4295 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4296 // by calling the runtime system.
4297 __ bind(&slow);
4298 __ push(r1);
4299 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4300}
4301
4302
4303void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4304 // Check if the calling frame is an arguments adaptor frame.
4305 Label runtime;
4306 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4307 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4308 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4309 __ b(ne, &runtime);
4310
4311 // Patch the arguments.length and the parameters pointer.
4312 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4313 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4314 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4315 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4316 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4317
4318 // Do the runtime call to allocate the arguments object.
4319 __ bind(&runtime);
4320 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004321}
4322
4323
4324void CallFunctionStub::Generate(MacroAssembler* masm) {
4325 Label slow;
4326 // Get the function to call from the stack.
4327 // function, receiver [, arguments]
4328 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4329
4330 // Check that the function is really a JavaScript function.
4331 // r1: pushed function (to be verified)
4332 __ tst(r1, Operand(kSmiTagMask));
4333 __ b(eq, &slow);
4334 // Get the map of the function object.
4335 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4336 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4337 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4338 __ b(ne, &slow);
4339
4340 // Fast-case: Invoke the function now.
4341 // r1: pushed function
4342 ParameterCount actual(argc_);
4343 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4344
4345 // Slow-case: Non-function called.
4346 __ bind(&slow);
4347 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004348 __ mov(r2, Operand(0));
4349 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
4350 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
4351 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004352}
4353
4354
4355#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004357} } // namespace v8::internal