blob: fa289fca2e0d9ed32994eab17911e05581a82a20 [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) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001147 {
1148 // Set initial value.
1149 Reference target(this, node->proxy());
1150 Load(val);
1151 target.SetValue(NOT_CONST_INIT);
1152 // The reference is removed from the stack (preserving TOS) when
1153 // it goes out of scope.
1154 }
1155 // Get rid of the assigned value (declarations are statements).
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.org32912102009-01-16 10:38:43 +00002238void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
2239 // Call runtime routine to allocate the catch extension object and
2240 // assign the exception value to the catch variable.
2241 Comment cmnt(masm_, "[CatchExtensionObject ");
2242 Load(node->key());
2243 Load(node->value());
2244 __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2245 frame_->Push(r0);
2246}
2247
2248
ager@chromium.org7c537e22008-10-16 08:43:32 +00002249void CodeGenerator::VisitAssignment(Assignment* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 Comment cmnt(masm_, "[ Assignment");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002251 CodeForStatement(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 Reference target(this, node->target());
2254 if (target.is_illegal()) return;
2255
2256 if (node->op() == Token::ASSIGN ||
2257 node->op() == Token::INIT_VAR ||
2258 node->op() == Token::INIT_CONST) {
2259 Load(node->value());
2260
2261 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002262 target.GetValue(NOT_INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 Literal* literal = node->value()->AsLiteral();
2264 if (literal != NULL && literal->handle()->IsSmi()) {
2265 SmiOperation(node->binary_op(), literal->handle(), false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002266 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 } else {
2269 Load(node->value());
kasper.lund7276f142008-07-30 08:49:36 +00002270 GenericBinaryOperation(node->binary_op());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002271 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 }
2273 }
2274
2275 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2276 if (var != NULL &&
2277 (var->mode() == Variable::CONST) &&
2278 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2279 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 } else {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002282 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 if (node->op() == Token::INIT_CONST) {
2284 // Dynamic constant initializations must use the function context
2285 // and initialize the actual constant declared. Dynamic variable
2286 // initializations are simply assignments and use SetValue.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002287 target.SetValue(CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002289 target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 }
2291 }
2292}
2293
2294
ager@chromium.org7c537e22008-10-16 08:43:32 +00002295void CodeGenerator::VisitThrow(Throw* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 Comment cmnt(masm_, "[ Throw");
2297
2298 Load(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002299 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 __ CallRuntime(Runtime::kThrow, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002301 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302}
2303
2304
ager@chromium.org7c537e22008-10-16 08:43:32 +00002305void CodeGenerator::VisitProperty(Property* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002307
ager@chromium.org7c537e22008-10-16 08:43:32 +00002308 Reference property(this, node);
2309 property.GetValue(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310}
2311
2312
ager@chromium.org7c537e22008-10-16 08:43:32 +00002313void CodeGenerator::VisitCall(Call* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 Comment cmnt(masm_, "[ Call");
2315
2316 ZoneList<Expression*>* args = node->arguments();
2317
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002318 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 // Standard function call.
2320
2321 // Check if the function is a variable or a property.
2322 Expression* function = node->expression();
2323 Variable* var = function->AsVariableProxy()->AsVariable();
2324 Property* property = function->AsProperty();
2325
2326 // ------------------------------------------------------------------------
2327 // Fast-case: Use inline caching.
2328 // ---
2329 // According to ECMA-262, section 11.2.3, page 44, the function to call
2330 // must be resolved after the arguments have been evaluated. The IC code
2331 // automatically handles this by loading the arguments before the function
2332 // is resolved in cache misses (this also holds for megamorphic calls).
2333 // ------------------------------------------------------------------------
2334
2335 if (var != NULL && !var->is_this() && var->is_global()) {
2336 // ----------------------------------
2337 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2338 // ----------------------------------
2339
2340 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002341 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002342 frame_->Push(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002343
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002344 // Pass the global object as the receiver and let the IC stub
2345 // patch the stack to use the global proxy as 'this' in the
2346 // invoked function.
2347 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
2349 // Load the arguments.
2350 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351
2352 // Setup the receiver register and call the IC initialization code.
2353 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002354 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002355 __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002356 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002358 frame_->Pop();
2359 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360
2361 } else if (var != NULL && var->slot() != NULL &&
2362 var->slot()->type() == Slot::LOOKUP) {
2363 // ----------------------------------
2364 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2365 // ----------------------------------
2366
2367 // Load the function
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002368 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002369 __ mov(r0, Operand(var->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002370 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 __ CallRuntime(Runtime::kLoadContextSlot, 2);
2372 // r0: slot value; r1: receiver
2373
2374 // Load the receiver.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002375 frame_->Push(r0); // function
2376 frame_->Push(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
2378 // Call the function.
2379 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002380 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381
2382 } else if (property != NULL) {
2383 // Check if the key is a literal string.
2384 Literal* literal = property->key()->AsLiteral();
2385
2386 if (literal != NULL && literal->handle()->IsSymbol()) {
2387 // ------------------------------------------------------------------
2388 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2389 // ------------------------------------------------------------------
2390
2391 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002392 __ mov(r0, Operand(literal->handle()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002393 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 Load(property->obj());
2395
2396 // Load the arguments.
2397 for (int i = 0; i < args->length(); i++) Load(args->at(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398
2399 // Set the receiver register and call the IC initialization code.
2400 Handle<Code> stub = ComputeCallInitialize(args->length());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002401 CodeForSourcePosition(node->position());
ager@chromium.org236ad962008-09-25 09:45:57 +00002402 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002403 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404
2405 // Remove the function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002406 frame_->Pop();
mads.s.ager31e71382008-08-13 09:32:07 +00002407
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002408 frame_->Push(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409
2410 } else {
2411 // -------------------------------------------
2412 // JavaScript example: 'array[index](1, 2, 3)'
2413 // -------------------------------------------
2414
2415 // Load the function to call from the property through a reference.
2416 Reference ref(this, property);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002417 ref.GetValue(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418
2419 // Pass receiver to called function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002420 __ ldr(r0, frame_->Element(ref.size()));
2421 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422 // Call the function.
2423 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002424 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425 }
2426
2427 } else {
2428 // ----------------------------------
2429 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
2430 // ----------------------------------
2431
2432 // Load the function.
2433 Load(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002434
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002435 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002436 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 // Call the function.
2439 CallWithArguments(args, node->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002440 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 }
2442}
2443
2444
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002445void CodeGenerator::VisitCallEval(CallEval* node) {
2446 Comment cmnt(masm_, "[ CallEval");
2447
2448 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
2449 // the function we need to call and the receiver of the call.
2450 // Then we call the resolved function using the given arguments.
2451
2452 ZoneList<Expression*>* args = node->arguments();
2453 Expression* function = node->expression();
2454
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002455 CodeForStatement(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002456
2457 // Prepare stack for call to resolved function.
2458 Load(function);
2459 __ mov(r2, Operand(Factory::undefined_value()));
2460 __ push(r2); // Slot for receiver
2461 for (int i = 0; i < args->length(); i++) {
2462 Load(args->at(i));
2463 }
2464
2465 // Prepare stack for call to ResolvePossiblyDirectEval.
2466 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize + kPointerSize));
2467 __ push(r1);
2468 if (args->length() > 0) {
2469 __ ldr(r1, MemOperand(sp, args->length() * kPointerSize));
2470 __ push(r1);
2471 } else {
2472 __ push(r2);
2473 }
2474
2475 // Resolve the call.
2476 __ CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
2477
2478 // Touch up stack with the right values for the function and the receiver.
2479 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
2480 __ str(r1, MemOperand(sp, (args->length() + 1) * kPointerSize));
2481 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
2482 __ str(r1, MemOperand(sp, args->length() * kPointerSize));
2483
2484 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002485 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002486
2487 CallFunctionStub call_function(args->length());
2488 __ CallStub(&call_function);
2489
2490 __ ldr(cp, frame_->Context());
2491 // Remove the function from the stack.
2492 frame_->Pop();
2493 frame_->Push(r0);
2494}
2495
2496
ager@chromium.org7c537e22008-10-16 08:43:32 +00002497void CodeGenerator::VisitCallNew(CallNew* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 Comment cmnt(masm_, "[ CallNew");
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002499 CodeForStatement(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500
2501 // According to ECMA-262, section 11.2.2, page 44, the function
2502 // expression in new calls must be evaluated before the
2503 // arguments. This is different from ordinary calls, where the
2504 // actual function to call is resolved after the arguments have been
2505 // evaluated.
2506
2507 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002508 // receiver. There is no need to use the global proxy here because
2509 // it will always be replaced with a newly allocated object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002510 Load(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002511 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512
2513 // Push the arguments ("left-to-right") on the stack.
2514 ZoneList<Expression*>* args = node->arguments();
2515 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2516
mads.s.ager31e71382008-08-13 09:32:07 +00002517 // r0: the number of arguments.
2518 __ mov(r0, Operand(args->length()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002520 // Load the function into r1 as per calling convention.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002521 __ ldr(r1, frame_->Element(args->length() + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 // Call the construct call builtin that handles allocation and
2524 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002525 CodeForSourcePosition(node->position());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +00002527 RelocInfo::CONSTRUCT_CALL);
mads.s.ager31e71382008-08-13 09:32:07 +00002528
2529 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002530 __ str(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531}
2532
2533
ager@chromium.org7c537e22008-10-16 08:43:32 +00002534void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 ASSERT(args->length() == 1);
2536 Label leave;
2537 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002538 frame_->Pop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00002539 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 __ tst(r0, Operand(kSmiTagMask));
2541 __ b(eq, &leave);
2542 // It is a heap object - get map.
2543 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2544 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00002545 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 __ cmp(r1, Operand(JS_VALUE_TYPE));
2547 __ b(ne, &leave);
2548 // Load the value.
2549 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
2550 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002551 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552}
2553
2554
ager@chromium.org7c537e22008-10-16 08:43:32 +00002555void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 ASSERT(args->length() == 2);
2557 Label leave;
2558 Load(args->at(0)); // Load the object.
2559 Load(args->at(1)); // Load the value.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002560 frame_->Pop(r0); // r0 contains value
2561 frame_->Pop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 // if (object->IsSmi()) return object.
2563 __ tst(r1, Operand(kSmiTagMask));
2564 __ b(eq, &leave);
2565 // It is a heap object - get map.
2566 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
2567 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
2568 // if (!object->IsJSValue()) return object.
2569 __ cmp(r2, Operand(JS_VALUE_TYPE));
2570 __ b(ne, &leave);
2571 // Store the value.
2572 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
2573 // Update the write barrier.
2574 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
2575 __ RecordWrite(r1, r2, r3);
2576 // Leave.
2577 __ bind(&leave);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002578 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579}
2580
2581
ager@chromium.org7c537e22008-10-16 08:43:32 +00002582void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 ASSERT(args->length() == 1);
2584 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002585 frame_->Pop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002586 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 cc_reg_ = eq;
2588}
2589
2590
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002591void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
2592 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
2593 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002594#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002595 if (ShouldGenerateLog(args->at(0))) {
2596 Load(args->at(1));
2597 Load(args->at(2));
2598 __ CallRuntime(Runtime::kLog, 2);
2599 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00002600#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002601 __ mov(r0, Operand(Factory::undefined_value()));
2602 frame_->Push(r0);
2603}
2604
2605
ager@chromium.org7c537e22008-10-16 08:43:32 +00002606void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002607 ASSERT(args->length() == 1);
2608 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002609 frame_->Pop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00002610 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
2611 cc_reg_ = eq;
2612}
2613
2614
kasper.lund7276f142008-07-30 08:49:36 +00002615// This should generate code that performs a charCodeAt() call or returns
2616// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
2617// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002618void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasper.lund7276f142008-07-30 08:49:36 +00002619 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00002620 __ mov(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002621 frame_->Push(r0);
kasper.lund7276f142008-07-30 08:49:36 +00002622}
2623
2624
ager@chromium.org7c537e22008-10-16 08:43:32 +00002625void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002626 ASSERT(args->length() == 1);
2627 Load(args->at(0));
2628 Label answer;
2629 // We need the CC bits to come out as not_equal in the case where the
2630 // object is a smi. This can't be done with the usual test opcode so
2631 // we use XOR to get the right CC bits.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002632 frame_->Pop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002633 __ and_(r1, r0, Operand(kSmiTagMask));
2634 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
2635 __ b(ne, &answer);
2636 // It is a heap object - get the map.
2637 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2638 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2639 // Check if the object is a JS array or not.
2640 __ cmp(r1, Operand(JS_ARRAY_TYPE));
2641 __ bind(&answer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 cc_reg_ = eq;
2643}
2644
2645
ager@chromium.org7c537e22008-10-16 08:43:32 +00002646void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 ASSERT(args->length() == 0);
2648
mads.s.ager31e71382008-08-13 09:32:07 +00002649 // Seed the result with the formal parameters count, which will be used
2650 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
2652
2653 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002654 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002656 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657}
2658
2659
ager@chromium.org7c537e22008-10-16 08:43:32 +00002660void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 ASSERT(args->length() == 1);
2662
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002663 // Satisfy contract with ArgumentsAccessStub:
2664 // Load the key into r1 and the formal parameters count into r0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665 Load(args->at(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002666 frame_->Pop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002667 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668
2669 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002670 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671 __ CallStub(&stub);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002672 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673}
2674
2675
ager@chromium.org7c537e22008-10-16 08:43:32 +00002676void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002677 ASSERT(args->length() == 2);
2678
2679 // Load the two objects into registers and perform the comparison.
2680 Load(args->at(0));
2681 Load(args->at(1));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002682 frame_->Pop(r0);
2683 frame_->Pop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002684 __ cmp(r0, Operand(r1));
2685 cc_reg_ = eq;
2686}
2687
2688
ager@chromium.org7c537e22008-10-16 08:43:32 +00002689void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002690 if (CheckForInlineRuntimeCall(node)) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691
2692 ZoneList<Expression*>* args = node->arguments();
2693 Comment cmnt(masm_, "[ CallRuntime");
2694 Runtime::Function* function = node->function();
2695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00002697 // Push the arguments ("left-to-right").
2698 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2699
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 // Call the C runtime function.
2701 __ CallRuntime(function, args->length());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002702 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002703
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00002705 // Prepare stack for calling JS runtime function.
2706 __ mov(r0, Operand(node->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002707 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002708 // Push the builtins object found in the current global object.
2709 __ ldr(r1, GlobalObject());
2710 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002711 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002712
2713 for (int i = 0; i < args->length(); i++) Load(args->at(i));
2714
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 Handle<Code> stub = ComputeCallInitialize(args->length());
ager@chromium.org236ad962008-09-25 09:45:57 +00002717 __ Call(stub, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002718 __ ldr(cp, frame_->Context());
2719 frame_->Pop();
2720 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 }
2722}
2723
2724
ager@chromium.org7c537e22008-10-16 08:43:32 +00002725void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 Comment cmnt(masm_, "[ UnaryOperation");
2727
2728 Token::Value op = node->op();
2729
2730 if (op == Token::NOT) {
2731 LoadCondition(node->expression(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002732 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 false_target(),
2734 true_target(),
2735 true);
2736 cc_reg_ = NegateCondition(cc_reg_);
2737
2738 } else if (op == Token::DELETE) {
2739 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00002740 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 if (property != NULL) {
2742 Load(property->obj());
2743 Load(property->key());
mads.s.ager31e71382008-08-13 09:32:07 +00002744 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002745 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
mads.s.ager31e71382008-08-13 09:32:07 +00002747 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 Slot* slot = variable->slot();
2749 if (variable->is_global()) {
2750 LoadGlobal();
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
2756 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
2757 // lookup the context holding the named variable
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002758 frame_->Push(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002759 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002760 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 __ CallRuntime(Runtime::kLookupContext, 2);
2762 // r0: context
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002763 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002764 __ mov(r0, Operand(variable->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002765 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002767 __ InvokeBuiltin(Builtins::DELETE, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768
mads.s.ager31e71382008-08-13 09:32:07 +00002769 } else {
2770 // Default: Result of deleting non-global, not dynamically
2771 // introduced variables is false.
2772 __ mov(r0, Operand(Factory::false_value()));
2773 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
2775 } else {
2776 // Default: Result of deleting expressions is true.
2777 Load(node->expression()); // may have side-effects
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002778 frame_->Pop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779 __ mov(r0, Operand(Factory::true_value()));
2780 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002781 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782
2783 } else if (op == Token::TYPEOF) {
2784 // Special case for loading the typeof expression; see comment on
2785 // LoadTypeofExpression().
2786 LoadTypeofExpression(node->expression());
2787 __ CallRuntime(Runtime::kTypeof, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002788 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789
2790 } else {
2791 Load(node->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002792 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 switch (op) {
2794 case Token::NOT:
2795 case Token::DELETE:
2796 case Token::TYPEOF:
2797 UNREACHABLE(); // handled above
2798 break;
2799
2800 case Token::SUB: {
2801 UnarySubStub stub;
2802 __ CallStub(&stub);
2803 break;
2804 }
2805
2806 case Token::BIT_NOT: {
2807 // smi check
2808 Label smi_label;
2809 Label continue_label;
2810 __ tst(r0, Operand(kSmiTagMask));
2811 __ b(eq, &smi_label);
2812
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002813 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002814 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002815 __ InvokeBuiltin(Builtins::BIT_NOT, CALL_JS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816
2817 __ b(&continue_label);
2818 __ bind(&smi_label);
2819 __ mvn(r0, Operand(r0));
2820 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
2821 __ bind(&continue_label);
2822 break;
2823 }
2824
2825 case Token::VOID:
2826 // since the stack top is cached in r0, popping and then
2827 // pushing a value can be done by just writing to r0.
2828 __ mov(r0, Operand(Factory::undefined_value()));
2829 break;
2830
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002831 case Token::ADD: {
2832 // Smi check.
2833 Label continue_label;
2834 __ tst(r0, Operand(kSmiTagMask));
2835 __ b(eq, &continue_label);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002836 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002837 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002838 __ InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002839 __ bind(&continue_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002841 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 default:
2843 UNREACHABLE();
2844 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002845 frame_->Push(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002846 }
2847}
2848
2849
ager@chromium.org7c537e22008-10-16 08:43:32 +00002850void CodeGenerator::VisitCountOperation(CountOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 Comment cmnt(masm_, "[ CountOperation");
2852
2853 bool is_postfix = node->is_postfix();
2854 bool is_increment = node->op() == Token::INC;
2855
2856 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
2857 bool is_const = (var != NULL && var->mode() == Variable::CONST);
2858
2859 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00002860 if (is_postfix) {
2861 __ mov(r0, Operand(0));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002862 frame_->Push(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002863 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864
2865 { Reference target(this, node->expression());
2866 if (target.is_illegal()) return;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002867 target.GetValue(NOT_INSIDE_TYPEOF);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002868 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869
2870 Label slow, exit;
2871
2872 // Load the value (1) into register r1.
2873 __ mov(r1, Operand(Smi::FromInt(1)));
2874
2875 // Check for smi operand.
2876 __ tst(r0, Operand(kSmiTagMask));
2877 __ b(ne, &slow);
2878
2879 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002880 if (is_postfix) {
2881 __ str(r0, frame_->Element(target.size()));
2882 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
2884 // Perform optimistic increment/decrement.
2885 if (is_increment) {
2886 __ add(r0, r0, Operand(r1), SetCC);
2887 } else {
2888 __ sub(r0, r0, Operand(r1), SetCC);
2889 }
2890
2891 // If the increment/decrement didn't overflow, we're done.
2892 __ b(vc, &exit);
2893
2894 // Revert optimistic increment/decrement.
2895 if (is_increment) {
2896 __ sub(r0, r0, Operand(r1));
2897 } else {
2898 __ add(r0, r0, Operand(r1));
2899 }
2900
2901 // Slow case: Convert to number.
2902 __ bind(&slow);
2903
2904 // Postfix: Convert the operand to a number and store it as the result.
2905 if (is_postfix) {
2906 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
2907 __ CallStub(&stub);
2908 // Store to result (on the stack).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002909 __ str(r0, frame_->Element(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 }
2911
2912 // Compute the new value by calling the right JavaScript native.
2913 if (is_increment) {
2914 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
2915 __ CallStub(&stub);
2916 } else {
2917 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
2918 __ CallStub(&stub);
2919 }
2920
2921 // Store the new value in the target if not const.
2922 __ bind(&exit);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002923 frame_->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002924 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925 }
2926
2927 // Postfix: Discard the new value and use the old.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002928 if (is_postfix) frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929}
2930
2931
ager@chromium.org7c537e22008-10-16 08:43:32 +00002932void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002933 Comment cmnt(masm_, "[ BinaryOperation");
2934 Token::Value op = node->op();
2935
2936 // According to ECMA-262 section 11.11, page 58, the binary logical
2937 // operators must yield the result of one of the two expressions
2938 // before any ToBoolean() conversions. This means that the value
2939 // produced by a && or || operator is not necessarily a boolean.
2940
2941 // NOTE: If the left hand side produces a materialized value (not in
2942 // the CC register), we force the right hand side to do the
2943 // same. This is necessary because we may have to branch to the exit
2944 // after evaluating the left hand side (due to the shortcut
2945 // semantics), but the compiler must (statically) know if the result
2946 // of compiling the binary operation is materialized or not.
2947
2948 if (op == Token::AND) {
2949 Label is_true;
2950 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002951 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 &is_true,
2953 false_target(),
2954 false);
2955 if (has_cc()) {
2956 Branch(false, false_target());
2957
2958 // Evaluate right side expression.
2959 __ bind(&is_true);
2960 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002961 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 true_target(),
2963 false_target(),
2964 false);
2965
2966 } else {
2967 Label pop_and_continue, exit;
2968
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002969 __ ldr(r0, frame_->Top()); // dup the stack top
2970 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971 // Avoid popping the result if it converts to 'false' using the
2972 // standard ToBoolean() conversion as described in ECMA-262,
2973 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00002974 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 Branch(false, &exit);
2976
2977 // Pop the result of evaluating the first part.
2978 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002979 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980
2981 // Evaluate right side expression.
2982 __ bind(&is_true);
2983 Load(node->right());
2984
2985 // Exit (always with a materialized value).
2986 __ bind(&exit);
2987 }
2988
2989 } else if (op == Token::OR) {
2990 Label is_false;
2991 LoadCondition(node->left(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00002992 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 true_target(),
2994 &is_false,
2995 false);
2996 if (has_cc()) {
2997 Branch(true, true_target());
2998
2999 // Evaluate right side expression.
3000 __ bind(&is_false);
3001 LoadCondition(node->right(),
ager@chromium.org7c537e22008-10-16 08:43:32 +00003002 NOT_INSIDE_TYPEOF,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 true_target(),
3004 false_target(),
3005 false);
3006
3007 } else {
3008 Label pop_and_continue, exit;
3009
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003010 __ ldr(r0, frame_->Top());
3011 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012 // Avoid popping the result if it converts to 'true' using the
3013 // standard ToBoolean() conversion as described in ECMA-262,
3014 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003015 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016 Branch(true, &exit);
3017
3018 // Pop the result of evaluating the first part.
3019 __ bind(&pop_and_continue);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003020 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021
3022 // Evaluate right side expression.
3023 __ bind(&is_false);
3024 Load(node->right());
3025
3026 // Exit (always with a materialized value).
3027 __ bind(&exit);
3028 }
3029
3030 } else {
3031 // Optimize for the case where (at least) one of the expressions
3032 // is a literal small integer.
3033 Literal* lliteral = node->left()->AsLiteral();
3034 Literal* rliteral = node->right()->AsLiteral();
3035
3036 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
3037 Load(node->left());
3038 SmiOperation(node->op(), rliteral->handle(), false);
3039
3040 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
3041 Load(node->right());
3042 SmiOperation(node->op(), lliteral->handle(), true);
3043
3044 } else {
3045 Load(node->left());
3046 Load(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003047 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003049 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 }
3051}
3052
3053
ager@chromium.org7c537e22008-10-16 08:43:32 +00003054void CodeGenerator::VisitThisFunction(ThisFunction* node) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003055 __ ldr(r0, frame_->Function());
3056 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057}
3058
3059
ager@chromium.org7c537e22008-10-16 08:43:32 +00003060void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 Comment cmnt(masm_, "[ CompareOperation");
3062
3063 // Get the expressions from the node.
3064 Expression* left = node->left();
3065 Expression* right = node->right();
3066 Token::Value op = node->op();
3067
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003068 // To make null checks efficient, we check if either left or right is the
3069 // literal 'null'. If so, we optimize the code by inlining a null check
3070 // instead of calling the (very) general runtime routine for checking
3071 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003073 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003074 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003075 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003076 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3077 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 if (left_is_null || right_is_null) {
3079 Load(left_is_null ? right : left);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003080 frame_->Pop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081 __ cmp(r0, Operand(Factory::null_value()));
3082
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003083 // The 'null' value is only equal to 'undefined' if using non-strict
3084 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 if (op != Token::EQ_STRICT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003086 __ b(eq, true_target());
3087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 __ cmp(r0, Operand(Factory::undefined_value()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003089 __ b(eq, true_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003091 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003092 __ b(eq, false_target());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003094 // It can be an undetectable object.
3095 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3096 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3097 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3098 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099 }
3100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 cc_reg_ = eq;
3102 return;
3103 }
3104 }
3105
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003106 // To make typeof testing for natives implemented in JavaScript really
3107 // efficient, we generate special code for expressions of the form:
3108 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 UnaryOperation* operation = left->AsUnaryOperation();
3110 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3111 (operation != NULL && operation->op() == Token::TYPEOF) &&
3112 (right->AsLiteral() != NULL &&
3113 right->AsLiteral()->handle()->IsString())) {
3114 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3115
mads.s.ager31e71382008-08-13 09:32:07 +00003116 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 LoadTypeofExpression(operation->expression());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003118 frame_->Pop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119
3120 if (check->Equals(Heap::number_symbol())) {
3121 __ tst(r1, Operand(kSmiTagMask));
3122 __ b(eq, true_target());
3123 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3124 __ cmp(r1, Operand(Factory::heap_number_map()));
3125 cc_reg_ = eq;
3126
3127 } else if (check->Equals(Heap::string_symbol())) {
3128 __ tst(r1, Operand(kSmiTagMask));
3129 __ b(eq, false_target());
3130
3131 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3132
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003133 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3135 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3136 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3137 __ b(eq, false_target());
3138
3139 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3140 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3141 cc_reg_ = lt;
3142
3143 } else if (check->Equals(Heap::boolean_symbol())) {
3144 __ cmp(r1, Operand(Factory::true_value()));
3145 __ b(eq, true_target());
3146 __ cmp(r1, Operand(Factory::false_value()));
3147 cc_reg_ = eq;
3148
3149 } else if (check->Equals(Heap::undefined_symbol())) {
3150 __ cmp(r1, Operand(Factory::undefined_value()));
3151 __ b(eq, true_target());
3152
3153 __ tst(r1, Operand(kSmiTagMask));
3154 __ b(eq, false_target());
3155
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003156 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3158 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3159 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3160 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3161
3162 cc_reg_ = eq;
3163
3164 } else if (check->Equals(Heap::function_symbol())) {
3165 __ tst(r1, Operand(kSmiTagMask));
3166 __ b(eq, false_target());
3167 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3168 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3169 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3170 cc_reg_ = eq;
3171
3172 } else if (check->Equals(Heap::object_symbol())) {
3173 __ tst(r1, Operand(kSmiTagMask));
3174 __ b(eq, false_target());
3175
3176 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3177 __ cmp(r1, Operand(Factory::null_value()));
3178 __ b(eq, true_target());
3179
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003180 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3182 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3183 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3184 __ b(eq, false_target());
3185
3186 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3187 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3188 __ b(lt, false_target());
3189 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3190 cc_reg_ = le;
3191
3192 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003193 // Uncommon case: typeof testing against a string literal that is
3194 // never returned from the typeof operator.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003195 __ b(false_target());
3196 }
3197 return;
3198 }
3199
3200 Load(left);
3201 Load(right);
3202 switch (op) {
3203 case Token::EQ:
3204 Comparison(eq, false);
3205 break;
3206
3207 case Token::LT:
3208 Comparison(lt);
3209 break;
3210
3211 case Token::GT:
3212 Comparison(gt);
3213 break;
3214
3215 case Token::LTE:
3216 Comparison(le);
3217 break;
3218
3219 case Token::GTE:
3220 Comparison(ge);
3221 break;
3222
3223 case Token::EQ_STRICT:
3224 Comparison(eq, true);
3225 break;
3226
3227 case Token::IN:
mads.s.ager31e71382008-08-13 09:32:07 +00003228 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003229 __ InvokeBuiltin(Builtins::IN, CALL_JS);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003230 frame_->Push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003231 break;
3232
3233 case Token::INSTANCEOF:
mads.s.ager31e71382008-08-13 09:32:07 +00003234 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003235 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003236 __ tst(r0, Operand(r0));
3237 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 break;
3239
3240 default:
3241 UNREACHABLE();
3242 }
3243}
3244
3245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003246#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003247#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248
ager@chromium.org7c537e22008-10-16 08:43:32 +00003249Handle<String> Reference::GetName() {
3250 ASSERT(type_ == NAMED);
3251 Property* property = expression_->AsProperty();
3252 if (property == NULL) {
3253 // Global variable reference treated as a named property reference.
3254 VariableProxy* proxy = expression_->AsVariableProxy();
3255 ASSERT(proxy->AsVariable() != NULL);
3256 ASSERT(proxy->AsVariable()->is_global());
3257 return proxy->name();
3258 } else {
3259 Literal* raw_name = property->key()->AsLiteral();
3260 ASSERT(raw_name != NULL);
3261 return Handle<String>(String::cast(*raw_name->handle()));
3262 }
3263}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003264
ager@chromium.org7c537e22008-10-16 08:43:32 +00003265
3266void Reference::GetValue(TypeofState typeof_state) {
3267 ASSERT(!is_illegal());
3268 ASSERT(!cgen_->has_cc());
3269 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003270 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003271 Property* property = expression_->AsProperty();
3272 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003273 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00003274 }
3275
3276 switch (type_) {
3277 case SLOT: {
3278 Comment cmnt(masm, "[ Load from Slot");
3279 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3280 ASSERT(slot != NULL);
3281 cgen_->LoadFromSlot(slot, typeof_state);
3282 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003283 }
3284
ager@chromium.org7c537e22008-10-16 08:43:32 +00003285 case NAMED: {
3286 // TODO(1241834): Make sure that this it is safe to ignore the
3287 // distinction between expressions in a typeof and not in a typeof. If
3288 // there is a chance that reference errors can be thrown below, we
3289 // must distinguish between the two kinds of loads (typeof expression
3290 // loads must not throw a reference error).
3291 Comment cmnt(masm, "[ Load from named Property");
3292 // Setup the name register.
3293 Handle<String> name(GetName());
3294 __ mov(r2, Operand(name));
3295 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
3296
3297 Variable* var = expression_->AsVariableProxy()->AsVariable();
3298 if (var != NULL) {
3299 ASSERT(var->is_global());
3300 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3301 } else {
3302 __ Call(ic, RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003303 }
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003304 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003305 break;
3306 }
3307
3308 case KEYED: {
3309 // TODO(1241834): Make sure that this it is safe to ignore the
3310 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003311
3312 // TODO(181): Implement inlined version of array indexing once
3313 // loop nesting is properly tracked on ARM.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003314 Comment cmnt(masm, "[ Load from keyed Property");
3315 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003316 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
3317
3318 Variable* var = expression_->AsVariableProxy()->AsVariable();
3319 if (var != NULL) {
3320 ASSERT(var->is_global());
3321 __ Call(ic, RelocInfo::CODE_TARGET_CONTEXT);
3322 } else {
3323 __ Call(ic, RelocInfo::CODE_TARGET);
3324 }
3325 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003326 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003327 }
3328
3329 default:
3330 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003331 }
3332}
3333
3334
ager@chromium.org7c537e22008-10-16 08:43:32 +00003335void Reference::SetValue(InitState init_state) {
3336 ASSERT(!is_illegal());
3337 ASSERT(!cgen_->has_cc());
3338 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003339 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00003340 Property* property = expression_->AsProperty();
3341 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003342 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003343 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003344
ager@chromium.org7c537e22008-10-16 08:43:32 +00003345 switch (type_) {
3346 case SLOT: {
3347 Comment cmnt(masm, "[ Store to Slot");
3348 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
3349 ASSERT(slot != NULL);
3350 if (slot->type() == Slot::LOOKUP) {
3351 ASSERT(slot->var()->mode() == Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003352
ager@chromium.org7c537e22008-10-16 08:43:32 +00003353 // For now, just do a runtime call.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003354 frame->Push(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003355 __ mov(r0, Operand(slot->var()->name()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003356 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003357
ager@chromium.org7c537e22008-10-16 08:43:32 +00003358 if (init_state == CONST_INIT) {
3359 // Same as the case for a normal store, but ignores attribute
3360 // (e.g. READ_ONLY) of context slot so that we can initialize
3361 // const properties (introduced via eval("const foo = (some
3362 // expr);")). Also, uses the current function context instead of
3363 // the top context.
3364 //
3365 // Note that we must declare the foo upon entry of eval(), via a
3366 // context slot declaration, but we cannot initialize it at the
3367 // same time, because the const declaration may be at the end of
3368 // the eval code (sigh...) and the const variable may have been
3369 // used before (where its value is 'undefined'). Thus, we can only
3370 // do the initialization when we actually encounter the expression
3371 // and when the expression operands are defined and valid, and
3372 // thus we need the split into 2 operations: declaration of the
3373 // context slot followed by initialization.
3374 __ CallRuntime(Runtime::kInitializeConstContextSlot, 3);
3375 } else {
3376 __ CallRuntime(Runtime::kStoreContextSlot, 3);
3377 }
3378 // Storing a variable must keep the (new) value on the expression
3379 // stack. This is necessary for compiling assignment expressions.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003380 frame->Push(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003381
ager@chromium.org7c537e22008-10-16 08:43:32 +00003382 } else {
3383 ASSERT(slot->var()->mode() != Variable::DYNAMIC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003384
ager@chromium.org7c537e22008-10-16 08:43:32 +00003385 Label exit;
3386 if (init_state == CONST_INIT) {
3387 ASSERT(slot->var()->mode() == Variable::CONST);
3388 // Only the first const initialization must be executed (the slot
3389 // still contains 'the hole' value). When the assignment is
3390 // executed, the code is identical to a normal store (see below).
3391 Comment cmnt(masm, "[ Init const");
3392 __ ldr(r2, cgen_->SlotOperand(slot, r2));
3393 __ cmp(r2, Operand(Factory::the_hole_value()));
3394 __ b(ne, &exit);
3395 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003396
ager@chromium.org7c537e22008-10-16 08:43:32 +00003397 // We must execute the store. Storing a variable must keep the
3398 // (new) value on the stack. This is necessary for compiling
3399 // assignment expressions.
3400 //
3401 // Note: We will reach here even with slot->var()->mode() ==
3402 // Variable::CONST because of const declarations which will
3403 // initialize consts to 'the hole' value and by doing so, end up
3404 // calling this code. r2 may be loaded with context; used below in
3405 // RecordWrite.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003406 frame->Pop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003407 __ str(r0, cgen_->SlotOperand(slot, r2));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003408 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003409 if (slot->type() == Slot::CONTEXT) {
3410 // Skip write barrier if the written value is a smi.
3411 __ tst(r0, Operand(kSmiTagMask));
3412 __ b(eq, &exit);
3413 // r2 is loaded with context when calling SlotOperand above.
3414 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
3415 __ mov(r3, Operand(offset));
3416 __ RecordWrite(r2, r3, r1);
3417 }
3418 // If we definitely did not jump over the assignment, we do not need
3419 // to bind the exit label. Doing so can defeat peephole
3420 // optimization.
3421 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
3422 __ bind(&exit);
3423 }
3424 }
3425 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003426 }
3427
ager@chromium.org7c537e22008-10-16 08:43:32 +00003428 case NAMED: {
3429 Comment cmnt(masm, "[ Store to named Property");
3430 // Call the appropriate IC code.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003431 frame->Pop(r0); // value
ager@chromium.org7c537e22008-10-16 08:43:32 +00003432 // Setup the name register.
3433 Handle<String> name(GetName());
3434 __ mov(r2, Operand(name));
3435 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3436 __ Call(ic, RelocInfo::CODE_TARGET);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003437 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003438 break;
3439 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003440
ager@chromium.org7c537e22008-10-16 08:43:32 +00003441 case KEYED: {
3442 Comment cmnt(masm, "[ Store to keyed Property");
3443 Property* property = expression_->AsProperty();
3444 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003445 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003446
3447 // Call IC code.
3448 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
3449 // TODO(1222589): Make the IC grab the values from the stack.
3450 frame->Pop(r0); // value
3451 __ Call(ic, RelocInfo::CODE_TARGET);
3452 frame->Push(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003453 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003454 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00003455
3456 default:
3457 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003458 }
3459}
3460
3461
3462void GetPropertyStub::Generate(MacroAssembler* masm) {
3463 // sp[0]: key
3464 // sp[1]: receiver
3465 Label slow, fast;
3466 // Get the key and receiver object from the stack.
3467 __ ldm(ia, sp, r0.bit() | r1.bit());
3468 // Check that the key is a smi.
3469 __ tst(r0, Operand(kSmiTagMask));
3470 __ b(ne, &slow);
3471 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3472 // Check that the object isn't a smi.
3473 __ tst(r1, Operand(kSmiTagMask));
3474 __ b(eq, &slow);
3475
3476 // Check that the object is some kind of JS object EXCEPT JS Value type.
3477 // In the case that the object is a value-wrapper object,
3478 // we enter the runtime system to make sure that indexing into string
3479 // objects work as intended.
3480 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
3481 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3482 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3483 __ cmp(r2, Operand(JS_OBJECT_TYPE));
3484 __ b(lt, &slow);
3485
3486 // Get the elements array of the object.
3487 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
3488 // Check that the object is in fast mode (not dictionary).
3489 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
3490 __ cmp(r3, Operand(Factory::hash_table_map()));
3491 __ b(eq, &slow);
3492 // Check that the key (index) is within bounds.
3493 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
3494 __ cmp(r0, Operand(r3));
3495 __ b(lo, &fast);
3496
3497 // Slow case: Push extra copies of the arguments (2).
3498 __ bind(&slow);
3499 __ ldm(ia, sp, r0.bit() | r1.bit());
3500 __ stm(db_w, sp, r0.bit() | r1.bit());
3501 // Do tail-call to runtime routine.
3502 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
3503
3504 // Fast case: Do the load.
3505 __ bind(&fast);
3506 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
3507 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
3508 __ cmp(r0, Operand(Factory::the_hole_value()));
3509 // In case the loaded value is the_hole we have to consult GetProperty
3510 // to ensure the prototype chain is searched.
3511 __ b(eq, &slow);
3512
3513 __ StubReturn(1);
3514}
3515
3516
3517void SetPropertyStub::Generate(MacroAssembler* masm) {
3518 // r0 : value
3519 // sp[0] : key
3520 // sp[1] : receiver
3521
3522 Label slow, fast, array, extra, exit;
3523 // Get the key and the object from the stack.
3524 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
3525 // Check that the key is a smi.
3526 __ tst(r1, Operand(kSmiTagMask));
3527 __ b(ne, &slow);
3528 // Check that the object isn't a smi.
3529 __ tst(r3, Operand(kSmiTagMask));
3530 __ b(eq, &slow);
3531 // Get the type of the object from its map.
3532 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3533 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3534 // Check if the object is a JS array or not.
3535 __ cmp(r2, Operand(JS_ARRAY_TYPE));
3536 __ b(eq, &array);
3537 // Check that the object is some kind of JS object.
3538 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
3539 __ b(lt, &slow);
3540
3541
3542 // Object case: Check key against length in the elements array.
3543 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
3544 // Check that the object is in fast mode (not dictionary).
3545 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3546 __ cmp(r2, Operand(Factory::hash_table_map()));
3547 __ b(eq, &slow);
3548 // Untag the key (for checking against untagged length in the fixed array).
3549 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3550 // Compute address to store into and check array bounds.
3551 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
3552 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
3553 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
3554 __ cmp(r1, Operand(ip));
3555 __ b(lo, &fast);
3556
3557
3558 // Slow case: Push extra copies of the arguments (3).
3559 __ bind(&slow);
3560 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
3561 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
3562 // Do tail-call to runtime routine.
3563 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
3564
3565
3566 // Extra capacity case: Check if there is extra capacity to
3567 // perform the store and update the length. Used for adding one
3568 // element to the array by writing to array[array.length].
3569 // r0 == value, r1 == key, r2 == elements, r3 == object
3570 __ bind(&extra);
3571 __ b(ne, &slow); // do not leave holes in the array
3572 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
3573 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
3574 __ cmp(r1, Operand(ip));
3575 __ b(hs, &slow);
3576 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
3577 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
3578 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
3579 __ mov(r3, Operand(r2));
3580 // NOTE: Computing the address to store into must take the fact
3581 // that the key has been incremented into account.
3582 int displacement = Array::kHeaderSize - kHeapObjectTag -
3583 ((1 << kSmiTagSize) * 2);
3584 __ add(r2, r2, Operand(displacement));
3585 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3586 __ b(&fast);
3587
3588
3589 // Array case: Get the length and the elements array from the JS
3590 // array. Check that the array is in fast mode; if it is the
3591 // length is always a smi.
3592 // r0 == value, r3 == object
3593 __ bind(&array);
3594 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
3595 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
3596 __ cmp(r1, Operand(Factory::hash_table_map()));
3597 __ b(eq, &slow);
3598
3599 // Check the key against the length in the array, compute the
3600 // address to store into and fall through to fast case.
3601 __ ldr(r1, MemOperand(sp));
3602 // r0 == value, r1 == key, r2 == elements, r3 == object.
3603 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
3604 __ cmp(r1, Operand(ip));
3605 __ b(hs, &extra);
3606 __ mov(r3, Operand(r2));
3607 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
3608 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3609
3610
3611 // Fast case: Do the store.
3612 // r0 == value, r2 == address to store into, r3 == elements
3613 __ bind(&fast);
3614 __ str(r0, MemOperand(r2));
3615 // Skip write barrier if the written value is a smi.
3616 __ tst(r0, Operand(kSmiTagMask));
3617 __ b(eq, &exit);
3618 // Update write barrier for the elements array address.
3619 __ sub(r1, r2, Operand(r3));
3620 __ RecordWrite(r3, r1, r2);
3621 __ bind(&exit);
3622 __ StubReturn(1);
3623}
3624
3625
3626void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
3627 // r1 : x
3628 // r0 : y
3629 // result : r0
3630
3631 switch (op_) {
3632 case Token::ADD: {
3633 Label slow, exit;
3634 // fast path
3635 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3636 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
3637 // go slow-path in case of overflow
3638 __ b(vs, &slow);
3639 // go slow-path in case of non-smi operands
3640 ASSERT(kSmiTag == 0); // adjust code below
3641 __ tst(r2, Operand(kSmiTagMask));
3642 __ b(eq, &exit);
3643 // slow path
3644 __ bind(&slow);
3645 __ sub(r0, r0, Operand(r1)); // revert optimistic add
3646 __ push(r1);
3647 __ push(r0);
3648 __ mov(r0, Operand(1)); // set number of arguments
3649 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3650 // done
3651 __ bind(&exit);
3652 break;
3653 }
3654
3655 case Token::SUB: {
3656 Label slow, exit;
3657 // fast path
3658 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3659 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
3660 // go slow-path in case of overflow
3661 __ b(vs, &slow);
3662 // go slow-path in case of non-smi operands
3663 ASSERT(kSmiTag == 0); // adjust code below
3664 __ tst(r2, Operand(kSmiTagMask));
3665 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
3666 __ b(eq, &exit);
3667 // slow path
3668 __ bind(&slow);
3669 __ push(r1);
3670 __ push(r0);
3671 __ mov(r0, Operand(1)); // set number of arguments
3672 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3673 // done
3674 __ bind(&exit);
3675 break;
3676 }
3677
3678 case Token::MUL: {
3679 Label slow, exit;
3680 // tag check
3681 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3682 ASSERT(kSmiTag == 0); // adjust code below
3683 __ tst(r2, Operand(kSmiTagMask));
3684 __ b(ne, &slow);
3685 // remove tag from one operand (but keep sign), so that result is smi
3686 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
3687 // do multiplication
3688 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
3689 // go slow on overflows (overflow bit is not set)
3690 __ mov(ip, Operand(r3, ASR, 31));
3691 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
3692 __ b(ne, &slow);
3693 // go slow on zero result to handle -0
3694 __ tst(r3, Operand(r3));
3695 __ mov(r0, Operand(r3), LeaveCC, ne);
3696 __ b(ne, &exit);
3697 // slow case
3698 __ bind(&slow);
3699 __ push(r1);
3700 __ push(r0);
3701 __ mov(r0, Operand(1)); // set number of arguments
3702 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3703 // done
3704 __ bind(&exit);
3705 break;
3706 }
3707
3708 case Token::BIT_OR:
3709 case Token::BIT_AND:
3710 case Token::BIT_XOR: {
3711 Label slow, exit;
3712 // tag check
3713 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3714 ASSERT(kSmiTag == 0); // adjust code below
3715 __ tst(r2, Operand(kSmiTagMask));
3716 __ b(ne, &slow);
3717 switch (op_) {
3718 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
3719 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
3720 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
3721 default: UNREACHABLE();
3722 }
3723 __ b(&exit);
3724 __ bind(&slow);
3725 __ push(r1); // restore stack
3726 __ push(r0);
3727 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3728 switch (op_) {
3729 case Token::BIT_OR:
3730 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3731 break;
3732 case Token::BIT_AND:
3733 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3734 break;
3735 case Token::BIT_XOR:
3736 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3737 break;
3738 default:
3739 UNREACHABLE();
3740 }
3741 __ bind(&exit);
3742 break;
3743 }
3744
3745 case Token::SHL:
3746 case Token::SHR:
3747 case Token::SAR: {
3748 Label slow, exit;
3749 // tag check
3750 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
3751 ASSERT(kSmiTag == 0); // adjust code below
3752 __ tst(r2, Operand(kSmiTagMask));
3753 __ b(ne, &slow);
3754 // remove tags from operands (but keep sign)
3755 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
3756 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
3757 // use only the 5 least significant bits of the shift count
3758 __ and_(r2, r2, Operand(0x1f));
3759 // perform operation
3760 switch (op_) {
3761 case Token::SAR:
3762 __ mov(r3, Operand(r3, ASR, r2));
3763 // no checks of result necessary
3764 break;
3765
3766 case Token::SHR:
3767 __ mov(r3, Operand(r3, LSR, r2));
3768 // check that the *unsigned* result fits in a smi
3769 // neither of the two high-order bits can be set:
3770 // - 0x80000000: high bit would be lost when smi tagging
3771 // - 0x40000000: this number would convert to negative when
3772 // smi tagging these two cases can only happen with shifts
3773 // by 0 or 1 when handed a valid smi
3774 __ and_(r2, r3, Operand(0xc0000000), SetCC);
3775 __ b(ne, &slow);
3776 break;
3777
3778 case Token::SHL:
3779 __ mov(r3, Operand(r3, LSL, r2));
3780 // check that the *signed* result fits in a smi
3781 __ add(r2, r3, Operand(0x40000000), SetCC);
3782 __ b(mi, &slow);
3783 break;
3784
3785 default: UNREACHABLE();
3786 }
3787 // tag result and store it in r0
3788 ASSERT(kSmiTag == 0); // adjust code below
3789 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
3790 __ b(&exit);
3791 // slow case
3792 __ bind(&slow);
3793 __ push(r1); // restore stack
3794 __ push(r0);
3795 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
3796 switch (op_) {
3797 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
3798 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
3799 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
3800 default: UNREACHABLE();
3801 }
3802 __ bind(&exit);
3803 break;
3804 }
3805
3806 default: UNREACHABLE();
3807 }
3808 __ Ret();
3809}
3810
3811
3812void StackCheckStub::Generate(MacroAssembler* masm) {
3813 Label within_limit;
3814 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
3815 __ ldr(ip, MemOperand(ip));
3816 __ cmp(sp, Operand(ip));
3817 __ b(hs, &within_limit);
3818 // Do tail-call to runtime routine.
3819 __ push(r0);
3820 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
3821 __ bind(&within_limit);
3822
3823 __ StubReturn(1);
3824}
3825
3826
3827void UnarySubStub::Generate(MacroAssembler* masm) {
3828 Label undo;
3829 Label slow;
3830 Label done;
3831
3832 // Enter runtime system if the value is not a smi.
3833 __ tst(r0, Operand(kSmiTagMask));
3834 __ b(ne, &slow);
3835
3836 // Enter runtime system if the value of the expression is zero
3837 // to make sure that we switch between 0 and -0.
3838 __ cmp(r0, Operand(0));
3839 __ b(eq, &slow);
3840
3841 // The value of the expression is a smi that is not zero. Try
3842 // optimistic subtraction '0 - value'.
3843 __ rsb(r1, r0, Operand(0), SetCC);
3844 __ b(vs, &slow);
3845
3846 // If result is a smi we are done.
3847 __ tst(r1, Operand(kSmiTagMask));
3848 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
3849 __ b(eq, &done);
3850
3851 // Enter runtime system.
3852 __ bind(&slow);
3853 __ push(r0);
3854 __ mov(r0, Operand(0)); // set number of arguments
3855 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3856
3857 __ bind(&done);
3858 __ StubReturn(1);
3859}
3860
3861
3862void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
3863 __ push(r0);
3864 __ mov(r0, Operand(0)); // set number of arguments
3865 switch (kind_) {
3866 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
3867 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
3868 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
3869 default: UNREACHABLE();
3870 }
3871 __ StubReturn(argc_);
3872}
3873
3874
3875void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3876 // r0 holds exception
3877 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
3878 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3879 __ ldr(sp, MemOperand(r3));
3880 __ pop(r2); // pop next in chain
3881 __ str(r2, MemOperand(r3));
3882 // restore parameter- and frame-pointer and pop state.
3883 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
3884 // Before returning we restore the context from the frame pointer if not NULL.
3885 // The frame pointer is NULL in the exception handler of a JS entry frame.
3886 __ cmp(fp, Operand(0));
3887 // Set cp to NULL if fp is NULL.
3888 __ mov(cp, Operand(0), LeaveCC, eq);
3889 // Restore cp otherwise.
3890 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3891 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3892 __ pop(pc);
3893}
3894
3895
3896void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
3897 // Fetch top stack handler.
3898 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
3899 __ ldr(r3, MemOperand(r3));
3900
3901 // Unwind the handlers until the ENTRY handler is found.
3902 Label loop, done;
3903 __ bind(&loop);
3904 // Load the type of the current stack handler.
3905 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
3906 StackHandlerConstants::kStateOffset;
3907 __ ldr(r2, MemOperand(r3, kStateOffset));
3908 __ cmp(r2, Operand(StackHandler::ENTRY));
3909 __ b(eq, &done);
3910 // Fetch the next handler in the list.
3911 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
3912 StackHandlerConstants::kNextOffset;
3913 __ ldr(r3, MemOperand(r3, kNextOffset));
3914 __ jmp(&loop);
3915 __ bind(&done);
3916
3917 // Set the top handler address to next handler past the current ENTRY handler.
3918 __ ldr(r0, MemOperand(r3, kNextOffset));
3919 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
3920 __ str(r0, MemOperand(r2));
3921
3922 // Set external caught exception to false.
3923 __ mov(r0, Operand(false));
3924 ExternalReference external_caught(Top::k_external_caught_exception_address);
3925 __ mov(r2, Operand(external_caught));
3926 __ str(r0, MemOperand(r2));
3927
3928 // Set pending exception and r0 to out of memory exception.
3929 Failure* out_of_memory = Failure::OutOfMemoryException();
3930 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3931 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
3932 __ str(r0, MemOperand(r2));
3933
3934 // Restore the stack to the address of the ENTRY handler
3935 __ mov(sp, Operand(r3));
3936
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003937 // Stack layout at this point. See also PushTryHandler
3938 // r3, sp -> next handler
3939 // state (ENTRY)
3940 // pp
3941 // fp
3942 // lr
3943
3944 // Discard ENTRY state (r2 is not used), and restore parameter-
3945 // and frame-pointer and pop state.
3946 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003947 // Before returning we restore the context from the frame pointer if not NULL.
3948 // The frame pointer is NULL in the exception handler of a JS entry frame.
3949 __ cmp(fp, Operand(0));
3950 // Set cp to NULL if fp is NULL.
3951 __ mov(cp, Operand(0), LeaveCC, eq);
3952 // Restore cp otherwise.
3953 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
3954 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
3955 __ pop(pc);
3956}
3957
3958
3959void CEntryStub::GenerateCore(MacroAssembler* masm,
3960 Label* throw_normal_exception,
3961 Label* throw_out_of_memory_exception,
3962 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003963 bool do_gc,
3964 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003965 // r0: result parameter for PerformGC, if any
3966 // r4: number of arguments including receiver (C callee-saved)
3967 // r5: pointer to builtin function (C callee-saved)
3968 // r6: pointer to the first argument (C callee-saved)
3969
3970 if (do_gc) {
3971 // Passing r0.
3972 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3973 }
3974
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003975 ExternalReference scope_depth =
3976 ExternalReference::heap_always_allocate_scope_depth();
3977 if (always_allocate) {
3978 __ mov(r0, Operand(scope_depth));
3979 __ ldr(r1, MemOperand(r0));
3980 __ add(r1, r1, Operand(1));
3981 __ str(r1, MemOperand(r0));
3982 }
3983
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003984 // Call C built-in.
3985 // r0 = argc, r1 = argv
3986 __ mov(r0, Operand(r4));
3987 __ mov(r1, Operand(r6));
3988
3989 // TODO(1242173): To let the GC traverse the return address of the exit
3990 // frames, we need to know where the return address is. Right now,
3991 // we push it on the stack to be able to find it again, but we never
3992 // restore from it in case of changes, which makes it impossible to
3993 // support moving the C entry code stub. This should be fixed, but currently
3994 // this is OK because the CEntryStub gets generated so early in the V8 boot
3995 // sequence that it is not moving ever.
3996 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
3997 __ push(lr);
3998#if !defined(__arm__)
3999 // Notify the simulator of the transition to C code.
4000 __ swi(assembler::arm::call_rt_r5);
4001#else /* !defined(__arm__) */
4002 __ mov(pc, Operand(r5));
4003#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004004
4005 if (always_allocate) {
4006 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4007 // though (contain the result).
4008 __ mov(r2, Operand(scope_depth));
4009 __ ldr(r3, MemOperand(r2));
4010 __ sub(r3, r3, Operand(1));
4011 __ str(r3, MemOperand(r2));
4012 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004013
4014 // check for failure result
4015 Label failure_returned;
4016 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4017 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4018 __ add(r2, r0, Operand(1));
4019 __ tst(r2, Operand(kFailureTagMask));
4020 __ b(eq, &failure_returned);
4021
4022 // Exit C frame and return.
4023 // r0:r1: result
4024 // sp: stack pointer
4025 // fp: frame pointer
4026 // pp: caller's parameter pointer pp (restored as C callee-saved)
4027 __ LeaveExitFrame(frame_type);
4028
4029 // check if we should retry or throw exception
4030 Label retry;
4031 __ bind(&failure_returned);
4032 ASSERT(Failure::RETRY_AFTER_GC == 0);
4033 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4034 __ b(eq, &retry);
4035
4036 Label continue_exception;
4037 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4038 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4039 __ b(ne, &continue_exception);
4040
4041 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004042 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004043 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004044 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004045 __ ldr(r0, MemOperand(ip));
4046 __ str(r3, MemOperand(ip));
4047
4048 __ bind(&continue_exception);
4049 // Special handling of out of memory exception.
4050 Failure* out_of_memory = Failure::OutOfMemoryException();
4051 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4052 __ b(eq, throw_out_of_memory_exception);
4053
4054 // Handle normal exception.
4055 __ jmp(throw_normal_exception);
4056
4057 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4058}
4059
4060
4061void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4062 // Called from JavaScript; parameters are on stack as if calling JS function
4063 // r0: number of arguments including receiver
4064 // r1: pointer to builtin function
4065 // fp: frame pointer (restored after C call)
4066 // sp: stack pointer (restored as callee's pp after C call)
4067 // cp: current context (C callee-saved)
4068 // pp: caller's parameter pointer pp (C callee-saved)
4069
4070 // NOTE: Invocations of builtins may return failure objects
4071 // instead of a proper result. The builtin entry handles
4072 // this by performing a garbage collection and retrying the
4073 // builtin once.
4074
4075 StackFrame::Type frame_type = is_debug_break
4076 ? StackFrame::EXIT_DEBUG
4077 : StackFrame::EXIT;
4078
4079 // Enter the exit frame that transitions from JavaScript to C++.
4080 __ EnterExitFrame(frame_type);
4081
4082 // r4: number of arguments (C callee-saved)
4083 // r5: pointer to builtin function (C callee-saved)
4084 // r6: pointer to first argument (C callee-saved)
4085
4086 Label throw_out_of_memory_exception;
4087 Label throw_normal_exception;
4088
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004089 // Call into the runtime system. Collect garbage before the call if
4090 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004091 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004092 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004093 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4094 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004095 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004096 &throw_out_of_memory_exception,
4097 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004098 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004099 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004100
4101 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004102 GenerateCore(masm,
4103 &throw_normal_exception,
4104 &throw_out_of_memory_exception,
4105 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004106 true,
4107 false);
4108
4109 // Do full GC and retry runtime call one final time.
4110 Failure* failure = Failure::InternalError();
4111 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4112 GenerateCore(masm,
4113 &throw_normal_exception,
4114 &throw_out_of_memory_exception,
4115 frame_type,
4116 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004117 true);
4118
4119 __ bind(&throw_out_of_memory_exception);
4120 GenerateThrowOutOfMemory(masm);
4121 // control flow for generated will not return.
4122
4123 __ bind(&throw_normal_exception);
4124 GenerateThrowTOS(masm);
4125}
4126
4127
4128void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4129 // r0: code entry
4130 // r1: function
4131 // r2: receiver
4132 // r3: argc
4133 // [sp+0]: argv
4134
4135 Label invoke, exit;
4136
4137 // Called from C, so do not pop argc and args on exit (preserve sp)
4138 // No need to save register-passed args
4139 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4140 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4141
4142 // Get address of argv, see stm above.
4143 // r0: code entry
4144 // r1: function
4145 // r2: receiver
4146 // r3: argc
4147 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4148 __ ldr(r4, MemOperand(r4)); // argv
4149
4150 // Push a frame with special values setup to mark it as an entry frame.
4151 // r0: code entry
4152 // r1: function
4153 // r2: receiver
4154 // r3: argc
4155 // r4: argv
4156 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4157 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4158 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4159 __ mov(r6, Operand(Smi::FromInt(marker)));
4160 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4161 __ ldr(r5, MemOperand(r5));
4162 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4163
4164 // Setup frame pointer for the frame to be pushed.
4165 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4166
4167 // Call a faked try-block that does the invoke.
4168 __ bl(&invoke);
4169
4170 // Caught exception: Store result (exception) in the pending
4171 // exception field in the JSEnv and return a failure sentinel.
4172 // Coming in here the fp will be invalid because the PushTryHandler below
4173 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004174 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004175 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004176 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004177 __ b(&exit);
4178
4179 // Invoke: Link this frame into the handler chain.
4180 __ bind(&invoke);
4181 // Must preserve r0-r4, r5-r7 are available.
4182 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4183 // If an exception not caught by another handler occurs, this handler returns
4184 // control to the code after the bl(&invoke) above, which restores all
4185 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4186 // before returning a failure to C.
4187
4188 // Clear any pending exceptions.
4189 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4190 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004191 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004192 __ str(r5, MemOperand(ip));
4193
4194 // Invoke the function by calling through JS entry trampoline builtin.
4195 // Notice that we cannot store a reference to the trampoline code directly in
4196 // this stub, because runtime stubs are not traversed when doing GC.
4197
4198 // Expected registers by Builtins::JSEntryTrampoline
4199 // r0: code entry
4200 // r1: function
4201 // r2: receiver
4202 // r3: argc
4203 // r4: argv
4204 if (is_construct) {
4205 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4206 __ mov(ip, Operand(construct_entry));
4207 } else {
4208 ExternalReference entry(Builtins::JSEntryTrampoline);
4209 __ mov(ip, Operand(entry));
4210 }
4211 __ ldr(ip, MemOperand(ip)); // deref address
4212
4213 // Branch and link to JSEntryTrampoline
4214 __ mov(lr, Operand(pc));
4215 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4216
4217 // Unlink this frame from the handler chain. When reading the
4218 // address of the next handler, there is no need to use the address
4219 // displacement since the current stack pointer (sp) points directly
4220 // to the stack handler.
4221 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
4222 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
4223 __ str(r3, MemOperand(ip));
4224 // No need to restore registers
4225 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4226
4227 __ bind(&exit); // r0 holds result
4228 // Restore the top frame descriptors from the stack.
4229 __ pop(r3);
4230 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4231 __ str(r3, MemOperand(ip));
4232
4233 // Reset the stack to the callee saved registers.
4234 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4235
4236 // Restore callee-saved registers and return.
4237#ifdef DEBUG
4238 if (FLAG_debug_code) __ mov(lr, Operand(pc));
4239#endif
4240 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4241}
4242
4243
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004245 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004246 Label adaptor;
4247 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4248 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4249 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004250 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004251
ager@chromium.org7c537e22008-10-16 08:43:32 +00004252 // Nothing to do: The formal number of parameters has already been
4253 // passed in register r0 by calling function. Just return it.
4254 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004255
ager@chromium.org7c537e22008-10-16 08:43:32 +00004256 // Arguments adaptor case: Read the arguments length from the
4257 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004258 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004259 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004260 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004261}
4262
4263
ager@chromium.org7c537e22008-10-16 08:43:32 +00004264void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4265 // The displacement is the offset of the last parameter (if any)
4266 // relative to the frame pointer.
4267 static const int kDisplacement =
4268 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004269
ager@chromium.org7c537e22008-10-16 08:43:32 +00004270 // Check that the key is a smi.
4271 Label slow;
4272 __ tst(r1, Operand(kSmiTagMask));
4273 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004274
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275 // Check if the calling frame is an arguments adaptor frame.
4276 Label adaptor;
4277 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4278 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4279 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4280 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004281
ager@chromium.org7c537e22008-10-16 08:43:32 +00004282 // Check index against formal parameters count limit passed in
4283 // through register eax. Use unsigned comparison to get negative
4284 // check for free.
4285 __ cmp(r1, r0);
4286 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004287
ager@chromium.org7c537e22008-10-16 08:43:32 +00004288 // Read the argument from the stack and return it.
4289 __ sub(r3, r0, r1);
4290 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4291 __ ldr(r0, MemOperand(r3, kDisplacement));
4292 __ mov(pc, lr);
4293
4294 // Arguments adaptor case: Check index against actual arguments
4295 // limit found in the arguments adaptor frame. Use unsigned
4296 // comparison to get negative check for free.
4297 __ bind(&adaptor);
4298 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4299 __ cmp(r1, r0);
4300 __ b(cs, &slow);
4301
4302 // Read the argument from the adaptor frame and return it.
4303 __ sub(r3, r0, r1);
4304 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4305 __ ldr(r0, MemOperand(r3, kDisplacement));
4306 __ mov(pc, lr);
4307
4308 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4309 // by calling the runtime system.
4310 __ bind(&slow);
4311 __ push(r1);
4312 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
4313}
4314
4315
4316void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4317 // Check if the calling frame is an arguments adaptor frame.
4318 Label runtime;
4319 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4320 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4321 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
4322 __ b(ne, &runtime);
4323
4324 // Patch the arguments.length and the parameters pointer.
4325 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4326 __ str(r0, MemOperand(sp, 0 * kPointerSize));
4327 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4328 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4329 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4330
4331 // Do the runtime call to allocate the arguments object.
4332 __ bind(&runtime);
4333 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004334}
4335
4336
4337void CallFunctionStub::Generate(MacroAssembler* masm) {
4338 Label slow;
4339 // Get the function to call from the stack.
4340 // function, receiver [, arguments]
4341 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4342
4343 // Check that the function is really a JavaScript function.
4344 // r1: pushed function (to be verified)
4345 __ tst(r1, Operand(kSmiTagMask));
4346 __ b(eq, &slow);
4347 // Get the map of the function object.
4348 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4349 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4350 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
4351 __ b(ne, &slow);
4352
4353 // Fast-case: Invoke the function now.
4354 // r1: pushed function
4355 ParameterCount actual(argc_);
4356 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4357
4358 // Slow-case: Non-function called.
4359 __ bind(&slow);
4360 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004361 __ mov(r2, Operand(0));
4362 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
4363 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
4364 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004365}
4366
4367
4368#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004369
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004370} } // namespace v8::internal